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
)
67 /* register our widget with WINGs and get our widget class ID */
69 myWidgetClass
= W_RegisterUserWidget();
76 * Our widget fabrication plant.
78 MyWidget
*CreateMyWidget(WMWidget
* parent
)
82 /* allocate some storage for our new widget instance */
83 mPtr
= wmalloc(sizeof(MyWidget
));
85 memset(mPtr
, 0, sizeof(MyWidget
));
87 /* set the class ID */
88 mPtr
->widgetClass
= myWidgetClass
;
91 * Create the view for our widget.
92 * Note: the Window for the view is only created after the view is
93 * realized with W_RealizeView()
95 * Consider the returned view as read-only.
97 mPtr
->view
= W_CreateView(W_VIEW(parent
));
103 mPtr
->view
->self
= mPtr
;
105 /* setup the delegates for the view */
106 mPtr
->view
->delegate
= &_MyWidgetDelegate
;
109 * Intercept some events for our widget, so that we can handle them.
111 WMCreateEventHandler(mPtr
->view
, ExposureMask
/* this allows us to know when we should paint */
112 | StructureNotifyMask
, /* this allows us to know things like when we are destroyed */
116 * Intercept some other events. This could be merged with the above
117 * call, but we separate for more organization.
119 WMCreateEventHandler(mPtr
->view
, ButtonPressMask
, handleActionEvents
, mPtr
);
125 * Paint our widget contents.
127 static void paintMyWidget(_MyWidget
* mPtr
)
129 W_Screen
*scr
= mPtr
->view
->screen
;
134 color
= WMWhiteColor(scr
);
136 W_PaintText(mPtr
->view
, mPtr
->view
->window
, scr
->normalFont
, 0, 0,
137 mPtr
->view
->size
.width
, WACenter
, color
, False
, mPtr
->text
, strlen(mPtr
->text
));
139 WMReleaseColor(color
);
143 static void handleEvents(XEvent
* event
, void *data
)
145 _MyWidget
*mPtr
= (_MyWidget
*) data
;
147 switch (event
->type
) {
149 if (event
->xexpose
.count
!= 0)
155 destroyMyWidget(mPtr
);
161 static void handleActionEvents(XEvent
* event
, void *data
)
163 _MyWidget
*mPtr
= (_MyWidget
*) data
;
165 switch (event
->type
) {
167 XBell(mPtr
->view
->screen
->display
, 100);
168 XBell(mPtr
->view
->screen
->display
, 100);
173 void SetMyWidgetText(MyWidget
* mPtr
, char *text
)
175 CHECK_CLASS(mPtr
, myWidgetClass
);
180 mPtr
->text
= wstrdup(text
);
182 if (W_VIEW_MAPPED(mPtr
->view
)) {
187 static void destroyMyWidget(_MyWidget
* mPtr
)
190 * Free all data we allocated for our widget.