1 /*
2 * Demo user widget for WINGs
3 *
4 * Author: Alfredo K. Kojima
5 *
6 * This file is in the public domain.
7 *
8 */
9
10 /*
11 *
12 * Include the WINGs private data header.
13 *
14 *
15 */
16 #include <WINGs/WINGsP.h>
17
18 /*
19 * Our public header.
20 */
21 #include "mywidget.h"
22
23 /*
24 * Define the widget "class"
25 */
26 typedef struct W_MyWidget {
27 /* these two fields must be present in all your widgets in this
28 * exact position */
29 W_Class widgetClass;
30 WMView *view;
31
32 /* put your stuff here */
33 char *text;
34
35 } _MyWidget;
36
37 /* some forward declarations */
38
39 static void destroyMyWidget(_MyWidget * mPtr);
40 static void paintMyWidget(_MyWidget * mPtr);
41
42 static void handleEvents(XEvent * event, void *data);
43 static void handleActionEvents(XEvent * event, void *data);
44
45 /*
46 * Delegates
47 * See the source for the other widgets to see how to use.
48 * You won't need to use this most of the time.
49 */
50 static W_ViewDelegate _MyWidgetDelegate = {
51 NULL,
52 NULL,
53 NULL,
54 NULL,
55 NULL
56 };
57
58 /* our widget class ID */
59 static W_Class myWidgetClass = 0;
60
61 /*
62 * Initializer for our widget. Must be called before creating any
63 * instances of the widget.
64 */
InitMyWidget(WMScreen * scr)65 W_Class InitMyWidget(WMScreen * scr)
66 {
67 /* register our widget with WINGs and get our widget class ID */
68 if (!myWidgetClass) {
69 myWidgetClass = W_RegisterUserWidget();
70 }
71
72 return myWidgetClass;
73 }
74
75 /*
76 * Our widget fabrication plant.
77 */
CreateMyWidget(WMWidget * parent)78 MyWidget *CreateMyWidget(WMWidget * parent)
79 {
80 MyWidget *mPtr;
81
82 /* allocate some storage for our new widget instance */
83 mPtr = wmalloc(sizeof(MyWidget));
84 /* initialize it */
85 memset(mPtr, 0, sizeof(MyWidget));
86
87 /* set the class ID */
88 mPtr->widgetClass = myWidgetClass;
89
90 /*
91 * Create the view for our widget.
92 * Note: the Window for the view is only created after the view is
93 * realized with W_RealizeView()
94 *
95 * Consider the returned view as read-only.
96 */
97 mPtr->view = W_CreateView(W_VIEW(parent));
98 if (!mPtr->view) {
99 wfree(mPtr);
100 return NULL;
101 }
102 /* always do this */
103 mPtr->view->self = mPtr;
104
105 /* setup the delegates for the view */
106 mPtr->view->delegate = &_MyWidgetDelegate;
107
108 /*
109 * Intercept some events for our widget, so that we can handle them.
110 */
111 WMCreateEventHandler(mPtr->view, ExposureMask /* this allows us to know when we should paint */
112 | StructureNotifyMask, /* this allows us to know things like when we are destroyed */
113 handleEvents, mPtr);
114
115 /*
116 * Intercept some other events. This could be merged with the above
117 * call, but we separate for more organization.
118 */
119 WMCreateEventHandler(mPtr->view, ButtonPressMask, handleActionEvents, mPtr);
120
121 return mPtr;
122 }
123
124 /*
125 * Paint our widget contents.
126 */
paintMyWidget(_MyWidget * mPtr)127 static void paintMyWidget(_MyWidget * mPtr)
128 {
129 W_Screen *scr = mPtr->view->screen;
130 WMColor *color;
131
132 if (mPtr->text) {
133
134 color = WMWhiteColor(scr);
135
136 W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
137 mPtr->view->size.width, WACenter, color, False, mPtr->text, strlen(mPtr->text));
138
139 WMReleaseColor(color);
140 }
141 }
142
handleEvents(XEvent * event,void * data)143 static void handleEvents(XEvent * event, void *data)
144 {
145 _MyWidget *mPtr = (_MyWidget *) data;
146
147 switch (event->type) {
148 case Expose:
149 if (event->xexpose.count != 0)
150 break;
151 paintMyWidget(mPtr);
152 break;
153
154 case DestroyNotify:
155 destroyMyWidget(mPtr);
156 break;
157
158 }
159 }
160
handleActionEvents(XEvent * event,void * data)161 static void handleActionEvents(XEvent * event, void *data)
162 {
163 _MyWidget *mPtr = (_MyWidget *) data;
164
165 switch (event->type) {
166 case ButtonPress:
167 XBell(mPtr->view->screen->display, 100);
168 XBell(mPtr->view->screen->display, 100);
169 break;
170 }
171 }
172
SetMyWidgetText(MyWidget * mPtr,char * text)173 void SetMyWidgetText(MyWidget * mPtr, char *text)
174 {
175 CHECK_CLASS(mPtr, myWidgetClass);
176
177 if (mPtr->text)
178 wfree(mPtr->text);
179
180 mPtr->text = wstrdup(text);
181
182 if (W_VIEW_MAPPED(mPtr->view)) {
183 paintMyWidget(mPtr);
184 }
185 }
186
destroyMyWidget(_MyWidget * mPtr)187 static void destroyMyWidget(_MyWidget * mPtr)
188 {
189 /*
190 * Free all data we allocated for our widget.
191 */
192
193 if (mPtr->text)
194 wfree(mPtr->text);
195
196 wfree(mPtr);
197 }
198