|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2014 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 |
|
33 #include "context.h" |
|
34 #include "../ui/window.h" |
|
35 |
|
36 void uic_docmgr_init() { |
|
37 |
|
38 } |
|
39 |
|
40 UiContext* uic_context(UiObject *toplevel, UcxMempool *mp) { |
|
41 UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext)); |
|
42 UcxAllocator *allocator = ucx_mempool_allocator(mp); |
|
43 ctx->mempool = mp; |
|
44 ctx->allocator = allocator; |
|
45 |
|
46 ctx->toplevel = toplevel; |
|
47 ctx->vars = ucx_map_new_a(allocator, 16); |
|
48 |
|
49 return ctx; |
|
50 } |
|
51 |
|
52 UiVar* uic_getvar(UiObject *obj, char *name) { |
|
53 return ucx_map_cstr_get(obj->ctx->vars, name); |
|
54 } |
|
55 |
|
56 UiVar* uic_addplaceholder(UiObject *obj, char *name) { |
|
57 UiVar *var = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiVar)); |
|
58 var->type = -1; |
|
59 var->value = NULL; |
|
60 ucx_map_cstr_put(obj->ctx->vars, name, var); |
|
61 } |
|
62 |
|
63 void ui_window_addint(UiObject *obj, char *name) { |
|
64 if(uic_getvar(obj, name)) { |
|
65 // var already exists |
|
66 return; |
|
67 } |
|
68 UiInteger *i = ucx_mempool_calloc(obj->ctx->mempool, 1, sizeof(UiInteger)); |
|
69 ui_window_regint(obj, name, i); |
|
70 } |
|
71 |
|
72 void ui_window_regint(UiObject *obj, char *name, UiInteger *i) { |
|
73 if(uic_getvar(obj, name)) { |
|
74 // var already exists |
|
75 return; |
|
76 } |
|
77 UiVar *var = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiVar)); |
|
78 var->value = i; |
|
79 var->type = 1; |
|
80 ucx_map_cstr_put(obj->ctx->vars, name, var); |
|
81 } |
|
82 |
|
83 void ui_window_setint(UiObject *obj, char *name, int val) { |
|
84 UiVar *var = uic_getvar(obj, name); |
|
85 if(var && var->type == 1) { |
|
86 UiInteger *i = var->value; |
|
87 i->set(i, val); |
|
88 } else { |
|
89 // TODO: error message |
|
90 } |
|
91 } |
|
92 |
|
93 int ui_window_getint(UiObject *obj, char *name) { |
|
94 UiVar *var = uic_getvar(obj, name); |
|
95 if(var && var->type == 1) { |
|
96 UiInteger *i = var->value; |
|
97 return i->get(i); |
|
98 } else { |
|
99 return 0; // TODO: error message |
|
100 } |
|
101 } |