show workspace name when changing workspace
[wmaker-crm.git] / WINGs / mywidget.c
blob9ea732f703a017bb2d4b80f156c0c6c41ecbc558
1 /*
2 * Demo user widget for WINGs
4 * Author: Alfredo K. Kojima
5 *
6 * This file is in the public domain.
7 *
8 */
13 * Include the WINGs private data header.
17 #include "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 * Some procedures you might want to override. Don't forget to call
54 * the equivalent view procedure after (or before) doing your stuff.
55 * See the source for the other widgets to see how to use.
56 * You won't need to use this most of the time.
58 static W_ViewProcedureTable _MyWidgetViewProcedures = {
59 NULL,
60 NULL,
61 NULL
65 /* our widget class ID */
66 static W_Class myWidgetClass = 0;
70 * Initializer for our widget. Must be called before creating any
71 * instances of the widget.
73 W_Class
74 InitMyWidget(WMScreen *scr)
76 /* register our widget with WINGs and get our widget class ID */
77 if (!myWidgetClass) {
78 myWidgetClass = W_RegisterUserWidget(&_MyWidgetViewProcedures);
81 return myWidgetClass;
86 * Our widget fabrication plant.
88 MyWidget*
89 CreateMyWidget(WMWidget *parent)
91 MyWidget *mPtr;
93 /* allocate some storage for our new widget instance */
94 mPtr = wmalloc(sizeof(MyWidget));
95 /* initialize it */
96 memset(mPtr, 0, sizeof(MyWidget));
98 /* set the class ID */
99 mPtr->widgetClass = myWidgetClass;
102 * Create the view for our widget.
103 * Note: the Window for the view is only created after the view is
104 * realized with W_RealizeView()
106 * Consider the returned view as read-only.
108 mPtr->view = W_CreateView(W_VIEW(parent));
109 if (!mPtr->view) {
110 free(mPtr);
111 return NULL;
113 /* always do this */
114 mPtr->view->self = mPtr;
117 * Intercept some events for our widget, so that we can handle them.
119 WMCreateEventHandler(mPtr->view, ExposureMask /* this allows us to know when we should paint */
120 |StructureNotifyMask, /* this allows us to know things like when we are destroyed */
121 handleEvents, mPtr);
124 * Intercept some other events. This could be merged with the above
125 * call, but we separate for more organization.
127 WMCreateEventHandler(mPtr->view, ButtonPressMask,handleActionEvents, mPtr);
129 return mPtr;
135 * Paint our widget contents.
137 static void
138 paintMyWidget(_MyWidget *mPtr)
140 W_Screen *scr = mPtr->view->screen;
141 WMColor *color;
144 if (mPtr->text) {
146 color = WMWhiteColor(scr);
148 W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
149 mPtr->view->size.width, WACenter, WMColorGC(color),
150 False, mPtr->text, strlen(mPtr->text));
152 WMReleaseColor(color);
158 static void
159 handleEvents(XEvent *event, void *data)
161 _MyWidget *mPtr = (_MyWidget*)data;
164 switch (event->type) {
165 case Expose:
166 if (event->xexpose.count!=0)
167 break;
168 paintMyWidget(mPtr);
169 break;
171 case DestroyNotify:
172 destroyMyWidget(mPtr);
173 break;
179 static void
180 handleActionEvents(XEvent *event, void *data)
182 _MyWidget *mPtr = (_MyWidget*)data;
184 switch (event->type) {
185 case ButtonPress:
186 XBell(mPtr->view->screen->display, 100);
187 XBell(mPtr->view->screen->display, 100);
188 break;
193 void
194 SetMyWidgetText(MyWidget *mPtr, char *text)
196 CHECK_CLASS(mPtr, myWidgetClass);
198 if (mPtr->text)
199 free(mPtr->text);
201 mPtr->text = wstrdup(text);
203 if (W_VIEW_MAPPED(mPtr->view)) {
204 paintMyWidget(mPtr);
210 static void
211 destroyMyWidget(_MyWidget *mPtr)
214 * Free all data we allocated for our widget.
217 if (mPtr->text)
218 free(mPtr->text);
220 free(mPtr);