Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / Extras / wtabledelegates.c
1
2 #include <stdint.h>
3 #include <WINGs/WINGsP.h>
4
5 #include "wtableview.h"
6
7 #include "wtabledelegates.h"
8
9 typedef struct {
10         WMTableView *table;
11         WMFont *font;
12         GC gc;
13         GC selGC;
14         WMColor *textColor;
15 } StringData;
16
17 typedef struct {
18         WMTableView *table;
19         GC selGc;
20 } PixmapData;
21
22 typedef struct {
23         WMTextField *widget;
24         WMTableView *table;
25         WMFont *font;
26         GC gc;
27         GC selGC;
28         WMColor *textColor;
29 } StringEditorData;
30
31 typedef struct {
32         WMPopUpButton *widget;
33         WMTableView *table;
34         WMFont *font;
35         char **options;
36         int count;
37         GC gc;
38         GC selGC;
39         WMColor *textColor;
40 } EnumSelectorData;
41
42 typedef struct {
43         WMButton *widget;
44         WMTableView *table;
45         Bool state;
46         GC gc;
47         GC selGC;
48 } BooleanSwitchData;
49
50 static char *SelectionColor = "#bbbbcc";
51
52 static void
53 stringDraw(WMScreen * scr, Drawable d, GC gc, GC sgc, WMColor * textColor,
54            WMFont * font, void *data, WMRect rect, Bool selected)
55 {
56         int x, y;
57         XRectangle rects[1];
58         Display *dpy = WMScreenDisplay(scr);
59
60         x = rect.pos.x + 5;
61         y = rect.pos.y + (rect.size.height - WMFontHeight(font)) / 2;
62
63         rects[0].x = rect.pos.x + 1;
64         rects[0].y = rect.pos.y + 1;
65         rects[0].width = rect.size.width - 1;
66         rects[0].height = rect.size.height - 1;
67         XSetClipRectangles(dpy, gc, 0, 0, rects, 1, YXSorted);
68
69         if (!selected) {
70                 XFillRectangles(dpy, d, gc, rects, 1);
71
72                 WMDrawString(scr, d, textColor, font, x, y, data, strlen(data));
73         } else {
74                 XFillRectangles(dpy, d, sgc, rects, 1);
75
76                 WMDrawString(scr, d, textColor, font, x, y, data, strlen(data));
77         }
78
79         XSetClipMask(dpy, gc, None);
80 }
81
82 static void pixmapDraw(WMScreen * scr, Drawable d, GC gc, GC sgc, WMPixmap * pixmap, WMRect rect, Bool selected)
83 {
84         int x, y;
85         XRectangle rects[1];
86         Display *dpy = WMScreenDisplay(scr);
87         WMSize size;
88
89         rects[0].x = rect.pos.x + 1;
90         rects[0].y = rect.pos.y + 1;
91         rects[0].width = rect.size.width - 1;
92         rects[0].height = rect.size.height - 1;
93         XSetClipRectangles(dpy, gc, 0, 0, rects, 1, YXSorted);
94
95         if (!selected) {
96                 XFillRectangles(dpy, d, gc, rects, 1);
97
98                 if (pixmap) {
99                         size = WMGetPixmapSize(pixmap);
100                         x = rect.pos.x + (rect.size.width - size.width) / 2;
101                         y = rect.pos.y + (rect.size.height - size.height) / 2;
102
103                         WMDrawPixmap(pixmap, d, x, y);
104                 }
105         } else {
106                 XFillRectangles(dpy, d, sgc, rects, 1);
107
108                 if (pixmap) {
109                         size = WMGetPixmapSize(pixmap);
110                         x = rect.pos.x + (rect.size.width - size.width) / 2;
111                         y = rect.pos.y + (rect.size.height - size.height) / 2;
112
113                         WMDrawPixmap(pixmap, d, x, y);
114                 }
115         }
116
117         XSetClipMask(dpy, gc, None);
118 }
119
120 /* ---------------------------------------------------------------------- */
121
122 static void SECellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
123 {
124         StringEditorData *strdata = (StringEditorData *) self->data;
125         WMTableView *table = WMGetTableColumnTableView(column);
126
127         stringDraw(WMWidgetScreen(table), d,
128                    strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
129                    WMTableViewDataForCell(table, column, row), WMTableViewRectForCell(table, column, row), False);
130 }
131
132 static void selectedSECellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
133 {
134         StringEditorData *strdata = (StringEditorData *) self->data;
135         WMTableView *table = WMGetTableColumnTableView(column);
136
137         stringDraw(WMWidgetScreen(table), d,
138                    strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
139                    WMTableViewDataForCell(table, column, row), WMTableViewRectForCell(table, column, row), True);
140 }
141
142 static void beginSECellEdit(WMTableColumnDelegate * self, WMTableColumn * column, int row)
143 {
144         StringEditorData *strdata = (StringEditorData *) self->data;
145         WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
146         void *data = WMTableViewDataForCell(strdata->table, column, row);
147
148         WMSetTextFieldText(strdata->widget, (char *)data);
149         WMMoveWidget(strdata->widget, rect.pos.x, rect.pos.y);
150         WMResizeWidget(strdata->widget, rect.size.width + 1, rect.size.height + 1);
151
152         WMMapWidget(strdata->widget);
153 }
154
155 static void endSECellEdit(WMTableColumnDelegate * self, WMTableColumn * column, int row)
156 {
157         StringEditorData *strdata = (StringEditorData *) self->data;
158         char *text;
159
160         WMUnmapWidget(strdata->widget);
161
162         text = WMGetTextFieldText(strdata->widget);
163         WMSetTableViewDataForCell(strdata->table, column, row, (void *)text);
164 }
165
166 WMTableColumnDelegate *WTCreateStringEditorDelegate(WMTableView * parent)
167 {
168         WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
169         WMScreen *scr = WMWidgetScreen(parent);
170         StringEditorData *data = wmalloc(sizeof(StringEditorData));
171
172         data->widget = WMCreateTextField(parent);
173         W_ReparentView(WMWidgetView(data->widget), WMGetTableViewDocumentView(parent), 0, 0);
174         data->table = parent;
175         data->font = WMSystemFontOfSize(scr, 12);
176         data->gc = WMColorGC(WMWhiteColor(scr));
177         data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
178         data->textColor = WMBlackColor(scr);
179
180         delegate->data = data;
181         delegate->drawCell = SECellPainter;
182         delegate->drawSelectedCell = selectedSECellPainter;
183         delegate->beginCellEdit = beginSECellEdit;
184         delegate->endCellEdit = endSECellEdit;
185
186         return delegate;
187 }
188
189 /* ---------------------------------------------------------------------- */
190
191 static void ESCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
192 {
193         EnumSelectorData *strdata = (EnumSelectorData *) self->data;
194         WMTableView *table = WMGetTableColumnTableView(column);
195         int i = (int)(uintptr_t) WMTableViewDataForCell(table, column, row);
196
197         stringDraw(WMWidgetScreen(table), d,
198                    strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
199                    strdata->options[i], WMTableViewRectForCell(table, column, row), False);
200 }
201
202 static void selectedESCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
203 {
204         EnumSelectorData *strdata = (EnumSelectorData *) self->data;
205         WMTableView *table = WMGetTableColumnTableView(column);
206         int i = (int)(uintptr_t) WMTableViewDataForCell(table, column, row);
207
208         stringDraw(WMWidgetScreen(table), d,
209                    strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
210                    strdata->options[i], WMTableViewRectForCell(table, column, row), True);
211 }
212
213 static void beginESCellEdit(WMTableColumnDelegate * self, WMTableColumn * column, int row)
214 {
215         EnumSelectorData *strdata = (EnumSelectorData *) self->data;
216         WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
217         int data = (int)(uintptr_t) WMTableViewDataForCell(strdata->table, column, row);
218
219         wassertr(data < strdata->count);
220
221         WMSetPopUpButtonSelectedItem(strdata->widget, data);
222
223         WMMoveWidget(strdata->widget, rect.pos.x, rect.pos.y);
224         WMResizeWidget(strdata->widget, rect.size.width, rect.size.height + 1);
225
226         WMMapWidget(strdata->widget);
227 }
228
229 static void endESCellEdit(WMTableColumnDelegate * self, WMTableColumn * column, int row)
230 {
231         EnumSelectorData *strdata = (EnumSelectorData *) self->data;
232         int option;
233
234         WMUnmapWidget(strdata->widget);
235
236         option = WMGetPopUpButtonSelectedItem(strdata->widget);
237         WMSetTableViewDataForCell(strdata->table, column, row, (void *)(uintptr_t) option);
238 }
239
240 WMTableColumnDelegate *WTCreateEnumSelectorDelegate(WMTableView * parent)
241 {
242         WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
243         WMScreen *scr = WMWidgetScreen(parent);
244         EnumSelectorData *data = wmalloc(sizeof(EnumSelectorData));
245
246         data->widget = WMCreatePopUpButton(parent);
247         W_ReparentView(WMWidgetView(data->widget), WMGetTableViewDocumentView(parent), 0, 0);
248         data->table = parent;
249         data->font = WMSystemFontOfSize(scr, 12);
250         data->gc = WMColorGC(WMWhiteColor(scr));
251         data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
252         data->textColor = WMBlackColor(scr);
253         data->count = 0;
254         data->options = NULL;
255
256         delegate->data = data;
257         delegate->drawCell = ESCellPainter;
258         delegate->drawSelectedCell = selectedESCellPainter;
259         delegate->beginCellEdit = beginESCellEdit;
260         delegate->endCellEdit = endESCellEdit;
261
262         return delegate;
263 }
264
265 void WTSetEnumSelectorOptions(WMTableColumnDelegate * delegate, char **options, int count)
266 {
267         EnumSelectorData *data = (EnumSelectorData *) delegate->data;
268         int i;
269
270         for (i = 0; i < WMGetPopUpButtonNumberOfItems(data->widget); i++) {
271                 WMRemovePopUpButtonItem(data->widget, 0);
272         }
273
274         data->options = options;
275         data->count = count;
276
277         for (i = 0; i < count; i++) {
278                 WMAddPopUpButtonItem(data->widget, options[i]);
279         }
280 }
281
282 /* ---------------------------------------------------------------------- */
283
284 static void BSCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
285 {
286         BooleanSwitchData *strdata = (BooleanSwitchData *) self->data;
287         WMTableView *table = WMGetTableColumnTableView(column);
288         int i = (int)(uintptr_t) WMTableViewDataForCell(table, column, row);
289         WMScreen *scr = WMWidgetScreen(table);
290
291         if (i) {
292                 pixmapDraw(scr, d,
293                            strdata->gc, strdata->selGC, WMGetSystemPixmap(scr, WSICheckMark),
294                            WMTableViewRectForCell(table, column, row), False);
295         } else {
296                 pixmapDraw(scr, d,
297                            strdata->gc, strdata->selGC, NULL, WMTableViewRectForCell(table, column, row), False);
298         }
299 }
300
301 static void selectedBSCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
302 {
303         BooleanSwitchData *strdata = (BooleanSwitchData *) self->data;
304         WMTableView *table = WMGetTableColumnTableView(column);
305         int i = (int)(uintptr_t) WMTableViewDataForCell(table, column, row);
306         WMScreen *scr = WMWidgetScreen(table);
307
308         if (i) {
309                 pixmapDraw(scr, d,
310                            strdata->gc, strdata->selGC, WMGetSystemPixmap(scr, WSICheckMark),
311                            WMTableViewRectForCell(table, column, row), True);
312         } else {
313                 pixmapDraw(scr, d,
314                            strdata->gc, strdata->selGC, NULL, WMTableViewRectForCell(table, column, row), True);
315         }
316 }
317
318 static void beginBSCellEdit(WMTableColumnDelegate * self, WMTableColumn * column, int row)
319 {
320         BooleanSwitchData *strdata = (BooleanSwitchData *) self->data;
321         WMRect rect = WMTableViewRectForCell(strdata->table, column, row);
322         int data = (int)(uintptr_t) WMTableViewDataForCell(strdata->table, column, row);
323
324         WMSetButtonSelected(strdata->widget, data);
325         WMMoveWidget(strdata->widget, rect.pos.x + 1, rect.pos.y + 1);
326         WMResizeWidget(strdata->widget, rect.size.width - 1, rect.size.height - 1);
327
328         WMMapWidget(strdata->widget);
329 }
330
331 static void endBSCellEdit(WMTableColumnDelegate * self, WMTableColumn * column, int row)
332 {
333         BooleanSwitchData *strdata = (BooleanSwitchData *) self->data;
334         int value;
335
336         value = WMGetButtonSelected(strdata->widget);
337         WMSetTableViewDataForCell(strdata->table, column, row, (void *)(uintptr_t) value);
338         WMUnmapWidget(strdata->widget);
339 }
340
341 WMTableColumnDelegate *WTCreateBooleanSwitchDelegate(WMTableView * parent)
342 {
343         WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
344         WMScreen *scr = WMWidgetScreen(parent);
345         BooleanSwitchData *data = wmalloc(sizeof(BooleanSwitchData));
346         WMColor *color;
347
348         data->widget = WMCreateSwitchButton(parent);
349         W_ReparentView(WMWidgetView(data->widget), WMGetTableViewDocumentView(parent), 0, 0);
350         WMSetButtonText(data->widget, NULL);
351         WMSetButtonImagePosition(data->widget, WIPImageOnly);
352         WMSetButtonImage(data->widget, NULL);
353         WMSetButtonAltImage(data->widget, WMGetSystemPixmap(scr, WSICheckMark));
354
355         data->table = parent;
356         color = WMCreateNamedColor(scr, SelectionColor, False);
357         WMSetWidgetBackgroundColor(data->widget, color);
358         data->gc = WMColorGC(WMWhiteColor(scr));
359         data->selGC = WMColorGC(color);
360
361         delegate->data = data;
362         delegate->drawCell = BSCellPainter;
363         delegate->drawSelectedCell = selectedBSCellPainter;
364         delegate->beginCellEdit = beginBSCellEdit;
365         delegate->endCellEdit = endBSCellEdit;
366
367         return delegate;
368 }
369
370 /* ---------------------------------------------------------------------- */
371
372 static void SCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
373 {
374         StringData *strdata = (StringData *) self->data;
375         WMTableView *table = WMGetTableColumnTableView(column);
376
377         stringDraw(WMWidgetScreen(table), d,
378                    strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
379                    WMTableViewDataForCell(table, column, row), WMTableViewRectForCell(table, column, row), False);
380 }
381
382 static void selectedSCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
383 {
384         StringData *strdata = (StringData *) self->data;
385         WMTableView *table = WMGetTableColumnTableView(column);
386
387         stringDraw(WMWidgetScreen(table), d,
388                    strdata->gc, strdata->selGC, strdata->textColor, strdata->font,
389                    WMTableViewDataForCell(table, column, row), WMTableViewRectForCell(table, column, row), True);
390 }
391
392 WMTableColumnDelegate *WTCreateStringDelegate(WMTableView * parent)
393 {
394         WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
395         WMScreen *scr = WMWidgetScreen(parent);
396         StringData *data = wmalloc(sizeof(StringData));
397
398         data->table = parent;
399         data->font = WMSystemFontOfSize(scr, 12);
400         data->gc = WMColorGC(WMWhiteColor(scr));
401         data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
402         data->textColor = WMBlackColor(scr);
403
404         delegate->data = data;
405         delegate->drawCell = SCellPainter;
406         delegate->drawSelectedCell = selectedSCellPainter;
407         delegate->beginCellEdit = NULL;
408         delegate->endCellEdit = NULL;
409
410         return delegate;
411 }
412
413 /* ---------------------------------------------------------------------- */
414
415 static void PCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
416 {
417         StringData *strdata = (StringData *) self->data;
418         WMTableView *table = WMGetTableColumnTableView(column);
419
420         pixmapDraw(WMWidgetScreen(table), d,
421                    strdata->gc, strdata->selGC,
422                    (WMPixmap *) WMTableViewDataForCell(table, column, row),
423                    WMTableViewRectForCell(table, column, row), False);
424 }
425
426 static void selectedPCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
427 {
428         StringData *strdata = (StringData *) self->data;
429         WMTableView *table = WMGetTableColumnTableView(column);
430
431         pixmapDraw(WMWidgetScreen(table), d,
432                    strdata->gc, strdata->selGC,
433                    (WMPixmap *) WMTableViewDataForCell(table, column, row),
434                    WMTableViewRectForCell(table, column, row), True);
435 }
436
437 WMTableColumnDelegate *WTCreatePixmapDelegate(WMTableView * table)
438 {
439         WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
440         WMScreen *scr = WMWidgetScreen(table);
441         StringData *data = wmalloc(sizeof(StringData));
442
443         data->table = table;
444         data->gc = WMColorGC(WMWhiteColor(scr));
445         data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
446
447         delegate->data = data;
448         delegate->drawCell = PCellPainter;
449         delegate->drawSelectedCell = selectedPCellPainter;
450         delegate->beginCellEdit = NULL;
451         delegate->endCellEdit = NULL;
452
453         return delegate;
454 }
455
456 /* ---------------------------------------------------------------------- */
457
458 static void drawPSCell(WMTableColumnDelegate * self, Drawable d, WMTableColumn * column, int row, Bool selected)
459 {
460         StringData *strdata = (StringData *) self->data;
461         WMTableView *table = WMGetTableColumnTableView(column);
462         void **data;
463         WMPixmap *pix;
464         char *str;
465         WMRect rect;
466         WMSize size;
467
468         data = WMTableViewDataForCell(table, column, row);
469
470         str = (char *)data[0];
471         pix = (WMPixmap *) data[1];
472
473         rect = WMTableViewRectForCell(table, column, row);
474
475         if (pix) {
476                 int owidth = rect.size.width;
477
478                 size = WMGetPixmapSize(pix);
479                 rect.size.width = size.width;
480
481                 pixmapDraw(WMWidgetScreen(table),
482                            WMViewXID(WMGetTableViewDocumentView(table)),
483                            strdata->gc, strdata->selGC, pix, rect, selected);
484
485                 rect.pos.x += size.width - 1;
486                 rect.size.width = owidth - size.width + 1;
487         }
488
489         stringDraw(WMWidgetScreen(table), d, strdata->gc, strdata->selGC,
490                    strdata->textColor, strdata->font, str, rect, selected);
491 }
492
493 static void PSCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
494 {
495         drawPSCell(self, d, column, row, False);
496 }
497
498 static void selectedPSCellPainter(WMTableColumnDelegate * self, WMTableColumn * column, int row, Drawable d)
499 {
500         drawPSCell(self, d, column, row, True);
501 }
502
503 WMTableColumnDelegate *WTCreatePixmapStringDelegate(WMTableView * parent)
504 {
505         WMTableColumnDelegate *delegate = wmalloc(sizeof(WMTableColumnDelegate));
506         WMScreen *scr = WMWidgetScreen(parent);
507         StringData *data = wmalloc(sizeof(StringData));
508
509         data->table = parent;
510         data->font = WMSystemFontOfSize(scr, 12);
511         data->gc = WMColorGC(WMWhiteColor(scr));
512         data->selGC = WMColorGC(WMCreateNamedColor(scr, SelectionColor, False));
513         data->textColor = WMBlackColor(scr);
514
515         delegate->data = data;
516         delegate->drawCell = PSCellPainter;
517         delegate->drawSelectedCell = selectedPSCellPainter;
518         delegate->beginCellEdit = NULL;
519         delegate->endCellEdit = NULL;
520
521         return delegate;
522 }