ui/gtk/entry.c

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

mercurial