7 char *WMListDidScrollNotification
= "WMListDidScrollNotification";
8 char *WMListSelectionDidChangeNotification
= "WMListSelectionDidChangeNotification";
10 typedef struct W_List
{
14 WMListItem
*items
; /* array of items */
21 short topItem
; /* index of first visible item */
23 short fullFitLines
; /* no of lines that fit entirely */
27 void *doubleClientData
;
28 WMAction
*doubleAction
;
32 WMHandlerID
*idleID
; /* for updating the scroller after
35 WMScroller
*vScroller
;
37 WMScroller *hScroller;
41 unsigned int allowMultipleSelection
:1;
42 unsigned int userDrawn
:1;
43 unsigned int userItemHeight
:1;
45 unsigned int dontFitAll
:1; /* 1 = last item won't be fully visible */
46 unsigned int redrawPending
:1;
47 unsigned int buttonPressed
:1;
48 unsigned int buttonWasPressed
:1;
54 #define DEFAULT_WIDTH 150
55 #define DEFAULT_HEIGHT 150
58 static void destroyList(List
*lPtr
);
59 static void paintList(List
*lPtr
);
62 static void handleEvents(XEvent
*event
, void *data
);
63 static void handleActionEvents(XEvent
*event
, void *data
);
64 static void updateScroller(List
*lPtr
);
66 static void vScrollCallBack(WMWidget
*scroller
, void *self
);
68 static void resizeList();
71 W_ViewProcedureTable _ListViewProcedures
= {
80 WMCreateList(WMWidget
*parent
)
83 W_Screen
*scrPtr
= W_VIEW(parent
)->screen
;
85 lPtr
= wmalloc(sizeof(List
));
86 memset(lPtr
, 0, sizeof(List
));
88 lPtr
->widgetClass
= WC_List
;
90 lPtr
->view
= W_CreateView(W_VIEW(parent
));
95 lPtr
->view
->self
= lPtr
;
97 WMCreateEventHandler(lPtr
->view
, ExposureMask
|StructureNotifyMask
98 |ClientMessageMask
, handleEvents
, lPtr
);
100 WMCreateEventHandler(lPtr
->view
, ButtonPressMask
|ButtonReleaseMask
101 |EnterWindowMask
|LeaveWindowMask
|ButtonMotionMask
,
102 handleActionEvents
, lPtr
);
104 lPtr
->itemHeight
= WMFontHeight(scrPtr
->normalFont
) + 1;
106 /* create the vertical scroller */
107 lPtr
->vScroller
= WMCreateScroller(lPtr
);
108 WMMoveWidget(lPtr
->vScroller
, 1, 1);
109 WMSetScrollerArrowsPosition(lPtr
->vScroller
, WSAMaxEnd
);
111 WMSetScrollerAction(lPtr
->vScroller
, vScrollCallBack
, lPtr
);
113 /* make the scroller map itself when it's realized */
114 WMMapWidget(lPtr
->vScroller
);
116 resizeList(lPtr
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
118 lPtr
->selectedItem
= -1;
126 WMAddSortedListItem(WMList
*lPtr
, char *text
)
132 item
= wmalloc(sizeof(WMListItem
));
133 memset(item
, 0, sizeof(WMListItem
));
134 item
->text
= wstrdup(text
);
138 } else if (strcmp(lPtr
->items
->text
, text
) > 0) {
139 item
->nextPtr
= lPtr
->items
;
147 while (tmp
->nextPtr
) {
148 if (strcmp(tmp
->nextPtr
->text
, text
) >= 0) {
149 item
->nextPtr
= tmp
->nextPtr
;
164 if (lPtr
->selectedItem
>= index
)
165 lPtr
->selectedItem
++;
167 /* update the scroller when idle, so that we don't waste time
168 * updating it when another item is going to be added later */
170 lPtr
->idleID
= WMAddIdleHandler((WMCallback
*)updateScroller
, lPtr
);
179 WMInsertListItem(WMList
*lPtr
, int row
, char *text
)
182 WMListItem
*tmp
= lPtr
->items
;
184 CHECK_CLASS(lPtr
, WC_List
);
186 item
= wmalloc(sizeof(WMListItem
));
187 memset(item
, 0, sizeof(WMListItem
));
188 item
->text
= wstrdup(text
);
190 if (lPtr
->selectedItem
>= row
&& lPtr
->selectedItem
>= 0)
191 lPtr
->selectedItem
++;
193 if (lPtr
->items
==NULL
) {
195 } else if (row
== 0) {
196 item
->nextPtr
= lPtr
->items
;
198 } else if (row
< 0) {
203 row
= lPtr
->itemCount
;
208 item
->nextPtr
= tmp
->nextPtr
;
214 /* update the scroller when idle, so that we don't waste time
215 * updating it when another item is going to be added later */
217 lPtr
->idleID
= WMAddIdleHandler((WMCallback
*)updateScroller
, lPtr
);
225 WMRemoveListItem(WMList
*lPtr
, int row
)
229 int topItem
= lPtr
->topItem
;
232 CHECK_CLASS(lPtr
, WC_List
);
234 if (row
< 0 || row
>= lPtr
->itemCount
)
237 if (lPtr
->selectedItem
== row
) {
238 lPtr
->selectedItem
= -1;
240 } else if (lPtr
->selectedItem
> row
) {
241 lPtr
->selectedItem
--;
244 if (row
<= lPtr
->topItem
+lPtr
->fullFitLines
+lPtr
->flags
.dontFitAll
)
246 if (lPtr
->topItem
< 0)
250 if (lPtr
->items
->text
)
251 free(lPtr
->items
->text
);
253 tmp
= lPtr
->items
->nextPtr
;
260 llist
= llist
->nextPtr
;
261 tmp
= llist
->nextPtr
;
262 llist
->nextPtr
= llist
->nextPtr
->nextPtr
;
273 lPtr
->idleID
= WMAddIdleHandler((WMCallback
*)updateScroller
, lPtr
);
275 if (lPtr
->topItem
!= topItem
)
276 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
278 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
,
279 (void*)((int)lPtr
->selectedItem
));
285 WMGetListItem(WMList
*lPtr
, int row
)
289 listPtr
= lPtr
->items
;
292 listPtr
= listPtr
->nextPtr
;
300 WMSetListUserDrawProc(WMList
*lPtr
, WMListDrawProc
*proc
)
302 lPtr
->flags
.userDrawn
= 1;
308 WMSetListUserDrawItemHeight(WMList
*lPtr
, unsigned short height
)
312 lPtr
->flags
.userItemHeight
= 1;
313 lPtr
->itemHeight
= height
;
318 WMClearList(WMList
*lPtr
)
320 WMListItem
*item
, *tmp
;
321 int oldSelected
= lPtr
->selectedItem
;
333 lPtr
->selectedItem
= -1;
336 WMDeleteIdleHandler(lPtr
->idleID
);
339 if (lPtr
->view
->flags
.realized
) {
340 updateScroller(lPtr
);
342 if (oldSelected
!= -1)
343 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
,
349 WMSetListAction(WMList
*lPtr
, WMAction
*action
, void *clientData
)
351 lPtr
->action
= action
;
352 lPtr
->clientData
= clientData
;
357 WMSetListDoubleAction(WMList
*lPtr
, WMAction
*action
, void *clientData
)
359 lPtr
->doubleAction
= action
;
360 lPtr
->doubleClientData
= clientData
;
365 WMGetListSelectedItem(WMList
*lPtr
)
367 int i
= lPtr
->selectedItem
;
370 if (lPtr
->selectedItem
< 0)
375 item
= item
->nextPtr
;
382 WMGetListSelectedItemRow(WMList
*lPtr
)
384 return lPtr
->selectedItem
;
389 WMGetListItemHeight(WMList
*lPtr
)
391 return lPtr
->itemHeight
;
396 WMSetListPosition(WMList
*lPtr
, int row
)
399 if (lPtr
->topItem
+ lPtr
->fullFitLines
> lPtr
->itemCount
)
400 lPtr
->topItem
= lPtr
->itemCount
- lPtr
->fullFitLines
;
402 if (lPtr
->topItem
< 0)
405 if (lPtr
->view
->flags
.realized
)
406 updateScroller(lPtr
);
411 WMSetListBottomPosition(WMList
*lPtr
, int row
)
413 if (lPtr
->itemCount
> lPtr
->fullFitLines
) {
414 lPtr
->topItem
= row
- lPtr
->fullFitLines
;
415 if (lPtr
->topItem
< 0)
417 if (lPtr
->view
->flags
.realized
)
418 updateScroller(lPtr
);
424 WMGetListNumberOfRows(WMList
*lPtr
)
426 return lPtr
->itemCount
;
430 WMGetListPosition(WMList
*lPtr
)
432 return lPtr
->topItem
;
437 vScrollCallBack(WMWidget
*scroller
, void *self
)
439 WMList
*lPtr
= (WMList
*)self
;
440 WMScroller
*sPtr
= (WMScroller
*)scroller
;
442 int topItem
= lPtr
->topItem
;
444 height
= lPtr
->view
->size
.height
- 4;
446 switch (WMGetScrollerHitPart(sPtr
)) {
447 case WSDecrementLine
:
448 if (lPtr
->topItem
> 0) {
451 updateScroller(lPtr
);
455 case WSDecrementPage
:
456 if (lPtr
->topItem
> 0) {
457 lPtr
->topItem
-= lPtr
->fullFitLines
-(1-lPtr
->flags
.dontFitAll
)-1;
458 if (lPtr
->topItem
< 0)
461 updateScroller(lPtr
);
466 case WSIncrementLine
:
467 if (lPtr
->topItem
+ lPtr
->fullFitLines
< lPtr
->itemCount
) {
470 updateScroller(lPtr
);
474 case WSIncrementPage
:
475 if (lPtr
->topItem
+ lPtr
->fullFitLines
< lPtr
->itemCount
) {
476 lPtr
->topItem
+= lPtr
->fullFitLines
-(1-lPtr
->flags
.dontFitAll
)-1;
478 if (lPtr
->topItem
+ lPtr
->fullFitLines
> lPtr
->itemCount
)
479 lPtr
->topItem
= lPtr
->itemCount
- lPtr
->fullFitLines
;
481 updateScroller(lPtr
);
487 int oldTopItem
= lPtr
->topItem
;
489 lPtr
->topItem
= WMGetScrollerValue(lPtr
->vScroller
) *
490 (float)(lPtr
->itemCount
- lPtr
->fullFitLines
);
492 if (oldTopItem
!= lPtr
->topItem
)
503 if (lPtr
->topItem
!= topItem
)
504 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
509 paintItem(List
*lPtr
, int index
)
511 WMView
*view
= lPtr
->view
;
512 W_Screen
*scr
= view
->screen
;
513 int width
, height
, x
, y
;
518 itemPtr
= lPtr
->items
;
520 itemPtr
= itemPtr
->nextPtr
;
522 width
= lPtr
->view
->size
.width
- 2 - 19;
523 height
= lPtr
->itemHeight
;
525 y
= 2 + (index
-lPtr
->topItem
) * lPtr
->itemHeight
+ 1;
527 if (lPtr
->flags
.userDrawn
) {
531 rect
.size
.width
= width
;
532 rect
.size
.height
= height
;
536 flags
= itemPtr
->uflags
;
537 if (itemPtr
->disabled
)
538 flags
|= WLDSDisabled
;
539 if (itemPtr
->selected
)
540 flags
|= WLDSSelected
;
541 if (itemPtr
->isBranch
)
542 flags
|= WLDSIsBranch
;
545 (*lPtr
->draw
)(lPtr
, index
, view
->window
, itemPtr
->text
, flags
,
548 if (itemPtr
->selected
)
549 XFillRectangle(scr
->display
, view
->window
, WMColorGC(scr
->white
), x
, y
,
552 XClearArea(scr
->display
, view
->window
, x
, y
, width
, height
, False
);
554 W_PaintText(view
, view
->window
, scr
->normalFont
, x
+4, y
, width
,
555 WALeft
, WMColorGC(scr
->black
), False
,
556 itemPtr
->text
, strlen(itemPtr
->text
));
563 paintList(List
*lPtr
)
565 W_Screen
*scrPtr
= lPtr
->view
->screen
;
568 if (!lPtr
->view
->flags
.mapped
)
571 if (lPtr
->itemCount
>0) {
572 if (lPtr
->topItem
+lPtr
->fullFitLines
+lPtr
->flags
.dontFitAll
> lPtr
->itemCount
) {
573 lim
= lPtr
->itemCount
- lPtr
->topItem
;
574 XClearArea(scrPtr
->display
, lPtr
->view
->window
, 19,
575 2+lim
*lPtr
->itemHeight
, lPtr
->view
->size
.width
-21,
576 lPtr
->view
->size
.height
-lim
*lPtr
->itemHeight
-3, False
);
578 lim
= lPtr
->fullFitLines
+ lPtr
->flags
.dontFitAll
;
580 for (i
= lPtr
->topItem
; i
< lPtr
->topItem
+ lim
; i
++) {
584 XClearWindow(scrPtr
->display
, lPtr
->view
->window
);
586 W_DrawRelief(scrPtr
, lPtr
->view
->window
, 0, 0, lPtr
->view
->size
.width
,
587 lPtr
->view
->size
.height
, WRSunken
);
592 scrollTo(List
*lPtr
, int newTop
)
599 updateScroller(List
*lPtr
)
601 float knobProportion
, floatValue
, tmp
;
604 WMDeleteIdleHandler(lPtr
->idleID
);
609 if (lPtr
->itemCount
== 0 || lPtr
->itemCount
<= lPtr
->fullFitLines
)
610 WMSetScrollerParameters(lPtr
->vScroller
, 0, 1);
612 tmp
= lPtr
->fullFitLines
;
613 knobProportion
= tmp
/(float)lPtr
->itemCount
;
615 floatValue
= (float)lPtr
->topItem
/(float)(lPtr
->itemCount
- lPtr
->fullFitLines
);
617 WMSetScrollerParameters(lPtr
->vScroller
, floatValue
, knobProportion
);
623 handleEvents(XEvent
*event
, void *data
)
625 List
*lPtr
= (List
*)data
;
627 CHECK_CLASS(data
, WC_List
);
630 switch (event
->type
) {
632 if (event
->xexpose
.count
!=0)
646 WMFindRowOfListItemWithTitle(WMList
*lPtr
, char *title
)
652 for (i
=0, item
=lPtr
->items
; item
!=NULL
; item
=item
->nextPtr
, i
++) {
653 if (strcmp(item
->text
, title
)==0) {
664 WMSelectListItem(WMList
*lPtr
, int row
)
669 if (row
>= lPtr
->itemCount
)
672 /* the check below must be changed when the multiple selection is
675 if (!lPtr
->flags
.allowMultipleSelection
&& row
== lPtr
->selectedItem
)
680 if (!lPtr
->flags
.allowMultipleSelection
) {
681 /* unselect previous selected item */
682 if (lPtr
->selectedItem
>= 0) {
683 itemPtr
= lPtr
->items
;
684 for (i
=0; i
<lPtr
->selectedItem
; i
++)
685 itemPtr
= itemPtr
->nextPtr
;
687 if (itemPtr
->selected
) {
688 itemPtr
->selected
= 0;
689 if (lPtr
->view
->flags
.mapped
&& i
>=lPtr
->topItem
690 && i
<=lPtr
->topItem
+lPtr
->fullFitLines
)
697 if (!lPtr
->flags
.allowMultipleSelection
) {
698 lPtr
->selectedItem
= -1;
700 WMPostNotificationName(WMListSelectionDidChangeNotification
,
701 lPtr
, (void*)((int)lPtr
->selectedItem
));
707 itemPtr
= lPtr
->items
;
708 for (i
=0; i
<row
; i
++)
709 itemPtr
= itemPtr
->nextPtr
;
710 if (lPtr
->flags
.allowMultipleSelection
)
711 itemPtr
->selected
= !itemPtr
->selected
;
713 itemPtr
->selected
= 1;
715 if (lPtr
->view
->flags
.mapped
) {
716 paintItem(lPtr
, row
);
718 if ((row
-lPtr
->topItem
+lPtr
->fullFitLines
)*lPtr
->itemHeight
719 > lPtr
->view
->size
.height
-2)
720 W_DrawRelief(lPtr
->view
->screen
, lPtr
->view
->window
, 0, 0,
721 lPtr
->view
->size
.width
, lPtr
->view
->size
.height
,
724 lPtr
->selectedItem
= row
;
726 WMPostNotificationName(WMListSelectionDidChangeNotification
, lPtr
,
727 (void*)((int)lPtr
->selectedItem
));
732 getItemIndexAt(List
*lPtr
, int clickY
)
736 index
= (clickY
- 2) / lPtr
->itemHeight
+ lPtr
->topItem
;
738 if (index
< 0 || index
>= lPtr
->itemCount
)
746 handleActionEvents(XEvent
*event
, void *data
)
748 List
*lPtr
= (List
*)data
;
750 int topItem
= lPtr
->topItem
;
752 CHECK_CLASS(data
, WC_List
);
754 switch (event
->type
) {
756 lPtr
->flags
.buttonPressed
= 0;
757 tmp
= getItemIndexAt(lPtr
, event
->xbutton
.y
);
759 if (tmp
== lPtr
->selectedItem
&& tmp
>= 0) {
761 (*lPtr
->action
)(lPtr
, lPtr
->clientData
);
766 lPtr
->flags
.buttonPressed
= lPtr
->flags
.buttonWasPressed
;
767 lPtr
->flags
.buttonWasPressed
= 0;
771 lPtr
->flags
.buttonWasPressed
= lPtr
->flags
.buttonPressed
;
772 lPtr
->flags
.buttonPressed
= 0;
776 if (event
->xbutton
.x
> WMWidgetWidth(lPtr
->vScroller
)) {
777 tmp
= getItemIndexAt(lPtr
, event
->xbutton
.y
);
778 lPtr
->flags
.buttonPressed
= 1;
781 if (tmp
== lPtr
->selectedItem
&& WMIsDoubleClick(event
)) {
782 WMSelectListItem(lPtr
, tmp
);
783 if (lPtr
->doubleAction
)
784 (*lPtr
->doubleAction
)(lPtr
, lPtr
->doubleClientData
);
786 WMSelectListItem(lPtr
, tmp
);
793 if (lPtr
->flags
.buttonPressed
) {
794 tmp
= getItemIndexAt(lPtr
, event
->xmotion
.y
);
795 if (tmp
>=0 && tmp
!= lPtr
->selectedItem
) {
796 WMSelectListItem(lPtr
, tmp
);
801 if (lPtr
->topItem
!= topItem
)
802 WMPostNotificationName(WMListDidScrollNotification
, lPtr
, NULL
);
807 resizeList(WMList
*lPtr
, unsigned int width
, unsigned int height
)
809 W_ResizeView(lPtr
->view
, width
, height
);
811 WMResizeWidget(lPtr
->vScroller
, 1, height
-2);
813 lPtr
->fullFitLines
= (height
- 4) / lPtr
->itemHeight
;
814 if (lPtr
->fullFitLines
* lPtr
->itemHeight
< height
-4) {
815 lPtr
->flags
.dontFitAll
= 1;
817 lPtr
->flags
.dontFitAll
= 0;
820 if (lPtr
->itemCount
- lPtr
->topItem
<= lPtr
->fullFitLines
) {
821 lPtr
->topItem
= lPtr
->itemCount
- lPtr
->fullFitLines
;
822 if (lPtr
->topItem
< 0)
826 updateScroller(lPtr
);
831 destroyList(List
*lPtr
)
836 WMDeleteIdleHandler(lPtr
->idleID
);
839 while (lPtr
->items
!=NULL
) {
840 itemPtr
= lPtr
->items
;
844 lPtr
->items
= itemPtr
->nextPtr
;