Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / double.c
blob92bcee2dcdc0673211a9b0062e69d848288b473b
2 /*
3 * Widget for testing double-clicks
5 */
7 #include <WINGs/WINGsP.h>
9 #include "double.h"
11 typedef struct W_DoubleTest {
12 W_Class widgetClass;
13 WMView *view;
15 WMHandlerID timer;
16 char on;
17 char active;
18 char *text;
19 } _DoubleTest;
21 /* some forward declarations */
23 static void destroyDoubleTest(_DoubleTest * dPtr);
24 static void paintDoubleTest(_DoubleTest * dPtr);
26 static void handleEvents(XEvent * event, void *data);
27 static void handleActionEvents(XEvent * event, void *data);
29 /* our widget class ID */
30 static W_Class DoubleTestClass = 0;
33 * Initializer for our widget. Must be called before creating any
34 * instances of the widget.
36 W_Class InitDoubleTest(WMScreen * scr)
38 /* register our widget with WINGs and get our widget class ID */
39 if (!DoubleTestClass) {
40 DoubleTestClass = W_RegisterUserWidget();
43 return DoubleTestClass;
47 * Our widget fabrication plant.
49 DoubleTest *CreateDoubleTest(WMWidget * parent, char *text)
51 DoubleTest *dPtr;
53 if (!DoubleTestClass)
54 InitDoubleTest(WMWidgetScreen(parent));
56 /* allocate some storage for our new widget instance */
57 dPtr = wmalloc(sizeof(DoubleTest));
58 /* initialize it */
59 memset(dPtr, 0, sizeof(DoubleTest));
61 /* set the class ID */
62 dPtr->widgetClass = DoubleTestClass;
64 dPtr->view = W_CreateView(W_VIEW(parent));
65 if (!dPtr->view) {
66 wfree(dPtr);
67 return NULL;
69 /* always do this */
70 dPtr->view->self = dPtr;
72 dPtr->text = wstrdup(text);
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);
78 WMCreateEventHandler(dPtr->view, ButtonPressMask, handleActionEvents, dPtr);
80 return dPtr;
83 static void paintDoubleTest(_DoubleTest * dPtr)
85 W_Screen *scr = dPtr->view->screen;
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);
94 W_DrawRelief(scr, dPtr->view->window, 0, 0, dPtr->view->size.width,
95 dPtr->view->size.height, dPtr->on ? WRSunken : WRRaised);
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));
106 static void handleEvents(XEvent * event, void *data)
108 _DoubleTest *dPtr = (_DoubleTest *) data;
110 switch (event->type) {
111 case Expose:
112 if (event->xexpose.count != 0)
113 break;
114 paintDoubleTest(dPtr);
115 break;
117 case DestroyNotify:
118 destroyDoubleTest(dPtr);
119 break;
124 static void deactivate(void *data)
126 _DoubleTest *dPtr = (_DoubleTest *) data;
128 if (dPtr->active)
129 dPtr->active = 0;
130 paintDoubleTest(dPtr);
132 dPtr->timer = NULL;
135 static void handleActionEvents(XEvent * event, void *data)
137 _DoubleTest *dPtr = (_DoubleTest *) data;
138 extern _WINGsConfiguration WINGsConfiguration;
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);
154 break;
158 static void destroyDoubleTest(_DoubleTest * dPtr)
160 if (dPtr->timer)
161 WMDeleteTimerHandler(dPtr->timer);
162 if (dPtr->text)
163 wfree(dPtr->text);
165 wfree(dPtr);