Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / Tests / mywidget.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/*
59eb9475 dan1998-11-03 12:53:26 +00002 * Demo user widget for WINGs
9d2e6ef9 scottc1998-09-29 22:36:29 +00003 *
59eb9475 dan1998-11-03 12:53:26 +00004 * Author: Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00005 *
59eb9475 dan1998-11-03 12:53:26 +00006 * This file is in the public domain.
6830b057 dan2004-10-12 21:28:27 +00007 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00008 */
9
9d2e6ef9 scottc1998-09-29 22:36:29 +000010/*
11 *
12 * Include the WINGs private data header.
13 *
6830b057 dan2004-10-12 21:28:27 +000014 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000015 */
a20aebde dan2001-01-18 19:21:56 +000016#include <WINGs/WINGsP.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000017
18/*
19 * Our public header.
20 */
21#include "mywidget.h"
22
23/*
24 * Define the widget "class"
25 */
26typedef struct W_MyWidget {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020027 /* these two fields must be present in all your widgets in this
28 * exact position */
29 W_Class widgetClass;
30 WMView *view;
6830b057 dan2004-10-12 21:28:27 +000031
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020032 /* put your stuff here */
33 char *text;
6830b057 dan2004-10-12 21:28:27 +000034
9d2e6ef9 scottc1998-09-29 22:36:29 +000035} _MyWidget;
36
9d2e6ef9 scottc1998-09-29 22:36:29 +000037/* some forward declarations */
38
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020039static void destroyMyWidget(_MyWidget * mPtr);
40static void paintMyWidget(_MyWidget * mPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +000041
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020042static void handleEvents(XEvent * event, void *data);
43static void handleActionEvents(XEvent * event, void *data);
9d2e6ef9 scottc1998-09-29 22:36:29 +000044
45/*
6830b057 dan2004-10-12 21:28:27 +000046 * Delegates
9d2e6ef9 scottc1998-09-29 22:36:29 +000047 * See the source for the other widgets to see how to use.
48 * You won't need to use this most of the time.
49 */
5e4625da kojima1999-05-29 21:41:25 +000050static W_ViewDelegate _MyWidgetDelegate = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020051 NULL,
52 NULL,
53 NULL,
54 NULL,
55 NULL
9d2e6ef9 scottc1998-09-29 22:36:29 +000056};
57
9d2e6ef9 scottc1998-09-29 22:36:29 +000058/* our widget class ID */
59static W_Class myWidgetClass = 0;
60
9d2e6ef9 scottc1998-09-29 22:36:29 +000061/*
62 * Initializer for our widget. Must be called before creating any
63 * instances of the widget.
64 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020065W_Class InitMyWidget(WMScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +000066{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020067 /* register our widget with WINGs and get our widget class ID */
68 if (!myWidgetClass) {
69 myWidgetClass = W_RegisterUserWidget();
70 }
6830b057 dan2004-10-12 21:28:27 +000071
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020072 return myWidgetClass;
9d2e6ef9 scottc1998-09-29 22:36:29 +000073}
74
9d2e6ef9 scottc1998-09-29 22:36:29 +000075/*
76 * Our widget fabrication plant.
77 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020078MyWidget *CreateMyWidget(WMWidget * parent)
9d2e6ef9 scottc1998-09-29 22:36:29 +000079{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020080 MyWidget *mPtr;
81
82 /* allocate some storage for our new widget instance */
83 mPtr = wmalloc(sizeof(MyWidget));
84 /* initialize it */
85 memset(mPtr, 0, sizeof(MyWidget));
86
87 /* set the class ID */
88 mPtr->widgetClass = myWidgetClass;
89
90 /*
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()
94 *
95 * Consider the returned view as read-only.
96 */
97 mPtr->view = W_CreateView(W_VIEW(parent));
98 if (!mPtr->view) {
99 wfree(mPtr);
100 return NULL;
101 }
102 /* always do this */
103 mPtr->view->self = mPtr;
104
105 /* setup the delegates for the view */
106 mPtr->view->delegate = &_MyWidgetDelegate;
107
108 /*
109 * Intercept some events for our widget, so that we can handle them.
110 */
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 */
113 handleEvents, mPtr);
114
115 /*
116 * Intercept some other events. This could be merged with the above
117 * call, but we separate for more organization.
118 */
119 WMCreateEventHandler(mPtr->view, ButtonPressMask, handleActionEvents, mPtr);
120
121 return mPtr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000122}
123
9d2e6ef9 scottc1998-09-29 22:36:29 +0000124/*
125 * Paint our widget contents.
126 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200127static void paintMyWidget(_MyWidget * mPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000128{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200129 W_Screen *scr = mPtr->view->screen;
130 WMColor *color;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000131
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200132 if (mPtr->text) {
6830b057 dan2004-10-12 21:28:27 +0000133
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200134 color = WMWhiteColor(scr);
6830b057 dan2004-10-12 21:28:27 +0000135
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200136 W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
137 mPtr->view->size.width, WACenter, color, False, mPtr->text, strlen(mPtr->text));
6830b057 dan2004-10-12 21:28:27 +0000138
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200139 WMReleaseColor(color);
140 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000141}
142
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200143static void handleEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000144{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200145 _MyWidget *mPtr = (_MyWidget *) data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000146
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200147 switch (event->type) {
148 case Expose:
149 if (event->xexpose.count != 0)
150 break;
151 paintMyWidget(mPtr);
152 break;
6830b057 dan2004-10-12 21:28:27 +0000153
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200154 case DestroyNotify:
155 destroyMyWidget(mPtr);
156 break;
6830b057 dan2004-10-12 21:28:27 +0000157
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200158 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000159}
160
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200161static void handleActionEvents(XEvent * event, void *data)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000162{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200163 _MyWidget *mPtr = (_MyWidget *) data;
164
165 switch (event->type) {
166 case ButtonPress:
167 XBell(mPtr->view->screen->display, 100);
168 XBell(mPtr->view->screen->display, 100);
169 break;
170 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000171}
172
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200173void SetMyWidgetText(MyWidget * mPtr, char *text)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000174{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200175 CHECK_CLASS(mPtr, myWidgetClass);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000176
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200177 if (mPtr->text)
178 wfree(mPtr->text);
6830b057 dan2004-10-12 21:28:27 +0000179
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200180 mPtr->text = wstrdup(text);
6830b057 dan2004-10-12 21:28:27 +0000181
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200182 if (W_VIEW_MAPPED(mPtr->view)) {
183 paintMyWidget(mPtr);
184 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000185}
186
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200187static void destroyMyWidget(_MyWidget * mPtr)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000188{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200189 /*
190 * Free all data we allocated for our widget.
191 */
6830b057 dan2004-10-12 21:28:27 +0000192
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200193 if (mPtr->text)
194 wfree(mPtr->text);
6830b057 dan2004-10-12 21:28:27 +0000195
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200196 wfree(mPtr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000197}