- Finished moving to the new proplist handling code in WINGs.
[wmaker-crm.git] / WINGs / Extras / wtabledelegates.c
blob52d1073fb71f2f0e7e97afa672c1a48da02b5cad
3 #include <WINGs/WINGsP.h>
5 #include "wtableview.h"
7 #include "wtabledelegates.h"
9 typedef struct {
10 WMTableView *table;
11 WMFont *font;
12 GC gc;
13 GC selGC;
14 GC textGC;
15 } StringData;
18 typedef struct {
19 WMTableView *table;
20 GC selGc;
21 } PixmapData;
25 typedef struct {
26 WMTextField *widget;
27 WMTableView *table;
28 WMFont *font;
29 GC gc;
30 GC selGC;
31 GC textGC;
32 } StringEditorData;
35 typedef struct {
36 WMPopUpButton *widget;
37 WMTableView *table;
38 WMFont *font;
39 char **options;
40 int count;
41 GC gc;
42 GC selGC;
43 GC textGC;
44 } EnumSelectorData;
47 typedef struct {
48 WMButton *widget;
49 WMTableView *table;
50 Bool state;
51 GC gc;
52 GC selGC;
53 } BooleanSwitchData;
56 static char *SelectionColor = "#bbbbcc";
60 static void stringDraw(WMScreen *scr, Drawable d, GC gc,
61 GC sgc, GC stgc, WMFont *font, void *data,
62 WMRect rect, Bool selected)
64 int x, y;
65 XRectangle rects[1];
66 Display *dpy = WMScreenDisplay(scr);
68 x = rect.pos.x + 5;
69 y = rect.pos.y + (rect.size.height - WMFontHeight(font))/2;
71 rects[0].x = rect.pos.x+1;
72 rects[0].y = rect.pos.y+1;
73 rects[0].width = rect.size.width-1;
74 rects[0].height = rect.size.height-1;
75 XSetClipRectangles(dpy, gc, 0, 0, rects, 1, YXSorted);
77 if (!selected) {
78 XFillRectangles(dpy, d, gc, rects, 1);
80 WMDrawString(scr, d, stgc, 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, GC sgc,
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 XFillRectangles(dpy, d, gc, rects, 1);
111 if (pixmap) {
112 size = WMGetPixmapSize(pixmap);
113 x = rect.pos.x + (rect.size.width - size.width) / 2;
114 y = rect.pos.y + (rect.size.height - size.height) / 2;
116 WMDrawPixmap(pixmap, d, x, y);
118 } else {
119 XFillRectangles(dpy, d, sgc, rects, 1);
121 if (pixmap) {
122 size = WMGetPixmapSize(pixmap);
123 x = rect.pos.x + (rect.size.width - size.width) / 2;
124 y = rect.pos.y + (rect.size.height - size.height) / 2;
126 WMDrawPixmap(pixmap, d, x, y);
130 XSetClipMask(dpy, gc, None);
134 /* ---------------------------------------------------------------------- */
138 static void SECellPainter(WMTableColumnDelegate *self,
139 WMTableColumn *column, int row, Drawable d)
141 StringEditorData *strdata = (StringEditorData*)self->data;
142 WMTableView *table = WMGetTableColumnTableView(column);
144 stringDraw(WMWidgetScreen(table), d,
145 strdata->gc, strdata->selGC, strdata->textGC, strdata->font,
146 WMTableViewDataForCell(table, column, row),
147 WMTableViewRectForCell(table, column, row),
148 False);
152 static void selectedSECellPainter(WMTableColumnDelegate *self,
153 WMTableColumn *column, int row, Drawable d)
155 StringEditorData *strdata = (StringEditorData*)self->data;
156 WMTableView *table = WMGetTableColumnTableView(column);
158 stringDraw(WMWidgetScreen(table), d,
159 strdata->gc, strdata->selGC, strdata->textGC, strdata->font,
160 WMTableViewDataForCell(table, column, row),
161 WMTableViewRectForCell(table, column, row),
162 True);
166 static void beginSECellEdit(WMTableColumnDelegate *self,
167 WMTableColumn *column, int row)
169 StringEditorData *strdata = (StringEditorData*)self->data;
170 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
171 void *data = WMTableViewDataForCell(strdata->table, column, row);
173 WMSetTextFieldText(strdata->widget, (char*)data);
174 WMMoveWidget(strdata->widget, rect.pos.x, rect.pos.y);
175 WMResizeWidget(strdata->widget, rect.size.width+1, rect.size.height+1);
177 WMMapWidget(strdata->widget);
181 static void endSECellEdit(WMTableColumnDelegate *self,
182 WMTableColumn *column, int row)
184 StringEditorData *strdata = (StringEditorData*)self->data;
185 char *text;
187 WMUnmapWidget(strdata->widget);
189 text = WMGetTextFieldText(strdata->widget);
190 WMSetTableViewDataForCell(strdata->table, column, row, (void*)text);
194 WMTableColumnDelegate *WTCreateStringEditorDelegate(WMTableView *parent)
196 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
197 WMScreen *scr = WMWidgetScreen(parent);
198 StringEditorData *data = wmalloc(sizeof(StringEditorData));
200 data->widget = WMCreateTextField(parent);
201 W_ReparentView(WMWidgetView(data->widget),
202 WMGetTableViewDocumentView(parent),
203 0, 0);
204 data->table = parent;
205 data->font = WMSystemFontOfSize(scr, 12);
206 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
207 data->textGC = WMColorGC(WMBlackColor(scr));
208 data->gc = WMColorGC(WMWhiteColor(scr));
210 delegate->data = data;
211 delegate->drawCell = SECellPainter;
212 delegate->drawSelectedCell = selectedSECellPainter;
213 delegate->beginCellEdit = beginSECellEdit;
214 delegate->endCellEdit = endSECellEdit;
216 return delegate;
221 /* ---------------------------------------------------------------------- */
224 static void ESCellPainter(WMTableColumnDelegate *self,
225 WMTableColumn *column, int row, Drawable d)
227 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
228 WMTableView *table = WMGetTableColumnTableView(column);
229 int i = (int)WMTableViewDataForCell(table, column, row);
231 stringDraw(WMWidgetScreen(table), d,
232 strdata->gc, strdata->selGC, strdata->textGC, strdata->font,
233 strdata->options[i],
234 WMTableViewRectForCell(table, column, row),
235 False);
239 static void selectedESCellPainter(WMTableColumnDelegate *self,
240 WMTableColumn *column, int row, Drawable d)
242 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
243 WMTableView *table = WMGetTableColumnTableView(column);
244 int i = (int)WMTableViewDataForCell(table, column, row);
246 stringDraw(WMWidgetScreen(table), d,
247 strdata->gc, strdata->selGC, strdata->textGC, strdata->font,
248 strdata->options[i],
249 WMTableViewRectForCell(table, column, row),
250 True);
254 static void beginESCellEdit(WMTableColumnDelegate *self,
255 WMTableColumn *column, int row)
257 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
258 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
259 int data = (int)WMTableViewDataForCell(strdata->table, column, row);
261 wassertr(data < strdata->count);
263 WMSetPopUpButtonSelectedItem(strdata->widget, data);
265 WMMoveWidget(strdata->widget, rect.pos.x, rect.pos.y);
266 WMResizeWidget(strdata->widget, rect.size.width, rect.size.height+1);
268 WMMapWidget(strdata->widget);
272 static void endESCellEdit(WMTableColumnDelegate *self,
273 WMTableColumn *column, int row)
275 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
276 int option;
278 WMUnmapWidget(strdata->widget);
280 option = WMGetPopUpButtonSelectedItem(strdata->widget);
281 WMSetTableViewDataForCell(strdata->table, column, row, (void*)option);
285 WMTableColumnDelegate *WTCreateEnumSelectorDelegate(WMTableView *parent)
287 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
288 WMScreen *scr = WMWidgetScreen(parent);
289 EnumSelectorData *data = wmalloc(sizeof(EnumSelectorData));
291 data->widget = WMCreatePopUpButton(parent);
292 W_ReparentView(WMWidgetView(data->widget),
293 WMGetTableViewDocumentView(parent),
294 0, 0);
295 data->table = parent;
296 data->font = WMSystemFontOfSize(scr, 12);
297 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
298 data->textGC = WMColorGC(WMBlackColor(scr));
299 data->gc = WMColorGC(WMWhiteColor(scr));
300 data->count = 0;
301 data->options = NULL;
303 delegate->data = data;
304 delegate->drawCell = ESCellPainter;
305 delegate->drawSelectedCell = selectedESCellPainter;
306 delegate->beginCellEdit = beginESCellEdit;
307 delegate->endCellEdit = endESCellEdit;
309 return delegate;
313 void WTSetEnumSelectorOptions(WMTableColumnDelegate *delegate,
314 char **options, int count)
316 EnumSelectorData *data = (EnumSelectorData*)delegate->data;
317 int i;
319 for (i = 0;
320 i < WMGetPopUpButtonNumberOfItems(data->widget);
321 i++) {
322 WMRemovePopUpButtonItem(data->widget, 0);
325 data->options = options;
326 data->count = count;
328 for (i = 0; i < count; i++) {
329 WMAddPopUpButtonItem(data->widget, options[i]);
335 /* ---------------------------------------------------------------------- */
337 static void BSCellPainter(WMTableColumnDelegate *self,
338 WMTableColumn *column, int row, Drawable d)
340 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
341 WMTableView *table = WMGetTableColumnTableView(column);
342 int i = (int)WMTableViewDataForCell(table, column, row);
343 WMScreen *scr = WMWidgetScreen(table);
345 if (i) {
346 pixmapDraw(scr, d,
347 strdata->gc, strdata->selGC, WMGetSystemPixmap(scr, WSICheckMark),
348 WMTableViewRectForCell(table, column, row), False);
349 } else {
350 pixmapDraw(scr, d,
351 strdata->gc, strdata->selGC, NULL,
352 WMTableViewRectForCell(table, column, row), False);
357 static void selectedBSCellPainter(WMTableColumnDelegate *self,
358 WMTableColumn *column, int row, Drawable d)
360 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
361 WMTableView *table = WMGetTableColumnTableView(column);
362 int i = (int)WMTableViewDataForCell(table, column, row);
363 WMScreen *scr = WMWidgetScreen(table);
365 if (i) {
366 pixmapDraw(scr, d,
367 strdata->gc, strdata->selGC, WMGetSystemPixmap(scr, WSICheckMark),
368 WMTableViewRectForCell(table, column, row), True);
369 } else {
370 pixmapDraw(scr, d,
371 strdata->gc, strdata->selGC, NULL,
372 WMTableViewRectForCell(table, column, row), True);
378 static void beginBSCellEdit(WMTableColumnDelegate *self,
379 WMTableColumn *column, int row)
381 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
382 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
383 int data = (int)WMTableViewDataForCell(strdata->table, column, row);
385 WMSetButtonSelected(strdata->widget, data);
386 WMMoveWidget(strdata->widget, rect.pos.x+1, rect.pos.y+1);
387 WMResizeWidget(strdata->widget, rect.size.width-1, rect.size.height-1);
389 WMMapWidget(strdata->widget);
393 static void endBSCellEdit(WMTableColumnDelegate *self,
394 WMTableColumn *column, int row)
396 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
397 int value;
399 value = WMGetButtonSelected(strdata->widget);
400 WMSetTableViewDataForCell(strdata->table, column, row, (void*)value);
401 WMUnmapWidget(strdata->widget);
405 WMTableColumnDelegate *WTCreateBooleanSwitchDelegate(WMTableView *parent)
407 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
408 WMScreen *scr = WMWidgetScreen(parent);
409 BooleanSwitchData *data = wmalloc(sizeof(BooleanSwitchData));
410 WMColor *color;
412 data->widget = WMCreateSwitchButton(parent);
413 W_ReparentView(WMWidgetView(data->widget),
414 WMGetTableViewDocumentView(parent),
415 0, 0);
416 WMSetButtonText(data->widget, NULL);
417 WMSetButtonImagePosition(data->widget, WIPImageOnly);
418 WMSetButtonImage(data->widget, NULL);
419 WMSetButtonAltImage(data->widget, WMGetSystemPixmap(scr, WSICheckMark));
421 data->table = parent;
422 color = WMCreateNamedColor(scr, SelectionColor, False);
423 WMSetWidgetBackgroundColor(data->widget, color);
424 data->gc = WMColorGC(WMWhiteColor(scr));
425 data->selGC = WMColorGC(color);
427 delegate->data = data;
428 delegate->drawCell = BSCellPainter;
429 delegate->drawSelectedCell = selectedBSCellPainter;
430 delegate->beginCellEdit = beginBSCellEdit;
431 delegate->endCellEdit = endBSCellEdit;
433 return delegate;
437 /* ---------------------------------------------------------------------- */
440 static void SCellPainter(WMTableColumnDelegate *self,
441 WMTableColumn *column, int row, Drawable d)
443 StringData *strdata = (StringData*)self->data;
444 WMTableView *table = WMGetTableColumnTableView(column);
446 stringDraw(WMWidgetScreen(table), d,
447 strdata->gc, strdata->selGC, strdata->textGC, strdata->font,
448 WMTableViewDataForCell(table, column, row),
449 WMTableViewRectForCell(table, column, row),
450 False);
454 static void selectedSCellPainter(WMTableColumnDelegate *self,
455 WMTableColumn *column, int row, Drawable d)
457 StringData *strdata = (StringData*)self->data;
458 WMTableView *table = WMGetTableColumnTableView(column);
460 stringDraw(WMWidgetScreen(table), d,
461 strdata->gc, strdata->selGC, strdata->textGC, strdata->font,
462 WMTableViewDataForCell(table, column, row),
463 WMTableViewRectForCell(table, column, row),
464 True);
468 WMTableColumnDelegate *WTCreateStringDelegate(WMTableView *parent)
470 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
471 WMScreen *scr = WMWidgetScreen(parent);
472 StringData *data = wmalloc(sizeof(StringData));
474 data->table = parent;
475 data->font = WMSystemFontOfSize(scr, 12);
476 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
477 data->textGC = WMColorGC(WMBlackColor(scr));
478 data->gc = WMColorGC(WMWhiteColor(scr));
480 delegate->data = data;
481 delegate->drawCell = SCellPainter;
482 delegate->drawSelectedCell = selectedSCellPainter;
483 delegate->beginCellEdit = NULL;
484 delegate->endCellEdit = NULL;
486 return delegate;
490 /* ---------------------------------------------------------------------- */
493 static void PCellPainter(WMTableColumnDelegate *self,
494 WMTableColumn *column, int row, Drawable d)
496 StringData *strdata = (StringData*)self->data;
497 WMTableView *table = WMGetTableColumnTableView(column);
499 pixmapDraw(WMWidgetScreen(table), d,
500 strdata->gc, strdata->selGC,
501 (WMPixmap*)WMTableViewDataForCell(table, column, row),
502 WMTableViewRectForCell(table, column, row),
503 False);
507 static void selectedPCellPainter(WMTableColumnDelegate *self,
508 WMTableColumn *column, int row, Drawable d)
510 StringData *strdata = (StringData*)self->data;
511 WMTableView *table = WMGetTableColumnTableView(column);
513 pixmapDraw(WMWidgetScreen(table), d,
514 strdata->gc, strdata->selGC,
515 (WMPixmap*)WMTableViewDataForCell(table, column, row),
516 WMTableViewRectForCell(table, column, row),
517 True);
521 WMTableColumnDelegate *WTCreatePixmapDelegate(WMTableView *table)
523 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
524 WMScreen *scr = WMWidgetScreen(table);
525 StringData *data = wmalloc(sizeof(StringData));
527 data->table = table;
528 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
529 data->gc = WMColorGC(WMWhiteColor(scr));
531 delegate->data = data;
532 delegate->drawCell = PCellPainter;
533 delegate->drawSelectedCell = selectedPCellPainter;
534 delegate->beginCellEdit = NULL;
535 delegate->endCellEdit = NULL;
537 return delegate;
541 /* ---------------------------------------------------------------------- */
544 static void drawPSCell(WMTableColumnDelegate *self, Drawable d,
545 WMTableColumn *column, int row, Bool selected)
547 StringData *strdata = (StringData*)self->data;
548 WMTableView *table = WMGetTableColumnTableView(column);
549 void **data;
550 WMPixmap *pix;
551 char *str;
552 WMRect rect;
553 WMSize size;
555 data = WMTableViewDataForCell(table, column, row);
557 str = (char*)data[0];
558 pix = (WMPixmap*)data[1];
560 rect = WMTableViewRectForCell(table, column, row);
562 if (pix) {
563 int owidth = rect.size.width;
565 size = WMGetPixmapSize(pix);
566 rect.size.width = size.width;
568 pixmapDraw(WMWidgetScreen(table),
569 WMViewXID(WMGetTableViewDocumentView(table)),
570 strdata->gc, strdata->selGC, pix, rect,
571 selected);
573 rect.pos.x += size.width-1;
574 rect.size.width = owidth-size.width+1;
577 stringDraw(WMWidgetScreen(table), d,
578 strdata->gc, strdata->selGC, strdata->textGC, strdata->font,
579 str, rect, selected);
583 static void PSCellPainter(WMTableColumnDelegate *self,
584 WMTableColumn *column, int row, Drawable d)
586 drawPSCell(self, d, column, row, False);
590 static void selectedPSCellPainter(WMTableColumnDelegate *self,
591 WMTableColumn *column, int row, Drawable d)
593 drawPSCell(self, d, column, row, True);
597 WMTableColumnDelegate *WTCreatePixmapStringDelegate(WMTableView *parent)
599 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
600 WMScreen *scr = WMWidgetScreen(parent);
601 StringData *data = wmalloc(sizeof(StringData));
603 data->table = parent;
604 data->font = WMSystemFontOfSize(scr, 12);
605 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
606 data->textGC = WMColorGC(WMBlackColor(scr));
607 data->gc = WMColorGC(WMWhiteColor(scr));
609 delegate->data = data;
610 delegate->drawCell = PSCellPainter;
611 delegate->drawSelectedCell = selectedPSCellPainter;
612 delegate->beginCellEdit = NULL;
613 delegate->endCellEdit = NULL;
615 return delegate;