📄 filedlg.c
字号:
/*** $Id: filedlg.c,v 1.6 2003/12/04 09:43:41 weiym Exp $** ** filedlg.c: Open File Dialog.** ** Copyright (C) 2003 Feynman Software.** Copyright (C) 2001 ~ 2002 Wei Yongming and others.**** Current maintainer: Leon**** Create date: 2000.xx.xx*//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//* ------------------------------------------------------ ** This FileDialog is based on the one wrote by FrankXM** and Modified greatly by leon** -------------------------------------------------------** main changes:** -------------------------------------------------------** 1, change the layout of dialog** 2, add the 'wildchar' matching ** and change other parts accordingly*//*** TODO:*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>#include <pwd.h>#include <errno.h>#include "common.h"#include "minigui.h"#include "gdi.h"#include "window.h"#include "control.h"#include "filedlg.h"#define IDC_UP 510#define IDC_STATIC1 520#define IDC_STATIC2 522#define IDC_DIRCHOISE 530#define IDC_FILECHOISE 540#define IDC_FILENAME 550#define IDC_PATH 600static int fitquestion (char *ptn, char *target);// filter begin /////////////////////////////////////////////////////// match after a '*'static int fitstar (char *ptn, char *target){ char a[50]; char *str = a; int i, j, k, dismatch; for (i=0; (ptn[i] != '*' && ptn[i] != '?' && ptn[i] != '\0'); i++) str[i] = ptn[i]; if (i ==0) { //'\0' ok! if (ptn[0] == '\0') return 0; if (ptn[0] == '*') return fitstar (ptn +1, target); //'?..' if (ptn[0] == '?') { if (target[0] == '\0') return 2; else return fitstar (ptn+1, target+1); } } //otherwise give it a 'end' str[i] ='\0'; j = 0; while (target[j+i-1] != '\0') { dismatch = 0; for (k=0; k<i; k++) if (str[k] != target[j+k]) { dismatch = 1; break; } if (dismatch){ j++; continue; } if (ptn[i] == '\0') { if (target[i+j] == '\0') return 0; else { j++; continue; } } if (ptn[i] == '*') { if (fitstar (ptn+i+1, target+j+k) == 0) return 0; }else if (ptn[i] == '?') { if (target[j+k] == '\0') return 2; else if (ptn[i+1] == '\0') { if (target[j+k+1] == '\0') return 0; else //ptn shorter than target, and the forepart is fit return 1; }else if (fitquestion (ptn+i+1, target+j+k+1) == 0) return 0; } j++; } return 2;}// match after a '?'static int fitquestion (char *ptn, char *target){ char a[50]; char *str = a; int i, j; for (i=0; (ptn[i] != '*' && ptn[i] != '?' && ptn[i] != '\0'); i++) str[i] = ptn[i]; if (i ==0) { //'\0' ok! if (ptn[0] == '\0') { if (target[0] == '\0') return 0; else //ptn not long enough! return 1; } if (ptn[0] == '*') return fitstar (ptn+1, target); if (ptn[0] == '?') { if (target[0] == '\0') return 2; else return fitquestion (ptn+1, target +1); } } str[i] ='\0'; //must fit on the head for (j=0; j<i; j++){ if (str[j] != target[j]) return 2; } if (ptn[i] == '*') return fitstar (ptn+i+1, target+i); else { return fitquestion (ptn+i, target+i); }}/*////// return value: 0: matches 1: ptn fites the forepart of target, but not long enough 2: unmatches*///////static int filter( char *ptn, char *target){ if (ptn[0] == '*') return fitstar (ptn+1, target); else return fitquestion (ptn, target);}//filter end///////////////////////////////////////////////////////////////////static char* GetParentDir (char *dir){ int i, nParent = 0; for (i = 0; i < strlen (dir) -1; i++) if (dir [i] == '/') nParent = i; dir [nParent + 1] = 0; return dir;}static void myWinFileListDirWithFilter ( HWND hWnd, int dirListID, int fileListID, char* path, char* filtstr){ struct dirent* pDirEnt; DIR* dir; struct stat ftype; char fullpath [PATH_MAX + 1]; int i; SendDlgItemMessage (hWnd, IDC_DIRCHOISE, LB_RESETCONTENT, 0, (LPARAM)0); SendDlgItemMessage (hWnd, IDC_FILECHOISE, LB_RESETCONTENT, 0, (LPARAM)0); SetWindowText (GetDlgItem (hWnd, IDC_PATH), path); dir = opendir (path); while ( (pDirEnt = readdir ( dir )) != NULL ) { // Assemble full path name. strncpy (fullpath, path, PATH_MAX); strcat (fullpath, "/"); strcat (fullpath, pDirEnt->d_name); if (stat (fullpath, &ftype) < 0 ){ //Ping(); continue; } if (S_ISDIR (ftype.st_mode)) SendDlgItemMessage (hWnd, dirListID, LB_ADDSTRING, 0, (LPARAM)pDirEnt->d_name); else if (S_ISREG(ftype.st_mode)) { if (filtstr != NULL) { i = filter (filtstr, pDirEnt->d_name); if (i == 0 /*|| i == 1 // when responding to the EN_TAB message later*/) SendDlgItemMessage (hWnd, fileListID, LB_ADDSTRING, 0, (LPARAM)pDirEnt->d_name); } else SendDlgItemMessage (hWnd, fileListID, LB_ADDSTRING, 0, (LPARAM)pDirEnt->d_name); } } closedir(dir);}//leon end/////////////////////////////////////////////////////////////////static intWinFileProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam){ switch (message) { case MSG_INITDIALOG: { PFILEDLGDATA pWinFileData = (PFILEDLGDATA)lParam; /* get current directory name */ if (strcmp (pWinFileData->filepath, ".") == 0 || strcmp (pWinFileData->filepath, "./") == 0) getcwd (pWinFileData->filepath, PATH_MAX); SetWindowAdditionalData (hDlg, (DWORD)lParam); myWinFileListDirWithFilter (hDlg, IDC_DIRCHOISE, IDC_FILECHOISE, pWinFileData->filepath, NULL); SetWindowText (GetDlgItem (hDlg, IDC_PATH), pWinFileData->filepath); return 1; } break; case MSG_MINIMIZE: SendMessage (hDlg, MSG_COMMAND, (WPARAM)IDOK, 0); break; case MSG_COMMAND: { PFILEDLGDATA pWinFileData = (PFILEDLGDATA) GetWindowAdditionalData (hDlg); int nSelect; int code = HIWORD (wParam); int id = LOWORD (wParam); char msg[129]; switch (id) { case IDC_DIRCHOISE: { char dir [NAME_MAX + 1];#if _DOUBLE_CLICK if (code == LBN_DBLCLK || code == LBN_ENTER) {#else if (code == LBN_SELCHANGE || code == LBN_ENTER) {#endif nSelect = SendDlgItemMessage (hDlg, IDC_DIRCHOISE, LB_GETCURSEL, 0, 0); if (nSelect == -1) break; SendDlgItemMessage(hDlg, IDC_DIRCHOISE, LB_GETTEXT, nSelect, (LPARAM)dir); if (strcmp (dir, ".") == 0) break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -