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.
32 #include "../common/context.h"
33 #include "../common/object.h"
34 #include "container.h"
37 #include <ucx/mempool.h>
39 UIWIDGET ui_spinner(UiObject *obj, int step, UiInteger *i) {
40 UiVar *var = malloc(sizeof(UiVar));
42 var->type = UI_VAR_SPECIAL;
43 return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
46 UIWIDGET ui_spinnerf(UiObject *obj, double step, int digits, UiDouble *d) {
47 UiVar *var = malloc(sizeof(UiVar));
49 var->type = UI_VAR_SPECIAL;
50 return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
53 UIWIDGET ui_spinnerr(UiObject *obj, UiRange *r) {
54 UiVar *var = malloc(sizeof(UiVar));
56 var->type = UI_VAR_SPECIAL;
57 return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
60 UIWIDGET ui_spinner_nv(UiObject *obj, int step, char *varname) {
61 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
62 return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
65 UIWIDGET ui_spinnerf_nv(UiObject *obj, double step, int digits, char *varname) {
66 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_DOUBLE);
67 return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
70 UIWIDGET ui_spinnerr_nv(UiObject *obj, char *varname) {
71 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_RANGE);
72 UiRange *r = var->value;
73 return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
76 UIWIDGET ui_spinner_var(UiObject *obj, double step, int digits, UiVar *var, UiVarType type) {
79 if(type == UI_VAR_RANGE) {
80 UiRange *r = var->value;
92 GtkWidget *spin = gtk_spin_button_new_with_range(min, max, step);
93 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
96 UiObserver **obs = NULL;
99 case UI_VAR_INTEGER: {
100 UiInteger *i = var->value;
101 i->get = ui_spinbutton_getint;
102 i->set = ui_spinbutton_setint;
104 value = (double)i->value;
108 case UI_VAR_DOUBLE: {
109 UiDouble *d = var->value;
110 d->get = ui_spinbutton_getdouble;
111 d->set = ui_spinbutton_setdouble;
118 UiRange *r = var->value;
119 r->get = ui_spinbutton_getrangeval;
120 r->set = ui_spinbutton_setrangeval;
121 r->setrange = ui_spinbutton_setrange;
122 r->setextent = ui_spinbutton_setextent;
129 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
131 UiVarEventData *event = malloc(sizeof(UiVarEventData));
134 event->observers = obs;
139 G_CALLBACK(ui_spinner_changed),
144 G_CALLBACK(ui_destroy_vardata),
148 UiContainer *ct = uic_get_current_container(obj);
149 ct->add(ct, spin, FALSE);
154 void ui_spinner_setrange(UIWIDGET spinner, double min, double max) {
155 gtk_spin_button_set_range(GTK_SPIN_BUTTON(spinner), min, max);
158 void ui_spinner_setdigits(UIWIDGET spinner, int digits) {
159 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spinner), digits);
163 void ui_spinner_changed(GtkSpinButton *spinner, UiVarEventData *event) {
166 e.window = event->obj->window;
167 e.document = event->obj->ctx->document;
168 e.eventdata = event->var->value;
171 UiObserver *observer = *event->observers;
172 ui_notify_evt(observer, &e);
176 int64_t ui_spinbutton_getint(UiInteger *i) {
177 i->value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(i->obj));
181 void ui_spinbutton_setint(UiInteger *i, int64_t val) {
182 gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->obj), (double)val);
186 double ui_spinbutton_getdouble(UiDouble *d) {
187 d->value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(d->obj));
191 void ui_spinbutton_setdouble(UiDouble *d, double val) {
192 gtk_spin_button_set_value(GTK_SPIN_BUTTON(d->obj), val);
196 double ui_spinbutton_getrangeval(UiRange *r) {
197 r->value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(r->obj));
201 void ui_spinbutton_setrangeval(UiRange *r, double val) {
202 gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->obj), val);
205 void ui_spinbutton_setrange(UiRange *r, double min, double max) {
206 gtk_spin_button_set_range(GTK_SPIN_BUTTON(r->obj), min, max);
211 void ui_spinbutton_setextent(UiRange *r, double extent) {
212 gtk_spin_button_set_increments(GTK_SPIN_BUTTON(r->obj), extent, extent*10);