2 * Demo user widget for WINGs.
5 * Copyright (c) 1998 Alfredo K. Kojima
11 * Include the WINGs private data header.
23 * Define the widget "class"
25 typedef struct W_MyWidget
{
26 /* these two fields must be present in all your widgets in this
31 /* put your stuff here */
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
= {
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.
72 InitMyWidget(WMScreen
*scr
)
74 /* register our widget with WINGs and get our widget class ID */
76 myWidgetClass
= W_RegisterUserWidget(&_MyWidgetViewProcedures
);
84 * Our widget fabrication plant.
87 CreateMyWidget(WMWidget
*parent
)
91 /* allocate some storage for our new widget instance */
92 mPtr
= wmalloc(sizeof(MyWidget
));
94 memset(mPtr
, 0, sizeof(MyWidget
));
96 /* set the class ID */
97 mPtr
->widgetClass
= myWidgetClass
;
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
));
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 */
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
);
133 * Paint our widget contents.
136 paintMyWidget(_MyWidget
*mPtr
)
138 W_Screen
*scr
= mPtr
->view
->screen
;
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
);
157 handleEvents(XEvent
*event
, void *data
)
159 _MyWidget
*mPtr
= (_MyWidget
*)data
;
162 switch (event
->type
) {
164 if (event
->xexpose
.count
!=0)
170 destroyMyWidget(mPtr
);
178 handleActionEvents(XEvent
*event
, void *data
)
180 _MyWidget
*mPtr
= (_MyWidget
*)data
;
182 switch (event
->type
) {
184 XBell(mPtr
->view
->screen
->display
, 100);
185 XBell(mPtr
->view
->screen
->display
, 100);
192 SetMyWidgetText(MyWidget
*mPtr
, char *text
)
194 CHECK_CLASS(mPtr
, myWidgetClass
);
199 mPtr
->text
= wstrdup(text
);
201 if (W_VIEW_MAPPED(mPtr
->view
)) {
209 destroyMyWidget(_MyWidget
*mPtr
)
212 * Free all data we allocated for our widget.