make/vs/testapp/main.c

branch
newapi
changeset 374
eae5d6623fd3
parent 252
7d176764756d
equal deleted inserted replaced
373:2eede3d98aba 374:eae5d6623fd3
32 #include <stdlib.h> 32 #include <stdlib.h>
33 #include <stdbool.h> 33 #include <stdbool.h>
34 34
35 #include <ui/ui.h> 35 #include <ui/ui.h>
36 36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include <ui/ui.h>
41
42
43 typedef struct {
44 UiString *str1;
45 UiString *str2;
46 UiString *path;
47 UiText *text;
48 UiDouble *progress;
49 UiList *list;
50 UiList *menulist;
51 UiInteger *radio;
52 UiInteger *tabview;
53 UiGeneric *image;
54 } MyDocument;
55
56 MyDocument *doc1;
57 MyDocument *doc2;
58
59 UIWIDGET tabview;
60
61 static UiCondVar *cond;
62 static int thr_end = 0;
63 static int thr_started = 0;
64
65 int threadfunc(void *data) {
66 printf("thr wait for data...\n");
67 ui_condvar_wait(cond);
68 printf("thr data received: {%s} [%d]\n", cond->data, cond->intdata);
69 ui_condvar_destroy(cond);
70 cond = NULL;
71
72 return 0;
73 }
74
75 void action_start_thread(UiEvent *event, void *data) {
76 if(!thr_started) {
77 cond = ui_condvar_create();
78 ui_job(event->obj, threadfunc, NULL, NULL, NULL);
79 thr_started = 1;
80 }
81 }
82
83 void action_notify_thread(UiEvent *event, void *data) {
84 if(!thr_end) {
85 ui_condvar_signal(cond, "hello thread", 123);
86 thr_end = 1;
87 }
88 }
89
90 void action_menu(UiEvent *event, void *userdata) {
91
92 }
93
94 void action_file_selected(UiEvent *event, void *userdata) {
95 UiFileList *files = event->eventdata;
96 MyDocument *doc = event->document;
97 printf("files: %d\n", (int)files->nfiles);
98 if(files->nfiles > 0) {
99 printf("selected file: %s\n", files->files[0]);
100 ui_image_load_file(doc->image, files->files[0]);
101 }
102 }
103
104 void action_button(UiEvent *event, void *userdata) {
105 ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_SINGLE, action_file_selected, NULL);
106 }
107
108 void action_switch(UiEvent *event, void *userdata) {
109
110 }
111
112 void action_toolbar_button(UiEvent *event, void *userdata) {
113 printf("toolbar button\n");
114
115 ui_dialog(event->obj, .title = "Dialog Title", .content = "Content Label", .button1_label = "btn1", .button2_label = "btn2", .input = TRUE, .closebutton_label = "Cancel");
116 }
117
118 void action_dialog_button(UiEvent *event, void *userdata) {
119 ui_close(event->obj);
120 }
121
122 void action_toolbar_dialog(UiEvent *event, void *userdata) {
123
124 UiObject *dialog = ui_dialog_window(event->obj, .title = "Dialog Window", .lbutton1 = "Cancel 1", .lbutton2 = "Btn2", .rbutton3 = "Btn3", .rbutton4 = "Login 4", .onclick = action_dialog_button, .default_button = 4, .show_closebutton = UI_OFF);
125
126 ui_vbox(dialog, .margin = 10, .spacing = 10) {
127 ui_label(dialog, .label = "Enter password:");
128 ui_passwordfield(dialog, .varname = "password");
129 }
130
131 ui_show(dialog);
132 }
133
134 void action_toolbar_newwindow(UiEvent *event, void *userdata) {
135 UiObject *obj = ui_simple_window("New Window", NULL);
136
137 ui_headerbar0(obj) {
138 ui_headerbar_start(obj) {
139 ui_button(obj, .label = "Open");
140 }
141 ui_headerbar_end(obj) {
142 ui_button(obj, .label = "Test");
143 }
144 }
145
146 ui_textarea(obj, .varname="text");
147
148 ui_show(obj);
149 }
150
151 MyDocument* create_doc(void) {
152 MyDocument *doc = ui_document_new(sizeof(MyDocument));
153 UiContext *docctx = ui_document_context(doc);
154 doc->str1 = ui_string_new(docctx, "str1");
155 doc->str1 = ui_string_new(docctx, "str2");
156 doc->path = ui_string_new(docctx, "path");
157 doc->progress = ui_double_new(docctx, "progress");
158 doc->list = ui_list_new(docctx, "list");
159 ui_list_append(doc->list, "test1");
160 ui_list_append(doc->list, "test2");
161 ui_list_append(doc->list, "test3");
162 doc->radio = ui_int_new(docctx, "radio");
163 doc->tabview = ui_int_new(docctx, "tabview");
164 doc->image = ui_generic_new(docctx, "image");
165 //doc->text = ui_text_new(docctx, "text");
166 return doc;
167 }
168
169 UiIcon *icon = NULL;
170
171 static void* list_getvalue(void *elm, int col) {
172 /*
173 if(col == 0) {
174 if(!icon) {
175 icon = ui_icon("folder", 24);
176 }
177 return icon;
178 }
179 */
180
181 char *str = elm;
182 return col == 0 ? str : "x";
183 }
184
185 static UiList *menu_list;
186 int new_item_count = 0;
187
188 void action_add_menu_item(UiEvent *event, void *userdata) {
189 char str[64];
190 snprintf(str, 64, "new item %d", new_item_count++);
191
192 ui_list_append(menu_list, strdup(str));
193 ui_list_notify(menu_list);
194 }
195
196 void action_menu_list(UiEvent *event, void *userdata) {
197 printf("menu list item: %d\n", event->intval);
198 }
199
200 static int tab_x = 0;
201 void action_tab2_button(UiEvent *event, void *userdata) {
202 MyDocument *doc = event->document;
203 printf("current page: %d\n", (int)ui_get(doc->tabview));
204 ui_set(doc->tabview, 0);
205 }
206
207
208 void action_group1(UiEvent *event, void *userdata) {
209 UiContext *ctx = event->obj->ctx;
210 if(userdata) {
211 ui_unset_group(ctx, 1);
212 } else {
213 ui_set_group(ctx, 1);
214 }
215 }
216
217 void action_group2(UiEvent *event, void *userdata) {
218 UiContext *ctx = event->obj->ctx;
219 if(userdata) {
220 ui_unset_group(ctx, 2);
221 } else {
222 ui_set_group(ctx, 2);
223 }
224 }
225
226 void application_startup(UiEvent *event, void *data) {
227 // global list
228 UiContext *global = ui_global_context();
229 menu_list = ui_list_new(global, "menulist");
230 ui_list_append(menu_list, "menu list item 1");
231 ui_list_append(menu_list, "menu list item 2");
232 ui_list_append(menu_list, "menu list item 3");
233
234
235
236 UiObject *obj = ui_window("Test", NULL);
237
238 MyDocument *doc = create_doc();
239 ui_attach_document(obj->ctx, doc);
240
241 ui_tabview(obj, .spacing=10, .margin=10, .tabview = UI_TABVIEW_NAVIGATION_SIDE, .varname="tabview") {
242 ui_tab(obj, "Tab 1") {
243 ui_vbox(obj, .fill = UI_OFF, .margin = 15, .spacing = 15) {
244 ui_button(obj, .label = "Test Button", .icon = "application-x-generic", .onclick = action_button);
245 ui_togglebutton(obj, .label = "Toggle");
246 ui_checkbox(obj, .label = "My Checkbox");
247 }
248 ui_grid(obj, .fill = UI_OFF, .columnspacing = 15, .rowspacing = 15, .margin = 15) {
249 ui_button(obj, .label = "Activate Group 1", .hexpand = TRUE, .onclick = action_group1);
250 ui_button(obj, .label = "Disable Group 1", .onclick = action_group1, .onclickdata = "disable");
251 ui_newline(obj);
252 ui_button(obj, .label = "Activate Group 2", .hexpand = TRUE, .onclick = action_group2);
253 ui_button(obj, .label = "Disable Group 2", .onclick = action_group2, .onclickdata = "disable");
254 ui_newline(obj);
255
256 ui_button(obj, .label = "Groups 1,2", .colspan = 2, .groups = UI_GROUPS(1, 2));
257 ui_newline(obj);
258
259 ui_label(obj, .label = "Label Col 1", .align = UI_ALIGN_LEFT);
260 ui_label(obj, .label = "Label Col 2", .style = UI_LABEL_STYLE_TITLE, .align = UI_ALIGN_RIGHT);
261 ui_newline(obj);
262
263 //ui_spinner(obj, .step = 5);
264 //ui_newline(obj);
265
266 ui_progressbar(obj, .colspan = 2, .varname = "progress");
267 ui_set(doc->progress, 0.75);
268 ui_newline(obj);
269
270 ui_textfield(obj, .value = doc->str1);
271 ui_newline(obj);
272
273 //ui_button(obj, .label="Test");
274 ui_path_textfield(obj, .varname = "path");
275 ui_set(doc->path, "/test/path/longdirectoryname/123");
276 ui_newline(obj);
277
278 //UiModel *model = ui_model(obj->ctx, UI_ICON_TEXT, "Col 1", UI_STRING, "Col 2", -1);
279 //model->getvalue = list_getvalue;
280 ui_combobox(obj, .hexpand = true, .vexpand = false, .colspan = 2, .varname = "list", .getvalue = list_getvalue);
281 ui_newline(obj);
282
283 ui_hbox0(obj) {
284 ui_radiobutton(obj, .label = "Radio 1", .varname = "radio");
285 ui_radiobutton(obj, .label = "Radio 2", .varname = "radio");
286 ui_radiobutton(obj, .label = "Radio 3", .varname = "radio");
287 }
288 }
289 }
290 ui_tab(obj, "Tab 2") {
291 ui_button(obj, .label = "Button 1 Start Thread", .onclick=action_start_thread);
292 ui_button(obj, .label = "Button 2 Notify Thread", .onclick=action_notify_thread);
293 ui_button(obj, .label = "Button 3", .onclick=action_tab2_button);
294 ui_button(obj, .label = "Button 4", .onclick=action_tab2_button);
295 ui_button(obj, .label = "Button 5", .onclick=action_tab2_button);
296 ui_button(obj, .label = "Button 6", .onclick=action_tab2_button);
297 }
298 ui_tab(obj, "Tab 3") {
299 UiTabViewArgs args = {0};
300 UI_CTN(obj, tabview=ui_tabview_create(obj, args)) {
301 UiObject *tab1 = ui_tabview_add(tabview, "Sub 1", -1);
302 ui_button(tab1, .label = "Button 1");
303
304
305 UiObject *tab2 = ui_tabview_add(tabview, "Sub 2", -1);
306 ui_button(tab2, .label = "Button 2");
307 }
308 }
309 ui_tab(obj, "Tab 4") {
310 ui_textarea(obj, .varname = "text");
311 }
312 ui_tab(obj, "Tab 5") {
313 ui_button(obj, .label = "Test Button", .icon = "application-x-generic", .onclick = action_button);
314 ui_imageviewer(obj, .varname = "image", .style_class = "imageviewer");
315 }
316
317 ui_tab(obj, "Tab 6") {
318 ui_scrolledwindow(obj, .fill = UI_ON) {
319 ui_expander(obj, .label = "Expander", .margin = 10, .spacing = 10) {
320 ui_label(obj, .label = "Test");
321 ui_button(obj, .label = "Button");
322 }
323
324 ui_frame(obj, .label = "Frame", .margin = 10, .spacing = 10) {
325 ui_label(obj, .label = "Title", .style = UI_LABEL_STYLE_TITLE);
326 ui_label(obj, .label = "Sub-Title", .style = UI_LABEL_STYLE_SUBTITLE);
327 ui_label(obj, .label = "Dim Label", .style = UI_LABEL_STYLE_DIM);
328 ui_label(obj, .label = "No Style");
329 }
330
331 for(int i=0;i<100;i++) {
332 char labelstr[32];
333 snprintf(labelstr, 32, "button %d", i);
334 ui_button(obj, .label = labelstr);
335 }
336 }
337 }
338 }
339
340 /*
341
342 */
343
344 ui_show(obj);
345 }
346
347 /*
37 typedef struct WindowData { 348 typedef struct WindowData {
38 UiInteger* check; 349 UiInteger* check;
39 UiInteger* toggle; 350 UiInteger* toggle;
40 UiInteger* radio; 351 UiInteger* radio;
41 UiString* text; 352 UiString* text;
366 } 677 }
367 678
368 ui_show(obj); 679 ui_show(obj);
369 } 680 }
370 681
682 */
371 683
372 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) 684 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow)
373 { 685 {
686 ui_init("app1", NULL, 0);
687 ui_onstartup(application_startup, NULL);
688
689 // menu
690 ui_menu("File") {
691 ui_menuitem(.label = "Test");
692 }
693
694 ui_toolbar_item("Test", .label = "Test", .onclick = action_toolbar_button);
695 ui_toolbar_item("Test2", .label = "New Window", .onclick = action_toolbar_newwindow);
696 ui_toolbar_item("Test3", .label = "Dialog", .onclick = action_toolbar_dialog);
697 ui_toolbar_item("Test4", .label = "Test 4", .onclick = action_toolbar_button);
698 ui_toolbar_item("Test5", .label = "Test 5", .onclick = action_toolbar_button);
699 ui_toolbar_item("Test6", .label = "Test 6", .onclick = action_toolbar_button);
700 ui_toolbar_toggleitem("Toggle", .label = "Toggle", .onchange = action_toolbar_button);
701 ui_toolbar_menu("Menu", .label = "Menu") {
702 ui_menuitem("Secondary Test", .onclick = action_toolbar_button, NULL);
703 ui_menu("Secondary Sub") {
704 ui_menuitem("Secondary subitem", NULL, NULL);
705 }
706 ui_menuseparator();
707 ui_menu_itemlist(.varname = "menulist", .onselect=action_menu_list);
708 ui_menuseparator();
709 ui_menuitem("last", .onclick = action_add_menu_item);
710 }
711
712 ui_toolbar_appmenu() {
713 ui_menuitem("New");
714 ui_menuitem("Open");
715 ui_menuitem("Save");
716
717 ui_menuseparator();
718
719 ui_menuitem("Close");
720 }
721
722 ui_toolbar_add_default("Test", UI_TOOLBAR_LEFT);
723 ui_toolbar_add_default("Test6", UI_TOOLBAR_LEFT);
724 ui_toolbar_add_default("Toggle", UI_TOOLBAR_LEFT);
725 ui_toolbar_add_default("Menu", UI_TOOLBAR_LEFT);
726
727 ui_toolbar_add_default("Test2", UI_TOOLBAR_CENTER);
728 ui_toolbar_add_default("Test3", UI_TOOLBAR_CENTER);
729
730 ui_toolbar_add_default("Test4", UI_TOOLBAR_RIGHT);
731 ui_toolbar_add_default("Test5", UI_TOOLBAR_RIGHT);
732
733 ui_main();
734
735 return (EXIT_SUCCESS);
736
737 /*
374 ui_init("app1", 0, NULL); 738 ui_init("app1", 0, NULL);
375 ui_onstartup(application_startup, NULL); 739 ui_onstartup(application_startup, NULL);
376 740
377 ui_menu("File") { 741 ui_menu("File") {
378 ui_menuitem(.label = "Item 1"); 742 ui_menuitem(.label = "Item 1");
417 ui_toolbar_add_default("Menu", UI_TOOLBAR_RIGHT); 781 ui_toolbar_add_default("Menu", UI_TOOLBAR_RIGHT);
418 782
419 ui_main(); 783 ui_main();
420 784
421 return (EXIT_SUCCESS); 785 return (EXIT_SUCCESS);
422 } 786 */
787 }

mercurial