ui/ui/toolkit.h

changeset 0
804d8803eade
child 2
ea89bbb0c4c8
equal deleted inserted replaced
-1:000000000000 0:804d8803eade
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 #ifndef UI_TOOLKIT_H
30 #define UI_TOOLKIT_H
31
32 #include <inttypes.h>
33
34 #ifdef UI_COCOA
35
36 #ifdef __OBJC__
37 #import <Cocoa/Cocoa.h>
38 #define UIWIDGET NSView*
39 #define UIMENU NSMenu*
40 #else
41 typedef void* UIWIDGET;
42 typedef void* UIMENU;
43 #endif
44
45 #elif UI_GTK2 || UI_GTK3
46
47 #include <gtk/gtk.h>
48 #define UIWIDGET GtkWidget*
49 #define UIMENU GtkMenu*
50 #define UI_GTK
51
52 #elif UI_MOTIF
53
54 #include <Xm/XmAll.h>
55 #define UIWIDGET Widget
56 #define UIMENU Widget
57
58 #elif defined(UI_QT4) || defined(UI_QT5)
59 #ifdef __cplusplus
60 #include <QApplication>
61 #include <QWidget>
62 #include <QMenu>
63 #define UIWIDGET QWidget*
64 #define UIMENU QMenu*
65 #else /* __cplusplus */
66 #define UIWIDGET void*
67 #define UIMENU void*
68 #endif
69
70 #elif UI_WPF
71 #define UIWIDGET void*
72 #define UIMENU void*
73 #endif
74
75 #ifndef TRUE
76 #define TRUE 1
77 #endif
78 #ifndef FALSE
79 #define FALSE 0
80 #endif
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86 #define UI_GROUP_SELECTION 20000
87
88 /* public types */
89 typedef int UiBool;
90
91 typedef struct UiObject UiObject;
92 typedef struct UiEvent UiEvent;
93 typedef struct UiMouseEvent UiMouseEvent;
94 typedef struct UiObserver UiObserver;
95
96 typedef struct UiInteger UiInteger;
97 typedef struct UiDouble UiDouble;
98 typedef struct UiString UiString;
99 typedef struct UiText UiText;
100 typedef struct UiList UiList;
101 typedef struct UiRange UiRange;
102
103 typedef struct UiStr UiStr;
104
105 /* begin opaque types */
106 typedef struct UiContext UiContext;
107 typedef struct UiContainer UiContainer;
108
109 typedef struct UiIcon UiIcon;
110 typedef struct UiImage UiImage;
111
112 typedef struct UiSelection UiSelection;
113 /* end opaque types */
114
115 typedef struct UiTabbedPane UiTabbedPane;
116
117 enum UiMouseEventType { UI_PRESS = 0, UI_PRESS2 };
118
119
120
121 typedef void(*ui_callback)(UiEvent*, void*); /* event, user data */
122
123 typedef void*(*ui_getvaluefunc)(void*, int);
124
125 typedef int(*ui_threadfunc)(void*);
126
127 typedef void(*ui_freefunc)(void*);
128
129 struct UiObject {
130 /*
131 * native widget
132 */
133 UIWIDGET widget;
134
135 /*
136 * user window data
137 */
138 void *window;
139
140 /*
141 * window context
142 */
143 UiContext *ctx;
144
145 /*
146 * container interface
147 */
148 UiContainer *container;
149
150 /*
151 * next container object
152 */
153 UiObject *next;
154 };
155
156 struct UiTabbedPane {
157 /*
158 * native widget
159 */
160 UIWIDGET widget;
161
162 /*
163 * current document
164 */
165 void *document;
166
167 /*
168 * parent context
169 */
170 UiContext *ctx;
171 };
172
173 struct UiEvent {
174 UiObject *obj;
175 void *document;
176 void *window;
177 void *eventdata;
178 int intval;
179 };
180
181 struct UiMouseEvent {
182 int x;
183 int y;
184 enum UiMouseEventType type;
185 int button;
186 };
187
188 struct UiObserver {
189 ui_callback callback;
190 void *data;
191 UiObserver *next;
192 };
193
194 struct UiStr {
195 char *ptr;
196 void (*free)(void *v);
197 };
198
199 struct UiInteger {
200 int64_t (*get)(UiInteger*);
201 void (*set)(UiInteger*, int64_t);
202 void *obj;
203
204 int64_t value;
205 UiObserver *observers;
206 };
207
208 struct UiDouble {
209 double (*get)(UiDouble*);
210 void (*set)(UiDouble*, double);
211 void *obj;
212
213 double value;
214 UiObserver *observers;
215 };
216
217 struct UiString {
218 char* (*get)(UiString*);
219 void (*set)(UiString*, char*);
220 void *obj;
221
222 UiStr value;
223 UiObserver *observers;
224 };
225
226 struct UiText {
227 void (*set)(UiText*, char*);
228 char* (*get)(UiText*);
229 char* (*getsubstr)(UiText*, int, int); /* text, begin, end */
230 void (*insert)(UiText*, int, char*);
231 void (*setposition)(UiText*,int);
232 int (*position)(UiText*);
233 void (*selection)(UiText*, int*, int*); /* text, begin, end */
234 int (*length)(UiText*);
235 void (*remove)(UiText*, int, int); /* text, begin, end */
236 UiStr value;
237 int pos;
238 void *obj;
239 void *undomgr;
240 // TODO: replacefunc, ...
241 UiObserver *observers;
242 };
243
244 /*
245 * abstract list
246 */
247 struct UiList {
248 /* get the first element */
249 void*(*first)(UiList *list);
250 /* get the next element */
251 void*(*next)(UiList *list);
252 /* get the nth element */
253 void*(*get)(UiList *list, int i);
254 /* get the number of elements */
255 int(*count)(UiList *list);
256 /* iterator changes after first() next() and get() */
257 void *iter;
258 /* private - implementation dependent */
259 void *data;
260
261 /* binding function */
262 void (*update)(UiList *list, int i);
263 /* binding object */
264 void *obj;
265
266 /* list of observers */
267 UiObserver *observers;
268 };
269
270 struct UiRange {
271 double (*get)(UiRange *range);
272 void (*set)(UiRange *range, double value);
273 void (*setrange)(UiRange *range, double min, double max);
274 void (*setextent)(UiRange *range, double extent);
275 double value;
276 double min;
277 double max;
278 double extent;
279 void *obj;
280 /* list of observers */
281 UiObserver *observers;
282 };
283
284
285 void ui_init(char *appname, int argc, char **argv);
286 char* ui_appname();
287
288 void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata);
289
290 void ui_onstartup(ui_callback f, void *userdata);
291 void ui_onopen(ui_callback f, void *userdata);
292 void ui_onexit(ui_callback f, void *userdata);
293
294 void ui_main();
295 void ui_show(UiObject *obj);
296 void ui_close(UiObject *obj);
297
298 void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd);
299
300 void* ui_document_new(size_t size);
301 void ui_document_destroy(void *doc);
302
303 void ui_set_document(UiObject *obj, void *document); // deprecated
304 void ui_detach_document(UiObject *obj); // deprecated
305 void* ui_get_document(UiObject *obj); // deprecated
306 void ui_set_subdocument(void *document, void *sub); // deprecated
307 void ui_detach_subdocument(void *document, void *sub); // deprecated
308 void* ui_get_subdocument(void *document); // deprecated
309
310 UiContext* ui_document_context(void *doc);
311
312 void ui_attach_document(UiContext *ctx, void *document);
313 void ui_detach_document2(UiContext *ctx, void *document);
314
315 void ui_set_group(UiContext *ctx, int group);
316 void ui_unset_group(UiContext *ctx, int group);
317 int* ui_active_groups(UiContext *ctx, int *ngroups);
318
319 void* ui_malloc(UiContext *ctx, size_t size);
320 void* ui_calloc(UiContext *ctx, size_t nelem, size_t elsize);
321 void ui_free(UiContext *ctx, void *ptr);
322 void* ui_realloc(UiContext *ctx, void *ptr, size_t size);
323
324 // types
325
326 UiInteger* ui_int_new(UiContext *ctx, char *name);
327 UiDouble* ui_double_new(UiContext *ctx, char *name);
328 UiString* ui_string_new(UiContext *ctx, char *name);
329 UiText* ui_text_new(UiContext *ctx, char *name);
330 UiRange* ui_range_new(UiContext *ctx, char *name);
331
332 UiObserver* ui_observer_new(ui_callback f, void *data);
333 UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer);
334 UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data);
335 void ui_notify(UiObserver *observer, void *data);
336 void ui_notify_except(UiObserver *observer, UiObserver *exc, void *data);
337 void ui_notify_evt(UiObserver *observer, UiEvent *event);
338
339
340 UiList* ui_list_new(UiContext *ctx, char *name);
341 void* ui_list_first(UiList *list);
342 void* ui_list_next(UiList *list);
343 void* ui_list_get(UiList *list, int i);
344 int ui_list_count(UiList *list);
345 void ui_list_append(UiList *list, void *data);
346 void ui_list_prepend(UiList *list, void *data);
347 void ui_list_clear(UiList *list);
348 void ui_list_addobsv(UiList *list, ui_callback f, void *data);
349 void ui_list_notify(UiList *list);
350
351 void ui_clipboard_set(char *str);
352 char* ui_clipboard_get();
353
354 void ui_add_image(char *imgname, char *filename); // TODO: remove?
355
356 // general widget functions
357 void ui_set_enabled(UIWIDGET widget, int enabled);
358 void ui_set_show_all(UIWIDGET widget, int value);
359 void ui_set_visible(UIWIDGET widget, int visible);
360
361
362 #ifdef __cplusplus
363 }
364 #endif
365
366 #endif /* UI_TOOLKIT_H */
367

mercurial