2 * Demo user widget for WINGs
4 * Author: Alfredo K. Kojima
6 * This file is in the public domain.
12 * Include the WINGs private data header.
16 #include <WINGs/WINGsP.h>
24 * Define the widget "class"
26 typedef struct W_MyWidget
{
27 /* these two fields must be present in all your widgets in this
32 /* put your stuff here */
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
);
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
= {
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
)
68 /* register our widget with WINGs and get our widget class ID */
70 myWidgetClass
= W_RegisterUserWidget();
77 * Our widget fabrication plant.
79 MyWidget
*CreateMyWidget(WMWidget
* parent
)
83 /* allocate some storage for our new widget instance */
84 mPtr
= wmalloc(sizeof(MyWidget
));
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
));
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 */
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
);
126 * Paint our widget contents.
128 static void paintMyWidget(_MyWidget
* mPtr
)
130 W_Screen
*scr
= mPtr
->view
->screen
;
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
) {
150 if (event
->xexpose
.count
!= 0)
156 destroyMyWidget(mPtr
);
162 static void handleActionEvents(XEvent
* event
, void *data
)
164 _MyWidget
*mPtr
= (_MyWidget
*) data
;
166 switch (event
->type
) {
168 XBell(mPtr
->view
->screen
->display
, 100);
169 XBell(mPtr
->view
->screen
->display
, 100);
174 void SetMyWidgetText(MyWidget
* mPtr
, char *text
)
176 CHECK_CLASS(mPtr
, myWidgetClass
);
181 mPtr
->text
= wstrdup(text
);
183 if (W_VIEW_MAPPED(mPtr
->view
)) {
188 static void destroyMyWidget(_MyWidget
* mPtr
)
191 * Free all data we allocated for our widget.