Initial revision
[wmaker-crm.git] / WINGs / wfilepanel.c
blob8ff656e9a2b136877b56a6179479138f305c0325
5 #include "WINGsP.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <dirent.h>
11 #include <limits.h>
13 #ifndef PATH_MAX
14 #define PATH_MAX 1024
15 #endif
17 typedef struct W_FilePanel {
18 WMWindow *win;
20 WMLabel *iconLabel;
21 WMLabel *titleLabel;
23 WMFrame *line;
25 WMLabel *nameLabel;
26 WMBrowser *browser;
28 WMButton *okButton;
29 WMButton *cancelButton;
31 WMButton *homeButton;
33 WMTextField *fileField;
35 char **fileTypes;
37 struct {
38 unsigned int canExit:1;
39 unsigned int canceled:1; /* clicked on cancel */
40 unsigned int done:1;
41 unsigned int filtered:1;
42 unsigned int canChooseFiles:1;
43 unsigned int canChooseDirectories:1;
44 unsigned int showAllFiles:1;
45 unsigned int canFreeFileTypes:1;
46 unsigned int fileMustExist:1;
47 } flags;
48 } W_FilePanel;
51 #define PWIDTH 320
52 #define PHEIGHT 360
54 static void listDirectoryOnColumn(WMFilePanel *panel, int column, char *path);
55 static void browserClick();
57 static void fillColumn(WMBrowser *bPtr, int column);
59 static void goHome();
61 static void buttonClick();
63 static char *getCurrentFileName(WMFilePanel *panel);
65 static int
66 closestListItem(WMList *list, char *text)
68 WMListItem *item = WMGetListItem(list, 0);
69 int i = 0;
70 int len = strlen(text);
72 if (len==0)
73 return -1;
75 while (item) {
76 if (strlen(item->text) >= len && strncmp(item->text, text, len)==0) {
77 return i;
79 item = item->nextPtr;
80 i++;
82 return -1;
86 static void
87 textChangedObserver(void *observerData, WMNotification *notification)
89 W_FilePanel *panel = (W_FilePanel*)observerData;
90 char *text;
91 WMList *list;
92 int col = WMGetBrowserNumberOfColumns(panel->browser) - 1;
93 int i;
95 list = WMGetBrowserListInColumn(panel->browser, col);
96 if (!list)
97 return;
98 text = WMGetTextFieldText(panel->fileField);
100 i = closestListItem(list, text);
101 WMSelectListItem(list, i);
102 if (i>=0)
103 WMSetListPosition(list, i);
105 free(text);
109 static void
110 textEditedObserver(void *observerData, WMNotification *notification)
112 W_FilePanel *panel = (W_FilePanel*)observerData;
114 if ((int)WMGetNotificationClientData(notification)==WMReturnTextMovement) {
115 WMPerformButtonClick(panel->okButton);
121 static WMFilePanel*
122 makeFilePanel(WMScreen *scrPtr, char *name, char *title)
124 WMFilePanel *fPtr;
125 WMFont *largeFont;
127 fPtr = wmalloc(sizeof(WMFilePanel));
128 memset(fPtr, 0, sizeof(WMFilePanel));
130 fPtr->win = WMCreateWindowWithStyle(scrPtr, name, WMTitledWindowMask
131 |WMResizableWindowMask);
132 WMResizeWidget(fPtr->win, PWIDTH, PHEIGHT);
133 WMSetWindowTitle(fPtr->win, "");
135 fPtr->iconLabel = WMCreateLabel(fPtr->win);
136 WMResizeWidget(fPtr->iconLabel, 64, 64);
137 WMMoveWidget(fPtr->iconLabel, 0, 0);
138 WMSetLabelImagePosition(fPtr->iconLabel, WIPImageOnly);
139 WMSetLabelImage(fPtr->iconLabel, scrPtr->applicationIcon);
141 fPtr->titleLabel = WMCreateLabel(fPtr->win);
142 WMResizeWidget(fPtr->titleLabel, PWIDTH-64, 64);
143 WMMoveWidget(fPtr->titleLabel, 64, 0);
144 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
145 WMSetLabelFont(fPtr->titleLabel, largeFont);
146 WMReleaseFont(largeFont);
147 WMSetLabelText(fPtr->titleLabel, title);
149 fPtr->line = WMCreateFrame(fPtr->win);
150 WMMoveWidget(fPtr->line, 0, 64);
151 WMResizeWidget(fPtr->line, PWIDTH, 2);
152 WMSetFrameRelief(fPtr->line, WRGroove);
154 fPtr->browser = WMCreateBrowser(fPtr->win);
155 WMSetBrowserFillColumnProc(fPtr->browser, fillColumn);
156 WMSetBrowserAction(fPtr->browser, browserClick, fPtr);
157 WMMoveWidget(fPtr->browser, 7, 72);
158 WMHangData(fPtr->browser, fPtr);
160 fPtr->nameLabel = WMCreateLabel(fPtr->win);
161 WMMoveWidget(fPtr->nameLabel, 7, 282);
162 WMResizeWidget(fPtr->nameLabel, 55, 14);
163 WMSetLabelText(fPtr->nameLabel, "Name:");
165 fPtr->fileField = WMCreateTextField(fPtr->win);
166 WMMoveWidget(fPtr->fileField, 60, 278);
167 WMResizeWidget(fPtr->fileField, PWIDTH-60-10, 24);
168 WMAddNotificationObserver(textEditedObserver, fPtr,
169 WMTextDidEndEditingNotification,
170 fPtr->fileField);
171 WMAddNotificationObserver(textChangedObserver, fPtr,
172 WMTextDidChangeNotification,
173 fPtr->fileField);
175 fPtr->okButton = WMCreateCommandButton(fPtr->win);
176 WMMoveWidget(fPtr->okButton, 230, 325);
177 WMResizeWidget(fPtr->okButton, 80, 28);
178 WMSetButtonText(fPtr->okButton, "OK");
179 WMSetButtonImage(fPtr->okButton, scrPtr->buttonArrow);
180 WMSetButtonAltImage(fPtr->okButton, scrPtr->pushedButtonArrow);
181 WMSetButtonImagePosition(fPtr->okButton, WIPRight);
182 WMSetButtonAction(fPtr->okButton, buttonClick, fPtr);
184 fPtr->cancelButton = WMCreateCommandButton(fPtr->win);
185 WMMoveWidget(fPtr->cancelButton, 140, 325);
186 WMResizeWidget(fPtr->cancelButton, 80, 28);
187 WMSetButtonText(fPtr->cancelButton, "Cancel");
188 WMSetButtonAction(fPtr->cancelButton, buttonClick, fPtr);
190 fPtr->homeButton = WMCreateCommandButton(fPtr->win);
191 WMMoveWidget(fPtr->homeButton, 55, 325);
192 WMResizeWidget(fPtr->homeButton, 28, 28);
193 WMSetButtonImagePosition(fPtr->homeButton, WIPImageOnly);
194 WMSetButtonImage(fPtr->homeButton, scrPtr->homeIcon);
195 WMSetButtonAction(fPtr->homeButton, goHome, fPtr);
197 WMRealizeWidget(fPtr->win);
198 WMMapSubwidgets(fPtr->win);
200 WMLoadBrowserColumnZero(fPtr->browser);
202 fPtr->flags.canChooseFiles = 1;
203 fPtr->flags.canChooseDirectories = 1;
205 return fPtr;
209 WMOpenPanel*
210 WMGetOpenPanel(WMScreen *scrPtr)
212 WMFilePanel *panel;
214 if (scrPtr->sharedOpenPanel)
215 return scrPtr->sharedOpenPanel;
217 panel = makeFilePanel(scrPtr, "openFilePanel", "Open");
218 panel->flags.fileMustExist = 1;
220 scrPtr->sharedOpenPanel = panel;
222 return panel;
226 WMSavePanel*
227 WMGetSavePanel(WMScreen *scrPtr)
229 WMFilePanel *panel;
231 if (scrPtr->sharedSavePanel)
232 return scrPtr->sharedSavePanel;
234 panel = makeFilePanel(scrPtr, "saveFilePanel", "Save");
235 panel->flags.fileMustExist = 0;
237 scrPtr->sharedSavePanel = panel;
239 return panel;
243 void
244 WMFreeFilePanel(WMFilePanel *panel)
246 if (panel == WMWidgetScreen(panel->win)->sharedOpenPanel) {
247 WMWidgetScreen(panel->win)->sharedOpenPanel = NULL;
249 WMRemoveNotificationObserver(panel);
250 WMUnmapWidget(panel->win);
251 WMDestroyWidget(panel->win);
252 free(panel);
257 WMRunModalSavePanelForDirectory(WMFilePanel *panel, WMWindow *owner,
258 char *path, char *name)
260 WMScreen *scr = WMWidgetScreen(panel->win);
261 XEvent event;
263 WMChangePanelOwner(panel->win, owner);
265 WMSetFilePanelDirectory(panel, path);
267 panel->flags.done = 0;
268 panel->fileTypes = NULL;
270 panel->flags.filtered = 0;
272 WMMapWidget(panel->win);
274 while (!panel->flags.done) {
275 WMNextEvent(scr->display, &event);
276 WMHandleEvent(&event);
279 WMCloseWindow(panel->win);
281 return (panel->flags.canceled ? False : True);
287 WMRunModalOpenPanelForDirectory(WMFilePanel *panel, WMWindow *owner,
288 char *path, char *name, char **fileTypes)
290 WMScreen *scr = WMWidgetScreen(panel->win);
291 XEvent event;
293 WMChangePanelOwner(panel->win, owner);
295 WMSetFilePanelDirectory(panel, path);
297 panel->flags.done = 0;
299 if (fileTypes)
300 panel->flags.filtered = 1;
301 panel->fileTypes = fileTypes;
303 WMMapWidget(panel->win);
305 while (!panel->flags.done) {
306 WMNextEvent(scr->display, &event);
307 WMHandleEvent(&event);
310 WMCloseWindow(panel->win);
312 return (panel->flags.canceled ? False : True);
316 void
317 WMSetFilePanelDirectory(WMFilePanel *panel, char *path)
319 WMList *list;
320 WMListItem *item;
321 int col;
323 WMSetBrowserPath(panel->browser, path);
324 col = WMGetBrowserNumberOfColumns(panel->browser) - 1;
325 list = WMGetBrowserListInColumn(panel->browser, col);
326 if (list && (item = WMGetListSelectedItem(list))) {
327 if (item->isBranch) {
328 WMSetTextFieldText(panel->fileField, NULL);
329 } else {
330 WMSetTextFieldText(panel->fileField, item->text);
332 } else {
333 WMSetTextFieldText(panel->fileField, path);
338 void
339 WMSetFilePanelCanChooseDirectories(WMFilePanel *panel, int flag)
341 panel->flags.canChooseDirectories = flag;
344 void
345 WMSetFilePanelCanChooseFiles(WMFilePanel *panel, int flag)
347 panel->flags.canChooseFiles = flag;
351 char*
352 WMGetFilePanelFileName(WMFilePanel *panel)
354 return getCurrentFileName(panel);
358 static char*
359 get_name_from_path(char *path)
361 int size;
363 assert(path!=NULL);
365 size = strlen(path);
367 /* remove trailing / */
368 while (size > 0 && path[size-1]=='/')
369 size--;
370 /* directory was root */
371 if (size == 0)
372 return wstrdup("/");
374 while (size > 0 && path[size-1] != '/')
375 size--;
377 return wstrdup(&(path[size]));
381 static int
382 filterFileName(WMFilePanel *panel, char *file, Bool isDirectory)
384 return True;
388 static void
389 listDirectoryOnColumn(WMFilePanel *panel, int column, char *path)
391 WMBrowser *bPtr = panel->browser;
392 struct dirent *dentry;
393 DIR *dir;
394 struct stat stat_buf;
395 char pbuf[PATH_MAX+16];
397 assert(column >= 0);
398 assert(path != NULL);
400 /* put directory name in the title */
401 WMSetBrowserColumnTitle(bPtr, column, get_name_from_path(path));
403 dir = opendir(path);
405 if (!dir) {
406 #ifdef VERBOSE
407 printf("WINGs: could not open directory %s\n", path);
408 #endif
409 return;
412 /* list contents in the column */
413 while ((dentry = readdir(dir))) {
414 if (strcmp(dentry->d_name, ".")==0 ||
415 strcmp(dentry->d_name, "..")==0)
416 continue;
418 strcpy(pbuf, path);
419 if (strcmp(path, "/")!=0)
420 strcat(pbuf, "/");
421 strcat(pbuf, dentry->d_name);
423 if (stat(pbuf, &stat_buf)!=0) {
424 #ifdef VERBOSE
425 printf("WINGs: could not stat %s\n", pbuf);
426 #endif
427 continue;
428 } else {
429 int isDirectory;
431 isDirectory = S_ISDIR(stat_buf.st_mode);
433 if (filterFileName(panel, dentry->d_name, isDirectory))
434 WMAddSortedBrowserItem(bPtr, column, dentry->d_name,
435 isDirectory);
439 closedir(dir);
443 static void
444 fillColumn(WMBrowser *bPtr, int column)
446 char *path;
447 WMFilePanel *panel;
449 if (column > 0) {
450 path = WMGetBrowserPathToColumn(bPtr, column-1);
451 } else {
452 path = wstrdup("/");
455 panel = WMGetHangedData(bPtr);
456 listDirectoryOnColumn(panel, column, path);
457 free(path);
462 static void
463 browserClick(WMBrowser *bPtr, WMFilePanel *panel)
465 int col = WMGetBrowserSelectedColumn(bPtr);
466 WMListItem *item = WMGetBrowserSelectedItemInColumn(bPtr, col);
468 if (!item || item->isBranch)
469 WMSetTextFieldText(panel->fileField, NULL);
470 else {
471 WMSetTextFieldText(panel->fileField, item->text);
476 static void
477 goHome(WMButton *bPtr, WMFilePanel *panel)
479 char *home;
481 /* home is statically allocated. Don't free it! */
482 home = wgethomedir();
483 if (!home)
484 return;
486 WMSetFilePanelDirectory(panel, home);
490 static char*
491 getCurrentFileName(WMFilePanel *panel)
493 char *path;
494 char *file;
495 char *tmp;
496 int len;
498 path = WMGetBrowserPath(panel->browser);
500 len = strlen(path);
501 if (path[len-1]=='/') {
502 file = WMGetTextFieldText(panel->fileField);
503 tmp = wmalloc(strlen(path)+strlen(file)+8);
504 strcpy(tmp, path);
505 strcat(tmp, file);
506 free(file);
507 free(path);
508 return tmp;
509 } else {
510 return path;
516 static Bool
517 validOpenFile(WMFilePanel *panel)
519 WMListItem *item;
520 int col;
522 col = WMGetBrowserSelectedColumn(panel->browser);
523 item = WMGetBrowserSelectedItemInColumn(panel->browser, col);
524 if (item) {
525 if (item->isBranch && !panel->flags.canChooseDirectories)
526 return False;
527 else if (!item->isBranch && !panel->flags.canChooseFiles)
528 return False;
530 return True;
535 static void
536 buttonClick(WMButton *bPtr, WMFilePanel *panel)
538 if (bPtr == panel->okButton) {
539 if (panel->flags.fileMustExist) {
540 char *file;
541 if (!validOpenFile(panel))
542 return;
544 file = getCurrentFileName(panel);
545 if (access(file, F_OK)!=0) {
546 WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win,
547 "Error", "File does not exist.",
548 "Ok", NULL, NULL);
549 free(file);
550 return;
552 free(file);
554 panel->flags.canceled = 0;
555 } else
556 panel->flags.canceled = 1;
558 panel->flags.done = 1;