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 hasScroller
:1;
48 unsigned int loaded
:1;
49 unsigned int loadingColumn
:1;
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
);
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
= {
104 WMCreateBrowser(WMWidget
*parent
)
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
));
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
)
142 for (i
=0; i
<bPtr
->maxVisibleColumns
; i
++) {
143 WMAddBrowserColumn(bPtr
);
145 bPtr
->usedColumnCount
= 0;
147 bPtr
->selectedColumn
= -1;
154 WMGetBrowserMaxVisibleColumns(WMBrowser
*bPtr
)
156 return bPtr
->maxVisibleColumns
;
161 WMSetBrowserMaxVisibleColumns(WMBrowser
*bPtr
, int columns
)
163 int curMaxVisibleColumns
;
164 int newFirstVisibleColumn
= 0;
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
) {
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
;
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
;
198 WMAddBrowserColumn(bPtr
);
200 bPtr
->usedColumnCount
= curUsedColumnCount
;
202 /* browser loaded and columns < curMaxVisibleColumns */
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
;
225 WMSetBrowserPathSeparator(WMBrowser
*bPtr
, char *separator
)
227 if (bPtr
->pathSeparator
)
228 wfree(bPtr
->pathSeparator
);
229 bPtr
->pathSeparator
= wstrdup(separator
);
235 drawTitleOfColumn(WMBrowser
*bPtr
, int column
)
237 WMScreen
*scr
= bPtr
->view
->screen
;
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
)
253 char *titleBuf
= createTruncatedString(scr
->boldFont
,
254 bPtr
->titles
[column
],
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
);
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
);
272 WMGetBrowserListInColumn(WMBrowser
*bPtr
, int column
)
274 if (column
< 0 || column
>= bPtr
->usedColumnCount
)
277 return bPtr
->columns
[column
];
282 WMSetBrowserDelegate(WMBrowser
*bPtr
, WMBrowserDelegate
*delegate
)
284 bPtr
->delegate
= delegate
;
289 WMGetBrowserFirstVisibleColumn(WMBrowser
*bPtr
)
291 return bPtr
->firstVisibleColumn
;
296 removeColumn(WMBrowser
*bPtr
, int column
)
298 int i
, clearEnd
, destroyEnd
;
304 column
= (column
< 0) ? 0 : column
;
305 if (column
>= bPtr
->columnCount
) {
308 if (column
< bPtr
->maxVisibleColumns
) {
309 clearEnd
= bPtr
->maxVisibleColumns
;
310 destroyEnd
= bPtr
->columnCount
;
311 bPtr
->columnCount
= bPtr
->maxVisibleColumns
;
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
,
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
));
343 wfree(bPtr
->columns
);
344 bPtr
->titles
= tlist
;
345 bPtr
->columns
= clist
;
350 WMGetBrowserSelectedItemInColumn(WMBrowser
*bPtr
, int column
)
352 if ((column
< 0) || (column
>= bPtr
->usedColumnCount
))
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
]);
379 WMSetBrowserColumnTitle(WMBrowser
*bPtr
, int column
, char *title
)
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
);
396 WMSetBrowserTitled(WMBrowser
*bPtr
, Bool flag
)
399 int columnX
, columnY
;
401 if (bPtr
->flags
.isTitled
== flag
)
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
);
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
;
437 WMSortBrowserColumn(WMBrowser
*bPtr
, int column
)
439 WMSortListItems(bPtr
->columns
[column
]);
445 WMInsertBrowserItem(WMBrowser
*bPtr
, int column
, int row
, char *text
,
450 if (column
< 0 || column
>= bPtr
->columnCount
)
453 item
= WMInsertListItem(bPtr
->columns
[column
], row
, text
);
454 item
->isBranch
= isBranch
;
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
;
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
;
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);
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
;
508 paintItem(WMList
*lPtr
, int index
, Drawable d
, char *text
, int state
,
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
;
520 if (state
& WLDSSelected
)
521 XFillRectangle(scr
->display
, d
, WMColorGC(scr
->white
), x
, y
,
524 XClearArea(scr
->display
, d
, x
, y
, width
, height
, False
);
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
);
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);
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);
558 scrollCallback(WMWidget
*scroller
, void *self
)
560 WMBrowser
*bPtr
= (WMBrowser
*)self
;
561 WMScroller
*sPtr
= (WMScroller
*)scroller
;
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
);
572 case WSDecrementPage
:
573 if (bPtr
->firstVisibleColumn
> 0) {
574 newFirst
= bPtr
->firstVisibleColumn
- bPtr
->maxVisibleColumns
;
576 scrollToColumn(bPtr
, newFirst
, True
);
581 case WSIncrementLine
:
582 if (LAST_VISIBLE_COLUMN
< bPtr
->usedColumnCount
) {
583 scrollToColumn(bPtr
, bPtr
->firstVisibleColumn
+1, True
);
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
);
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
);
612 WMSetScrollerParameters(bPtr->scroller, floatValue,
613 bPtr->maxVisibleColumns/(float)bPtr->columnCount);
624 #undef LAST_VISIBLE_COLUMN
629 setupScroller(WMBrowser
*bPtr
)
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
;
648 WMSetBrowserAction(WMBrowser
*bPtr
, WMAction
*action
, void *clientData
)
650 bPtr
->action
= action
;
651 bPtr
->clientData
= clientData
;
656 WMSetBrowserDoubleAction(WMBrowser
*bPtr
, WMAction
*action
, void *clientData
)
658 bPtr
->doubleAction
= action
;
659 bPtr
->doubleClientData
= clientData
;
664 WMSetBrowserHasScroller(WMBrowser
*bPtr
, int hasScroller
)
666 bPtr
->flags
.hasScroller
= hasScroller
;
672 WMSetBrowserPath(WMBrowser
*bPtr
, char *path
)
675 char *str
= wstrdup(path
);
676 char *tmp
, *retPtr
= NULL
;
678 WMListItem
*listItem
;
680 /* WMLoadBrowserColumnZero must be call first */
681 if (!bPtr
->flags
.loaded
) {
685 removeColumn(bPtr
, 1);
687 WMSelectListItem(bPtr
->columns
[0], -1);
688 WMSetListPosition(bPtr
->columns
[0], 0);
691 tmp
= strtok(str
, bPtr
->pathSeparator
);
693 /* select it in the column */
694 item
= WMFindRowOfListItemWithTitle(bPtr
->columns
[i
], tmp
);
696 retPtr
= &path
[(int)(tmp
- str
)];
699 WMSelectListItem(bPtr
->columns
[i
], item
);
700 WMSetListPosition(bPtr
->columns
[i
], item
);
702 listItem
= WMGetListItem(bPtr
->columns
[i
], item
);
703 if (!listItem
|| !listItem
->isBranch
) {
707 /* load next column */
708 WMAddBrowserColumn(bPtr
);
710 loadColumn(bPtr
, i
+1);
712 tmp
= strtok(NULL
, bPtr
->pathSeparator
);
718 for (i
= bPtr
->usedColumnCount
- 1;
719 (i
> -1) && !WMGetListSelectedItem(bPtr
->columns
[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
;
729 WMAddBrowserColumn(bPtr
);
731 bPtr
->usedColumnCount
= curUsedColumnCount
;
734 scrollToColumn(bPtr
, bPtr
->columnCount
- bPtr
->maxVisibleColumns
, True
);
741 WMGetBrowserPath(WMBrowser
*bPtr
)
743 return WMGetBrowserPathToColumn(bPtr
, bPtr
->columnCount
);
749 WMGetBrowserPathToColumn(WMBrowser
*bPtr
, int column
)
755 if (column
>= bPtr
->usedColumnCount
)
756 column
= bPtr
->usedColumnCount
-1;
759 return wstrdup(bPtr
->pathSeparator
);
762 /* calculate size of buffer */
764 for (i
= 0; i
<= column
; i
++) {
765 item
= WMGetListSelectedItem(bPtr
->columns
[i
]);
768 size
+= strlen(item
->text
);
772 path
= wmalloc(size
+(column
+1)*strlen(bPtr
->pathSeparator
)+1);
775 for (i
= 0; i
<= column
; i
++) {
776 strcat(path
, bPtr
->pathSeparator
);
777 item
= WMGetListSelectedItem(bPtr
->columns
[i
]);
780 strcat(path
, item
->text
);
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
) {
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
);
816 paintBrowser(WMBrowser
*bPtr
)
820 if (!bPtr
->view
->flags
.mapped
)
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
);
836 handleEvents(XEvent
*event
, void *data
)
838 WMBrowser
*bPtr
= (WMBrowser
*)data
;
840 CHECK_CLASS(data
, WC_Browser
);
843 switch (event
->type
) {
849 destroyBrowser(bPtr
);
858 scrollToColumn(WMBrowser
*bPtr
, int column
, Bool updateScroller
)
865 if (column
!= bPtr
->firstVisibleColumn
) {
872 if (notify
&& bPtr
->delegate
&& bPtr
->delegate
->willScroll
) {
873 (*bPtr
->delegate
->willScroll
)(bPtr
->delegate
, bPtr
);
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
;
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
);
901 WMSetScrollerParameters(bPtr
->scroller
, 0, 1);
905 if (bPtr
->view
->flags
.mapped
)
908 if (notify
&& bPtr
->delegate
&& bPtr
->delegate
->didScroll
) {
909 (*bPtr
->delegate
->didScroll
)(bPtr
->delegate
, bPtr
);
915 listCallback(void *self
, void *clientData
)
917 WMBrowser
*bPtr
= (WMBrowser
*)clientData
;
918 WMList
*lPtr
= (WMList
*)self
;
920 static WMListItem
*oldItem
= NULL
;
923 item
= WMGetListSelectedItem(lPtr
);
929 if (oldItem
!= item
) {
930 for (i
=0; i
<bPtr
->columnCount
; i
++) {
931 if (lPtr
== bPtr
->columns
[i
])
934 assert(i
<bPtr
->columnCount
);
936 bPtr
->selectedColumn
= i
;
938 /* columns at right must be cleared */
939 removeColumn(bPtr
, i
+1);
941 if (item
->isBranch
) {
942 WMAddBrowserColumn(bPtr
);
944 if (bPtr
->usedColumnCount
< bPtr
->maxVisibleColumns
)
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 */
957 (*bPtr
->action
)(bPtr
, bPtr
->clientData
);
964 listDoubleCallback(void *self
, void *clientData
)
966 WMBrowser
*bPtr
= (WMBrowser
*)clientData
;
967 WMList
*lPtr
= (WMList
*)self
;
970 item
= WMGetListSelectedItem(lPtr
);
974 /* call callback for double click */
975 if (bPtr
->doubleAction
)
976 (*bPtr
->doubleAction
)(bPtr
, bPtr
->doubleClientData
);
981 WMLoadBrowserColumnZero(WMBrowser
*bPtr
)
983 if (!bPtr
->flags
.loaded
) {
984 /* create column 0 */
985 WMAddBrowserColumn(bPtr
);
989 /* make column 0 visible */
990 scrollToColumn(bPtr
, 0, True
);
992 bPtr
->flags
.loaded
= 1;
998 WMRemoveBrowserItem(WMBrowser
*bPtr
, int column
, int row
)
1002 if (column
< 0 || column
>= bPtr
->usedColumnCount
)
1005 list
= WMGetBrowserListInColumn(bPtr
, column
);
1007 if (row
< 0 || row
>= WMGetListNumberOfRows(list
))
1010 removeColumn(bPtr
, column
+1);
1011 if (bPtr
->usedColumnCount
< bPtr
->maxVisibleColumns
)
1012 scrollToColumn(bPtr
, 0, True
);
1014 scrollToColumn(bPtr
, bPtr
->usedColumnCount
-bPtr
->maxVisibleColumns
,
1017 WMRemoveListItem(list
, row
);
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
)
1032 /* this can happen when a list is being cleared with WMClearList
1033 * after the column was removed */
1034 if (column
>= bPtr
->usedColumnCount
) {
1041 bPtr
->selectedColumn
= column
;
1046 WMAddBrowserColumn(WMBrowser
*bPtr
)
1055 if (bPtr
->usedColumnCount
< bPtr
->columnCount
) {
1056 return bPtr
->usedColumnCount
++;
1059 bPtr
->usedColumnCount
++;
1061 if (bPtr
->flags
.isTitled
) {
1062 colY
= TITLE_SPACING
+ bPtr
->titleHeight
;
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));
1074 wfree(bPtr
->columns
);
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
))
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
);
1112 destroyBrowser(WMBrowser
*bPtr
)
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
);
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
);
1142 && (WMWidthOfString(font
, textBuf
, tmpTextLen
)+dddLen
> width
))
1144 strcpy(textBuf
+tmpTextLen
, "...");
1145 *textLen
= tmpTextLen
+3;
1146 } else if (width
>= 2*dLen
) {
1147 strcpy(textBuf
, "..");
1149 } else if (width
>= dLen
) {
1150 strcpy(textBuf
, ".");