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(WMBrowserDelegate
*self
, WMBrowser
*bPtr
, int column
,
71 static void buttonClick();
73 static char *getCurrentFileName(WMFilePanel
*panel
);
75 static void handleEvents(XEvent
*event
, void *data
);
79 static WMBrowserDelegate browserDelegate
= {
81 fillColumn
, /* createRowsForColumn */
82 NULL
, /* titleOfColumn */
89 closestListItem(WMList
*list
, char *text
, Bool exact
)
91 WMListItem
*item
= WMGetListItem(list
, 0);
93 int len
= strlen(text
);
99 if (strlen(item
->text
) >= len
&&
100 ((exact
&& strcmp(item
->text
, text
)==0) ||
101 (!exact
&& strncmp(item
->text
, text
, len
)==0))) {
104 item
= item
->nextPtr
;
112 textChangedObserver(void *observerData
, WMNotification
*notification
)
114 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
117 int col
= WMGetBrowserNumberOfColumns(panel
->browser
) - 1;
120 if (panel
->flags
.ignoreTextChangeNotification
)
123 if (!(list
= WMGetBrowserListInColumn(panel
->browser
, col
)))
126 text
= WMGetTextFieldText(panel
->fileField
);
127 textEvent
= (int)WMGetNotificationClientData(notification
);
129 if (panel
->flags
.autoCompletion
&& textEvent
!=WMDeleteTextEvent
)
130 i
= closestListItem(list
, text
, False
);
132 i
= closestListItem(list
, text
, True
);
134 WMSelectListItem(list
, i
);
135 if (i
>=0 && panel
->flags
.autoCompletion
) {
136 WMListItem
*item
= WMGetListItem(list
, i
);
137 int textLen
= strlen(text
), itemTextLen
= strlen(item
->text
);
138 int visibleItems
= WMWidgetHeight(list
)/WMGetListItemHeight(list
);
140 WMSetListPosition(list
, i
- visibleItems
/2);
142 if (textEvent
!=WMDeleteTextEvent
) {
145 panel
->flags
.ignoreTextChangeNotification
= 1;
146 WMInsertTextFieldText(panel
->fileField
, &item
->text
[textLen
],
148 panel
->flags
.ignoreTextChangeNotification
= 0;
150 WMSetTextFieldCursorPosition(panel
->fileField
, itemTextLen
);
151 range
.position
= textLen
;
152 range
.count
= itemTextLen
- textLen
;
153 WMSelectTextFieldRange(panel
->fileField
, range
);
162 textEditedObserver(void *observerData
, WMNotification
*notification
)
164 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
166 if ((int)WMGetNotificationClientData(notification
)==WMReturnTextMovement
) {
167 WMPerformButtonClick(panel
->okButton
);
174 makeFilePanel(WMScreen
*scrPtr
, char *name
, char *title
)
179 fPtr
= wmalloc(sizeof(WMFilePanel
));
180 memset(fPtr
, 0, sizeof(WMFilePanel
));
182 fPtr
->win
= WMCreateWindowWithStyle(scrPtr
, name
, WMTitledWindowMask
183 |WMResizableWindowMask
);
184 WMResizeWidget(fPtr
->win
, PWIDTH
, PHEIGHT
);
185 WMSetWindowTitle(fPtr
->win
, "");
187 WMCreateEventHandler(WMWidgetView(fPtr
->win
), StructureNotifyMask
,
189 WMSetWindowMinSize(fPtr
->win
, PWIDTH
, PHEIGHT
);
192 fPtr
->iconLabel
= WMCreateLabel(fPtr
->win
);
193 WMResizeWidget(fPtr
->iconLabel
, 64, 64);
194 WMMoveWidget(fPtr
->iconLabel
, 0, 0);
195 WMSetLabelImagePosition(fPtr
->iconLabel
, WIPImageOnly
);
196 WMSetLabelImage(fPtr
->iconLabel
, scrPtr
->applicationIcon
);
198 fPtr
->titleLabel
= WMCreateLabel(fPtr
->win
);
199 WMResizeWidget(fPtr
->titleLabel
, PWIDTH
-64, 64);
200 WMMoveWidget(fPtr
->titleLabel
, 64, 0);
201 largeFont
= WMBoldSystemFontOfSize(scrPtr
, 24);
202 WMSetLabelFont(fPtr
->titleLabel
, largeFont
);
203 WMReleaseFont(largeFont
);
204 WMSetLabelText(fPtr
->titleLabel
, title
);
206 fPtr
->line
= WMCreateFrame(fPtr
->win
);
207 WMMoveWidget(fPtr
->line
, 0, 64);
208 WMResizeWidget(fPtr
->line
, PWIDTH
, 2);
209 WMSetFrameRelief(fPtr
->line
, WRGroove
);
211 fPtr
->browser
= WMCreateBrowser(fPtr
->win
);
212 WMSetBrowserDelegate(fPtr
->browser
, &browserDelegate
);
213 WMSetBrowserAction(fPtr
->browser
, browserClick
, fPtr
);
214 WMSetBrowserDoubleAction(fPtr
->browser
, browserDClick
, fPtr
);
215 WMMoveWidget(fPtr
->browser
, 7, 72);
216 WMHangData(fPtr
->browser
, fPtr
);
218 fPtr
->nameLabel
= WMCreateLabel(fPtr
->win
);
219 WMMoveWidget(fPtr
->nameLabel
, 7, 282);
220 WMResizeWidget(fPtr
->nameLabel
, 55, 14);
221 WMSetLabelText(fPtr
->nameLabel
, "Name:");
223 fPtr
->fileField
= WMCreateTextField(fPtr
->win
);
224 WMMoveWidget(fPtr
->fileField
, 60, 278);
225 WMResizeWidget(fPtr
->fileField
, PWIDTH
-60-10, 24);
226 WMAddNotificationObserver(textEditedObserver
, fPtr
,
227 WMTextDidEndEditingNotification
,
229 WMAddNotificationObserver(textChangedObserver
, fPtr
,
230 WMTextDidChangeNotification
,
233 fPtr
->okButton
= WMCreateCommandButton(fPtr
->win
);
234 WMMoveWidget(fPtr
->okButton
, 230, 325);
235 WMResizeWidget(fPtr
->okButton
, 80, 28);
236 WMSetButtonText(fPtr
->okButton
, "OK");
237 WMSetButtonImage(fPtr
->okButton
, scrPtr
->buttonArrow
);
238 WMSetButtonAltImage(fPtr
->okButton
, scrPtr
->pushedButtonArrow
);
239 WMSetButtonImagePosition(fPtr
->okButton
, WIPRight
);
240 WMSetButtonAction(fPtr
->okButton
, buttonClick
, fPtr
);
242 fPtr
->cancelButton
= WMCreateCommandButton(fPtr
->win
);
243 WMMoveWidget(fPtr
->cancelButton
, 140, 325);
244 WMResizeWidget(fPtr
->cancelButton
, 80, 28);
245 WMSetButtonText(fPtr
->cancelButton
, "Cancel");
246 WMSetButtonAction(fPtr
->cancelButton
, buttonClick
, fPtr
);
248 fPtr
->homeButton
= WMCreateCommandButton(fPtr
->win
);
249 WMMoveWidget(fPtr
->homeButton
, 55, 325);
250 WMResizeWidget(fPtr
->homeButton
, 28, 28);
251 WMSetButtonImagePosition(fPtr
->homeButton
, WIPImageOnly
);
252 WMSetButtonImage(fPtr
->homeButton
, scrPtr
->homeIcon
);
253 WMSetButtonAltImage(fPtr
->homeButton
, scrPtr
->altHomeIcon
);
254 WMSetButtonAction(fPtr
->homeButton
, goHome
, fPtr
);
256 WMRealizeWidget(fPtr
->win
);
257 WMMapSubwidgets(fPtr
->win
);
259 WMSetFocusToWidget(fPtr
->fileField
);
260 WMSetTextFieldCursorPosition(fPtr
->fileField
, 0);
262 WMLoadBrowserColumnZero(fPtr
->browser
);
264 fPtr
->flags
.canChooseFiles
= 1;
265 fPtr
->flags
.canChooseDirectories
= 1;
266 fPtr
->flags
.autoCompletion
= 1;
273 WMGetOpenPanel(WMScreen
*scrPtr
)
277 if (scrPtr
->sharedOpenPanel
)
278 return scrPtr
->sharedOpenPanel
;
280 panel
= makeFilePanel(scrPtr
, "openFilePanel", "Open");
281 panel
->flags
.fileMustExist
= 1;
282 panel
->flags
.panelType
= WP_OPEN
;
284 scrPtr
->sharedOpenPanel
= panel
;
291 WMGetSavePanel(WMScreen
*scrPtr
)
295 if (scrPtr
->sharedSavePanel
)
296 return scrPtr
->sharedSavePanel
;
298 panel
= makeFilePanel(scrPtr
, "saveFilePanel", "Save");
299 panel
->flags
.fileMustExist
= 0;
300 panel
->flags
.panelType
= WP_SAVE
;
302 scrPtr
->sharedSavePanel
= panel
;
309 WMFreeFilePanel(WMFilePanel
*panel
)
311 if (panel
== WMWidgetScreen(panel
->win
)->sharedSavePanel
) {
312 WMWidgetScreen(panel
->win
)->sharedSavePanel
= NULL
;
314 if (panel
== WMWidgetScreen(panel
->win
)->sharedOpenPanel
) {
315 WMWidgetScreen(panel
->win
)->sharedOpenPanel
= NULL
;
317 WMRemoveNotificationObserver(panel
);
318 WMUnmapWidget(panel
->win
);
319 WMDestroyWidget(panel
->win
);
325 WMRunModalFilePanelForDirectory(WMFilePanel
*panel
, WMWindow
*owner
,
326 char *path
, char *name
, char **fileTypes
)
328 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
331 if (name
&& !owner
) {
332 WMSetWindowTitle(panel
->win
, name
);
335 WMChangePanelOwner(panel
->win
, owner
);
337 WMSetFilePanelDirectory(panel
, path
);
339 panel
->flags
.done
= 0;
340 switch(panel
->flags
.panelType
) {
343 panel
->flags
.filtered
= 1;
344 panel
->fileTypes
= fileTypes
;
349 panel
->fileTypes
= NULL
;
350 panel
->flags
.filtered
= 0;
358 WMSetWindowUPosition(panel
->win
,
359 (scr
->rootView
->size
.width
- WMWidgetWidth(panel
->win
))/2,
360 (scr
->rootView
->size
.height
- WMWidgetHeight(panel
->win
))/2);
361 WMSetLabelText(panel
->titleLabel
, name
);
363 scr
->modalView
= W_VIEW(panel
->win
);
364 WMMapWidget(panel
->win
);
367 while (!panel
->flags
.done
) {
368 WMNextEvent(scr
->display
, &event
);
369 WMHandleEvent(&event
);
373 /* Must withdraw window because the next time we map
374 * it, it might have a different transient owner.
376 WMCloseWindow(panel
->win
);
378 return (panel
->flags
.canceled
? False
: True
);
385 WMSetFilePanelDirectory(WMFilePanel
*panel
, char *path
)
392 rest
= WMSetBrowserPath(panel
->browser
, path
);
393 if (strcmp(path
, "/")==0)
396 col
= WMGetBrowserSelectedColumn(panel
->browser
);
397 list
= WMGetBrowserListInColumn(panel
->browser
, col
);
398 if (list
&& (item
= WMGetListSelectedItem(list
))) {
399 if (item
->isBranch
) {
400 WMSetTextFieldText(panel
->fileField
, rest
);
402 WMSetTextFieldText(panel
->fileField
, item
->text
);
405 WMSetTextFieldText(panel
->fileField
, rest
);
411 WMSetFilePanelCanChooseDirectories(WMFilePanel
*panel
, Bool flag
)
413 panel
->flags
.canChooseDirectories
= flag
;
417 WMSetFilePanelCanChooseFiles(WMFilePanel
*panel
, Bool flag
)
419 panel
->flags
.canChooseFiles
= flag
;
424 WMSetFilePanelAutoCompletion(WMFilePanel
*panel
, Bool flag
)
426 panel
->flags
.autoCompletion
= flag
;
431 WMGetFilePanelFileName(WMFilePanel
*panel
)
433 return getCurrentFileName(panel
);
438 WMSetFilePanelAccessoryView(WMFilePanel
*panel
, WMView
*view
)
442 panel
->accessoryView
= view
;
444 v
= WMWidgetView(panel
->win
);
446 W_ReparentView(view
, v
, 0, 0);
448 W_MoveView(view
, (v
->size
.width
- v
->size
.width
)/2, 300);
453 WMGetFilePanelAccessoryView(WMFilePanel
*panel
)
455 return panel
->accessoryView
;
460 get_name_from_path(char *path
)
468 /* remove trailing / */
469 while (size
> 0 && path
[size
-1]=='/')
471 /* directory was root */
475 while (size
> 0 && path
[size
-1] != '/')
478 return wstrdup(&(path
[size
]));
483 filterFileName(WMFilePanel
*panel
, char *file
, Bool isDirectory
)
490 listDirectoryOnColumn(WMFilePanel
*panel
, int column
, char *path
)
492 WMBrowser
*bPtr
= panel
->browser
;
493 struct dirent
*dentry
;
495 struct stat stat_buf
;
496 char pbuf
[PATH_MAX
+16];
499 assert(path
!= NULL
);
501 /* put directory name in the title */
502 WMSetBrowserColumnTitle(bPtr
, column
, get_name_from_path(path
));
508 printf("WINGs: could not open directory %s\n", path
);
513 /* list contents in the column */
514 while ((dentry
= readdir(dir
))) {
515 if (strcmp(dentry
->d_name
, ".")==0 ||
516 strcmp(dentry
->d_name
, "..")==0)
520 if (strcmp(path
, "/")!=0)
522 strcat(pbuf
, dentry
->d_name
);
524 if (stat(pbuf
, &stat_buf
)!=0) {
526 printf("WINGs: could not stat %s\n", pbuf
);
532 isDirectory
= S_ISDIR(stat_buf
.st_mode
);
534 if (filterFileName(panel
, dentry
->d_name
, isDirectory
))
535 WMAddSortedBrowserItem(bPtr
, column
, dentry
->d_name
,
545 fillColumn(WMBrowserDelegate
*self
, WMBrowser
*bPtr
, int column
, WMList
*list
)
551 path
= WMGetBrowserPathToColumn(bPtr
, column
-1);
556 panel
= WMGetHangedData(bPtr
);
557 listDirectoryOnColumn(panel
, column
, path
);
563 browserDClick(WMBrowser
*bPtr
, WMFilePanel
*panel
)
565 WMPerformButtonClick(panel
->okButton
);
569 browserClick(WMBrowser
*bPtr
, WMFilePanel
*panel
)
571 int col
= WMGetBrowserSelectedColumn(bPtr
);
572 WMListItem
*item
= WMGetBrowserSelectedItemInColumn(bPtr
, col
);
574 panel
->flags
.ignoreTextChangeNotification
= 1;
576 if (!item
|| item
->isBranch
)
577 WMSetTextFieldText(panel
->fileField
, NULL
);
579 WMSetTextFieldText(panel
->fileField
, item
->text
);
582 panel
->flags
.ignoreTextChangeNotification
= 0;
587 goHome(WMButton
*bPtr
, WMFilePanel
*panel
)
591 /* home is statically allocated. Don't free it! */
592 home
= wgethomedir();
596 WMSetFilePanelDirectory(panel
, home
);
601 handleEvents(XEvent
*event
, void *data
)
603 W_FilePanel
*pPtr
= (W_FilePanel
*)data
;
604 W_View
*view
= WMWidgetView(pPtr
->win
);
606 if (event
->type
== ConfigureNotify
) {
607 if (event
->xconfigure
.width
!= view
->size
.width
608 || event
->xconfigure
.height
!= view
->size
.height
) {
609 unsigned int newWidth
= event
->xconfigure
.width
;
610 unsigned int newHeight
= event
->xconfigure
.height
;
613 W_ResizeView(view
, newWidth
, newHeight
);
614 WMResizeWidget(pPtr
->line
, newWidth
, 2);
615 WMResizeWidget(pPtr
->browser
, newWidth
-14,
616 newHeight
-(PHEIGHT
-200));
617 WMResizeWidget(pPtr
->fileField
, newWidth
-60-10, 24);
618 WMMoveWidget(pPtr
->nameLabel
, 7, newHeight
-(PHEIGHT
-282));
619 WMMoveWidget(pPtr
->fileField
, 60, newHeight
-(PHEIGHT
-278));
620 WMMoveWidget(pPtr
->okButton
, newWidth
-(PWIDTH
-230),
621 newHeight
-(PHEIGHT
-325));
622 WMMoveWidget(pPtr
->cancelButton
, newWidth
-(PWIDTH
-140),
623 newHeight
-(PHEIGHT
-325));
624 WMMoveWidget(pPtr
->homeButton
, 55, newHeight
-(PHEIGHT
-325));
626 newColumnCount
= (newWidth
- 14) / 140;
627 WMSetBrowserMaxVisibleColumns(pPtr
->browser
, newColumnCount
);
634 getCurrentFileName(WMFilePanel
*panel
)
641 path
= WMGetBrowserPath(panel
->browser
);
644 if (path
[len
-1]=='/') {
645 file
= WMGetTextFieldText(panel
->fileField
);
646 tmp
= wmalloc(strlen(path
)+strlen(file
)+8);
664 validOpenFile(WMFilePanel
*panel
)
667 int col
, haveFile
= 0;
668 char *file
= WMGetTextFieldText(panel
->fileField
);
674 col
= WMGetBrowserSelectedColumn(panel
->browser
);
675 item
= WMGetBrowserSelectedItemInColumn(panel
->browser
, col
);
677 if (item
->isBranch
&& !panel
->flags
.canChooseDirectories
&& !haveFile
)
679 else if (!item
->isBranch
&& !panel
->flags
.canChooseFiles
)
684 /* we compute for / here */
685 if (!panel
->flags
.canChooseDirectories
&& !haveFile
)
696 buttonClick(WMButton
*bPtr
, WMFilePanel
*panel
)
700 if (bPtr
== panel
->okButton
) {
701 if (!validOpenFile(panel
))
703 if (panel
->flags
.fileMustExist
) {
706 file
= getCurrentFileName(panel
);
707 if (access(file
, F_OK
)!=0) {
708 WMRunAlertPanel(WMWidgetScreen(panel
->win
), panel
->win
,
709 "Error", "File does not exist.",
716 panel
->flags
.canceled
= 0;
718 panel
->flags
.canceled
= 1;
720 range
.count
= range
.position
= 0;
721 WMSelectTextFieldRange(panel
->fileField
, range
);
722 panel
->flags
.done
= 1;