17 typedef struct W_FilePanel
{
29 WMButton
*cancelButton
;
32 WMButton
*trashcanButton
;
33 WMButton
*createDirButton
;
34 WMButton
*disketteButton
;
35 WMButton
*unmountButton
;
37 WMView
*accessoryView
;
39 WMTextField
*fileField
;
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;
64 static void listDirectoryOnColumn(WMFilePanel
* panel
, int column
, char *path
);
65 static void browserClick();
66 static void browserDClick();
68 static void fillColumn(WMBrowserDelegate
* self
, WMBrowser
* bPtr
, int column
, WMList
* list
);
70 static void deleteFile();
72 static void createDir();
76 static void goFloppy();
78 static void goUnmount();
80 static void buttonClick();
82 static char *getCurrentFileName(WMFilePanel
* panel
);
84 static void handleEvents(XEvent
* event
, void *data
);
86 static WMBrowserDelegate browserDelegate
= {
88 fillColumn
, /* createRowsForColumn */
89 NULL
, /* titleOfColumn */
94 static int closestListItem(WMList
* list
, char *text
, Bool exact
)
97 WMArray
*items
= WMGetListItems(list
);
98 int i
, len
= strlen(text
);
103 for (i
= 0; i
< WMGetArrayItemCount(items
); i
++) {
104 item
= WMGetFromArray(items
, i
);
105 if (strlen(item
->text
) >= len
&&
106 ((exact
&& strcmp(item
->text
, text
) == 0) ||
107 (!exact
&& strncmp(item
->text
, text
, len
) == 0))) {
115 static void textChangedObserver(void *observerData
, WMNotification
* notification
)
117 W_FilePanel
*panel
= (W_FilePanel
*) observerData
;
120 int col
= WMGetBrowserNumberOfColumns(panel
->browser
) - 1;
124 if (!(list
= WMGetBrowserListInColumn(panel
->browser
, col
)))
127 text
= WMGetTextFieldText(panel
->fileField
);
128 textEvent
= (uintptr_t)WMGetNotificationClientData(notification
);
130 if (panel
->flags
.autoCompletion
&& textEvent
!= WMDeleteTextEvent
)
131 i
= closestListItem(list
, text
, False
);
133 i
= closestListItem(list
, text
, True
);
135 WMSelectListItem(list
, i
);
136 if (i
>= 0 && panel
->flags
.autoCompletion
) {
137 WMListItem
*item
= WMGetListItem(list
, i
);
138 int textLen
= strlen(text
), itemTextLen
= strlen(item
->text
);
139 int visibleItems
= WMWidgetHeight(list
) / WMGetListItemHeight(list
);
141 WMSetListPosition(list
, i
- visibleItems
/ 2);
143 if (textEvent
!= WMDeleteTextEvent
) {
146 WMInsertTextFieldText(panel
->fileField
, &item
->text
[textLen
], textLen
);
147 range
.position
= textLen
;
148 range
.count
= itemTextLen
- textLen
;
149 WMSelectTextFieldRange(panel
->fileField
, range
);
150 /*WMSetTextFieldCursorPosition(panel->fileField, itemTextLen); */
157 static void textEditedObserver(void *observerData
, WMNotification
* notification
)
159 W_FilePanel
*panel
= (W_FilePanel
*) observerData
;
161 if ((uintptr_t)WMGetNotificationClientData(notification
) == WMReturnTextMovement
) {
162 WMPerformButtonClick(panel
->okButton
);
166 static WMFilePanel
*makeFilePanel(WMScreen
* scrPtr
, char *name
, char *title
)
172 fPtr
= wmalloc(sizeof(WMFilePanel
));
173 memset(fPtr
, 0, sizeof(WMFilePanel
));
175 fPtr
->win
= WMCreateWindowWithStyle(scrPtr
, name
, WMTitledWindowMask
| WMResizableWindowMask
);
176 WMResizeWidget(fPtr
->win
, PWIDTH
, PHEIGHT
);
177 WMSetWindowTitle(fPtr
->win
, "");
179 WMCreateEventHandler(WMWidgetView(fPtr
->win
), StructureNotifyMask
, handleEvents
, fPtr
);
180 WMSetWindowMinSize(fPtr
->win
, PWIDTH
, PHEIGHT
);
182 fPtr
->iconLabel
= WMCreateLabel(fPtr
->win
);
183 WMResizeWidget(fPtr
->iconLabel
, 64, 64);
184 WMMoveWidget(fPtr
->iconLabel
, 0, 0);
185 WMSetLabelImagePosition(fPtr
->iconLabel
, WIPImageOnly
);
186 icon
= WMCreateApplicationIconBlendedPixmap(scrPtr
, (RColor
*) NULL
);
188 WMSetLabelImage(fPtr
->iconLabel
, icon
);
189 WMReleasePixmap(icon
);
191 WMSetLabelImage(fPtr
->iconLabel
, scrPtr
->applicationIconPixmap
);
194 fPtr
->titleLabel
= WMCreateLabel(fPtr
->win
);
195 WMResizeWidget(fPtr
->titleLabel
, PWIDTH
- 64, 64);
196 WMMoveWidget(fPtr
->titleLabel
, 64, 0);
197 largeFont
= WMBoldSystemFontOfSize(scrPtr
, 24);
198 WMSetLabelFont(fPtr
->titleLabel
, largeFont
);
199 WMReleaseFont(largeFont
);
200 WMSetLabelText(fPtr
->titleLabel
, title
);
202 fPtr
->line
= WMCreateFrame(fPtr
->win
);
203 WMMoveWidget(fPtr
->line
, 0, 64);
204 WMResizeWidget(fPtr
->line
, PWIDTH
, 2);
205 WMSetFrameRelief(fPtr
->line
, WRGroove
);
207 fPtr
->browser
= WMCreateBrowser(fPtr
->win
);
208 WMSetBrowserAllowEmptySelection(fPtr
->browser
, True
);
209 WMSetBrowserDelegate(fPtr
->browser
, &browserDelegate
);
210 WMSetBrowserAction(fPtr
->browser
, browserClick
, fPtr
);
211 WMSetBrowserDoubleAction(fPtr
->browser
, browserDClick
, fPtr
);
212 WMMoveWidget(fPtr
->browser
, 7, 72);
213 WMResizeWidget(fPtr
->browser
, PWIDTH
- 14, 200);
214 WMHangData(fPtr
->browser
, fPtr
);
216 fPtr
->nameLabel
= WMCreateLabel(fPtr
->win
);
217 WMMoveWidget(fPtr
->nameLabel
, 7, 282);
218 WMResizeWidget(fPtr
->nameLabel
, 55, 14);
219 WMSetLabelText(fPtr
->nameLabel
, _("Name:"));
221 fPtr
->fileField
= WMCreateTextField(fPtr
->win
);
222 WMMoveWidget(fPtr
->fileField
, 60, 278);
223 WMResizeWidget(fPtr
->fileField
, PWIDTH
- 60 - 10, 24);
224 WMAddNotificationObserver(textEditedObserver
, fPtr
, WMTextDidEndEditingNotification
, fPtr
->fileField
);
225 WMAddNotificationObserver(textChangedObserver
, fPtr
, WMTextDidChangeNotification
, fPtr
->fileField
);
227 fPtr
->okButton
= WMCreateCommandButton(fPtr
->win
);
228 WMMoveWidget(fPtr
->okButton
, 245, 325);
229 WMResizeWidget(fPtr
->okButton
, 75, 28);
230 WMSetButtonText(fPtr
->okButton
, _("OK"));
231 WMSetButtonImage(fPtr
->okButton
, scrPtr
->buttonArrow
);
232 WMSetButtonAltImage(fPtr
->okButton
, scrPtr
->pushedButtonArrow
);
233 WMSetButtonImagePosition(fPtr
->okButton
, WIPRight
);
234 WMSetButtonAction(fPtr
->okButton
, buttonClick
, fPtr
);
236 fPtr
->cancelButton
= WMCreateCommandButton(fPtr
->win
);
237 WMMoveWidget(fPtr
->cancelButton
, 165, 325);
238 WMResizeWidget(fPtr
->cancelButton
, 75, 28);
239 WMSetButtonText(fPtr
->cancelButton
, _("Cancel"));
240 WMSetButtonAction(fPtr
->cancelButton
, buttonClick
, fPtr
);
242 fPtr
->trashcanButton
= WMCreateCommandButton(fPtr
->win
);
243 WMMoveWidget(fPtr
->trashcanButton
, 7, 325);
244 WMResizeWidget(fPtr
->trashcanButton
, 28, 28);
245 WMSetButtonImagePosition(fPtr
->trashcanButton
, WIPImageOnly
);
246 WMSetButtonImage(fPtr
->trashcanButton
, scrPtr
->trashcanIcon
);
247 WMSetButtonAltImage(fPtr
->trashcanButton
, scrPtr
->altTrashcanIcon
);
248 WMSetButtonAction(fPtr
->trashcanButton
, deleteFile
, fPtr
);
250 fPtr
->createDirButton
= WMCreateCommandButton(fPtr
->win
);
251 WMMoveWidget(fPtr
->createDirButton
, 37, 325);
252 WMResizeWidget(fPtr
->createDirButton
, 28, 28);
253 WMSetButtonImagePosition(fPtr
->createDirButton
, WIPImageOnly
);
254 WMSetButtonImage(fPtr
->createDirButton
, scrPtr
->createDirIcon
);
255 WMSetButtonAltImage(fPtr
->createDirButton
, scrPtr
->altCreateDirIcon
);
256 WMSetButtonAction(fPtr
->createDirButton
, createDir
, fPtr
);
258 fPtr
->homeButton
= WMCreateCommandButton(fPtr
->win
);
259 WMMoveWidget(fPtr
->homeButton
, 67, 325);
260 WMResizeWidget(fPtr
->homeButton
, 28, 28);
261 WMSetButtonImagePosition(fPtr
->homeButton
, WIPImageOnly
);
262 WMSetButtonImage(fPtr
->homeButton
, scrPtr
->homeIcon
);
263 WMSetButtonAltImage(fPtr
->homeButton
, scrPtr
->altHomeIcon
);
264 WMSetButtonAction(fPtr
->homeButton
, goHome
, fPtr
);
266 fPtr
->disketteButton
= WMCreateCommandButton(fPtr
->win
);
267 WMMoveWidget(fPtr
->disketteButton
, 97, 325);
268 WMResizeWidget(fPtr
->disketteButton
, 28, 28);
269 WMSetButtonImagePosition(fPtr
->disketteButton
, WIPImageOnly
);
270 WMSetButtonImage(fPtr
->disketteButton
, scrPtr
->disketteIcon
);
271 WMSetButtonAltImage(fPtr
->disketteButton
, scrPtr
->altDisketteIcon
);
272 WMSetButtonAction(fPtr
->disketteButton
, goFloppy
, fPtr
);
274 fPtr
->unmountButton
= WMCreateCommandButton(fPtr
->win
);
275 WMMoveWidget(fPtr
->unmountButton
, 127, 325);
276 WMResizeWidget(fPtr
->unmountButton
, 28, 28);
277 WMSetButtonImagePosition(fPtr
->unmountButton
, WIPImageOnly
);
278 WMSetButtonImage(fPtr
->unmountButton
, scrPtr
->unmountIcon
);
279 WMSetButtonAltImage(fPtr
->unmountButton
, scrPtr
->altUnmountIcon
);
280 WMSetButtonAction(fPtr
->unmountButton
, goUnmount
, fPtr
);
281 WMSetButtonEnabled(fPtr
->unmountButton
, False
);
283 WMRealizeWidget(fPtr
->win
);
284 WMMapSubwidgets(fPtr
->win
);
286 WMSetFocusToWidget(fPtr
->fileField
);
287 WMSetTextFieldCursorPosition(fPtr
->fileField
, 0);
289 WMLoadBrowserColumnZero(fPtr
->browser
);
291 WMSetWindowInitialPosition(fPtr
->win
,
292 (scrPtr
->rootView
->size
.width
- WMWidgetWidth(fPtr
->win
)) / 2,
293 (scrPtr
->rootView
->size
.height
- WMWidgetHeight(fPtr
->win
)) / 2);
295 fPtr
->flags
.canChooseFiles
= 1;
296 fPtr
->flags
.canChooseDirectories
= 1;
297 fPtr
->flags
.autoCompletion
= 1;
302 WMOpenPanel
*WMGetOpenPanel(WMScreen
* scrPtr
)
306 if (scrPtr
->sharedOpenPanel
)
307 return scrPtr
->sharedOpenPanel
;
309 panel
= makeFilePanel(scrPtr
, "openFilePanel", _("Open"));
310 panel
->flags
.fileMustExist
= 1;
311 panel
->flags
.panelType
= WP_OPEN
;
313 scrPtr
->sharedOpenPanel
= panel
;
318 WMSavePanel
*WMGetSavePanel(WMScreen
* scrPtr
)
322 if (scrPtr
->sharedSavePanel
)
323 return scrPtr
->sharedSavePanel
;
325 panel
= makeFilePanel(scrPtr
, "saveFilePanel", _("Save"));
326 panel
->flags
.fileMustExist
= 0;
327 panel
->flags
.panelType
= WP_SAVE
;
329 scrPtr
->sharedSavePanel
= panel
;
334 void WMFreeFilePanel(WMFilePanel
* panel
)
336 if (panel
== WMWidgetScreen(panel
->win
)->sharedSavePanel
) {
337 WMWidgetScreen(panel
->win
)->sharedSavePanel
= NULL
;
339 if (panel
== WMWidgetScreen(panel
->win
)->sharedOpenPanel
) {
340 WMWidgetScreen(panel
->win
)->sharedOpenPanel
= NULL
;
342 WMRemoveNotificationObserver(panel
);
343 WMUnmapWidget(panel
->win
);
344 WMDestroyWidget(panel
->win
);
349 WMRunModalFilePanelForDirectory(WMFilePanel
* panel
, WMWindow
* owner
, char *path
, char *name
, char **fileTypes
)
351 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
353 if (name
&& !owner
) {
354 WMSetWindowTitle(panel
->win
, name
);
357 WMChangePanelOwner(panel
->win
, owner
);
359 WMSetFilePanelDirectory(panel
, path
);
361 switch (panel
->flags
.panelType
) {
364 panel
->flags
.filtered
= 1;
365 panel
->fileTypes
= fileTypes
;
370 panel
->fileTypes
= NULL
;
371 panel
->flags
.filtered
= 0;
379 WMSetLabelText(panel
->titleLabel
, name
);
381 WMMapWidget(panel
->win
);
383 WMRunModalLoop(scr
, W_VIEW(panel
->win
));
385 /* Must withdraw window because the next time we map
386 * it, it might have a different transient owner.
388 WMCloseWindow(panel
->win
);
390 return (panel
->flags
.canceled
? False
: True
);
393 void WMSetFilePanelDirectory(WMFilePanel
* panel
, char *path
)
400 rest
= WMSetBrowserPath(panel
->browser
, path
);
401 if (strcmp(path
, "/") == 0)
404 col
= WMGetBrowserSelectedColumn(panel
->browser
);
405 list
= WMGetBrowserListInColumn(panel
->browser
, col
);
406 if (list
&& (item
= WMGetListSelectedItem(list
))) {
407 if (item
->isBranch
) {
408 WMSetTextFieldText(panel
->fileField
, rest
);
410 WMSetTextFieldText(panel
->fileField
, item
->text
);
413 WMSetTextFieldText(panel
->fileField
, rest
);
417 void WMSetFilePanelCanChooseDirectories(WMFilePanel
* panel
, Bool flag
)
419 panel
->flags
.canChooseDirectories
= ((flag
== 0) ? 0 : 1);
422 void WMSetFilePanelCanChooseFiles(WMFilePanel
* panel
, Bool flag
)
424 panel
->flags
.canChooseFiles
= ((flag
== 0) ? 0 : 1);
427 void WMSetFilePanelAutoCompletion(WMFilePanel
* panel
, Bool flag
)
429 panel
->flags
.autoCompletion
= ((flag
== 0) ? 0 : 1);
432 char *WMGetFilePanelFileName(WMFilePanel
* panel
)
434 return getCurrentFileName(panel
);
437 void WMSetFilePanelAccessoryView(WMFilePanel
* panel
, WMView
* view
)
441 panel
->accessoryView
= view
;
443 v
= WMWidgetView(panel
->win
);
445 W_ReparentView(view
, v
, 0, 0);
447 W_MoveView(view
, (v
->size
.width
- v
->size
.width
) / 2, 300);
450 WMView
*WMGetFilePanelAccessoryView(WMFilePanel
* panel
)
452 return panel
->accessoryView
;
455 static char *get_name_from_path(char *path
)
459 assert(path
!= NULL
);
463 /* remove trailing / */
464 while (size
> 0 && path
[size
- 1] == '/')
466 /* directory was root */
470 while (size
> 0 && path
[size
- 1] != '/')
473 return wstrdup(&(path
[size
]));
476 static int filterFileName(WMFilePanel
* panel
, char *file
, Bool isDirectory
)
481 #define CAST(item) (*((WMListItem**)item))
482 static int comparer(const void *a
, const void *b
)
484 if (CAST(a
)->isBranch
== CAST(b
)->isBranch
)
485 return (strcmp(CAST(a
)->text
, CAST(b
)->text
));
486 if (CAST(a
)->isBranch
)
493 static void listDirectoryOnColumn(WMFilePanel
* panel
, int column
, char *path
)
495 WMBrowser
*bPtr
= panel
->browser
;
496 struct dirent
*dentry
;
498 struct stat stat_buf
;
499 char pbuf
[PATH_MAX
+ 16];
503 assert(path
!= NULL
);
505 /* put directory name in the title */
506 name
= get_name_from_path(path
);
507 WMSetBrowserColumnTitle(bPtr
, column
, name
);
514 printf(_("WINGs: could not open directory %s\n"), path
);
519 /* list contents in the column */
520 while ((dentry
= readdir(dir
))) {
521 if (strcmp(dentry
->d_name
, ".") == 0 || strcmp(dentry
->d_name
, "..") == 0)
525 if (strcmp(path
, "/") != 0)
527 strcat(pbuf
, dentry
->d_name
);
529 if (stat(pbuf
, &stat_buf
) != 0) {
531 printf(_("WINGs: could not stat %s\n"), pbuf
);
537 isDirectory
= S_ISDIR(stat_buf
.st_mode
);
539 if (filterFileName(panel
, dentry
->d_name
, isDirectory
))
540 WMInsertBrowserItem(bPtr
, column
, -1, dentry
->d_name
, isDirectory
);
543 WMSortBrowserColumnWithComparer(bPtr
, column
, comparer
);
548 static void fillColumn(WMBrowserDelegate
* self
, WMBrowser
* bPtr
, int column
, WMList
* list
)
554 path
= WMGetBrowserPathToColumn(bPtr
, column
- 1);
559 panel
= WMGetHangedData(bPtr
);
560 listDirectoryOnColumn(panel
, column
, path
);
564 static void browserDClick(WMBrowser
* bPtr
, WMFilePanel
* panel
)
566 WMPerformButtonClick(panel
->okButton
);
569 static void browserClick(WMBrowser
* bPtr
, WMFilePanel
* panel
)
571 int col
= WMGetBrowserSelectedColumn(bPtr
);
572 WMListItem
*item
= WMGetBrowserSelectedItemInColumn(bPtr
, col
);
574 if (!item
|| item
->isBranch
)
575 WMSetTextFieldText(panel
->fileField
, NULL
);
577 WMSetTextFieldText(panel
->fileField
, item
->text
);
581 static void showError(WMScreen
* scr
, WMWindow
* owner
, char *s
, char *file
)
586 errStr
= wmalloc(strlen(file
) + strlen(s
));
587 sprintf(errStr
, s
, file
);
591 WMRunAlertPanel(scr
, owner
, _("Error"), errStr
, _("OK"), NULL
, NULL
);
595 static void createDir(WMButton
* bPre
, WMFilePanel
* panel
)
597 char *dirName
, *directory
, *file
, *s
;
598 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
600 dirName
= WMRunInputPanel(scr
, panel
->win
, _("Create Directory"),
601 _("Enter directory name"), "", _("OK"), _("Cancel"));
605 directory
= getCurrentFileName(panel
);
606 s
= strrchr(directory
, '/');
610 if (dirName
[0] == '/') {
613 while ((s
= strstr(directory
, "//"))) {
615 for (i
= 2; s
[i
] == '/'; i
++) ;
616 strcpy(s
, &s
[i
- 1]);
618 if ((s
= strrchr(directory
, '/')) && !s
[1])
621 while ((s
= strstr(dirName
, "//"))) {
623 for (i
= 2; s
[i
] == '/'; i
++) ;
624 strcpy(s
, &s
[i
- 1]);
626 if ((s
= strrchr(dirName
, '/')) && !s
[1])
629 file
= wmalloc(strlen(dirName
) + strlen(directory
) + 4);
630 sprintf(file
, "%s/%s", directory
, dirName
);
631 while ((s
= strstr(file
, "//"))) {
633 for (i
= 2; s
[i
] == '/'; i
++) ;
634 strcpy(s
, &s
[i
- 1]);
637 if (mkdir(file
, 0xfff) != 0) {
640 showError(scr
, panel
->win
, _("Permission denied."), NULL
);
643 showError(scr
, panel
->win
, _("'%s' already exists."), file
);
646 showError(scr
, panel
->win
, _("Path does not exist."), NULL
);
649 WMSetFilePanelDirectory(panel
, file
);
656 static void deleteFile(WMButton
* bPre
, WMFilePanel
* panel
)
660 struct stat filestat
;
661 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
663 file
= getCurrentFileName(panel
);
665 while ((s
= strstr(file
, "//"))) {
667 for (i
= 2; s
[i
] == '/'; i
++) ;
668 strcpy(s
, &s
[i
- 1]);
670 if (strlen(file
) > 1 && (s
= strrchr(file
, '/')) && !s
[1])
673 if (stat(file
, &filestat
)) {
676 showError(scr
, panel
->win
, _("'%s' does not exist."), file
);
679 showError(scr
, panel
->win
, _("Permission denied."), NULL
);
682 showError(scr
, panel
->win
, _("Insufficient memory available."), NULL
);
685 showError(scr
, panel
->win
, _("'%s' is on a read-only filesystem."), file
);
688 showError(scr
, panel
->win
, _("Can not delete '%s'."), file
);
692 } else if (S_ISDIR(filestat
.st_mode
)) {
693 int len
= strlen(file
) + 20;
694 buffer
= wmalloc(len
);
695 snprintf(buffer
, len
, _("Delete directory %s ?"), file
);
697 int len
= strlen(file
) + 15;
698 buffer
= wmalloc(len
);
699 snprintf(buffer
, len
, _("Delete file %s ?"), file
);
702 if (!WMRunAlertPanel(WMWidgetScreen(panel
->win
), panel
->win
,
703 _("Warning"), buffer
, _("OK"), _("Cancel"), NULL
)) {
704 if (S_ISDIR(filestat
.st_mode
)) {
705 if (rmdir(file
) != 0) {
708 showError(scr
, panel
->win
, _("Permission denied."), NULL
);
711 showError(scr
, panel
->win
, _("Directory '%s' does not exist."), file
);
714 showError(scr
, panel
->win
, _("Directory '%s' is not empty."), file
);
717 showError(scr
, panel
->win
, _("Directory '%s' is busy."), file
);
720 showError(scr
, panel
->win
, _("Can not delete '%s'."), file
);
723 char *s
= strrchr(file
, '/');
726 WMSetFilePanelDirectory(panel
, file
);
728 } else if (remove(file
) != 0) {
731 showError(scr
, panel
->win
, _("'%s' is a directory."), file
);
734 showError(scr
, panel
->win
, _("'%s' does not exist."), file
);
737 showError(scr
, panel
->win
, _("Permission denied."), NULL
);
740 showError(scr
, panel
->win
, _("Insufficient memory available."), NULL
);
743 showError(scr
, panel
->win
, _("'%s' is on a read-only filesystem."), file
);
746 showError(scr
, panel
->win
, _("Can not delete '%s'."), file
);
749 char *s
= strrchr(file
, '/');
752 WMSetFilePanelDirectory(panel
, file
);
759 static void goUnmount(WMButton
* bPtr
, WMFilePanel
* panel
)
763 static void goFloppy(WMButton
* bPtr
, WMFilePanel
* panel
)
765 struct stat filestat
;
766 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
768 if (stat(WINGsConfiguration
.floppyPath
, &filestat
)) {
769 showError(scr
, panel
->win
, _("An error occured browsing '%s'."), WINGsConfiguration
.floppyPath
);
771 } else if (!S_ISDIR(filestat
.st_mode
)) {
772 showError(scr
, panel
->win
, _("'%s' is not a directory."), WINGsConfiguration
.floppyPath
);
776 WMSetFilePanelDirectory(panel
, WINGsConfiguration
.floppyPath
);
779 static void goHome(WMButton
* bPtr
, WMFilePanel
* panel
)
783 /* home is statically allocated. Don't free it! */
784 home
= wgethomedir();
788 WMSetFilePanelDirectory(panel
, home
);
791 static void handleEvents(XEvent
* event
, void *data
)
793 W_FilePanel
*pPtr
= (W_FilePanel
*) data
;
794 W_View
*view
= WMWidgetView(pPtr
->win
);
796 if (event
->type
== ConfigureNotify
) {
797 if (event
->xconfigure
.width
!= view
->size
.width
|| event
->xconfigure
.height
!= view
->size
.height
) {
798 unsigned int newWidth
= event
->xconfigure
.width
;
799 unsigned int newHeight
= event
->xconfigure
.height
;
802 W_ResizeView(view
, newWidth
, newHeight
);
803 WMResizeWidget(pPtr
->line
, newWidth
, 2);
804 WMResizeWidget(pPtr
->browser
, newWidth
- 14, newHeight
- (PHEIGHT
- 200));
805 WMResizeWidget(pPtr
->fileField
, newWidth
- 60 - 10, 24);
806 WMMoveWidget(pPtr
->nameLabel
, 7, newHeight
- (PHEIGHT
- 282));
807 WMMoveWidget(pPtr
->fileField
, 60, newHeight
- (PHEIGHT
- 278));
808 WMMoveWidget(pPtr
->okButton
, newWidth
- (PWIDTH
- 245), newHeight
- (PHEIGHT
- 325));
809 WMMoveWidget(pPtr
->cancelButton
, newWidth
- (PWIDTH
- 165), newHeight
- (PHEIGHT
- 325));
811 WMMoveWidget(pPtr
->trashcanButton
, 7, newHeight
- (PHEIGHT
- 325));
812 WMMoveWidget(pPtr
->createDirButton
, 37, newHeight
- (PHEIGHT
- 325));
813 WMMoveWidget(pPtr
->homeButton
, 67, newHeight
- (PHEIGHT
- 325));
814 WMMoveWidget(pPtr
->disketteButton
, 97, newHeight
- (PHEIGHT
- 325));
815 WMMoveWidget(pPtr
->unmountButton
, 127, newHeight
- (PHEIGHT
- 325));
817 newColumnCount
= (newWidth
- 14) / 140;
818 WMSetBrowserMaxVisibleColumns(pPtr
->browser
, newColumnCount
);
823 static char *getCurrentFileName(WMFilePanel
* panel
)
830 path
= WMGetBrowserPath(panel
->browser
);
833 if (path
[len
- 1] == '/') {
834 file
= WMGetTextFieldText(panel
->fileField
);
835 tmp
= wmalloc(strlen(path
) + strlen(file
) + 8);
836 if (file
[0] != '/') {
850 static Bool
validOpenFile(WMFilePanel
* panel
)
853 int col
, haveFile
= 0;
854 char *file
= WMGetTextFieldText(panel
->fileField
);
860 col
= WMGetBrowserSelectedColumn(panel
->browser
);
861 item
= WMGetBrowserSelectedItemInColumn(panel
->browser
, col
);
863 if (item
->isBranch
&& !panel
->flags
.canChooseDirectories
&& !haveFile
)
865 else if (!item
->isBranch
&& !panel
->flags
.canChooseFiles
)
870 /* we compute for / here */
871 if (!panel
->flags
.canChooseDirectories
&& !haveFile
)
879 static void buttonClick(WMButton
* bPtr
, WMFilePanel
* panel
)
883 if (bPtr
== panel
->okButton
) {
884 if (!validOpenFile(panel
))
886 if (panel
->flags
.fileMustExist
) {
889 file
= getCurrentFileName(panel
);
890 if (access(file
, F_OK
) != 0) {
891 WMRunAlertPanel(WMWidgetScreen(panel
->win
), panel
->win
,
892 _("Error"), _("File does not exist."), _("OK"), NULL
, NULL
);
898 panel
->flags
.canceled
= 0;
900 panel
->flags
.canceled
= 1;
902 range
.count
= range
.position
= 0;
903 WMSelectTextFieldRange(panel
->fileField
, range
);
904 WMBreakModalLoop(WMWidgetScreen(bPtr
));