32 #include "../common/context.h" |
32 #include "../common/context.h" |
33 #include "../common/object.h" |
33 #include "../common/object.h" |
34 #include "container.h" |
34 #include "container.h" |
35 #include "entry.h" |
35 #include "entry.h" |
36 |
36 |
37 #include <ucx/mempool.h> |
37 |
38 |
38 UIWIDGET ui_spinner_create(UiObject *obj, UiSpinnerArgs args) { |
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; |
39 double min = 0; |
78 double max = 1000; |
40 double max = 1000; |
79 if(type == UI_VAR_RANGE) { |
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) { |
80 UiRange *r = var->value; |
60 UiRange *r = var->value; |
81 min = r->min; |
61 min = r->min; |
82 max = r->max; |
62 max = r->max; |
83 } |
63 } |
84 if(step == 0) { |
64 if(args.step == 0) { |
85 step = 1; |
65 args.step = 1; |
86 } |
66 } |
87 #ifdef UI_GTK2LEGACY |
67 #ifdef UI_GTK2LEGACY |
88 if(min == max) { |
68 if(min == max) { |
89 max = min + 1; |
69 max = min + 1; |
90 } |
70 } |
91 #endif |
71 #endif |
92 GtkWidget *spin = gtk_spin_button_new_with_range(min, max, step); |
72 GtkWidget *spin = gtk_spin_button_new_with_range(min, max, args.step); |
93 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits); |
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; |
94 if(var) { |
77 if(var) { |
95 double value = 0; |
78 double value = 0; |
96 UiObserver **obs = NULL; |
79 switch(var->type) { |
97 switch(type) { |
|
98 default: break; |
80 default: break; |
99 case UI_VAR_INTEGER: { |
81 case UI_VAR_INTEGER: { |
100 UiInteger *i = var->value; |
82 UiInteger *i = var->value; |
101 i->get = ui_spinbutton_getint; |
83 i->get = ui_spinbutton_getint; |
102 i->set = ui_spinbutton_setint; |
84 i->set = ui_spinbutton_setint; |
125 obs = &r->observers; |
107 obs = &r->observers; |
126 break; |
108 break; |
127 } |
109 } |
128 } |
110 } |
129 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value); |
111 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value); |
130 |
112 } |
131 UiVarEventData *event = malloc(sizeof(UiVarEventData)); |
113 |
132 event->obj = obj; |
114 UiVarEventData *event = malloc(sizeof(UiVarEventData)); |
133 event->var = var; |
115 event->obj = obj; |
134 event->observers = obs; |
116 event->var = var; |
135 |
117 event->observers = obs; |
136 g_signal_connect( |
118 event->callback = args.onchange; |
137 spin, |
119 event->userdata = args.onchangedata; |
138 "value-changed", |
120 |
139 G_CALLBACK(ui_spinner_changed), |
121 g_signal_connect( |
140 event); |
122 spin, |
141 g_signal_connect( |
123 "value-changed", |
142 spin, |
124 G_CALLBACK(ui_spinner_changed), |
143 "destroy", |
125 event); |
144 G_CALLBACK(ui_destroy_vardata), |
126 g_signal_connect( |
145 event); |
127 spin, |
146 } |
128 "destroy", |
147 |
129 G_CALLBACK(ui_destroy_vardata), |
148 UiContainer *ct = uic_get_current_container(obj); |
130 event); |
149 ct->add(ct, spin, FALSE); |
131 |
|
132 UI_APPLY_LAYOUT1(current, args); |
|
133 current->container->add(current->container, spin, FALSE); |
150 |
134 |
151 return spin; |
135 return spin; |
152 } |
136 } |
153 |
137 |
154 void ui_spinner_setrange(UIWIDGET spinner, double min, double max) { |
138 void ui_spinner_setrange(UIWIDGET spinner, double min, double max) { |
159 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spinner), digits); |
143 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spinner), digits); |
160 } |
144 } |
161 |
145 |
162 |
146 |
163 void ui_spinner_changed(GtkSpinButton *spinner, UiVarEventData *event) { |
147 void ui_spinner_changed(GtkSpinButton *spinner, UiVarEventData *event) { |
|
148 gdouble value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spinner)); |
164 UiEvent e; |
149 UiEvent e; |
165 e.obj = event->obj; |
150 e.obj = event->obj; |
166 e.window = event->obj->window; |
151 e.window = event->obj->window; |
167 e.document = event->obj->ctx->document; |
152 e.document = event->obj->ctx->document; |
168 e.eventdata = event->var->value; |
153 e.eventdata = &value; |
169 e.intval = 0; |
154 e.intval = (int64_t)value; |
170 |
155 |
171 UiObserver *observer = *event->observers; |
156 if(event->callback) { |
172 ui_notify_evt(observer, &e); |
157 event->callback(&e, event->userdata); |
|
158 } |
|
159 |
|
160 if(event->observers) { |
|
161 UiObserver *observer = *event->observers; |
|
162 ui_notify_evt(observer, &e); |
|
163 } |
173 } |
164 } |
174 |
165 |
175 |
166 |
176 int64_t ui_spinbutton_getint(UiInteger *i) { |
167 int64_t ui_spinbutton_getint(UiInteger *i) { |
177 i->value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(i->obj)); |
168 i->value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(i->obj)); |