7 char *WMListDidScrollNotification
= "WMListDidScrollNotification";
8 char *WMListSelectionDidChangeNotification
= "WMListSelectionDidChangeNotification";
10 typedef struct W_List
{
14 WMArray
*items
; /* list of WMListItem */
15 WMArray
*selectedItems
; /* list of selected WMListItems */
19 short topItem
; /* index of first visible item */
21 short fullFitLines
; /* no of lines that fit entirely */
25 void *doubleClientData
;
26 WMAction
*doubleAction
;
30 WMHandlerID
*idleID
; /* for updating the scroller after adding elements */
32 WMHandlerID
*selectID
; /* for selecting items in list while scrolling */
34 WMScroller
*vScroller
;
37 unsigned int allowMultipleSelection
:1;
38 unsigned int allowEmptySelection
:1;
39 unsigned int userDrawn
:1;
40 unsigned int userItemHeight
:1;
41 unsigned int dontFitAll
:1; /* 1 = last item won't be fully visible */
42 unsigned int redrawPending
:1;
43 unsigned int buttonPressed
:1;
44 unsigned int buttonWasPressed
:1;
50 #define DEFAULT_WIDTH 150
51 #define DEFAULT_HEIGHT 150
53 #define SCROLL_DELAY 100
56 static void destroyList(List
*lPtr
);
57 static void paintList(List
*lPtr
);
60 static void handleEvents(XEvent
*event
, void *data
);
61 static void handleActionEvents(XEvent
*event
, void *data
);
63 static void updateScroller(void *data
);
64 static void scrollForwardSelecting(void *data
);
65 static void scrollBackwardSelecting(void *data
);
67 static void vScrollCallBack(WMWidget
*scroller
, void *self
);
69 static void toggleItemSelection(WMList
*lPtr
, int index
);
71 static void updateGeometry(WMList
*lPtr
);
72 static void didResizeList();
74 static void unselectAllListItems(WMList
*lPtr
, WMListItem
*exceptThis
);
77 W_ViewDelegate _ListViewDelegate
= {
87 releaseItem(void *data
)
89 WMListItem
*item
= (WMListItem
*)data
;
98 WMCreateList(WMWidget
*parent
)
101 W_Screen
*scrPtr
= W_VIEW(parent
)->screen
;
103 lPtr
= wmalloc(sizeof(List
));
104 memset(lPtr
, 0, sizeof(List
));
106 lPtr
->widgetClass
= WC_List
;
108 lPtr
->view
= W_CreateView(W_VIEW(parent
));
113 lPtr
->view
->self
= lPtr
;
115 lPtr
->view
->delegate
= &_ListViewDelegate
;
117 WMCreateEventHandler(lPtr
->view
, ExposureMask
|StructureNotifyMask
118 |ClientMessageMask
, handleEvents
, lPtr
);
120 WMCreateEventHandler(lPtr
->view
, ButtonPressMask
|ButtonReleaseMask
121 |EnterWindowMask
|LeaveWindowMask
|ButtonMotionMask
,
122 handleActionEvents
, lPtr
);
124 lPtr
->itemHeight
= WMFontHeight(scrPtr
->normalFont
) + 1;
126 lPtr
->items
= WMCreateArrayWithDestructor(4, releaseItem
);
127 lPtr
->selectedItems
= WMCreateArray(4);
129 /* create the vertical scroller */
130 lPtr
->vScroller
= WMCreateScroller(lPtr
);
131 WMMoveWidget(lPtr
->vScroller
, 1, 1);
132 WMSetScrollerArrowsPosition(lPtr
->vScroller
, WSAMaxEnd
);
134 WMSetScrollerAction(lPtr
->vScroller
, vScrollCallBack
, lPtr
);
136 /* make the scroller map itself when it's realized */
137 WMMapWidget(lPtr
->vScroller
);
139 W_ResizeView(lPtr
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
146 WMSetListAllowMultipleSelection(WMList
*lPtr
, Bool flag
)
148 lPtr
->flags
.allowMultipleSelection
= flag
? 1 : 0;
153 WMSetListAllowEmptySelection(WMList
*lPtr
, Bool flag
)
155 lPtr
->flags
.allowEmptySelection
= flag
? 1 : 0;
160 comparator(const void *a
, const void *b
)
162 return (strcmp((*(WMListItem
**)a
)->text
, (*(WMListItem
**)b
)->text
));
167 WMSortListItems(WMList
*lPtr
)
169 WMSortArray(lPtr
->items
, comparator
);
177 WMSortListItemsWithComparer(WMList
*lPtr
, WMCompareDataProc
*func
)
179 WMSortArray(lPtr
->items
, func
);
187 WMInsertListItem(WMList
*lPtr
, int row
, char *text
)
191 CHECK_CLASS(lPtr
, WC_List
);
193 item
= wmalloc(sizeof(WMListItem
));
194 memset(item
, 0, sizeof(WMListItem
));
195 item
->text
= wstrdup(text
);
197 row
= WMIN(row
, WMGetArrayItemCount(lPtr
->items
));
200 WMAddToArray(lPtr
->items
, item
);
202 WMInsertInArray(lPtr
->items
, row
, item
);
204 /* update the scroller when idle, so that we don't waste time
205 * updating it when another item is going to be added later */
207 lPtr
->idleID
= WMAddIdleHandler((WMCallback
*)updateScroller
, lPtr
);
215 WMRemoveListItem(WMList
*lPtr
, int row
)
218 int topItem
= lPtr
->topItem
;
221 CHECK_CLASS(lPtr
, WC_List
);
223 /*wassertr(row>=0 && row<WMGetArrayItemCount(lPtr->items));*/
224 if (row
<0 || row
>=WMGetArrayItemCount(lPtr
->items
))
227 item
= WMGetFromArray(lPtr
->items
, row
);
228 if (item
->selected
) {
229 WMRemoveFromArray(lPtr
->selectedItems
, item
);
233 if (row
<= lPtr
->topItem
+lPtr
->fullFitLines
+lPtr
->flags
.dontFitAll
)
235 if (lPtr
->topItem
< 0)
238 WMDeleteFromArray(lPtr
->items
, row
);
241 lPtr
->idleID
= WMAddIdleHandler((WMCallback
*)updateScroller
, lPtr
);
243 if (lPtr
->topItem
!= topItem
) {
244 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
247 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
253 WMGetListItem(WMList
*lPtr
, int row
)
255 return WMGetFromArray(lPtr
->items
, row
);
260 WMGetListItems(WMList
*lPtr
)
267 WMSetListUserDrawProc(WMList
*lPtr
, WMListDrawProc
*proc
)
269 lPtr
->flags
.userDrawn
= 1;
275 WMSetListUserDrawItemHeight(WMList
*lPtr
, unsigned short height
)
279 lPtr
->flags
.userItemHeight
= 1;
280 lPtr
->itemHeight
= height
;
282 updateGeometry(lPtr
);
287 WMClearList(WMList
*lPtr
)
289 int selNo
= WMGetArrayItemCount(lPtr
->selectedItems
);
291 WMEmptyArray(lPtr
->selectedItems
);
292 WMEmptyArray(lPtr
->items
);
297 WMDeleteIdleHandler(lPtr
->idleID
);
300 if (lPtr
->selectID
) {
301 WMDeleteTimerHandler(lPtr
->selectID
);
302 lPtr
->selectID
= NULL
;
304 if (lPtr
->view
->flags
.realized
) {
305 updateScroller(lPtr
);
308 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
314 WMSetListAction(WMList
*lPtr
, WMAction
*action
, void *clientData
)
316 lPtr
->action
= action
;
317 lPtr
->clientData
= clientData
;
322 WMSetListDoubleAction(WMList
*lPtr
, WMAction
*action
, void *clientData
)
324 lPtr
->doubleAction
= action
;
325 lPtr
->doubleClientData
= clientData
;
330 WMGetListSelectedItems(WMList
*lPtr
)
332 return lPtr
->selectedItems
;
337 WMGetListSelectedItem(WMList
*lPtr
)
339 return WMGetFromArray(lPtr
->selectedItems
, 0);
344 WMGetListSelectedItemRow(WMList
*lPtr
)
346 WMListItem
*item
= WMGetFromArray(lPtr
->selectedItems
, 0);
348 return (item
!=NULL
? WMGetFirstInArray(lPtr
->items
, item
) : WLNotFound
);
353 WMGetListItemHeight(WMList
*lPtr
)
355 return lPtr
->itemHeight
;
360 WMSetListPosition(WMList
*lPtr
, int row
)
363 if (lPtr
->topItem
+ lPtr
->fullFitLines
> WMGetArrayItemCount(lPtr
->items
))
364 lPtr
->topItem
= WMGetArrayItemCount(lPtr
->items
) - lPtr
->fullFitLines
;
366 if (lPtr
->topItem
< 0)
369 if (lPtr
->view
->flags
.realized
)
370 updateScroller(lPtr
);
375 WMSetListBottomPosition(WMList
*lPtr
, int row
)
377 if (WMGetArrayItemCount(lPtr
->items
) > lPtr
->fullFitLines
) {
378 lPtr
->topItem
= row
- lPtr
->fullFitLines
;
379 if (lPtr
->topItem
< 0)
381 if (lPtr
->view
->flags
.realized
)
382 updateScroller(lPtr
);
388 WMGetListNumberOfRows(WMList
*lPtr
)
390 return WMGetArrayItemCount(lPtr
->items
);
395 WMGetListPosition(WMList
*lPtr
)
397 return lPtr
->topItem
;
402 WMListAllowsMultipleSelection(WMList
*lPtr
)
404 return lPtr
->flags
.allowMultipleSelection
;
409 WMListAllowsEmptySelection(WMList
*lPtr
)
411 return lPtr
->flags
.allowEmptySelection
;
416 scrollByAmount(WMList
*lPtr
, int amount
)
418 int itemCount
= WMGetArrayItemCount(lPtr
->items
);
420 if ((amount
< 0 && lPtr
->topItem
> 0) ||
421 (amount
> 0 && (lPtr
->topItem
+ lPtr
->fullFitLines
< itemCount
))) {
423 lPtr
->topItem
+= amount
;
424 if (lPtr
->topItem
< 0)
426 if (lPtr
->topItem
+ lPtr
->fullFitLines
> itemCount
)
427 lPtr
->topItem
= itemCount
- lPtr
->fullFitLines
;
429 updateScroller(lPtr
);
435 vScrollCallBack(WMWidget
*scroller
, void *self
)
437 WMList
*lPtr
= (WMList
*)self
;
439 int oldTopItem
= lPtr
->topItem
;
440 int itemCount
= WMGetArrayItemCount(lPtr
->items
);
442 height
= lPtr
->view
->size
.height
- 4;
444 switch (WMGetScrollerHitPart((WMScroller
*)scroller
)) {
445 case WSDecrementLine
:
446 scrollByAmount(lPtr
, -1);
449 case WSIncrementLine
:
450 scrollByAmount(lPtr
, 1);
453 case WSDecrementPage
:
454 scrollByAmount(lPtr
, -lPtr
->fullFitLines
+(1-lPtr
->flags
.dontFitAll
)+1);
457 case WSIncrementPage
:
458 scrollByAmount(lPtr
, lPtr
->fullFitLines
-(1-lPtr
->flags
.dontFitAll
)-1);
461 case WSDecrementWheel
:
462 scrollByAmount(lPtr
, -lPtr
->fullFitLines
/ 3);
465 case WSIncrementWheel
:
466 scrollByAmount(lPtr
, lPtr
->fullFitLines
/ 3);
470 lPtr
->topItem
= WMGetScrollerValue(lPtr
->vScroller
) *
471 (float)(itemCount
- lPtr
->fullFitLines
);
473 if (oldTopItem
!= lPtr
->topItem
)
474 paintList(lPtr
); /* use updateScroller(lPtr) here? -Dan */
484 if (lPtr
->topItem
!= oldTopItem
)
485 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
490 paintItem(List
*lPtr
, int index
)
492 WMView
*view
= lPtr
->view
;
493 W_Screen
*scr
= view
->screen
;
494 int width
, height
, x
, y
;
497 itemPtr
= WMGetFromArray(lPtr
->items
, index
);
499 width
= lPtr
->view
->size
.width
- 2 - 19;
500 height
= lPtr
->itemHeight
;
502 y
= 2 + (index
-lPtr
->topItem
) * lPtr
->itemHeight
+ 1;
504 if (lPtr
->flags
.userDrawn
) {
508 rect
.size
.width
= width
;
509 rect
.size
.height
= height
;
513 flags
= itemPtr
->uflags
;
514 if (itemPtr
->disabled
)
515 flags
|= WLDSDisabled
;
516 if (itemPtr
->selected
)
517 flags
|= WLDSSelected
;
518 if (itemPtr
->isBranch
)
519 flags
|= WLDSIsBranch
;
522 (*lPtr
->draw
)(lPtr
, index
, view
->window
, itemPtr
->text
, flags
,
525 if (itemPtr
->selected
) {
526 XFillRectangle(scr
->display
, view
->window
, WMColorGC(scr
->white
),
527 x
, y
, width
, height
);
529 XClearArea(scr
->display
, view
->window
, x
, y
, width
, height
, False
);
532 W_PaintText(view
, view
->window
, scr
->normalFont
, x
+4, y
, width
,
533 WALeft
, WMColorGC(scr
->black
), False
,
534 itemPtr
->text
, strlen(itemPtr
->text
));
537 if ((index
-lPtr
->topItem
+lPtr
->fullFitLines
)*lPtr
->itemHeight
>
538 lPtr
->view
->size
.height
-2) {
539 W_DrawRelief(lPtr
->view
->screen
, lPtr
->view
->window
, 0, 0,
540 lPtr
->view
->size
.width
, lPtr
->view
->size
.height
,
548 paintList(List
*lPtr
)
550 W_Screen
*scrPtr
= lPtr
->view
->screen
;
553 if (!lPtr
->view
->flags
.mapped
)
556 if (WMGetArrayItemCount(lPtr
->items
) > 0) {
557 if (lPtr
->topItem
+lPtr
->fullFitLines
+lPtr
->flags
.dontFitAll
558 > WMGetArrayItemCount(lPtr
->items
)) {
560 lim
= WMGetArrayItemCount(lPtr
->items
) - lPtr
->topItem
;
561 XClearArea(scrPtr
->display
, lPtr
->view
->window
, 19,
562 2+lim
*lPtr
->itemHeight
, lPtr
->view
->size
.width
-21,
563 lPtr
->view
->size
.height
-lim
*lPtr
->itemHeight
-3, False
);
565 lim
= lPtr
->fullFitLines
+ lPtr
->flags
.dontFitAll
;
567 for (i
= lPtr
->topItem
; i
< lPtr
->topItem
+ lim
; i
++) {
571 XClearWindow(scrPtr
->display
, lPtr
->view
->window
);
573 W_DrawRelief(scrPtr
, lPtr
->view
->window
, 0, 0, lPtr
->view
->size
.width
,
574 lPtr
->view
->size
.height
, WRSunken
);
579 scrollTo(List
*lPtr
, int newTop
)
586 updateScroller(void *data
)
588 List
*lPtr
= (List
*)data
;
590 float knobProportion
, floatValue
, tmp
;
591 int count
= WMGetArrayItemCount(lPtr
->items
);
594 WMDeleteIdleHandler(lPtr
->idleID
);
599 if (count
== 0 || count
<= lPtr
->fullFitLines
)
600 WMSetScrollerParameters(lPtr
->vScroller
, 0, 1);
602 tmp
= lPtr
->fullFitLines
;
603 knobProportion
= tmp
/(float)count
;
605 floatValue
= (float)lPtr
->topItem
/(float)(count
- lPtr
->fullFitLines
);
607 WMSetScrollerParameters(lPtr
->vScroller
, floatValue
, knobProportion
);
613 scrollForwardSelecting(void *data
)
615 List
*lPtr
= (List
*)data
;
618 lastSelected
= lPtr
->topItem
+lPtr
->fullFitLines
+lPtr
->flags
.dontFitAll
-1;
620 if (lastSelected
>= WMGetArrayItemCount(lPtr
->items
)-1) {
621 lPtr
->selectID
= NULL
;
622 if (lPtr
->flags
.dontFitAll
)
623 scrollByAmount(lPtr
, 1);
627 /* selecting NEEDS to be done before scrolling to avoid flickering */
628 if (lPtr
->flags
.allowMultipleSelection
) {
632 item
= WMGetFromArray(lPtr
->selectedItems
, 0);
633 range
.position
= WMGetFirstInArray(lPtr
->items
, item
);
634 if (lastSelected
+1 >= range
.position
) {
635 range
.count
= lastSelected
- range
.position
+ 2;
637 range
.count
= lastSelected
- range
.position
;
639 WMSetListSelectionToRange(lPtr
, range
);
641 WMSelectListItem(lPtr
, lastSelected
+1);
643 scrollByAmount(lPtr
, 1);
645 lPtr
->selectID
= WMAddTimerHandler(SCROLL_DELAY
, scrollForwardSelecting
,
651 scrollBackwardSelecting(void *data
)
653 List
*lPtr
= (List
*)data
;
655 if (lPtr
->topItem
< 1) {
656 lPtr
->selectID
= NULL
;
660 /* selecting NEEDS to be done before scrolling to avoid flickering */
661 if (lPtr
->flags
.allowMultipleSelection
) {
665 item
= WMGetFromArray(lPtr
->selectedItems
, 0);
666 range
.position
= WMGetFirstInArray(lPtr
->items
, item
);
667 if (lPtr
->topItem
-1 >= range
.position
) {
668 range
.count
= lPtr
->topItem
- range
.position
;
670 range
.count
= lPtr
->topItem
- range
.position
- 2;
672 WMSetListSelectionToRange(lPtr
, range
);
674 WMSelectListItem(lPtr
, lPtr
->topItem
-1);
676 scrollByAmount(lPtr
, -1);
678 lPtr
->selectID
= WMAddTimerHandler(SCROLL_DELAY
, scrollBackwardSelecting
,
684 handleEvents(XEvent
*event
, void *data
)
686 List
*lPtr
= (List
*)data
;
688 CHECK_CLASS(data
, WC_List
);
691 switch (event
->type
) {
693 if (event
->xexpose
.count
!=0)
707 matchTitle(void *item
, void *title
)
709 return (strcmp(((WMListItem
*)item
)->text
, (char*)title
)==0 ? 1 : 0);
714 WMFindRowOfListItemWithTitle(WMList
*lPtr
, char *title
)
716 return WMFindInArray(lPtr
->items
, matchTitle
, title
);
721 WMSelectListItem(WMList
*lPtr
, int row
)
725 if (row
>= WMGetArrayItemCount(lPtr
->items
))
729 /* row = -1 will deselects all for backward compatibility.
730 * will be removed later. -Dan */
731 WMUnselectAllListItems(lPtr
);
735 item
= WMGetFromArray(lPtr
->items
, row
);
737 return; /* Return if already selected */
739 if (!lPtr
->flags
.allowMultipleSelection
) {
740 /* unselect previous selected items */
741 unselectAllListItems(lPtr
, NULL
);
746 WMAddToArray(lPtr
->selectedItems
, item
);
748 if (lPtr
->view
->flags
.mapped
&& row
>=lPtr
->topItem
749 && row
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
750 paintItem(lPtr
, row
);
753 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
758 WMUnselectListItem(WMList
*lPtr
, int row
)
760 WMListItem
*item
= WMGetFromArray(lPtr
->items
, row
);
762 if (!item
|| !item
->selected
)
765 if (!lPtr
->flags
.allowEmptySelection
&&
766 WMGetArrayItemCount(lPtr
->selectedItems
) <= 1) {
771 WMRemoveFromArray(lPtr
->selectedItems
, item
);
773 if (lPtr
->view
->flags
.mapped
&& row
>=lPtr
->topItem
774 && row
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
775 paintItem(lPtr
, row
);
778 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
783 WMSelectListItemsInRange(WMList
*lPtr
, WMRange range
)
786 int position
= range
.position
, k
= 1, notify
= 0;
787 int total
= WMGetArrayItemCount(lPtr
->items
);
789 if (!lPtr
->flags
.allowMultipleSelection
)
792 return; /* Nothing to select */
794 if (range
.count
< 0) {
795 range
.count
= -range
.count
;
799 for (; range
.count
>0 && position
>=0 && position
<total
; range
.count
--) {
800 item
= WMGetFromArray(lPtr
->items
, position
);
801 if (!item
->selected
) {
803 WMAddToArray(lPtr
->selectedItems
, item
);
804 if (lPtr
->view
->flags
.mapped
&& position
>=lPtr
->topItem
805 && position
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
806 paintItem(lPtr
, position
);
814 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
820 WMSetListSelectionToRange(WMList
*lPtr
, WMRange range
)
823 int mark1
, mark2
, i
, k
;
824 int position
= range
.position
, notify
= 0;
825 int total
= WMGetArrayItemCount(lPtr
->items
);
827 if (!lPtr
->flags
.allowMultipleSelection
)
830 if (range
.count
==0) {
831 WMUnselectAllListItems(lPtr
);
835 if (range
.count
< 0) {
836 mark1
= range
.position
+ range
.count
+ 1;
837 mark2
= range
.position
+ 1;
838 range
.count
= -range
.count
;
841 mark1
= range
.position
;
842 mark2
= range
.position
+ range
.count
;
850 WMEmptyArray(lPtr
->selectedItems
);
852 for (i
=0; i
<mark1
; i
++) {
853 item
= WMGetFromArray(lPtr
->items
, i
);
854 if (item
->selected
) {
856 if (lPtr
->view
->flags
.mapped
&& i
>=lPtr
->topItem
857 && i
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
863 for (; range
.count
>0 && position
>=0 && position
<total
; range
.count
--) {
864 item
= WMGetFromArray(lPtr
->items
, position
);
865 if (!item
->selected
) {
867 if (lPtr
->view
->flags
.mapped
&& position
>=lPtr
->topItem
868 && position
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
869 paintItem(lPtr
, position
);
873 WMAddToArray(lPtr
->selectedItems
, item
);
876 for (i
=mark2
; i
<total
; i
++) {
877 item
= WMGetFromArray(lPtr
->items
, i
);
878 if (item
->selected
) {
880 if (lPtr
->view
->flags
.mapped
&& i
>=lPtr
->topItem
881 && i
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
889 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
895 WMSelectAllListItems(WMList
*lPtr
)
900 if (!lPtr
->flags
.allowMultipleSelection
)
903 if (WMGetArrayItemCount(lPtr
->items
) ==
904 WMGetArrayItemCount(lPtr
->selectedItems
)) {
905 return; /* All items are selected already */
908 WMFreeArray(lPtr
->selectedItems
);
909 lPtr
->selectedItems
= WMCreateArrayWithArray(lPtr
->items
);
911 for (i
=0; i
<WMGetArrayItemCount(lPtr
->items
); i
++) {
912 item
= WMGetFromArray(lPtr
->items
, i
);
913 if (!item
->selected
) {
915 if (lPtr
->view
->flags
.mapped
&& i
>=lPtr
->topItem
916 && i
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
922 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
926 * Be careful from where you call this function! It doesn't honor the
927 * allowEmptySelection flag and doesn't send a notification about selection
928 * change! You need to manage these in the functions from where you call it.
930 * This will unselect all items if exceptThis is NULL, else will keep
931 * exceptThis selected.
932 * Make sure that exceptThis is one of the already selected items if not NULL!
936 unselectAllListItems(WMList
*lPtr
, WMListItem
*exceptThis
)
941 for (i
=0; i
<WMGetArrayItemCount(lPtr
->items
); i
++) {
942 item
= WMGetFromArray(lPtr
->items
, i
);
943 if (item
!=exceptThis
&& item
->selected
) {
945 if (lPtr
->view
->flags
.mapped
&& i
>=lPtr
->topItem
946 && i
<=lPtr
->topItem
+lPtr
->fullFitLines
) {
952 WMEmptyArray(lPtr
->selectedItems
);
953 if (exceptThis
!=NULL
) {
954 exceptThis
->selected
= 1;
955 WMAddToArray(lPtr
->selectedItems
, exceptThis
);
961 WMUnselectAllListItems(WMList
*lPtr
)
964 WMListItem
*keepItem
;
966 keep
= lPtr
->flags
.allowEmptySelection
? 0 : 1;
968 if (WMGetArrayItemCount(lPtr
->selectedItems
) == keep
)
971 keepItem
= (keep
==1 ? WMGetFromArray(lPtr
->selectedItems
, 0) : NULL
);
973 unselectAllListItems(lPtr
, keepItem
);
975 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
, NULL
);
980 getItemIndexAt(List
*lPtr
, int clickY
)
984 index
= (clickY
- 2) / lPtr
->itemHeight
+ lPtr
->topItem
;
986 if (index
< 0 || index
>= WMGetArrayItemCount(lPtr
->items
))
994 toggleItemSelection(WMList
*lPtr
, int index
)
996 WMListItem
*item
= WMGetFromArray(lPtr
->items
, index
);
998 if (item
&& item
->selected
) {
999 WMUnselectListItem(lPtr
, index
);
1001 WMSelectListItem(lPtr
, index
);
1007 handleActionEvents(XEvent
*event
, void *data
)
1009 List
*lPtr
= (List
*)data
;
1011 int topItem
= lPtr
->topItem
;
1012 static int lastClicked
= -1, prevItem
= -1;
1014 CHECK_CLASS(data
, WC_List
);
1016 switch (event
->type
) {
1018 /* Ignore mouse wheel events, they're not "real" button events */
1019 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelUp
||
1020 event
->xbutton
.button
== WINGsConfiguration
.mouseWheelDown
) {
1024 lPtr
->flags
.buttonPressed
= 0;
1025 if (lPtr
->selectID
) {
1026 WMDeleteTimerHandler(lPtr
->selectID
);
1027 lPtr
->selectID
= NULL
;
1029 tmp
= getItemIndexAt(lPtr
, event
->xbutton
.y
);
1033 (*lPtr
->action
)(lPtr
, lPtr
->clientData
);
1036 if (!(event
->xbutton
.state
& ShiftMask
))
1037 lastClicked
= prevItem
= tmp
;
1042 if (lPtr
->selectID
) {
1043 WMDeleteTimerHandler(lPtr
->selectID
);
1044 lPtr
->selectID
= NULL
;
1049 height
= WMWidgetHeight(lPtr
);
1050 if (lPtr
->flags
.buttonPressed
&& !lPtr
->selectID
) {
1051 if (event
->xcrossing
.y
>= height
) {
1052 lPtr
->selectID
= WMAddTimerHandler(SCROLL_DELAY
,
1053 scrollForwardSelecting
,
1055 } else if (event
->xcrossing
.y
<= 0) {
1056 lPtr
->selectID
= WMAddTimerHandler(SCROLL_DELAY
,
1057 scrollBackwardSelecting
,
1064 if (event
->xbutton
.x
<= WMWidgetWidth(lPtr
->vScroller
))
1066 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelDown
||
1067 event
->xbutton
.button
== WINGsConfiguration
.mouseWheelUp
) {
1070 if (event
->xbutton
.state
& ControlMask
) {
1071 amount
= lPtr
->fullFitLines
-(1-lPtr
->flags
.dontFitAll
)-1;
1072 } else if (event
->xbutton
.state
& ShiftMask
) {
1075 amount
= lPtr
->fullFitLines
/ 3;
1079 if (event
->xbutton
.button
== WINGsConfiguration
.mouseWheelUp
)
1082 scrollByAmount(lPtr
, amount
);
1086 tmp
= getItemIndexAt(lPtr
, event
->xbutton
.y
);
1087 lPtr
->flags
.buttonPressed
= 1;
1090 if (tmp
== lastClicked
&& WMIsDoubleClick(event
)) {
1091 WMSelectListItem(lPtr
, tmp
);
1092 if (lPtr
->doubleAction
)
1093 (*lPtr
->doubleAction
)(lPtr
, lPtr
->doubleClientData
);
1095 if (!lPtr
->flags
.allowMultipleSelection
) {
1096 if (event
->xbutton
.state
& ControlMask
) {
1097 toggleItemSelection(lPtr
, tmp
);
1099 WMSelectListItem(lPtr
, tmp
);
1103 WMListItem
*lastSel
;
1105 if (event
->xbutton
.state
& ControlMask
) {
1106 toggleItemSelection(lPtr
, tmp
);
1107 } else if (event
->xbutton
.state
& ShiftMask
) {
1108 if (WMGetArrayItemCount(lPtr
->selectedItems
) == 0) {
1109 WMSelectListItem(lPtr
, tmp
);
1111 lastSel
= WMGetFromArray(lPtr
->items
, lastClicked
);
1112 range
.position
= WMGetFirstInArray(lPtr
->items
,
1114 if (tmp
>= range
.position
)
1115 range
.count
= tmp
- range
.position
+ 1;
1117 range
.count
= tmp
- range
.position
- 1;
1119 WMSetListSelectionToRange(lPtr
, range
);
1122 range
.position
= tmp
;
1124 WMSetListSelectionToRange(lPtr
, range
);
1130 if (!(event
->xbutton
.state
& ShiftMask
))
1131 lastClicked
= prevItem
= tmp
;
1136 height
= WMWidgetHeight(lPtr
);
1137 if (lPtr
->selectID
&& event
->xmotion
.y
>0 && event
->xmotion
.y
<height
) {
1138 WMDeleteTimerHandler(lPtr
->selectID
);
1139 lPtr
->selectID
= NULL
;
1141 if (lPtr
->flags
.buttonPressed
&& !lPtr
->selectID
) {
1142 if (event
->xmotion
.y
<= 0) {
1143 lPtr
->selectID
= WMAddTimerHandler(SCROLL_DELAY
,
1144 scrollBackwardSelecting
,
1147 } else if (event
->xmotion
.y
>= height
) {
1148 lPtr
->selectID
= WMAddTimerHandler(SCROLL_DELAY
,
1149 scrollForwardSelecting
,
1154 tmp
= getItemIndexAt(lPtr
, event
->xmotion
.y
);
1155 if (tmp
>=0 && tmp
!=prevItem
) {
1156 if (lPtr
->flags
.allowMultipleSelection
) {
1159 range
.position
= lastClicked
;
1160 if (tmp
>= lastClicked
)
1161 range
.count
= tmp
- lastClicked
+ 1;
1163 range
.count
= tmp
- lastClicked
- 1;
1164 WMSetListSelectionToRange(lPtr
, range
);
1166 WMSelectListItem(lPtr
, tmp
);
1173 if (lPtr
->topItem
!= topItem
)
1174 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
1179 updateGeometry(WMList
*lPtr
)
1181 lPtr
->fullFitLines
= (lPtr
->view
->size
.height
- 4) / lPtr
->itemHeight
;
1182 if (lPtr
->fullFitLines
* lPtr
->itemHeight
< lPtr
->view
->size
.height
- 4) {
1183 lPtr
->flags
.dontFitAll
= 1;
1185 lPtr
->flags
.dontFitAll
= 0;
1188 if (WMGetArrayItemCount(lPtr
->items
) - lPtr
->topItem
<= lPtr
->fullFitLines
) {
1189 lPtr
->topItem
= WMGetArrayItemCount(lPtr
->items
) - lPtr
->fullFitLines
;
1190 if (lPtr
->topItem
< 0)
1194 updateScroller(lPtr
);
1199 didResizeList(W_ViewDelegate
*self
, WMView
*view
)
1201 WMList
*lPtr
= (WMList
*)view
->self
;
1203 WMResizeWidget(lPtr
->vScroller
, 1, view
->size
.height
-2);
1205 updateGeometry(lPtr
);
1210 destroyList(List
*lPtr
)
1213 WMDeleteIdleHandler(lPtr
->idleID
);
1214 lPtr
->idleID
= NULL
;
1217 WMDeleteTimerHandler(lPtr
->selectID
);
1218 lPtr
->selectID
= NULL
;
1220 WMFreeArray(lPtr
->items
);