- Fixed some issues with WMBrowser and the file panel that were
[wmaker-crm.git] / WINGs / wbrowser.c
blob7c0f21b9ab8ee6c32fb46571d7066f53a2dd9cc1
5 #include "WINGsP.h"
6 #include <math.h> /* for : double rint (double) */
10 typedef struct W_Browser {
11 W_Class widgetClass;
12 W_View *view;
14 char **titles;
15 WMList **columns;
17 short columnCount;
18 short usedColumnCount; /* columns actually being used */
19 short minColumnWidth;
21 short maxVisibleColumns;
22 short firstVisibleColumn;
24 short titleHeight;
26 short selectedColumn;
28 WMSize columnSize;
31 void *clientData;
32 WMAction *action;
33 void *doubleClientData;
34 WMAction *doubleAction;
36 WMBrowserDelegate *delegate;
38 WMScroller *scroller;
40 char *pathSeparator;
42 struct {
43 unsigned int isTitled:1;
44 unsigned int allowMultipleSelection:1;
45 unsigned int allowEmptySelection:1;
46 unsigned int hasScroller:1;
48 /* */
49 unsigned int loaded:1;
50 unsigned int loadingColumn:1;
51 } flags;
52 } Browser;
55 #define COLUMN_SPACING 4
56 #define TITLE_SPACING 2
58 #define DEFAULT_WIDTH 305
59 #define DEFAULT_HEIGHT 200
60 #define DEFAULT_HAS_SCROLLER True
61 #define DEFAULT_TITLE_HEIGHT 20
62 #define DEFAULT_IS_TITLED True
63 #define DEFAULT_MAX_VISIBLE_COLUMNS 2
64 #define DEFAULT_SEPARATOR "/"
66 #define MIN_VISIBLE_COLUMNS 1
67 #define MAX_VISIBLE_COLUMNS 32
70 #define COLUMN_IS_VISIBLE(b, c) ((c) >= (b)->firstVisibleColumn \
71 && (c) < (b)->firstVisibleColumn + (b)->maxVisibleColumns)
74 static void handleEvents(XEvent *event, void *data);
75 static void destroyBrowser(WMBrowser *bPtr);
77 static void setupScroller(WMBrowser *bPtr);
79 static void scrollToColumn(WMBrowser *bPtr, int column, Bool updateScroller);
81 static void paintItem(WMList *lPtr, int index, Drawable d, char *text,
82 int state, WMRect *rect);
84 static void loadColumn(WMBrowser *bPtr, int column);
86 static void removeColumn(WMBrowser *bPtr, int column);
88 static char* createTruncatedString(WMFont *font, char *text, int *textLen,
89 int width);
91 static void willResizeBrowser(W_ViewDelegate*, WMView*,
92 unsigned int*, unsigned int*);
94 W_ViewDelegate _BrowserViewDelegate = {
95 NULL,
96 NULL,
97 NULL,
98 NULL,
99 willResizeBrowser
104 WMBrowser*
105 WMCreateBrowser(WMWidget *parent)
107 WMBrowser *bPtr;
108 int i;
110 wassertrv(parent, NULL);
112 bPtr = wmalloc(sizeof(WMBrowser));
113 memset(bPtr, 0, sizeof(WMBrowser));
115 bPtr->widgetClass = WC_Browser;
117 bPtr->view = W_CreateView(W_VIEW(parent));
118 if (!bPtr->view) {
119 wfree(bPtr);
120 return NULL;
122 bPtr->view->self = bPtr;
124 bPtr->view->delegate = &_BrowserViewDelegate;
126 WMCreateEventHandler(bPtr->view, ExposureMask|StructureNotifyMask
127 |ClientMessageMask, handleEvents, bPtr);
129 /* default configuration */
130 bPtr->flags.hasScroller = DEFAULT_HAS_SCROLLER;
132 bPtr->titleHeight = DEFAULT_TITLE_HEIGHT;
133 bPtr->flags.isTitled = DEFAULT_IS_TITLED;
134 bPtr->maxVisibleColumns = DEFAULT_MAX_VISIBLE_COLUMNS;
136 WMResizeWidget(bPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
138 bPtr->pathSeparator = wstrdup(DEFAULT_SEPARATOR);
140 if (bPtr->flags.hasScroller)
141 setupScroller(bPtr);
143 for (i=0; i<bPtr->maxVisibleColumns; i++) {
144 WMAddBrowserColumn(bPtr);
146 bPtr->usedColumnCount = 0;
148 bPtr->selectedColumn = -1;
150 return bPtr;
154 void
155 WMSetBrowserAllowMultipleSelection(WMBrowser *bPtr, Bool flag)
157 int i;
159 bPtr->flags.allowMultipleSelection = flag ? 1 : 0;
160 for (i=0; i<bPtr->columnCount; i++) {
161 WMSetListAllowMultipleSelection(bPtr->columns[i], flag);
166 void
167 WMSetBrowserAllowEmptySelection(WMBrowser *bPtr, Bool flag)
169 int i;
171 bPtr->flags.allowEmptySelection = flag ? 1 : 0;
172 for (i=0; i<bPtr->columnCount; i++) {
173 WMSetListAllowEmptySelection(bPtr->columns[i], flag);
179 WMGetBrowserMaxVisibleColumns(WMBrowser *bPtr)
181 return bPtr->maxVisibleColumns;
185 void
186 WMSetBrowserMaxVisibleColumns(WMBrowser *bPtr, int columns)
188 int curMaxVisibleColumns;
189 int newFirstVisibleColumn = 0;
191 assert ((int) bPtr);
193 columns = (columns < MIN_VISIBLE_COLUMNS) ? MIN_VISIBLE_COLUMNS : columns;
194 columns = (columns > MAX_VISIBLE_COLUMNS) ? MAX_VISIBLE_COLUMNS : columns;
195 if (columns == bPtr->maxVisibleColumns) {
196 return;
198 curMaxVisibleColumns = bPtr->maxVisibleColumns;
199 bPtr->maxVisibleColumns = columns;
200 /* browser not loaded */
201 if (!bPtr->flags.loaded) {
202 if ((columns > curMaxVisibleColumns) && (columns > bPtr->columnCount)) {
203 int i = columns - bPtr->columnCount;
204 bPtr->usedColumnCount = bPtr->columnCount;
205 while (i--) {
206 WMAddBrowserColumn(bPtr);
208 bPtr->usedColumnCount = 0;
210 /* browser loaded and columns > curMaxVisibleColumns */
211 } else if (columns > curMaxVisibleColumns) {
212 if (bPtr->usedColumnCount > columns) {
213 newFirstVisibleColumn = bPtr->usedColumnCount - columns;
215 if (newFirstVisibleColumn > bPtr->firstVisibleColumn) {
216 newFirstVisibleColumn = bPtr->firstVisibleColumn;
218 if (columns > bPtr->columnCount) {
219 int i = columns - bPtr->columnCount;
220 int curUsedColumnCount = bPtr->usedColumnCount;
221 bPtr->usedColumnCount = bPtr->columnCount;
222 while (i--) {
223 WMAddBrowserColumn(bPtr);
225 bPtr->usedColumnCount = curUsedColumnCount;
227 /* browser loaded and columns < curMaxVisibleColumns */
228 } else {
229 newFirstVisibleColumn = bPtr->firstVisibleColumn;
230 if (newFirstVisibleColumn + columns >= bPtr->usedColumnCount) {
231 removeColumn(bPtr, newFirstVisibleColumn + columns);
234 WMResizeWidget(bPtr, bPtr->view->size.width, bPtr->view->size.height);
235 if (bPtr->flags.loaded) {
236 XClearArea(bPtr->view->screen->display, bPtr->view->window, 0, 0,
237 bPtr->view->size.width, bPtr->titleHeight, False);
238 scrollToColumn (bPtr, newFirstVisibleColumn, True);
244 WMGetBrowserNumberOfColumns(WMBrowser *bPtr)
246 return bPtr->usedColumnCount;
249 void
250 WMSetBrowserPathSeparator(WMBrowser *bPtr, char *separator)
252 if (bPtr->pathSeparator)
253 wfree(bPtr->pathSeparator);
254 bPtr->pathSeparator = wstrdup(separator);
259 static void
260 drawTitleOfColumn(WMBrowser *bPtr, int column)
262 WMScreen *scr = bPtr->view->screen;
263 int x;
265 x=(column-bPtr->firstVisibleColumn)*(bPtr->columnSize.width+COLUMN_SPACING);
267 XFillRectangle(scr->display, bPtr->view->window, WMColorGC(scr->darkGray), x, 0,
268 bPtr->columnSize.width, bPtr->titleHeight);
269 W_DrawRelief(scr, bPtr->view->window, x, 0,
270 bPtr->columnSize.width, bPtr->titleHeight, WRSunken);
272 if (column < bPtr->usedColumnCount && bPtr->titles[column]) {
273 int titleLen = strlen(bPtr->titles[column]);
274 int widthC = bPtr->columnSize.width-8;
276 if (WMWidthOfString(scr->boldFont, bPtr->titles[column], titleLen)
277 > widthC) {
278 char *titleBuf = createTruncatedString(scr->boldFont,
279 bPtr->titles[column],
280 &titleLen, widthC);
281 W_PaintText(bPtr->view, bPtr->view->window, scr->boldFont, x,
282 (bPtr->titleHeight-WMFontHeight(scr->boldFont))/2,
283 bPtr->columnSize.width, WACenter, WMColorGC(scr->white),
284 False, titleBuf, titleLen);
285 free (titleBuf);
286 } else {
287 W_PaintText(bPtr->view, bPtr->view->window, scr->boldFont, x,
288 (bPtr->titleHeight-WMFontHeight(scr->boldFont))/2,
289 bPtr->columnSize.width, WACenter, WMColorGC(scr->white),
290 False, bPtr->titles[column], titleLen);
296 WMList*
297 WMGetBrowserListInColumn(WMBrowser *bPtr, int column)
299 if (column < 0 || column >= bPtr->usedColumnCount)
300 return NULL;
302 return bPtr->columns[column];
306 void
307 WMSetBrowserDelegate(WMBrowser *bPtr, WMBrowserDelegate *delegate)
309 bPtr->delegate = delegate;
314 WMGetBrowserFirstVisibleColumn(WMBrowser *bPtr)
316 return bPtr->firstVisibleColumn;
320 static void
321 removeColumn(WMBrowser *bPtr, int column)
323 int i, clearEnd, destroyEnd;
324 WMList **clist;
325 char **tlist;
327 assert ((int) bPtr);
329 column = (column < 0) ? 0 : column;
330 if (column >= bPtr->columnCount) {
331 return;
333 if (column < bPtr->maxVisibleColumns) {
334 clearEnd = bPtr->maxVisibleColumns;
335 destroyEnd = bPtr->columnCount;
336 bPtr->columnCount = bPtr->maxVisibleColumns;
337 } else {
338 clearEnd = column;
339 destroyEnd = bPtr->columnCount;
340 bPtr->columnCount = column;
342 if (column < bPtr->usedColumnCount) {
343 bPtr->usedColumnCount = column;
345 for (i=column; i < clearEnd; i++) {
346 if (bPtr->titles[i]) {
347 wfree(bPtr->titles[i]);
348 bPtr->titles[i] = NULL;
350 WMClearList(bPtr->columns[i]);
352 for (;i < destroyEnd; i++) {
353 if (bPtr->titles[i]) {
354 wfree(bPtr->titles[i]);
355 bPtr->titles[i] = NULL;
357 WMRemoveNotificationObserverWithName(bPtr,
358 WMListSelectionDidChangeNotification,
359 bPtr->columns[i]);
360 WMDestroyWidget(bPtr->columns[i]);
361 bPtr->columns[i] = NULL;
363 clist = wmalloc(sizeof(WMList*) * (bPtr->columnCount));
364 tlist = wmalloc(sizeof(char*) * (bPtr->columnCount));
365 memcpy(clist, bPtr->columns, sizeof(WMList*) * (bPtr->columnCount));
366 memcpy(tlist, bPtr->titles, sizeof(char*) * (bPtr->columnCount));
367 wfree(bPtr->titles);
368 wfree(bPtr->columns);
369 bPtr->titles = tlist;
370 bPtr->columns = clist;
374 WMListItem*
375 WMGetBrowserSelectedItemInColumn(WMBrowser *bPtr, int column)
377 if ((column < 0) || (column >= bPtr->usedColumnCount))
378 return NULL;
380 return WMGetListSelectedItem(bPtr->columns[column]);
386 WMGetBrowserSelectedColumn(WMBrowser *bPtr)
388 return bPtr->selectedColumn;
393 WMGetBrowserSelectedRowInColumn(WMBrowser *bPtr, int column)
395 if (column >= 0 && column < bPtr->columnCount) {
396 return WMGetListSelectedItemRow(bPtr->columns[column]);
397 } else {
398 return -1;
403 void
404 WMSetBrowserColumnTitle(WMBrowser *bPtr, int column, char *title)
406 assert(column >= 0);
407 assert(column < bPtr->usedColumnCount);
409 if (bPtr->titles[column])
410 wfree(bPtr->titles[column]);
412 bPtr->titles[column] = wstrdup(title);
414 if (COLUMN_IS_VISIBLE(bPtr, column) && bPtr->flags.isTitled) {
415 drawTitleOfColumn(bPtr, column);
420 void
421 WMSetBrowserTitled(WMBrowser *bPtr, Bool flag)
423 int i;
424 int columnX, columnY;
426 if (bPtr->flags.isTitled == flag)
427 return;
429 columnX = 0;
431 if (!bPtr->flags.isTitled) {
432 columnY = TITLE_SPACING + bPtr->titleHeight;
434 bPtr->columnSize.height -= columnY;
436 for (i=0; i<bPtr->columnCount; i++) {
437 WMResizeWidget(bPtr->columns[i], bPtr->columnSize.width,
438 bPtr->columnSize.height);
440 columnX = WMWidgetView(bPtr->columns[i])->pos.x;
442 WMMoveWidget(bPtr->columns[i], columnX, columnY);
444 } else {
445 bPtr->columnSize.height += TITLE_SPACING + bPtr->titleHeight;
447 for (i=0; i<bPtr->columnCount; i++) {
448 WMResizeWidget(bPtr->columns[i], bPtr->columnSize.width,
449 bPtr->columnSize.height);
451 columnX = WMWidgetView(bPtr->columns[i])->pos.x;
453 WMMoveWidget(bPtr->columns[i], columnX, 0);
457 bPtr->flags.isTitled = flag;
461 void
462 WMSortBrowserColumn(WMBrowser *bPtr, int column)
464 WMSortListItems(bPtr->columns[column]);
468 void
469 WMSortBrowserColumnWithComparer(WMBrowser *bPtr, int column,
470 WMCompareDataProc *func)
472 WMSortListItemsWithComparer(bPtr->columns[column], func);
477 WMListItem*
478 WMInsertBrowserItem(WMBrowser *bPtr, int column, int row, char *text,
479 Bool isBranch)
481 WMListItem *item;
483 if (column < 0 || column >= bPtr->columnCount)
484 return NULL;
486 item = WMInsertListItem(bPtr->columns[column], row, text);
487 item->isBranch = isBranch;
489 return item;
495 static void
496 willResizeBrowser(W_ViewDelegate *self, WMView *view,
497 unsigned int *width, unsigned int *height)
499 WMBrowser *bPtr = (WMBrowser*)view->self;
500 int cols = bPtr->maxVisibleColumns;
501 int colX, colY;
502 int i;
504 assert(*width > 0);
505 assert(*height > 0);
507 bPtr->columnSize.width = (*width-(cols-1)*COLUMN_SPACING) / cols;
508 bPtr->columnSize.height = *height;
510 if (bPtr->flags.isTitled) {
511 colY = TITLE_SPACING + bPtr->titleHeight;
512 bPtr->columnSize.height -= colY;
513 } else {
514 colY = 0;
517 if (bPtr->flags.hasScroller) {
518 bPtr->columnSize.height -= SCROLLER_WIDTH + 4;
520 if (bPtr->scroller) {
521 WMResizeWidget(bPtr->scroller, *width-2, 1);
522 WMMoveWidget(bPtr->scroller, 1, *height-SCROLLER_WIDTH-1);
526 colX = 0;
527 for (i = 0; i < bPtr->columnCount; i++) {
528 WMResizeWidget(bPtr->columns[i], bPtr->columnSize.width,
529 bPtr->columnSize.height);
531 WMMoveWidget(bPtr->columns[i], colX, colY);
533 if (COLUMN_IS_VISIBLE(bPtr, i)) {
534 colX += bPtr->columnSize.width+COLUMN_SPACING;
540 static void
541 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state,
542 WMRect *rect)
544 WMView *view = W_VIEW(lPtr);
545 W_Screen *scr = view->screen;
546 int width, height, x, y;
548 width = rect->size.width;
549 height = rect->size.height;
550 x = rect->pos.x;
551 y = rect->pos.y;
553 if (state & WLDSSelected)
554 XFillRectangle(scr->display, d, WMColorGC(scr->white), x, y,
555 width, height);
556 else
557 XClearArea(scr->display, d, x, y, width, height, False);
559 if (text) {
560 /* Avoid overlaping... */
561 WMFont *font = (state & WLDSIsBranch) ? scr->boldFont : scr->normalFont;
562 int textLen = strlen(text);
563 int widthC = (state & WLDSIsBranch) ? width-20 : width-8;
564 if (WMWidthOfString(font, text, textLen) > widthC) {
565 char *textBuf = createTruncatedString(font, text, &textLen, widthC);
566 W_PaintText(view, d, font, x+4, y, widthC,
567 WALeft, WMColorGC(scr->black), False, textBuf, textLen);
568 wfree(textBuf);
569 } else {
570 W_PaintText(view, d, font, x+4, y, widthC,
571 WALeft, WMColorGC(scr->black), False, text, textLen);
575 if (state & WLDSIsBranch) {
576 XDrawLine(scr->display, d, WMColorGC(scr->darkGray), x+width-11, y+3,
577 x+width-6, y+height/2);
578 if (state & WLDSSelected)
579 XDrawLine(scr->display, d,WMColorGC(scr->gray), x+width-11, y+height-5,
580 x+width-6, y+height/2);
581 else
582 XDrawLine(scr->display, d,WMColorGC(scr->white), x+width-11, y+height-5,
583 x+width-6, y+height/2);
584 XDrawLine(scr->display, d, WMColorGC(scr->black), x+width-12, y+3,
585 x+width-12, y+height-5);
590 static void
591 scrollCallback(WMWidget *scroller, void *self)
593 WMBrowser *bPtr = (WMBrowser*)self;
594 WMScroller *sPtr = (WMScroller*)scroller;
595 int newFirst;
596 #define LAST_VISIBLE_COLUMN bPtr->firstVisibleColumn+bPtr->maxVisibleColumns
598 switch (WMGetScrollerHitPart(sPtr)) {
599 case WSDecrementLine:
600 if (bPtr->firstVisibleColumn > 0) {
601 scrollToColumn(bPtr, bPtr->firstVisibleColumn-1, True);
603 break;
605 case WSDecrementPage:
606 case WSDecrementWheel:
607 if (bPtr->firstVisibleColumn > 0) {
608 newFirst = bPtr->firstVisibleColumn - bPtr->maxVisibleColumns;
610 scrollToColumn(bPtr, newFirst, True);
612 break;
615 case WSIncrementLine:
616 if (LAST_VISIBLE_COLUMN < bPtr->usedColumnCount) {
617 scrollToColumn(bPtr, bPtr->firstVisibleColumn+1, True);
619 break;
621 case WSIncrementPage:
622 case WSIncrementWheel:
623 if (LAST_VISIBLE_COLUMN < bPtr->usedColumnCount) {
624 newFirst = bPtr->firstVisibleColumn + bPtr->maxVisibleColumns;
626 if (newFirst+bPtr->maxVisibleColumns >= bPtr->columnCount)
627 newFirst = bPtr->columnCount - bPtr->maxVisibleColumns;
629 scrollToColumn(bPtr, newFirst, True);
631 break;
633 case WSKnob:
635 double floatValue;
636 double value = bPtr->columnCount - bPtr->maxVisibleColumns;
638 floatValue = WMGetScrollerValue(bPtr->scroller);
640 floatValue = (floatValue*value)/value;
642 newFirst = rint(floatValue*(float)(bPtr->columnCount - bPtr->maxVisibleColumns));
644 if (bPtr->firstVisibleColumn != newFirst)
645 scrollToColumn(bPtr, newFirst, False);
646 /* else
647 WMSetScrollerParameters(bPtr->scroller, floatValue,
648 bPtr->maxVisibleColumns/(float)bPtr->columnCount);
652 break;
654 case WSKnobSlot:
655 case WSNoPart:
656 /* do nothing */
657 break;
659 #undef LAST_VISIBLE_COLUMN
663 static void
664 setupScroller(WMBrowser *bPtr)
666 WMScroller *sPtr;
667 int y;
669 y = bPtr->view->size.height - SCROLLER_WIDTH - 1;
671 sPtr = WMCreateScroller(bPtr);
672 WMSetScrollerAction(sPtr, scrollCallback, bPtr);
673 WMMoveWidget(sPtr, 1, y);
674 WMResizeWidget(sPtr, bPtr->view->size.width-2, SCROLLER_WIDTH);
676 bPtr->scroller = sPtr;
678 WMMapWidget(sPtr);
682 void
683 WMSetBrowserAction(WMBrowser *bPtr, WMAction *action, void *clientData)
685 bPtr->action = action;
686 bPtr->clientData = clientData;
690 void
691 WMSetBrowserDoubleAction(WMBrowser *bPtr, WMAction *action, void *clientData)
693 bPtr->doubleAction = action;
694 bPtr->doubleClientData = clientData;
698 void
699 WMSetBrowserHasScroller(WMBrowser *bPtr, int hasScroller)
701 bPtr->flags.hasScroller = hasScroller;
706 char*
707 WMSetBrowserPath(WMBrowser *bPtr, char *path)
709 int i;
710 char *str = wstrdup(path);
711 char *tmp, *retPtr = NULL;
712 int item;
713 WMListItem *listItem;
715 /* WMLoadBrowserColumnZero must be call first */
716 if (!bPtr->flags.loaded) {
717 return False;
720 removeColumn(bPtr, 1);
722 WMSelectListItem(bPtr->columns[0], -1);
723 WMSetListPosition(bPtr->columns[0], 0);
725 i = 0;
726 tmp = strtok(str, bPtr->pathSeparator);
727 while (tmp) {
728 /* select it in the column */
729 item = WMFindRowOfListItemWithTitle(bPtr->columns[i], tmp);
730 if (item<0) {
731 retPtr = &path[(int)(tmp - str)];
732 break;
734 WMSelectListItem(bPtr->columns[i], item);
735 WMSetListPosition(bPtr->columns[i], item);
737 listItem = WMGetListItem(bPtr->columns[i], item);
738 if (!listItem || !listItem->isBranch) {
739 break;
742 /* load next column */
743 WMAddBrowserColumn(bPtr);
745 loadColumn(bPtr, i+1);
747 tmp = strtok(NULL, bPtr->pathSeparator);
749 i++;
751 wfree(str);
753 for (i = bPtr->usedColumnCount - 1;
754 (i > -1) && !WMGetListSelectedItem(bPtr->columns[i]);
755 i--);
757 bPtr->selectedColumn = i;
759 if (bPtr->columnCount < bPtr->maxVisibleColumns) {
760 int i = bPtr->maxVisibleColumns - bPtr->columnCount;
761 int curUsedColumnCount = bPtr->usedColumnCount;
762 bPtr->usedColumnCount = bPtr->columnCount;
763 while (i--) {
764 WMAddBrowserColumn(bPtr);
766 bPtr->usedColumnCount = curUsedColumnCount;
769 scrollToColumn(bPtr, bPtr->columnCount - bPtr->maxVisibleColumns, True);
771 return retPtr;
775 char*
776 WMGetBrowserPath(WMBrowser *bPtr)
778 return WMGetBrowserPathToColumn(bPtr, bPtr->columnCount);
783 char*
784 WMGetBrowserPathToColumn(WMBrowser *bPtr, int column)
786 int i, size;
787 char *path;
788 WMListItem *item;
790 if (column >= bPtr->usedColumnCount)
791 column = bPtr->usedColumnCount-1;
793 if (column < 0) {
794 return wstrdup(bPtr->pathSeparator);
797 /* calculate size of buffer */
798 size = 0;
799 for (i = 0; i <= column; i++) {
800 item = WMGetListSelectedItem(bPtr->columns[i]);
801 if (!item)
802 break;
803 size += strlen(item->text);
806 /* get the path */
807 path = wmalloc(size+(column+1)*strlen(bPtr->pathSeparator)+1);
808 /* ignore first / */
809 *path = 0;
810 for (i = 0; i <= column; i++) {
811 strcat(path, bPtr->pathSeparator);
812 item = WMGetListSelectedItem(bPtr->columns[i]);
813 if (!item)
814 break;
815 strcat(path, item->text);
818 return path;
822 Bool
823 WMBrowserAllowsMultipleSelection(WMBrowser *bPtr)
825 return bPtr->flags.allowMultipleSelection;
829 Bool
830 WMBrowserAllowsEmptySelection(WMBrowser *bPtr)
832 return bPtr->flags.allowEmptySelection;
836 static void
837 loadColumn(WMBrowser *bPtr, int column)
839 assert(bPtr->delegate);
840 assert(bPtr->delegate->createRowsForColumn);
842 bPtr->flags.loadingColumn = 1;
843 (*bPtr->delegate->createRowsForColumn)(bPtr->delegate, bPtr, column,
844 bPtr->columns[column]);
845 bPtr->flags.loadingColumn = 0;
847 if (bPtr->delegate->titleOfColumn) {
848 char *title;
850 title = (*bPtr->delegate->titleOfColumn)(bPtr->delegate, bPtr, column);
852 if (bPtr->titles[column])
853 wfree(bPtr->titles[column]);
855 bPtr->titles[column] = wstrdup(title);
857 if (COLUMN_IS_VISIBLE(bPtr, column) && bPtr->flags.isTitled) {
858 drawTitleOfColumn(bPtr, column);
864 static void
865 paintBrowser(WMBrowser *bPtr)
867 int i;
869 if (!bPtr->view->flags.mapped)
870 return;
872 W_DrawRelief(bPtr->view->screen, bPtr->view->window, 0,
873 bPtr->view->size.height-SCROLLER_WIDTH-2,
874 bPtr->view->size.width, 22, WRSunken);
876 if (bPtr->flags.isTitled) {
877 for (i=0; i<bPtr->maxVisibleColumns; i++) {
878 drawTitleOfColumn(bPtr, i+bPtr->firstVisibleColumn);
884 static void
885 handleEvents(XEvent *event, void *data)
887 WMBrowser *bPtr = (WMBrowser*)data;
889 CHECK_CLASS(data, WC_Browser);
892 switch (event->type) {
893 case Expose:
894 paintBrowser(bPtr);
895 break;
897 case DestroyNotify:
898 destroyBrowser(bPtr);
899 break;
906 static void
907 scrollToColumn(WMBrowser *bPtr, int column, Bool updateScroller)
909 int i;
910 int x;
911 int notify = 0;
914 if (column != bPtr->firstVisibleColumn) {
915 notify = 1;
918 if (column < 0)
919 column = 0;
921 if (notify && bPtr->delegate && bPtr->delegate->willScroll) {
922 (*bPtr->delegate->willScroll)(bPtr->delegate, bPtr);
925 x = 0;
926 bPtr->firstVisibleColumn = column;
927 for (i = 0; i < bPtr->columnCount; i++) {
928 if (COLUMN_IS_VISIBLE(bPtr, i)) {
929 WMMoveWidget(bPtr->columns[i], x,
930 WMWidgetView(bPtr->columns[i])->pos.y);
931 if (!WMWidgetView(bPtr->columns[i])->flags.realized)
932 WMRealizeWidget(bPtr->columns[i]);
933 WMMapWidget(bPtr->columns[i]);
934 x += bPtr->columnSize.width + COLUMN_SPACING;
935 } else {
936 WMUnmapWidget(bPtr->columns[i]);
940 /* update the scroller */
941 if (updateScroller) {
942 if (bPtr->columnCount > bPtr->maxVisibleColumns) {
943 float value, proportion;
945 value = bPtr->firstVisibleColumn
946 /(float)(bPtr->columnCount-bPtr->maxVisibleColumns);
947 proportion = bPtr->maxVisibleColumns/(float)bPtr->columnCount;
948 WMSetScrollerParameters(bPtr->scroller, value, proportion);
949 } else {
950 WMSetScrollerParameters(bPtr->scroller, 0, 1);
954 if (bPtr->view->flags.mapped)
955 paintBrowser(bPtr);
957 if (notify && bPtr->delegate && bPtr->delegate->didScroll) {
958 (*bPtr->delegate->didScroll)(bPtr->delegate, bPtr);
963 static void
964 listCallback(void *self, void *clientData)
966 WMBrowser *bPtr = (WMBrowser*)clientData;
967 WMList *lPtr = (WMList*)self;
968 WMListItem *item;
969 static WMListItem *oldItem = NULL;
970 int i;
972 item = WMGetListSelectedItem(lPtr);
974 if (oldItem==NULL || oldItem!=item) {
975 for (i=0; i<bPtr->columnCount; i++) {
976 if (lPtr == bPtr->columns[i])
977 break;
979 assert(i<bPtr->columnCount);
981 bPtr->selectedColumn = i;
983 /* columns at right must be cleared */
984 removeColumn(bPtr, i+1);
985 /* open directory */
986 if (item && item->isBranch) {
987 WMAddBrowserColumn(bPtr);
989 if (bPtr->usedColumnCount < bPtr->maxVisibleColumns)
990 i = 0;
991 else
992 i = bPtr->usedColumnCount-bPtr->maxVisibleColumns;
993 scrollToColumn(bPtr, i, True);
994 if (item && item->isBranch) {
995 loadColumn(bPtr, bPtr->usedColumnCount-1);
1000 /* call callback for click */
1001 if (bPtr->action)
1002 (*bPtr->action)(bPtr, bPtr->clientData);
1004 oldItem = item;
1008 static void
1009 listDoubleCallback(void *self, void *clientData)
1011 WMBrowser *bPtr = (WMBrowser*)clientData;
1012 WMList *lPtr = (WMList*)self;
1013 WMListItem *item;
1015 item = WMGetListSelectedItem(lPtr);
1016 if (!item)
1017 return;
1019 /* call callback for double click */
1020 if (bPtr->doubleAction)
1021 (*bPtr->doubleAction)(bPtr, bPtr->doubleClientData);
1025 void
1026 WMLoadBrowserColumnZero(WMBrowser *bPtr)
1028 if (!bPtr->flags.loaded) {
1029 /* create column 0 */
1030 WMAddBrowserColumn(bPtr);
1032 loadColumn(bPtr, 0);
1034 /* make column 0 visible */
1035 scrollToColumn(bPtr, 0, True);
1037 bPtr->flags.loaded = 1;
1042 void
1043 WMRemoveBrowserItem(WMBrowser *bPtr, int column, int row)
1045 WMList *list;
1047 if (column < 0 || column >= bPtr->usedColumnCount)
1048 return;
1050 list = WMGetBrowserListInColumn(bPtr, column);
1052 if (row < 0 || row >= WMGetListNumberOfRows(list))
1053 return;
1055 removeColumn(bPtr, column+1);
1056 if (bPtr->usedColumnCount < bPtr->maxVisibleColumns)
1057 scrollToColumn(bPtr, 0, True);
1058 else
1059 scrollToColumn(bPtr, bPtr->usedColumnCount-bPtr->maxVisibleColumns,
1060 True);
1062 WMRemoveListItem(list, row);
1066 static void
1067 listSelectionObserver(void *observerData, WMNotification *notification)
1069 WMBrowser *bPtr = (WMBrowser*)observerData;
1070 int column;
1071 WMList *lPtr = (WMList*)WMGetNotificationObject(notification);
1073 for (column = 0; column < bPtr->usedColumnCount; column++)
1074 if (bPtr->columns[column] == lPtr)
1075 break;
1077 /* this can happen when a list is being cleared with WMClearList
1078 * after the column was removed */
1079 if (column >= bPtr->usedColumnCount) {
1080 return;
1083 if (WMGetArrayItemCount(WMGetListSelectedItems(lPtr)) == 0)
1084 column--;
1086 bPtr->selectedColumn = column;
1091 WMAddBrowserColumn(WMBrowser *bPtr)
1093 WMList *list;
1094 WMList **clist;
1095 char **tlist;
1096 int colY;
1097 int index;
1100 if (bPtr->usedColumnCount < bPtr->columnCount) {
1101 return bPtr->usedColumnCount++;
1104 bPtr->usedColumnCount++;
1106 if (bPtr->flags.isTitled) {
1107 colY = TITLE_SPACING + bPtr->titleHeight;
1108 } else {
1109 colY = 0;
1112 index = bPtr->columnCount;
1113 bPtr->columnCount++;
1114 clist = wmalloc(sizeof(WMList*)*bPtr->columnCount);
1115 tlist = wmalloc(sizeof(char*)*bPtr->columnCount);
1116 memcpy(clist, bPtr->columns, sizeof(WMList*)*(bPtr->columnCount-1));
1117 memcpy(tlist, bPtr->titles, sizeof(char*)*(bPtr->columnCount-1));
1118 if (bPtr->columns)
1119 wfree(bPtr->columns);
1120 if (bPtr->titles)
1121 wfree(bPtr->titles);
1122 bPtr->columns = clist;
1123 bPtr->titles = tlist;
1125 bPtr->titles[index] = NULL;
1127 list = WMCreateList(bPtr);
1128 WMSetListAllowMultipleSelection(list, bPtr->flags.allowMultipleSelection);
1129 WMSetListAllowEmptySelection(list, bPtr->flags.allowEmptySelection);
1130 WMSetListAction(list, listCallback, bPtr);
1131 WMSetListDoubleAction(list, listDoubleCallback, bPtr);
1132 WMSetListUserDrawProc(list, paintItem);
1133 WMAddNotificationObserver(listSelectionObserver, bPtr,
1134 WMListSelectionDidChangeNotification, list);
1136 bPtr->columns[index] = list;
1138 WMResizeWidget(list, bPtr->columnSize.width, bPtr->columnSize.height);
1139 WMMoveWidget(list, (bPtr->columnSize.width+COLUMN_SPACING)*index, colY);
1140 if (COLUMN_IS_VISIBLE(bPtr, index))
1141 WMMapWidget(list);
1143 /* update the scroller */
1144 if (bPtr->columnCount > bPtr->maxVisibleColumns) {
1145 float value, proportion;
1147 value = bPtr->firstVisibleColumn
1148 /(float)(bPtr->columnCount-bPtr->maxVisibleColumns);
1149 proportion = bPtr->maxVisibleColumns/(float)bPtr->columnCount;
1150 WMSetScrollerParameters(bPtr->scroller, value, proportion);
1153 return index;
1158 static void
1159 destroyBrowser(WMBrowser *bPtr)
1161 int i;
1163 for (i = 0; i < bPtr->columnCount; i++) {
1164 if (bPtr->titles[i])
1165 wfree(bPtr->titles[i]);
1167 wfree(bPtr->titles);
1169 wfree(bPtr->pathSeparator);
1171 WMRemoveNotificationObserver(bPtr);
1173 wfree(bPtr);
1177 static char*
1178 createTruncatedString(WMFont *font, char *text, int *textLen, int width)
1180 int dLen = WMWidthOfString(font, ".", 1);
1181 char *textBuf = (char*)wmalloc((*textLen)+4);
1183 if (width >= 3*dLen) {
1184 int dddLen = 3*dLen;
1185 int tmpTextLen = *textLen;
1187 strcpy(textBuf, text);
1188 while (tmpTextLen
1189 && (WMWidthOfString(font, textBuf, tmpTextLen)+dddLen > width))
1190 tmpTextLen--;
1191 strcpy(textBuf+tmpTextLen, "...");
1192 *textLen = tmpTextLen+3;
1193 } else if (width >= 2*dLen) {
1194 strcpy(textBuf, "..");
1195 *textLen = 2;
1196 } else if (width >= dLen) {
1197 strcpy(textBuf, ".");
1198 *textLen = 1;
1199 } else {
1200 *textBuf = '\0';
1201 *textLen = 0;
1203 return textBuf;