2 #include <WINGs/WINGsP.h>
13 #include "FSBrowser.h"
16 #include "xpm/list_file.xpm"
17 #include "xpm/list_dir.xpm"
19 #define DEFAULT_SEPARATOR "/"
20 #define LISTITEM_HEIGHT 20
22 typedef struct _FSMCList
{
25 WMScrollView
*scrollV
;
35 void *doubleClientData
;
36 WMAction
*doubleAction
;
40 static void handleDoubleAction(WMWidget
*self
, void *data
);
41 static void handleItemSelection(WMWidget
*self
, void *clientData
);
42 static void fillBrowserColumn(FSBrowser
*bPtr
, int column
);
43 static void paintItem(WMList
*lPtr
, int index
, Drawable d
, char *text
,
44 int state
, WMRect
*rect
);
45 static char *createTruncatedString(WMFont
*font
, char *text
,
46 int *textLen
, int width
);
47 char *parseItem(WMListItem
*item
);
50 FSCreateMCList(WMFrame
*parent
)
54 mPtr
= (FSMCList
*) wmalloc(sizeof(FSMCList
));
55 memset(mPtr
, 0, sizeof(FSMCList
));
57 mPtr
->parent
= parent
;
59 mPtr
->scrollV
= WMCreateScrollView(mPtr
->parent
);
60 WMResizeWidget(mPtr
->scrollV
, 510, 275);
61 WMMoveWidget(mPtr
->scrollV
, 0, 0);
62 WMSetScrollViewRelief(mPtr
->scrollV
, WRSunken
);
63 WMSetScrollViewHasVerticalScroller(mPtr
->scrollV
, False
);
64 WMSetScrollViewHasHorizontalScroller(mPtr
->scrollV
, True
);
66 mPtr
->mainF
= WMCreateFrame(mPtr
->parent
);
67 WMResizeWidget(mPtr
->mainF
, 637, 255);
68 WMMoveWidget(mPtr
->mainF
, 0, 0);
69 WMSetFrameRelief(mPtr
->mainF
, WRFlat
);
71 mPtr
->size
.width
= 637;
72 mPtr
->size
.height
= 255;
74 mPtr
->titles
[0] = WMCreateLabel(mPtr
->mainF
);
75 WMSetLabelText(mPtr
->titles
[0], _("Name"));
76 WMSetLabelTextAlignment(mPtr
->titles
[0], WACenter
);
77 WMSetLabelRelief(mPtr
->titles
[0], WRGroove
);
78 WMResizeWidget(mPtr
->titles
[0], 214, 20);
79 WMMoveWidget(mPtr
->titles
[0], 0, 0);
81 mPtr
->titles
[1] = WMCreateLabel(mPtr
->mainF
);
82 WMSetLabelText(mPtr
->titles
[1], _("Size"));
83 WMSetLabelTextAlignment(mPtr
->titles
[1], WACenter
);
84 WMSetLabelRelief(mPtr
->titles
[1], WRGroove
);
85 WMResizeWidget(mPtr
->titles
[1], 82, 20);
86 WMMoveWidget(mPtr
->titles
[1], 213, 0);
88 mPtr
->titles
[2] = WMCreateLabel(mPtr
->mainF
);
89 WMSetLabelText(mPtr
->titles
[2], _("Last Changed"));
90 WMSetLabelTextAlignment(mPtr
->titles
[2], WACenter
);
91 WMSetLabelRelief(mPtr
->titles
[2], WRGroove
);
92 WMResizeWidget(mPtr
->titles
[2], 100, 20);
93 WMMoveWidget(mPtr
->titles
[2], 294, 0);
95 mPtr
->titles
[3] = WMCreateLabel(mPtr
->mainF
);
96 WMSetLabelText(mPtr
->titles
[3], _("Permissions"));
97 WMSetLabelTextAlignment(mPtr
->titles
[3], WACenter
);
98 WMSetLabelRelief(mPtr
->titles
[3], WRGroove
);
99 WMResizeWidget(mPtr
->titles
[3], 82, 20);
100 WMMoveWidget(mPtr
->titles
[3], 393, 0);
102 mPtr
->titles
[4] = WMCreateLabel(mPtr
->mainF
);
103 WMSetLabelText(mPtr
->titles
[4], _("Owner"));
104 WMSetLabelTextAlignment(mPtr
->titles
[4], WACenter
);
105 WMSetLabelRelief(mPtr
->titles
[4], WRGroove
);
106 WMResizeWidget(mPtr
->titles
[4], 82, 20);
107 WMMoveWidget(mPtr
->titles
[4], 474, 0);
109 mPtr
->titles
[5] = WMCreateLabel(mPtr
->mainF
);
110 WMSetLabelText(mPtr
->titles
[5], _("Group"));
111 WMSetLabelTextAlignment(mPtr
->titles
[5], WACenter
);
112 WMSetLabelRelief(mPtr
->titles
[5], WRGroove
);
113 WMResizeWidget(mPtr
->titles
[5], 82, 20);
114 WMMoveWidget(mPtr
->titles
[5], 555, 0);
116 mPtr
->browser
= FSCreateBrowser(mPtr
->mainF
);
117 FSSetBrowserHasScroller(mPtr
->browser
, False
);
118 FSSetBrowserTitled(mPtr
->browser
, False
);
119 FSSetBrowserRelief(mPtr
->browser
, WRFlat
);
120 WMMoveWidget(mPtr
->browser
, 0, 20);
121 WMResizeWidget(mPtr
->browser
, 637, 235);
122 FSSetBrowserFillColumnProc(mPtr
->browser
, fillBrowserColumn
);
123 FSSetBrowserGetItemNameProc(mPtr
->browser
, parseItem
);
124 FSSetBrowserPathSeparator(mPtr
->browser
, DEFAULT_SEPARATOR
);
125 FSSetBrowserAction(mPtr
->browser
, handleItemSelection
, mPtr
);
126 FSSetBrowserDoubleAction(mPtr
->browser
, handleDoubleAction
, mPtr
);
127 FSSetBrowserDisplayFileCol(mPtr
->browser
, 0);
128 WMHangData(mPtr
->browser
, mPtr
);
129 FSSetBrowserMaxVisibleColumns(mPtr
->browser
, 1);
131 WMMapSubwidgets(mPtr
->mainF
);
132 WMMapWidget(mPtr
->mainF
);
133 WMMapWidget(mPtr
->scrollV
);
135 WMSetScrollViewContentView(mPtr
->scrollV
, WMWidgetView(mPtr
->mainF
));
141 parseItem(WMListItem
*item
)
150 str
= wstrdup(item
->text
);
151 tmp
= strtok(str
, "==");
157 tmp
= strtok(NULL
, "==");
163 text
= wstrdup(item
->text
);
169 formatFileInfo(WMScreen
*scr
, FileInfo
*fileInfo
)
177 char mode
[11] = "----------";
180 struct passwd
*psswd
;
184 if(isDirectory(fileInfo
->fileType
))
193 name
= wstrdup(fileInfo
->name
);
195 name
= wstrdup("nameless");
197 snprintf(size
, 128, "%ld", (long) fileInfo
->st
->st_size
);
198 st_tm
= localtime(&(fileInfo
->st
->st_ctime
));
199 strftime(time
, 128, "%b %d %H:%M", st_tm
);
201 if(fileInfo
->st
->st_mode
& S_IRUSR
)
203 if(fileInfo
->st
->st_mode
& S_IWUSR
)
205 if(fileInfo
->st
->st_mode
& S_IXUSR
)
207 if(fileInfo
->st
->st_mode
& S_IRGRP
)
209 if(fileInfo
->st
->st_mode
& S_IWGRP
)
211 if(fileInfo
->st
->st_mode
& S_IXGRP
)
213 if(fileInfo
->st
->st_mode
& S_IROTH
)
215 if(fileInfo
->st
->st_mode
& S_IWOTH
)
217 if(fileInfo
->st
->st_mode
& S_IXOTH
)
220 grp
= getgrgid(fileInfo
->st
->st_gid
);
222 group
= wstrdup(grp
->gr_name
);
224 group
= wstrdup("nogroup");
226 psswd
= getpwuid(fileInfo
->st
->st_uid
);
228 owner
= wstrdup(psswd
->pw_name
);
230 owner
= wstrdup("ownerless");
232 text
= (char *) wmalloc(strlen(img
) + strlen(name
) + strlen(size
) +
233 strlen(time
) + strlen(mode
) + strlen(group
) +
235 /* strcpy(text, sep); */
236 /* strcat(text, img); */
250 /* strcat(text, sep); */
256 fillBrowserColumn(FSBrowser
*bPtr
, int column
)
260 FileInfo
*start
= NULL
;
261 WMListItem
*listItem
;
262 FSMCList
*mPtr
= (FSMCList
*)(WMGetHangedData(bPtr
));
263 WMList
*list
= FSGetBrowserListInColumn(bPtr
, column
);
264 WMScreen
*scr
= WMWidgetScreen(list
);
267 * Override the FSBrowser drawing procedure
271 WMSetListUserDrawProc(list
, paintItem
);
272 WMSetListUserDrawItemHeight(list
, LISTITEM_HEIGHT
);
275 FSSetBusyCursor(bPtr
, True
);
277 path
= FSGetBrowserPathToColumn(bPtr
, column
);
280 start
= GetDirList(path
);
283 while(fileList
!= NULL
)
285 if( DisplayFile(fileList
->name
, mPtr
->filter
, fileList
->fileType
) )
287 listItem
= FSInsertBrowserItem(bPtr
, column
, -1,
289 isDirectory(fileList
->fileType
));
290 listItem
->clientData
= formatFileInfo(scr
, fileList
);
293 fileList
= fileList
->next
;
303 FSFreeFileInfo(start
);
305 FSSetBusyCursor(bPtr
, False
);
309 handleItemSelection(WMWidget
*self
, void *clientData
)
313 /* FileBrowser *bPtr = (FileBrowser *) clientData; */
315 /* col = FSGetFileBrowserColumnCount(bPtr); */
317 /* FSSetPathViewToColumn(bPtr->pathView, col-1); */
320 /* wwarning("%s %d: %s", __FILE__, __LINE__, bPtr->text); */
321 /* free(bPtr->text); */
322 /* bPtr->text = NULL; */
327 handleDoubleAction(WMWidget
*self
, void *clientData
)
329 /* FileBrowser *bPtr = (FileBrowser *) clientData; */
331 /* if (bPtr->doubleAction) */
332 /* (*bPtr->doubleAction)(bPtr, bPtr->doubleClientData); */
336 FSLoadMCListColumnZero(FSMCList
*mPtr
)
338 FSLoadBrowserColumnZero(mPtr
->browser
);
342 FSSetMCListPath(FSMCList
*mPtr
, char *path
)
344 FSSetBrowserPath(mPtr
->browser
, path
);
348 FSGetMCListNumberOfColumns(FSMCList
*mPtr
)
350 return FSGetBrowserNumberOfColumns(mPtr
->browser
);
354 FSResizeMCList(FSMCList
*mPtr
, unsigned int width
, unsigned int height
)
356 if(mPtr
->size
.width
< width
)
358 WMResizeWidget(mPtr
->mainF
, width
-3, height
);
359 WMResizeScrollViewContent(mPtr
->scrollV
, width
-3, height
);
360 WMResizeWidget(mPtr
->scrollV
, width
, height
);
361 WMResizeWidget(mPtr
->browser
, width
-3, height
-40);
365 WMResizeScrollViewContent(mPtr
->scrollV
, mPtr
->size
.width
-3, height
);
366 WMResizeWidget(mPtr
->scrollV
, width
, height
);
367 WMResizeWidget(mPtr
->browser
, mPtr
->size
.width
, height
-40);
368 WMResizeWidget(mPtr
->mainF
, mPtr
->size
.width
, height
);
374 FSGetMCListPath(FSMCList
*mPtr
)
380 path
= FSGetBrowserPath(mPtr
->browser
);
382 if(len
> 1 && path
[len
-1] == '/')
384 npath
= (char *)wmalloc(len
);
385 strncpy(npath
, path
, len
-1);
389 npath
= wstrdup(path
);
397 FSGetMCListPathToColumn(FSMCList
*mPtr
, int column
)
399 return FSGetBrowserPathToColumn(mPtr
->browser
, column
);
403 createTruncatedString(WMFont
*font
, char *text
, int *textLen
, int width
)
405 int dLen
= WMWidthOfString(font
, ".", 1);
406 char *textBuf
= (char*)wmalloc((*textLen
)+4);
408 if (width
>= 3*dLen
) {
410 int tmpTextLen
= *textLen
;
412 strcpy(textBuf
, text
);
414 && (WMWidthOfString(font
, textBuf
, tmpTextLen
)+dddLen
> width
))
416 strcpy(textBuf
+tmpTextLen
, "...");
417 *textLen
= tmpTextLen
+3;
418 } else if (width
>= 2*dLen
) {
419 strcpy(textBuf
, "..");
421 } else if (width
>= dLen
) {
422 strcpy(textBuf
, ".");
432 paintItem(WMList
*lPtr
, int index
, Drawable d
,
433 char *text
, int state
, WMRect
*rect
)
443 WMView
*view
= W_VIEW(lPtr
);
444 W_Screen
*scr
= view
->screen
;
445 int width
, height
, x
, y
, len
;
446 WMListItem
*item
= WMGetListItem(lPtr
, index
);
448 width
= rect
->size
.width
;
449 height
= rect
->size
.height
;
452 fontHeight
= WMFontHeight(scr
->normalFont
);
454 /* Highlight the selected area */
455 if (state
& WLDSSelected
)
456 WMPaintColorSwatch(WMWhiteColor(scr
), d
, x
, y
,
459 WMPaintColorSwatch(WMGetWidgetBackgroundColor(lPtr
), d
, x
, y
,
464 str
= wstrdup(item
->clientData
);
473 /* simple parse of the clientdata */
474 for(i
= 0; i
< len
; i
++)
476 if(/* str[i-1] != '=' && */str
[i
] != '|')
486 items
[j
++] = wstrdup(tmp
);
490 items
[j
++] = wstrdup(tmp
);
492 /* iterate through items and draw each one */
493 for(i
= 0; i
< j
; i
++)
497 if(strcmp(items
[i
], "0") == 0)
501 /* do nothing for the moment */
507 int widthC
= 168; /*194 - 26*/
508 int textLen
= strlen(items
[i
]);
510 if(FSGetIntegerForName("DisplayMCListPixmap"))
513 image
= WMCreatePixmapFromXPMData(scr
, list_dir_xpm
);
515 image
= WMCreatePixmapFromXPMData(scr
, list_file_xpm
);
516 size
= WMGetPixmapSize(image
);
517 WMDrawPixmap(image
, d
,
518 x
+6+(20-size
.width
)/2, (height
-size
.height
)/2+y
);
519 WMReleasePixmap(image
);
527 tmpStr
= wstrdup("D");
529 tmpStr
= wstrdup("F");
531 W_PaintText(view
, d
, scr
->boldFont
,
532 x
+6, y
+(height
-fontHeight
)/2, widthC
,
533 WALeft
, WMDarkGrayColor(scr
), False
,
539 if (WMWidthOfString(scr
->normalFont
, items
[i
], textLen
) > widthC
)
541 char *textBuf
= createTruncatedString(scr
->normalFont
,
545 W_PaintText(view
, d
, scr
->normalFont
,
546 x
+26, y
+(height
-fontHeight
)/2, widthC
,
547 WALeft
, WMBlackColor(scr
), False
,
554 W_PaintText(view
, d
, scr
->normalFont
,
555 x
+26, y
+(height
-fontHeight
)/2, widthC
,
556 WALeft
, WMBlackColor(scr
), False
, items
[i
],
563 W_PaintText(view
, d
, scr
->normalFont
,
564 x
+6, y
+(height
-fontHeight
)/2, 100,
565 WALeft
, WMBlackColor(scr
), False
, items
[i
],
571 W_PaintText(view
, d
, scr
->normalFont
,
572 x
+6, y
+(height
-fontHeight
)/2, 82,
573 WALeft
, WMBlackColor(scr
), False
, items
[i
],
584 FSSetMCListFilter(FSMCList
*mPtr
, char *filter
)
586 mPtr
->filter
= filter
;
590 FSScrollMCListToColumn(FSMCList
*mPtr
, int col
, Bool updateScroller
)
592 FSScrollBrowserToColumn(mPtr
->browser
, col
, updateScroller
);
596 FSSetMCListAction(FSMCList
*mPtr
, WMAction
*action
, void *clientData
)
598 FSSetBrowserAction(mPtr
->browser
, action
, clientData
);
602 FSSetMCListDoubleAction(FSMCList
*mPtr
, WMAction
*action
, void *clientData
)
604 FSSetBrowserDoubleAction(mPtr
->browser
, action
, clientData
);
608 FSGetMCListSelectedItemInColumn(FSMCList
*mPtr
, int column
)
610 return FSGetBrowserSelectedItemInColumn(mPtr
->browser
, column
);
614 FSGetMCListListInColumn(FSMCList
*mPtr
, int column
)
616 return FSGetBrowserListInColumn(mPtr
->browser
, column
);
620 FSMCListFormatFileInfo(FSMCList
*mPtr
, FileInfo
*fileInfo
)
622 return formatFileInfo(WMWidgetScreen(mPtr
), fileInfo
);