2
* Demo user widget for WINGs
4
* Author: Alfredo K. Kojima
6
* This file is in the public domain.
13
* Include the WINGs private data header.
17
#include <WINGs/WINGsP.h>
25
* Define the widget "class"
27
typedef struct W_MyWidget {
28
/* these two fields must be present in all your widgets in this
33
/* put your stuff here */
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);
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 = {
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.
75
InitMyWidget(WMScreen *scr)
77
/* register our widget with WINGs and get our widget class ID */
79
myWidgetClass = W_RegisterUserWidget();
87
* Our widget fabrication plant.
90
CreateMyWidget(WMWidget *parent)
94
/* allocate some storage for our new widget instance */
95
mPtr = wmalloc(sizeof(MyWidget));
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));
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 */
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);
139
* Paint our widget contents.
142
paintMyWidget(_MyWidget *mPtr)
144
W_Screen *scr = mPtr->view->screen;
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);
163
handleEvents(XEvent *event, void *data)
165
_MyWidget *mPtr = (_MyWidget*)data;
168
switch (event->type) {
170
if (event->xexpose.count!=0)
176
destroyMyWidget(mPtr);
184
handleActionEvents(XEvent *event, void *data)
186
_MyWidget *mPtr = (_MyWidget*)data;
188
switch (event->type) {
190
XBell(mPtr->view->screen->display, 100);
191
XBell(mPtr->view->screen->display, 100);
198
SetMyWidgetText(MyWidget *mPtr, char *text)
200
CHECK_CLASS(mPtr, myWidgetClass);
205
mPtr->text = wstrdup(text);
207
if (W_VIEW_MAPPED(mPtr->view)) {
215
destroyMyWidget(_MyWidget *mPtr)
218
* Free all data we allocated for our widget.