various table widget updates
[wmaker-crm.git] / WINGs / Extras / tabledelegates.c
blob574fdb1a2ecc70cc13eb9f651be9da7a2d32be83
3 #include <WINGs.h>
5 #include "wtableview.h"
8 typedef struct {
9 WMTableView *table;
10 WMFont *font;
11 GC gc;
12 GC selGc;
13 GC selTextGc;
14 } StringData;
17 typedef struct {
18 WMTableView *table;
19 GC selGc;
20 } PixmapData;
24 typedef struct {
25 WMTextField *widget;
26 WMTableView *table;
27 WMFont *font;
28 GC gc;
29 GC selGc;
30 GC selTextGc;
31 } StringEditorData;
34 typedef struct {
35 WMPopUpButton *widget;
36 WMTableView *table;
37 WMFont *font;
38 char **options;
39 int count;
40 GC gc;
41 GC selGc;
42 GC selTextGc;
43 } EnumSelectorData;
46 typedef struct {
47 WMButton *widget;
48 WMTableView *table;
49 Bool state;
50 GC gc;
51 } BooleanSwitchData;
54 static char *SelectionColor = "#bbbbcc";
58 static void stringDraw(WMScreen *scr, Drawable d, GC gc,
59 GC sgc, GC stgc, WMFont *font, void *data,
60 WMRect rect, Bool selected)
62 int x, y;
63 XRectangle rects[1];
64 Display *dpy = WMScreenDisplay(scr);
66 x = rect.pos.x + 5;
67 y = rect.pos.y + (rect.size.height - WMFontHeight(font))/2;
69 rects[0].x = rect.pos.x+1;
70 rects[0].y = rect.pos.y+1;
71 rects[0].width = rect.size.width-1;
72 rects[0].height = rect.size.height-1;
73 XSetClipRectangles(dpy, gc, 0, 0, rects, 1, YXSorted);
75 if (!selected) {
76 XClearArea(dpy, d, rects[0].x, rects[0].y,
77 rects[0].width, rects[0].height,
78 False);
80 WMDrawString(scr, d, gc, font, x, y,
81 data, strlen(data));
82 } else {
83 XFillRectangles(dpy, d, sgc, rects, 1);
85 WMDrawString(scr, d, stgc, font, x, y,
86 data, strlen(data));
89 XSetClipMask(dpy, gc, None);
94 static void pixmapDraw(WMScreen *scr, Drawable d, GC gc,
95 WMPixmap *pixmap, WMRect rect, Bool selected)
97 int x, y;
98 XRectangle rects[1];
99 Display *dpy = WMScreenDisplay(scr);
100 WMSize size;
102 rects[0].x = rect.pos.x+1;
103 rects[0].y = rect.pos.y+1;
104 rects[0].width = rect.size.width-1;
105 rects[0].height = rect.size.height-1;
106 XSetClipRectangles(dpy, gc, 0, 0, rects, 1, YXSorted);
108 if (!selected) {
109 XClearArea(dpy, d, rects[0].x, rects[0].y,
110 rects[0].width, rects[0].height,
111 False);
113 if (pixmap) {
114 size = WMGetPixmapSize(pixmap);
115 x = rect.pos.x + (rect.size.width - size.width) / 2;
116 y = rect.pos.y + (rect.size.height - size.height) / 2;
118 WMDrawPixmap(pixmap, d, x, y);
120 } else {
121 XFillRectangles(dpy, d, gc, rects, 1);
123 if (pixmap) {
124 size = WMGetPixmapSize(pixmap);
125 x = rect.pos.x + (rect.size.width - size.width) / 2;
126 y = rect.pos.y + (rect.size.height - size.height) / 2;
128 WMDrawPixmap(pixmap, d, x, y);
132 XSetClipMask(dpy, gc, None);
136 /* ---------------------------------------------------------------------- */
140 static void SECellPainter(WMTableColumnDelegate *self,
141 WMTableColumn *column, int row)
143 StringEditorData *strdata = (StringEditorData*)self->data;
144 WMTableView *table = WMGetTableColumnTableView(column);
146 stringDraw(WMWidgetScreen(table),
147 WMViewXID(WMGetTableViewDocumentView(table)),
148 strdata->gc, strdata->selGc, strdata->selTextGc, strdata->font,
149 WMTableViewDataForCell(table, column, row),
150 WMTableViewRectForCell(table, column, row),
151 False);
155 static void selectedSECellPainter(WMTableColumnDelegate *self,
156 WMTableColumn *column, int row)
158 StringEditorData *strdata = (StringEditorData*)self->data;
159 WMTableView *table = WMGetTableColumnTableView(column);
161 stringDraw(WMWidgetScreen(table),
162 WMViewXID(WMGetTableViewDocumentView(table)),
163 strdata->gc, strdata->selGc, strdata->selTextGc, strdata->font,
164 WMTableViewDataForCell(table, column, row),
165 WMTableViewRectForCell(table, column, row),
166 True);
170 static void beginSECellEdit(WMTableColumnDelegate *self,
171 WMTableColumn *column, int row)
173 StringEditorData *strdata = (StringEditorData*)self->data;
174 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
175 void *data = WMTableViewDataForCell(strdata->table, column, row);
177 WMSetTextFieldText(strdata->widget, (char*)data);
178 WMMoveWidget(strdata->widget, rect.pos.x, rect.pos.y);
179 WMResizeWidget(strdata->widget, rect.size.width+1, rect.size.height+1);
181 WMMapWidget(strdata->widget);
185 static void endSECellEdit(WMTableColumnDelegate *self,
186 WMTableColumn *column, int row)
188 StringEditorData *strdata = (StringEditorData*)self->data;
189 char *text;
191 WMUnmapWidget(strdata->widget);
193 text = WMGetTextFieldText(strdata->widget);
194 WMSetTableViewDataForCell(strdata->table, column, row, (void*)text);
198 WMTableColumnDelegate *WTCreateStringEditorDelegate(WMTableView *parent)
200 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
201 WMScreen *scr = WMWidgetScreen(parent);
202 StringEditorData *data = wmalloc(sizeof(StringEditorData));
204 data->widget = WMCreateTextField(parent);
205 W_ReparentView(WMWidgetView(data->widget),
206 WMGetTableViewDocumentView(parent),
207 0, 0);
208 data->table = parent;
209 data->font = WMSystemFontOfSize(scr, 12);
210 data->selGc = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
211 data->selTextGc = WMColorGC(WMBlackColor(scr));
212 data->gc = WMColorGC(WMBlackColor(scr));
214 delegate->data = data;
215 delegate->drawCell = SECellPainter;
216 delegate->drawSelectedCell = selectedSECellPainter;
217 delegate->beginCellEdit = beginSECellEdit;
218 delegate->endCellEdit = endSECellEdit;
220 return delegate;
225 /* ---------------------------------------------------------------------- */
228 static void ESCellPainter(WMTableColumnDelegate *self,
229 WMTableColumn *column, int row)
231 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
232 WMTableView *table = WMGetTableColumnTableView(column);
233 int i = (int)WMTableViewDataForCell(table, column, row);
235 stringDraw(WMWidgetScreen(table),
236 WMViewXID(WMGetTableViewDocumentView(table)),
237 strdata->gc, strdata->selGc, strdata->selTextGc, strdata->font,
238 strdata->options[i],
239 WMTableViewRectForCell(table, column, row),
240 False);
244 static void selectedESCellPainter(WMTableColumnDelegate *self,
245 WMTableColumn *column, int row)
247 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
248 WMTableView *table = WMGetTableColumnTableView(column);
249 int i = (int)WMTableViewDataForCell(table, column, row);
251 stringDraw(WMWidgetScreen(table),
252 WMViewXID(WMGetTableViewDocumentView(table)),
253 strdata->gc, strdata->selGc, strdata->selTextGc, strdata->font,
254 strdata->options[i],
255 WMTableViewRectForCell(table, column, row),
256 True);
260 static void beginESCellEdit(WMTableColumnDelegate *self,
261 WMTableColumn *column, int row)
263 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
264 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
265 int data = (int)WMTableViewDataForCell(strdata->table, column, row);
267 wassertr(data < strdata->count);
269 WMSetPopUpButtonSelectedItem(strdata->widget, data);
271 WMMoveWidget(strdata->widget, rect.pos.x, rect.pos.y);
272 WMResizeWidget(strdata->widget, rect.size.width, rect.size.height+1);
274 WMMapWidget(strdata->widget);
278 static void endESCellEdit(WMTableColumnDelegate *self,
279 WMTableColumn *column, int row)
281 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
282 int option;
284 WMUnmapWidget(strdata->widget);
286 option = WMGetPopUpButtonSelectedItem(strdata->widget);
287 WMSetTableViewDataForCell(strdata->table, column, row, (void*)option);
291 WMTableColumnDelegate *WTCreateEnumSelectorDelegate(WMTableView *parent)
293 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
294 WMScreen *scr = WMWidgetScreen(parent);
295 EnumSelectorData *data = wmalloc(sizeof(EnumSelectorData));
297 data->widget = WMCreatePopUpButton(parent);
298 W_ReparentView(WMWidgetView(data->widget),
299 WMGetTableViewDocumentView(parent),
300 0, 0);
301 data->table = parent;
302 data->font = WMSystemFontOfSize(scr, 12);
303 data->selGc = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
304 data->selTextGc = WMColorGC(WMBlackColor(scr));
305 data->gc = WMColorGC(WMBlackColor(scr));
306 data->count = 0;
307 data->options = NULL;
309 delegate->data = data;
310 delegate->drawCell = ESCellPainter;
311 delegate->drawSelectedCell = selectedESCellPainter;
312 delegate->beginCellEdit = beginESCellEdit;
313 delegate->endCellEdit = endESCellEdit;
315 return delegate;
319 void WTSetEnumSelectorOptions(WMTableColumnDelegate *delegate,
320 char **options, int count)
322 EnumSelectorData *data = (EnumSelectorData*)delegate->data;
323 int i;
325 for (i = 0;
326 i < WMGetPopUpButtonNumberOfItems(data->widget);
327 i++) {
328 WMRemovePopUpButtonItem(data->widget, 0);
331 data->options = options;
332 data->count = count;
334 for (i = 0; i < count; i++) {
335 WMAddPopUpButtonItem(data->widget, options[i]);
341 /* ---------------------------------------------------------------------- */
343 static void BSCellPainter(WMTableColumnDelegate *self,
344 WMTableColumn *column, int row)
346 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
347 WMTableView *table = WMGetTableColumnTableView(column);
348 int i = (int)WMTableViewDataForCell(table, column, row);
349 WMScreen *scr = WMWidgetScreen(table);
351 if (i) {
352 pixmapDraw(scr, WMViewXID(WMGetTableViewDocumentView(table)),
353 strdata->gc, WMGetSystemPixmap(scr, WSICheckMark),
354 WMTableViewRectForCell(table, column, row), False);
355 } else {
356 pixmapDraw(scr, WMViewXID(WMGetTableViewDocumentView(table)),
357 strdata->gc, NULL,
358 WMTableViewRectForCell(table, column, row), False);
363 static void selectedBSCellPainter(WMTableColumnDelegate *self,
364 WMTableColumn *column, int row)
366 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
367 WMTableView *table = WMGetTableColumnTableView(column);
368 int i = (int)WMTableViewDataForCell(table, column, row);
369 WMScreen *scr = WMWidgetScreen(table);
371 if (i) {
372 pixmapDraw(scr, WMViewXID(WMGetTableViewDocumentView(table)),
373 strdata->gc, WMGetSystemPixmap(scr, WSICheckMark),
374 WMTableViewRectForCell(table, column, row), True);
375 } else {
376 pixmapDraw(scr, WMViewXID(WMGetTableViewDocumentView(table)),
377 strdata->gc, NULL,
378 WMTableViewRectForCell(table, column, row), True);
384 static void beginBSCellEdit(WMTableColumnDelegate *self,
385 WMTableColumn *column, int row)
387 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
388 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
389 int data = (int)WMTableViewDataForCell(strdata->table, column, row);
391 WMSetButtonSelected(strdata->widget, data);
392 WMMoveWidget(strdata->widget, rect.pos.x+1, rect.pos.y+1);
393 WMResizeWidget(strdata->widget, rect.size.width-1, rect.size.height-1);
395 WMMapWidget(strdata->widget);
399 static void endBSCellEdit(WMTableColumnDelegate *self,
400 WMTableColumn *column, int row)
402 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
403 int value;
405 value = WMGetButtonSelected(strdata->widget);
406 WMSetTableViewDataForCell(strdata->table, column, row, (void*)value);
407 WMUnmapWidget(strdata->widget);
411 WMTableColumnDelegate *WTCreateBooleanSwitchDelegate(WMTableView *parent)
413 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
414 WMScreen *scr = WMWidgetScreen(parent);
415 BooleanSwitchData *data = wmalloc(sizeof(BooleanSwitchData));
416 WMColor *color;
418 data->widget = WMCreateSwitchButton(parent);
419 W_ReparentView(WMWidgetView(data->widget),
420 WMGetTableViewDocumentView(parent),
421 0, 0);
422 WMSetButtonText(data->widget, NULL);
423 WMSetButtonImagePosition(data->widget, WIPImageOnly);
424 WMSetButtonImage(data->widget, NULL);
425 WMSetButtonAltImage(data->widget, WMGetSystemPixmap(scr, WSICheckMark));
427 data->table = parent;
428 color = WMCreateNamedColor(scr, SelectionColor, False);
429 WMSetWidgetBackgroundColor(data->widget, color);
430 data->gc = WMColorGC(color);
432 delegate->data = data;
433 delegate->drawCell = BSCellPainter;
434 delegate->drawSelectedCell = selectedBSCellPainter;
435 delegate->beginCellEdit = beginBSCellEdit;
436 delegate->endCellEdit = endBSCellEdit;
438 return delegate;
442 /* ---------------------------------------------------------------------- */
445 static void SCellPainter(WMTableColumnDelegate *self,
446 WMTableColumn *column, int row)
448 StringData *strdata = (StringData*)self->data;
449 WMTableView *table = WMGetTableColumnTableView(column);
451 stringDraw(WMWidgetScreen(table),
452 WMViewXID(WMGetTableViewDocumentView(table)),
453 strdata->gc, strdata->selGc, strdata->selTextGc, strdata->font,
454 WMTableViewDataForCell(table, column, row),
455 WMTableViewRectForCell(table, column, row),
456 False);
460 static void selectedSCellPainter(WMTableColumnDelegate *self,
461 WMTableColumn *column, int row)
463 StringData *strdata = (StringData*)self->data;
464 WMTableView *table = WMGetTableColumnTableView(column);
466 stringDraw(WMWidgetScreen(table),
467 WMViewXID(WMGetTableViewDocumentView(table)),
468 strdata->gc, strdata->selGc, strdata->selTextGc, strdata->font,
469 WMTableViewDataForCell(table, column, row),
470 WMTableViewRectForCell(table, column, row),
471 True);
475 WMTableColumnDelegate *WTCreateStringDelegate(WMTableView *parent)
477 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
478 WMScreen *scr = WMWidgetScreen(parent);
479 StringData *data = wmalloc(sizeof(StringData));
481 data->table = parent;
482 data->font = WMSystemFontOfSize(scr, 12);
483 data->selGc = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
484 data->selTextGc = WMColorGC(WMBlackColor(scr));
485 data->gc = WMColorGC(WMBlackColor(scr));
487 delegate->data = data;
488 delegate->drawCell = SCellPainter;
489 delegate->drawSelectedCell = selectedSCellPainter;
490 delegate->beginCellEdit = NULL;
491 delegate->endCellEdit = NULL;
493 return delegate;
497 /* ---------------------------------------------------------------------- */
500 static void PCellPainter(WMTableColumnDelegate *self,
501 WMTableColumn *column, int row)
503 StringData *strdata = (StringData*)self->data;
504 WMTableView *table = WMGetTableColumnTableView(column);
506 pixmapDraw(WMWidgetScreen(table),
507 WMViewXID(WMGetTableViewDocumentView(table)),
508 strdata->selGc,
509 (WMPixmap*)WMTableViewDataForCell(table, column, row),
510 WMTableViewRectForCell(table, column, row),
511 False);
515 static void selectedPCellPainter(WMTableColumnDelegate *self,
516 WMTableColumn *column, int row)
518 StringData *strdata = (StringData*)self->data;
519 WMTableView *table = WMGetTableColumnTableView(column);
521 pixmapDraw(WMWidgetScreen(table),
522 WMViewXID(WMGetTableViewDocumentView(table)),
523 strdata->selGc,
524 (WMPixmap*)WMTableViewDataForCell(table, column, row),
525 WMTableViewRectForCell(table, column, row),
526 True);
530 WMTableColumnDelegate *WTCreatePixmapDelegate(WMTableView *table)
532 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
533 WMScreen *scr = WMWidgetScreen(table);
534 StringData *data = wmalloc(sizeof(StringData));
536 data->table = table;
537 data->selGc = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
539 delegate->data = data;
540 delegate->drawCell = PCellPainter;
541 delegate->drawSelectedCell = selectedPCellPainter;
542 delegate->beginCellEdit = NULL;
543 delegate->endCellEdit = NULL;
545 return delegate;
549 /* ---------------------------------------------------------------------- */
552 static void drawPSCell(WMTableColumnDelegate *self,
553 WMTableColumn *column, int row, Bool selected)
555 StringData *strdata = (StringData*)self->data;
556 WMTableView *table = WMGetTableColumnTableView(column);
557 void **data;
558 WMPixmap *pix;
559 char *str;
560 WMRect rect;
561 WMSize size;
563 data = WMTableViewDataForCell(table, column, row);
565 str = (char*)data[0];
566 pix = (WMPixmap*)data[1];
568 rect = WMTableViewRectForCell(table, column, row);
570 if (pix) {
571 int owidth = rect.size.width;
573 size = WMGetPixmapSize(pix);
574 rect.size.width = size.width;
576 pixmapDraw(WMWidgetScreen(table),
577 WMViewXID(WMGetTableViewDocumentView(table)),
578 strdata->selGc, pix, rect,
579 selected);
581 rect.pos.x += size.width-1;
582 rect.size.width = owidth-size.width+1;
585 stringDraw(WMWidgetScreen(table),
586 WMViewXID(WMGetTableViewDocumentView(table)),
587 strdata->gc, strdata->selGc, strdata->selTextGc, strdata->font,
588 str, rect, selected);
592 static void PSCellPainter(WMTableColumnDelegate *self,
593 WMTableColumn *column, int row)
595 drawPSCell(self, column, row, False);
599 static void selectedPSCellPainter(WMTableColumnDelegate *self,
600 WMTableColumn *column, int row)
602 drawPSCell(self, column, row, True);
606 WMTableColumnDelegate *WTCreatePixmapStringDelegate(WMTableView *parent)
608 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
609 WMScreen *scr = WMWidgetScreen(parent);
610 StringData *data = wmalloc(sizeof(StringData));
612 data->table = parent;
613 data->font = WMSystemFontOfSize(scr, 12);
614 data->selGc = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
615 data->selTextGc = WMColorGC(WMBlackColor(scr));
616 data->gc = WMColorGC(WMBlackColor(scr));
618 delegate->data = data;
619 delegate->drawCell = PSCellPainter;
620 delegate->drawSelectedCell = selectedPSCellPainter;
621 delegate->beginCellEdit = NULL;
622 delegate->endCellEdit = NULL;
624 return delegate;