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 #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 typedef void(*ui_enablefunc)(void*, UiBool); 130 131 struct UiObject { 132 /* 133 * native widget 134 */ 135 UIWIDGET widget; 136 137 /* 138 * user window data 139 */ 140 void *window; 141 142 /* 143 * window context 144 */ 145 UiContext *ctx; 146 147 /* 148 * container interface 149 */ 150 UiContainer *container; 151 152 /* 153 * next container object 154 */ 155 UiObject *next; 156 }; 157 158 struct UiTabbedPane { 159 /* 160 * native widget 161 */ 162 UIWIDGET widget; 163 164 /* 165 * current document 166 */ 167 void *document; 168 169 /* 170 * parent context 171 */ 172 UiContext *ctx; 173 }; 174 175 struct UiEvent { 176 UiObject *obj; 177 void *document; 178 void *window; 179 void *eventdata; 180 int intval; 181 }; 182 183 struct UiMouseEvent { 184 int x; 185 int y; 186 enum UiMouseEventType type; 187 int button; 188 }; 189 190 struct UiObserver { 191 ui_callback callback; 192 void *data; 193 UiObserver *next; 194 }; 195 196 struct UiStr { 197 char *ptr; 198 void (*free)(void *v); 199 }; 200 201 struct UiInteger { 202 int64_t (*get)(UiInteger*); 203 void (*set)(UiInteger*, int64_t); 204 void *obj; 205 206 int64_t value; 207 UiObserver *observers; 208 }; 209 210 struct UiDouble { 211 double (*get)(UiDouble*); 212 void (*set)(UiDouble*, double); 213 void *obj; 214 215 double value; 216 UiObserver *observers; 217 }; 218 219 struct UiString { 220 char* (*get)(UiString*); 221 void (*set)(UiString*, char*); 222 void *obj; 223 224 UiStr value; 225 UiObserver *observers; 226 }; 227 228 struct UiText { 229 void (*set)(UiText*, char*); 230 char* (*get)(UiText*); 231 char* (*getsubstr)(UiText*, int, int); /* text, begin, end */ 232 void (*insert)(UiText*, int, char*); 233 void (*setposition)(UiText*,int); 234 int (*position)(UiText*); 235 void (*selection)(UiText*, int*, int*); /* text, begin, end */ 236 int (*length)(UiText*); 237 void (*remove)(UiText*, int, int); /* text, begin, end */ 238 UiStr value; 239 int pos; 240 void *obj; 241 void *undomgr; 242 // TODO: replacefunc, ... 243 UiObserver *observers; 244 }; 245 246 /* 247 * abstract list 248 */ 249 struct UiList { 250 /* get the first element */ 251 void*(*first)(UiList *list); 252 /* get the next element */ 253 void*(*next)(UiList *list); 254 /* get the nth element */ 255 void*(*get)(UiList *list, int i); 256 /* get the number of elements */ 257 int(*count)(UiList *list); 258 /* iterator changes after first() next() and get() */ 259 void *iter; 260 /* private - implementation dependent */ 261 void *data; 262 263 /* binding function */ 264 void (*update)(UiList *list, int i); 265 /* binding object */ 266 void *obj; 267 268 /* list of observers */ 269 UiObserver *observers; 270 }; 271 272 struct UiRange { 273 double (*get)(UiRange *range); 274 void (*set)(UiRange *range, double value); 275 void (*setrange)(UiRange *range, double min, double max); 276 void (*setextent)(UiRange *range, double extent); 277 double value; 278 double min; 279 double max; 280 double extent; 281 void *obj; 282 /* list of observers */ 283 UiObserver *observers; 284 }; 285 286 287 void ui_init(char *appname, int argc, char **argv); 288 char* ui_appname(); 289 290 UiContext* ui_global_context(void); 291 292 void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata); 293 294 void ui_onstartup(ui_callback f, void *userdata); 295 void ui_onopen(ui_callback f, void *userdata); 296 void ui_onexit(ui_callback f, void *userdata); 297 298 void ui_main(); 299 void ui_show(UiObject *obj); 300 void ui_close(UiObject *obj); 301 302 void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd); 303 304 void* ui_document_new(size_t size); 305 void ui_document_destroy(void *doc); 306 307 void ui_set_document(UiObject *obj, void *document); // deprecated 308 void ui_detach_document(UiObject *obj); // deprecated 309 void* ui_get_document(UiObject *obj); // deprecated 310 void ui_set_subdocument(void *document, void *sub); // deprecated 311 void ui_detach_subdocument(void *document, void *sub); // deprecated 312 void* ui_get_subdocument(void *document); // deprecated 313 314 UiContext* ui_document_context(void *doc); 315 316 void ui_attach_document(UiContext *ctx, void *document); 317 void ui_detach_document2(UiContext *ctx, void *document); 318 319 void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...); 320 321 void ui_set_group(UiContext *ctx, int group); 322 void ui_unset_group(UiContext *ctx, int group); 323 int* ui_active_groups(UiContext *ctx, int *ngroups); 324 325 void* ui_malloc(UiContext *ctx, size_t size); 326 void* ui_calloc(UiContext *ctx, size_t nelem, size_t elsize); 327 void ui_free(UiContext *ctx, void *ptr); 328 void* ui_realloc(UiContext *ctx, void *ptr, size_t size); 329 330 // types 331 332 UiInteger* ui_int_new(UiContext *ctx, char *name); 333 UiDouble* ui_double_new(UiContext *ctx, char *name); 334 UiString* ui_string_new(UiContext *ctx, char *name); 335 UiText* ui_text_new(UiContext *ctx, char *name); 336 UiRange* ui_range_new(UiContext *ctx, char *name); 337 338 UiObserver* ui_observer_new(ui_callback f, void *data); 339 UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer); 340 UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data); 341 void ui_notify(UiObserver *observer, void *data); 342 void ui_notify_except(UiObserver *observer, UiObserver *exc, void *data); 343 void ui_notify_evt(UiObserver *observer, UiEvent *event); 344 345 346 UiList* ui_list_new(UiContext *ctx, char *name); 347 void* ui_list_first(UiList *list); 348 void* ui_list_next(UiList *list); 349 void* ui_list_get(UiList *list, int i); 350 int ui_list_count(UiList *list); 351 void ui_list_append(UiList *list, void *data); 352 void ui_list_prepend(UiList *list, void *data); 353 void ui_list_clear(UiList *list); 354 void ui_list_addobsv(UiList *list, ui_callback f, void *data); 355 void ui_list_notify(UiList *list); 356 357 void ui_clipboard_set(char *str); 358 char* ui_clipboard_get(); 359 360 void ui_add_image(char *imgname, char *filename); // TODO: remove? 361 362 // general widget functions 363 void ui_set_enabled(UIWIDGET widget, int enabled); 364 void ui_set_show_all(UIWIDGET widget, int value); 365 void ui_set_visible(UIWIDGET widget, int visible); 366 367 368 #ifdef __cplusplus 369 } 370 #endif 371 372 #endif /* UI_TOOLKIT_H */ 373 374