Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / Tests / mywidget.c
blob9212fdaa94983112c5a8cc23d0f197dc65a30610
1 /*
2 * Demo user widget for WINGs
4 * Author: Alfredo K. Kojima
6 * This file is in the public domain.
8 */
12 * Include the WINGs private data header.
16 #include <WINGs/WINGsP.h>
19 * Our public header.
21 #include "mywidget.h"
24 * Define the widget "class"
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;
32 /* put your stuff here */
33 char *text;
35 } _MyWidget;
37 /* some forward declarations */
39 static void destroyMyWidget(_MyWidget * mPtr);
40 static void paintMyWidget(_MyWidget * mPtr);
42 static void handleEvents(XEvent * event, void *data);
43 static void handleActionEvents(XEvent * event, void *data);
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.
50 static W_ViewDelegate _MyWidgetDelegate = {
51 NULL,
52 NULL,
53 NULL,
54 NULL,
55 NULL
58 /* our widget class ID */
59 static W_Class myWidgetClass = 0;
62 * Initializer for our widget. Must be called before creating any
63 * instances of the widget.
65 W_Class InitMyWidget(WMScreen * scr)
67 /* register our widget with WINGs and get our widget class ID */
68 if (!myWidgetClass) {
69 myWidgetClass = W_RegisterUserWidget();
72 return myWidgetClass;
76 * Our widget fabrication plant.
78 MyWidget *CreateMyWidget(WMWidget * parent)
80 MyWidget *mPtr;
82 /* allocate some storage for our new widget instance */
83 mPtr = wmalloc(sizeof(MyWidget));
84 /* initialize it */
85 memset(mPtr, 0, sizeof(MyWidget));
87 /* set the class ID */
88 mPtr->widgetClass = myWidgetClass;
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()
95 * Consider the returned view as read-only.
97 mPtr->view = W_CreateView(W_VIEW(parent));
98 if (!mPtr->view) {
99 wfree(mPtr);
100 return NULL;
102 /* always do this */
103 mPtr->view->self = mPtr;
105 /* setup the delegates for the view */
106 mPtr->view->delegate = &_MyWidgetDelegate;
109 * Intercept some events for our widget, so that we can handle them.
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);
116 * Intercept some other events. This could be merged with the above
117 * call, but we separate for more organization.
119 WMCreateEventHandler(mPtr->view, ButtonPressMask, handleActionEvents, mPtr);
121 return mPtr;
125 * Paint our widget contents.
127 static void paintMyWidget(_MyWidget * mPtr)
129 W_Screen *scr = mPtr->view->screen;
130 WMColor *color;
132 if (mPtr->text) {
134 color = WMWhiteColor(scr);
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));
139 WMReleaseColor(color);
143 static void handleEvents(XEvent * event, void *data)
145 _MyWidget *mPtr = (_MyWidget *) data;
147 switch (event->type) {
148 case Expose:
149 if (event->xexpose.count != 0)
150 break;
151 paintMyWidget(mPtr);
152 break;
154 case DestroyNotify:
155 destroyMyWidget(mPtr);
156 break;
161 static void handleActionEvents(XEvent * event, void *data)
163 _MyWidget *mPtr = (_MyWidget *) data;
165 switch (event->type) {
166 case ButtonPress:
167 XBell(mPtr->view->screen->display, 100);
168 XBell(mPtr->view->screen->display, 100);
169 break;
173 void SetMyWidgetText(MyWidget * mPtr, char *text)
175 CHECK_CLASS(mPtr, myWidgetClass);
177 if (mPtr->text)
178 wfree(mPtr->text);
180 mPtr->text = wstrdup(text);
182 if (W_VIEW_MAPPED(mPtr->view)) {
183 paintMyWidget(mPtr);
187 static void destroyMyWidget(_MyWidget * mPtr)
190 * Free all data we allocated for our widget.
193 if (mPtr->text)
194 wfree(mPtr->text);
196 wfree(mPtr);