ui/gtk/button.c

changeset 1096
655349c3021e
parent 1063
e0251f6e15f7
equal deleted inserted replaced
1095:e519ecc27cfe 1096:655349c3021e
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 31
32 #include "button.h" 32 #include "button.h"
33 #include "container.h" 33 #include "container.h"
34 #include "widget.h"
34 #include <cx/allocator.h> 35 #include <cx/allocator.h>
35 #include <cx/buffer.h> 36 #include <cx/buffer.h>
36 #include <cx/json.h> 37 #include <cx/json.h>
37 #include "../common/context.h" 38 #include "../common/context.h"
38 #include "../common/object.h" 39 #include "../common/object.h"
40 #include "../common/action.h"
39 41
40 void ui_button_set_icon_name(GtkWidget *button, const char *icon) { 42 void ui_button_set_icon_name(GtkWidget *button, const char *icon) {
41 if(!icon) { 43 if(!icon) {
42 return; 44 return;
43 } 45 }
61 const char *label, 63 const char *label,
62 const char *icon, 64 const char *icon,
63 const char *tooltip, 65 const char *tooltip,
64 ui_callback onclick, 66 ui_callback onclick,
65 void *userdata, 67 void *userdata,
68 const char *action,
66 int event_value, 69 int event_value,
67 bool activate_event) 70 bool activate_event)
68 { 71 {
69 GtkWidget *button = gtk_button_new_with_label(label); 72 GtkWidget *button = gtk_button_new_with_label(label);
70 ui_button_set_icon_name(button, icon); 73 ui_button_set_icon_name(button, icon);
71 if(tooltip) { 74 if(tooltip) {
72 gtk_widget_set_tooltip_text(button, tooltip); 75 gtk_widget_set_tooltip_text(button, tooltip);
73 } 76 }
74 77
75 if(onclick) { 78 if(onclick || action) {
76 UiEventData *event = malloc(sizeof(UiEventData)); 79 UiEventData *event = malloc(sizeof(UiEventData));
77 event->obj = obj; 80 event->obj = obj;
78 event->userdata = userdata; 81 event->userdata = userdata;
79 event->callback = onclick; 82 event->callback = onclick;
80 event->value = event_value; 83 event->value = event_value;
84 event->action = action ? strdup(action) : NULL;
81 event->customdata = NULL; 85 event->customdata = NULL;
82 event->customint = 0; 86 event->customint = 0;
83 87
84 g_signal_connect( 88 g_signal_connect(
85 button, 89 button,
87 G_CALLBACK(ui_button_clicked), 91 G_CALLBACK(ui_button_clicked),
88 event); 92 event);
89 g_signal_connect( 93 g_signal_connect(
90 button, 94 button,
91 "destroy", 95 "destroy",
92 G_CALLBACK(ui_destroy_userdata), 96 G_CALLBACK(ui_destroy_eventdata),
93 event); 97 event);
94 if(activate_event) { 98 if(activate_event) {
95 g_signal_connect( 99 g_signal_connect(
96 button, 100 button,
97 "activate", 101 "activate",
98 G_CALLBACK(ui_button_clicked), 102 G_CALLBACK(ui_button_clicked),
99 event); 103 event);
100 } 104 }
105
106 if(action) {
107 uic_bind_action(obj->ctx, action, button, (ui_enablefunc)ui_set_enabled);
108 UiAction *ui_action = uic_resolve_action(obj->ctx, action);
109 if(!ui_action) {
110 ui_set_enabled(button, FALSE);
111 }
112 }
101 } 113 }
102 114
103 return button; 115 return button;
104 } 116 }
105 117
106 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs *args) { 118 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs *args) {
107 GtkWidget *button = ui_create_button(obj, args->label, args->icon, args->tooltip, args->onclick, args->onclickdata, 0, FALSE); 119 GtkWidget *button = ui_create_button(obj, args->label, args->icon, args->tooltip, args->onclick, args->onclickdata, args->action, 0, FALSE);
108 ui_set_name_and_style(button, args->name, args->style_class); 120 ui_set_name_and_style(button, args->name, args->style_class);
109 ui_set_widget_states(obj->ctx, button, args->states); 121 ui_set_widget_states(obj->ctx, button, args->states);
110 UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end; 122 UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
111 UiLayout layout = UI_ARGS2LAYOUT(args); 123 UiLayout layout = UI_ARGS2LAYOUT(args);
112 ct->add(ct, button, &layout); 124 ct->add(ct, button, &layout);
122 e.document = event->obj->ctx->document; 134 e.document = event->obj->ctx->document;
123 e.eventdata = NULL; 135 e.eventdata = NULL;
124 e.eventdatatype = 0; 136 e.eventdatatype = 0;
125 e.intval = event->value; 137 e.intval = event->value;
126 e.set = ui_get_setop(); 138 e.set = ui_get_setop();
127 event->callback(&e, event->userdata); 139 if(event->callback) {
140 event->callback(&e, event->userdata);
141 }
142
143 if(event->action) {
144 uic_action_callback(&e, event->action);
145 }
128 } 146 }
129 147
130 void ui_button_set_label(UIWIDGET button, const char *label) { 148 void ui_button_set_label(UIWIDGET button, const char *label) {
131 gtk_button_set_label(GTK_BUTTON(button), label); 149 gtk_button_set_label(GTK_BUTTON(button), label);
132 } 150 }

mercurial