ui/gtk/button.c

changeset 142
46448d38885c
parent 140
c03c338a7dcf
child 143
d499b29d7cb6
equal deleted inserted replaced
141:cc2170ea05ad 142:46448d38885c
97 GtkToggleButton *button = integer->obj; 97 GtkToggleButton *button = integer->obj;
98 integer->value = value; 98 integer->value = value;
99 gtk_toggle_button_set_active(button, value != 0 ? TRUE : FALSE); 99 gtk_toggle_button_set_active(button, value != 0 ? TRUE : FALSE);
100 } 100 }
101 101
102 102 void ui_toggled_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
103 UIWIDGET ui_checkbox(UiObject *obj, char *label, UiInteger *value) { 103 UiEvent e;
104 e.obj = event->obj;
105 e.window = event->obj->window;
106 e.document = event->obj->ctx->document;
107 e.eventdata = NULL;
108 e.intval = gtk_toggle_tool_button_get_active(widget);
109
110 UiInteger *i = event->var->value;
111 ui_notify_evt(i->observers, &e);
112 }
113
114 UIWIDGET ui_checkbox_var(UiObject *obj, char *label, UiVar *var) {
104 GtkWidget *button = gtk_check_button_new_with_label(label); 115 GtkWidget *button = gtk_check_button_new_with_label(label);
105 116
106 // bind value 117 // bind value
107 if(value) { 118 if(var) {
119 UiInteger *value = var->value;
108 value->obj = GTK_TOGGLE_BUTTON(button); 120 value->obj = GTK_TOGGLE_BUTTON(button);
109 value->get = ui_toggle_button_get; 121 value->get = ui_toggle_button_get;
110 value->set = ui_toggle_button_set; 122 value->set = ui_toggle_button_set;
111 if(value->value != 0) { 123 gtk_toggle_button_set_active(value->obj, value->value);
112 gtk_toggle_button_set_active(value->obj, TRUE); 124
113 } 125 UiVarEventData *event = malloc(sizeof(UiVarEventData));
126 event->obj = obj;
127 event->var = var;
128
129 g_signal_connect(
130 button,
131 "clicked",
132 G_CALLBACK(ui_toggled_obs),
133 event);
134 g_signal_connect(
135 button,
136 "destroy",
137 G_CALLBACK(ui_destroy_vardata),
138 event);
114 } 139 }
115 140
116 UiContainer *ct = uic_get_current_container(obj); 141 UiContainer *ct = uic_get_current_container(obj);
117 ct->add(ct, button, FALSE); 142 ct->add(ct, button, FALSE);
118 143
119 return button; 144 return button;
120 } 145 }
121 146
122 UIWIDGET ui_checkbox_cb(UiObject *obj, char *label, ui_callback f, void *data) { 147 UIWIDGET ui_checkbox(UiObject *obj, char *label, UiInteger *value) {
123 GtkWidget *button = gtk_check_button_new_with_label(label); 148 UiVar *var = NULL;
124 149 if(value) {
125 if(f) { 150 var = malloc(sizeof(UiVar));
126 //UiEventData *event = ucx_mempool_malloc( 151 var->value = value;
127 // obj->ctx->mempool, 152 var->type = UI_VAR_SPECIAL;
128 // sizeof(UiEventData)); 153 }
129 UiEventData *event = malloc(sizeof(UiEventData)); 154 return ui_checkbox_var(obj, label, var);
130 event->obj = obj; 155 }
131 event->userdata = data; 156
132 event->callback = f; 157 UIWIDGET ui_checkbox_nv(UiObject *obj, char *label, char *varname) {
133 event->value = 0; 158 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
134 159 return ui_checkbox_var(obj, label, var);
135 g_signal_connect( 160 }
136 button, 161
137 "clicked", 162
138 G_CALLBACK(ui_button_toggled), 163 UIWIDGET ui_radiobutton_var(UiObject *obj, char *label, UiVar *var) {
139 event); 164 GSList *rg = NULL;
140 g_signal_connect( 165 UiInteger *rgroup;
141 button, 166
142 "destroy", 167 if(var) {
143 G_CALLBACK(ui_destroy_userdata), 168 rgroup = var->value;
144 event); 169 rg = rgroup->obj;
145 } 170 }
146
147 UiContainer *ct = uic_get_current_container(obj);
148 ct->add(ct, button, FALSE);
149
150 return button;
151 }
152
153
154
155 UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup) {
156 GSList *rg = rgroup ? rgroup->obj : NULL;
157 171
158 GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, label); 172 GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, label);
159 rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton)); 173 rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
160 174
161 if(rgroup) { 175 if(rgroup) {
162 rgroup->obj = rg; 176 rgroup->obj = rg;
163 rgroup->get = ui_radiobutton_get; 177 rgroup->get = ui_radiobutton_get;
164 rgroup->set = ui_radiobutton_set; 178 rgroup->set = ui_radiobutton_set;
165 if(rgroup->value > 0) { 179
166 ui_radiobutton_set(rgroup, rgroup->value); 180 ui_radiobutton_set(rgroup, rgroup->value);
167 } 181
182 UiVarEventData *event = malloc(sizeof(UiVarEventData));
183 event->obj = obj;
184 event->var = var;
185
186 g_signal_connect(
187 rbutton,
188 "clicked",
189 G_CALLBACK(ui_radio_obs),
190 event);
191 g_signal_connect(
192 rbutton,
193 "destroy",
194 G_CALLBACK(ui_destroy_vardata),
195 event);
168 } 196 }
169 197
170 UiContainer *ct = uic_get_current_container(obj); 198 UiContainer *ct = uic_get_current_container(obj);
171 ct->add(ct, rbutton, FALSE); 199 ct->add(ct, rbutton, FALSE);
172 200
173 return rbutton; 201 return rbutton;
202 }
203
204 UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup) {
205 UiVar *var = NULL;
206 if(rgroup) {
207 var = malloc(sizeof(UiVar));
208 var->value = rgroup;
209 var->type = UI_VAR_SPECIAL;
210 }
211 return ui_radiobutton_var(obj, label, var);
212 }
213
214 UIWIDGET ui_radiobutton_nv(UiObject *obj, char *label, char *varname) {
215 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
216 return ui_radiobutton_var(obj, label, var);
217 }
218
219 void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
220 UiInteger *i = event->var->value;
221
222 UiEvent e;
223 e.obj = event->obj;
224 e.window = event->obj->window;
225 e.document = event->obj->ctx->document;
226 e.eventdata = NULL;
227 e.intval = i->get(i);
228
229 ui_notify_evt(i->observers, &e);
174 } 230 }
175 231
176 int ui_radiobutton_get(UiInteger *value) { 232 int ui_radiobutton_get(UiInteger *value) {
177 int selection = 0; 233 int selection = 0;
178 GSList *ls = value->obj; 234 GSList *ls = value->obj;
179 int i = 0; 235 int i = 0;
236 guint len = g_slist_length(ls);
180 while(ls) { 237 while(ls) {
181 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ls->data))) { 238 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ls->data))) {
182 selection = g_slist_length(value->obj) - i - 1; 239 selection = len - i - 1;
183 break; 240 break;
184 } 241 }
185 ls = ls->next; 242 ls = ls->next;
186 i++; 243 i++;
187 } 244 }
201 } 258 }
202 ls = ls->next; 259 ls = ls->next;
203 j++; 260 j++;
204 } 261 }
205 262
206 value->value = j; 263 value->value = i;
207 } 264 }
208 265

mercurial