syncing wtextfield.c
[wmaker-crm.git] / WINGs / wbrowser.c
blob1720b27ed7759da945911d4a2e7f7a995e45de46
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 hasScroller:1;
47 /* */
48 unsigned int loaded:1;
49 unsigned int loadingColumn:1;
50 } flags;
51 } Browser;
54 #define COLUMN_SPACING 4
55 #define TITLE_SPACING 2
57 #define DEFAULT_WIDTH 305
58 #define DEFAULT_HEIGHT 200
59 #define DEFAULT_HAS_SCROLLER True
60 #define DEFAULT_TITLE_HEIGHT 20
61 #define DEFAULT_IS_TITLED True
62 #define DEFAULT_MAX_VISIBLE_COLUMNS 2
63 #define DEFAULT_SEPARATOR "/"
65 #define MIN_VISIBLE_COLUMNS 1
66 #define MAX_VISIBLE_COLUMNS 32
69 #define COLUMN_IS_VISIBLE(b, c) ((c) >= (b)->firstVisibleColumn \
70 && (c) < (b)->firstVisibleColumn + (b)->maxVisibleColumns)
73 static void handleEvents(XEvent *event, void *data);
74 static void destroyBrowser(WMBrowser *bPtr);
76 static void setupScroller(WMBrowser *bPtr);
78 static void scrollToColumn(WMBrowser *bPtr, int column, Bool updateScroller);
80 static void paintItem(WMList *lPtr, int index, Drawable d, char *text,
81 int state, WMRect *rect);
83 static void loadColumn(WMBrowser *bPtr, int column);
85 static void removeColumn(WMBrowser *bPtr, int column);
87 static char*
88 createTruncatedString(WMFont *font, char *text, int *textLen, int width);
90 static void willResizeBrowser(W_ViewDelegate*, WMView*,
91 unsigned int*, unsigned int*);
93 W_ViewDelegate _BrowserViewDelegate = {
94 NULL,
95 NULL,
96 NULL,
97 NULL,
98 willResizeBrowser
103 WMBrowser*
104 WMCreateBrowser(WMWidget *parent)
106 WMBrowser *bPtr;
107 int i;
109 wassertrv(parent, NULL);
111 bPtr = wmalloc(sizeof(WMBrowser));
112 memset(bPtr, 0, sizeof(WMBrowser));
114 bPtr->widgetClass = WC_Browser;
116 bPtr->view = W_CreateView(W_VIEW(parent));
117 if (!bPtr->view) {
118 wfree(bPtr);
119 return NULL;
121 bPtr->view->self = bPtr;
123 bPtr->view->delegate = &_BrowserViewDelegate;
125 WMCreateEventHandler(bPtr->view, ExposureMask|StructureNotifyMask
126 |ClientMessageMask, handleEvents, bPtr);
128 /* default configuration */
129 bPtr->flags.hasScroller = DEFAULT_HAS_SCROLLER;
131 bPtr->titleHeight = DEFAULT_TITLE_HEIGHT;
132 bPtr->flags.isTitled = DEFAULT_IS_TITLED;
133 bPtr->maxVisibleColumns = DEFAULT_MAX_VISIBLE_COLUMNS;
135 WMResizeWidget(bPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
137 bPtr->pathSeparator = wstrdup(DEFAULT_SEPARATOR);
139 if (bPtr->flags.hasScroller)
140 setupScroller(bPtr);
142 for (i=0; i<bPtr->maxVisibleColumns; i++) {
143 WMAddBrowserColumn(bPtr);
145 bPtr->usedColumnCount = 0;
147 bPtr->selectedColumn = -1;
149 return bPtr;
154 WMGetBrowserMaxVisibleColumns(WMBrowser *bPtr)
156 return bPtr->maxVisibleColumns;
160 void
161 WMSetBrowserMaxVisibleColumns(WMBrowser *bPtr, int columns)
163 int curMaxVisibleColumns;
164 int newFirstVisibleColumn = 0;
166 assert ((int) bPtr);
168 columns = (columns < MIN_VISIBLE_COLUMNS) ? MIN_VISIBLE_COLUMNS : columns;
169 columns = (columns > MAX_VISIBLE_COLUMNS) ? MAX_VISIBLE_COLUMNS : columns;
170 if (columns == bPtr->maxVisibleColumns) {
171 return;
173 curMaxVisibleColumns = bPtr->maxVisibleColumns;
174 bPtr->maxVisibleColumns = columns;
175 /* browser not loaded */
176 if (!bPtr->flags.loaded) {
177 if ((columns > curMaxVisibleColumns) && (columns > bPtr->columnCount)) {
178 int i = columns - bPtr->columnCount;
179 bPtr->usedColumnCount = bPtr->columnCount;
180 while (i--) {
181 WMAddBrowserColumn(bPtr);
183 bPtr->usedColumnCount = 0;
185 /* browser loaded and columns > curMaxVisibleColumns */
186 } else if (columns > curMaxVisibleColumns) {
187 if (bPtr->usedColumnCount > columns) {
188 newFirstVisibleColumn = bPtr->usedColumnCount - columns;
190 if (newFirstVisibleColumn > bPtr->firstVisibleColumn) {
191 newFirstVisibleColumn = bPtr->firstVisibleColumn;
193 if (columns > bPtr->columnCount) {
194 int i = columns - bPtr->columnCount;
195 int curUsedColumnCount = bPtr->usedColumnCount;
196 bPtr->usedColumnCount = bPtr->columnCount;
197 while (i--) {
198 WMAddBrowserColumn(bPtr);
200 bPtr->usedColumnCount = curUsedColumnCount;
202 /* browser loaded and columns < curMaxVisibleColumns */
203 } else {
204 newFirstVisibleColumn = bPtr->firstVisibleColumn;
205 if (newFirstVisibleColumn + columns >= bPtr->usedColumnCount) {
206 removeColumn(bPtr, newFirstVisibleColumn + columns);
209 WMResizeWidget(bPtr, bPtr->view->size.width, bPtr->view->size.height);
210 if (bPtr->flags.loaded) {
211 XClearArea(bPtr->view->screen->display, bPtr->view->window, 0, 0,
212 bPtr->view->size.width, bPtr->titleHeight, False);
213 scrollToColumn (bPtr, newFirstVisibleColumn, True);
219 WMGetBrowserNumberOfColumns(WMBrowser *bPtr)
221 return bPtr->usedColumnCount;
224 void
225 WMSetBrowserPathSeparator(WMBrowser *bPtr, char *separator)
227 if (bPtr->pathSeparator)
228 wfree(bPtr->pathSeparator);
229 bPtr->pathSeparator = wstrdup(separator);
234 static void
235 drawTitleOfColumn(WMBrowser *bPtr, int column)
237 WMScreen *scr = bPtr->view->screen;
238 int x;
240 x=(column-bPtr->firstVisibleColumn)*(bPtr->columnSize.width+COLUMN_SPACING);
242 XFillRectangle(scr->display, bPtr->view->window, WMColorGC(scr->darkGray), x, 0,
243 bPtr->columnSize.width, bPtr->titleHeight);
244 W_DrawRelief(scr, bPtr->view->window, x, 0,
245 bPtr->columnSize.width, bPtr->titleHeight, WRSunken);
247 if (column < bPtr->usedColumnCount && bPtr->titles[column]) {
248 int titleLen = strlen(bPtr->titles[column]);
249 int widthC = bPtr->columnSize.width-8;
251 if (WMWidthOfString(scr->boldFont, bPtr->titles[column], titleLen)
252 > widthC) {
253 char *titleBuf = createTruncatedString(scr->boldFont,
254 bPtr->titles[column],
255 &titleLen, widthC);
256 W_PaintText(bPtr->view, bPtr->view->window, scr->boldFont, x,
257 (bPtr->titleHeight-WMFontHeight(scr->boldFont))/2,
258 bPtr->columnSize.width, WACenter, WMColorGC(scr->white),
259 False, titleBuf, titleLen);
260 free (titleBuf);
261 } else {
262 W_PaintText(bPtr->view, bPtr->view->window, scr->boldFont, x,
263 (bPtr->titleHeight-WMFontHeight(scr->boldFont))/2,
264 bPtr->columnSize.width, WACenter, WMColorGC(scr->white),
265 False, bPtr->titles[column], titleLen);
271 WMList*
272 WMGetBrowserListInColumn(WMBrowser *bPtr, int column)
274 if (column < 0 || column >= bPtr->usedColumnCount)
275 return NULL;
277 return bPtr->columns[column];
281 void
282 WMSetBrowserDelegate(WMBrowser *bPtr, WMBrowserDelegate *delegate)
284 bPtr->delegate = delegate;
289 WMGetBrowserFirstVisibleColumn(WMBrowser *bPtr)
291 return bPtr->firstVisibleColumn;
295 static void
296 removeColumn(WMBrowser *bPtr, int column)
298 int i, clearEnd, destroyEnd;
299 WMList **clist;
300 char **tlist;
302 assert ((int) bPtr);
304 column = (column < 0) ? 0 : column;
305 if (column >= bPtr->columnCount) {
306 return;
308 if (column < bPtr->maxVisibleColumns) {
309 clearEnd = bPtr->maxVisibleColumns;
310 destroyEnd = bPtr->columnCount;
311 bPtr->columnCount = bPtr->maxVisibleColumns;
312 } else {
313 clearEnd = column;
314 destroyEnd = bPtr->columnCount;
315 bPtr->columnCount = column;
317 if (column < bPtr->usedColumnCount) {
318 bPtr->usedColumnCount = column;
320 for (i=column; i < clearEnd; i++) {
321 if (bPtr->titles[i]) {
322 wfree(bPtr->titles[i]);
323 bPtr->titles[i] = NULL;
325 WMClearList(bPtr->columns[i]);
327 for (;i < destroyEnd; i++) {
328 if (bPtr->titles[i]) {
329 wfree(bPtr->titles[i]);
330 bPtr->titles[i] = NULL;
332 WMRemoveNotificationObserverWithName(bPtr,
333 WMListSelectionDidChangeNotification,
334 bPtr->columns[i]);
335 WMDestroyWidget(bPtr->columns[i]);
336 bPtr->columns[i] = NULL;
338 clist = wmalloc(sizeof(WMList*) * (bPtr->columnCount));
339 tlist = wmalloc(sizeof(char*) * (bPtr->columnCount));
340 memcpy(clist, bPtr->columns, sizeof(WMList*) * (bPtr->columnCount));
341 memcpy(tlist, bPtr->titles, sizeof(char*) * (bPtr->columnCount));
342 wfree(bPtr->titles);
343 wfree(bPtr->columns);
344 bPtr->titles = tlist;
345 bPtr->columns = clist;
349 WMListItem*
350 WMGetBrowserSelectedItemInColumn(WMBrowser *bPtr, int column)
352 if ((column < 0) || (column >= bPtr->usedColumnCount))
353 return NULL;
355 return WMGetListSelectedItem(bPtr->columns[column]);
361 WMGetBrowserSelectedColumn(WMBrowser *bPtr)
363 return bPtr->selectedColumn;
368 WMGetBrowserSelectedRowInColumn(WMBrowser *bPtr, int column)
370 if (column >= 0 && column < bPtr->columnCount) {
371 return WMGetListSelectedItemRow(bPtr->columns[column]);
372 } else {
373 return -1;
378 void
379 WMSetBrowserColumnTitle(WMBrowser *bPtr, int column, char *title)
381 assert(column >= 0);
382 assert(column < bPtr->usedColumnCount);
384 if (bPtr->titles[column])
385 wfree(bPtr->titles[column]);
387 bPtr->titles[column] = wstrdup(title);
389 if (COLUMN_IS_VISIBLE(bPtr, column) && bPtr->flags.isTitled) {
390 drawTitleOfColumn(bPtr, column);
395 void
396 WMSetBrowserTitled(WMBrowser *bPtr, Bool flag)
398 int i;
399 int columnX, columnY;
401 if (bPtr->flags.isTitled == flag)
402 return;
404 columnX = 0;
406 if (!bPtr->flags.isTitled) {
407 columnY = TITLE_SPACING + bPtr->titleHeight;
409 bPtr->columnSize.height -= columnY;
411 for (i=0; i<bPtr->columnCount; i++) {
412 WMResizeWidget(bPtr->columns[i], bPtr->columnSize.width,
413 bPtr->columnSize.height);
415 columnX = WMWidgetView(bPtr->columns[i])->pos.x;
417 WMMoveWidget(bPtr->columns[i], columnX, columnY);
419 } else {
420 bPtr->columnSize.height += TITLE_SPACING + bPtr->titleHeight;
422 for (i=0; i<bPtr->columnCount; i++) {
423 WMResizeWidget(bPtr->columns[i], bPtr->columnSize.width,
424 bPtr->columnSize.height);
426 columnX = WMWidgetView(bPtr->columns[i])->pos.x;
428 WMMoveWidget(bPtr->columns[i], columnX, 0);
432 bPtr->flags.isTitled = flag;
436 void
437 WMSortBrowserColumn(WMBrowser *bPtr, int column)
439 WMSortListItems(bPtr->columns[column]);
444 WMListItem*
445 WMInsertBrowserItem(WMBrowser *bPtr, int column, int row, char *text,
446 Bool isBranch)
448 WMListItem *item;
450 if (column < 0 || column >= bPtr->columnCount)
451 return NULL;
453 item = WMInsertListItem(bPtr->columns[column], row, text);
454 item->isBranch = isBranch;
456 return item;
462 static void
463 willResizeBrowser(W_ViewDelegate *self, WMView *view,
464 unsigned int *width, unsigned int *height)
466 WMBrowser *bPtr = (WMBrowser*)view->self;
467 int cols = bPtr->maxVisibleColumns;
468 int colX, colY;
469 int i;
471 assert(*width > 0);
472 assert(*height > 0);
474 bPtr->columnSize.width = (*width-(cols-1)*COLUMN_SPACING) / cols;
475 bPtr->columnSize.height = *height;
477 if (bPtr->flags.isTitled) {
478 colY = TITLE_SPACING + bPtr->titleHeight;
479 bPtr->columnSize.height -= colY;
480 } else {
481 colY = 0;
484 if (bPtr->flags.hasScroller) {
485 bPtr->columnSize.height -= SCROLLER_WIDTH + 4;
487 if (bPtr->scroller) {
488 WMResizeWidget(bPtr->scroller, *width-2, 1);
489 WMMoveWidget(bPtr->scroller, 1, *height-SCROLLER_WIDTH-1);
493 colX = 0;
494 for (i = 0; i < bPtr->columnCount; i++) {
495 WMResizeWidget(bPtr->columns[i], bPtr->columnSize.width,
496 bPtr->columnSize.height);
498 WMMoveWidget(bPtr->columns[i], colX, colY);
500 if (COLUMN_IS_VISIBLE(bPtr, i)) {
501 colX += bPtr->columnSize.width+COLUMN_SPACING;
507 static void
508 paintItem(WMList *lPtr, int index, Drawable d, char *text, int state,
509 WMRect *rect)
511 WMView *view = W_VIEW(lPtr);
512 W_Screen *scr = view->screen;
513 int width, height, x, y;
515 width = rect->size.width;
516 height = rect->size.height;
517 x = rect->pos.x;
518 y = rect->pos.y;
520 if (state & WLDSSelected)
521 XFillRectangle(scr->display, d, WMColorGC(scr->white), x, y,
522 width, height);
523 else
524 XClearArea(scr->display, d, x, y, width, height, False);
526 if (text) {
527 /* Avoid overlaping... */
528 int textLen = strlen(text);
529 int widthC = (state & WLDSIsBranch) ? width-20 : width-8;
530 if (WMWidthOfString(scr->normalFont, text, textLen) > widthC) {
531 char *textBuf = createTruncatedString(scr->normalFont,
532 text, &textLen, widthC);
533 W_PaintText(view, d, scr->normalFont, x+4, y, widthC,
534 WALeft, WMColorGC(scr->black), False, textBuf, textLen);
535 wfree(textBuf);
536 } else {
537 W_PaintText(view, d, scr->normalFont, x+4, y, widthC,
538 WALeft, WMColorGC(scr->black), False, text, textLen);
542 if (state & WLDSIsBranch) {
543 XDrawLine(scr->display, d, WMColorGC(scr->darkGray), x+width-11, y+3,
544 x+width-6, y+height/2);
545 if (state & WLDSSelected)
546 XDrawLine(scr->display, d,WMColorGC(scr->gray), x+width-11, y+height-5,
547 x+width-6, y+height/2);
548 else
549 XDrawLine(scr->display, d,WMColorGC(scr->white), x+width-11, y+height-5,
550 x+width-6, y+height/2);
551 XDrawLine(scr->display, d, WMColorGC(scr->black), x+width-12, y+3,
552 x+width-12, y+height-5);
557 static void
558 scrollCallback(WMWidget *scroller, void *self)
560 WMBrowser *bPtr = (WMBrowser*)self;
561 WMScroller *sPtr = (WMScroller*)scroller;
562 int newFirst;
563 #define LAST_VISIBLE_COLUMN bPtr->firstVisibleColumn+bPtr->maxVisibleColumns
565 switch (WMGetScrollerHitPart(sPtr)) {
566 case WSDecrementLine:
567 if (bPtr->firstVisibleColumn > 0) {
568 scrollToColumn(bPtr, bPtr->firstVisibleColumn-1, True);
570 break;
572 case WSDecrementPage:
573 if (bPtr->firstVisibleColumn > 0) {
574 newFirst = bPtr->firstVisibleColumn - bPtr->maxVisibleColumns;
576 scrollToColumn(bPtr, newFirst, True);
578 break;
581 case WSIncrementLine:
582 if (LAST_VISIBLE_COLUMN < bPtr->usedColumnCount) {
583 scrollToColumn(bPtr, bPtr->firstVisibleColumn+1, True);
585 break;
587 case WSIncrementPage:
588 if (LAST_VISIBLE_COLUMN < bPtr->usedColumnCount) {
589 newFirst = bPtr->firstVisibleColumn + bPtr->maxVisibleColumns;
591 if (newFirst+bPtr->maxVisibleColumns >= bPtr->columnCount)
592 newFirst = bPtr->columnCount - bPtr->maxVisibleColumns;
594 scrollToColumn(bPtr, newFirst, True);
596 break;
598 case WSKnob:
600 double floatValue;
601 double value = bPtr->columnCount - bPtr->maxVisibleColumns;
603 floatValue = WMGetScrollerValue(bPtr->scroller);
605 floatValue = (floatValue*value)/value;
607 newFirst = rint(floatValue*(float)(bPtr->columnCount - bPtr->maxVisibleColumns));
609 if (bPtr->firstVisibleColumn != newFirst)
610 scrollToColumn(bPtr, newFirst, False);
611 /* else
612 WMSetScrollerParameters(bPtr->scroller, floatValue,
613 bPtr->maxVisibleColumns/(float)bPtr->columnCount);
617 break;
619 case WSKnobSlot:
620 case WSNoPart:
621 /* do nothing */
622 break;
624 #undef LAST_VISIBLE_COLUMN
628 static void
629 setupScroller(WMBrowser *bPtr)
631 WMScroller *sPtr;
632 int y;
634 y = bPtr->view->size.height - SCROLLER_WIDTH - 1;
636 sPtr = WMCreateScroller(bPtr);
637 WMSetScrollerAction(sPtr, scrollCallback, bPtr);
638 WMMoveWidget(sPtr, 1, y);
639 WMResizeWidget(sPtr, bPtr->view->size.width-2, SCROLLER_WIDTH);
641 bPtr->scroller = sPtr;
643 WMMapWidget(sPtr);
647 void
648 WMSetBrowserAction(WMBrowser *bPtr, WMAction *action, void *clientData)
650 bPtr->action = action;
651 bPtr->clientData = clientData;
655 void
656 WMSetBrowserDoubleAction(WMBrowser *bPtr, WMAction *action, void *clientData)
658 bPtr->doubleAction = action;
659 bPtr->doubleClientData = clientData;
663 void
664 WMSetBrowserHasScroller(WMBrowser *bPtr, int hasScroller)
666 bPtr->flags.hasScroller = hasScroller;
671 char*
672 WMSetBrowserPath(WMBrowser *bPtr, char *path)
674 int i;
675 char *str = wstrdup(path);
676 char *tmp, *retPtr = NULL;
677 int item;
678 WMListItem *listItem;
680 /* WMLoadBrowserColumnZero must be call first */
681 if (!bPtr->flags.loaded) {
682 return False;
685 removeColumn(bPtr, 1);
687 WMSelectListItem(bPtr->columns[0], -1);
688 WMSetListPosition(bPtr->columns[0], 0);
690 i = 0;
691 tmp = strtok(str, bPtr->pathSeparator);
692 while (tmp) {
693 /* select it in the column */
694 item = WMFindRowOfListItemWithTitle(bPtr->columns[i], tmp);
695 if (item<0) {
696 retPtr = &path[(int)(tmp - str)];
697 break;
699 WMSelectListItem(bPtr->columns[i], item);
700 WMSetListPosition(bPtr->columns[i], item);
702 listItem = WMGetListItem(bPtr->columns[i], item);
703 if (!listItem || !listItem->isBranch) {
704 break;
707 /* load next column */
708 WMAddBrowserColumn(bPtr);
710 loadColumn(bPtr, i+1);
712 tmp = strtok(NULL, bPtr->pathSeparator);
714 i++;
716 wfree(str);
718 for (i = bPtr->usedColumnCount - 1;
719 (i > -1) && !WMGetListSelectedItem(bPtr->columns[i]);
720 i--);
722 bPtr->selectedColumn = i;
724 if (bPtr->columnCount < bPtr->maxVisibleColumns) {
725 int i = bPtr->maxVisibleColumns - bPtr->columnCount;
726 int curUsedColumnCount = bPtr->usedColumnCount;
727 bPtr->usedColumnCount = bPtr->columnCount;
728 while (i--) {
729 WMAddBrowserColumn(bPtr);
731 bPtr->usedColumnCount = curUsedColumnCount;
734 scrollToColumn(bPtr, bPtr->columnCount - bPtr->maxVisibleColumns, True);
736 return retPtr;
740 char*
741 WMGetBrowserPath(WMBrowser *bPtr)
743 return WMGetBrowserPathToColumn(bPtr, bPtr->columnCount);
748 char*
749 WMGetBrowserPathToColumn(WMBrowser *bPtr, int column)
751 int i, size;
752 char *path;
753 WMListItem *item;
755 if (column >= bPtr->usedColumnCount)
756 column = bPtr->usedColumnCount-1;
758 if (column < 0) {
759 return wstrdup(bPtr->pathSeparator);
762 /* calculate size of buffer */
763 size = 0;
764 for (i = 0; i <= column; i++) {
765 item = WMGetListSelectedItem(bPtr->columns[i]);
766 if (!item)
767 break;
768 size += strlen(item->text);
771 /* get the path */
772 path = wmalloc(size+(column+1)*strlen(bPtr->pathSeparator)+1);
773 /* ignore first / */
774 *path = 0;
775 for (i = 0; i <= column; i++) {
776 strcat(path, bPtr->pathSeparator);
777 item = WMGetListSelectedItem(bPtr->columns[i]);
778 if (!item)
779 break;
780 strcat(path, item->text);
783 return path;
787 static void
788 loadColumn(WMBrowser *bPtr, int column)
790 assert(bPtr->delegate);
791 assert(bPtr->delegate->createRowsForColumn);
793 bPtr->flags.loadingColumn = 1;
794 (*bPtr->delegate->createRowsForColumn)(bPtr->delegate, bPtr, column,
795 bPtr->columns[column]);
796 bPtr->flags.loadingColumn = 0;
798 if (bPtr->delegate->titleOfColumn) {
799 char *title;
801 title = (*bPtr->delegate->titleOfColumn)(bPtr->delegate, bPtr, column);
803 if (bPtr->titles[column])
804 wfree(bPtr->titles[column]);
806 bPtr->titles[column] = wstrdup(title);
808 if (COLUMN_IS_VISIBLE(bPtr, column) && bPtr->flags.isTitled) {
809 drawTitleOfColumn(bPtr, column);
815 static void
816 paintBrowser(WMBrowser *bPtr)
818 int i;
820 if (!bPtr->view->flags.mapped)
821 return;
823 W_DrawRelief(bPtr->view->screen, bPtr->view->window, 0,
824 bPtr->view->size.height-SCROLLER_WIDTH-2,
825 bPtr->view->size.width, 22, WRSunken);
827 if (bPtr->flags.isTitled) {
828 for (i=0; i<bPtr->maxVisibleColumns; i++) {
829 drawTitleOfColumn(bPtr, i+bPtr->firstVisibleColumn);
835 static void
836 handleEvents(XEvent *event, void *data)
838 WMBrowser *bPtr = (WMBrowser*)data;
840 CHECK_CLASS(data, WC_Browser);
843 switch (event->type) {
844 case Expose:
845 paintBrowser(bPtr);
846 break;
848 case DestroyNotify:
849 destroyBrowser(bPtr);
850 break;
857 static void
858 scrollToColumn(WMBrowser *bPtr, int column, Bool updateScroller)
860 int i;
861 int x;
862 int notify = 0;
865 if (column != bPtr->firstVisibleColumn) {
866 notify = 1;
869 if (column < 0)
870 column = 0;
872 if (notify && bPtr->delegate && bPtr->delegate->willScroll) {
873 (*bPtr->delegate->willScroll)(bPtr->delegate, bPtr);
876 x = 0;
877 bPtr->firstVisibleColumn = column;
878 for (i = 0; i < bPtr->columnCount; i++) {
879 if (COLUMN_IS_VISIBLE(bPtr, i)) {
880 WMMoveWidget(bPtr->columns[i], x,
881 WMWidgetView(bPtr->columns[i])->pos.y);
882 if (!WMWidgetView(bPtr->columns[i])->flags.realized)
883 WMRealizeWidget(bPtr->columns[i]);
884 WMMapWidget(bPtr->columns[i]);
885 x += bPtr->columnSize.width + COLUMN_SPACING;
886 } else {
887 WMUnmapWidget(bPtr->columns[i]);
891 /* update the scroller */
892 if (updateScroller) {
893 if (bPtr->columnCount > bPtr->maxVisibleColumns) {
894 float value, proportion;
896 value = bPtr->firstVisibleColumn
897 /(float)(bPtr->columnCount-bPtr->maxVisibleColumns);
898 proportion = bPtr->maxVisibleColumns/(float)bPtr->columnCount;
899 WMSetScrollerParameters(bPtr->scroller, value, proportion);
900 } else {
901 WMSetScrollerParameters(bPtr->scroller, 0, 1);
905 if (bPtr->view->flags.mapped)
906 paintBrowser(bPtr);
908 if (notify && bPtr->delegate && bPtr->delegate->didScroll) {
909 (*bPtr->delegate->didScroll)(bPtr->delegate, bPtr);
914 static void
915 listCallback(void *self, void *clientData)
917 WMBrowser *bPtr = (WMBrowser*)clientData;
918 WMList *lPtr = (WMList*)self;
919 WMListItem *item;
920 static WMListItem *oldItem = NULL;
921 int i;
923 item = WMGetListSelectedItem(lPtr);
924 if (!item) {
925 oldItem = item;
926 return;
929 if (oldItem != item) {
930 for (i=0; i<bPtr->columnCount; i++) {
931 if (lPtr == bPtr->columns[i])
932 break;
934 assert(i<bPtr->columnCount);
936 bPtr->selectedColumn = i;
938 /* columns at right must be cleared */
939 removeColumn(bPtr, i+1);
940 /* open directory */
941 if (item->isBranch) {
942 WMAddBrowserColumn(bPtr);
944 if (bPtr->usedColumnCount < bPtr->maxVisibleColumns)
945 i = 0;
946 else
947 i = bPtr->usedColumnCount-bPtr->maxVisibleColumns;
948 scrollToColumn(bPtr, i, True);
949 if (item->isBranch) {
950 loadColumn(bPtr, bPtr->usedColumnCount-1);
955 /* call callback for click */
956 if (bPtr->action)
957 (*bPtr->action)(bPtr, bPtr->clientData);
959 oldItem = item;
963 static void
964 listDoubleCallback(void *self, void *clientData)
966 WMBrowser *bPtr = (WMBrowser*)clientData;
967 WMList *lPtr = (WMList*)self;
968 WMListItem *item;
970 item = WMGetListSelectedItem(lPtr);
971 if (!item)
972 return;
974 /* call callback for double click */
975 if (bPtr->doubleAction)
976 (*bPtr->doubleAction)(bPtr, bPtr->doubleClientData);
980 void
981 WMLoadBrowserColumnZero(WMBrowser *bPtr)
983 if (!bPtr->flags.loaded) {
984 /* create column 0 */
985 WMAddBrowserColumn(bPtr);
987 loadColumn(bPtr, 0);
989 /* make column 0 visible */
990 scrollToColumn(bPtr, 0, True);
992 bPtr->flags.loaded = 1;
997 void
998 WMRemoveBrowserItem(WMBrowser *bPtr, int column, int row)
1000 WMList *list;
1002 if (column < 0 || column >= bPtr->usedColumnCount)
1003 return;
1005 list = WMGetBrowserListInColumn(bPtr, column);
1007 if (row < 0 || row >= WMGetListNumberOfRows(list))
1008 return;
1010 removeColumn(bPtr, column+1);
1011 if (bPtr->usedColumnCount < bPtr->maxVisibleColumns)
1012 scrollToColumn(bPtr, 0, True);
1013 else
1014 scrollToColumn(bPtr, bPtr->usedColumnCount-bPtr->maxVisibleColumns,
1015 True);
1017 WMRemoveListItem(list, row);
1021 static void
1022 listSelectionObserver(void *observerData, WMNotification *notification)
1024 WMBrowser *bPtr = (WMBrowser*)observerData;
1025 int column, item = (int)WMGetNotificationClientData(notification);
1026 WMList *lPtr = (WMList*)WMGetNotificationObject(notification);
1028 for (column = 0; column < bPtr->usedColumnCount; column++)
1029 if (bPtr->columns[column] == lPtr)
1030 break;
1032 /* this can happen when a list is being cleared with WMClearList
1033 * after the column was removed */
1034 if (column >= bPtr->usedColumnCount) {
1035 return;
1038 if (item < 0)
1039 column--;
1041 bPtr->selectedColumn = column;
1046 WMAddBrowserColumn(WMBrowser *bPtr)
1048 WMList *list;
1049 WMList **clist;
1050 char **tlist;
1051 int colY;
1052 int index;
1055 if (bPtr->usedColumnCount < bPtr->columnCount) {
1056 return bPtr->usedColumnCount++;
1059 bPtr->usedColumnCount++;
1061 if (bPtr->flags.isTitled) {
1062 colY = TITLE_SPACING + bPtr->titleHeight;
1063 } else {
1064 colY = 0;
1067 index = bPtr->columnCount;
1068 bPtr->columnCount++;
1069 clist = wmalloc(sizeof(WMList*)*bPtr->columnCount);
1070 tlist = wmalloc(sizeof(char*)*bPtr->columnCount);
1071 memcpy(clist, bPtr->columns, sizeof(WMList*)*(bPtr->columnCount-1));
1072 memcpy(tlist, bPtr->titles, sizeof(char*)*(bPtr->columnCount-1));
1073 if (bPtr->columns)
1074 wfree(bPtr->columns);
1075 if (bPtr->titles)
1076 wfree(bPtr->titles);
1077 bPtr->columns = clist;
1078 bPtr->titles = tlist;
1080 bPtr->titles[index] = NULL;
1082 list = WMCreateList(bPtr);
1083 WMSetListAction(list, listCallback, bPtr);
1084 WMSetListDoubleAction(list, listDoubleCallback, bPtr);
1085 WMSetListUserDrawProc(list, paintItem);
1086 WMAddNotificationObserver(listSelectionObserver, bPtr,
1087 WMListSelectionDidChangeNotification, list);
1089 bPtr->columns[index] = list;
1091 WMResizeWidget(list, bPtr->columnSize.width, bPtr->columnSize.height);
1092 WMMoveWidget(list, (bPtr->columnSize.width+COLUMN_SPACING)*index, colY);
1093 if (COLUMN_IS_VISIBLE(bPtr, index))
1094 WMMapWidget(list);
1096 /* update the scroller */
1097 if (bPtr->columnCount > bPtr->maxVisibleColumns) {
1098 float value, proportion;
1100 value = bPtr->firstVisibleColumn
1101 /(float)(bPtr->columnCount-bPtr->maxVisibleColumns);
1102 proportion = bPtr->maxVisibleColumns/(float)bPtr->columnCount;
1103 WMSetScrollerParameters(bPtr->scroller, value, proportion);
1106 return index;
1111 static void
1112 destroyBrowser(WMBrowser *bPtr)
1114 int i;
1116 for (i = 0; i < bPtr->columnCount; i++) {
1117 if (bPtr->titles[i])
1118 wfree(bPtr->titles[i]);
1120 wfree(bPtr->titles);
1122 wfree(bPtr->pathSeparator);
1124 WMRemoveNotificationObserver(bPtr);
1126 wfree(bPtr);
1130 static char*
1131 createTruncatedString(WMFont *font, char *text, int *textLen, int width)
1133 int dLen = WMWidthOfString(font, ".", 1);
1134 char *textBuf = (char*)wmalloc((*textLen)+4);
1136 if (width >= 3*dLen) {
1137 int dddLen = 3*dLen;
1138 int tmpTextLen = *textLen;
1140 strcpy(textBuf, text);
1141 while (tmpTextLen
1142 && (WMWidthOfString(font, textBuf, tmpTextLen)+dddLen > width))
1143 tmpTextLen--;
1144 strcpy(textBuf+tmpTextLen, "...");
1145 *textLen = tmpTextLen+3;
1146 } else if (width >= 2*dLen) {
1147 strcpy(textBuf, "..");
1148 *textLen = 2;
1149 } else if (width >= dLen) {
1150 strcpy(textBuf, ".");
1151 *textLen = 1;
1152 } else {
1153 *textBuf = '\0';
1154 *textLen = 0;
1156 return textBuf;