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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
//
// Copyright(C) 2005-2014 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
#include <stdlib.h>
#include <string.h>
#include "txt_io.h"
#include "txt_widget.h"
#include "txt_gui.h"
#include "txt_desktop.h"
typedef struct
{
char *signal_name;
TxtWidgetSignalFunc func;
void *user_data;
} txt_callback_t;
struct txt_callback_table_s
{
int refcount;
txt_callback_t *callbacks;
int num_callbacks;
};
txt_callback_table_t *TXT_NewCallbackTable(void)
{
txt_callback_table_t *table;
table = malloc(sizeof(txt_callback_table_t));
table->callbacks = NULL;
table->num_callbacks = 0;
table->refcount = 1;
return table;
}
void TXT_RefCallbackTable(txt_callback_table_t *table)
{
++table->refcount;
}
void TXT_UnrefCallbackTable(txt_callback_table_t *table)
{
int i;
--table->refcount;
if (table->refcount == 0)
{
// No more references to this table
for (i=0; i<table->num_callbacks; ++i)
{
free(table->callbacks[i].signal_name);
}
free(table->callbacks);
free(table);
}
}
void TXT_InitWidget(TXT_UNCAST_ARG(widget), txt_widget_class_t *widget_class)
{
TXT_CAST_ARG(txt_widget_t, widget);
widget->widget_class = widget_class;
widget->callback_table = TXT_NewCallbackTable();
widget->parent = NULL;
// Not focused until we hear otherwise.
widget->focused = 0;
// Visible by default.
widget->visible = 1;
// Align left by default
widget->align = TXT_HORIZ_LEFT;
}
void TXT_SignalConnect(TXT_UNCAST_ARG(widget),
const char *signal_name,
TxtWidgetSignalFunc func,
void *user_data)
{
TXT_CAST_ARG(txt_widget_t, widget);
txt_callback_table_t *table;
txt_callback_t *callback;
table = widget->callback_table;
// Add a new callback to the table
table->callbacks
= realloc(table->callbacks,
sizeof(txt_callback_t) * (table->num_callbacks + 1));
callback = &table->callbacks[table->num_callbacks];
++table->num_callbacks;
callback->signal_name = strdup(signal_name);
callback->func = func;
callback->user_data = user_data;
}
void TXT_EmitSignal(TXT_UNCAST_ARG(widget), const char *signal_name)
{
TXT_CAST_ARG(txt_widget_t, widget);
txt_callback_table_t *table;
int i;
table = widget->callback_table;
// Don't destroy the table while we're searching through it
// (one of the callbacks may destroy this window)
TXT_RefCallbackTable(table);
// Search the table for all callbacks with this name and invoke
// the functions.
for (i=0; i<table->num_callbacks; ++i)
{
if (!strcmp(table->callbacks[i].signal_name, signal_name))
{
table->callbacks[i].func(widget, table->callbacks[i].user_data);
}
}
// Finished using the table
TXT_UnrefCallbackTable(table);
}
void TXT_CalcWidgetSize(TXT_UNCAST_ARG(widget))
{
TXT_CAST_ARG(txt_widget_t, widget);
widget->widget_class->size_calc(widget);
}
void TXT_DrawWidget(TXT_UNCAST_ARG(widget))
{
TXT_CAST_ARG(txt_widget_t, widget);
txt_saved_colors_t colors;
// The drawing function might change the fg/bg colors,
// so make sure we restore them after it's done.
TXT_SaveColors(&colors);
// For convenience...
TXT_GotoXY(widget->x, widget->y);
// Call drawer method
widget->widget_class->drawer(widget);
TXT_RestoreColors(&colors);
}
void TXT_DestroyWidget(TXT_UNCAST_ARG(widget))
{
TXT_CAST_ARG(txt_widget_t, widget);
widget->widget_class->destructor(widget);
TXT_UnrefCallbackTable(widget->callback_table);
free(widget);
}
int TXT_WidgetKeyPress(TXT_UNCAST_ARG(widget), int key)
{
TXT_CAST_ARG(txt_widget_t, widget);
if (widget->widget_class->key_press != NULL)
{
return widget->widget_class->key_press(widget, key);
}
return 0;
}
void TXT_SetWidgetFocus(TXT_UNCAST_ARG(widget), int focused)
{
TXT_CAST_ARG(txt_widget_t, widget);
if (widget == NULL)
{
return;
}
if (widget->focused != focused)
{
widget->focused = focused;
if (widget->widget_class->focus_change != NULL)
{
widget->widget_class->focus_change(widget, focused);
}
}
}
void TXT_SetWidgetAlign(TXT_UNCAST_ARG(widget), txt_horiz_align_t horiz_align)
{
TXT_CAST_ARG(txt_widget_t, widget);
widget->align = horiz_align;
}
void TXT_WidgetMousePress(TXT_UNCAST_ARG(widget), int x, int y, int b)
{
TXT_CAST_ARG(txt_widget_t, widget);
if (widget->widget_class->mouse_press != NULL)
{
widget->widget_class->mouse_press(widget, x, y, b);
}
}
void TXT_LayoutWidget(TXT_UNCAST_ARG(widget))
{
TXT_CAST_ARG(txt_widget_t, widget);
if (widget->widget_class->layout != NULL)
{
widget->widget_class->layout(widget);
}
}
int TXT_AlwaysSelectable(TXT_UNCAST_ARG(widget))
{
return 1;
}
int TXT_NeverSelectable(TXT_UNCAST_ARG(widget))
{
return 0;
}
int TXT_SelectableWidget(TXT_UNCAST_ARG(widget))
{
TXT_CAST_ARG(txt_widget_t, widget);
if (widget->widget_class->selectable != NULL)
{
return widget->widget_class->selectable(widget);
}
else
{
return 0;
}
}
int TXT_ContainsWidget(TXT_UNCAST_ARG(haystack), TXT_UNCAST_ARG(needle))
{
TXT_CAST_ARG(txt_widget_t, haystack);
TXT_CAST_ARG(txt_widget_t, needle);
while (needle != NULL)
{
if (needle == haystack)
{
return 1;
}
needle = needle->parent;
}
return 0;
}
int TXT_HoveringOverWidget(TXT_UNCAST_ARG(widget))
{
TXT_CAST_ARG(txt_widget_t, widget);
txt_window_t *active_window;
int x, y;
// We can only be hovering over widgets in the active window.
active_window = TXT_GetActiveWindow();
if (active_window == NULL || !TXT_ContainsWidget(active_window, widget))
{
return 0;
}
// Is the mouse cursor within the bounds of the widget?
TXT_GetMousePosition(&x, &y);
return (x >= widget->x && x < widget->x + widget->w
&& y >= widget->y && y < widget->y + widget->h);
}
void TXT_SetWidgetBG(TXT_UNCAST_ARG(widget))
{
TXT_CAST_ARG(txt_widget_t, widget);
if (widget->focused)
{
TXT_BGColor(TXT_COLOR_GREY, 0);
}
else if (TXT_HoveringOverWidget(widget))
{
TXT_BGColor(TXT_HOVER_BACKGROUND, 0);
}
else
{
// Use normal window background.
}
}
|