forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snis_ui_element.c
215 lines (187 loc) · 4.54 KB
/
snis_ui_element.c
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
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#define DEFINE_UI_ELEMENT_GLOBALS
#include "snis_ui_element.h"
struct ui_element {
void *element;
int active_displaymode;
volatile int *displaymode;
ui_element_drawing_function draw;
ui_element_button_press_function button_press;
ui_element_set_focus_function set_focus;
int has_focus;
ui_element_keypress_function keypress_fn, keyrelease_fn;
};
struct ui_element_list {
struct ui_element *element;
struct ui_element_list *next;
};
struct ui_element *ui_element_init(void *element,
ui_element_drawing_function draw,
ui_element_button_press_function button_press,
int active_displaymode, volatile int *displaymode)
{
struct ui_element *e;
e = malloc(sizeof(*e));
e->element = element;
e->draw = draw;
e->button_press = button_press;
e->active_displaymode = active_displaymode;
e->displaymode = displaymode;
e->set_focus = NULL;
e->has_focus = 0;
e->keypress_fn = NULL;
e->keyrelease_fn = NULL;
return e;
}
void ui_element_draw(GtkWidget *w, GdkGC *gc, struct ui_element *element)
{
element->draw(w, gc, element);
}
void ui_element_list_add_element(struct ui_element_list **list,
struct ui_element *element)
{
struct ui_element_list *l;
if (*list == NULL) {
*list = malloc(sizeof(**list));
(*list)->element = element;
(*list)->next = NULL;
return;
}
l = malloc(sizeof(*l));
l->element = element;
l->next = *list;
*list = l;
}
void ui_element_list_free(struct ui_element_list *list)
{
struct ui_element_list *n;
if (list == NULL)
return;
n = list->next;
free(list->element);
free(list);
ui_element_list_free(n);
}
void ui_element_list_draw(GtkWidget *w, GdkGC *gc,
struct ui_element_list *list)
{
for (; list != NULL; list = list->next) {
struct ui_element *e = list->element;
if (e->draw && e->active_displaymode == *e->displaymode)
e->draw(w, gc, e->element);
}
}
static void ui_set_focus(struct ui_element_list *list, struct ui_element *e)
{
if (!e->set_focus)
return;
for (; list != NULL; list = list->next) {
if (!list->element->set_focus)
continue;
if (list->element == e) {
e->set_focus(e->element, 1);
e->has_focus = 1;
} else {
list->element->set_focus(list->element->element, 0);
list->element->has_focus = 0;
}
}
}
void ui_element_list_button_press(struct ui_element_list *list, int x, int y)
{
int hit;
struct ui_element *e;
struct ui_element_list *i;
for (i = list; i != NULL; i = i->next) {
e = i->element;
if (e->button_press && e->active_displaymode == *e->displaymode) {
hit = e->button_press(e->element, x, y);
if (hit) {
ui_set_focus(list, e);
break;
}
}
}
}
void ui_element_set_focus_callback(struct ui_element *e,
ui_element_set_focus_function set_focus)
{
e->set_focus = set_focus;
}
static void advance_focus(struct ui_element_list *list)
{
struct ui_element_list *i;
int found = 0;
int done = 0;
for (i = list; i; i = i->next) {
struct ui_element *e = i->element;
if (e->set_focus && e->has_focus &&
e->active_displaymode == *e->displaymode) {
e->has_focus = 0;
e->set_focus(e->element, 0);
found = 1;
continue;
}
if (found) {
if (e->active_displaymode == *e->displaymode && e->set_focus) {
e->has_focus = 1;
e->set_focus(e->element, 1);
done = 1;
break;
}
}
}
if (done)
return;
/* Start from the beginning of the list */
for (i = list; i; i = i->next) {
struct ui_element *e = i->element;
if (e->active_displaymode == *e->displaymode && e->set_focus) {
e->has_focus = 1;
e->set_focus(e->element, 1);
break;
}
}
}
void ui_element_list_keypress(struct ui_element_list *list, GdkEventKey *event)
{
struct ui_element_list *i;
if (event->type == GDK_KEY_PRESS && (event->keyval & ~0x7f) != 0) {
switch (event->keyval) {
case GDK_KEY_Tab:
advance_focus(list);
return;
default:
break;
}
}
for (i = list; i; i = i->next) {
if (!i->element->has_focus)
continue;
if (!i->element->keypress_fn)
continue;
i->element->keypress_fn(i->element->element, event);
break;
}
}
void ui_element_list_keyrelease(struct ui_element_list *list, GdkEventKey *event)
{
struct ui_element_list *i;
for (i = list; i; i = i->next) {
if (!i->element->has_focus)
continue;
if (!i->element->keyrelease_fn)
continue;
i->element->keyrelease_fn(i->element->element, event);
break;
}
}
void ui_element_get_keystrokes(struct ui_element *e,
ui_element_keypress_function keypress_fn,
ui_element_keypress_function keyrelease_fn)
{
e->keypress_fn = keypress_fn;
e->keyrelease_fn = keyrelease_fn;
}