ui/gtk/entry.c

changeset 0
2483f517c562
child 32
e5f4d8af567e
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
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 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "../common/context.h"
33 #include "../common/object.h"
34 #include "container.h"
35 #include "entry.h"
36
37
38 UIWIDGET ui_spinner(UiObject *obj, int step, UiInteger *i) {
39 UiVar *var = malloc(sizeof(UiVar));
40 var->value = i;
41 var->type = UI_VAR_SPECIAL;
42 return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
43 }
44
45 UIWIDGET ui_spinnerf(UiObject *obj, double step, int digits, UiDouble *d) {
46 UiVar *var = malloc(sizeof(UiVar));
47 var->value = d;
48 var->type = UI_VAR_SPECIAL;
49 return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
50 }
51
52 UIWIDGET ui_spinnerr(UiObject *obj, UiRange *r) {
53 UiVar *var = malloc(sizeof(UiVar));
54 var->value = r;
55 var->type = UI_VAR_SPECIAL;
56 return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
57 }
58
59 UIWIDGET ui_spinner_nv(UiObject *obj, int step, char *varname) {
60 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
61 return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
62 }
63
64 UIWIDGET ui_spinnerf_nv(UiObject *obj, double step, int digits, char *varname) {
65 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_DOUBLE);
66 return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
67 }
68
69 UIWIDGET ui_spinnerr_nv(UiObject *obj, char *varname) {
70 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_RANGE);
71 UiRange *r = var->value;
72 return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
73 }
74
75 UIWIDGET ui_spinner_var(UiObject *obj, double step, int digits, UiVar *var, UiVarType type) {
76 double min = 0;
77 double max = 1000;
78 if(type == UI_VAR_RANGE) {
79 UiRange *r = var->value;
80 min = r->min;
81 max = r->max;
82 }
83 if(step == 0) {
84 step = 1;
85 }
86 #ifdef UI_GTK2LEGACY
87 if(min == max) {
88 max = min + 1;
89 }
90 #endif
91 GtkWidget *spin = gtk_spin_button_new_with_range(min, max, step);
92 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
93 if(var) {
94 double value = 0;
95 UiObserver **obs = NULL;
96 switch(type) {
97 default: break;
98 case UI_VAR_INTEGER: {
99 UiInteger *i = var->value;
100 i->get = ui_spinbutton_getint;
101 i->set = ui_spinbutton_setint;
102 i->obj = spin;
103 value = (double)i->value;
104 obs = &i->observers;
105 break;
106 }
107 case UI_VAR_DOUBLE: {
108 UiDouble *d = var->value;
109 d->get = ui_spinbutton_getdouble;
110 d->set = ui_spinbutton_setdouble;
111 d->obj = spin;
112 value = d->value;
113 obs = &d->observers;
114 break;
115 }
116 case UI_VAR_RANGE: {
117 UiRange *r = var->value;
118 r->get = ui_spinbutton_getrangeval;
119 r->set = ui_spinbutton_setrangeval;
120 r->setrange = ui_spinbutton_setrange;
121 r->setextent = ui_spinbutton_setextent;
122 r->obj = spin;
123 value = r->value;
124 obs = &r->observers;
125 break;
126 }
127 }
128 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
129
130 UiVarEventData *event = malloc(sizeof(UiVarEventData));
131 event->obj = obj;
132 event->var = var;
133 event->observers = obs;
134
135 g_signal_connect(
136 spin,
137 "value-changed",
138 G_CALLBACK(ui_spinner_changed),
139 event);
140 g_signal_connect(
141 spin,
142 "destroy",
143 G_CALLBACK(ui_destroy_vardata),
144 event);
145 }
146
147 UiContainer *ct = uic_get_current_container(obj);
148 ct->add(ct, spin, FALSE);
149
150 return spin;
151 }
152
153 void ui_spinner_setrange(UIWIDGET spinner, double min, double max) {
154 gtk_spin_button_set_range(GTK_SPIN_BUTTON(spinner), min, max);
155 }
156
157 void ui_spinner_setdigits(UIWIDGET spinner, int digits) {
158 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spinner), digits);
159 }
160
161
162 void ui_spinner_changed(GtkSpinButton *spinner, UiVarEventData *event) {
163 UiEvent e;
164 e.obj = event->obj;
165 e.window = event->obj->window;
166 e.document = event->obj->ctx->document;
167 e.eventdata = event->var->value;
168 e.intval = 0;
169
170 UiObserver *observer = *event->observers;
171 ui_notify_evt(observer, &e);
172 }
173
174
175 int64_t ui_spinbutton_getint(UiInteger *i) {
176 i->value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(i->obj));
177 return i->value;
178 }
179
180 void ui_spinbutton_setint(UiInteger *i, int64_t val) {
181 gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->obj), (double)val);
182 i->value = val;
183 }
184
185 double ui_spinbutton_getdouble(UiDouble *d) {
186 d->value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(d->obj));
187 return d->value;
188 }
189
190 void ui_spinbutton_setdouble(UiDouble *d, double val) {
191 gtk_spin_button_set_value(GTK_SPIN_BUTTON(d->obj), val);
192 d->value = val;
193 }
194
195 double ui_spinbutton_getrangeval(UiRange *r) {
196 r->value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(r->obj));
197 return r->value;
198 }
199
200 void ui_spinbutton_setrangeval(UiRange *r, double val) {
201 gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->obj), val);
202 r->value = val;
203 }
204 void ui_spinbutton_setrange(UiRange *r, double min, double max) {
205 gtk_spin_button_set_range(GTK_SPIN_BUTTON(r->obj), min, max);
206 r->min = min;
207 r->max = max;
208 }
209
210 void ui_spinbutton_setextent(UiRange *r, double extent) {
211 gtk_spin_button_set_increments(GTK_SPIN_BUTTON(r->obj), extent, extent*10);
212 r->extent = extent;
213 }

mercurial