7 char *WMListDidScrollNotification
= "WMListDidScrollNotification";
8 char *WMListSelectionDidChangeNotification
= "WMListSelectionDidChangeNotification";
10 typedef struct W_List
{
14 WMBag
*items
; /* list of WMListItem */
20 short topItem
; /* index of first visible item */
22 short fullFitLines
; /* no of lines that fit entirely */
26 void *doubleClientData
;
27 WMAction
*doubleAction
;
31 WMHandlerID
*idleID
; /* for updating the scroller after
34 WMScroller
*vScroller
;
36 WMScroller *hScroller;
40 unsigned int allowMultipleSelection
:1;
41 unsigned int userDrawn
:1;
42 unsigned int userItemHeight
:1;
44 unsigned int dontFitAll
:1; /* 1 = last item won't be fully visible */
45 unsigned int redrawPending
:1;
46 unsigned int buttonPressed
:1;
47 unsigned int buttonWasPressed
:1;
53 #define DEFAULT_WIDTH 150
54 #define DEFAULT_HEIGHT 150
57 static void destroyList(List
*lPtr
);
58 static void paintList(List
*lPtr
);
61 static void handleEvents(XEvent
*event
, void *data
);
62 static void handleActionEvents(XEvent
*event
, void *data
);
63 static void updateScroller(List
*lPtr
);
65 static void vScrollCallBack(WMWidget
*scroller
, void *self
);
67 static void updateGeometry(WMList
*lPtr
);
68 static void didResizeList();
71 W_ViewDelegate _ListViewDelegate
= {
82 WMCreateList(WMWidget
*parent
)
85 W_Screen
*scrPtr
= W_VIEW(parent
)->screen
;
87 lPtr
= wmalloc(sizeof(List
));
88 memset(lPtr
, 0, sizeof(List
));
90 lPtr
->widgetClass
= WC_List
;
92 lPtr
->view
= W_CreateView(W_VIEW(parent
));
97 lPtr
->view
->self
= lPtr
;
99 lPtr
->view
->delegate
= &_ListViewDelegate
;
101 WMCreateEventHandler(lPtr
->view
, ExposureMask
|StructureNotifyMask
102 |ClientMessageMask
, handleEvents
, lPtr
);
104 WMCreateEventHandler(lPtr
->view
, ButtonPressMask
|ButtonReleaseMask
105 |EnterWindowMask
|LeaveWindowMask
|ButtonMotionMask
,
106 handleActionEvents
, lPtr
);
108 lPtr
->itemHeight
= WMFontHeight(scrPtr
->normalFont
) + 1;
110 lPtr
->items
= WMCreateBag(4);
112 /* create the vertical scroller */
113 lPtr
->vScroller
= WMCreateScroller(lPtr
);
114 WMMoveWidget(lPtr
->vScroller
, 1, 1);
115 WMSetScrollerArrowsPosition(lPtr
->vScroller
, WSAMaxEnd
);
117 WMSetScrollerAction(lPtr
->vScroller
, vScrollCallBack
, lPtr
);
119 /* make the scroller map itself when it's realized */
120 WMMapWidget(lPtr
->vScroller
);
122 W_ResizeView(lPtr
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
124 lPtr
->selectedItem
= -1;
131 comparator(const void *a
, const void *b
)
133 return (strcmp((*(WMListItem
**)a
)->text
, (*(WMListItem
**)b
)->text
));
138 WMSortListItems(WMList
*lPtr
)
140 WMSortBag(lPtr
->items
, comparator
);
148 WMSortListItemsWithComparer(WMList
*lPtr
, int (f
)(const void*, const void*))
150 WMSortBag(lPtr
->items
, f
);
158 WMInsertListItem(WMList
*lPtr
, int row
, char *text
)
162 CHECK_CLASS(lPtr
, WC_List
);
164 item
= wmalloc(sizeof(WMListItem
));
165 memset(item
, 0, sizeof(WMListItem
));
166 item
->text
= wstrdup(text
);
169 if (lPtr
->selectedItem
>= row
&& lPtr
->selectedItem
>= 0
171 lPtr
->selectedItem
++;
173 row
= WMIN(row
, WMGetBagItemCount(lPtr
->items
));
176 WMPutInBag(lPtr
->items
, item
);
178 WMInsertInBag(lPtr
->items
, row
, item
);
180 /* update the scroller when idle, so that we don't waste time
181 * updating it when another item is going to be added later */
183 lPtr
->idleID
= WMAddIdleHandler((WMCallback
*)updateScroller
, lPtr
);
191 WMRemoveListItem(WMList
*lPtr
, int row
)
194 int topItem
= lPtr
->topItem
;
197 CHECK_CLASS(lPtr
, WC_List
);
199 if (row
< 0 || row
>= WMGetBagItemCount(lPtr
->items
))
202 if (lPtr
->selectedItem
== row
) {
203 lPtr
->selectedItem
= -1;
205 } else if (lPtr
->selectedItem
> row
) {
206 lPtr
->selectedItem
--;
209 if (row
<= lPtr
->topItem
+lPtr
->fullFitLines
+lPtr
->flags
.dontFitAll
)
211 if (lPtr
->topItem
< 0)
214 item
= WMGetFromBag(lPtr
->items
, row
);
219 WMDeleteFromBag(lPtr
->items
, row
);
222 lPtr
->idleID
= WMAddIdleHandler((WMCallback
*)updateScroller
, lPtr
);
224 if (lPtr
->topItem
!= topItem
)
225 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
227 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
,
228 (void*)((int)lPtr
->selectedItem
));
234 WMGetListItem(WMList
*lPtr
, int row
)
236 return WMGetFromBag(lPtr
->items
, row
);
241 WMGetListItems(WMList
*lPtr
)
249 WMSetListUserDrawProc(WMList
*lPtr
, WMListDrawProc
*proc
)
251 lPtr
->flags
.userDrawn
= 1;
257 WMSetListUserDrawItemHeight(WMList
*lPtr
, unsigned short height
)
261 lPtr
->flags
.userItemHeight
= 1;
262 lPtr
->itemHeight
= height
;
264 updateGeometry(lPtr
);
269 WMClearList(WMList
*lPtr
)
272 int oldSelected
= lPtr
->selectedItem
;
275 for (i
= 0; i
< WMGetBagItemCount(lPtr
->items
); i
++) {
276 item
= WMGetFromBag(lPtr
->items
, i
);
280 WMEmptyBag(lPtr
->items
);
283 lPtr
->selectedItem
= -1;
286 WMDeleteIdleHandler(lPtr
->idleID
);
289 if (lPtr
->view
->flags
.realized
) {
290 updateScroller(lPtr
);
292 if (oldSelected
!= -1)
293 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
,
299 WMSetListAction(WMList
*lPtr
, WMAction
*action
, void *clientData
)
301 lPtr
->action
= action
;
302 lPtr
->clientData
= clientData
;
307 WMSetListDoubleAction(WMList
*lPtr
, WMAction
*action
, void *clientData
)
309 lPtr
->doubleAction
= action
;
310 lPtr
->doubleClientData
= clientData
;
315 WMGetListSelectedItem(WMList
*lPtr
)
317 if (lPtr
->selectedItem
< 0)
320 return WMGetFromBag(lPtr
->items
, lPtr
->selectedItem
);
325 WMGetListSelectedItemRow(WMList
*lPtr
)
327 return lPtr
->selectedItem
;
332 WMGetListItemHeight(WMList
*lPtr
)
334 return lPtr
->itemHeight
;
339 WMSetListPosition(WMList
*lPtr
, int row
)
342 if (lPtr
->topItem
+ lPtr
->fullFitLines
> WMGetBagItemCount(lPtr
->items
))
343 lPtr
->topItem
= WMGetBagItemCount(lPtr
->items
) - lPtr
->fullFitLines
;
345 if (lPtr
->topItem
< 0)
348 if (lPtr
->view
->flags
.realized
)
349 updateScroller(lPtr
);
354 WMSetListBottomPosition(WMList
*lPtr
, int row
)
356 if (WMGetBagItemCount(lPtr
->items
) > lPtr
->fullFitLines
) {
357 lPtr
->topItem
= row
- lPtr
->fullFitLines
;
358 if (lPtr
->topItem
< 0)
360 if (lPtr
->view
->flags
.realized
)
361 updateScroller(lPtr
);
367 WMGetListNumberOfRows(WMList
*lPtr
)
369 return WMGetBagItemCount(lPtr
->items
);
373 WMGetListPosition(WMList
*lPtr
)
375 return lPtr
->topItem
;
380 vScrollCallBack(WMWidget
*scroller
, void *self
)
382 WMList
*lPtr
= (WMList
*)self
;
383 WMScroller
*sPtr
= (WMScroller
*)scroller
;
385 int topItem
= lPtr
->topItem
;
386 int itemCount
= WMGetBagItemCount(lPtr
->items
);
388 height
= lPtr
->view
->size
.height
- 4;
390 switch (WMGetScrollerHitPart(sPtr
)) {
391 case WSDecrementLine
:
392 if (lPtr
->topItem
> 0) {
395 updateScroller(lPtr
);
399 case WSDecrementPage
:
400 if (lPtr
->topItem
> 0) {
401 lPtr
->topItem
-= lPtr
->fullFitLines
-(1-lPtr
->flags
.dontFitAll
)-1;
402 if (lPtr
->topItem
< 0)
405 updateScroller(lPtr
);
410 case WSIncrementLine
:
411 if (lPtr
->topItem
+ lPtr
->fullFitLines
< itemCount
) {
414 updateScroller(lPtr
);
418 case WSIncrementPage
:
419 if (lPtr
->topItem
+ lPtr
->fullFitLines
< itemCount
) {
420 lPtr
->topItem
+= lPtr
->fullFitLines
-(1-lPtr
->flags
.dontFitAll
)-1;
422 if (lPtr
->topItem
+ lPtr
->fullFitLines
> itemCount
)
423 lPtr
->topItem
= itemCount
- lPtr
->fullFitLines
;
425 updateScroller(lPtr
);
431 int oldTopItem
= lPtr
->topItem
;
433 lPtr
->topItem
= WMGetScrollerValue(lPtr
->vScroller
) *
434 (float)(itemCount
- lPtr
->fullFitLines
);
436 if (oldTopItem
!= lPtr
->topItem
)
447 if (lPtr
->topItem
!= topItem
)
448 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
453 paintItem(List
*lPtr
, int index
)
455 WMView
*view
= lPtr
->view
;
456 W_Screen
*scr
= view
->screen
;
457 int width
, height
, x
, y
;
460 itemPtr
= WMGetFromBag(lPtr
->items
, index
);
462 width
= lPtr
->view
->size
.width
- 2 - 19;
463 height
= lPtr
->itemHeight
;
465 y
= 2 + (index
-lPtr
->topItem
) * lPtr
->itemHeight
+ 1;
467 if (lPtr
->flags
.userDrawn
) {
471 rect
.size
.width
= width
;
472 rect
.size
.height
= height
;
476 flags
= itemPtr
->uflags
;
477 if (itemPtr
->disabled
)
478 flags
|= WLDSDisabled
;
479 if (itemPtr
->selected
)
480 flags
|= WLDSSelected
;
481 if (itemPtr
->isBranch
)
482 flags
|= WLDSIsBranch
;
485 (*lPtr
->draw
)(lPtr
, index
, view
->window
, itemPtr
->text
, flags
,
488 if (itemPtr
->selected
)
489 XFillRectangle(scr
->display
, view
->window
, WMColorGC(scr
->white
),
490 x
, y
, width
, height
);
492 XClearArea(scr
->display
, view
->window
, x
, y
, width
, height
, False
);
494 W_PaintText(view
, view
->window
, scr
->normalFont
, x
+4, y
, width
,
495 WALeft
, WMColorGC(scr
->black
), False
,
496 itemPtr
->text
, strlen(itemPtr
->text
));
503 paintList(List
*lPtr
)
505 W_Screen
*scrPtr
= lPtr
->view
->screen
;
508 if (!lPtr
->view
->flags
.mapped
)
511 if (WMGetBagItemCount(lPtr
->items
) > 0) {
512 if (lPtr
->topItem
+lPtr
->fullFitLines
+lPtr
->flags
.dontFitAll
513 > WMGetBagItemCount(lPtr
->items
)) {
515 lim
= WMGetBagItemCount(lPtr
->items
) - lPtr
->topItem
;
516 XClearArea(scrPtr
->display
, lPtr
->view
->window
, 19,
517 2+lim
*lPtr
->itemHeight
, lPtr
->view
->size
.width
-21,
518 lPtr
->view
->size
.height
-lim
*lPtr
->itemHeight
-3, False
);
520 lim
= lPtr
->fullFitLines
+ lPtr
->flags
.dontFitAll
;
522 for (i
= lPtr
->topItem
; i
< lPtr
->topItem
+ lim
; i
++) {
526 XClearWindow(scrPtr
->display
, lPtr
->view
->window
);
528 W_DrawRelief(scrPtr
, lPtr
->view
->window
, 0, 0, lPtr
->view
->size
.width
,
529 lPtr
->view
->size
.height
, WRSunken
);
534 scrollTo(List
*lPtr
, int newTop
)
541 updateScroller(List
*lPtr
)
543 float knobProportion
, floatValue
, tmp
;
544 int count
= WMGetBagItemCount(lPtr
->items
);
547 WMDeleteIdleHandler(lPtr
->idleID
);
552 if (count
== 0 || count
<= lPtr
->fullFitLines
)
553 WMSetScrollerParameters(lPtr
->vScroller
, 0, 1);
555 tmp
= lPtr
->fullFitLines
;
556 knobProportion
= tmp
/(float)count
;
558 floatValue
= (float)lPtr
->topItem
/(float)(count
- lPtr
->fullFitLines
);
560 WMSetScrollerParameters(lPtr
->vScroller
, floatValue
, knobProportion
);
566 handleEvents(XEvent
*event
, void *data
)
568 List
*lPtr
= (List
*)data
;
570 CHECK_CLASS(data
, WC_List
);
573 switch (event
->type
) {
575 if (event
->xexpose
.count
!=0)
589 WMFindRowOfListItemWithTitle(WMList
*lPtr
, char *title
)
595 WM_ITERATE_BAG(lPtr
->items
, item
, i
) {
596 if (strcmp(item
->text
, title
)==0) {
602 return ok
? WMBagIndexForIterator(lPtr
->items
, i
) : -1;
607 WMSelectListItem(WMList
*lPtr
, int row
)
612 if (row
>= WMGetBagItemCount(lPtr
->items
))
615 /* the check below must be changed when the multiple selection is
618 if (!lPtr
->flags
.allowMultipleSelection
&& row
== lPtr
->selectedItem
)
623 assert(lPtr
->selectedItem
< WMGetBagItemCount(lPtr
->items
));
625 if (!lPtr
->flags
.allowMultipleSelection
) {
626 /* unselect previous selected item */
627 if (lPtr
->selectedItem
>= 0) {
628 itemPtr
= WMGetFromBag(lPtr
->items
, lPtr
->selectedItem
);
630 if (itemPtr
->selected
) {
631 itemPtr
->selected
= 0;
632 if (lPtr
->view
->flags
.mapped
633 && lPtr
->selectedItem
>=lPtr
->topItem
634 && lPtr
->selectedItem
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
635 paintItem(lPtr
, lPtr
->selectedItem
);
642 if (!lPtr
->flags
.allowMultipleSelection
) {
643 lPtr
->selectedItem
= -1;
645 WMPostNotificationName(WMListSelectionDidChangeNotification
,
646 lPtr
, (void*)((int)lPtr
->selectedItem
));
652 itemPtr
= WMGetFromBag(lPtr
->items
, row
);
654 if (lPtr
->flags
.allowMultipleSelection
)
655 itemPtr
->selected
= !itemPtr
->selected
;
657 itemPtr
->selected
= 1;
659 if (lPtr
->view
->flags
.mapped
) {
660 paintItem(lPtr
, row
);
662 if ((row
-lPtr
->topItem
+lPtr
->fullFitLines
)*lPtr
->itemHeight
663 > lPtr
->view
->size
.height
-2)
664 W_DrawRelief(lPtr
->view
->screen
, lPtr
->view
->window
, 0, 0,
665 lPtr
->view
->size
.width
, lPtr
->view
->size
.height
,
668 lPtr
->selectedItem
= row
;
670 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
,
671 (void*)((int)lPtr
->selectedItem
));
676 getItemIndexAt(List
*lPtr
, int clickY
)
680 index
= (clickY
- 2) / lPtr
->itemHeight
+ lPtr
->topItem
;
682 if (index
< 0 || index
>= WMGetBagItemCount(lPtr
->items
))
690 handleActionEvents(XEvent
*event
, void *data
)
692 List
*lPtr
= (List
*)data
;
694 int topItem
= lPtr
->topItem
;
696 CHECK_CLASS(data
, WC_List
);
698 switch (event
->type
) {
700 lPtr
->flags
.buttonPressed
= 0;
701 tmp
= getItemIndexAt(lPtr
, event
->xbutton
.y
);
703 if (tmp
== lPtr
->selectedItem
&& tmp
>= 0) {
705 (*lPtr
->action
)(lPtr
, lPtr
->clientData
);
710 lPtr
->flags
.buttonPressed
= lPtr
->flags
.buttonWasPressed
;
711 lPtr
->flags
.buttonWasPressed
= 0;
715 lPtr
->flags
.buttonWasPressed
= lPtr
->flags
.buttonPressed
;
716 lPtr
->flags
.buttonPressed
= 0;
720 if (event
->xbutton
.x
> WMWidgetWidth(lPtr
->vScroller
)) {
721 tmp
= getItemIndexAt(lPtr
, event
->xbutton
.y
);
722 lPtr
->flags
.buttonPressed
= 1;
725 if (tmp
== lPtr
->selectedItem
&& WMIsDoubleClick(event
)) {
726 WMSelectListItem(lPtr
, tmp
);
727 if (lPtr
->doubleAction
)
728 (*lPtr
->doubleAction
)(lPtr
, lPtr
->doubleClientData
);
730 WMSelectListItem(lPtr
, tmp
);
737 if (lPtr
->flags
.buttonPressed
) {
738 tmp
= getItemIndexAt(lPtr
, event
->xmotion
.y
);
739 if (tmp
>=0 && tmp
!= lPtr
->selectedItem
) {
740 WMSelectListItem(lPtr
, tmp
);
745 if (lPtr
->topItem
!= topItem
)
746 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
751 updateGeometry(WMList
*lPtr
)
753 lPtr
->fullFitLines
= (lPtr
->view
->size
.height
- 4) / lPtr
->itemHeight
;
754 if (lPtr
->fullFitLines
* lPtr
->itemHeight
< lPtr
->view
->size
.height
- 4) {
755 lPtr
->flags
.dontFitAll
= 1;
757 lPtr
->flags
.dontFitAll
= 0;
760 if (WMGetBagItemCount(lPtr
->items
) - lPtr
->topItem
<= lPtr
->fullFitLines
) {
761 lPtr
->topItem
= WMGetBagItemCount(lPtr
->items
) - lPtr
->fullFitLines
;
762 if (lPtr
->topItem
< 0)
766 updateScroller(lPtr
);
771 didResizeList(W_ViewDelegate
*self
, WMView
*view
)
773 WMList
*lPtr
= (WMList
*)view
->self
;
775 WMResizeWidget(lPtr
->vScroller
, 1, view
->size
.height
-2);
777 updateGeometry(lPtr
);
782 destroyList(List
*lPtr
)
788 WMDeleteIdleHandler(lPtr
->idleID
);
791 WM_ITERATE_BAG(lPtr
->items
, item
, i
) {
795 WMFreeBag(lPtr
->items
);