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.
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
);
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
= {
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.
74 InitMyWidget(WMScreen
*scr
)
76 /* register our widget with WINGs and get our widget class ID */
78 myWidgetClass
= W_RegisterUserWidget(&_MyWidgetViewProcedures
);
86 * Our widget fabrication plant.
89 CreateMyWidget(WMWidget
*parent
)
93 /* allocate some storage for our new widget instance */
94 mPtr
= wmalloc(sizeof(MyWidget
));
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
));
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 */
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
);
135 * Paint our widget contents.
138 paintMyWidget(_MyWidget
*mPtr
)
140 W_Screen
*scr
= mPtr
->view
->screen
;
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
);
159 handleEvents(XEvent
*event
, void *data
)
161 _MyWidget
*mPtr
= (_MyWidget
*)data
;
164 switch (event
->type
) {
166 if (event
->xexpose
.count
!=0)
172 destroyMyWidget(mPtr
);
180 handleActionEvents(XEvent
*event
, void *data
)
182 _MyWidget
*mPtr
= (_MyWidget
*)data
;
184 switch (event
->type
) {
186 XBell(mPtr
->view
->screen
->display
, 100);
187 XBell(mPtr
->view
->screen
->display
, 100);
194 SetMyWidgetText(MyWidget
*mPtr
, char *text
)
196 CHECK_CLASS(mPtr
, myWidgetClass
);
201 mPtr
->text
= wstrdup(text
);
203 if (W_VIEW_MAPPED(mPtr
->view
)) {
211 destroyMyWidget(_MyWidget
*mPtr
)
214 * Free all data we allocated for our widget.