6 #include <math.h> /* for : double rint (double) */
10 typedef struct W_Browser
{
18 short usedColumnCount
; /* columns actually being used */
21 short maxVisibleColumns
;
22 short firstVisibleColumn
;
33 void *doubleClientData
;
34 WMAction
*doubleAction
;
36 WMBrowserDelegate
*delegate
;
43 unsigned int isTitled
:1;
44 unsigned int allowMultipleSelection
:1;
45 unsigned int allowEmptySelection
:1;
46 unsigned int hasScroller
:1;
49 unsigned int loaded
:1;
50 unsigned int loadingColumn
:1;
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
,
91 static void willResizeBrowser(W_ViewDelegate
*, WMView
*,
92 unsigned int*, unsigned int*);
94 W_ViewDelegate _BrowserViewDelegate
= {
105 WMCreateBrowser(WMWidget
*parent
)
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
));
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
)
143 for (i
=0; i
<bPtr
->maxVisibleColumns
; i
++) {
144 WMAddBrowserColumn(bPtr
);
146 bPtr
->usedColumnCount
= 0;
148 bPtr
->selectedColumn
= -1;
155 WMSetBrowserAllowMultipleSelection(WMBrowser
*bPtr
, Bool flag
)
159 bPtr
->flags
.allowMultipleSelection
= flag
? 1 : 0;
160 for (i
=0; i
<bPtr
->columnCount
; i
++) {
161 WMSetListAllowMultipleSelection(bPtr
->columns
[i
], flag
);
167 WMSetBrowserAllowEmptySelection(WMBrowser
*bPtr
, Bool flag
)
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
;
186 WMSetBrowserMaxVisibleColumns(WMBrowser
*bPtr
, int columns
)
188 int curMaxVisibleColumns
;
189 int newFirstVisibleColumn
= 0;
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
) {
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
;
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
;
223 WMAddBrowserColumn(bPtr
);
225 bPtr
->usedColumnCount
= curUsedColumnCount
;
227 /* browser loaded and columns < curMaxVisibleColumns */
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
;
250 WMSetBrowserPathSeparator(WMBrowser
*bPtr
, char *separator
)
252 if (bPtr
->pathSeparator
)
253 wfree(bPtr
->pathSeparator
);
254 bPtr
->pathSeparator
= wstrdup(separator
);
260 drawTitleOfColumn(WMBrowser
*bPtr
, int column
)
262 WMScreen
*scr
= bPtr
->view
->screen
;
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
)
278 char *titleBuf
= createTruncatedString(scr
->boldFont
,
279 bPtr
->titles
[column
],
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
);
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
);
297 WMGetBrowserListInColumn(WMBrowser
*bPtr
, int column
)
299 if (column
< 0 || column
>= bPtr
->usedColumnCount
)
302 return bPtr
->columns
[column
];
307 WMSetBrowserDelegate(WMBrowser
*bPtr
, WMBrowserDelegate
*delegate
)
309 bPtr
->delegate
= delegate
;
314 WMGetBrowserFirstVisibleColumn(WMBrowser
*bPtr
)
316 return bPtr
->firstVisibleColumn
;
321 removeColumn(WMBrowser
*bPtr
, int column
)
323 int i
, clearEnd
, destroyEnd
;
329 column
= (column
< 0) ? 0 : column
;
330 if (column
>= bPtr
->columnCount
) {
333 if (column
< bPtr
->maxVisibleColumns
) {
334 clearEnd
= bPtr
->maxVisibleColumns
;
335 destroyEnd
= bPtr
->columnCount
;
336 bPtr
->columnCount
= bPtr
->maxVisibleColumns
;
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
,
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
));
368 wfree(bPtr
->columns
);
369 bPtr
->titles
= tlist
;
370 bPtr
->columns
= clist
;
375 WMGetBrowserSelectedItemInColumn(WMBrowser
*bPtr
, int column
)
377 if ((column
< 0) || (column
>= bPtr
->usedColumnCount
))
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
]);
404 WMSetBrowserColumnTitle(WMBrowser
*bPtr
, int column
, char *title
)
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
);
421 WMSetBrowserTitled(WMBrowser
*bPtr
, Bool flag
)
424 int columnX
, columnY
;
426 if (bPtr
->flags
.isTitled
== flag
)
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
);
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
;
462 WMSortBrowserColumn(WMBrowser
*bPtr
, int column
)
464 WMSortListItems(bPtr
->columns
[column
]);
469 WMSortBrowserColumnWithComparer(WMBrowser
*bPtr
, int column
,
470 WMCompareDataProc
*func
)
472 WMSortListItemsWithComparer(bPtr
->columns
[column
], func
);
478 WMInsertBrowserItem(WMBrowser
*bPtr
, int column
, int row
, char *text
,
483 if (column
< 0 || column
>= bPtr
->columnCount
)
486 item
= WMInsertListItem(bPtr
->columns
[column
], row
, text
);
487 item
->isBranch
= isBranch
;
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
;
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
;
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);
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
;
541 paintItem(WMList
*lPtr
, int index
, Drawable d
, char *text
, int state
,
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
;
553 if (state
& WLDSSelected
)
554 XFillRectangle(scr
->display
, d
, WMColorGC(scr
->white
), x
, y
,
557 XClearArea(scr
->display
, d
, x
, y
, width
, height
, False
);
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
);
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);
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);
591 scrollCallback(WMWidget
*scroller
, void *self
)
593 WMBrowser
*bPtr
= (WMBrowser
*)self
;
594 WMScroller
*sPtr
= (WMScroller
*)scroller
;
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
);
605 case WSDecrementPage
:
606 case WSDecrementWheel
:
607 if (bPtr
->firstVisibleColumn
> 0) {
608 newFirst
= bPtr
->firstVisibleColumn
- bPtr
->maxVisibleColumns
;
610 scrollToColumn(bPtr
, newFirst
, True
);
615 case WSIncrementLine
:
616 if (LAST_VISIBLE_COLUMN
< bPtr
->usedColumnCount
) {
617 scrollToColumn(bPtr
, bPtr
->firstVisibleColumn
+1, True
);
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
);
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
);
647 WMSetScrollerParameters(bPtr->scroller, floatValue,
648 bPtr->maxVisibleColumns/(float)bPtr->columnCount);
659 #undef LAST_VISIBLE_COLUMN
664 setupScroller(WMBrowser
*bPtr
)
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
;
683 WMSetBrowserAction(WMBrowser
*bPtr
, WMAction
*action
, void *clientData
)
685 bPtr
->action
= action
;
686 bPtr
->clientData
= clientData
;
691 WMSetBrowserDoubleAction(WMBrowser
*bPtr
, WMAction
*action
, void *clientData
)
693 bPtr
->doubleAction
= action
;
694 bPtr
->doubleClientData
= clientData
;
699 WMSetBrowserHasScroller(WMBrowser
*bPtr
, int hasScroller
)
701 bPtr
->flags
.hasScroller
= hasScroller
;
707 WMSetBrowserPath(WMBrowser
*bPtr
, char *path
)
710 char *str
= wstrdup(path
);
711 char *tmp
, *retPtr
= NULL
;
713 WMListItem
*listItem
;
715 /* WMLoadBrowserColumnZero must be call first */
716 if (!bPtr
->flags
.loaded
) {
720 removeColumn(bPtr
, 1);
722 WMSelectListItem(bPtr
->columns
[0], -1);
723 WMSetListPosition(bPtr
->columns
[0], 0);
726 tmp
= strtok(str
, bPtr
->pathSeparator
);
728 /* select it in the column */
729 item
= WMFindRowOfListItemWithTitle(bPtr
->columns
[i
], tmp
);
731 retPtr
= &path
[(int)(tmp
- str
)];
734 WMSelectListItem(bPtr
->columns
[i
], item
);
735 WMSetListPosition(bPtr
->columns
[i
], item
);
737 listItem
= WMGetListItem(bPtr
->columns
[i
], item
);
738 if (!listItem
|| !listItem
->isBranch
) {
742 /* load next column */
743 WMAddBrowserColumn(bPtr
);
745 loadColumn(bPtr
, i
+1);
747 tmp
= strtok(NULL
, bPtr
->pathSeparator
);
753 for (i
= bPtr
->usedColumnCount
- 1;
754 (i
> -1) && !WMGetListSelectedItem(bPtr
->columns
[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
;
764 WMAddBrowserColumn(bPtr
);
766 bPtr
->usedColumnCount
= curUsedColumnCount
;
769 scrollToColumn(bPtr
, bPtr
->columnCount
- bPtr
->maxVisibleColumns
, True
);
776 WMGetBrowserPath(WMBrowser
*bPtr
)
778 return WMGetBrowserPathToColumn(bPtr
, bPtr
->columnCount
);
784 WMGetBrowserPathToColumn(WMBrowser
*bPtr
, int column
)
790 if (column
>= bPtr
->usedColumnCount
)
791 column
= bPtr
->usedColumnCount
-1;
794 return wstrdup(bPtr
->pathSeparator
);
797 /* calculate size of buffer */
799 for (i
= 0; i
<= column
; i
++) {
800 item
= WMGetListSelectedItem(bPtr
->columns
[i
]);
803 size
+= strlen(item
->text
);
807 path
= wmalloc(size
+(column
+1)*strlen(bPtr
->pathSeparator
)+1);
810 for (i
= 0; i
<= column
; i
++) {
811 strcat(path
, bPtr
->pathSeparator
);
812 item
= WMGetListSelectedItem(bPtr
->columns
[i
]);
815 strcat(path
, item
->text
);
823 WMBrowserAllowsMultipleSelection(WMBrowser
*bPtr
)
825 return bPtr
->flags
.allowMultipleSelection
;
830 WMBrowserAllowsEmptySelection(WMBrowser
*bPtr
)
832 return bPtr
->flags
.allowEmptySelection
;
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
) {
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
);
865 paintBrowser(WMBrowser
*bPtr
)
869 if (!bPtr
->view
->flags
.mapped
)
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
);
885 handleEvents(XEvent
*event
, void *data
)
887 WMBrowser
*bPtr
= (WMBrowser
*)data
;
889 CHECK_CLASS(data
, WC_Browser
);
892 switch (event
->type
) {
898 destroyBrowser(bPtr
);
907 scrollToColumn(WMBrowser
*bPtr
, int column
, Bool updateScroller
)
914 if (column
!= bPtr
->firstVisibleColumn
) {
921 if (notify
&& bPtr
->delegate
&& bPtr
->delegate
->willScroll
) {
922 (*bPtr
->delegate
->willScroll
)(bPtr
->delegate
, bPtr
);
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
;
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
);
950 WMSetScrollerParameters(bPtr
->scroller
, 0, 1);
954 if (bPtr
->view
->flags
.mapped
)
957 if (notify
&& bPtr
->delegate
&& bPtr
->delegate
->didScroll
) {
958 (*bPtr
->delegate
->didScroll
)(bPtr
->delegate
, bPtr
);
964 listCallback(void *self
, void *clientData
)
966 WMBrowser
*bPtr
= (WMBrowser
*)clientData
;
967 WMList
*lPtr
= (WMList
*)self
;
969 static WMListItem
*oldItem
= NULL
;
972 item
= WMGetListSelectedItem(lPtr
);
974 if (oldItem
==NULL
|| oldItem
!=item
) {
975 for (i
=0; i
<bPtr
->columnCount
; i
++) {
976 if (lPtr
== bPtr
->columns
[i
])
979 assert(i
<bPtr
->columnCount
);
981 bPtr
->selectedColumn
= i
;
983 /* columns at right must be cleared */
984 removeColumn(bPtr
, i
+1);
986 if (item
&& item
->isBranch
) {
987 WMAddBrowserColumn(bPtr
);
989 if (bPtr
->usedColumnCount
< bPtr
->maxVisibleColumns
)
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 */
1002 (*bPtr
->action
)(bPtr
, bPtr
->clientData
);
1009 listDoubleCallback(void *self
, void *clientData
)
1011 WMBrowser
*bPtr
= (WMBrowser
*)clientData
;
1012 WMList
*lPtr
= (WMList
*)self
;
1015 item
= WMGetListSelectedItem(lPtr
);
1019 /* call callback for double click */
1020 if (bPtr
->doubleAction
)
1021 (*bPtr
->doubleAction
)(bPtr
, bPtr
->doubleClientData
);
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;
1043 WMRemoveBrowserItem(WMBrowser
*bPtr
, int column
, int row
)
1047 if (column
< 0 || column
>= bPtr
->usedColumnCount
)
1050 list
= WMGetBrowserListInColumn(bPtr
, column
);
1052 if (row
< 0 || row
>= WMGetListNumberOfRows(list
))
1055 removeColumn(bPtr
, column
+1);
1056 if (bPtr
->usedColumnCount
< bPtr
->maxVisibleColumns
)
1057 scrollToColumn(bPtr
, 0, True
);
1059 scrollToColumn(bPtr
, bPtr
->usedColumnCount
-bPtr
->maxVisibleColumns
,
1062 WMRemoveListItem(list
, row
);
1067 listSelectionObserver(void *observerData
, WMNotification
*notification
)
1069 WMBrowser
*bPtr
= (WMBrowser
*)observerData
;
1071 WMList
*lPtr
= (WMList
*)WMGetNotificationObject(notification
);
1073 for (column
= 0; column
< bPtr
->usedColumnCount
; column
++)
1074 if (bPtr
->columns
[column
] == lPtr
)
1077 /* this can happen when a list is being cleared with WMClearList
1078 * after the column was removed */
1079 if (column
>= bPtr
->usedColumnCount
) {
1083 if (WMGetArrayItemCount(WMGetListSelectedItems(lPtr
)) == 0)
1086 bPtr
->selectedColumn
= column
;
1091 WMAddBrowserColumn(WMBrowser
*bPtr
)
1100 if (bPtr
->usedColumnCount
< bPtr
->columnCount
) {
1101 return bPtr
->usedColumnCount
++;
1104 bPtr
->usedColumnCount
++;
1106 if (bPtr
->flags
.isTitled
) {
1107 colY
= TITLE_SPACING
+ bPtr
->titleHeight
;
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));
1119 wfree(bPtr
->columns
);
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
))
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
);
1159 destroyBrowser(WMBrowser
*bPtr
)
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
);
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
);
1189 && (WMWidthOfString(font
, textBuf
, tmpTextLen
)+dddLen
> width
))
1191 strcpy(textBuf
+tmpTextLen
, "...");
1192 *textLen
= tmpTextLen
+3;
1193 } else if (width
>= 2*dLen
) {
1194 strcpy(textBuf
, "..");
1196 } else if (width
>= dLen
) {
1197 strcpy(textBuf
, ".");