1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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_create(UiObject *obj, UiSpinnerArgs args) {
39 double min =
0;
40 double max =
1000;
41
42 UiObject* current = uic_current_obj(obj);
43
44 UiVar *var =
NULL;
45 if(args.varname) {
46 var = uic_get_var(obj->ctx, args.varname);
47 }
48
49 if(!var) {
50 if(args.intvalue) {
51 var = uic_widget_var(obj->ctx, current->ctx, args.intvalue,
NULL,
UI_VAR_INTEGER);
52 }
else if(args.doublevalue) {
53 var = uic_widget_var(obj->ctx, current->ctx, args.doublevalue,
NULL,
UI_VAR_DOUBLE);
54 }
else if(args.rangevalue) {
55 var = uic_widget_var(obj->ctx, current->ctx, args.rangevalue,
NULL,
UI_VAR_RANGE);
56 }
57 }
58
59 if(var && var->type ==
UI_VAR_RANGE) {
60 UiRange *r = var->value;
61 min = r->min;
62 max = r->max;
63 }
64 if(args.step ==
0) {
65 args.step =
1;
66 }
67 #ifdef UI_GTK2LEGACY
68 if(min == max) {
69 max = min +
1;
70 }
71 #endif
72 GtkWidget *spin = gtk_spin_button_new_with_range(min, max, args.step);
73 ui_set_name_and_style(spin, args.name, args.style_class);
74 ui_set_widget_groups(obj->ctx, spin, args.groups);
75 gtk_spin_button_set_digits(
GTK_SPIN_BUTTON(spin), args.digits);
76 UiObserver **obs =
NULL;
77 if(var) {
78 double value =
0;
79 switch(var->type) {
80 default:
break;
81 case UI_VAR_INTEGER: {
82 UiInteger *i = var->value;
83 i->get = ui_spinbutton_getint;
84 i->set = ui_spinbutton_setint;
85 i->obj = spin;
86 value = (
double)i->value;
87 obs = &i->observers;
88 break;
89 }
90 case UI_VAR_DOUBLE: {
91 UiDouble *d = var->value;
92 d->get = ui_spinbutton_getdouble;
93 d->set = ui_spinbutton_setdouble;
94 d->obj = spin;
95 value = d->value;
96 obs = &d->observers;
97 break;
98 }
99 case UI_VAR_RANGE: {
100 UiRange *r = var->value;
101 r->get = ui_spinbutton_getrangeval;
102 r->set = ui_spinbutton_setrangeval;
103 r->setrange = ui_spinbutton_setrange;
104 r->setextent = ui_spinbutton_setextent;
105 r->obj = spin;
106 value = r->value;
107 obs = &r->observers;
108 break;
109 }
110 }
111 gtk_spin_button_set_value(
GTK_SPIN_BUTTON(spin), value);
112 }
113
114 UiVarEventData *event = malloc(
sizeof(UiVarEventData));
115 event->obj = obj;
116 event->var = var;
117 event->observers = obs;
118 event->callback = args.onchange;
119 event->userdata = args.onchangedata;
120
121 g_signal_connect(
122 spin,
123 "value-changed",
124 G_CALLBACK(ui_spinner_changed),
125 event);
126 g_signal_connect(
127 spin,
128 "destroy",
129 G_CALLBACK(ui_destroy_vardata),
130 event);
131
132 UI_APPLY_LAYOUT1(current, args);
133 current->container->add(current->container, spin,
FALSE);
134
135 return spin;
136 }
137
138 void ui_spinner_setrange(
UIWIDGET spinner,
double min,
double max) {
139 gtk_spin_button_set_range(
GTK_SPIN_BUTTON(spinner), min, max);
140 }
141
142 void ui_spinner_setdigits(
UIWIDGET spinner,
int digits) {
143 gtk_spin_button_set_digits(
GTK_SPIN_BUTTON(spinner), digits);
144 }
145
146
147 void ui_spinner_changed(GtkSpinButton *spinner, UiVarEventData *event) {
148 gdouble value = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(spinner));
149 UiEvent e;
150 e.obj = event->obj;
151 e.window = event->obj->window;
152 e.document = event->obj->ctx->document;
153 e.eventdata = &value;
154 e.intval = (
int64_t)value;
155
156 if(event->callback) {
157 event->callback(&e, event->userdata);
158 }
159
160 if(event->observers) {
161 UiObserver *observer = *event->observers;
162 ui_notify_evt(observer, &e);
163 }
164 }
165
166
167 int64_t ui_spinbutton_getint(UiInteger *i) {
168 i->value = gtk_spin_button_get_value_as_int(
GTK_SPIN_BUTTON(i->obj));
169 return i->value;
170 }
171
172 void ui_spinbutton_setint(UiInteger *i,
int64_t val) {
173 gtk_spin_button_set_value(
GTK_SPIN_BUTTON(i->obj), (
double)val);
174 i->value = val;
175 }
176
177 double ui_spinbutton_getdouble(UiDouble *d) {
178 d->value = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(d->obj));
179 return d->value;
180 }
181
182 void ui_spinbutton_setdouble(UiDouble *d,
double val) {
183 gtk_spin_button_set_value(
GTK_SPIN_BUTTON(d->obj), val);
184 d->value = val;
185 }
186
187 double ui_spinbutton_getrangeval(UiRange *r) {
188 r->value = gtk_spin_button_get_value(
GTK_SPIN_BUTTON(r->obj));
189 return r->value;
190 }
191
192 void ui_spinbutton_setrangeval(UiRange *r,
double val) {
193 gtk_spin_button_set_value(
GTK_SPIN_BUTTON(r->obj), val);
194 r->value = val;
195 }
196 void ui_spinbutton_setrange(UiRange *r,
double min,
double max) {
197 gtk_spin_button_set_range(
GTK_SPIN_BUTTON(r->obj), min, max);
198 r->min = min;
199 r->max = max;
200 }
201
202 void ui_spinbutton_setextent(UiRange *r,
double extent) {
203 gtk_spin_button_set_increments(
GTK_SPIN_BUTTON(r->obj), extent, extent*
10);
204 r->extent = extent;
205 }
206