2 * widgets/common.c - common widget functions or callbacks
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "globalconf.h"
25 #include "common/luaobject.h"
26 #include "common/lualib.h"
27 #include "widgets/common.h"
30 key_press_cb(GtkWidget
*win
, GdkEventKey
*ev
, widget_t
*w
)
34 lua_State
*L
= globalconf
.L
;
35 luaH_object_push(L
, w
->ref
);
36 luaH_modifier_table_push(L
, ev
->state
);
37 luaH_keystr_push(L
, ev
->keyval
);
38 ret
= luaH_object_emit_signal(L
, -3, "key-press", 2, 1);
40 return ret
? TRUE
: FALSE
;
45 button_press_cb(GtkWidget
*win
, GdkEventButton
*ev
, widget_t
*w
)
49 lua_State
*L
= globalconf
.L
;
50 luaH_object_push(L
, w
->ref
);
51 luaH_modifier_table_push(L
, ev
->state
);
52 lua_pushinteger(L
, ev
->button
);
53 ret
= luaH_object_emit_signal(L
, -3, "button-press", 2, 1);
54 /* User responded with TRUE, so do not propagate event any further */
55 if (ret
&& luaH_checkboolean(L
, -1)) {
60 /* propagate event further */
65 button_release_cb(GtkWidget
*win
, GdkEventButton
*ev
, widget_t
*w
)
69 lua_State
*L
= globalconf
.L
;
70 luaH_object_push(L
, w
->ref
);
71 luaH_modifier_table_push(L
, ev
->state
);
72 lua_pushinteger(L
, ev
->button
);
73 ret
= luaH_object_emit_signal(L
, -3, "button-release", 2, 1);
74 /* User responded with TRUE, so do not propagate event any further */
75 if (ret
&& luaH_checkboolean(L
, -1)) {
80 /* propagate event further */
85 focus_cb(GtkWidget
*win
, GdkEventFocus
*ev
, widget_t
*w
)
88 lua_State
*L
= globalconf
.L
;
89 luaH_object_push(L
, w
->ref
);
91 luaH_object_emit_signal(L
, -1, "focus", 0, 0);
93 luaH_object_emit_signal(L
, -1, "unfocus", 0, 0);
98 /* gtk container add callback */
100 add_cb(GtkContainer
*c
, GtkWidget
*widget
, widget_t
*w
)
103 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
104 lua_State
*L
= globalconf
.L
;
105 luaH_object_push(L
, w
->ref
);
106 luaH_object_push(L
, child
->ref
);
107 luaH_object_emit_signal(L
, -2, "add", 1, 0);
111 /* gtk container remove callback */
113 remove_cb(GtkContainer
*c
, GtkWidget
*widget
, widget_t
*w
)
116 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
117 lua_State
*L
= globalconf
.L
;
118 luaH_object_push(L
, w
->ref
);
119 luaH_object_push(L
, child
->ref
);
120 luaH_object_emit_signal(L
, -2, "remove", 1, 0);
125 parent_set_cb(GtkWidget
*widget
, GtkObject
*old
, widget_t
*w
)
128 lua_State
*L
= globalconf
.L
;
129 widget_t
*parent
= NULL
;
131 g_object_get(G_OBJECT(widget
), "parent", &new, NULL
);
132 luaH_object_push(L
, w
->ref
);
134 parent
= g_object_get_data(G_OBJECT(new), "widget");
136 luaH_object_push(L
, parent
->ref
);
139 luaH_object_emit_signal(L
, -2, "parent-set", 1, 0);
149 /* set child method for gtk container widgets */
151 luaH_widget_set_child(lua_State
*L
)
153 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
154 widget_t
*child
= luaH_checkudataornil(L
, 2, &widget_class
);
156 /* remove old child */
157 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(w
->widget
));
159 g_object_ref(G_OBJECT(widget
));
160 gtk_container_remove(GTK_CONTAINER(w
->widget
), GTK_WIDGET(widget
));
163 /* add new child to container */
165 gtk_container_add(GTK_CONTAINER(w
->widget
), GTK_WIDGET(child
->widget
));
169 /* get child method for gtk container widgets */
171 luaH_widget_get_child(lua_State
*L
)
173 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
174 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(w
->widget
));
175 widget_t
*child
= NULL
;
180 child
= g_object_get_data(G_OBJECT(child
), "lua_widget");
181 luaH_object_push(L
, child
->ref
);
186 luaH_widget_show(lua_State
*L
)
188 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
189 gtk_widget_show(w
->widget
);
194 luaH_widget_hide(lua_State
*L
)
196 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
197 gtk_widget_hide(w
->widget
);
202 luaH_widget_focus(lua_State
*L
)
204 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
205 gtk_widget_grab_focus(w
->widget
);
210 luaH_widget_destroy(lua_State
*L
)
212 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
215 w
->destructor
= NULL
;
216 luaH_object_unref(L
, w
->ref
);
220 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80