changed indentation to use spaces only
[wmaker-crm.git] / WINGs / Extras / wtabledelegates.c
blob9c46e7dc193fbbffbac73a51facd5d29f2a668d5
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 WMColor *textColor;
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 WMColor *textColor;
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 WMColor *textColor;
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
61 stringDraw(WMScreen *scr, Drawable d, GC gc, GC sgc, WMColor *textColor,
62 WMFont *font, void *data, 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, textColor, font, x, y,
81 data, strlen(data));
82 } else {
83 XFillRectangles(dpy, d, sgc, rects, 1);
85 WMDrawString(scr, d, textColor, font, x, y,
86 data, strlen(data));
89 XSetClipMask(dpy, gc, None);
94 static void
95 pixmapDraw(WMScreen *scr, Drawable d, GC gc, GC sgc, WMPixmap *pixmap,
96 WMRect rect, Bool selected)
98 int x, y;
99 XRectangle rects[1];
100 Display *dpy = WMScreenDisplay(scr);
101 WMSize size;
103 rects[0].x = rect.pos.x+1;
104 rects[0].y = rect.pos.y+1;
105 rects[0].width = rect.size.width-1;
106 rects[0].height = rect.size.height-1;
107 XSetClipRectangles(dpy, gc, 0, 0, rects, 1, YXSorted);
109 if (!selected) {
110 XFillRectangles(dpy, d, gc, rects, 1);
112 if (pixmap) {
113 size = WMGetPixmapSize(pixmap);
114 x = rect.pos.x + (rect.size.width - size.width) / 2;
115 y = rect.pos.y + (rect.size.height - size.height) / 2;
117 WMDrawPixmap(pixmap, d, x, y);
119 } else {
120 XFillRectangles(dpy, d, sgc, rects, 1);
122 if (pixmap) {
123 size = WMGetPixmapSize(pixmap);
124 x = rect.pos.x + (rect.size.width - size.width) / 2;
125 y = rect.pos.y + (rect.size.height - size.height) / 2;
127 WMDrawPixmap(pixmap, d, x, y);
131 XSetClipMask(dpy, gc, None);
135 /* ---------------------------------------------------------------------- */
139 static void
140 SECellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
141 int row, Drawable d)
143 StringEditorData *strdata = (StringEditorData*)self->data;
144 WMTableView *table = WMGetTableColumnTableView(column);
146 stringDraw(WMWidgetScreen(table), d,
147 strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
148 WMTableViewDataForCell(table, column, row),
149 WMTableViewRectForCell(table, column, row),
150 False);
154 static void
155 selectedSECellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
156 int row, Drawable d)
158 StringEditorData *strdata = (StringEditorData*)self->data;
159 WMTableView *table = WMGetTableColumnTableView(column);
161 stringDraw(WMWidgetScreen(table), d,
162 strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
163 WMTableViewDataForCell(table, column, row),
164 WMTableViewRectForCell(table, column, row),
165 True);
169 static void
170 beginSECellEdit(WMTableColumnDelegate *self, WMTableColumn *column, int row)
172 StringEditorData *strdata = (StringEditorData*)self->data;
173 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
174 void *data = WMTableViewDataForCell(strdata->table, column, row);
176 WMSetTextFieldText(strdata->widget, (char*)data);
177 WMMoveWidget(strdata->widget, rect.pos.x, rect.pos.y);
178 WMResizeWidget(strdata->widget, rect.size.width+1, rect.size.height+1);
180 WMMapWidget(strdata->widget);
184 static void
185 endSECellEdit(WMTableColumnDelegate *self, WMTableColumn *column, int row)
187 StringEditorData *strdata = (StringEditorData*)self->data;
188 char *text;
190 WMUnmapWidget(strdata->widget);
192 text = WMGetTextFieldText(strdata->widget);
193 WMSetTableViewDataForCell(strdata->table, column, row, (void*)text);
197 WMTableColumnDelegate*
198 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->gc = WMColorGC(WMWhiteColor(scr));
211 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
212 data->textColor = 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
229 ESCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
230 int row, Drawable d)
232 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
233 WMTableView *table = WMGetTableColumnTableView(column);
234 int i = (int)WMTableViewDataForCell(table, column, row);
236 stringDraw(WMWidgetScreen(table), d,
237 strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
238 strdata->options[i],
239 WMTableViewRectForCell(table, column, row),
240 False);
244 static void
245 selectedESCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
246 int row, Drawable d)
248 EnumSelectorData *strdata = (EnumSelectorData*)self->data;
249 WMTableView *table = WMGetTableColumnTableView(column);
250 int i = (int)WMTableViewDataForCell(table, column, row);
252 stringDraw(WMWidgetScreen(table), d,
253 strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
254 strdata->options[i],
255 WMTableViewRectForCell(table, column, row),
256 True);
260 static void
261 beginESCellEdit(WMTableColumnDelegate *self, 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
279 endESCellEdit(WMTableColumnDelegate *self, 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*
292 WTCreateEnumSelectorDelegate(WMTableView *parent)
294 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
295 WMScreen *scr = WMWidgetScreen(parent);
296 EnumSelectorData *data = wmalloc(sizeof(EnumSelectorData));
298 data->widget = WMCreatePopUpButton(parent);
299 W_ReparentView(WMWidgetView(data->widget),
300 WMGetTableViewDocumentView(parent),
301 0, 0);
302 data->table = parent;
303 data->font = WMSystemFontOfSize(scr, 12);
304 data->gc = WMColorGC(WMWhiteColor(scr));
305 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
306 data->textColor = WMBlackColor(scr);
307 data->count = 0;
308 data->options = NULL;
310 delegate->data = data;
311 delegate->drawCell = ESCellPainter;
312 delegate->drawSelectedCell = selectedESCellPainter;
313 delegate->beginCellEdit = beginESCellEdit;
314 delegate->endCellEdit = endESCellEdit;
316 return delegate;
320 void
321 WTSetEnumSelectorOptions(WMTableColumnDelegate *delegate, char **options,
322 int count)
324 EnumSelectorData *data = (EnumSelectorData*)delegate->data;
325 int i;
327 for (i = 0;
328 i < WMGetPopUpButtonNumberOfItems(data->widget);
329 i++) {
330 WMRemovePopUpButtonItem(data->widget, 0);
333 data->options = options;
334 data->count = count;
336 for (i = 0; i < count; i++) {
337 WMAddPopUpButtonItem(data->widget, options[i]);
343 /* ---------------------------------------------------------------------- */
345 static void
346 BSCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
347 int row, Drawable d)
349 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
350 WMTableView *table = WMGetTableColumnTableView(column);
351 int i = (int)WMTableViewDataForCell(table, column, row);
352 WMScreen *scr = WMWidgetScreen(table);
354 if (i) {
355 pixmapDraw(scr, d,
356 strdata->gc, strdata->selGC, WMGetSystemPixmap(scr, WSICheckMark),
357 WMTableViewRectForCell(table, column, row), False);
358 } else {
359 pixmapDraw(scr, d,
360 strdata->gc, strdata->selGC, NULL,
361 WMTableViewRectForCell(table, column, row), False);
366 static void
367 selectedBSCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
368 int row, Drawable d)
370 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
371 WMTableView *table = WMGetTableColumnTableView(column);
372 int i = (int)WMTableViewDataForCell(table, column, row);
373 WMScreen *scr = WMWidgetScreen(table);
375 if (i) {
376 pixmapDraw(scr, d,
377 strdata->gc, strdata->selGC, WMGetSystemPixmap(scr, WSICheckMark),
378 WMTableViewRectForCell(table, column, row), True);
379 } else {
380 pixmapDraw(scr, d,
381 strdata->gc, strdata->selGC, NULL,
382 WMTableViewRectForCell(table, column, row), True);
388 static void
389 beginBSCellEdit(WMTableColumnDelegate *self, WMTableColumn *column, int row)
391 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
392 WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
393 int data = (int)WMTableViewDataForCell(strdata->table, column, row);
395 WMSetButtonSelected(strdata->widget, data);
396 WMMoveWidget(strdata->widget, rect.pos.x+1, rect.pos.y+1);
397 WMResizeWidget(strdata->widget, rect.size.width-1, rect.size.height-1);
399 WMMapWidget(strdata->widget);
403 static void
404 endBSCellEdit(WMTableColumnDelegate *self, WMTableColumn *column, int row)
406 BooleanSwitchData *strdata = (BooleanSwitchData*)self->data;
407 int value;
409 value = WMGetButtonSelected(strdata->widget);
410 WMSetTableViewDataForCell(strdata->table, column, row, (void*)value);
411 WMUnmapWidget(strdata->widget);
415 WMTableColumnDelegate*
416 WTCreateBooleanSwitchDelegate(WMTableView *parent)
418 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
419 WMScreen *scr = WMWidgetScreen(parent);
420 BooleanSwitchData *data = wmalloc(sizeof(BooleanSwitchData));
421 WMColor *color;
423 data->widget = WMCreateSwitchButton(parent);
424 W_ReparentView(WMWidgetView(data->widget),
425 WMGetTableViewDocumentView(parent),
426 0, 0);
427 WMSetButtonText(data->widget, NULL);
428 WMSetButtonImagePosition(data->widget, WIPImageOnly);
429 WMSetButtonImage(data->widget, NULL);
430 WMSetButtonAltImage(data->widget, WMGetSystemPixmap(scr, WSICheckMark));
432 data->table = parent;
433 color = WMCreateNamedColor(scr, SelectionColor, False);
434 WMSetWidgetBackgroundColor(data->widget, color);
435 data->gc = WMColorGC(WMWhiteColor(scr));
436 data->selGC = WMColorGC(color);
438 delegate->data = data;
439 delegate->drawCell = BSCellPainter;
440 delegate->drawSelectedCell = selectedBSCellPainter;
441 delegate->beginCellEdit = beginBSCellEdit;
442 delegate->endCellEdit = endBSCellEdit;
444 return delegate;
448 /* ---------------------------------------------------------------------- */
451 static void
452 SCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
453 int row, Drawable d)
455 StringData *strdata = (StringData*)self->data;
456 WMTableView *table = WMGetTableColumnTableView(column);
458 stringDraw(WMWidgetScreen(table), d,
459 strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
460 WMTableViewDataForCell(table, column, row),
461 WMTableViewRectForCell(table, column, row),
462 False);
466 static void
467 selectedSCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
468 int row, Drawable d)
470 StringData *strdata = (StringData*)self->data;
471 WMTableView *table = WMGetTableColumnTableView(column);
473 stringDraw(WMWidgetScreen(table), d,
474 strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
475 WMTableViewDataForCell(table, column, row),
476 WMTableViewRectForCell(table, column, row),
477 True);
481 WMTableColumnDelegate*
482 WTCreateStringDelegate(WMTableView *parent)
484 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
485 WMScreen *scr = WMWidgetScreen(parent);
486 StringData *data = wmalloc(sizeof(StringData));
488 data->table = parent;
489 data->font = WMSystemFontOfSize(scr, 12);
490 data->gc = WMColorGC(WMWhiteColor(scr));
491 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
492 data->textColor = WMBlackColor(scr);
494 delegate->data = data;
495 delegate->drawCell = SCellPainter;
496 delegate->drawSelectedCell = selectedSCellPainter;
497 delegate->beginCellEdit = NULL;
498 delegate->endCellEdit = NULL;
500 return delegate;
504 /* ---------------------------------------------------------------------- */
507 static void
508 PCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
509 int row, Drawable d)
511 StringData *strdata = (StringData*)self->data;
512 WMTableView *table = WMGetTableColumnTableView(column);
514 pixmapDraw(WMWidgetScreen(table), d,
515 strdata->gc, strdata->selGC,
516 (WMPixmap*)WMTableViewDataForCell(table, column, row),
517 WMTableViewRectForCell(table, column, row),
518 False);
522 static void
523 selectedPCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
524 int row, Drawable d)
526 StringData *strdata = (StringData*)self->data;
527 WMTableView *table = WMGetTableColumnTableView(column);
529 pixmapDraw(WMWidgetScreen(table), d,
530 strdata->gc, strdata->selGC,
531 (WMPixmap*)WMTableViewDataForCell(table, column, row),
532 WMTableViewRectForCell(table, column, row),
533 True);
537 WMTableColumnDelegate*
538 WTCreatePixmapDelegate(WMTableView *table)
540 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
541 WMScreen *scr = WMWidgetScreen(table);
542 StringData *data = wmalloc(sizeof(StringData));
544 data->table = table;
545 data->gc = WMColorGC(WMWhiteColor(scr));
546 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
548 delegate->data = data;
549 delegate->drawCell = PCellPainter;
550 delegate->drawSelectedCell = selectedPCellPainter;
551 delegate->beginCellEdit = NULL;
552 delegate->endCellEdit = NULL;
554 return delegate;
558 /* ---------------------------------------------------------------------- */
561 static void
562 drawPSCell(WMTableColumnDelegate *self, Drawable d, WMTableColumn *column,
563 int row, Bool selected)
565 StringData *strdata = (StringData*)self->data;
566 WMTableView *table = WMGetTableColumnTableView(column);
567 void **data;
568 WMPixmap *pix;
569 char *str;
570 WMRect rect;
571 WMSize size;
573 data = WMTableViewDataForCell(table, column, row);
575 str = (char*)data[0];
576 pix = (WMPixmap*)data[1];
578 rect = WMTableViewRectForCell(table, column, row);
580 if (pix) {
581 int owidth = rect.size.width;
583 size = WMGetPixmapSize(pix);
584 rect.size.width = size.width;
586 pixmapDraw(WMWidgetScreen(table),
587 WMViewXID(WMGetTableViewDocumentView(table)),
588 strdata->gc, strdata->selGC, pix, rect,
589 selected);
591 rect.pos.x += size.width-1;
592 rect.size.width = owidth-size.width+1;
595 stringDraw(WMWidgetScreen(table), d, strdata->gc, strdata->selGC,
596 strdata->textColor, strdata->font, str, rect, selected);
600 static void
601 PSCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
602 int row, Drawable d)
604 drawPSCell(self, d, column, row, False);
608 static void
609 selectedPSCellPainter(WMTableColumnDelegate *self, WMTableColumn *column,
610 int row, Drawable d)
612 drawPSCell(self, d, column, row, True);
616 WMTableColumnDelegate*
617 WTCreatePixmapStringDelegate(WMTableView *parent)
619 WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
620 WMScreen *scr = WMWidgetScreen(parent);
621 StringData *data = wmalloc(sizeof(StringData));
623 data->table = parent;
624 data->font = WMSystemFontOfSize(scr, 12);
625 data->gc = WMColorGC(WMWhiteColor(scr));
626 data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
627 data->textColor = WMBlackColor(scr);
629 delegate->data = data;
630 delegate->drawCell = PSCellPainter;
631 delegate->drawSelectedCell = selectedPSCellPainter;
632 delegate->beginCellEdit = NULL;
633 delegate->endCellEdit = NULL;
635 return delegate;