~profzoom/ubuntu/quantal/wmaker/bug-1079925

« back to all changes in this revision

Viewing changes to WINGs/Tests/mywidget.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2004-11-10 14:05:30 UTC
  • Revision ID: james.westby@ubuntu.com-20041110140530-qpd66b5lm38x7apk
Tags: upstream-0.91.0
ImportĀ upstreamĀ versionĀ 0.91.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Demo user widget for WINGs
 
3
 *
 
4
 * Author: Alfredo K. Kojima
 
5
 *
 
6
 * This file is in the public domain.
 
7
 *
 
8
 */
 
9
 
 
10
 
 
11
/*
 
12
 *
 
13
 * Include the WINGs private data header.
 
14
 *
 
15
 *
 
16
 */
 
17
#include <WINGs/WINGsP.h>
 
18
 
 
19
/*
 
20
 * Our public header.
 
21
 */
 
22
#include "mywidget.h"
 
23
 
 
24
/*
 
25
 * Define the widget "class"
 
26
 */
 
27
typedef struct W_MyWidget {
 
28
    /* these two fields must be present in all your widgets in this
 
29
     * exact position */
 
30
    W_Class widgetClass;
 
31
    WMView *view;
 
32
 
 
33
    /* put your stuff here */
 
34
    char *text;
 
35
 
 
36
} _MyWidget;
 
37
 
 
38
 
 
39
 
 
40
 
 
41
/* some forward declarations */
 
42
 
 
43
static void destroyMyWidget(_MyWidget *mPtr);
 
44
static void paintMyWidget(_MyWidget *mPtr);
 
45
 
 
46
 
 
47
static void handleEvents(XEvent *event, void *data);
 
48
static void handleActionEvents(XEvent *event, void *data);
 
49
 
 
50
 
 
51
 
 
52
/*
 
53
 * Delegates
 
54
 * See the source for the other widgets to see how to use.
 
55
 * You won't need to use this most of the time.
 
56
 */
 
57
static W_ViewDelegate _MyWidgetDelegate = {
 
58
    NULL,
 
59
    NULL,
 
60
    NULL,
 
61
    NULL,
 
62
    NULL
 
63
};
 
64
 
 
65
 
 
66
/* our widget class ID */
 
67
static W_Class myWidgetClass = 0;
 
68
 
 
69
 
 
70
/*
 
71
 * Initializer for our widget. Must be called before creating any
 
72
 * instances of the widget.
 
73
 */
 
74
W_Class
 
75
InitMyWidget(WMScreen *scr)
 
76
{
 
77
    /* register our widget with WINGs and get our widget class ID */
 
78
    if (!myWidgetClass) {
 
79
        myWidgetClass = W_RegisterUserWidget();
 
80
    }
 
81
 
 
82
    return myWidgetClass;
 
83
}
 
84
 
 
85
 
 
86
/*
 
87
 * Our widget fabrication plant.
 
88
 */
 
89
MyWidget*
 
90
CreateMyWidget(WMWidget *parent)
 
91
{
 
92
    MyWidget *mPtr;
 
93
 
 
94
    /* allocate some storage for our new widget instance */
 
95
    mPtr = wmalloc(sizeof(MyWidget));
 
96
    /* initialize it */
 
97
    memset(mPtr, 0, sizeof(MyWidget));
 
98
 
 
99
    /* set the class ID */
 
100
    mPtr->widgetClass = myWidgetClass;
 
101
 
 
102
    /*
 
103
     * Create the view for our widget.
 
104
     * Note: the Window for the view is only created after the view is
 
105
     * realized with W_RealizeView()
 
106
     *
 
107
     * Consider the returned view as read-only.
 
108
     */
 
109
    mPtr->view = W_CreateView(W_VIEW(parent));
 
110
    if (!mPtr->view) {
 
111
        wfree(mPtr);
 
112
        return NULL;
 
113
    }
 
114
    /* always do this */
 
115
    mPtr->view->self = mPtr;
 
116
 
 
117
    /* setup the delegates for the view */
 
118
    mPtr->view->delegate = &_MyWidgetDelegate;
 
119
 
 
120
    /*
 
121
     * Intercept some events for our widget, so that we can handle them.
 
122
     */
 
123
    WMCreateEventHandler(mPtr->view, ExposureMask /* this allows us to know when we should paint */
 
124
                         |StructureNotifyMask, /* this allows us to know things like when we are destroyed */
 
125
                         handleEvents, mPtr);
 
126
 
 
127
    /*
 
128
     * Intercept some other events. This could be merged with the above
 
129
     * call, but we separate for more organization.
 
130
     */
 
131
    WMCreateEventHandler(mPtr->view, ButtonPressMask,handleActionEvents, mPtr);
 
132
 
 
133
    return mPtr;
 
134
}
 
135
 
 
136
 
 
137
 
 
138
/*
 
139
 * Paint our widget contents.
 
140
 */
 
141
static void
 
142
paintMyWidget(_MyWidget *mPtr)
 
143
{
 
144
    W_Screen *scr = mPtr->view->screen;
 
145
    WMColor *color;
 
146
 
 
147
 
 
148
    if (mPtr->text) {
 
149
 
 
150
        color = WMWhiteColor(scr);
 
151
 
 
152
        W_PaintText(mPtr->view, mPtr->view->window, scr->normalFont,  0, 0,
 
153
                    mPtr->view->size.width, WACenter, color,
 
154
                    False, mPtr->text, strlen(mPtr->text));
 
155
 
 
156
        WMReleaseColor(color);
 
157
    }
 
158
}
 
159
 
 
160
 
 
161
 
 
162
static void
 
163
handleEvents(XEvent *event, void *data)
 
164
{
 
165
    _MyWidget *mPtr = (_MyWidget*)data;
 
166
 
 
167
 
 
168
    switch (event->type) {
 
169
    case Expose:
 
170
        if (event->xexpose.count!=0)
 
171
            break;
 
172
        paintMyWidget(mPtr);
 
173
        break;
 
174
 
 
175
    case DestroyNotify:
 
176
        destroyMyWidget(mPtr);
 
177
        break;
 
178
 
 
179
    }
 
180
}
 
181
 
 
182
 
 
183
static void
 
184
handleActionEvents(XEvent *event, void *data)
 
185
{
 
186
    _MyWidget *mPtr = (_MyWidget*)data;
 
187
 
 
188
    switch (event->type) {
 
189
    case ButtonPress:
 
190
        XBell(mPtr->view->screen->display, 100);
 
191
        XBell(mPtr->view->screen->display, 100);
 
192
        break;
 
193
    }
 
194
}
 
195
 
 
196
 
 
197
void
 
198
SetMyWidgetText(MyWidget *mPtr, char *text)
 
199
{
 
200
    CHECK_CLASS(mPtr, myWidgetClass);
 
201
 
 
202
    if (mPtr->text)
 
203
        wfree(mPtr->text);
 
204
 
 
205
    mPtr->text = wstrdup(text);
 
206
 
 
207
    if (W_VIEW_MAPPED(mPtr->view)) {
 
208
        paintMyWidget(mPtr);
 
209
    }
 
210
}
 
211
 
 
212
 
 
213
 
 
214
static void
 
215
destroyMyWidget(_MyWidget *mPtr)
 
216
{
 
217
    /*
 
218
     * Free all data we allocated for our widget.
 
219
     */
 
220
 
 
221
    if (mPtr->text)
 
222
        wfree(mPtr->text);
 
223
 
 
224
    wfree(mPtr);
 
225
}
 
226
 
 
227