Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / double.c
1
2 /*
3  * Widget for testing double-clicks
4  *
5  */
6
7 #include <WINGs/WINGsP.h>
8
9 #include "double.h"
10
11 typedef struct W_DoubleTest {
12         W_Class widgetClass;
13         WMView *view;
14
15         WMHandlerID timer;
16         char on;
17         char active;
18         char *text;
19 } _DoubleTest;
20
21 /* some forward declarations */
22
23 static void destroyDoubleTest(_DoubleTest * dPtr);
24 static void paintDoubleTest(_DoubleTest * dPtr);
25
26 static void handleEvents(XEvent * event, void *data);
27 static void handleActionEvents(XEvent * event, void *data);
28
29 /* our widget class ID */
30 static W_Class DoubleTestClass = 0;
31
32 /*
33  * Initializer for our widget. Must be called before creating any
34  * instances of the widget.
35  */
36 W_Class InitDoubleTest(WMScreen * scr)
37 {
38         /* register our widget with WINGs and get our widget class ID */
39         if (!DoubleTestClass) {
40                 DoubleTestClass = W_RegisterUserWidget();
41         }
42
43         return DoubleTestClass;
44 }
45
46 /*
47  * Our widget fabrication plant.
48  */
49 DoubleTest *CreateDoubleTest(WMWidget * parent, char *text)
50 {
51         DoubleTest *dPtr;
52
53         if (!DoubleTestClass)
54                 InitDoubleTest(WMWidgetScreen(parent));
55
56         /* allocate some storage for our new widget instance */
57         dPtr = wmalloc(sizeof(DoubleTest));
58         /* initialize it */
59         memset(dPtr, 0, sizeof(DoubleTest));
60
61         /* set the class ID */
62         dPtr->widgetClass = DoubleTestClass;
63
64         dPtr->view = W_CreateView(W_VIEW(parent));
65         if (!dPtr->view) {
66                 wfree(dPtr);
67                 return NULL;
68         }
69         /* always do this */
70         dPtr->view->self = dPtr;
71
72         dPtr->text = wstrdup(text);
73
74         WMCreateEventHandler(dPtr->view, ExposureMask   /* this allows us to know when we should paint */
75                              | StructureNotifyMask,     /* this allows us to know things like when we are destroyed */
76                              handleEvents, dPtr);
77
78         WMCreateEventHandler(dPtr->view, ButtonPressMask, handleActionEvents, dPtr);
79
80         return dPtr;
81 }
82
83 static void paintDoubleTest(_DoubleTest * dPtr)
84 {
85         W_Screen *scr = dPtr->view->screen;
86
87         if (dPtr->active) {
88                 XFillRectangle(scr->display, dPtr->view->window, WMColorGC(scr->white),
89                                0, 0, dPtr->view->size.width, dPtr->view->size.height);
90         } else {
91                 XClearWindow(scr->display, dPtr->view->window);
92         }
93
94         W_DrawRelief(scr, dPtr->view->window, 0, 0, dPtr->view->size.width,
95                      dPtr->view->size.height, dPtr->on ? WRSunken : WRRaised);
96
97         if (dPtr->text) {
98                 int y;
99                 y = (dPtr->view->size.height - scr->normalFont->height) / 2;
100                 W_PaintText(dPtr->view, dPtr->view->window, scr->normalFont,
101                             dPtr->on, dPtr->on + y, dPtr->view->size.width, WACenter,
102                             scr->black, False, dPtr->text, strlen(dPtr->text));
103         }
104 }
105
106 static void handleEvents(XEvent * event, void *data)
107 {
108         _DoubleTest *dPtr = (_DoubleTest *) data;
109
110         switch (event->type) {
111         case Expose:
112                 if (event->xexpose.count != 0)
113                         break;
114                 paintDoubleTest(dPtr);
115                 break;
116
117         case DestroyNotify:
118                 destroyDoubleTest(dPtr);
119                 break;
120
121         }
122 }
123
124 static void deactivate(void *data)
125 {
126         _DoubleTest *dPtr = (_DoubleTest *) data;
127
128         if (dPtr->active)
129                 dPtr->active = 0;
130         paintDoubleTest(dPtr);
131
132         dPtr->timer = NULL;
133 }
134
135 static void handleActionEvents(XEvent * event, void *data)
136 {
137         _DoubleTest *dPtr = (_DoubleTest *) data;
138         extern _WINGsConfiguration WINGsConfiguration;
139
140         switch (event->type) {
141         case ButtonPress:
142                 if (WMIsDoubleClick(event)) {
143                         if (dPtr->timer)
144                                 WMDeleteTimerHandler(dPtr->timer);
145                         dPtr->timer = NULL;
146                         dPtr->on = !dPtr->on;
147                         dPtr->active = 0;
148                         paintDoubleTest(dPtr);
149                 } else {
150                         dPtr->timer = WMAddTimerHandler(WINGsConfiguration.doubleClickDelay, deactivate, dPtr);
151                         dPtr->active = 1;
152                         paintDoubleTest(dPtr);
153                 }
154                 break;
155         }
156 }
157
158 static void destroyDoubleTest(_DoubleTest * dPtr)
159 {
160         if (dPtr->timer)
161                 WMDeleteTimerHandler(dPtr->timer);
162         if (dPtr->text)
163                 wfree(dPtr->text);
164
165         wfree(dPtr);
166 }