more code for proplist handling
[wmaker-crm.git] / WINGs / wfilepanel.c
blob35e915c500b145a3fd229c713b970725b07345d8
2 #include "WINGsP.h"
3 #include "wconfig.h"
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include <dirent.h>
9 #include <limits.h>
10 #include <errno.h>
12 #ifndef PATH_MAX
13 #define PATH_MAX 1024
14 #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;
32 WMButton *trashcanButton;
33 WMButton *createDirButton;
34 WMButton *disketteButton;
35 WMButton *unmountButton;
37 WMView *accessoryView;
39 WMTextField *fileField;
41 char **fileTypes;
43 struct {
44 unsigned int canExit:1;
45 unsigned int canceled:1; /* clicked on cancel */
46 unsigned int filtered:1;
47 unsigned int canChooseFiles:1;
48 unsigned int canChooseDirectories:1;
49 unsigned int autoCompletion:1;
50 unsigned int showAllFiles:1;
51 unsigned int canFreeFileTypes:1;
52 unsigned int fileMustExist:1;
53 unsigned int panelType:1;
54 } flags;
55 } W_FilePanel;
58 /* Type of panel */
59 #define WP_OPEN 0
60 #define WP_SAVE 1
62 #define PWIDTH 330
63 #define PHEIGHT 360
65 static void listDirectoryOnColumn(WMFilePanel *panel, int column, char *path);
66 static void browserClick();
67 static void browserDClick();
69 static void fillColumn(WMBrowserDelegate *self, WMBrowser *bPtr, int column,
70 WMList *list);
72 static void deleteFile();
74 static void createDir();
76 static void goHome();
78 static void goFloppy();
80 static void goUnmount();
82 static void buttonClick();
84 static char *getCurrentFileName(WMFilePanel *panel);
86 static void handleEvents(XEvent *event, void *data);
90 static WMBrowserDelegate browserDelegate = {
91 NULL, /* data */
92 fillColumn, /* createRowsForColumn */
93 NULL, /* titleOfColumn */
94 NULL, /* didScroll */
95 NULL /* willScroll */
99 static int
100 closestListItem(WMList *list, char *text, Bool exact)
102 WMListItem *item;
103 WMArray *items = WMGetListItems(list);
104 int i, len = strlen(text);
106 if (len==0)
107 return -1;
109 for(i=0; i<WMGetArrayItemCount(items); i++) {
110 item = WMGetFromArray(items, i);
111 if (strlen(item->text) >= len &&
112 ((exact && strcmp(item->text, text)==0) ||
113 (!exact && strncmp(item->text, text, len)==0))) {
114 return i;
118 return -1;
122 static void
123 textChangedObserver(void *observerData, WMNotification *notification)
125 W_FilePanel *panel = (W_FilePanel*)observerData;
126 char *text;
127 WMList *list;
128 int col = WMGetBrowserNumberOfColumns(panel->browser) - 1;
129 int i, textEvent;
131 if (!(list = WMGetBrowserListInColumn(panel->browser, col)))
132 return;
134 text = WMGetTextFieldText(panel->fileField);
135 textEvent = (int)WMGetNotificationClientData(notification);
137 if (panel->flags.autoCompletion && textEvent!=WMDeleteTextEvent)
138 i = closestListItem(list, text, False);
139 else
140 i = closestListItem(list, text, True);
142 WMSelectListItem(list, i);
143 if (i>=0 && panel->flags.autoCompletion) {
144 WMListItem *item = WMGetListItem(list, i);
145 int textLen = strlen(text), itemTextLen = strlen(item->text);
146 int visibleItems = WMWidgetHeight(list)/WMGetListItemHeight(list);
148 WMSetListPosition(list, i - visibleItems/2);
150 if (textEvent!=WMDeleteTextEvent) {
151 WMRange range;
153 WMInsertTextFieldText(panel->fileField, &item->text[textLen],
154 textLen);
155 range.position = textLen;
156 range.count = itemTextLen - textLen;
157 WMSelectTextFieldRange(panel->fileField, range);
158 /*WMSetTextFieldCursorPosition(panel->fileField, itemTextLen);*/
162 wfree(text);
166 static void
167 textEditedObserver(void *observerData, WMNotification *notification)
169 W_FilePanel *panel = (W_FilePanel*)observerData;
171 if ((int)WMGetNotificationClientData(notification)==WMReturnTextMovement) {
172 WMPerformButtonClick(panel->okButton);
178 static WMFilePanel*
179 makeFilePanel(WMScreen *scrPtr, char *name, char *title)
181 WMFilePanel *fPtr;
182 WMFont *largeFont;
183 WMPixmap *icon;
185 fPtr = wmalloc(sizeof(WMFilePanel));
186 memset(fPtr, 0, sizeof(WMFilePanel));
188 fPtr->win = WMCreateWindowWithStyle(scrPtr, name, WMTitledWindowMask
189 |WMResizableWindowMask);
190 WMResizeWidget(fPtr->win, PWIDTH, PHEIGHT);
191 WMSetWindowTitle(fPtr->win, "");
193 WMCreateEventHandler(WMWidgetView(fPtr->win), StructureNotifyMask,
194 handleEvents, fPtr);
195 WMSetWindowMinSize(fPtr->win, PWIDTH, PHEIGHT);
198 fPtr->iconLabel = WMCreateLabel(fPtr->win);
199 WMResizeWidget(fPtr->iconLabel, 64, 64);
200 WMMoveWidget(fPtr->iconLabel, 0, 0);
201 WMSetLabelImagePosition(fPtr->iconLabel, WIPImageOnly);
202 icon = WMCreateApplicationIconBlendedPixmap(scrPtr, (RColor*)NULL);
203 if (icon) {
204 WMSetLabelImage(fPtr->iconLabel, icon);
205 WMReleasePixmap(icon);
206 } else {
207 WMSetLabelImage(fPtr->iconLabel, scrPtr->applicationIconPixmap);
210 fPtr->titleLabel = WMCreateLabel(fPtr->win);
211 WMResizeWidget(fPtr->titleLabel, PWIDTH-64, 64);
212 WMMoveWidget(fPtr->titleLabel, 64, 0);
213 largeFont = WMBoldSystemFontOfSize(scrPtr, 24);
214 WMSetLabelFont(fPtr->titleLabel, largeFont);
215 WMReleaseFont(largeFont);
216 WMSetLabelText(fPtr->titleLabel, title);
218 fPtr->line = WMCreateFrame(fPtr->win);
219 WMMoveWidget(fPtr->line, 0, 64);
220 WMResizeWidget(fPtr->line, PWIDTH, 2);
221 WMSetFrameRelief(fPtr->line, WRGroove);
223 fPtr->browser = WMCreateBrowser(fPtr->win);
224 WMSetBrowserAllowEmptySelection(fPtr->browser, True);
225 WMSetBrowserDelegate(fPtr->browser, &browserDelegate);
226 WMSetBrowserAction(fPtr->browser, browserClick, fPtr);
227 WMSetBrowserDoubleAction(fPtr->browser, browserDClick, fPtr);
228 WMMoveWidget(fPtr->browser, 7, 72);
229 WMResizeWidget(fPtr->browser, PWIDTH-14,200);
230 WMHangData(fPtr->browser, fPtr);
232 fPtr->nameLabel = WMCreateLabel(fPtr->win);
233 WMMoveWidget(fPtr->nameLabel, 7, 282);
234 WMResizeWidget(fPtr->nameLabel, 55, 14);
235 WMSetLabelText(fPtr->nameLabel, _("Name:"));
237 fPtr->fileField = WMCreateTextField(fPtr->win);
238 WMMoveWidget(fPtr->fileField, 60, 278);
239 WMResizeWidget(fPtr->fileField, PWIDTH-60-10, 24);
240 WMAddNotificationObserver(textEditedObserver, fPtr,
241 WMTextDidEndEditingNotification,
242 fPtr->fileField);
243 WMAddNotificationObserver(textChangedObserver, fPtr,
244 WMTextDidChangeNotification,
245 fPtr->fileField);
247 fPtr->okButton = WMCreateCommandButton(fPtr->win);
248 WMMoveWidget(fPtr->okButton, 245, 325);
249 WMResizeWidget(fPtr->okButton, 75, 28);
250 WMSetButtonText(fPtr->okButton, _("OK"));
251 WMSetButtonImage(fPtr->okButton, scrPtr->buttonArrow);
252 WMSetButtonAltImage(fPtr->okButton, scrPtr->pushedButtonArrow);
253 WMSetButtonImagePosition(fPtr->okButton, WIPRight);
254 WMSetButtonAction(fPtr->okButton, buttonClick, fPtr);
256 fPtr->cancelButton = WMCreateCommandButton(fPtr->win);
257 WMMoveWidget(fPtr->cancelButton, 165, 325);
258 WMResizeWidget(fPtr->cancelButton, 75, 28);
259 WMSetButtonText(fPtr->cancelButton, _("Cancel"));
260 WMSetButtonAction(fPtr->cancelButton, buttonClick, fPtr);
262 fPtr->trashcanButton = WMCreateCommandButton(fPtr->win);
263 WMMoveWidget(fPtr->trashcanButton, 7, 325);
264 WMResizeWidget(fPtr->trashcanButton, 28, 28);
265 WMSetButtonImagePosition(fPtr->trashcanButton, WIPImageOnly);
266 WMSetButtonImage(fPtr->trashcanButton, scrPtr->trashcanIcon);
267 WMSetButtonAltImage(fPtr->trashcanButton, scrPtr->altTrashcanIcon);
268 WMSetButtonAction(fPtr->trashcanButton, deleteFile, fPtr);
270 fPtr->createDirButton = WMCreateCommandButton(fPtr->win);
271 WMMoveWidget(fPtr->createDirButton, 37, 325);
272 WMResizeWidget(fPtr->createDirButton, 28, 28);
273 WMSetButtonImagePosition(fPtr->createDirButton, WIPImageOnly);
274 WMSetButtonImage(fPtr->createDirButton, scrPtr->createDirIcon);
275 WMSetButtonAltImage(fPtr->createDirButton, scrPtr->altCreateDirIcon);
276 WMSetButtonAction(fPtr->createDirButton, createDir, fPtr);
278 fPtr->homeButton = WMCreateCommandButton(fPtr->win);
279 WMMoveWidget(fPtr->homeButton, 67, 325);
280 WMResizeWidget(fPtr->homeButton, 28, 28);
281 WMSetButtonImagePosition(fPtr->homeButton, WIPImageOnly);
282 WMSetButtonImage(fPtr->homeButton, scrPtr->homeIcon);
283 WMSetButtonAltImage(fPtr->homeButton, scrPtr->altHomeIcon);
284 WMSetButtonAction(fPtr->homeButton, goHome, fPtr);
286 fPtr->disketteButton = WMCreateCommandButton(fPtr->win);
287 WMMoveWidget(fPtr->disketteButton, 97, 325);
288 WMResizeWidget(fPtr->disketteButton, 28, 28);
289 WMSetButtonImagePosition(fPtr->disketteButton, WIPImageOnly);
290 WMSetButtonImage(fPtr->disketteButton, scrPtr->disketteIcon);
291 WMSetButtonAltImage(fPtr->disketteButton, scrPtr->altDisketteIcon);
292 WMSetButtonAction(fPtr->disketteButton, goFloppy, fPtr);
294 fPtr->unmountButton = WMCreateCommandButton(fPtr->win);
295 WMMoveWidget(fPtr->unmountButton, 127, 325);
296 WMResizeWidget(fPtr->unmountButton, 28, 28);
297 WMSetButtonImagePosition(fPtr->unmountButton, WIPImageOnly);
298 WMSetButtonImage(fPtr->unmountButton, scrPtr->unmountIcon);
299 WMSetButtonAltImage(fPtr->unmountButton, scrPtr->altUnmountIcon);
300 WMSetButtonAction(fPtr->unmountButton, goUnmount, fPtr);
301 WMSetButtonEnabled(fPtr->unmountButton, False);
304 WMRealizeWidget(fPtr->win);
305 WMMapSubwidgets(fPtr->win);
307 WMSetFocusToWidget(fPtr->fileField);
308 WMSetTextFieldCursorPosition(fPtr->fileField, 0);
310 WMLoadBrowserColumnZero(fPtr->browser);
312 WMSetWindowInitialPosition(fPtr->win,
313 (scrPtr->rootView->size.width - WMWidgetWidth(fPtr->win))/2,
314 (scrPtr->rootView->size.height - WMWidgetHeight(fPtr->win))/2);
316 fPtr->flags.canChooseFiles = 1;
317 fPtr->flags.canChooseDirectories = 1;
318 fPtr->flags.autoCompletion = 1;
320 return fPtr;
324 WMOpenPanel*
325 WMGetOpenPanel(WMScreen *scrPtr)
327 WMFilePanel *panel;
329 if (scrPtr->sharedOpenPanel)
330 return scrPtr->sharedOpenPanel;
332 panel = makeFilePanel(scrPtr, "openFilePanel", _("Open"));
333 panel->flags.fileMustExist = 1;
334 panel->flags.panelType = WP_OPEN;
336 scrPtr->sharedOpenPanel = panel;
338 return panel;
342 WMSavePanel*
343 WMGetSavePanel(WMScreen *scrPtr)
345 WMFilePanel *panel;
347 if (scrPtr->sharedSavePanel)
348 return scrPtr->sharedSavePanel;
350 panel = makeFilePanel(scrPtr, "saveFilePanel", _("Save"));
351 panel->flags.fileMustExist = 0;
352 panel->flags.panelType = WP_SAVE;
354 scrPtr->sharedSavePanel = panel;
356 return panel;
360 void
361 WMFreeFilePanel(WMFilePanel *panel)
363 if (panel == WMWidgetScreen(panel->win)->sharedSavePanel) {
364 WMWidgetScreen(panel->win)->sharedSavePanel = NULL;
366 if (panel == WMWidgetScreen(panel->win)->sharedOpenPanel) {
367 WMWidgetScreen(panel->win)->sharedOpenPanel = NULL;
369 WMRemoveNotificationObserver(panel);
370 WMUnmapWidget(panel->win);
371 WMDestroyWidget(panel->win);
372 wfree(panel);
377 WMRunModalFilePanelForDirectory(WMFilePanel *panel, WMWindow *owner,
378 char *path, char *name, char **fileTypes)
380 WMScreen *scr = WMWidgetScreen(panel->win);
382 if (name && !owner) {
383 WMSetWindowTitle(panel->win, name);
386 WMChangePanelOwner(panel->win, owner);
388 WMSetFilePanelDirectory(panel, path);
390 switch(panel->flags.panelType) {
391 case WP_OPEN:
392 if (fileTypes)
393 panel->flags.filtered = 1;
394 panel->fileTypes = fileTypes;
395 if (name == NULL)
396 name = _("Open");
397 break;
398 case WP_SAVE:
399 panel->fileTypes = NULL;
400 panel->flags.filtered = 0;
401 if (name == NULL)
402 name = _("Save");
403 break;
404 default:
405 break;
408 WMSetLabelText(panel->titleLabel, name);
410 WMMapWidget(panel->win);
412 WMRunModalLoop(scr, W_VIEW(panel->win));
414 /* Must withdraw window because the next time we map
415 * it, it might have a different transient owner.
417 WMCloseWindow(panel->win);
419 return (panel->flags.canceled ? False : True);
425 void
426 WMSetFilePanelDirectory(WMFilePanel *panel, char *path)
428 WMList *list;
429 WMListItem *item;
430 int col;
431 char *rest;
433 rest = WMSetBrowserPath(panel->browser, path);
434 if (strcmp(path, "/")==0)
435 rest = NULL;
437 col = WMGetBrowserSelectedColumn(panel->browser);
438 list = WMGetBrowserListInColumn(panel->browser, col);
439 if (list && (item = WMGetListSelectedItem(list))) {
440 if (item->isBranch) {
441 WMSetTextFieldText(panel->fileField, rest);
442 } else {
443 WMSetTextFieldText(panel->fileField, item->text);
445 } else {
446 WMSetTextFieldText(panel->fileField, rest);
451 void
452 WMSetFilePanelCanChooseDirectories(WMFilePanel *panel, Bool flag)
454 panel->flags.canChooseDirectories = flag;
457 void
458 WMSetFilePanelCanChooseFiles(WMFilePanel *panel, Bool flag)
460 panel->flags.canChooseFiles = flag;
464 void
465 WMSetFilePanelAutoCompletion(WMFilePanel *panel, Bool flag)
467 panel->flags.autoCompletion = flag;
471 char*
472 WMGetFilePanelFileName(WMFilePanel *panel)
474 return getCurrentFileName(panel);
478 void
479 WMSetFilePanelAccessoryView(WMFilePanel *panel, WMView *view)
481 WMView *v;
483 panel->accessoryView = view;
485 v = WMWidgetView(panel->win);
487 W_ReparentView(view, v, 0, 0);
489 W_MoveView(view, (v->size.width - v->size.width)/2, 300);
493 WMView*
494 WMGetFilePanelAccessoryView(WMFilePanel *panel)
496 return panel->accessoryView;
500 static char*
501 get_name_from_path(char *path)
503 int size;
505 assert(path!=NULL);
507 size = strlen(path);
509 /* remove trailing / */
510 while (size > 0 && path[size-1]=='/')
511 size--;
512 /* directory was root */
513 if (size == 0)
514 return wstrdup("/");
516 while (size > 0 && path[size-1] != '/')
517 size--;
519 return wstrdup(&(path[size]));
523 static int
524 filterFileName(WMFilePanel *panel, char *file, Bool isDirectory)
526 return True;
530 #define CAST(item) (*((WMListItem**)item))
531 static int
532 comparer(const void *a, const void *b)
534 if (CAST(a)->isBranch == CAST(b)->isBranch)
535 return (strcmp(CAST(a)->text, CAST(b)->text));
536 if (CAST(a)->isBranch)
537 return (-1);
538 return (1);
540 #undef CAST
543 static void
544 listDirectoryOnColumn(WMFilePanel *panel, int column, char *path)
546 WMBrowser *bPtr = panel->browser;
547 struct dirent *dentry;
548 DIR *dir;
549 struct stat stat_buf;
550 char pbuf[PATH_MAX+16];
552 assert(column >= 0);
553 assert(path != NULL);
555 /* put directory name in the title */
556 WMSetBrowserColumnTitle(bPtr, column, get_name_from_path(path));
558 dir = opendir(path);
560 if (!dir) {
561 #ifdef VERBOSE
562 printf(_("WINGs: could not open directory %s\n"), path);
563 #endif
564 return;
567 /* list contents in the column */
568 while ((dentry = readdir(dir))) {
569 if (strcmp(dentry->d_name, ".")==0 ||
570 strcmp(dentry->d_name, "..")==0)
571 continue;
573 strcpy(pbuf, path);
574 if (strcmp(path, "/")!=0)
575 strcat(pbuf, "/");
576 strcat(pbuf, dentry->d_name);
578 if (stat(pbuf, &stat_buf)!=0) {
579 #ifdef VERBOSE
580 printf(_("WINGs: could not stat %s\n"), pbuf);
581 #endif
582 continue;
583 } else {
584 int isDirectory;
586 isDirectory = S_ISDIR(stat_buf.st_mode);
588 if (filterFileName(panel, dentry->d_name, isDirectory))
589 WMInsertBrowserItem(bPtr, column, -1, dentry->d_name, isDirectory);
592 WMSortBrowserColumnWithComparer(bPtr, column, comparer);
594 closedir(dir);
598 static void
599 fillColumn(WMBrowserDelegate *self, WMBrowser *bPtr, int column, WMList *list)
601 char *path;
602 WMFilePanel *panel;
604 if (column > 0) {
605 path = WMGetBrowserPathToColumn(bPtr, column-1);
606 } else {
607 path = wstrdup("/");
610 panel = WMGetHangedData(bPtr);
611 listDirectoryOnColumn(panel, column, path);
612 wfree(path);
616 static void
617 browserDClick(WMBrowser *bPtr, WMFilePanel *panel)
619 WMPerformButtonClick(panel->okButton);
622 static void
623 browserClick(WMBrowser *bPtr, WMFilePanel *panel)
625 int col = WMGetBrowserSelectedColumn(bPtr);
626 WMListItem *item = WMGetBrowserSelectedItemInColumn(bPtr, col);
628 if (!item || item->isBranch)
629 WMSetTextFieldText(panel->fileField, NULL);
630 else {
631 WMSetTextFieldText(panel->fileField, item->text);
636 static void
637 showError(WMScreen *scr, WMWindow *owner, char *s, char *file)
639 char *errStr;
641 if (file) {
642 errStr = wmalloc(strlen(file)+strlen(s));
643 sprintf(errStr, s, file);
644 } else {
645 errStr = wstrdup(s);
647 WMRunAlertPanel(scr, owner, _("Error"), errStr, _("OK"), NULL, NULL);
648 wfree(errStr);
652 static void
653 createDir(WMButton *bPre, WMFilePanel *panel)
655 char *dirName, *directory, *file, *s;
656 WMScreen *scr = WMWidgetScreen(panel->win);
658 dirName = WMRunInputPanel(scr, panel->win, _("Create Directory"),
659 _("Enter directory name"), "", _("OK"), _("Cancel"));
660 if (!dirName)
661 return;
663 directory = getCurrentFileName(panel);
664 s = strrchr(directory,'/');
665 if (s) s[1] = 0;
667 if (dirName[0] == '/') {
668 directory[0] = 0;
669 } else {
670 while ((s = strstr(directory,"//"))) {
671 int i;
672 for (i = 2;s[i] == '/';i++);
673 strcpy(s, &s[i-1]);
675 if ((s = strrchr(directory, '/')) && !s[1]) s[0] = 0;
677 while ((s = strstr(dirName,"//"))) {
678 int i;
679 for (i = 2;s[i] == '/';i++);
680 strcpy(s, &s[i-1]);
682 if ((s = strrchr(dirName, '/')) && !s[1]) s[0] = 0;
684 file = wmalloc(strlen(dirName)+strlen(directory)+4);
685 sprintf(file, "%s/%s", directory, dirName);
686 while ((s = strstr(file,"//"))) {
687 int i;
688 for (i = 2;s[i] == '/';i++);
689 strcpy(s, &s[i-1]);
692 if (mkdir(file,0xfff) != 0) {
693 switch (errno) {
694 case EACCES:
695 showError(scr, panel->win, _("Permission denied."), NULL);
696 break;
697 case EEXIST:
698 showError(scr, panel->win, _("'%s' already exists."), file);
699 break;
700 case ENOENT:
701 showError(scr, panel->win, _("Path does not exist."), NULL);
704 else WMSetFilePanelDirectory(panel, file);
706 wfree(dirName);
707 wfree(directory);
708 wfree(file);
711 static void
712 deleteFile(WMButton *bPre, WMFilePanel *panel)
714 char *file;
715 char *buffer, *s;
716 struct stat filestat;
717 WMScreen *scr = WMWidgetScreen(panel->win);
719 file = getCurrentFileName(panel);
721 while ((s = strstr(file,"//"))) {
722 int i;
723 for (i = 2;s[i] == '/';i++);
724 strcpy(s, &s[i-1]);
726 if (strlen(file) > 1 && (s = strrchr(file, '/')) && !s[1]) s[0] = 0;
728 if (stat(file,&filestat)) {
729 switch (errno) {
730 case ENOENT:
731 showError(scr, panel->win, _("'%s' does not exist."), file);
732 break;
733 case EACCES:
734 showError(scr, panel->win, _("Permission denied."), NULL);
735 break;
736 case ENOMEM:
737 showError(scr, panel->win,
738 _("Insufficient memory available."), NULL);
739 break;
740 case EROFS:
741 showError(scr, panel->win,
742 _("'%s' is on a read-only filesystem."), file);
743 break;
744 default:
745 showError(scr, panel->win, _("Can not delete '%s'."), file);
747 wfree(file);
748 return;
749 } else if (S_ISDIR(filestat.st_mode)) {
750 int len = strlen(file)+20;
751 buffer = wmalloc(len);
752 snprintf(buffer,len,_("Delete directory %s ?"),file);
753 } else {
754 int len = strlen(file)+15;
755 buffer = wmalloc(len);
756 snprintf(buffer,len,_("Delete file %s ?"),file);
759 if (!WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win,
760 _("Warning"), buffer, _("OK"), _("Cancel"), NULL)) {
761 if (S_ISDIR(filestat.st_mode)) {
762 if (rmdir(file) != 0) {
763 switch (errno) {
764 case EACCES:
765 showError(scr, panel->win, _("Permission denied."), NULL);
766 break;
767 case ENOENT:
768 showError(scr, panel->win, _("Directory '%s' does not exist."), file);
769 break;
770 case ENOTEMPTY:
771 showError(scr, panel->win, _("Directory '%s' is not empty."), file);
772 break;
773 case EBUSY:
774 showError(scr, panel->win, _("Directory '%s' is busy."), file);
775 break;
776 default:
777 showError(scr, panel->win, _("Can not delete '%s'."), file);
779 } else {
780 char *s = strrchr(file,'/');
781 if (s) s[0] = 0;
782 WMSetFilePanelDirectory(panel, file);
784 } else if (remove(file) != 0) {
785 switch (errno) {
786 case EISDIR:
787 showError(scr, panel->win, _("'%s' is a directory."), file);
788 break;
789 case ENOENT:
790 showError(scr, panel->win, _("'%s' does not exist."), file);
791 break;
792 case EACCES:
793 showError(scr, panel->win, _("Permission denied."), NULL);
794 break;
795 case ENOMEM:
796 showError(scr, panel->win,
797 _("Insufficient memory available."), NULL);
798 break;
799 case EROFS:
800 showError(scr, panel->win,
801 _("'%s' is on a read-only filesystem."), file);
802 break;
803 default:
804 showError(scr, panel->win, _("Can not delete '%s'."), file);
806 } else {
807 char *s = strrchr(file,'/');
808 if (s) s[1] = 0;
809 WMSetFilePanelDirectory(panel, file);
812 wfree(buffer);
813 wfree(file);
816 static void
817 goUnmount(WMButton *bPtr, WMFilePanel *panel)
822 static void
823 goFloppy(WMButton *bPtr, WMFilePanel *panel)
825 struct stat filestat;
826 WMScreen *scr = WMWidgetScreen(panel->win);
828 if (stat(WINGsConfiguration.floppyPath, &filestat)) {
829 showError(scr, panel->win, _("An error occured browsing '%s'."),
830 WINGsConfiguration.floppyPath);
831 return;
832 } else if (!S_ISDIR(filestat.st_mode)) {
833 showError(scr, panel->win, _("'%s' is not a directory."),
834 WINGsConfiguration.floppyPath);
835 return;
838 WMSetFilePanelDirectory(panel, WINGsConfiguration.floppyPath);
842 static void
843 goHome(WMButton *bPtr, WMFilePanel *panel)
845 char *home;
847 /* home is statically allocated. Don't free it! */
848 home = wgethomedir();
849 if (!home)
850 return;
852 WMSetFilePanelDirectory(panel, home);
856 static void
857 handleEvents(XEvent *event, void *data)
859 W_FilePanel *pPtr = (W_FilePanel*)data;
860 W_View *view = WMWidgetView(pPtr->win);
862 if (event->type == ConfigureNotify) {
863 if (event->xconfigure.width != view->size.width
864 || event->xconfigure.height != view->size.height) {
865 unsigned int newWidth = event->xconfigure.width;
866 unsigned int newHeight = event->xconfigure.height;
867 int newColumnCount;
869 W_ResizeView(view, newWidth, newHeight);
870 WMResizeWidget(pPtr->line, newWidth, 2);
871 WMResizeWidget(pPtr->browser, newWidth-14,
872 newHeight-(PHEIGHT-200));
873 WMResizeWidget(pPtr->fileField, newWidth-60-10, 24);
874 WMMoveWidget(pPtr->nameLabel, 7, newHeight-(PHEIGHT-282));
875 WMMoveWidget(pPtr->fileField, 60, newHeight-(PHEIGHT-278));
876 WMMoveWidget(pPtr->okButton, newWidth-(PWIDTH-245),
877 newHeight-(PHEIGHT-325));
878 WMMoveWidget(pPtr->cancelButton, newWidth-(PWIDTH-165),
879 newHeight-(PHEIGHT-325));
881 WMMoveWidget(pPtr->trashcanButton, 7, newHeight-(PHEIGHT-325));
882 WMMoveWidget(pPtr->createDirButton, 37, newHeight-(PHEIGHT-325));
883 WMMoveWidget(pPtr->homeButton, 67, newHeight-(PHEIGHT-325));
884 WMMoveWidget(pPtr->disketteButton, 97, newHeight-(PHEIGHT-325));
885 WMMoveWidget(pPtr->unmountButton, 127, newHeight-(PHEIGHT-325));
887 newColumnCount = (newWidth - 14) / 140;
888 WMSetBrowserMaxVisibleColumns(pPtr->browser, newColumnCount);
894 static char*
895 getCurrentFileName(WMFilePanel *panel)
897 char *path;
898 char *file;
899 char *tmp;
900 int len;
902 path = WMGetBrowserPath(panel->browser);
904 len = strlen(path);
905 if (path[len-1]=='/') {
906 file = WMGetTextFieldText(panel->fileField);
907 tmp = wmalloc(strlen(path)+strlen(file)+8);
908 if (file[0]!='/') {
909 strcpy(tmp, path);
910 strcat(tmp, file);
911 } else
912 strcpy(tmp, file);
914 wfree(file);
915 wfree(path);
916 return tmp;
917 } else {
918 return path;
924 static Bool
925 validOpenFile(WMFilePanel *panel)
927 WMListItem *item;
928 int col, haveFile = 0;
929 char *file = WMGetTextFieldText(panel->fileField);
931 if (file[0] != '\0')
932 haveFile = 1;
933 wfree(file);
935 col = WMGetBrowserSelectedColumn(panel->browser);
936 item = WMGetBrowserSelectedItemInColumn(panel->browser, col);
937 if (item) {
938 if (item->isBranch && !panel->flags.canChooseDirectories && !haveFile)
939 return False;
940 else if (!item->isBranch && !panel->flags.canChooseFiles)
941 return False;
942 else
943 return True;
944 } else {
945 /* we compute for / here */
946 if (!panel->flags.canChooseDirectories && !haveFile)
947 return False;
948 else
949 return True;
951 return True;
956 static void
957 buttonClick(WMButton *bPtr, WMFilePanel *panel)
959 WMRange range;
961 if (bPtr == panel->okButton) {
962 if (!validOpenFile(panel))
963 return;
964 if (panel->flags.fileMustExist) {
965 char *file;
967 file = getCurrentFileName(panel);
968 if (access(file, F_OK)!=0) {
969 WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win,
970 _("Error"), _("File does not exist."),
971 _("OK"), NULL, NULL);
972 wfree(file);
973 return;
975 wfree(file);
977 panel->flags.canceled = 0;
978 } else
979 panel->flags.canceled = 1;
981 range.count = range.position = 0;
982 WMSelectTextFieldRange(panel->fileField, range);
983 WMBreakModalLoop(WMWidgetScreen(bPtr));