Change to the linux kernel coding style
[wmaker-crm.git] / WPrefs.app / double.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001
2/*
3 * Widget for testing double-clicks
6830b057 dan2004-10-12 21:28:27 +00004 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00005 */
6
a20aebde dan2001-01-18 19:21:56 +00007#include <WINGs/WINGsP.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +00008
9#include "double.h"
10
9d2e6ef9 scottc1998-09-29 22:36:29 +000011typedef struct W_DoubleTest {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020012 W_Class widgetClass;
13 WMView *view;
9d2e6ef9 scottc1998-09-29 22:36:29 +000014
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020015 WMHandlerID timer;
16 char on;
17 char active;
18 char *text;
9d2e6ef9 scottc1998-09-29 22:36:29 +000019} _DoubleTest;
20
9d2e6ef9 scottc1998-09-29 22:36:29 +000021/* some forward declarations */
22
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020023static void destroyDoubleTest(_DoubleTest * dPtr);
24static void paintDoubleTest(_DoubleTest * dPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +000025
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020026static void handleEvents(XEvent * event, void *data);
27static void handleActionEvents(XEvent * event, void *data);
9d2e6ef9 scottc1998-09-29 22:36:29 +000028
29/* our widget class ID */
30static W_Class DoubleTestClass = 0;
31
9d2e6ef9 scottc1998-09-29 22:36:29 +000032/*
33 * Initializer for our widget. Must be called before creating any
34 * instances of the widget.
35 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020036W_Class InitDoubleTest(WMScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +000037{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020038 /* register our widget with WINGs and get our widget class ID */
39 if (!DoubleTestClass) {
40 DoubleTestClass = W_RegisterUserWidget();
41 }
5e4625da kojima1999-05-29 21:41:25 +000042
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020043 return DoubleTestClass;
9d2e6ef9 scottc1998-09-29 22:36:29 +000044}
45
9d2e6ef9 scottc1998-09-29 22:36:29 +000046/*
47 * Our widget fabrication plant.
48 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020049DoubleTest *CreateDoubleTest(WMWidget * parent, char *text)
9d2e6ef9 scottc1998-09-29 22:36:29 +000050{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020051 DoubleTest *dPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +000052
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020053 if (!DoubleTestClass)
54 InitDoubleTest(WMWidgetScreen(parent));
6830b057 dan2004-10-12 21:28:27 +000055
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020056 /* allocate some storage for our new widget instance */
57 dPtr = wmalloc(sizeof(DoubleTest));
58 /* initialize it */
59 memset(dPtr, 0, sizeof(DoubleTest));
9d2e6ef9 scottc1998-09-29 22:36:29 +000060
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020061 /* set the class ID */
62 dPtr->widgetClass = DoubleTestClass;
6830b057 dan2004-10-12 21:28:27 +000063
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020064 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;
6830b057 dan2004-10-12 21:28:27 +000071
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020072 dPtr->text = wstrdup(text);
9d2e6ef9 scottc1998-09-29 22:36:29 +000073
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020074 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);
9d2e6ef9 scottc1998-09-29 22:36:29 +000077
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020078 WMCreateEventHandler(dPtr->view, ButtonPressMask, handleActionEvents, dPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +000079
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020080 return dPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +000081}
82
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020083static void paintDoubleTest(_DoubleTest * dPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +000084{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020085 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 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000104}
105
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200106static void handleEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000107{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200108 _DoubleTest *dPtr = (_DoubleTest *) data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000109
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200110 switch (event->type) {
111 case Expose:
112 if (event->xexpose.count != 0)
113 break;
114 paintDoubleTest(dPtr);
115 break;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000116
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200117 case DestroyNotify:
118 destroyDoubleTest(dPtr);
119 break;
6830b057 dan2004-10-12 21:28:27 +0000120
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200121 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000122}
123
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200124static void deactivate(void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000125{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200126 _DoubleTest *dPtr = (_DoubleTest *) data;
6830b057 dan2004-10-12 21:28:27 +0000127
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200128 if (dPtr->active)
129 dPtr->active = 0;
130 paintDoubleTest(dPtr);
6830b057 dan2004-10-12 21:28:27 +0000131
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200132 dPtr->timer = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000133}
134
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200135static void handleActionEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000136{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200137 _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 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000156}
157
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200158static void destroyDoubleTest(_DoubleTest * dPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000159{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200160 if (dPtr->timer)
161 WMDeleteTimerHandler(dPtr->timer);
162 if (dPtr->text)
163 wfree(dPtr->text);
6830b057 dan2004-10-12 21:28:27 +0000164
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200165 wfree(dPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000166}