ui/gtk/button.c

changeset 32
e5f4d8af567e
parent 29
3fc287f06305
child 44
473954dc6b74
equal deleted inserted replaced
31:bf810176ddb8 32:e5f4d8af567e
33 #include "container.h" 33 #include "container.h"
34 #include <cx/allocator.h> 34 #include <cx/allocator.h>
35 #include "../common/context.h" 35 #include "../common/context.h"
36 #include "../common/object.h" 36 #include "../common/object.h"
37 37
38 UIWIDGET ui_button_deprecated(UiObject *obj, char *label, ui_callback f, void *data) { 38 void ui_button_set_icon_name(GtkWidget *button, const char *icon) {
39 GtkWidget *button = gtk_button_new_with_label(label); 39 if(!icon) {
40 40 return;
41 if(f) { 41 }
42
43 #ifdef UI_GTK4
44 gtk_button_set_icon_name(GTK_BUTTON(button), icon);
45 #else
46 #if GTK_CHECK_VERSION(2, 6, 0)
47 GtkWidget *image = gtk_image_new_from_icon_name(icon, GTK_ICON_SIZE_BUTTON);
48 if(image) {
49 gtk_button_set_image(GTK_BUTTON(button), image);
50 }
51 #else
52 // TODO
53 #endif
54 #endif
55 }
56
57 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs args) {
58 UiObject* current = uic_current_obj(obj);
59 GtkWidget *button = gtk_button_new();
60 if(args.label) {
61 gtk_button_set_label(GTK_BUTTON(button), args.label);
62 }
63 ui_button_set_icon_name(button, args.icon);
64
65
66 if(args.onclick) {
42 UiEventData *event = malloc(sizeof(UiEventData)); 67 UiEventData *event = malloc(sizeof(UiEventData));
43 event->obj = obj; 68 event->obj = obj;
44 event->userdata = data; 69 event->userdata = args.onclickdata;
45 event->callback = f; 70 event->callback = args.onclick;
46 event->value = 0; 71 event->value = 0;
72 event->customdata = NULL;
47 73
48 g_signal_connect( 74 g_signal_connect(
49 button, 75 button,
50 "clicked", 76 "clicked",
51 G_CALLBACK(ui_button_clicked), 77 G_CALLBACK(ui_button_clicked),
55 "destroy", 81 "destroy",
56 G_CALLBACK(ui_destroy_userdata), 82 G_CALLBACK(ui_destroy_userdata),
57 event); 83 event);
58 } 84 }
59 85
60 UiContainer *ct = uic_get_current_container(obj); 86 UI_APPLY_LAYOUT1(current, args);
61 ct->add(ct, button, FALSE); 87 current->container->add(current->container, button, FALSE);
62 88
63 return button; 89 return button;
64 } 90 }
65 91
66 92
96 122
97 UiInteger *i = event->var->value; 123 UiInteger *i = event->var->value;
98 ui_notify_evt(i->observers, &e); 124 ui_notify_evt(i->observers, &e);
99 } 125 }
100 126
101 UIWIDGET ui_checkbox_var(UiObject *obj, char *label, UiVar *var) { 127 static UIWIDGET togglebutton_create(UiObject *obj, GtkWidget *widget, UiToggleArgs args) {
102 GtkWidget *button = gtk_check_button_new_with_label(label); 128 UiObject* current = uic_current_obj(obj);
103 129
104 // bind value 130 if(args.label) {
105 if(var) { 131 gtk_button_set_label(GTK_BUTTON(widget), args.label);
106 UiInteger *value = var->value; 132 }
107 value->obj = GTK_TOGGLE_BUTTON(button); 133 ui_button_set_icon_name(widget, args.icon);
134
135 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER);
136 if (var) {
137 UiInteger* value = (UiInteger*)var->value;
138 value->obj = widget;
108 value->get = ui_toggle_button_get; 139 value->get = ui_toggle_button_get;
109 value->set = ui_toggle_button_set; 140 value->set = ui_toggle_button_set;
110 gtk_toggle_button_set_active(value->obj, value->value);
111 141
112 UiVarEventData *event = malloc(sizeof(UiVarEventData)); 142 UiVarEventData *event = malloc(sizeof(UiVarEventData));
113 event->obj = obj; 143 event->obj = obj;
114 event->var = var; 144 event->var = var;
115 event->observers = NULL; 145 event->observers = NULL;
116 146 event->callback = NULL;
117 g_signal_connect( 147 event->userdata = NULL;
118 button, 148
149 g_signal_connect(
150 widget,
119 "clicked", 151 "clicked",
120 G_CALLBACK(ui_toggled_obs), 152 G_CALLBACK(ui_toggled_obs),
121 event); 153 event);
122 g_signal_connect( 154 g_signal_connect(
123 button, 155 widget,
124 "destroy", 156 "destroy",
125 G_CALLBACK(ui_destroy_vardata), 157 G_CALLBACK(ui_destroy_vardata),
126 event); 158 event);
127 } 159 }
128 160
129 UiContainer *ct = uic_get_current_container(obj); 161 UI_APPLY_LAYOUT1(current, args);
130 ct->add(ct, button, FALSE); 162 current->container->add(current->container, widget, FALSE);
131 163
132 return button; 164 return widget;
133 } 165 }
134 166
135 UIWIDGET ui_checkbox_deprecated(UiObject *obj, char *label, UiInteger *value) { 167 UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) {
136 UiVar *var = NULL; 168 return togglebutton_create(obj, gtk_toggle_button_new(), args);
137 if(value) { 169 }
138 var = malloc(sizeof(UiVar)); 170
139 var->value = value; 171 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
140 var->type = UI_VAR_SPECIAL; 172 return togglebutton_create(obj, gtk_check_button_new(), args);
141 } 173 }
142 return ui_checkbox_var(obj, label, var); 174
143 } 175 UIWIDGET ui_switch_create(UiObject* obj, UiToggleArgs args) {
144 176 #ifdef UI_GTK3
145 UIWIDGET ui_checkbox_nv(UiObject *obj, char *label, char *varname) { 177 return NULL; // TODO
146 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER); 178 #else
147 return ui_checkbox_var(obj, label, var); 179 return ui_checkbox_create(obj, args);
148 } 180 #endif
149 181 }
150 182
151 UIWIDGET ui_radiobutton_var(UiObject *obj, char *label, UiVar *var) { 183
184
185
186
187
188 UIWIDGET ui_radiobutton_create(UiObject *obj, UiToggleArgs args) {
189 UiObject* current = uic_current_obj(obj);
190
152 GSList *rg = NULL; 191 GSList *rg = NULL;
153 UiInteger *rgroup; 192 UiInteger *rgroup;
154 193
194 UiVar* var = NULL;
195 if (args.value) {
196 var = uic_create_value_var(current->ctx, args.value);
197 } else if (args.varname) {
198 var = uic_create_var(obj->ctx, args.varname, UI_VAR_INTEGER);
199 }
200
201 UiBool first = FALSE;
155 if(var) { 202 if(var) {
156 rgroup = var->value; 203 rgroup = var->value;
157 rg = rgroup->obj; 204 rg = rgroup->obj;
158 } 205 if(!rg) {
159 206 first = TRUE;
160 GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, label); 207 }
208 }
209
210 GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, args.label ? args.label : "");
161 rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton)); 211 rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
162 212
163 if(rgroup) { 213 if(rgroup) {
164 rgroup->obj = rg; 214 rgroup->obj = rg;
165 rgroup->get = ui_radiobutton_get; 215 rgroup->get = ui_radiobutton_get;
169 219
170 UiVarEventData *event = malloc(sizeof(UiVarEventData)); 220 UiVarEventData *event = malloc(sizeof(UiVarEventData));
171 event->obj = obj; 221 event->obj = obj;
172 event->var = var; 222 event->var = var;
173 event->observers = NULL; 223 event->observers = NULL;
224 event->callback = NULL;
225 event->userdata = NULL;
174 226
175 g_signal_connect( 227 g_signal_connect(
176 rbutton, 228 rbutton,
177 "clicked", 229 "clicked",
178 G_CALLBACK(ui_radio_obs), 230 G_CALLBACK(ui_radio_obs),
179 event); 231 event);
180 g_signal_connect( 232 if(first) {
233 g_signal_connect(
181 rbutton, 234 rbutton,
182 "destroy", 235 "destroy",
183 G_CALLBACK(ui_destroy_vardata), 236 G_CALLBACK(ui_destroy_vardata),
184 event); 237 event);
238 }
185 } 239 }
186 240
187 UiContainer *ct = uic_get_current_container(obj); 241 UiContainer *ct = uic_get_current_container(obj);
188 ct->add(ct, rbutton, FALSE); 242 ct->add(ct, rbutton, FALSE);
189 243
190 return rbutton; 244 return rbutton;
191 }
192
193 UIWIDGET ui_radiobutton_deprecated(UiObject *obj, char *label, UiInteger *rgroup) {
194 UiVar *var = NULL;
195 if(rgroup) {
196 var = malloc(sizeof(UiVar));
197 var->value = rgroup;
198 var->type = UI_VAR_SPECIAL;
199 }
200 return ui_radiobutton_var(obj, label, var);
201 }
202
203 UIWIDGET ui_radiobutton_nv(UiObject *obj, char *label, char *varname) {
204 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
205 return ui_radiobutton_var(obj, label, var);
206 } 245 }
207 246
208 void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) { 247 void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
209 UiInteger *i = event->var->value; 248 UiInteger *i = event->var->value;
210 249

mercurial