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;
50 unsigned int ignoreTextChangeNotification
:1;
62 static void listDirectoryOnColumn(WMFilePanel
*panel
, int column
, char *path
);
63 static void browserClick();
64 static void browserDClick();
66 static void fillColumn(WMBrowser
*bPtr
, int column
);
70 static void buttonClick();
72 static char *getCurrentFileName(WMFilePanel
*panel
);
74 static void handleEvents(XEvent
*event
, void *data
);
78 closestListItem(WMList
*list
, char *text
, Bool exact
)
80 WMListItem
*item
= WMGetListItem(list
, 0);
82 int len
= strlen(text
);
88 if (strlen(item
->text
) >= len
&&
89 ((exact
&& strcmp(item
->text
, text
)==0) ||
90 (!exact
&& strncmp(item
->text
, text
, len
)==0))) {
101 textChangedObserver(void *observerData
, WMNotification
*notification
)
103 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
106 int col
= WMGetBrowserNumberOfColumns(panel
->browser
) - 1;
107 int i
, j
, textEvent
= (int)WMGetNotificationClientData(notification
);
108 static int running
= 0;
115 if (panel
->flags
.ignoreTextChangeNotification
)
118 list
= WMGetBrowserListInColumn(panel
->browser
, col
);
122 text
= WMGetTextFieldText(panel
->fileField
);
124 if (panel
->flags
.autoCompletion
&& textEvent
!=WMDeleteTextEvent
)
125 i
= closestListItem(list
, text
, False
);
127 i
= closestListItem(list
, text
, True
);
129 WMSelectListItem(list
, i
);
130 if (i
>=0 && panel
->flags
.autoCompletion
) {
131 WMListItem
*item
= WMGetListItem(list
, i
);
132 int textLen
= strlen(text
), itemTextLen
= strlen(item
->text
);
133 int visibleItems
= WMWidgetHeight(list
)/WMGetListItemHeight(list
);
135 if (textEvent
!=WMSetTextEvent
|| textLen
<itemTextLen
)
136 WMSetListPosition(list
, i
- visibleItems
/2);
138 if (textEvent
!=WMDeleteTextEvent
&& textLen
<itemTextLen
) {
141 WMInsertTextFieldText(panel
->fileField
, &item
->text
[textLen
],
143 WMSetTextFieldCursorPosition(panel
->fileField
, itemTextLen
);
144 range
.position
= textLen
;
145 range
.count
= itemTextLen
- textLen
;
146 WMSelectTextFieldRange(panel
->fileField
, range
);
156 textEditedObserver(void *observerData
, WMNotification
*notification
)
158 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
160 if ((int)WMGetNotificationClientData(notification
)==WMReturnTextMovement
) {
161 WMPerformButtonClick(panel
->okButton
);
168 makeFilePanel(WMScreen
*scrPtr
, char *name
, char *title
)
173 fPtr
= wmalloc(sizeof(WMFilePanel
));
174 memset(fPtr
, 0, sizeof(WMFilePanel
));
176 fPtr
->win
= WMCreateWindowWithStyle(scrPtr
, name
, WMTitledWindowMask
177 |WMResizableWindowMask
);
178 WMResizeWidget(fPtr
->win
, PWIDTH
, PHEIGHT
);
179 WMSetWindowTitle(fPtr
->win
, "");
181 WMCreateEventHandler(WMWidgetView(fPtr
->win
), StructureNotifyMask
,
183 WMSetWindowMinSize(fPtr
->win
, PWIDTH
, PHEIGHT
);
186 fPtr
->iconLabel
= WMCreateLabel(fPtr
->win
);
187 WMResizeWidget(fPtr
->iconLabel
, 64, 64);
188 WMMoveWidget(fPtr
->iconLabel
, 0, 0);
189 WMSetLabelImagePosition(fPtr
->iconLabel
, WIPImageOnly
);
190 WMSetLabelImage(fPtr
->iconLabel
, scrPtr
->applicationIcon
);
192 fPtr
->titleLabel
= WMCreateLabel(fPtr
->win
);
193 WMResizeWidget(fPtr
->titleLabel
, PWIDTH
-64, 64);
194 WMMoveWidget(fPtr
->titleLabel
, 64, 0);
195 largeFont
= WMBoldSystemFontOfSize(scrPtr
, 24);
196 WMSetLabelFont(fPtr
->titleLabel
, largeFont
);
197 WMReleaseFont(largeFont
);
198 WMSetLabelText(fPtr
->titleLabel
, title
);
200 fPtr
->line
= WMCreateFrame(fPtr
->win
);
201 WMMoveWidget(fPtr
->line
, 0, 64);
202 WMResizeWidget(fPtr
->line
, PWIDTH
, 2);
203 WMSetFrameRelief(fPtr
->line
, WRGroove
);
205 fPtr
->browser
= WMCreateBrowser(fPtr
->win
);
206 WMSetBrowserFillColumnProc(fPtr
->browser
, fillColumn
);
207 WMSetBrowserAction(fPtr
->browser
, browserClick
, fPtr
);
208 WMSetBrowserDoubleAction(fPtr
->browser
, browserDClick
, fPtr
);
209 WMMoveWidget(fPtr
->browser
, 7, 72);
210 WMHangData(fPtr
->browser
, fPtr
);
212 fPtr
->nameLabel
= WMCreateLabel(fPtr
->win
);
213 WMMoveWidget(fPtr
->nameLabel
, 7, 282);
214 WMResizeWidget(fPtr
->nameLabel
, 55, 14);
215 WMSetLabelText(fPtr
->nameLabel
, "Name:");
217 fPtr
->fileField
= WMCreateTextField(fPtr
->win
);
218 WMMoveWidget(fPtr
->fileField
, 60, 278);
219 WMResizeWidget(fPtr
->fileField
, PWIDTH
-60-10, 24);
220 WMAddNotificationObserver(textEditedObserver
, fPtr
,
221 WMTextDidEndEditingNotification
,
223 WMAddNotificationObserver(textChangedObserver
, fPtr
,
224 WMTextDidChangeNotification
,
227 fPtr
->okButton
= WMCreateCommandButton(fPtr
->win
);
228 WMMoveWidget(fPtr
->okButton
, 230, 325);
229 WMResizeWidget(fPtr
->okButton
, 80, 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
, 140, 325);
238 WMResizeWidget(fPtr
->cancelButton
, 80, 28);
239 WMSetButtonText(fPtr
->cancelButton
, "Cancel");
240 WMSetButtonAction(fPtr
->cancelButton
, buttonClick
, fPtr
);
242 fPtr
->homeButton
= WMCreateCommandButton(fPtr
->win
);
243 WMMoveWidget(fPtr
->homeButton
, 55, 325);
244 WMResizeWidget(fPtr
->homeButton
, 28, 28);
245 WMSetButtonImagePosition(fPtr
->homeButton
, WIPImageOnly
);
246 WMSetButtonImage(fPtr
->homeButton
, scrPtr
->homeIcon
);
247 WMSetButtonAltImage(fPtr
->homeButton
, scrPtr
->altHomeIcon
);
248 WMSetButtonAction(fPtr
->homeButton
, goHome
, fPtr
);
250 WMRealizeWidget(fPtr
->win
);
251 WMMapSubwidgets(fPtr
->win
);
253 WMSetFocusToWidget(fPtr
->fileField
);
254 WMSetTextFieldCursorPosition(fPtr
->fileField
, 0);
256 WMLoadBrowserColumnZero(fPtr
->browser
);
258 fPtr
->flags
.canChooseFiles
= 1;
259 fPtr
->flags
.canChooseDirectories
= 1;
260 fPtr
->flags
.autoCompletion
= 1;
267 WMGetOpenPanel(WMScreen
*scrPtr
)
271 if (scrPtr
->sharedOpenPanel
)
272 return scrPtr
->sharedOpenPanel
;
274 panel
= makeFilePanel(scrPtr
, "openFilePanel", "Open");
275 panel
->flags
.fileMustExist
= 1;
276 panel
->flags
.panelType
= WP_OPEN
;
278 scrPtr
->sharedOpenPanel
= panel
;
285 WMGetSavePanel(WMScreen
*scrPtr
)
289 if (scrPtr
->sharedSavePanel
)
290 return scrPtr
->sharedSavePanel
;
292 panel
= makeFilePanel(scrPtr
, "saveFilePanel", "Save");
293 panel
->flags
.fileMustExist
= 0;
294 panel
->flags
.panelType
= WP_SAVE
;
296 scrPtr
->sharedSavePanel
= panel
;
303 WMFreeFilePanel(WMFilePanel
*panel
)
305 if (panel
== WMWidgetScreen(panel
->win
)->sharedSavePanel
) {
306 WMWidgetScreen(panel
->win
)->sharedSavePanel
= NULL
;
308 if (panel
== WMWidgetScreen(panel
->win
)->sharedOpenPanel
) {
309 WMWidgetScreen(panel
->win
)->sharedOpenPanel
= NULL
;
311 WMRemoveNotificationObserver(panel
);
312 WMUnmapWidget(panel
->win
);
313 WMDestroyWidget(panel
->win
);
319 WMRunModalFilePanelForDirectory(WMFilePanel
*panel
, WMWindow
*owner
,
320 char *path
, char *name
, char **fileTypes
)
322 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
325 WMChangePanelOwner(panel
->win
, owner
);
327 if (name
&& !owner
) {
328 WMSetWindowTitle(panel
->win
, name
);
331 WMSetFilePanelDirectory(panel
, path
);
333 panel
->flags
.done
= 0;
334 switch(panel
->flags
.panelType
) {
337 panel
->flags
.filtered
= 1;
338 panel
->fileTypes
= fileTypes
;
343 panel
->fileTypes
= NULL
;
344 panel
->flags
.filtered
= 0;
352 WMSetLabelText(panel
->titleLabel
, name
);
354 WMMapWidget(panel
->win
);
356 while (!panel
->flags
.done
) {
357 WMNextEvent(scr
->display
, &event
);
358 WMHandleEvent(&event
);
361 /* Must withdraw window because the next time we map
362 * it, it might have a different transient owner.
364 WMCloseWindow(panel
->win
);
366 return (panel
->flags
.canceled
? False
: True
);
373 WMSetFilePanelDirectory(WMFilePanel
*panel
, char *path
)
380 rest
= WMSetBrowserPath(panel
->browser
, path
);
381 if (strcmp(path
, "/")==0)
384 col
= WMGetBrowserSelectedColumn(panel
->browser
);
385 list
= WMGetBrowserListInColumn(panel
->browser
, col
);
386 if (list
&& (item
= WMGetListSelectedItem(list
))) {
387 if (item
->isBranch
) {
388 WMSetTextFieldText(panel
->fileField
, rest
);
390 WMSetTextFieldText(panel
->fileField
, item
->text
);
393 WMSetTextFieldText(panel
->fileField
, rest
);
399 WMSetFilePanelCanChooseDirectories(WMFilePanel
*panel
, Bool flag
)
401 panel
->flags
.canChooseDirectories
= flag
;
405 WMSetFilePanelCanChooseFiles(WMFilePanel
*panel
, Bool flag
)
407 panel
->flags
.canChooseFiles
= flag
;
412 WMSetFilePanelAutoCompletion(WMFilePanel
*panel
, Bool flag
)
414 panel
->flags
.autoCompletion
= flag
;
419 WMGetFilePanelFileName(WMFilePanel
*panel
)
421 return getCurrentFileName(panel
);
426 WMSetFilePanelAccessoryView(WMFilePanel
*panel
, WMView
*view
)
430 panel
->accessoryView
= view
;
432 v
= WMWidgetView(panel
->win
);
434 W_ReparentView(view
, v
, 0, 0);
436 W_MoveView(view
, (v
->size
.width
- v
->size
.width
)/2, 300);
441 WMGetFilePanelAccessoryView(WMFilePanel
*panel
)
443 return panel
->accessoryView
;
448 get_name_from_path(char *path
)
456 /* remove trailing / */
457 while (size
> 0 && path
[size
-1]=='/')
459 /* directory was root */
463 while (size
> 0 && path
[size
-1] != '/')
466 return wstrdup(&(path
[size
]));
471 filterFileName(WMFilePanel
*panel
, char *file
, Bool isDirectory
)
478 listDirectoryOnColumn(WMFilePanel
*panel
, int column
, char *path
)
480 WMBrowser
*bPtr
= panel
->browser
;
481 struct dirent
*dentry
;
483 struct stat stat_buf
;
484 char pbuf
[PATH_MAX
+16];
487 assert(path
!= NULL
);
489 /* put directory name in the title */
490 WMSetBrowserColumnTitle(bPtr
, column
, get_name_from_path(path
));
496 printf("WINGs: could not open directory %s\n", path
);
501 /* list contents in the column */
502 while ((dentry
= readdir(dir
))) {
503 if (strcmp(dentry
->d_name
, ".")==0 ||
504 strcmp(dentry
->d_name
, "..")==0)
508 if (strcmp(path
, "/")!=0)
510 strcat(pbuf
, dentry
->d_name
);
512 if (stat(pbuf
, &stat_buf
)!=0) {
514 printf("WINGs: could not stat %s\n", pbuf
);
520 isDirectory
= S_ISDIR(stat_buf
.st_mode
);
522 if (filterFileName(panel
, dentry
->d_name
, isDirectory
))
523 WMAddSortedBrowserItem(bPtr
, column
, dentry
->d_name
,
533 fillColumn(WMBrowser
*bPtr
, int column
)
539 path
= WMGetBrowserPathToColumn(bPtr
, column
-1);
544 panel
= WMGetHangedData(bPtr
);
545 listDirectoryOnColumn(panel
, column
, path
);
551 browserDClick(WMBrowser
*bPtr
, WMFilePanel
*panel
)
553 WMPerformButtonClick(panel
->okButton
);
557 browserClick(WMBrowser
*bPtr
, WMFilePanel
*panel
)
559 int col
= WMGetBrowserSelectedColumn(bPtr
);
560 WMListItem
*item
= WMGetBrowserSelectedItemInColumn(bPtr
, col
);
562 panel
->flags
.ignoreTextChangeNotification
= 1;
564 if (!item
|| item
->isBranch
)
565 WMSetTextFieldText(panel
->fileField
, NULL
);
567 WMSetTextFieldText(panel
->fileField
, item
->text
);
570 panel
->flags
.ignoreTextChangeNotification
= 0;
575 goHome(WMButton
*bPtr
, WMFilePanel
*panel
)
579 /* home is statically allocated. Don't free it! */
580 home
= wgethomedir();
584 WMSetFilePanelDirectory(panel
, home
);
589 handleEvents(XEvent
*event
, void *data
)
591 W_FilePanel
*pPtr
= (W_FilePanel
*)data
;
592 W_View
*view
= WMWidgetView(pPtr
->win
);
594 if (event
->type
== ConfigureNotify
) {
595 if (event
->xconfigure
.width
!= view
->size
.width
596 || event
->xconfigure
.height
!= view
->size
.height
) {
597 unsigned int newWidth
= event
->xconfigure
.width
;
598 unsigned int newHeight
= event
->xconfigure
.height
;
601 W_ResizeView(view
, newWidth
, newHeight
);
602 WMResizeWidget(pPtr
->line
, newWidth
, 2);
603 WMResizeWidget(pPtr
->browser
, newWidth
-14,
604 newHeight
-(PHEIGHT
-200));
605 WMResizeWidget(pPtr
->fileField
, newWidth
-60-10, 24);
606 WMMoveWidget(pPtr
->nameLabel
, 7, newHeight
-(PHEIGHT
-282));
607 WMMoveWidget(pPtr
->fileField
, 60, newHeight
-(PHEIGHT
-278));
608 WMMoveWidget(pPtr
->okButton
, newWidth
-(PWIDTH
-230),
609 newHeight
-(PHEIGHT
-325));
610 WMMoveWidget(pPtr
->cancelButton
, newWidth
-(PWIDTH
-140),
611 newHeight
-(PHEIGHT
-325));
612 WMMoveWidget(pPtr
->homeButton
, 55, newHeight
-(PHEIGHT
-325));
614 newColumnCount
= (newWidth
- 14) / 140;
615 WMSetBrowserMaxVisibleColumns(pPtr
->browser
, newColumnCount
);
622 getCurrentFileName(WMFilePanel
*panel
)
629 path
= WMGetBrowserPath(panel
->browser
);
632 if (path
[len
-1]=='/') {
633 file
= WMGetTextFieldText(panel
->fileField
);
634 tmp
= wmalloc(strlen(path
)+strlen(file
)+8);
652 validOpenFile(WMFilePanel
*panel
)
655 int col
, haveFile
= 0;
656 char *file
= WMGetTextFieldText(panel
->fileField
);
662 col
= WMGetBrowserSelectedColumn(panel
->browser
);
663 item
= WMGetBrowserSelectedItemInColumn(panel
->browser
, col
);
665 if (item
->isBranch
&& !panel
->flags
.canChooseDirectories
&& !haveFile
)
667 else if (!item
->isBranch
&& !panel
->flags
.canChooseFiles
)
672 /* we compute for / here */
673 if (!panel
->flags
.canChooseDirectories
&& !haveFile
)
684 buttonClick(WMButton
*bPtr
, WMFilePanel
*panel
)
688 if (bPtr
== panel
->okButton
) {
689 if (!validOpenFile(panel
))
691 if (panel
->flags
.fileMustExist
) {
694 file
= getCurrentFileName(panel
);
695 if (access(file
, F_OK
)!=0) {
696 WMRunAlertPanel(WMWidgetScreen(panel
->win
), panel
->win
,
697 "Error", "File does not exist.",
704 panel
->flags
.canceled
= 0;
706 panel
->flags
.canceled
= 1;
708 range
.count
= range
.position
= 0;
709 WMSelectTextFieldRange(panel
->fileField
, range
);
710 panel
->flags
.done
= 1;