|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2017 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <string.h> |
|
32 #include <stdarg.h> |
|
33 |
|
34 #include <ucx/list.h> |
|
35 #include "../ui/tree.h" |
|
36 #include "types.h" |
|
37 #include "context.h" |
|
38 |
|
39 UiObserver* ui_observer_new(ui_callback f, void *data) { |
|
40 UiObserver *observer = malloc(sizeof(UiObserver)); |
|
41 observer->callback = f; |
|
42 observer->data = data; |
|
43 observer->next = NULL; |
|
44 return observer; |
|
45 } |
|
46 |
|
47 UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer) { |
|
48 if(!list) { |
|
49 return observer; |
|
50 } else { |
|
51 UiObserver *l = list; |
|
52 while(l->next) { |
|
53 l = l->next; |
|
54 } |
|
55 l->next = observer; |
|
56 return list; |
|
57 } |
|
58 } |
|
59 |
|
60 UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data) { |
|
61 UiObserver *observer = ui_observer_new(f, data); |
|
62 return ui_obsvlist_add(list, observer); |
|
63 } |
|
64 |
|
65 void ui_notify(UiObserver *observer, void *data) { |
|
66 ui_notify_except(observer, NULL, data); |
|
67 } |
|
68 |
|
69 void ui_notify_except(UiObserver *observer, UiObserver *exc, void *data) { |
|
70 while(observer) { |
|
71 if(observer != exc) { |
|
72 UiEvent evt; |
|
73 evt.obj = NULL; |
|
74 evt.window = NULL; |
|
75 evt.document = NULL; |
|
76 evt.eventdata = data; |
|
77 evt.intval = 0; |
|
78 observer->callback(&evt, observer->data); |
|
79 } |
|
80 observer = observer->next; |
|
81 } |
|
82 } |
|
83 |
|
84 void ui_notify_evt(UiObserver *observer, UiEvent *event) { |
|
85 while(observer) { |
|
86 observer->callback(event, observer->data); |
|
87 observer = observer->next; |
|
88 } |
|
89 } |
|
90 |
|
91 /* --------------------------- UiList --------------------------- */ |
|
92 |
|
93 UiList* ui_list_new(UiContext *ctx, char *name) { |
|
94 UiList *list = malloc(sizeof(UiList)); |
|
95 list->first = ui_list_first; |
|
96 list->next = ui_list_next; |
|
97 list->get = ui_list_get; |
|
98 list->count = ui_list_count; |
|
99 list->observers = NULL; |
|
100 |
|
101 list->data = NULL; |
|
102 list->iter = NULL; |
|
103 |
|
104 list->update = NULL; |
|
105 list->obj = NULL; |
|
106 |
|
107 if(name) { |
|
108 uic_reg_var(ctx, name, UI_VAR_LIST, list); |
|
109 } |
|
110 |
|
111 return list; |
|
112 } |
|
113 |
|
114 void ui_list_free(UiList *list) { |
|
115 ucx_list_free(list->data); |
|
116 free(list); |
|
117 } |
|
118 |
|
119 void* ui_list_first(UiList *list) { |
|
120 UcxList *elm = list->data; |
|
121 list->iter = elm; |
|
122 return elm ? elm->data : NULL; |
|
123 } |
|
124 |
|
125 void* ui_list_next(UiList *list) { |
|
126 UcxList *elm = list->iter; |
|
127 if(elm) { |
|
128 elm = elm->next; |
|
129 if(elm) { |
|
130 list->iter = elm; |
|
131 return elm->data; |
|
132 } |
|
133 } |
|
134 return NULL; |
|
135 } |
|
136 |
|
137 void* ui_list_get(UiList *list, int i) { |
|
138 UcxList *elm = ucx_list_get(list->data, i); |
|
139 if(elm) { |
|
140 list->iter = elm; |
|
141 return elm->data; |
|
142 } else { |
|
143 return NULL; |
|
144 } |
|
145 } |
|
146 |
|
147 int ui_list_count(UiList *list) { |
|
148 UcxList *elm = list->data; |
|
149 return (int)ucx_list_size(elm); |
|
150 } |
|
151 |
|
152 void ui_list_append(UiList *list, void *data) { |
|
153 list->data = ucx_list_append(list->data, data); |
|
154 } |
|
155 |
|
156 void ui_list_prepend(UiList *list, void *data) { |
|
157 list->data = ucx_list_prepend(list->data, data); |
|
158 } |
|
159 |
|
160 void ui_list_clear(UiList *list) { |
|
161 ucx_list_free(list->data); |
|
162 list->data = NULL; |
|
163 } |
|
164 |
|
165 void ui_list_addobsv(UiList *list, ui_callback f, void *data) { |
|
166 list->observers = ui_add_observer(list->observers, f, data); |
|
167 } |
|
168 |
|
169 void ui_list_notify(UiList *list) { |
|
170 ui_notify(list->observers, list); |
|
171 } |
|
172 |
|
173 |
|
174 typedef struct { |
|
175 int type; |
|
176 char *name; |
|
177 } UiColumn; |
|
178 |
|
179 UiModel* ui_model(UiContext *ctx, ...) { |
|
180 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel)); |
|
181 |
|
182 va_list ap; |
|
183 va_start(ap, ctx); |
|
184 |
|
185 UcxList *cols = NULL; |
|
186 int type; |
|
187 while((type = va_arg(ap, int)) != -1) { |
|
188 char *name = va_arg(ap, char*); |
|
189 |
|
190 UiColumn *column = malloc(sizeof(UiColumn)); |
|
191 column->type = type; |
|
192 column->name = name; |
|
193 |
|
194 cols = ucx_list_append(cols, column); |
|
195 } |
|
196 |
|
197 va_end(ap); |
|
198 |
|
199 size_t len = ucx_list_size(cols); |
|
200 info->columns = len; |
|
201 info->types = ui_calloc(ctx, len, sizeof(UiModelType)); |
|
202 info->titles = ui_calloc(ctx, len, sizeof(char*)); |
|
203 |
|
204 int i = 0; |
|
205 UCX_FOREACH(elm, cols) { |
|
206 UiColumn *c = elm->data; |
|
207 info->types[i] = c->type; |
|
208 info->titles[i] = c->name; |
|
209 free(c); |
|
210 i++; |
|
211 } |
|
212 ucx_list_free(cols); |
|
213 |
|
214 return info; |
|
215 } |
|
216 |
|
217 void ui_model_free(UiContext *ctx, UiModel *mi) { |
|
218 ucx_mempool_free(ctx->mempool, mi->types); |
|
219 ucx_mempool_free(ctx->mempool, mi->titles); |
|
220 ucx_mempool_free(ctx->mempool, mi); |
|
221 } |
|
222 |
|
223 // types |
|
224 |
|
225 // public functions |
|
226 UiInteger* ui_int_new(UiContext *ctx, char *name) { |
|
227 UiInteger *i = ui_malloc(ctx, sizeof(UiInteger)); |
|
228 memset(i, 0, sizeof(UiInteger)); |
|
229 if(name) { |
|
230 uic_reg_var(ctx, name, UI_VAR_INTEGER, i); |
|
231 } |
|
232 return i; |
|
233 } |
|
234 |
|
235 UiDouble* ui_double_new(UiContext *ctx, char *name) { |
|
236 UiDouble *d = ui_malloc(ctx, sizeof(UiDouble)); |
|
237 memset(d, 0, sizeof(UiDouble)); |
|
238 if(name) { |
|
239 uic_reg_var(ctx, name, UI_VAR_DOUBLE, d); |
|
240 } |
|
241 return d; |
|
242 } |
|
243 |
|
244 UiString* ui_string_new(UiContext *ctx, char *name) { |
|
245 UiString *s = ui_malloc(ctx, sizeof(UiString)); |
|
246 memset(s, 0, sizeof(UiString)); |
|
247 if(name) { |
|
248 uic_reg_var(ctx, name, UI_VAR_STRING, s); |
|
249 } |
|
250 return s; |
|
251 } |
|
252 |
|
253 UiText* ui_text_new(UiContext *ctx, char *name) { |
|
254 UiText *t = ui_malloc(ctx, sizeof(UiText)); |
|
255 memset(t, 0, sizeof(UiText)); |
|
256 if(name) { |
|
257 uic_reg_var(ctx, name, UI_VAR_TEXT, t); |
|
258 } |
|
259 return t; |
|
260 } |
|
261 |
|
262 UiRange* ui_range_new(UiContext *ctx, char *name) { |
|
263 UiRange *r = ui_malloc(ctx, sizeof(UiRange)); |
|
264 memset(r, 0, sizeof(UiRange)); |
|
265 if(name) { |
|
266 uic_reg_var(ctx, name, UI_VAR_RANGE, r); |
|
267 } |
|
268 return r; |
|
269 } |
|
270 |
|
271 |
|
272 // private functions |
|
273 void uic_int_copy(UiInteger *from, UiInteger *to) { |
|
274 to->get = from->get; |
|
275 to->set = from->set; |
|
276 to->obj = from->obj; |
|
277 } |
|
278 |
|
279 void uic_double_copy(UiDouble *from, UiDouble *to) { |
|
280 to->get = from->get; |
|
281 to->set = from->set; |
|
282 to->obj = from->obj; |
|
283 } |
|
284 |
|
285 void uic_string_copy(UiString *from, UiString *to) { |
|
286 to->get = from->get; |
|
287 to->set = from->set; |
|
288 to->obj = from->obj; |
|
289 } |
|
290 |
|
291 void uic_text_copy(UiText *from, UiText *to) { |
|
292 to->get = from->get; |
|
293 to->set = from->set; |
|
294 to->getsubstr = from->getsubstr; |
|
295 to->insert = from->insert; |
|
296 to->setposition = from->setposition; |
|
297 to->position = from->position; |
|
298 to->selection = from->selection; |
|
299 to->length = from->length; |
|
300 to->remove = from->remove; |
|
301 |
|
302 to->obj = from->obj; |
|
303 // do not copy the undo manager |
|
304 } |
|
305 |
|
306 void uic_range_copy(UiRange *from, UiRange *to) { |
|
307 to->get = from->get; |
|
308 to->set = from->set; |
|
309 to->setrange = from->setrange; |
|
310 to->setextent = from->setextent; |
|
311 to->obj = from->obj; |
|
312 } |
|
313 |
|
314 void uic_list_copy(UiList *from, UiList *to) { |
|
315 to->update = from->update; |
|
316 to->obj = from->obj; |
|
317 } |
|
318 |
|
319 |
|
320 void uic_int_save(UiInteger *i) { |
|
321 if(!i->obj) return; |
|
322 i->value = i->get(i); |
|
323 } |
|
324 |
|
325 void uic_double_save(UiDouble *d) { |
|
326 if(!d->obj) return; |
|
327 d->value = d->get(d); |
|
328 } |
|
329 |
|
330 void uic_string_save(UiString *s) { |
|
331 if(!s->obj) return; |
|
332 s->get(s); |
|
333 } |
|
334 |
|
335 void uic_text_save(UiText *t) { |
|
336 if(!t->obj) return; |
|
337 t->get(t); |
|
338 t->position(t); |
|
339 } |
|
340 |
|
341 void uic_range_save(UiRange *r) { |
|
342 if(!r->obj) return; |
|
343 r->get(r); |
|
344 } |
|
345 |
|
346 |
|
347 void uic_int_unbind(UiInteger *i) { |
|
348 i->get = NULL; |
|
349 i->set = NULL; |
|
350 i->obj = NULL; |
|
351 } |
|
352 |
|
353 void uic_double_unbind(UiDouble *d) { |
|
354 d->get = NULL; |
|
355 d->set = NULL; |
|
356 d->obj = NULL; |
|
357 } |
|
358 |
|
359 void uic_string_unbind(UiString *s) { |
|
360 s->get = NULL; |
|
361 s->set = NULL; |
|
362 s->obj = NULL; |
|
363 } |
|
364 |
|
365 void uic_text_unbind(UiText *t) { |
|
366 t->set = NULL; |
|
367 t->get = NULL; |
|
368 t->getsubstr = NULL; |
|
369 t->insert = NULL; |
|
370 t->setposition = NULL; |
|
371 t->position = NULL; |
|
372 t->selection = NULL; |
|
373 t->length = NULL; |
|
374 t->remove = NULL; |
|
375 t->obj = NULL; |
|
376 t->undomgr = NULL; |
|
377 } |
|
378 |
|
379 void uic_range_unbind(UiRange *r) { |
|
380 r->get = NULL; |
|
381 r->set = NULL; |
|
382 r->setextent = NULL; |
|
383 r->setrange = NULL; |
|
384 r->obj = NULL; |
|
385 } |
|
386 |
|
387 void uic_list_unbind(UiList *l) { |
|
388 l->update = NULL; |
|
389 l->obj = NULL; |
|
390 } |