| |
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 "demo_bindings.h" |
| |
30 |
| |
31 |
| |
32 |
| |
33 Document* document_create(int id) { |
| |
34 Document* doc = ui_document_new(sizeof(Document)); |
| |
35 UiContext *ctx = ui_document_context(doc); |
| |
36 |
| |
37 char buf[32]; |
| |
38 snprintf(buf, 32, "%d", id); |
| |
39 |
| |
40 doc->id = ui_string_new(ctx, "id"); |
| |
41 doc->name = ui_string_new(ctx, "name"); |
| |
42 |
| |
43 doc->input_name = ui_string_new(ctx, "input_name"); |
| |
44 |
| |
45 ui_set(doc->id, buf); |
| |
46 |
| |
47 return doc; |
| |
48 } |
| |
49 |
| |
50 void update_name(UiEvent *event, void *userdata) { |
| |
51 Document *doc = event->document; |
| |
52 if(!doc) { |
| |
53 return; |
| |
54 } |
| |
55 char *s = ui_get(doc->input_name); |
| |
56 ui_set(doc->name, s); |
| |
57 } |
| |
58 |
| |
59 void switch_document(UiEvent *event, void *userdata) { |
| |
60 MainWindow *wdata = event->window; |
| |
61 if(event->document) { |
| |
62 ui_detach_document(event->obj->ctx, event->document); |
| |
63 } |
| |
64 Document *doc = ui_list_get(wdata->doclist, event->intval); |
| |
65 if(doc) { |
| |
66 ui_attach_document(event->obj->ctx, doc); |
| |
67 } |
| |
68 } |
| |
69 |
| |
70 void* doclist_get_value(void *elm, int col) { |
| |
71 Document *doc = elm; |
| |
72 return ui_get(doc->id); |
| |
73 } |
| |
74 |
| |
75 void application_startup(UiEvent *event, void *data) { |
| |
76 UiObject *obj = ui_simple_window("Bindings Demo", NULL); |
| |
77 MainWindow *wdata = ui_malloc(obj->ctx, sizeof(MainWindow)); |
| |
78 obj->window = wdata; |
| |
79 |
| |
80 wdata->doclist = ui_list_new(obj->ctx, "doclist"); |
| |
81 |
| |
82 ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10, .fill = TRUE) { |
| |
83 ui_rlabel(obj, .label = "Document:"); |
| |
84 ui_llabel(obj, .varname = "id"); |
| |
85 ui_newline(obj); |
| |
86 |
| |
87 ui_rlabel(obj, .label = "Name:"); |
| |
88 ui_llabel(obj, .varname = "name"); |
| |
89 ui_newline(obj); |
| |
90 |
| |
91 ui_combobox(obj, .varname = "doclist", .colspan = 2, .onactivate = switch_document, .getvalue = doclist_get_value); |
| |
92 ui_newline(obj); |
| |
93 |
| |
94 ui_frame(obj, .label = "Document", .colspan = 2, .fill = TRUE) { |
| |
95 ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10) { |
| |
96 ui_rlabel(obj, .label = "Name:", .vfill = TRUE); |
| |
97 ui_textfield(obj, .varname = "input_name", .onchange = update_name); |
| |
98 } |
| |
99 } |
| |
100 } |
| |
101 |
| |
102 for(int i=0;i<10;i++) { |
| |
103 Document *doc = document_create(i); |
| |
104 ui_list_append(wdata->doclist, doc); |
| |
105 } |
| |
106 ui_list_update(wdata->doclist); |
| |
107 Document *doc = ui_list_get(wdata->doclist, 0); |
| |
108 ui_attach_document(obj->ctx, doc); |
| |
109 |
| |
110 |
| |
111 ui_show(obj); |
| |
112 } |
| |
113 |
| |
114 |
| |
115 #ifndef UI_WIN32 |
| |
116 |
| |
117 int main(int argc, char **argv) { |
| |
118 ui_init(NULL, argc, argv); |
| |
119 ui_onstartup(application_startup, NULL); |
| |
120 ui_main(); |
| |
121 return 0; |
| |
122 } |
| |
123 |
| |
124 #else |
| |
125 |
| |
126 int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { |
| |
127 return 0; |
| |
128 } |
| |
129 |
| |
130 #endif |