Code update for Window Maker version 0.50.0
[wmaker-crm.git] / WINGs / wfilepanel.c
blobccf61e791bda33af1577bf7092a7058e43ff8951
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 WMView *accessoryView;
35 WMTextField *fileField;
37 char **fileTypes;
39 struct {
40 unsigned int canExit:1;
41 unsigned int canceled:1; /* clicked on cancel */
42 unsigned int done:1;
43 unsigned int filtered:1;
44 unsigned int canChooseFiles:1;
45 unsigned int canChooseDirectories:1;
46 unsigned int showAllFiles:1;
47 unsigned int canFreeFileTypes:1;
48 unsigned int fileMustExist:1;
49 } flags;
50 } W_FilePanel;
53 #define PWIDTH 320
54 #define PHEIGHT 360
56 static void listDirectoryOnColumn(WMFilePanel *panel, int column, char *path);
57 static void browserClick();
59 static void fillColumn(WMBrowser *bPtr, int column);
61 static void goHome();
63 static void buttonClick();
65 static char *getCurrentFileName(WMFilePanel *panel);
67 static int
68 closestListItem(WMList *list, char *text)
70 WMListItem *item = WMGetListItem(list, 0);
71 int i = 0;
72 int len = strlen(text);
74 if (len==0)
75 return -1;
77 while (item) {
78 if (strlen(item->text) >= len && strncmp(item->text, text, len)==0) {
79 return i;
81 item = item->nextPtr;
82 i++;
84 return -1;
88 static void
89 textChangedObserver(void *observerData, WMNotification *notification)
91 W_FilePanel *panel = (W_FilePanel*)observerData;
92 char *text;
93 WMList *list;
94 int col = WMGetBrowserNumberOfColumns(panel->browser) - 1;
95 int i;
97 list = WMGetBrowserListInColumn(panel->browser, col);
98 if (!list)
99 return;
100 text = WMGetTextFieldText(panel->fileField);
102 i = closestListItem(list, text);
103 WMSelectListItem(list, i);
104 if (i>=0)
105 WMSetListPosition(list, i);
107 free(text);
111 static void
112 textEditedObserver(void *observerData, WMNotification *notification)
114 W_FilePanel *panel = (W_FilePanel*)observerData;
116 if ((int)WMGetNotificationClientData(notification)==WMReturnTextMovement) {
117 WMPerformButtonClick(panel->okButton);
123 static WMFilePanel*
124 makeFilePanel(WMScreen *scrPtr, char *name, char *title)
126 WMFilePanel *fPtr;
127 WMFont *largeFont;
129 fPtr = wmalloc(sizeof(WMFilePanel));
130 memset(fPtr, 0, sizeof(WMFilePanel));
132 fPtr->win = WMCreateWindowWithStyle(scrPtr, name, WMTitledWindowMask
133 |WMResizableWindowMask);
134 WMResizeWidget(fPtr->win, PWIDTH, PHEIGHT);
135 WMSetWindowTitle(fPtr->win, "");
137 fPtr->iconLabel = WMCreateLabel(fPtr->win);
138 WMResizeWidget(fPtr->iconLabel, 64, 64);
139 WMMoveWidget(fPtr->iconLabel, 0, 0);
140 WMSetLabelImagePosition(fPtr->iconLabel, WIPImageOnly);
141 WMSetLabelImage(fPtr->iconLabel, scrPtr->applicationIcon);
143 fPtr->titleLabel = WMCreateLabel(fPtr->win);
144 WMResizeWidget(fPtr->titleLabel, PWIDTH-64, 64);
145 WMMoveWidget(fPtr->titleLabel, 64, 0);
146 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
147 WMSetLabelFont(fPtr->titleLabel, largeFont);
148 WMReleaseFont(largeFont);
149 WMSetLabelText(fPtr->titleLabel, title);
151 fPtr->line = WMCreateFrame(fPtr->win);
152 WMMoveWidget(fPtr->line, 0, 64);
153 WMResizeWidget(fPtr->line, PWIDTH, 2);
154 WMSetFrameRelief(fPtr->line, WRGroove);
156 fPtr->browser = WMCreateBrowser(fPtr->win);
157 WMSetBrowserFillColumnProc(fPtr->browser, fillColumn);
158 WMSetBrowserAction(fPtr->browser, browserClick, fPtr);
159 WMMoveWidget(fPtr->browser, 7, 72);
160 WMHangData(fPtr->browser, fPtr);
162 fPtr->nameLabel = WMCreateLabel(fPtr->win);
163 WMMoveWidget(fPtr->nameLabel, 7, 282);
164 WMResizeWidget(fPtr->nameLabel, 55, 14);
165 WMSetLabelText(fPtr->nameLabel, "Name:");
167 fPtr->fileField = WMCreateTextField(fPtr->win);
168 WMMoveWidget(fPtr->fileField, 60, 278);
169 WMResizeWidget(fPtr->fileField, PWIDTH-60-10, 24);
170 WMAddNotificationObserver(textEditedObserver, fPtr,
171 WMTextDidEndEditingNotification,
172 fPtr->fileField);
173 WMAddNotificationObserver(textChangedObserver, fPtr,
174 WMTextDidChangeNotification,
175 fPtr->fileField);
177 fPtr->okButton = WMCreateCommandButton(fPtr->win);
178 WMMoveWidget(fPtr->okButton, 230, 325);
179 WMResizeWidget(fPtr->okButton, 80, 28);
180 WMSetButtonText(fPtr->okButton, "OK");
181 WMSetButtonImage(fPtr->okButton, scrPtr->buttonArrow);
182 WMSetButtonAltImage(fPtr->okButton, scrPtr->pushedButtonArrow);
183 WMSetButtonImagePosition(fPtr->okButton, WIPRight);
184 WMSetButtonAction(fPtr->okButton, buttonClick, fPtr);
186 fPtr->cancelButton = WMCreateCommandButton(fPtr->win);
187 WMMoveWidget(fPtr->cancelButton, 140, 325);
188 WMResizeWidget(fPtr->cancelButton, 80, 28);
189 WMSetButtonText(fPtr->cancelButton, "Cancel");
190 WMSetButtonAction(fPtr->cancelButton, buttonClick, fPtr);
192 fPtr->homeButton = WMCreateCommandButton(fPtr->win);
193 WMMoveWidget(fPtr->homeButton, 55, 325);
194 WMResizeWidget(fPtr->homeButton, 28, 28);
195 WMSetButtonImagePosition(fPtr->homeButton, WIPImageOnly);
196 WMSetButtonImage(fPtr->homeButton, scrPtr->homeIcon);
197 WMSetButtonAction(fPtr->homeButton, goHome, fPtr);
199 WMRealizeWidget(fPtr->win);
200 WMMapSubwidgets(fPtr->win);
202 WMLoadBrowserColumnZero(fPtr->browser);
204 fPtr->flags.canChooseFiles = 1;
205 fPtr->flags.canChooseDirectories = 1;
207 return fPtr;
211 WMOpenPanel*
212 WMGetOpenPanel(WMScreen *scrPtr)
214 WMFilePanel *panel;
216 if (scrPtr->sharedOpenPanel)
217 return scrPtr->sharedOpenPanel;
219 panel = makeFilePanel(scrPtr, "openFilePanel", "Open");
220 panel->flags.fileMustExist = 1;
222 scrPtr->sharedOpenPanel = panel;
224 return panel;
228 WMSavePanel*
229 WMGetSavePanel(WMScreen *scrPtr)
231 WMFilePanel *panel;
233 if (scrPtr->sharedSavePanel)
234 return scrPtr->sharedSavePanel;
236 panel = makeFilePanel(scrPtr, "saveFilePanel", "Save");
237 panel->flags.fileMustExist = 0;
239 scrPtr->sharedSavePanel = panel;
241 return panel;
245 void
246 WMFreeFilePanel(WMFilePanel *panel)
248 if (panel == WMWidgetScreen(panel->win)->sharedSavePanel) {
249 WMWidgetScreen(panel->win)->sharedSavePanel = NULL;
251 if (panel == WMWidgetScreen(panel->win)->sharedOpenPanel) {
252 WMWidgetScreen(panel->win)->sharedOpenPanel = NULL;
254 WMRemoveNotificationObserver(panel);
255 WMUnmapWidget(panel->win);
256 WMDestroyWidget(panel->win);
257 free(panel);
262 WMRunModalSavePanelForDirectory(WMFilePanel *panel, WMWindow *owner,
263 char *path, char *name)
265 WMScreen *scr = WMWidgetScreen(panel->win);
266 XEvent event;
268 WMChangePanelOwner(panel->win, owner);
270 WMSetFilePanelDirectory(panel, path);
272 panel->flags.done = 0;
273 panel->fileTypes = NULL;
275 panel->flags.filtered = 0;
277 WMMapWidget(panel->win);
279 while (!panel->flags.done) {
280 WMNextEvent(scr->display, &event);
281 WMHandleEvent(&event);
284 WMCloseWindow(panel->win);
286 return (panel->flags.canceled ? False : True);
292 WMRunModalOpenPanelForDirectory(WMFilePanel *panel, WMWindow *owner,
293 char *path, char *name, char **fileTypes)
295 WMScreen *scr = WMWidgetScreen(panel->win);
296 XEvent event;
298 WMChangePanelOwner(panel->win, owner);
300 WMSetFilePanelDirectory(panel, path);
302 panel->flags.done = 0;
304 if (fileTypes)
305 panel->flags.filtered = 1;
306 panel->fileTypes = fileTypes;
308 WMMapWidget(panel->win);
310 while (!panel->flags.done) {
311 WMNextEvent(scr->display, &event);
312 WMHandleEvent(&event);
315 WMCloseWindow(panel->win);
317 return (panel->flags.canceled ? False : True);
321 void
322 WMSetFilePanelDirectory(WMFilePanel *panel, char *path)
324 WMList *list;
325 WMListItem *item;
326 int col;
328 WMSetBrowserPath(panel->browser, path);
329 col = WMGetBrowserNumberOfColumns(panel->browser) - 1;
330 list = WMGetBrowserListInColumn(panel->browser, col);
331 if (list && (item = WMGetListSelectedItem(list))) {
332 if (item->isBranch) {
333 WMSetTextFieldText(panel->fileField, NULL);
334 } else {
335 WMSetTextFieldText(panel->fileField, item->text);
337 } else {
338 WMSetTextFieldText(panel->fileField, path);
343 void
344 WMSetFilePanelCanChooseDirectories(WMFilePanel *panel, int flag)
346 panel->flags.canChooseDirectories = flag;
349 void
350 WMSetFilePanelCanChooseFiles(WMFilePanel *panel, int flag)
352 panel->flags.canChooseFiles = flag;
356 char*
357 WMGetFilePanelFileName(WMFilePanel *panel)
359 return getCurrentFileName(panel);
363 void
364 WMSetFilePanelAccessoryView(WMFilePanel *panel, WMView *view)
366 WMView *v;
368 panel->accessoryView = view;
370 v = WMWidgetView(panel->win);
372 W_ReparentView(view, v);
374 W_MoveView(view, (v->size.width - v->size.width)/2, 300);
378 WMView*
379 WMGetFilePanelAccessoryView(WMFilePanel *panel)
381 return panel->accessoryView;
385 static char*
386 get_name_from_path(char *path)
388 int size;
390 assert(path!=NULL);
392 size = strlen(path);
394 /* remove trailing / */
395 while (size > 0 && path[size-1]=='/')
396 size--;
397 /* directory was root */
398 if (size == 0)
399 return wstrdup("/");
401 while (size > 0 && path[size-1] != '/')
402 size--;
404 return wstrdup(&(path[size]));
408 static int
409 filterFileName(WMFilePanel *panel, char *file, Bool isDirectory)
411 return True;
415 static void
416 listDirectoryOnColumn(WMFilePanel *panel, int column, char *path)
418 WMBrowser *bPtr = panel->browser;
419 struct dirent *dentry;
420 DIR *dir;
421 struct stat stat_buf;
422 char pbuf[PATH_MAX+16];
424 assert(column >= 0);
425 assert(path != NULL);
427 /* put directory name in the title */
428 WMSetBrowserColumnTitle(bPtr, column, get_name_from_path(path));
430 dir = opendir(path);
432 if (!dir) {
433 #ifdef VERBOSE
434 printf("WINGs: could not open directory %s\n", path);
435 #endif
436 return;
439 /* list contents in the column */
440 while ((dentry = readdir(dir))) {
441 if (strcmp(dentry->d_name, ".")==0 ||
442 strcmp(dentry->d_name, "..")==0)
443 continue;
445 strcpy(pbuf, path);
446 if (strcmp(path, "/")!=0)
447 strcat(pbuf, "/");
448 strcat(pbuf, dentry->d_name);
450 if (stat(pbuf, &stat_buf)!=0) {
451 #ifdef VERBOSE
452 printf("WINGs: could not stat %s\n", pbuf);
453 #endif
454 continue;
455 } else {
456 int isDirectory;
458 isDirectory = S_ISDIR(stat_buf.st_mode);
460 if (filterFileName(panel, dentry->d_name, isDirectory))
461 WMAddSortedBrowserItem(bPtr, column, dentry->d_name,
462 isDirectory);
466 closedir(dir);
470 static void
471 fillColumn(WMBrowser *bPtr, int column)
473 char *path;
474 WMFilePanel *panel;
476 if (column > 0) {
477 path = WMGetBrowserPathToColumn(bPtr, column-1);
478 } else {
479 path = wstrdup("/");
482 panel = WMGetHangedData(bPtr);
483 listDirectoryOnColumn(panel, column, path);
484 free(path);
489 static void
490 browserClick(WMBrowser *bPtr, WMFilePanel *panel)
492 int col = WMGetBrowserSelectedColumn(bPtr);
493 WMListItem *item = WMGetBrowserSelectedItemInColumn(bPtr, col);
495 if (!item || item->isBranch)
496 WMSetTextFieldText(panel->fileField, NULL);
497 else {
498 WMSetTextFieldText(panel->fileField, item->text);
503 static void
504 goHome(WMButton *bPtr, WMFilePanel *panel)
506 char *home;
508 /* home is statically allocated. Don't free it! */
509 home = wgethomedir();
510 if (!home)
511 return;
513 WMSetFilePanelDirectory(panel, home);
517 static char*
518 getCurrentFileName(WMFilePanel *panel)
520 char *path;
521 char *file;
522 char *tmp;
523 int len;
525 path = WMGetBrowserPath(panel->browser);
527 len = strlen(path);
528 if (path[len-1]=='/') {
529 file = WMGetTextFieldText(panel->fileField);
530 tmp = wmalloc(strlen(path)+strlen(file)+8);
531 if (file[0]!='/') {
532 strcpy(tmp, path);
533 strcat(tmp, file);
534 } else
535 strcpy(tmp, file);
537 free(file);
538 free(path);
539 return tmp;
540 } else {
541 return path;
547 static Bool
548 validOpenFile(WMFilePanel *panel)
550 WMListItem *item;
551 int col;
553 col = WMGetBrowserSelectedColumn(panel->browser);
554 item = WMGetBrowserSelectedItemInColumn(panel->browser, col);
555 if (item) {
556 if (item->isBranch && !panel->flags.canChooseDirectories)
557 return False;
558 else if (!item->isBranch && !panel->flags.canChooseFiles)
559 return False;
561 return True;
566 static void
567 buttonClick(WMButton *bPtr, WMFilePanel *panel)
569 if (bPtr == panel->okButton) {
570 if (panel->flags.fileMustExist) {
571 char *file;
572 if (!validOpenFile(panel))
573 return;
575 file = getCurrentFileName(panel);
576 if (access(file, F_OK)!=0) {
577 WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win,
578 "Error", "File does not exist.",
579 "Ok", NULL, NULL);
580 free(file);
581 return;
583 free(file);
585 panel->flags.canceled = 0;
586 } else
587 panel->flags.canceled = 1;
589 panel->flags.done = 1;