Update Serbian translation from master branch
[wmaker-crm.git] / WINGs / Tests / mywidget.c
blob76c963caddca06a7b39e2de8ea8539c60d50f7a2
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 (void)scr;
68 /* register our widget with WINGs and get our widget class ID */
69 if (!myWidgetClass) {
70 myWidgetClass = W_RegisterUserWidget();
73 return myWidgetClass;
77 * Our widget fabrication plant.
79 MyWidget *CreateMyWidget(WMWidget * parent)
81 MyWidget *mPtr;
83 /* allocate some storage for our new widget instance */
84 mPtr = wmalloc(sizeof(MyWidget));
85 /* initialize it */
86 memset(mPtr, 0, sizeof(MyWidget));
88 /* set the class ID */
89 mPtr->widgetClass = myWidgetClass;
92 * Create the view for our widget.
93 * Note: the Window for the view is only created after the view is
94 * realized with W_RealizeView()
96 * Consider the returned view as read-only.
98 mPtr->view = W_CreateView(W_VIEW(parent));
99 if (!mPtr->view) {
100 wfree(mPtr);
101 return NULL;
103 /* always do this */
104 mPtr->view->self = mPtr;
106 /* setup the delegates for the view */
107 mPtr->view->delegate = &_MyWidgetDelegate;
110 * Intercept some events for our widget, so that we can handle them.
112 WMCreateEventHandler(mPtr->view, ExposureMask /* this allows us to know when we should paint */
113 | StructureNotifyMask, /* this allows us to know things like when we are destroyed */
114 handleEvents, mPtr);
117 * Intercept some other events. This could be merged with the above
118 * call, but we separate for more organization.
120 WMCreateEventHandler(mPtr->view, ButtonPressMask, handleActionEvents, mPtr);
122 return mPtr;
126 * Paint our widget contents.
128 static void paintMyWidget(_MyWidget * mPtr)
130 W_Screen *scr = mPtr->view->screen;
131 WMColor *color;
133 if (mPtr->text) {
135 color = WMWhiteColor(scr);
137 W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
138 mPtr->view->size.width, WACenter, color, False, mPtr->text, strlen(mPtr->text));
140 WMReleaseColor(color);
144 static void handleEvents(XEvent * event, void *data)
146 _MyWidget *mPtr = (_MyWidget *) data;
148 switch (event->type) {
149 case Expose:
150 if (event->xexpose.count != 0)
151 break;
152 paintMyWidget(mPtr);
153 break;
155 case DestroyNotify:
156 destroyMyWidget(mPtr);
157 break;
162 static void handleActionEvents(XEvent * event, void *data)
164 _MyWidget *mPtr = (_MyWidget *) data;
166 switch (event->type) {
167 case ButtonPress:
168 XBell(mPtr->view->screen->display, 100);
169 XBell(mPtr->view->screen->display, 100);
170 break;
174 void SetMyWidgetText(MyWidget * mPtr, char *text)
176 CHECK_CLASS(mPtr, myWidgetClass);
178 if (mPtr->text)
179 wfree(mPtr->text);
181 mPtr->text = wstrdup(text);
183 if (W_VIEW_MAPPED(mPtr->view)) {
184 paintMyWidget(mPtr);
188 static void destroyMyWidget(_MyWidget * mPtr)
191 * Free all data we allocated for our widget.
194 if (mPtr->text)
195 wfree(mPtr->text);
197 wfree(mPtr);