UNIXworkcode

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 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 35 #if !defined(UI_WIN32) && !defined(UI_SERVER) 36 37 Document* document_create(int id) { 38 Document* doc = ui_document_new(sizeof(Document)); 39 UiContext *ctx = ui_document_context(doc); 40 41 char buf[32]; 42 snprintf(buf, 32, "%d", id); 43 44 doc->id = ui_string_new(ctx, "id"); 45 doc->name = ui_string_new(ctx, "name"); 46 doc->input_name = ui_string_new(ctx, "input_name"); 47 doc->subdocuments = ui_list_new(ctx, "subdocuments"); 48 49 ui_set(doc->id, buf); 50 51 // create some sub documents 52 for(int i=0;i<3;i++) { 53 SubDocument *sub = ui_document_new(sizeof(SubDocument)); 54 UiContext *subctx = ui_document_context(sub); 55 sub->id = ui_string_new(subctx, "sub_id"); 56 sub->name = ui_string_new(subctx, "sub_name"); 57 sub->input_name = ui_string_new(subctx, "sub_input_name"); 58 59 snprintf(buf, 32, "%d.%d", id, i); 60 ui_set(sub->id, buf); 61 62 ui_list_append(doc->subdocuments, sub); 63 } 64 65 return doc; 66 } 67 68 void update_name(UiEvent *event, void *userdata) { 69 Document *doc = event->document; 70 if(!doc) { 71 return; 72 } 73 char *s = ui_get(doc->input_name); 74 ui_set(doc->name, s); 75 } 76 77 void switch_document(UiEvent *event, void *userdata) { 78 MainWindow *wdata = event->window; 79 if(event->document) { 80 ui_detach_document(event->obj->ctx, event->document); 81 } 82 printf("selection: %d\n", event->intval); 83 Document *doc = ui_list_get(wdata->doclist, event->intval); 84 if(doc) { 85 char *s1 = ui_get(doc->id); 86 char *s2 = ui_get(doc->name); 87 printf("doc %s - %s\n", s1, s2); 88 ui_attach_document(event->obj->ctx, doc); 89 90 } 91 } 92 93 void switch_subdocument(UiEvent *event, void *userdata) { 94 MainWindow *wdata = event->window; 95 Document *doc = event->document; 96 UiContext *ctx = ui_document_context(doc); 97 if(doc->current_sub) { 98 ui_detach_document(ctx, doc->current_sub); 99 doc->current_sub = NULL; 100 } 101 102 SubDocument *sub = ui_list_get(doc->subdocuments, event->intval); 103 if(sub) { 104 char *s1 = ui_get(sub->id); 105 char *s2 = ui_get(sub->name); 106 printf("sub %s - %s\n", s1, s2); 107 ui_attach_document(ctx, sub); 108 doc->current_sub = sub; 109 110 } 111 } 112 113 void* doclist_get_value(void *elm, int col) { 114 Document *doc = elm; 115 return ui_get(doc->id); 116 } 117 118 void* sublist_get_value(void *elm, int col) { 119 SubDocument *doc = elm; 120 return ui_get(doc->id); 121 } 122 123 void application_startup(UiEvent *event, void *data) { 124 UiObject *obj = ui_simple_window("Bindings Demo", NULL); 125 MainWindow *wdata = ui_malloc(obj->ctx, sizeof(MainWindow)); 126 obj->window = wdata; 127 128 wdata->doclist = ui_list_new(obj->ctx, "doclist"); 129 130 ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10, .fill = TRUE) { 131 ui_rlabel(obj, .label = "Document:"); 132 ui_llabel(obj, .varname = "id", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); 133 ui_newline(obj); 134 135 ui_rlabel(obj, .label = "Name:"); 136 ui_llabel(obj, .varname = "name", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); 137 ui_newline(obj); 138 139 ui_rlabel(obj, .label = "Sub Document:"); 140 ui_llabel(obj, .varname = "sub_id", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); 141 ui_newline(obj); 142 143 ui_rlabel(obj, .label = "Name:"); 144 ui_llabel(obj, .varname = "sub_name", .hfill = TRUE, .hexpand = TRUE, .vfill = TRUE); 145 ui_newline(obj); 146 147 ui_dropdown(obj, .varname = "doclist", .colspan = 2, .onactivate = switch_document, .getvalue = doclist_get_value, .colspan = 2, .hfill = TRUE); 148 ui_newline(obj); 149 150 ui_frame(obj, .label = "Document", .colspan = 2, .fill = TRUE, .subcontainer = UI_CONTAINER_GRID, .columnspacing = 10, .rowspacing = 10, .padding = 10) { 151 ui_rlabel(obj, .label = "Name:", .vfill = TRUE); 152 ui_textfield(obj, .varname = "input_name", .onchange = update_name); 153 ui_newline(obj); 154 ui_separator(obj, .colspan = 2, .hfill = TRUE); 155 ui_newline(obj); 156 ui_dropdown(obj, .varname = "subdocuments", .getvalue = sublist_get_value, .onactivate = switch_subdocument, .colspan = 2, .hfill = TRUE); 157 } 158 } 159 160 for(int i=0;i<10;i++) { 161 Document *doc = document_create(i); 162 ui_list_append(wdata->doclist, doc); 163 } 164 ui_list_update(wdata->doclist); 165 ui_list_setselection(wdata->doclist, 0); 166 Document *doc = ui_list_get(wdata->doclist, 0); 167 ui_attach_document(obj->ctx, doc); 168 169 ui_list_setselection(wdata->doclist, 0); 170 171 ui_show(obj); 172 } 173 174 int main(int argc, char **argv) { 175 ui_init(NULL, argc, argv); 176 ui_onstartup(application_startup, NULL); 177 ui_main(); 178 return 0; 179 } 180 181 #else 182 183 #ifndef UI_WIN32 184 185 int main(int argc, char **argv) { 186 return 0; 187 } 188 189 #else 190 191 int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { 192 return 0; 193 } 194 195 #endif 196 197 #endif 198