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 TOOLKIT_H 30 #define TOOLKIT_H 31 32 #include "../ui/toolkit.h" 33 #include "../common/context.h" 34 #include "../common/object.h" 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 41 42 43 #if GLIB_MAJOR_VERSION * 1000 + GLIB_MINOR_VERSION > 2074 44 #define UI_G_APPLICATION_FLAGS G_APPLICATION_DEFAULT_FLAGS 45 #else 46 #define UI_G_APPLICATION_FLAGS G_APPLICATION_FLAGS_NONE 47 #endif 48 49 #ifdef UI_LIBADWAITA 50 #define UI_APPLICATION AdwApplication* 51 #define UI_APPLICATION_NEW(id) adw_application_new(id, UI_G_APPLICATION_FLAGS) 52 #elif GTK_MAJOR_VERSION >= 3 53 #define UI_APPLICATION GtkApplication* 54 #define UI_APPLICATION_NEW(id) gtk_application_new(id, UI_G_APPLICATION_FLAGS) 55 #endif 56 57 #if GTK_MAJOR_VERSION >= 4 58 #define WINDOW_SHOW(window) gtk_window_present(GTK_WINDOW(window)) 59 #define WINDOW_DESTROY(window) gtk_window_destroy(GTK_WINDOW(window)) 60 #define WINDOW_SET_CONTENT(window, child) gtk_window_set_child(GTK_WINDOW(window), child) 61 #define BOX_ADD(box, child) gtk_box_append(GTK_BOX(box), child) 62 #define BOX_ADD_EXPAND(box, child) gtk_widget_set_hexpand(child, TRUE); gtk_widget_set_vexpand(child, TRUE); gtk_box_append(GTK_BOX(box), child) 63 #define BOX_ADD_NO_EXPAND(box, child) gtk_box_append(GTK_BOX(box), child) 64 #define BOX_REMOVE(box, child) gtk_box_remove(GTK_BOX(box), child) 65 #define ENTRY_SET_TEXT(entry, text) gtk_editable_set_text(GTK_EDITABLE(entry), text) 66 #define ENTRY_GET_TEXT(entry) gtk_editable_get_text(GTK_EDITABLE(entry)) 67 #define SCROLLEDWINDOW_NEW() gtk_scrolled_window_new() 68 #define SCROLLEDWINDOW_SET_CHILD(sw, child) gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), child) 69 #define SCROLLEDWINDOW_GET_CHILD(sw) gtk_scrolled_window_get_child(GTK_SCROLLED_WINDOW(sw)) 70 #define FRAME_SET_CHILD(frame, child) gtk_frame_set_child(GTK_FRAME(frame), child) 71 #define EXPANDER_SET_CHILD(expander, child) gtk_expander_set_child(GTK_EXPANDER(expander), child) 72 #define WIDGET_ADD_CSS_CLASS(w, cssclass) gtk_widget_add_css_class(w, cssclass) 73 #define WIDGET_REMOVE_CSS_CLASS(w, cssclass) gtk_widget_remove_css_class(w, cssclass) 74 #define ICON_IMAGE(icon) gtk_image_new_from_icon_name(icon) 75 #define LISTBOX_REMOVE(listbox, row) gtk_list_box_remove(GTK_LIST_BOX(listbox), row) 76 #define LISTBOX_ROW_SET_CHILD(row, child) gtk_list_box_row_set_child(GTK_LIST_BOX_ROW(row), child) 77 #else 78 #define WINDOW_SHOW(window) gtk_widget_show_all(window) 79 #define WINDOW_DESTROY(window) gtk_widget_destroy(window) 80 #define WINDOW_SET_CONTENT(window, child) gtk_container_add(GTK_CONTAINER(window), child) 81 #define BOX_ADD(box, child) gtk_container_add(GTK_CONTAINER(box), child) 82 #define BOX_ADD_EXPAND(box, child) gtk_box_pack_start(GTK_BOX(box), child, TRUE, TRUE, 0) 83 #define BOX_ADD_NO_EXPAND(box, child) gtk_box_pack_start(GTK_BOX(box), child, TRUE, FALSE, 0) 84 #define BOX_REMOVE(box, child) gtk_container_remove(GTK_CONTAINER(box), child) 85 #define ENTRY_SET_TEXT(entry, text) gtk_entry_set_text(GTK_ENTRY(entry), text) 86 #define ENTRY_GET_TEXT(entry) gtk_entry_get_text(GTK_ENTRY(entry)) 87 #define SCROLLEDWINDOW_NEW() gtk_scrolled_window_new(NULL, NULL) 88 #define SCROLLEDWINDOW_SET_CHILD(sw, child) gtk_container_add(GTK_CONTAINER(sw), child) 89 #define SCROLLEDWINDOW_GET_CHILD(sw) gtk_bin_get_child(GTK_BIN(sw)) 90 #define FRAME_SET_CHILD(frame, child) gtk_container_add(GTK_CONTAINER(frame), child) 91 #define EXPANDER_SET_CHILD(expander, child) gtk_container_add(GTK_CONTAINER(expander), child) 92 #define WIDGET_ADD_CSS_CLASS(w, cssclass) gtk_style_context_add_class(gtk_widget_get_style_context(w), cssclass) 93 #define WIDGET_REMOVE_CSS_CLASS(w, cssclass) gtk_style_context_remove_class(gtk_widget_get_style_context(w), cssclass) 94 #define ICON_IMAGE(icon) gtk_image_new_from_icon_name(icon, GTK_ICON_SIZE_BUTTON) 95 #define LISTBOX_REMOVE(listbox, row) gtk_container_remove(GTK_CONTAINER(listbox), row) 96 #define LISTBOX_ROW_SET_CHILD(row, child) gtk_container_add(GTK_CONTAINER(row), child) 97 #endif 98 99 #ifdef UI_GTK2 100 #undef SCROLLEDWINDOW_SET_CHILD 101 #define SCROLLEDWINDOW_SET_CHILD(sw, child) gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), child) 102 #endif 103 104 #if GTK_MAJOR_VERSION >= 4 105 #define UI_GTK_SINCE_V4(st) st 106 #define UI_GTK_SINCE_V3(st) 107 #define UI_GTK_V2(st) 108 #define UI_GTK_V3(st) 109 #define UI_GTK_V4(st) st 110 #elif GTK_MAJOR_VERSION >= 3 111 #define UI_GTK_SINCE_V4(st) st 112 #define UI_GTK_SINCE_V3(st) st 113 #define UI_GTK_V2(st) 114 #define UI_GTK_V3(st) st 115 #define UI_GTK_V4(st) 116 #else 117 #define UI_GTK_SINCE_V4(st) 118 #define UI_GTK_SINCE_V3(st) 119 #define UI_GTK_V2(st) st 120 #define UI_GTK_V3(st) 121 #define UI_GTK_V4(st) 122 #endif 123 124 125 typedef struct UiEventData { 126 UiObject *obj; 127 ui_callback callback; 128 void *userdata; 129 int value; 130 void *customdata; 131 } UiEventData; 132 133 typedef struct UiEventDataExt { 134 UiObject *obj; 135 ui_callback callback; 136 void *userdata; 137 ui_callback callback2; 138 void *userdata2; 139 int value0; 140 int value1; 141 int value2; 142 int value3; 143 void *customdata0; 144 void *customdata1; 145 void *customdata2; 146 void *customdata3; 147 } UiEventDataExt; 148 149 typedef struct UiVarEventData { 150 UiObject *obj; 151 UiVar *var; 152 UiObserver **observers; 153 ui_callback callback; 154 void *userdata; 155 } UiVarEventData; 156 157 typedef enum UiOrientation UiOrientation; 158 enum UiOrientation { UI_HORIZONTAL = 0, UI_VERTICAL }; 159 160 #ifndef UI_GTK4 161 struct UiSelection { 162 GtkSelectionData *data; 163 }; 164 #endif 165 166 #ifdef UI_APPLICATION 167 void ui_app_quit(); 168 GtkApplication* ui_get_application(); 169 #endif 170 171 int ui_get_scalefactor(); 172 173 void ui_set_name_and_style(GtkWidget *widget, const char *name, const char *style); 174 void ui_set_widget_groups(UiContext *ctx, GtkWidget *widget, const int *groups); 175 void ui_set_widget_ngroups(UiContext *ctx, GtkWidget *widget, const int *groups, size_t ngroups); 176 177 void ui_destroy_userdata(GtkWidget *object, void *userdata); 178 void ui_destroy_vardata(GtkWidget *object, UiVarEventData *data); 179 void ui_destroy_widget_var(GtkWidget *object, UiVar *var); 180 void ui_destroy_boundvar(UiContext *ctx, UiVar *var); 181 182 void ui_set_active_window(UiObject *obj); 183 UiObject *ui_get_active_window(); 184 185 #if GTK_MAJOR_VERSION >= 3 186 void ui_css_init(void); 187 #endif 188 189 #ifdef __cplusplus 190 } 191 #endif 192 193 #endif /* TOOLKIT_H */ 194 195