17 typedef struct W_FilePanel
{
29 WMButton
*cancelButton
;
33 WMTextField
*fileField
;
38 unsigned int canExit
:1;
39 unsigned int canceled
:1; /* clicked on cancel */
41 unsigned int filtered
:1;
42 unsigned int canChooseFiles
:1;
43 unsigned int canChooseDirectories
:1;
44 unsigned int showAllFiles
:1;
45 unsigned int canFreeFileTypes
:1;
46 unsigned int fileMustExist
:1;
54 static void listDirectoryOnColumn(WMFilePanel
*panel
, int column
, char *path
);
55 static void browserClick();
57 static void fillColumn(WMBrowser
*bPtr
, int column
);
61 static void buttonClick();
63 static char *getCurrentFileName(WMFilePanel
*panel
);
66 closestListItem(WMList
*list
, char *text
)
68 WMListItem
*item
= WMGetListItem(list
, 0);
70 int len
= strlen(text
);
76 if (strlen(item
->text
) >= len
&& strncmp(item
->text
, text
, len
)==0) {
87 textChangedObserver(void *observerData
, WMNotification
*notification
)
89 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
92 int col
= WMGetBrowserNumberOfColumns(panel
->browser
) - 1;
95 list
= WMGetBrowserListInColumn(panel
->browser
, col
);
98 text
= WMGetTextFieldText(panel
->fileField
);
100 i
= closestListItem(list
, text
);
101 WMSelectListItem(list
, i
);
103 WMSetListPosition(list
, i
);
110 textEditedObserver(void *observerData
, WMNotification
*notification
)
112 W_FilePanel
*panel
= (W_FilePanel
*)observerData
;
114 if ((int)WMGetNotificationClientData(notification
)==WMReturnTextMovement
) {
115 WMPerformButtonClick(panel
->okButton
);
122 makeFilePanel(WMScreen
*scrPtr
, char *name
, char *title
)
127 fPtr
= wmalloc(sizeof(WMFilePanel
));
128 memset(fPtr
, 0, sizeof(WMFilePanel
));
130 fPtr
->win
= WMCreateWindowWithStyle(scrPtr
, name
, WMTitledWindowMask
131 |WMResizableWindowMask
);
132 WMResizeWidget(fPtr
->win
, PWIDTH
, PHEIGHT
);
133 WMSetWindowTitle(fPtr
->win
, "");
135 fPtr
->iconLabel
= WMCreateLabel(fPtr
->win
);
136 WMResizeWidget(fPtr
->iconLabel
, 64, 64);
137 WMMoveWidget(fPtr
->iconLabel
, 0, 0);
138 WMSetLabelImagePosition(fPtr
->iconLabel
, WIPImageOnly
);
139 WMSetLabelImage(fPtr
->iconLabel
, scrPtr
->applicationIcon
);
141 fPtr
->titleLabel
= WMCreateLabel(fPtr
->win
);
142 WMResizeWidget(fPtr
->titleLabel
, PWIDTH
-64, 64);
143 WMMoveWidget(fPtr
->titleLabel
, 64, 0);
144 largeFont
= WMBoldSystemFontOfSize(scrPtr
, 24);
145 WMSetLabelFont(fPtr
->titleLabel
, largeFont
);
146 WMReleaseFont(largeFont
);
147 WMSetLabelText(fPtr
->titleLabel
, title
);
149 fPtr
->line
= WMCreateFrame(fPtr
->win
);
150 WMMoveWidget(fPtr
->line
, 0, 64);
151 WMResizeWidget(fPtr
->line
, PWIDTH
, 2);
152 WMSetFrameRelief(fPtr
->line
, WRGroove
);
154 fPtr
->browser
= WMCreateBrowser(fPtr
->win
);
155 WMSetBrowserFillColumnProc(fPtr
->browser
, fillColumn
);
156 WMSetBrowserAction(fPtr
->browser
, browserClick
, fPtr
);
157 WMMoveWidget(fPtr
->browser
, 7, 72);
158 WMHangData(fPtr
->browser
, fPtr
);
160 fPtr
->nameLabel
= WMCreateLabel(fPtr
->win
);
161 WMMoveWidget(fPtr
->nameLabel
, 7, 282);
162 WMResizeWidget(fPtr
->nameLabel
, 55, 14);
163 WMSetLabelText(fPtr
->nameLabel
, "Name:");
165 fPtr
->fileField
= WMCreateTextField(fPtr
->win
);
166 WMMoveWidget(fPtr
->fileField
, 60, 278);
167 WMResizeWidget(fPtr
->fileField
, PWIDTH
-60-10, 24);
168 WMAddNotificationObserver(textEditedObserver
, fPtr
,
169 WMTextDidEndEditingNotification
,
171 WMAddNotificationObserver(textChangedObserver
, fPtr
,
172 WMTextDidChangeNotification
,
175 fPtr
->okButton
= WMCreateCommandButton(fPtr
->win
);
176 WMMoveWidget(fPtr
->okButton
, 230, 325);
177 WMResizeWidget(fPtr
->okButton
, 80, 28);
178 WMSetButtonText(fPtr
->okButton
, "OK");
179 WMSetButtonImage(fPtr
->okButton
, scrPtr
->buttonArrow
);
180 WMSetButtonAltImage(fPtr
->okButton
, scrPtr
->pushedButtonArrow
);
181 WMSetButtonImagePosition(fPtr
->okButton
, WIPRight
);
182 WMSetButtonAction(fPtr
->okButton
, buttonClick
, fPtr
);
184 fPtr
->cancelButton
= WMCreateCommandButton(fPtr
->win
);
185 WMMoveWidget(fPtr
->cancelButton
, 140, 325);
186 WMResizeWidget(fPtr
->cancelButton
, 80, 28);
187 WMSetButtonText(fPtr
->cancelButton
, "Cancel");
188 WMSetButtonAction(fPtr
->cancelButton
, buttonClick
, fPtr
);
190 fPtr
->homeButton
= WMCreateCommandButton(fPtr
->win
);
191 WMMoveWidget(fPtr
->homeButton
, 55, 325);
192 WMResizeWidget(fPtr
->homeButton
, 28, 28);
193 WMSetButtonImagePosition(fPtr
->homeButton
, WIPImageOnly
);
194 WMSetButtonImage(fPtr
->homeButton
, scrPtr
->homeIcon
);
195 WMSetButtonAction(fPtr
->homeButton
, goHome
, fPtr
);
197 WMRealizeWidget(fPtr
->win
);
198 WMMapSubwidgets(fPtr
->win
);
200 WMLoadBrowserColumnZero(fPtr
->browser
);
202 fPtr
->flags
.canChooseFiles
= 1;
203 fPtr
->flags
.canChooseDirectories
= 1;
210 WMGetOpenPanel(WMScreen
*scrPtr
)
214 if (scrPtr
->sharedOpenPanel
)
215 return scrPtr
->sharedOpenPanel
;
217 panel
= makeFilePanel(scrPtr
, "openFilePanel", "Open");
218 panel
->flags
.fileMustExist
= 1;
220 scrPtr
->sharedOpenPanel
= panel
;
227 WMGetSavePanel(WMScreen
*scrPtr
)
231 if (scrPtr
->sharedSavePanel
)
232 return scrPtr
->sharedSavePanel
;
234 panel
= makeFilePanel(scrPtr
, "saveFilePanel", "Save");
235 panel
->flags
.fileMustExist
= 0;
237 scrPtr
->sharedSavePanel
= panel
;
244 WMFreeFilePanel(WMFilePanel
*panel
)
246 if (panel
== WMWidgetScreen(panel
->win
)->sharedOpenPanel
) {
247 WMWidgetScreen(panel
->win
)->sharedOpenPanel
= NULL
;
249 WMRemoveNotificationObserver(panel
);
250 WMUnmapWidget(panel
->win
);
251 WMDestroyWidget(panel
->win
);
257 WMRunModalSavePanelForDirectory(WMFilePanel
*panel
, WMWindow
*owner
,
258 char *path
, char *name
)
260 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
263 WMChangePanelOwner(panel
->win
, owner
);
265 WMSetFilePanelDirectory(panel
, path
);
267 panel
->flags
.done
= 0;
268 panel
->fileTypes
= NULL
;
270 panel
->flags
.filtered
= 0;
272 WMMapWidget(panel
->win
);
274 while (!panel
->flags
.done
) {
275 WMNextEvent(scr
->display
, &event
);
276 WMHandleEvent(&event
);
279 WMCloseWindow(panel
->win
);
281 return (panel
->flags
.canceled
? False
: True
);
287 WMRunModalOpenPanelForDirectory(WMFilePanel
*panel
, WMWindow
*owner
,
288 char *path
, char *name
, char **fileTypes
)
290 WMScreen
*scr
= WMWidgetScreen(panel
->win
);
293 WMChangePanelOwner(panel
->win
, owner
);
295 WMSetFilePanelDirectory(panel
, path
);
297 panel
->flags
.done
= 0;
300 panel
->flags
.filtered
= 1;
301 panel
->fileTypes
= fileTypes
;
303 WMMapWidget(panel
->win
);
305 while (!panel
->flags
.done
) {
306 WMNextEvent(scr
->display
, &event
);
307 WMHandleEvent(&event
);
310 WMCloseWindow(panel
->win
);
312 return (panel
->flags
.canceled
? False
: True
);
317 WMSetFilePanelDirectory(WMFilePanel
*panel
, char *path
)
323 WMSetBrowserPath(panel
->browser
, path
);
324 col
= WMGetBrowserNumberOfColumns(panel
->browser
) - 1;
325 list
= WMGetBrowserListInColumn(panel
->browser
, col
);
326 if (list
&& (item
= WMGetListSelectedItem(list
))) {
327 if (item
->isBranch
) {
328 WMSetTextFieldText(panel
->fileField
, NULL
);
330 WMSetTextFieldText(panel
->fileField
, item
->text
);
333 WMSetTextFieldText(panel
->fileField
, path
);
339 WMSetFilePanelCanChooseDirectories(WMFilePanel
*panel
, int flag
)
341 panel
->flags
.canChooseDirectories
= flag
;
345 WMSetFilePanelCanChooseFiles(WMFilePanel
*panel
, int flag
)
347 panel
->flags
.canChooseFiles
= flag
;
352 WMGetFilePanelFileName(WMFilePanel
*panel
)
354 return getCurrentFileName(panel
);
359 get_name_from_path(char *path
)
367 /* remove trailing / */
368 while (size
> 0 && path
[size
-1]=='/')
370 /* directory was root */
374 while (size
> 0 && path
[size
-1] != '/')
377 return wstrdup(&(path
[size
]));
382 filterFileName(WMFilePanel
*panel
, char *file
, Bool isDirectory
)
389 listDirectoryOnColumn(WMFilePanel
*panel
, int column
, char *path
)
391 WMBrowser
*bPtr
= panel
->browser
;
392 struct dirent
*dentry
;
394 struct stat stat_buf
;
395 char pbuf
[PATH_MAX
+16];
398 assert(path
!= NULL
);
400 /* put directory name in the title */
401 WMSetBrowserColumnTitle(bPtr
, column
, get_name_from_path(path
));
407 printf("WINGs: could not open directory %s\n", path
);
412 /* list contents in the column */
413 while ((dentry
= readdir(dir
))) {
414 if (strcmp(dentry
->d_name
, ".")==0 ||
415 strcmp(dentry
->d_name
, "..")==0)
419 if (strcmp(path
, "/")!=0)
421 strcat(pbuf
, dentry
->d_name
);
423 if (stat(pbuf
, &stat_buf
)!=0) {
425 printf("WINGs: could not stat %s\n", pbuf
);
431 isDirectory
= S_ISDIR(stat_buf
.st_mode
);
433 if (filterFileName(panel
, dentry
->d_name
, isDirectory
))
434 WMAddSortedBrowserItem(bPtr
, column
, dentry
->d_name
,
444 fillColumn(WMBrowser
*bPtr
, int column
)
450 path
= WMGetBrowserPathToColumn(bPtr
, column
-1);
455 panel
= WMGetHangedData(bPtr
);
456 listDirectoryOnColumn(panel
, column
, path
);
463 browserClick(WMBrowser
*bPtr
, WMFilePanel
*panel
)
465 int col
= WMGetBrowserSelectedColumn(bPtr
);
466 WMListItem
*item
= WMGetBrowserSelectedItemInColumn(bPtr
, col
);
468 if (!item
|| item
->isBranch
)
469 WMSetTextFieldText(panel
->fileField
, NULL
);
471 WMSetTextFieldText(panel
->fileField
, item
->text
);
477 goHome(WMButton
*bPtr
, WMFilePanel
*panel
)
481 /* home is statically allocated. Don't free it! */
482 home
= wgethomedir();
486 WMSetFilePanelDirectory(panel
, home
);
491 getCurrentFileName(WMFilePanel
*panel
)
498 path
= WMGetBrowserPath(panel
->browser
);
501 if (path
[len
-1]=='/') {
502 file
= WMGetTextFieldText(panel
->fileField
);
503 tmp
= wmalloc(strlen(path
)+strlen(file
)+8);
517 validOpenFile(WMFilePanel
*panel
)
522 col
= WMGetBrowserSelectedColumn(panel
->browser
);
523 item
= WMGetBrowserSelectedItemInColumn(panel
->browser
, col
);
525 if (item
->isBranch
&& !panel
->flags
.canChooseDirectories
)
527 else if (!item
->isBranch
&& !panel
->flags
.canChooseFiles
)
536 buttonClick(WMButton
*bPtr
, WMFilePanel
*panel
)
538 if (bPtr
== panel
->okButton
) {
539 if (panel
->flags
.fileMustExist
) {
541 if (!validOpenFile(panel
))
544 file
= getCurrentFileName(panel
);
545 if (access(file
, F_OK
)!=0) {
546 WMRunAlertPanel(WMWidgetScreen(panel
->win
), panel
->win
,
547 "Error", "File does not exist.",
554 panel
->flags
.canceled
= 0;
556 panel
->flags
.canceled
= 1;
558 panel
->flags
.done
= 1;