4 #include <X11/cursorfont.h>
6 #include "wtableview.h"
9 const char *WMTableViewSelectionDidChangeNotification
= "WMTableViewSelectionDidChangeNotification";
12 struct W_TableColumn
{
22 WMTableColumnDelegate
*delegate
;
30 static void handleResize(W_ViewDelegate
*self
, WMView
*view
);
32 static void rearrangeHeader(WMTableView
*table
);
34 static WMRange
rowsInRect(WMTableView
*table
, WMRect rect
);
37 WMTableColumn
*WMCreateTableColumn(char *title
)
39 WMTableColumn
*col
= wmalloc(sizeof(WMTableColumn
));
49 col
->title
= wstrdup(title
);
60 void WMSetTableColumnId(WMTableColumn
*column
, void *id
)
66 void *WMGetTableColumnId(WMTableColumn
*column
)
72 void WMSetTableColumnWidth(WMTableColumn
*column
, unsigned width
)
74 if (column
->maxWidth
== 0)
75 column
->width
= WMAX(column
->minWidth
, width
);
77 column
->width
= WMAX(column
->minWidth
, WMIN(column
->maxWidth
, width
));
80 rearrangeHeader(column
->table
);
85 void WMSetTableColumnDelegate(WMTableColumn
*column
,
86 WMTableColumnDelegate
*delegate
)
88 column
->delegate
= delegate
;
92 void WMSetTableColumnConstraints(WMTableColumn
*column
,
93 unsigned minWidth
, unsigned maxWidth
)
95 wassertr(minWidth
<= maxWidth
);
97 column
->minWidth
= minWidth
;
98 column
->maxWidth
= maxWidth
;
100 if (column
->width
< column
->minWidth
)
101 WMSetTableColumnWidth(column
, column
->minWidth
);
102 else if (column
->width
> column
->maxWidth
|| column
->maxWidth
== 0)
103 WMSetTableColumnWidth(column
, column
->maxWidth
);
107 void WMSetTableColumnEditable(WMTableColumn
*column
, Bool flag
)
109 column
->editable
= flag
;
113 WMTableView
*WMGetTableColumnTableView(WMTableColumn
*column
)
115 return column
->table
;
128 WMScrollView
*scrollView
;
134 WMArray
*selectedRows
;
143 Cursor splitterCursor
;
147 WMTableViewDelegate
*delegate
;
157 unsigned headerHeight
;
162 unsigned drawsGrid
:1;
163 unsigned canSelectRow
:1;
164 unsigned canSelectMultiRows
:1;
165 unsigned canDeselectRow
:1;
168 static W_Class tableClass
= 0;
171 static W_ViewDelegate viewDelegate
= {
183 static void handleEvents(XEvent
*event
, void *data
);
184 static void handleTableEvents(XEvent
*event
, void *data
);
187 static void scrollObserver(void *self
, WMNotification
*notif
)
189 WMTableView
*table
= (WMTableView
*)self
;
193 rect
= WMGetScrollViewVisibleRect(table
->scrollView
);
196 for (i
= 0; i
< WMGetArrayItemCount(table
->columns
); i
++) {
197 WMTableColumn
*column
;
199 column
= WMGetFromArray(table
->columns
, i
);
201 WMMoveWidget(column
->titleW
, x
- rect
.pos
.x
, 0);
206 splitter
= WMGetFromArray(table
->splitters
, i
-1);
207 W_MoveView(splitter
, x
- rect
.pos
.x
- 1, 0);
210 x
+= W_VIEW_WIDTH(WMWidgetView(column
->titleW
)) + 1;
215 static void splitterHandler(XEvent
*event
, void *data
)
217 WMTableView
*table
= (WMTableView
*)data
;
223 WMMaskEvent(event
->xany
.display
, ButtonMotionMask
|ButtonReleaseMask
,
226 switch (event
->type
) {
228 printf("%i\n", event
->xmotion
.x
);
240 WMTableView
*WMCreateTableView(WMWidget
*parent
)
242 WMTableView
*table
= wmalloc(sizeof(WMTableView
));
243 WMScreen
*scr
= WMWidgetScreen(parent
);
245 memset(table
, 0, sizeof(WMTableView
));
248 tableClass
= W_RegisterUserWidget();
250 table
->widgetClass
= tableClass
;
252 table
->view
= W_CreateView(W_VIEW(parent
));
255 table
->view
->self
= table
;
257 table
->view
->delegate
= &viewDelegate
;
259 table
->headerHeight
= 20;
261 table
->scrollView
= WMCreateScrollView(table
);
262 if (!table
->scrollView
)
264 WMResizeWidget(table
->scrollView
, 10, 10);
265 WMSetScrollViewHasVerticalScroller(table
->scrollView
, True
);
266 WMSetScrollViewHasHorizontalScroller(table
->scrollView
, True
);
268 WMScroller
*scroller
;
269 scroller
= WMGetScrollViewHorizontalScroller(table
->scrollView
);
270 WMAddNotificationObserver(scrollObserver
, table
,
271 WMScrollerDidScrollNotification
,
274 WMMoveWidget(table
->scrollView
, 1, 2+table
->headerHeight
);
275 WMMapWidget(table
->scrollView
);
277 table
->header
= WMCreateFrame(table
);
278 WMMoveWidget(table
->header
, 22, 2);
279 WMMapWidget(table
->header
);
280 WMSetFrameRelief(table
->header
, WRFlat
);
282 table
->corner
= WMCreateLabel(table
);
283 WMResizeWidget(table
->corner
, 20, table
->headerHeight
);
284 WMMoveWidget(table
->corner
, 2, 2);
285 WMMapWidget(table
->corner
);
286 WMSetLabelRelief(table
->corner
, WRRaised
);
287 WMSetWidgetBackgroundColor(table
->corner
, scr
->darkGray
);
290 table
->tableView
= W_CreateView(W_VIEW(parent
));
291 if (!table
->tableView
)
293 table
->tableView
->self
= table
;
294 W_ResizeView(table
->tableView
, 100, 1000);
295 W_MapView(table
->tableView
);
297 WMSetScrollViewContentView(table
->scrollView
, table
->tableView
);
299 table
->tableView
->flags
.dontCompressExpose
= 1;
301 table
->gridColor
= WMCreateNamedColor(scr
, "#cccccc", False
);
302 /* table->gridColor = WMGrayColor(scr);*/
307 gcv
.foreground
= WMColorPixel(table
->gridColor
);
309 gcv
.line_style
= LineOnOffDash
;
310 table
->gridGC
= XCreateGC(WMScreenDisplay(scr
), W_DRAWABLE(scr
),
314 table
->editingRow
= -1;
315 table
->clickedRow
= -1;
317 table
->drawsGrid
= 1;
318 table
->rowHeight
= 16;
320 WMSetScrollViewLineScroll(table
->scrollView
, table
->rowHeight
);
322 table
->tableWidth
= 1;
324 table
->columns
= WMCreateArray(4);
325 table
->splitters
= WMCreateArray(4);
327 table
->selectedRows
= WMCreateArray(16);
329 table
->splitterCursor
= XCreateFontCursor(WMScreenDisplay(scr
),
330 XC_sb_h_double_arrow
);
332 table
->canSelectRow
= 1;
334 WMCreateEventHandler(table
->view
, ExposureMask
|StructureNotifyMask
,
335 handleEvents
, table
);
337 WMCreateEventHandler(table
->tableView
, ExposureMask
|ButtonPressMask
|
338 ButtonReleaseMask
|ButtonMotionMask
,
339 handleTableEvents
, table
);
344 if (table
->scrollView
)
345 WMDestroyWidget(table
->scrollView
);
346 if (table
->tableView
)
347 W_DestroyView(table
->tableView
);
349 W_DestroyView(table
->view
);
355 void WMAddTableViewColumn(WMTableView
*table
, WMTableColumn
*column
)
359 WMScreen
*scr
= WMWidgetScreen(table
);
362 column
->table
= table
;
364 WMAddToArray(table
->columns
, column
);
366 if (!column
->titleW
) {
367 column
->titleW
= WMCreateLabel(table
);
368 WMSetLabelRelief(column
->titleW
, WRRaised
);
369 WMSetLabelFont(column
->titleW
, scr
->boldFont
);
370 WMSetLabelTextColor(column
->titleW
, scr
->white
);
371 WMSetWidgetBackgroundColor(column
->titleW
, scr
->darkGray
);
372 WMSetLabelText(column
->titleW
, column
->title
);
373 W_ReparentView(WMWidgetView(column
->titleW
),
374 WMWidgetView(table
->header
), 0, 0);
375 if (W_VIEW_REALIZED(table
->view
))
376 WMRealizeWidget(column
->titleW
);
377 WMMapWidget(column
->titleW
);
380 if (WMGetArrayItemCount(table
->columns
) > 1) {
381 WMView
*splitter
= W_CreateView(WMWidgetView(table
->header
));
383 W_SetViewBackgroundColor(splitter
, WMWhiteColor(scr
));
385 if (W_VIEW_REALIZED(table
->view
))
386 W_RealizeView(splitter
);
388 W_ResizeView(splitter
, 2, table
->headerHeight
-1);
391 W_SetViewCursor(splitter
, table
->splitterCursor
);
392 WMCreateEventHandler(splitter
, ButtonPressMask
,
393 splitterHandler
, table
);
395 WMAddToArray(table
->splitters
, splitter
);
398 rearrangeHeader(table
);
402 void WMSetTableViewHeaderHeight(WMTableView
*table
, unsigned height
)
404 table
->headerHeight
= height
;
406 handleResize(NULL
, table
->view
);
410 void WMSetTableViewDelegate(WMTableView
*table
, WMTableViewDelegate
*delegate
)
412 table
->delegate
= delegate
;
416 void WMSetTableViewAction(WMTableView
*table
, WMAction
*action
, void *clientData
)
418 table
->action
= action
;
420 table
->clientData
= clientData
;
424 void *WMGetTableViewClickedColumn(WMTableView
*table
)
426 return table
->clickedColumn
;
430 int WMGetTableViewClickedRow(WMTableView
*table
)
432 return table
->clickedRow
;
436 WMView
*WMGetTableViewDocumentView(WMTableView
*table
)
438 return table
->tableView
;
442 void *WMTableViewDataForCell(WMTableView
*table
, WMTableColumn
*column
,
445 return (*table
->delegate
->valueForCell
)(table
->delegate
, column
, row
);
449 void WMSetTableViewDataForCell(WMTableView
*table
, WMTableColumn
*column
,
452 (*table
->delegate
->setValueForCell
)(table
->delegate
, column
, row
, data
);
456 WMRect
WMTableViewRectForCell(WMTableView
*table
, WMTableColumn
*column
,
463 rect
.pos
.y
= row
* table
->rowHeight
;
464 rect
.size
.height
= table
->rowHeight
;
466 for (i
= 0; i
< WMGetArrayItemCount(table
->columns
); i
++) {
468 col
= WMGetFromArray(table
->columns
, i
);
471 rect
.size
.width
= col
->width
;
475 rect
.pos
.x
+= col
->width
;
481 void WMSetTableViewDataSource(WMTableView
*table
, void *source
)
483 table
->dataSource
= source
;
487 void *WMGetTableViewDataSource(WMTableView
*table
)
489 return table
->dataSource
;
493 void WMSetTableViewBackgroundColor(WMTableView
*table
, WMColor
*color
)
495 W_SetViewBackgroundColor(table
->tableView
, color
);
499 void WMSetTableViewGridColor(WMTableView
*table
, WMColor
*color
)
501 WMReleaseColor(table
->gridColor
);
502 table
->gridColor
= WMRetainColor(color
);
503 XSetForeground(WMScreenDisplay(WMWidgetScreen(table
)), table
->gridGC
,
504 WMColorPixel(color
));
509 void WMSetTableViewRowHeight(WMTableView
*table
, int height
)
511 table
->rowHeight
= height
;
513 WMRedisplayWidget(table
);
517 void WMScrollTableViewRowToVisible(WMTableView
*table
, int row
)
519 WMScroller
*scroller
;
524 rect
= WMGetScrollViewVisibleRect(table
->scrollView
);
525 range
= rowsInRect(table
, rect
);
527 scroller
= WMGetScrollViewVerticalScroller(table
->scrollView
);
529 if (row
< range
.position
) {
530 newY
= row
* table
->rowHeight
- rect
.size
.height
/ 2;
531 } else if (row
>= range
.position
+ range
.count
) {
532 newY
= row
* table
->rowHeight
- rect
.size
.height
/ 2;
536 tmp
= table
->rows
*table
->rowHeight
- rect
.size
.height
;
537 newY
= WMAX(0, WMIN(newY
, tmp
));
538 WMScrollViewScrollPoint(table
->scrollView
, rect
.pos
.x
, newY
);
543 static void drawGrid(WMTableView
*table
, WMRect rect
)
545 WMScreen
*scr
= WMWidgetScreen(table
);
546 Display
*dpy
= WMScreenDisplay(scr
);
551 Drawable d
= W_VIEW_DRAWABLE(table
->tableView
);
552 GC gc
= table
->gridGC
;
557 XSetDashes(dpy
, gc
, 0, dashl
, 1);
559 y1
= (rect
.pos
.y
/table
->rowHeight
- 1)*table
->rowHeight
;
560 y2
= y1
+ (rect
.size
.height
/table
->rowHeight
+2)*table
->rowHeight
;
563 y2
= W_VIEW_HEIGHT(table
->tableView
);
566 for (i
= 0; i
< WMGetArrayItemCount(table
->columns
); i
++) {
567 WMTableColumn
*column
;
569 if (xx
>= rect
.pos
.x
&& xx
<= rect
.pos
.x
+rect
.size
.width
) {
570 XDrawLine(dpy
, d
, gc
, xx
, y1
, xx
, y2
);
572 column
= WMGetFromArray(table
->columns
, i
);
575 XDrawLine(dpy
, d
, gc
, xx
, y1
, xx
, y2
);
579 x2
= WMIN(x1
+ rect
.size
.width
, xx
);
584 XSetDashes(dpy
, gc
, (rect
.pos
.x
&1), dashl
, 1);
587 y1
= rect
.pos
.y
- rect
.pos
.y
%table
->rowHeight
;
588 y2
= y1
+ rect
.size
.height
+ table
->rowHeight
;
590 for (i
= y1
; i
<= y2
; i
+= table
->rowHeight
) {
591 XDrawLine(dpy
, d
, gc
, x1
, i
, x2
, i
);
596 static WMRange
columnsInRect(WMTableView
*table
, WMRect rect
)
598 WMTableColumn
*column
;
601 int totalColumns
= WMGetArrayItemCount(table
->columns
);
606 for (i
= 0; i
< totalColumns
; i
++) {
607 column
= WMGetFromArray(table
->columns
, i
);
609 if (width
<= rect
.pos
.x
&& width
+ column
->width
> rect
.pos
.x
) {
615 if (width
> rect
.pos
.x
+ rect
.size
.width
) {
619 width
+= column
->width
;
621 range
.count
= WMAX(1, WMIN(range
.count
, totalColumns
- range
.position
));
626 static WMRange
rowsInRect(WMTableView
*table
, WMRect rect
)
629 int rh
= table
->rowHeight
;
632 dif
= rect
.pos
.y
% rh
;
634 range
.position
= WMAX(0, (rect
.pos
.y
- dif
) / rh
);
635 range
.count
= WMAX(1, WMIN((rect
.size
.height
+ dif
) / rh
, table
->rows
));
641 static void drawRow(WMTableView
*table
, int row
, WMRect clipRect
)
644 WMRange cols
= columnsInRect(table
, clipRect
);
645 WMTableColumn
*column
;
647 for (i
= cols
.position
; i
< cols
.position
+cols
.count
; i
++) {
648 column
= WMGetFromArray(table
->columns
, i
);
650 wassertr(column
->delegate
&& column
->delegate
->drawCell
);
652 if (WMFindInArray(table
->selectedRows
, NULL
, (void*)row
) != WANotFound
)
653 (*column
->delegate
->drawSelectedCell
)(column
->delegate
, column
, row
);
655 (*column
->delegate
->drawCell
)(column
->delegate
, column
, row
);
660 static void drawFullRow(WMTableView
*table
, int row
)
663 WMTableColumn
*column
;
665 for (i
= 0; i
< WMGetArrayItemCount(table
->columns
); i
++) {
666 column
= WMGetFromArray(table
->columns
, i
);
668 wassertr(column
->delegate
&& column
->delegate
->drawCell
);
670 if (WMFindInArray(table
->selectedRows
, NULL
, (void*)row
) != WANotFound
)
671 (*column
->delegate
->drawSelectedCell
)(column
->delegate
, column
, row
);
673 (*column
->delegate
->drawCell
)(column
->delegate
, column
, row
);
678 static void setRowSelected(WMTableView
*table
, unsigned row
, Bool flag
)
682 if (WMFindInArray(table
->selectedRows
, NULL
, (void*)row
) != WANotFound
) {
684 WMRemoveFromArray(table
->selectedRows
, (void*)row
);
689 WMAddToArray(table
->selectedRows
, (void*)row
);
693 if (repaint
&& row
< table
->rows
) {
694 drawFullRow(table
, row
);
699 static void repaintTable(WMTableView
*table
, int x
, int y
,
700 int width
, int height
)
706 wassertr(table
->delegate
&& table
->delegate
->numberOfRows
);
707 i
= (*table
->delegate
->numberOfRows
)(table
->delegate
, table
);
709 if (i
!= table
->rows
) {
711 W_ResizeView(table
->tableView
, table
->tableWidth
,
712 table
->rows
* table
->rowHeight
+ 1);
716 rect
.pos
= wmkpoint(x
,y
);
717 rect
.size
= wmksize(width
, height
);
719 if (table
->drawsGrid
) {
720 drawGrid(table
, rect
);
723 rows
= rowsInRect(table
, rect
);
724 for (i
= rows
.position
;
725 i
< WMIN(rows
.position
+rows
.count
+ 1, table
->rows
);
727 drawRow(table
, i
, rect
);
732 static void stopRowEdit(WMTableView
*table
, int row
)
735 WMTableColumn
*column
;
737 table
->editingRow
= -1;
738 for (i
= 0; i
< WMGetArrayItemCount(table
->columns
); i
++) {
739 column
= WMGetFromArray(table
->columns
, i
);
741 if (column
->delegate
&& column
->delegate
->endCellEdit
)
742 (*column
->delegate
->endCellEdit
)(column
->delegate
, column
, row
);
748 void WMEditTableViewRow(WMTableView
*table
, int row
)
751 WMTableColumn
*column
;
753 if (table
->editingRow
>= 0) {
754 stopRowEdit(table
, table
->editingRow
);
757 table
->editingRow
= row
;
758 for (i
= 0; i
< WMGetArrayItemCount(table
->columns
); i
++) {
759 column
= WMGetFromArray(table
->columns
, i
);
761 if (column
->delegate
&& column
->delegate
->beginCellEdit
)
762 (*column
->delegate
->beginCellEdit
)(column
->delegate
, column
, row
);
767 void WMSelectTableViewRow(WMTableView
*table
, int row
)
769 if (table
->clickedRow
>= 0)
770 setRowSelected(table
, table
->clickedRow
, False
);
772 if (row
>= table
->rows
) {
776 setRowSelected(table
, row
, True
);
777 table
->clickedRow
= row
;
780 (*table
->action
)(table
, table
->clientData
);
781 WMPostNotificationName(WMTableViewSelectionDidChangeNotification
,
786 void WMReloadTableView(WMTableView
*table
)
788 WMRect rect
= WMGetScrollViewVisibleRect(table
->scrollView
);
790 /* when this is called, nothing in the table can be assumed to be
791 * like the last time we accessed it (ie, rows might have disappeared) */
793 WMEmptyArray(table
->selectedRows
);
795 if (table
->clickedRow
>= 0) {
796 table
->clickedRow
= -1;
799 (*table
->action
)(table
, table
->clientData
);
800 WMPostNotificationName(WMTableViewSelectionDidChangeNotification
,
804 repaintTable(table
, 0, 0,
805 W_VIEW_WIDTH(table
->tableView
), rect
.size
.height
);
809 void WMNoteTableViewNumberOfRowsChanged(WMTableView
*table
)
811 WMReloadTableView(table
);
815 static void handleTableEvents(XEvent
*event
, void *data
)
817 WMTableView
*table
= (WMTableView
*)data
;
820 switch (event
->type
) {
822 if (event
->xbutton
.button
== Button1
) {
823 row
= event
->xbutton
.y
/table
->rowHeight
;
824 if (row
!= table
->clickedRow
) {
825 setRowSelected(table
, table
->clickedRow
, False
);
826 setRowSelected(table
, row
, True
);
827 table
->clickedRow
= row
;
834 if (table
->dragging
&& event
->xmotion
.y
>= 0) {
835 row
= event
->xmotion
.y
/table
->rowHeight
;
836 if (table
->clickedRow
!= row
&& row
>= 0) {
837 setRowSelected(table
, table
->clickedRow
, False
);
838 setRowSelected(table
, row
, True
);
839 table
->clickedRow
= row
;
845 if (event
->xbutton
.button
== Button1
) {
847 (*table
->action
)(table
, table
->clientData
);
848 WMPostNotificationName(WMTableViewSelectionDidChangeNotification
,
855 repaintTable(table
, event
->xexpose
.x
, event
->xexpose
.y
,
856 event
->xexpose
.width
, event
->xexpose
.height
);
862 static void handleEvents(XEvent
*event
, void *data
)
864 WMTableView
*table
= (WMTableView
*)data
;
865 WMScreen
*scr
= WMWidgetScreen(table
);
867 switch (event
->type
) {
869 W_DrawRelief(scr
, W_VIEW_DRAWABLE(table
->view
), 0, 0,
870 W_VIEW_WIDTH(table
->view
), W_VIEW_HEIGHT(table
->view
),
873 if (event
->xexpose
.serial
== 0) {
874 WMRect rect
= WMGetScrollViewVisibleRect(table
->scrollView
);
876 repaintTable(table
, rect
.pos
.x
, rect
.pos
.y
,
877 rect
.size
.width
, rect
.size
.height
);
884 static void handleResize(W_ViewDelegate
*self
, WMView
*view
)
888 WMTableView
*table
= view
->self
;
890 width
= W_VIEW_WIDTH(view
) - 2;
891 height
= W_VIEW_HEIGHT(view
) - 3;
893 height
-= table
->headerHeight
; /* table header */
896 WMResizeWidget(table
->corner
, 20, table
->headerHeight
);
898 if (table
->scrollView
) {
899 WMMoveWidget(table
->scrollView
, 1, table
->headerHeight
+ 2);
900 WMResizeWidget(table
->scrollView
, width
, height
);
903 WMResizeWidget(table
->header
, width
- 21, table
->headerHeight
);
907 static void rearrangeHeader(WMTableView
*table
)
914 count
= WMGetArrayItemCount(table
->columns
);
915 for (i
= 0; i
< count
; i
++) {
916 WMTableColumn
*column
= WMGetFromArray(table
->columns
, i
);
918 WMMoveWidget(column
->titleW
, width
, 0);
919 WMResizeWidget(column
->titleW
, column
->width
-1, table
->headerHeight
);
922 WMView
*splitter
= WMGetFromArray(table
->splitters
, i
-1);
924 W_MoveView(splitter
, width
-1, 0);
926 width
+= column
->width
;
929 wassertr(table
->delegate
&& table
->delegate
->numberOfRows
);
931 table
->rows
= table
->delegate
->numberOfRows(table
->delegate
, table
);
933 W_ResizeView(table
->tableView
, width
+1,
934 table
->rows
* table
->rowHeight
+ 1);
936 table
->tableWidth
= width
+ 1;