Initial revision
[wmaker-crm.git] / WINGs / mywidget.c
blobb7f1c25008d6708b6bbc906356ab76bb92c6f7ef
1 /*
2 * Demo user widget for WINGs.
4 *
5 * Copyright (c) 1998 Alfredo K. Kojima
6 */
9 /*
11 * Include the WINGs private data header.
15 #include "WINGsP.h"
18 * Our public header.
20 #include "mywidget.h"
23 * Define the widget "class"
25 typedef struct W_MyWidget {
26 /* these two fields must be present in all your widgets in this
27 * exact position */
28 W_Class widgetClass;
29 WMView *view;
31 /* put your stuff here */
32 char *text;
34 } _MyWidget;
39 /* some forward declarations */
41 static void destroyMyWidget(_MyWidget *mPtr);
42 static void paintMyWidget(_MyWidget *mPtr);
45 static void handleEvents(XEvent *event, void *data);
46 static void handleActionEvents(XEvent *event, void *data);
51 * Some procedures you might want to override. Don't forget to call
52 * the equivalent view procedure after (or before) doing your stuff.
53 * See the source for the other widgets to see how to use.
54 * You won't need to use this most of the time.
56 static W_ViewProcedureTable _MyWidgetViewProcedures = {
57 NULL,
58 NULL,
59 NULL
63 /* our widget class ID */
64 static W_Class myWidgetClass = 0;
68 * Initializer for our widget. Must be called before creating any
69 * instances of the widget.
71 W_Class
72 InitMyWidget(WMScreen *scr)
74 /* register our widget with WINGs and get our widget class ID */
75 if (!myWidgetClass) {
76 myWidgetClass = W_RegisterUserWidget(&_MyWidgetViewProcedures);
79 return myWidgetClass;
84 * Our widget fabrication plant.
86 MyWidget*
87 CreateMyWidget(WMWidget *parent)
89 MyWidget *mPtr;
91 /* allocate some storage for our new widget instance */
92 mPtr = wmalloc(sizeof(MyWidget));
93 /* initialize it */
94 memset(mPtr, 0, sizeof(MyWidget));
96 /* set the class ID */
97 mPtr->widgetClass = myWidgetClass;
99 /*
100 * Create the view for our widget.
101 * Note: the Window for the view is only created after the view is
102 * realized with W_RealizeView()
104 * Consider the returned view as read-only.
106 mPtr->view = W_CreateView(W_VIEW(parent));
107 if (!mPtr->view) {
108 free(mPtr);
109 return NULL;
111 /* always do this */
112 mPtr->view->self = mPtr;
115 * Intercept some events for our widget, so that we can handle them.
117 WMCreateEventHandler(mPtr->view, ExposureMask /* this allows us to know when we should paint */
118 |StructureNotifyMask, /* this allows us to know things like when we are destroyed */
119 handleEvents, mPtr);
122 * Intercept some other events. This could be merged with the above
123 * call, but we separate for more organization.
125 WMCreateEventHandler(mPtr->view, ButtonPressMask,handleActionEvents, mPtr);
127 return mPtr;
133 * Paint our widget contents.
135 static void
136 paintMyWidget(_MyWidget *mPtr)
138 W_Screen *scr = mPtr->view->screen;
139 WMColor *color;
142 if (mPtr->text) {
144 color = WMWhiteColor(scr);
146 W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
147 mPtr->view->size.width, WACenter, W_GC(color),
148 False, mPtr->text, strlen(mPtr->text));
150 WMReleaseColor(color);
156 static void
157 handleEvents(XEvent *event, void *data)
159 _MyWidget *mPtr = (_MyWidget*)data;
162 switch (event->type) {
163 case Expose:
164 if (event->xexpose.count!=0)
165 break;
166 paintMyWidget(mPtr);
167 break;
169 case DestroyNotify:
170 destroyMyWidget(mPtr);
171 break;
177 static void
178 handleActionEvents(XEvent *event, void *data)
180 _MyWidget *mPtr = (_MyWidget*)data;
182 switch (event->type) {
183 case ButtonPress:
184 XBell(mPtr->view->screen->display, 100);
185 XBell(mPtr->view->screen->display, 100);
186 break;
191 void
192 SetMyWidgetText(MyWidget *mPtr, char *text)
194 CHECK_CLASS(mPtr, myWidgetClass);
196 if (mPtr->text)
197 free(mPtr->text);
199 mPtr->text = wstrdup(text);
201 if (W_VIEW_MAPPED(mPtr->view)) {
202 paintMyWidget(mPtr);
208 static void
209 destroyMyWidget(_MyWidget *mPtr)
212 * Free all data we allocated for our widget.
215 if (mPtr->text)
216 free(mPtr->text);
218 free(mPtr);