1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <sys/stat.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <dirent.h>
10 #include <pwd.h>
11 #include <errno.h>
12
13 #include <minigui/common.h>
14 #include <minigui/minigui.h>
15 #include <minigui/gdi.h>
16 #include <minigui/window.h>
17 #include <minigui/control.h>
18
19 #define IDL_DIR 100
20 #define IDL_FILE 110
21 #define IDC_PATH 120
22
23 #define deleting_the_files "Deleting the files"
24 #define directories "Directories:"
25 #define files_ "Files:"
26 #define path_ "Path: "
27 #define delete_ "Delete"
28 #define cancel_ "Cancel"
29 #define deleting_files_ "Deleting files"
30
31 static DLGTEMPLATE DlgDelFiles =
32 {
33 WS_BORDER | WS_CAPTION,
34 WS_EX_NONE,
35 100, 100, 500, 250,
36 deleting_the_files,
37 0, 0,
38 7, NULL,
39 0
40 };
41
42 static CTRLDATA CtrlDelFiles[] =
43 {
44 {
45 CTRL_STATIC,
46 WS_VISIBLE | SS_SIMPLE,
47 10, 10, 130, 15,
48 IDC_STATIC,
49 directories,
50 0
51 },
52 {
53 CTRL_LISTBOX,
54 WS_VISIBLE | WS_VSCROLL | WS_BORDER | LBS_SORT | LBS_NOTIFY,
55 10, 30, 130, 100,
56 IDL_DIR,
57 "",
58 0
59 },
60 {
61 CTRL_STATIC,
62 WS_VISIBLE | SS_SIMPLE,
63 150, 10, 130, 15,
64 IDC_STATIC,
65 files_,
66 0
67 },
68 {
69 CTRL_LISTBOX,
70 WS_VISIBLE | WS_VSCROLL | WS_BORDER | LBS_SORT | LBS_AUTOCHECKBOX,
71 150, 30, 260, 100,
72 IDL_FILE,
73 "",
74 0
75 },
76 {
77 CTRL_STATIC,
78 WS_VISIBLE | SS_SIMPLE,
79 10, 150, 290, 15,
80 IDC_PATH,
81 path_,
82 0
83 },
84 {
85 "button",
86 WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_GROUP,
87 10, 170, 130, 25,
88 IDOK,
89 delete_,
90 0
91 },
92 {
93 "button",
94 WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
95 150, 170, 130, 25,
96 IDCANCEL,
97 cancel_,
98 0
99 },
100 };
101
102 static void fill_boxes (HWND hDlg, const char* path)
103 {
104 #ifdef __NOUNIX__
105 LISTBOXITEMINFO lbii;
106
107 lbii.string = "file.1";
108 lbii.cmFlag = CMFLAG_BLANK;
109 lbii.hIcon = 0;
110 SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
111
112 lbii.string = "file.2";
113 SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
114
115 lbii.string = "file.3";
116 SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
117 #else
118 struct dirent* dir_ent;
119 DIR* dir;
120 struct stat ftype;
121 char fullpath [PATH_MAX + 1];
122
123 SendDlgItemMessage (hDlg, IDL_DIR, LB_RESETCONTENT, 0, (LPARAM)0);
124 SendDlgItemMessage (hDlg, IDL_FILE, LB_RESETCONTENT, 0, (LPARAM)0);
125 SetWindowText (GetDlgItem (hDlg, IDC_PATH), path);
126
127 if ((dir = opendir (path)) == NULL)
128 return;
129
130 while ( (dir_ent = readdir ( dir )) != NULL ) {
131
132 /* Assemble full path name. */
133 strncpy (fullpath, path, PATH_MAX);
134 strcat (fullpath, "/");
135 strcat (fullpath, dir_ent->d_name);
136
137 if (stat (fullpath, &ftype) < 0 ) {
138 continue;
139 }
140
141 if (S_ISDIR (ftype.st_mode))
142 SendDlgItemMessage (hDlg, IDL_DIR, LB_ADDSTRING, 0, (LPARAM)dir_ent->d_name);
143 else if (S_ISREG (ftype.st_mode)) {
144 LISTBOXITEMINFO lbii;
145
146 lbii.string = dir_ent->d_name;
147 lbii.cmFlag = CMFLAG_BLANK;
148 lbii.hIcon = 0;
149 SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
150 }
151 }
152
153 closedir (dir);
154 #endif
155 }
156
157 static void dir_notif_proc (HWND hwnd, int id, int nc, DWORD add_data)
158 {
159 if (nc == LBN_DBLCLK || nc == LBN_ENTER) {
160 int cur_sel = SendMessage (hwnd, LB_GETCURSEL, 0, 0L);
161 if (cur_sel >= 0) {
162 char cwd [MAX_PATH + 1];
163 char dir [MAX_NAME + 1];
164 GetWindowText (GetDlgItem (GetParent (hwnd), IDC_PATH), cwd, MAX_PATH);
165 SendMessage (hwnd, LB_GETTEXT, cur_sel, (LPARAM)dir);
166
167 if (strcmp (dir, ".") == 0)
168 return;
169
170 if (strcmp (dir, "..") == 0) {
171 char* slash;
172
173 if (strcmp (cwd, "/") == 0)
174 return;
175
176 slash = strrchr (cwd, '/');
177 if (slash == NULL)
178 return;
179 if (slash == cwd)
180 strcpy (cwd, "/");
181 else
182 *slash = '\0';
183 }
184 else {
185 if (strcmp (cwd, "/") != 0)
186 strcat (cwd, "/");
187 strcat (cwd, dir);
188 }
189
190 fill_boxes (GetParent (hwnd), cwd);
191 }
192 }
193 }
194
195 static void file_notif_proc (HWND hwnd, int id, int nc, DWORD add_data)
196 {
197 /* Do nothing */
198 }
199
200 static void prompt (HWND hDlg)
201 {
202 int i;
203 #ifdef _LANG_ZHCN
204 char files [1024] = "你選擇要刪除的文件是:\n";
205 #else
206 char files [1024] = "The files followed will be deleted\n";
207 #endif
208 for (i = 0; i < SendDlgItemMessage (hDlg, IDL_FILE, LB_GETCOUNT, 0, 0L); i++) {
209 char file [MAX_NAME + 1];
210 int status = SendDlgItemMessage (hDlg, IDL_FILE, LB_GETCHECKMARK, i, 0);
211 if (status == CMFLAG_CHECKED) {
212 SendDlgItemMessage (hDlg, IDL_FILE, LB_GETTEXT, i, (LPARAM)file);
213 strcat (files, file);
214 strcat (files, "\n");
215 }
216 }
217
218 MessageBox (hDlg, files, deleting_files_, MB_OK | MB_ICONINFORMATION);
219
220 }
221
222 static int DelFilesBoxProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
223 {
224 switch (message) {
225 case MSG_INITDIALOG:
226 {
227 char cwd [MAX_PATH + 1];
228 SetNotificationCallback (GetDlgItem (hDlg, IDL_DIR), dir_notif_proc);
229 SetNotificationCallback (GetDlgItem (hDlg, IDL_FILE), file_notif_proc);
230 fill_boxes (hDlg, getcwd (cwd, MAX_PATH));
231 return 1;
232 }
233
234 case MSG_COMMAND:
235 switch (wParam) {
236 case IDOK:
237 prompt (hDlg);
238 case IDCANCEL:
239 EndDialog (hDlg, wParam);
240 break;
241 }
242 break;
243
244 }
245
246 return DefaultDialogProc (hDlg, message, wParam, lParam);
247 }
248
249 int MiniGUIMain (int argc, const char* argv[])
250 {
251 #ifdef _MGRM_PROCESSES
252 JoinLayer(NAME_DEF_LAYER , "listbox" , 0 , 0);
253 #endif
254
255 DlgDelFiles.controls = CtrlDelFiles;
256
257 DialogBoxIndirectParam (&DlgDelFiles, HWND_DESKTOP, DelFilesBoxProc, 0L);
258
259 return 0;
260 }
261
262 #ifdef _MGRM_THREADS
263 #include <minigui/dti.c>
264 #endif