2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
33 #include "container.h"
34 #include <ucx/mempool.h>
35 #include "../common/context.h"
36 #include "../common/object.h"
38 UIWIDGET ui_button(UiObject *obj, char *label, ui_callback f, void *data) {
39 GtkWidget *button = gtk_button_new_with_label(label);
42 UiEventData *event = malloc(sizeof(UiEventData));
44 event->userdata = data;
51 G_CALLBACK(ui_button_clicked),
56 G_CALLBACK(ui_destroy_userdata),
60 UiContainer *ct = uic_get_current_container(obj);
61 ct->add(ct, button, FALSE);
67 void ui_button_clicked(GtkWidget *widget, UiEventData *event) {
70 e.window = event->obj->window;
71 e.document = event->obj->ctx->document;
73 e.intval = event->value;
74 event->callback(&e, event->userdata);
77 int64_t ui_toggle_button_get(UiInteger *integer) {
78 GtkToggleButton *button = integer->obj;
79 integer->value = (int)gtk_toggle_button_get_active(button);
80 return integer->value;
83 void ui_toggle_button_set(UiInteger *integer, int64_t value) {
84 GtkToggleButton *button = integer->obj;
85 integer->value = value;
86 gtk_toggle_button_set_active(button, value != 0 ? TRUE : FALSE);
89 void ui_toggled_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
92 e.window = event->obj->window;
93 e.document = event->obj->ctx->document;
94 e.eventdata = event->var->value;
95 e.intval = gtk_toggle_tool_button_get_active(widget);
97 UiInteger *i = event->var->value;
98 ui_notify_evt(i->observers, &e);
101 UIWIDGET ui_checkbox_var(UiObject *obj, char *label, UiVar *var) {
102 GtkWidget *button = gtk_check_button_new_with_label(label);
106 UiInteger *value = var->value;
107 value->obj = GTK_TOGGLE_BUTTON(button);
108 value->get = ui_toggle_button_get;
109 value->set = ui_toggle_button_set;
110 gtk_toggle_button_set_active(value->obj, value->value);
112 UiVarEventData *event = malloc(sizeof(UiVarEventData));
115 event->observers = NULL;
120 G_CALLBACK(ui_toggled_obs),
125 G_CALLBACK(ui_destroy_vardata),
129 UiContainer *ct = uic_get_current_container(obj);
130 ct->add(ct, button, FALSE);
135 UIWIDGET ui_checkbox(UiObject *obj, char *label, UiInteger *value) {
138 var = malloc(sizeof(UiVar));
140 var->type = UI_VAR_SPECIAL;
142 return ui_checkbox_var(obj, label, var);
145 UIWIDGET ui_checkbox_nv(UiObject *obj, char *label, char *varname) {
146 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
147 return ui_checkbox_var(obj, label, var);
151 UIWIDGET ui_radiobutton_var(UiObject *obj, char *label, UiVar *var) {
160 GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, label);
161 rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
165 rgroup->get = ui_radiobutton_get;
166 rgroup->set = ui_radiobutton_set;
168 ui_radiobutton_set(rgroup, rgroup->value);
170 UiVarEventData *event = malloc(sizeof(UiVarEventData));
173 event->observers = NULL;
178 G_CALLBACK(ui_radio_obs),
183 G_CALLBACK(ui_destroy_vardata),
187 UiContainer *ct = uic_get_current_container(obj);
188 ct->add(ct, rbutton, FALSE);
193 UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup) {
196 var = malloc(sizeof(UiVar));
198 var->type = UI_VAR_SPECIAL;
200 return ui_radiobutton_var(obj, label, var);
203 UIWIDGET ui_radiobutton_nv(UiObject *obj, char *label, char *varname) {
204 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
205 return ui_radiobutton_var(obj, label, var);
208 void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
209 UiInteger *i = event->var->value;
213 e.window = event->obj->window;
214 e.document = event->obj->ctx->document;
216 e.intval = i->get(i);
218 ui_notify_evt(i->observers, &e);
221 int64_t ui_radiobutton_get(UiInteger *value) {
223 GSList *ls = value->obj;
225 guint len = g_slist_length(ls);
227 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ls->data))) {
228 selection = len - i - 1;
235 value->value = selection;
239 void ui_radiobutton_set(UiInteger *value, int64_t i) {
240 GSList *ls = value->obj;
241 int s = g_slist_length(ls) - 1 - i;
245 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ls->data), TRUE);