- rewrote the font conversion routines to avoid the need to allocate memory
[wmaker-crm.git] / WPrefs.app / double.c
blob98d81256ab53919c5ba04bf4df07fced3aecdb0e
2 /*
3 * Widget for testing double-clicks
4 *
5 */
7 #include <WINGs/WINGsP.h>
9 #include "double.h"
12 typedef struct W_DoubleTest {
13 W_Class widgetClass;
14 WMView *view;
16 WMHandlerID timer;
17 char on;
18 char active;
19 char *text;
20 } _DoubleTest;
25 /* some forward declarations */
27 static void destroyDoubleTest(_DoubleTest *dPtr);
28 static void paintDoubleTest(_DoubleTest *dPtr);
31 static void handleEvents(XEvent *event, void *data);
32 static void handleActionEvents(XEvent *event, void *data);
36 /* our widget class ID */
37 static W_Class DoubleTestClass = 0;
41 * Initializer for our widget. Must be called before creating any
42 * instances of the widget.
44 W_Class
45 InitDoubleTest(WMScreen *scr)
47 /* register our widget with WINGs and get our widget class ID */
48 if (!DoubleTestClass) {
49 DoubleTestClass = W_RegisterUserWidget();
52 return DoubleTestClass;
57 * Our widget fabrication plant.
59 DoubleTest*
60 CreateDoubleTest(WMWidget *parent, char *text)
62 DoubleTest *dPtr;
64 if (!DoubleTestClass)
65 InitDoubleTest(WMWidgetScreen(parent));
67 /* allocate some storage for our new widget instance */
68 dPtr = wmalloc(sizeof(DoubleTest));
69 /* initialize it */
70 memset(dPtr, 0, sizeof(DoubleTest));
72 /* set the class ID */
73 dPtr->widgetClass = DoubleTestClass;
75 dPtr->view = W_CreateView(W_VIEW(parent));
76 if (!dPtr->view) {
77 wfree(dPtr);
78 return NULL;
80 /* always do this */
81 dPtr->view->self = dPtr;
83 dPtr->text = wstrdup(text);
85 WMCreateEventHandler(dPtr->view, ExposureMask /* this allows us to know when we should paint */
86 |StructureNotifyMask, /* this allows us to know things like when we are destroyed */
87 handleEvents, dPtr);
89 WMCreateEventHandler(dPtr->view, ButtonPressMask,handleActionEvents, dPtr);
91 return dPtr;
96 static void
97 paintDoubleTest(_DoubleTest *dPtr)
99 W_Screen *scr = dPtr->view->screen;
101 if (dPtr->active) {
102 XFillRectangle(scr->display, dPtr->view->window, WMColorGC(scr->white),
103 0, 0, dPtr->view->size.width, dPtr->view->size.height);
104 } else {
105 XClearWindow(scr->display, dPtr->view->window);
108 W_DrawRelief(scr, dPtr->view->window, 0, 0, dPtr->view->size.width,
109 dPtr->view->size.height, dPtr->on ? WRSunken : WRRaised);
111 if (dPtr->text) {
112 int y;
113 y = (dPtr->view->size.height-scr->normalFont->height)/2;
114 W_PaintText(dPtr->view, dPtr->view->window, scr->normalFont,
115 dPtr->on, dPtr->on+y, dPtr->view->size.width, WACenter,
116 scr->black, False, dPtr->text, strlen(dPtr->text));
122 static void
123 handleEvents(XEvent *event, void *data)
125 _DoubleTest *dPtr = (_DoubleTest*)data;
128 switch (event->type) {
129 case Expose:
130 if (event->xexpose.count!=0)
131 break;
132 paintDoubleTest(dPtr);
133 break;
135 case DestroyNotify:
136 destroyDoubleTest(dPtr);
137 break;
143 static void
144 deactivate(void *data)
146 _DoubleTest *dPtr = (_DoubleTest*)data;
148 if (dPtr->active)
149 dPtr->active = 0;
150 paintDoubleTest(dPtr);
152 dPtr->timer = NULL;
156 static void
157 handleActionEvents(XEvent *event, void *data)
159 _DoubleTest *dPtr = (_DoubleTest*)data;
160 extern _WINGsConfiguration WINGsConfiguration;
162 switch (event->type) {
163 case ButtonPress:
164 if (WMIsDoubleClick(event)) {
165 if (dPtr->timer)
166 WMDeleteTimerHandler(dPtr->timer);
167 dPtr->timer = NULL;
168 dPtr->on = !dPtr->on;
169 dPtr->active = 0;
170 paintDoubleTest(dPtr);
171 } else {
172 dPtr->timer=WMAddTimerHandler(WINGsConfiguration.doubleClickDelay,
173 deactivate, dPtr);
174 dPtr->active = 1;
175 paintDoubleTest(dPtr);
177 break;
183 static void
184 destroyDoubleTest(_DoubleTest *dPtr)
186 if (dPtr->timer)
187 WMDeleteTimerHandler(dPtr->timer);
188 if (dPtr->text)
189 wfree(dPtr->text);
191 wfree(dPtr);