File: mywidget.c

package info (click to toggle)
wmaker 0.96.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,332 kB
  • sloc: ansic: 99,482; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (198 lines) | stat: -rw-r--r-- 3,857 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Demo user widget for WINGs
 *
 * Author: Alfredo K. Kojima
 *
 * This file is in the public domain.
 *
 */

/*
 *
 * Include the WINGs private data header.
 *
 *
 */
#include <WINGs/WINGsP.h>

/*
 * Our public header.
 */
#include "mywidget.h"

/*
 * Define the widget "class"
 */
typedef struct W_MyWidget {
	/* these two fields must be present in all your widgets in this
	 * exact position */
	W_Class widgetClass;
	WMView *view;

	/* put your stuff here */
	char *text;

} _MyWidget;

/* some forward declarations */

static void destroyMyWidget(_MyWidget * mPtr);
static void paintMyWidget(_MyWidget * mPtr);

static void handleEvents(XEvent * event, void *data);
static void handleActionEvents(XEvent * event, void *data);

/*
 * Delegates
 * See the source for the other widgets to see how to use.
 * You won't need to use this most of the time.
 */
static W_ViewDelegate _MyWidgetDelegate = {
	NULL,
	NULL,
	NULL,
	NULL,
	NULL
};

/* our widget class ID */
static W_Class myWidgetClass = 0;

/*
 * Initializer for our widget. Must be called before creating any
 * instances of the widget.
 */
W_Class InitMyWidget(WMScreen * scr)
{
	(void)scr;
	/* register our widget with WINGs and get our widget class ID */
	if (!myWidgetClass) {
		myWidgetClass = W_RegisterUserWidget();
	}

	return myWidgetClass;
}

/*
 * Our widget fabrication plant.
 */
MyWidget *CreateMyWidget(WMWidget * parent)
{
	MyWidget *mPtr;

	/* allocate some storage for our new widget instance */
	mPtr = wmalloc(sizeof(MyWidget));
	/* initialize it */
	memset(mPtr, 0, sizeof(MyWidget));

	/* set the class ID */
	mPtr->widgetClass = myWidgetClass;

	/*
	 * Create the view for our widget.
	 * Note: the Window for the view is only created after the view is
	 * realized with W_RealizeView()
	 *
	 * Consider the returned view as read-only.
	 */
	mPtr->view = W_CreateView(W_VIEW(parent));
	if (!mPtr->view) {
		wfree(mPtr);
		return NULL;
	}
	/* always do this */
	mPtr->view->self = mPtr;

	/* setup the delegates for the view */
	mPtr->view->delegate = &_MyWidgetDelegate;

	/*
	 * Intercept some events for our widget, so that we can handle them.
	 */
	WMCreateEventHandler(mPtr->view, ExposureMask	/* this allows us to know when we should paint */
			     | StructureNotifyMask,	/* this allows us to know things like when we are destroyed */
			     handleEvents, mPtr);

	/*
	 * Intercept some other events. This could be merged with the above
	 * call, but we separate for more organization.
	 */
	WMCreateEventHandler(mPtr->view, ButtonPressMask, handleActionEvents, mPtr);

	return mPtr;
}

/*
 * Paint our widget contents.
 */
static void paintMyWidget(_MyWidget * mPtr)
{
	W_Screen *scr = mPtr->view->screen;
	WMColor *color;

	if (mPtr->text) {

		color = WMWhiteColor(scr);

		W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont, 0, 0,
			    mPtr->view->size.width, WACenter, color, False, mPtr->text, strlen(mPtr->text));

		WMReleaseColor(color);
	}
}

static void handleEvents(XEvent * event, void *data)
{
	_MyWidget *mPtr = (_MyWidget *) data;

	switch (event->type) {
	case Expose:
		if (event->xexpose.count != 0)
			break;
		paintMyWidget(mPtr);
		break;

	case DestroyNotify:
		destroyMyWidget(mPtr);
		break;

	}
}

static void handleActionEvents(XEvent * event, void *data)
{
	_MyWidget *mPtr = (_MyWidget *) data;

	switch (event->type) {
	case ButtonPress:
		XBell(mPtr->view->screen->display, 100);
		XBell(mPtr->view->screen->display, 100);
		break;
	}
}

void SetMyWidgetText(MyWidget * mPtr, char *text)
{
	CHECK_CLASS(mPtr, myWidgetClass);

	if (mPtr->text)
		wfree(mPtr->text);

	mPtr->text = wstrdup(text);

	if (W_VIEW_MAPPED(mPtr->view)) {
		paintMyWidget(mPtr);
	}
}

static void destroyMyWidget(_MyWidget * mPtr)
{
	/*
	 * Free all data we allocated for our widget.
	 */

	if (mPtr->text)
		wfree(mPtr->text);

	wfree(mPtr);
}