application/main.c

changeset 39
4e66271541e8
parent 37
56016468753d
child 42
29b2821d1262
equal deleted inserted replaced
38:8ccdde37275b 39:4e66271541e8
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 TestDocument {
37 UiInteger check1;
38 } TestDocument;
39 36
40 typedef struct TestWindowData { 37 typedef struct Person {
41 TestDocument *doc1; 38 char *name;
42 TestDocument *doc2; 39 char *mail;
43 UiText text; 40 } Person;
44 } TestWindowData;
45 41
46 UiInteger check1; 42 void action_close(UiEvent *event, void *data) {
47 43 ui_close(event->obj);
48 UiList *list;
49
50 ///*
51
52 void action_new(UiEvent *event, void *data) {
53 UiObject *window = ui_window("Mod1", NULL);
54 //ui_window_addint(window, "check1");
55 ui_show(window);
56 if(event->eventdata) {
57 printf("%s\n", event->eventdata);
58 }
59 } 44 }
60 45
61 void action_open(UiEvent *event, void *data) { 46 char* person_getvalue(Person *p, int column) {
62 //printf("check1: %s\n", event->intval ? "true" : "false"); 47 switch(column) {
63 //printf("check1: %s\n", ui_getint(event->obj, "check1") ? "true" : "false"); 48 case 0: return p->name;
64 TestDocument *doc = event->document; 49 case 1: return p->mail;
65 TestWindowData *wd = event->window;
66 printf("check1: %s\n", ui_getval(doc->check1) ? "true" : "false");
67
68 char *path = ui_openfiledialog(event->obj);
69 if(path) {
70 UcxBuffer *buf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
71 FILE *file = fopen(path, "r");
72 if(file) {
73 ucx_stream_hcopy(file, buf, fread, ucx_buffer_write);
74 ucx_buffer_putc(buf, '\0');
75 ui_setval(wd->text, buf->space);
76 fclose(file);
77 }
78 ucx_buffer_free(buf);
79 } 50 }
51 return NULL;
80 } 52 }
81
82 void action_save(UiEvent *event, void *data) {
83 TestWindowData *wd = event->window;
84 printf("Text: {%s}\n", ui_getval(wd->text));
85 ui_setval(wd->text, uistr("hello"));
86
87 ui_list_append(list, "abc");
88 ui_notify(list->observers, NULL);
89 }
90
91 void action_close(UiEvent *event, void *data) {
92 exit(0);
93 }
94
95 void action_doc1(UiEvent *event, void *data) {
96 TestWindowData *wdata = event->window;
97 if(event->obj->document != wdata->doc1) {
98 ui_set_document(event->obj, wdata->doc1);
99 }
100 ui_unset_group(event->obj->ctx, 1);
101 }
102
103 void action_doc2(UiEvent *event, void *data) {
104 TestWindowData *wdata = event->window;
105 if(event->obj->document != wdata->doc2) {
106 ui_set_document(event->obj, wdata->doc2);
107 }
108 ui_set_group(event->obj->ctx, 1);
109 }
110
111 void action_undo(UiEvent *event, void *data) {
112 printf("undo\n");
113 TestWindowData *wd = event->window;
114 ui_text_undo(&wd->text);
115 }
116
117 void action_redo(UiEvent *event, void *data) {
118 printf("redo\n");
119 TestWindowData *wd = event->window;
120 ui_text_redo(&wd->text);
121 }
122
123 void action_document(UiEvent *event, void *data) {
124 UiList *documents = data;
125 printf("selected document: %d\n", event->intval);
126 }
127
128 void action_cut(UiEvent *event, void *data) {
129 printf("cut\n");
130 }
131
132 void action_copy(UiEvent *event, void *data) {
133 printf("copy\n");
134 TestWindowData *wd = event->window;
135 int begin;
136 int end;
137 wd->text.selection(&wd->text, &begin, &end);
138 char *selection = wd->text.getsubstr(&wd->text, begin, end);
139 ui_clipboard_set(selection);
140 }
141
142 void action_paste(UiEvent *event, void *data) {
143 printf("paste\n");
144 TestWindowData *wd = event->window;
145 char *str = ui_clipboard_get();
146 if(str) {
147 int pos = wd->text.position(&wd->text);
148 wd->text.insert(&wd->text, pos, str);
149 free(str);
150 }
151 }
152
153 void action_delete(UiEvent *event, void *data) {
154 printf("delete\n");
155 }
156
157 //*/
158 53
159 int main(int argc, char** argv) { 54 int main(int argc, char** argv) {
160 ui_init("app1", argc, argv); 55 ui_init("app1", argc, argv);
161 ui_locales_dir("/opt/app1/locales"); 56 ui_locales_dir("/opt/app1/locales");
162 ui_load_lang_def(NULL, "en_EN"); 57 ui_load_lang_def(NULL, "en_EN");
163 //ui_openfilefunc(action_new, NULL); 58 //ui_openfilefunc(action_new, NULL);
164 59
165 ///* 60
166 list = ui_list_new();
167 ui_list_append(list, "file1.txt");
168 ui_list_append(list, "hello.txt");
169 ui_list_append(list, "main.c");
170 61
171 ui_menu("File"); 62 ui_menu("File");
172 ui_menuitem_st(UI_STOCK_NEW, action_new, NULL);
173 ui_menuitem_st(UI_STOCK_OPEN, action_open, NULL);
174 ui_menuitem_stgr(UI_STOCK_SAVE, action_save, NULL, 1, -1);
175 ui_menuseparator();
176
177 ui_menuitem("Dokument 1", action_doc1, NULL);
178 ui_menuitem("Dokument 2", action_doc2, NULL);
179
180 ui_menuseparator();
181
182 ui_checkitem_nv("Check", "check1");
183 //ui_checkitem("Check", action_open, NULL);
184
185 void ui_menuseparator();
186 ui_menuitem_list(list, action_document, list);
187 ui_menuseparator();
188 ui_menuitem_st(UI_STOCK_CLOSE, action_close, NULL); 63 ui_menuitem_st(UI_STOCK_CLOSE, action_close, NULL);
189 64
190 ui_menu("Edit"); 65 printf("create window\n");
191 ui_menuitem_stgr(UI_STOCK_CUT, action_cut, NULL, UI_GROUP_SELECTION, -1); 66 UiObject *window = ui_window("Mod0", NULL);
192 ui_menuitem_stgr(UI_STOCK_COPY, action_copy, NULL, UI_GROUP_SELECTION, -1); 67
193 ui_menuitem_st(UI_STOCK_PASTE, action_paste, NULL); 68 UiModelInfo *model = ui_model_info(window->ctx, UI_STRING, "Name", UI_STRING, "Email", -1);
194 ui_menuitem_stgr(UI_STOCK_DELETE, action_delete, NULL, UI_GROUP_SELECTION, -1); 69 model->getvalue = (ui_model_getvalue_f)person_getvalue;
70 UiList *list = ui_list_new();
71 Person *p1 = ui_malloc(window->ctx, sizeof(Person));
72 Person *p2 = ui_malloc(window->ctx, sizeof(Person));
73 Person *p3 = ui_malloc(window->ctx, sizeof(Person));
74 Person *p4 = ui_malloc(window->ctx, sizeof(Person));
75 p1->name = "Some Name";
76 p1->mail = "mail@host.com";
77 p2->name = "Other Person";
78 p2->mail = "other.person@provider.com";
79 p3->name = "My Self";
80 p3->mail = "my@self.org";
81 p4->name = "Gregory House";
82 p4->mail = "greg@pp";
83 ui_list_append(list, p1);
84 ui_list_append(list, p2);
85 ui_list_append(list, p3);
86 ui_list_append(list, p4);
87 ui_table(window, list, model);
195 88
196 89
197 ui_toolitem_st("new", UI_STOCK_NEW, action_new, NULL);
198 ui_toolitem_st("open", UI_STOCK_GO_BACK, action_open, NULL);
199 ui_toolitem_stgr("save", UI_STOCK_GO_FORWARD, action_save, NULL, 1, -1);
200 ui_toolitem_st("close", UI_STOCK_CLOSE, action_close, NULL);
201 ui_toolitem_st("undo", UI_STOCK_UNDO, action_undo, NULL);
202 ui_toolitem_st("redo", UI_STOCK_REDO, action_redo, NULL);
203
204 ui_toolbar_add_default("new");
205 ui_toolbar_add_default("open");
206 ui_toolbar_add_default("save");
207 ui_toolbar_add_default("close");
208 ui_toolbar_add_default("undo");
209 ui_toolbar_add_default("redo");
210 //*/
211
212 //ui_menu("File");
213 //ui_menuitem("New", NULL, NULL);
214 //ui_menuitem("Close", NULL, NULL);
215 //ui_menu("Edit");
216 //ui_menuitem("Preferences", NULL, NULL);
217
218 printf("create window\n");
219 UiObject *window = ui_window("Mod0", NULL);
220 TestWindowData *wdata = calloc(1, sizeof(TestWindowData));
221 window->window = wdata;
222
223 TestDocument *doc1 = ui_document_new(sizeof(TestDocument));
224 TestDocument *doc2 = ui_document_new(sizeof(TestDocument));
225 ui_document_regint(doc1, "check1", &doc1->check1);
226 ui_document_regint(doc2, "check1", &doc2->check1);
227 wdata->doc1 = doc1;
228 wdata->doc2 = doc2;
229
230 ui_set_document(window, doc1);
231
232 //ui_button(window, "OK", action_open, NULL);
233 ui_textarea(window, &wdata->text);
234
235 //ui_window_addint(window, "check1");
236 ui_show(window); 90 ui_show(window);
237 ui_main(); 91 ui_main();
238 92
239 return (EXIT_SUCCESS); 93 return (EXIT_SUCCESS);
240 } 94 }

mercurial