14 typedef struct W_FilePanel
{
26 WMButton
*cancelButton
;
30 WMView
*accessoryView
;
32 WMTextField
*fileField
;
37 unsigned int canExit
:1;
38 unsigned int canceled
:1; /* clicked on cancel */
40 unsigned int filtered
:1;
41 unsigned int canChooseFiles
:1;
42 unsigned int canChooseDirectories
:1;
43 unsigned int autoCompletion
:1;
44 unsigned int showAllFiles
:1;
45 unsigned int canFreeFileTypes
:1;
46 unsigned int fileMustExist
:1;
47 unsigned int panelType
:1;
59 static void listDirectoryOnColumn(WMFilePanel
*panel
, int column
, char *path
);
60 static void browserClick();
61 static void browserDClick();
63 static void fillColumn(WMBrowserDelegate
*self
, WMBrowser
*bPtr
, int column
,
68 static void buttonClick();
70 static char *getCurrentFileName(WMFilePanel
*panel
);
72 static void handleEvents(XEvent
*event
, void *data
);
76 static WMBrowserDelegate browserDelegate
= {
78 fillColumn
, /* createRowsForColumn */
79 NULL
, /* titleOfColumn */
86 closestListItem(WMList
*list
, char *text
, Bool exact
)
88 WMListItem
*item
= WMGetListItem(list
, 0);
90 int len
= strlen(text
);
96 if (strlen(item
->text
) >= len
&&
97 ((exact
&& strcmp(item
->text
, text
)==0) ||
98 (!exact
&& strncmp(item
->text
, text
, len
)==0))) {
101 item
= item
->nextPtr
;
109 textChangedObserver(void *observerData
, WMNotification
*notification
)
111 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
114 int col
= WMGetBrowserNumberOfColumns(panel
->browser
) - 1;
117 if (!(list
= WMGetBrowserListInColumn(panel
->browser
, col
)))
120 text
= WMGetTextFieldText(panel
->fileField
);
121 textEvent
= (int)WMGetNotificationClientData(notification
);
123 if (panel
->flags
.autoCompletion
&& textEvent
!=WMDeleteTextEvent
)
124 i
= closestListItem(list
, text
, False
);
126 i
= closestListItem(list
, text
, True
);
128 WMSelectListItem(list
, i
);
129 if (i
>=0 && panel
->flags
.autoCompletion
) {
130 WMListItem
*item
= WMGetListItem(list
, i
);
131 int textLen
= strlen(text
), itemTextLen
= strlen(item
->text
);
132 int visibleItems
= WMWidgetHeight(list
)/WMGetListItemHeight(list
);
134 WMSetListPosition(list
, i
- visibleItems
/2);
136 if (textEvent
!=WMDeleteTextEvent
) {
139 WMInsertTextFieldText(panel
->fileField
, &item
->text
[textLen
],
141 WMSetTextFieldCursorPosition(panel
->fileField
, itemTextLen
);
142 range
.position
= textLen
;
143 range
.count
= itemTextLen
- textLen
;
144 WMSelectTextFieldRange(panel
->fileField
, range
);
153 textEditedObserver(void *observerData
, WMNotification
*notification
)
155 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
157 if ((int)WMGetNotificationClientData(notification
)==WMReturnTextMovement
) {
158 WMPerformButtonClick(panel
->okButton
);
165 makeFilePanel(WMScreen
*scrPtr
, char *name
, char *title
)
170 fPtr
= wmalloc(sizeof(WMFilePanel
));
171 memset(fPtr
, 0, sizeof(WMFilePanel
));
173 fPtr
->win
= WMCreateWindowWithStyle(scrPtr
, name
, WMTitledWindowMask
174 |WMResizableWindowMask
);
175 WMResizeWidget(fPtr
->win
, PWIDTH
, PHEIGHT
);
176 WMSetWindowTitle(fPtr
->win
, "");
178 WMCreateEventHandler(WMWidgetView(fPtr
->win
), StructureNotifyMask
,
180 WMSetWindowMinSize(fPtr
->win
, PWIDTH
, PHEIGHT
);
183 fPtr
->iconLabel
= WMCreateLabel(fPtr
->win
);
184 WMResizeWidget(fPtr
->iconLabel
, 64, 64);
185 WMMoveWidget(fPtr
->iconLabel
, 0, 0);
186 WMSetLabelImagePosition(fPtr
->iconLabel
, WIPImageOnly
);
187 WMSetLabelImage(fPtr
->iconLabel
, scrPtr
->applicationIcon
);
189 fPtr
->titleLabel
= WMCreateLabel(fPtr
->win
);
190 WMResizeWidget(fPtr
->titleLabel
, PWIDTH
-64, 64);
191 WMMoveWidget(fPtr
->titleLabel
, 64, 0);
192 largeFont
= WMBoldSystemFontOfSize(scrPtr
, 24);
193 WMSetLabelFont(fPtr
->titleLabel
, largeFont
);
194 WMReleaseFont(largeFont
);
195 WMSetLabelText(fPtr
->titleLabel
, title
);
197 fPtr
->line
= WMCreateFrame(fPtr
->win
);
198 WMMoveWidget(fPtr
->line
, 0, 64);
199 WMResizeWidget(fPtr
->line
, PWIDTH
, 2);
200 WMSetFrameRelief(fPtr
->line
, WRGroove
);
202 fPtr
->browser
= WMCreateBrowser(fPtr
->win
);
203 WMSetBrowserDelegate(fPtr
->browser
, &browserDelegate
);
204 WMSetBrowserAction(fPtr
->browser
, browserClick
, fPtr
);
205 WMSetBrowserDoubleAction(fPtr
->browser
, browserDClick
, fPtr
);
206 WMMoveWidget(fPtr
->browser
, 7, 72);
207 WMHangData(fPtr
->browser
, fPtr
);
209 fPtr
->nameLabel
= WMCreateLabel(fPtr
->win
);
210 WMMoveWidget(fPtr
->nameLabel
, 7, 282);
211 WMResizeWidget(fPtr
->nameLabel
, 55, 14);
212 WMSetLabelText(fPtr
->nameLabel
, "Name:");
214 fPtr
->fileField
= WMCreateTextField(fPtr
->win
);
215 WMMoveWidget(fPtr
->fileField
, 60, 278);
216 WMResizeWidget(fPtr
->fileField
, PWIDTH
-60-10, 24);
217 WMAddNotificationObserver(textEditedObserver
, fPtr
,
218 WMTextDidEndEditingNotification
,
220 WMAddNotificationObserver(textChangedObserver
, fPtr
,
221 WMTextDidChangeNotification
,
224 fPtr
->okButton
= WMCreateCommandButton(fPtr
->win
);
225 WMMoveWidget(fPtr
->okButton
, 230, 325);
226 WMResizeWidget(fPtr
->okButton
, 80, 28);
227 WMSetButtonText(fPtr
->okButton
, "OK");
228 WMSetButtonImage(fPtr
->okButton
, scrPtr
->buttonArrow
);
229 WMSetButtonAltImage(fPtr
->okButton
, scrPtr
->pushedButtonArrow
);
230 WMSetButtonImagePosition(fPtr
->okButton
, WIPRight
);
231 WMSetButtonAction(fPtr
->okButton
, buttonClick
, fPtr
);
233 fPtr
->cancelButton
= WMCreateCommandButton(fPtr
->win
);
234 WMMoveWidget(fPtr
->cancelButton
, 140, 325);
235 WMResizeWidget(fPtr
->cancelButton
, 80, 28);
236 WMSetButtonText(fPtr
->cancelButton
, "Cancel");
237 WMSetButtonAction(fPtr
->cancelButton
, buttonClick
, fPtr
);
239 fPtr
->homeButton
= WMCreateCommandButton(fPtr
->win
);
240 WMMoveWidget(fPtr
->homeButton
, 55, 325);
241 WMResizeWidget(fPtr
->homeButton
, 28, 28);
242 WMSetButtonImagePosition(fPtr
->homeButton
, WIPImageOnly
);
243 WMSetButtonImage(fPtr
->homeButton
, scrPtr
->homeIcon
);
244 WMSetButtonAltImage(fPtr
->homeButton
, scrPtr
->altHomeIcon
);
245 WMSetButtonAction(fPtr
->homeButton
, goHome
, fPtr
);
247 WMRealizeWidget(fPtr
->win
);
248 WMMapSubwidgets(fPtr
->win
);
250 WMSetFocusToWidget(fPtr
->fileField
);
251 WMSetTextFieldCursorPosition(fPtr
->fileField
, 0);
253 WMLoadBrowserColumnZero(fPtr
->browser
);
255 fPtr
->flags
.canChooseFiles
= 1;
256 fPtr
->flags
.canChooseDirectories
= 1;
257 fPtr
->flags
.autoCompletion
= 1;
264 WMGetOpenPanel(WMScreen
*scrPtr
)
268 if (scrPtr
->sharedOpenPanel
)
269 return scrPtr
->sharedOpenPanel
;
271 panel
= makeFilePanel(scrPtr
, "openFilePanel", "Open");
272 panel
->flags
.fileMustExist
= 1;
273 panel
->flags
.panelType
= WP_OPEN
;
275 scrPtr
->sharedOpenPanel
= panel
;
282 WMGetSavePanel(WMScreen
*scrPtr
)
286 if (scrPtr
->sharedSavePanel
)
287 return scrPtr
->sharedSavePanel
;
289 panel
= makeFilePanel(scrPtr
, "saveFilePanel", "Save");
290 panel
->flags
.fileMustExist
= 0;
291 panel
->flags
.panelType
= WP_SAVE
;
293 scrPtr
->sharedSavePanel
= panel
;
300 WMFreeFilePanel(WMFilePanel
*panel
)
302 if (panel
== WMWidgetScreen(panel
->win
)->sharedSavePanel
) {
303 WMWidgetScreen(panel
->win
)->sharedSavePanel
= NULL
;
305 if (panel
== WMWidgetScreen(panel
->win
)->sharedOpenPanel
) {
306 WMWidgetScreen(panel
->win
)->sharedOpenPanel
= NULL
;
308 WMRemoveNotificationObserver(panel
);
309 WMUnmapWidget(panel
->win
);
310 WMDestroyWidget(panel
->win
);
316 WMRunModalFilePanelForDirectory(WMFilePanel
*panel
, WMWindow
*owner
,
317 char *path
, char *name
, char **fileTypes
)
319 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
322 if (name
&& !owner
) {
323 WMSetWindowTitle(panel
->win
, name
);
326 WMChangePanelOwner(panel
->win
, owner
);
328 WMSetFilePanelDirectory(panel
, path
);
330 panel
->flags
.done
= 0;
331 switch(panel
->flags
.panelType
) {
334 panel
->flags
.filtered
= 1;
335 panel
->fileTypes
= fileTypes
;
340 panel
->fileTypes
= NULL
;
341 panel
->flags
.filtered
= 0;
349 WMSetWindowUPosition(panel
->win
,
350 (scr
->rootView
->size
.width
- WMWidgetWidth(panel
->win
))/2,
351 (scr
->rootView
->size
.height
- WMWidgetHeight(panel
->win
))/2);
352 WMSetLabelText(panel
->titleLabel
, name
);
354 scr
->modalView
= W_VIEW(panel
->win
);
355 WMMapWidget(panel
->win
);
358 while (!panel
->flags
.done
) {
359 WMNextEvent(scr
->display
, &event
);
360 WMHandleEvent(&event
);
364 /* Must withdraw window because the next time we map
365 * it, it might have a different transient owner.
367 WMCloseWindow(panel
->win
);
369 return (panel
->flags
.canceled
? False
: True
);
376 WMSetFilePanelDirectory(WMFilePanel
*panel
, char *path
)
383 rest
= WMSetBrowserPath(panel
->browser
, path
);
384 if (strcmp(path
, "/")==0)
387 col
= WMGetBrowserSelectedColumn(panel
->browser
);
388 list
= WMGetBrowserListInColumn(panel
->browser
, col
);
389 if (list
&& (item
= WMGetListSelectedItem(list
))) {
390 if (item
->isBranch
) {
391 WMSetTextFieldText(panel
->fileField
, rest
);
393 WMSetTextFieldText(panel
->fileField
, item
->text
);
396 WMSetTextFieldText(panel
->fileField
, rest
);
402 WMSetFilePanelCanChooseDirectories(WMFilePanel
*panel
, Bool flag
)
404 panel
->flags
.canChooseDirectories
= flag
;
408 WMSetFilePanelCanChooseFiles(WMFilePanel
*panel
, Bool flag
)
410 panel
->flags
.canChooseFiles
= flag
;
415 WMSetFilePanelAutoCompletion(WMFilePanel
*panel
, Bool flag
)
417 panel
->flags
.autoCompletion
= flag
;
422 WMGetFilePanelFileName(WMFilePanel
*panel
)
424 return getCurrentFileName(panel
);
429 WMSetFilePanelAccessoryView(WMFilePanel
*panel
, WMView
*view
)
433 panel
->accessoryView
= view
;
435 v
= WMWidgetView(panel
->win
);
437 W_ReparentView(view
, v
, 0, 0);
439 W_MoveView(view
, (v
->size
.width
- v
->size
.width
)/2, 300);
444 WMGetFilePanelAccessoryView(WMFilePanel
*panel
)
446 return panel
->accessoryView
;
451 get_name_from_path(char *path
)
459 /* remove trailing / */
460 while (size
> 0 && path
[size
-1]=='/')
462 /* directory was root */
466 while (size
> 0 && path
[size
-1] != '/')
469 return wstrdup(&(path
[size
]));
474 filterFileName(WMFilePanel
*panel
, char *file
, Bool isDirectory
)
481 listDirectoryOnColumn(WMFilePanel
*panel
, int column
, char *path
)
483 WMBrowser
*bPtr
= panel
->browser
;
484 struct dirent
*dentry
;
486 struct stat stat_buf
;
487 char pbuf
[PATH_MAX
+16];
490 assert(path
!= NULL
);
492 /* put directory name in the title */
493 WMSetBrowserColumnTitle(bPtr
, column
, get_name_from_path(path
));
499 printf("WINGs: could not open directory %s\n", path
);
504 /* list contents in the column */
505 while ((dentry
= readdir(dir
))) {
506 if (strcmp(dentry
->d_name
, ".")==0 ||
507 strcmp(dentry
->d_name
, "..")==0)
511 if (strcmp(path
, "/")!=0)
513 strcat(pbuf
, dentry
->d_name
);
515 if (stat(pbuf
, &stat_buf
)!=0) {
517 printf("WINGs: could not stat %s\n", pbuf
);
523 isDirectory
= S_ISDIR(stat_buf
.st_mode
);
525 if (filterFileName(panel
, dentry
->d_name
, isDirectory
))
526 WMAddSortedBrowserItem(bPtr
, column
, dentry
->d_name
,
536 fillColumn(WMBrowserDelegate
*self
, WMBrowser
*bPtr
, int column
, WMList
*list
)
542 path
= WMGetBrowserPathToColumn(bPtr
, column
-1);
547 panel
= WMGetHangedData(bPtr
);
548 listDirectoryOnColumn(panel
, column
, path
);
554 browserDClick(WMBrowser
*bPtr
, WMFilePanel
*panel
)
556 WMPerformButtonClick(panel
->okButton
);
560 browserClick(WMBrowser
*bPtr
, WMFilePanel
*panel
)
562 int col
= WMGetBrowserSelectedColumn(bPtr
);
563 WMListItem
*item
= WMGetBrowserSelectedItemInColumn(bPtr
, col
);
565 if (!item
|| item
->isBranch
)
566 WMSetTextFieldText(panel
->fileField
, NULL
);
568 WMSetTextFieldText(panel
->fileField
, item
->text
);
574 goHome(WMButton
*bPtr
, WMFilePanel
*panel
)
578 /* home is statically allocated. Don't free it! */
579 home
= wgethomedir();
583 WMSetFilePanelDirectory(panel
, home
);
588 handleEvents(XEvent
*event
, void *data
)
590 W_FilePanel
*pPtr
= (W_FilePanel
*)data
;
591 W_View
*view
= WMWidgetView(pPtr
->win
);
593 if (event
->type
== ConfigureNotify
) {
594 if (event
->xconfigure
.width
!= view
->size
.width
595 || event
->xconfigure
.height
!= view
->size
.height
) {
596 unsigned int newWidth
= event
->xconfigure
.width
;
597 unsigned int newHeight
= event
->xconfigure
.height
;
600 W_ResizeView(view
, newWidth
, newHeight
);
601 WMResizeWidget(pPtr
->line
, newWidth
, 2);
602 WMResizeWidget(pPtr
->browser
, newWidth
-14,
603 newHeight
-(PHEIGHT
-200));
604 WMResizeWidget(pPtr
->fileField
, newWidth
-60-10, 24);
605 WMMoveWidget(pPtr
->nameLabel
, 7, newHeight
-(PHEIGHT
-282));
606 WMMoveWidget(pPtr
->fileField
, 60, newHeight
-(PHEIGHT
-278));
607 WMMoveWidget(pPtr
->okButton
, newWidth
-(PWIDTH
-230),
608 newHeight
-(PHEIGHT
-325));
609 WMMoveWidget(pPtr
->cancelButton
, newWidth
-(PWIDTH
-140),
610 newHeight
-(PHEIGHT
-325));
611 WMMoveWidget(pPtr
->homeButton
, 55, newHeight
-(PHEIGHT
-325));
613 newColumnCount
= (newWidth
- 14) / 140;
614 WMSetBrowserMaxVisibleColumns(pPtr
->browser
, newColumnCount
);
621 getCurrentFileName(WMFilePanel
*panel
)
628 path
= WMGetBrowserPath(panel
->browser
);
631 if (path
[len
-1]=='/') {
632 file
= WMGetTextFieldText(panel
->fileField
);
633 tmp
= wmalloc(strlen(path
)+strlen(file
)+8);
651 validOpenFile(WMFilePanel
*panel
)
654 int col
, haveFile
= 0;
655 char *file
= WMGetTextFieldText(panel
->fileField
);
661 col
= WMGetBrowserSelectedColumn(panel
->browser
);
662 item
= WMGetBrowserSelectedItemInColumn(panel
->browser
, col
);
664 if (item
->isBranch
&& !panel
->flags
.canChooseDirectories
&& !haveFile
)
666 else if (!item
->isBranch
&& !panel
->flags
.canChooseFiles
)
671 /* we compute for / here */
672 if (!panel
->flags
.canChooseDirectories
&& !haveFile
)
683 buttonClick(WMButton
*bPtr
, WMFilePanel
*panel
)
687 if (bPtr
== panel
->okButton
) {
688 if (!validOpenFile(panel
))
690 if (panel
->flags
.fileMustExist
) {
693 file
= getCurrentFileName(panel
);
694 if (access(file
, F_OK
)!=0) {
695 WMRunAlertPanel(WMWidgetScreen(panel
->win
), panel
->win
,
696 "Error", "File does not exist.",
703 panel
->flags
.canceled
= 0;
705 panel
->flags
.canceled
= 1;
707 range
.count
= range
.position
= 0;
708 WMSelectTextFieldRange(panel
->fileField
, range
);
709 panel
->flags
.done
= 1;