changed indentation to use spaces only
[wmaker-crm.git] / WINGs / Tests / mywidget.c
blob3bd78d77a90d23f5d51849f74b82d6285aa78dc6
1 /*
2 * Demo user widget for WINGs
4 * Author: Alfredo K. Kojima
6 * This file is in the public domain.
8 */
13 * Include the WINGs private data header.
17 #include <WINGs/WINGsP.h>
20 * Our public header.
22 #include "mywidget.h"
25 * Define the widget "class"
27 typedef struct W_MyWidget {
28 /* these two fields must be present in all your widgets in this
29 * exact position */
30 W_Class widgetClass;
31 WMView *view;
33 /* put your stuff here */
34 char *text;
36 } _MyWidget;
41 /* some forward declarations */
43 static void destroyMyWidget(_MyWidget *mPtr);
44 static void paintMyWidget(_MyWidget *mPtr);
47 static void handleEvents(XEvent *event, void *data);
48 static void handleActionEvents(XEvent *event, void *data);
53 * Delegates
54 * See the source for the other widgets to see how to use.
55 * You won't need to use this most of the time.
57 static W_ViewDelegate _MyWidgetDelegate = {
58 NULL,
59 NULL,
60 NULL,
61 NULL,
62 NULL
66 /* our widget class ID */
67 static W_Class myWidgetClass = 0;
71 * Initializer for our widget. Must be called before creating any
72 * instances of the widget.
74 W_Class
75 InitMyWidget(WMScreen *scr)
77 /* register our widget with WINGs and get our widget class ID */
78 if (!myWidgetClass) {
79 myWidgetClass = W_RegisterUserWidget();
82 return myWidgetClass;
87 * Our widget fabrication plant.
89 MyWidget*
90 CreateMyWidget(WMWidget *parent)
92 MyWidget *mPtr;
94 /* allocate some storage for our new widget instance */
95 mPtr = wmalloc(sizeof(MyWidget));
96 /* initialize it */
97 memset(mPtr, 0, sizeof(MyWidget));
99 /* set the class ID */
100 mPtr->widgetClass = myWidgetClass;
103 * Create the view for our widget.
104 * Note: the Window for the view is only created after the view is
105 * realized with W_RealizeView()
107 * Consider the returned view as read-only.
109 mPtr->view = W_CreateView(W_VIEW(parent));
110 if (!mPtr->view) {
111 wfree(mPtr);
112 return NULL;
114 /* always do this */
115 mPtr->view->self = mPtr;
117 /* setup the delegates for the view */
118 mPtr->view->delegate = &_MyWidgetDelegate;
121 * Intercept some events for our widget, so that we can handle them.
123 WMCreateEventHandler(mPtr->view, ExposureMask /* this allows us to know when we should paint */
124 |StructureNotifyMask, /* this allows us to know things like when we are destroyed */
125 handleEvents, mPtr);
128 * Intercept some other events. This could be merged with the above
129 * call, but we separate for more organization.
131 WMCreateEventHandler(mPtr->view, ButtonPressMask,handleActionEvents, mPtr);
133 return mPtr;
139 * Paint our widget contents.
141 static void
142 paintMyWidget(_MyWidget *mPtr)
144 W_Screen *scr = mPtr->view->screen;
145 WMColor *color;
148 if (mPtr->text) {
150 color = WMWhiteColor(scr);
152 W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
153 mPtr->view->size.width, WACenter, color,
154 False, mPtr->text, strlen(mPtr->text));
156 WMReleaseColor(color);
162 static void
163 handleEvents(XEvent *event, void *data)
165 _MyWidget *mPtr = (_MyWidget*)data;
168 switch (event->type) {
169 case Expose:
170 if (event->xexpose.count!=0)
171 break;
172 paintMyWidget(mPtr);
173 break;
175 case DestroyNotify:
176 destroyMyWidget(mPtr);
177 break;
183 static void
184 handleActionEvents(XEvent *event, void *data)
186 _MyWidget *mPtr = (_MyWidget*)data;
188 switch (event->type) {
189 case ButtonPress:
190 XBell(mPtr->view->screen->display, 100);
191 XBell(mPtr->view->screen->display, 100);
192 break;
197 void
198 SetMyWidgetText(MyWidget *mPtr, char *text)
200 CHECK_CLASS(mPtr, myWidgetClass);
202 if (mPtr->text)
203 wfree(mPtr->text);
205 mPtr->text = wstrdup(text);
207 if (W_VIEW_MAPPED(mPtr->view)) {
208 paintMyWidget(mPtr);
214 static void
215 destroyMyWidget(_MyWidget *mPtr)
218 * Free all data we allocated for our widget.
221 if (mPtr->text)
222 wfree(mPtr->text);
224 wfree(mPtr);