ui/gtk/entry.c

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

mercurial