application/main.c

changeset 162
18892c0a9adc
parent 158
4bde241c49b1
child 163
b70e2a77dea0
equal deleted inserted replaced
161:b1eac0878ce7 162:18892c0a9adc
31 31
32 #include <ui/ui.h> 32 #include <ui/ui.h>
33 #include <ucx/buffer.h> 33 #include <ucx/buffer.h>
34 #include <ucx/utils.h> 34 #include <ucx/utils.h>
35 35
36 typedef struct Document { 36 void action_menu(UiEvent *event, void *userdata) {
37 UiText *text;
38 UiString *t1;
39 UiString *t2;
40 UiString *t3;
41 UiInteger *i;
42 UiDouble *d;
43 UiRange *r;
44 UiList *list;
45 UiDouble *progress;
46 } Document;
47
48 typedef struct Entry {
49 char *name;
50 char *desc;
51 } Entry;
52
53 Document *d1;
54 Document *d2;
55 int n = 1;
56
57 UiImage *img;
58
59 Document* create_doc(char *str);
60
61 Document* next_doc(void) {
62 Document *doc = d1;
63 if(n == 1) {
64 doc = d2;
65 printf("doc2\n");
66 } else {
67 printf("doc1\n");
68 }
69 n++;
70 n = n%2;
71 return doc;
72 }
73
74 void action_menu(UiEvent *event, void *data) {
75 printf("action_menu\n");
76 37
77 Document *doc = event->document;
78 char *s = doc->text->get(doc->text);
79 printf("text: {\n%s\n}\n", s);
80 //int i = doc->i->get(doc->i);
81 //printf("i: %d\n", i);
82
83 fflush(stdout);
84 }
85
86 void list_data_get(UiEvent *event, UiSelection *selection, UiList *l, int i) {
87 ui_selection_settext(selection, "Hello World!", -1);
88 }
89
90 void list_drop(UiEvent *event, UiSelection *selection, UiList *l, int i) {
91 char *text = ui_selection_gettext(selection);
92 printf("dnd drop: {%s}\n", text);
93 free(text);
94 }
95
96 Document* create_doc(char *str) {
97 Document *doc = ui_document_new(sizeof(Document));
98 UiContext *ctx = ui_document_context(doc);
99
100 doc->text = ui_text_new(ctx, "text");
101 doc->t1 = ui_string_new(ctx, "t1");
102 doc->t2 = ui_string_new(ctx, "t2");
103 doc->t3 = ui_string_new(ctx, "t3");
104
105 doc->i = ui_int_new(ctx, "int");
106 doc->d = ui_double_new(ctx, "d");
107 doc->r = ui_range_new(ctx, "r");
108
109 doc->progress = ui_double_new(ctx, "progress");
110
111 doc->list = ui_list_new(ctx, "list");
112 Entry *e1 = calloc(1, sizeof(Entry));
113 e1->name = "test";
114 e1->desc = "test file";
115 Entry *e2 = calloc(1, sizeof(Entry));
116 e2->name = str;
117 e2->desc = "important document";
118 ui_list_append(doc->list, e1);
119 ui_list_append(doc->list, e2);
120
121 return doc;
122 }
123
124 void* model_get(Entry *e, int col) {
125 if(col == 0) {
126 return img;
127 } else if(col == 1) {
128 return e->name;
129 } else if(col == 2) {
130 return e->desc;
131 }
132 return NULL;
133 }
134
135 void action_newdoc(UiEvent *event, void *data) {
136 printf("new doc;\n ");
137
138 Document *newd = next_doc();
139 ui_set_document(event->obj, newd);
140 }
141
142 void observ(UiEvent *event, void *data) {
143 printf("observ: %s\n", (char*)data);
144 }
145
146 void doublechanged(UiEvent *event, void *data) {
147 UiDouble *d = event->eventdata;
148 printf("d: %f\n", (float)d->get(d));
149 } 38 }
150 39
151 void application_startup(UiEvent *event, void *data) { 40 void application_startup(UiEvent *event, void *data) {
152 UiIcon *icon = ui_icon("folder", 16);
153 img = ui_icon_image(icon);
154 if(!img) {
155 fprintf(stderr, "Cannot load folder icon\n");
156 }
157
158 //Document *doc = create_doc();
159 d1 = create_doc("doc1");
160 d2 = create_doc("doc2");
161 41
162 UiObject *obj = ui_window("Test", NULL); 42 UiObject *obj = ui_window("Test", NULL);
163 ui_set_document(obj, d1);
164 43
165 ui_textarea_nv(obj, "text");
166 //ui_radiobutton_nv(obj, "1", "int");
167 //ui_radiobutton_nv(obj, "2", "int");
168 //ui_radiobutton_nv(obj, "3", "int");
169
170 ui_textfield_nv(obj, "t1");
171 //ui_textarea_nv(obj, "text");
172 //d1->t1->observers = ui_add_observer(d1->t1->observers, observ, "t1");
173 //d1->text->observers = ui_add_observer(d1->text->observers, observ, "text");
174
175 UiModel *model = ui_model(obj->ctx, UI_ICON_TEXT, "name", UI_STRING, "desc", -1);
176 model->getvalue = (ui_getvaluefunc)model_get;
177 model->drop = list_drop;
178 model->data_get = list_data_get;
179 UiListCallbacks cb = {NULL, NULL, NULL};
180 //UIWIDGET table = ui_table_nv(obj, "list", model, cb);
181 //ui_table_dragsource(table, 0, "text/plain", NULL);
182
183 //ui_spinner_setrange(ui_spinnerf_nv(obj, 1, 0, "d"), 0, 1000);
184 //ui_spinnerr_nv(obj, "r");
185 //d1->r->setrange(d1->r, 0, 10);
186 //d1->r->setextent(d1->r, 1);
187 //d1->d->observers = ui_add_observer(d1->d->observers, doublechanged, NULL);
188
189 //ui_progressbar_nv(obj, "progress");
190
191 ui_textfield(obj, NULL);
192
193 ui_button(obj, "Switch Document", action_newdoc, NULL);
194 44
195 ui_show(obj); 45 ui_show(obj);
196 } 46 }
197 47
198 int main(int argc, char** argv) { 48 int main(int argc, char** argv) {

mercurial