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
;
44 button_release_cb(GtkWidget
*win
, GdkEventButton
*ev
, widget_t
*w
)
48 lua_State
*L
= globalconf
.L
;
49 luaH_object_push(L
, w
->ref
);
50 luaH_modifier_table_push(L
, ev
->state
);
51 lua_pushinteger(L
, ev
->button
);
52 ret
= luaH_object_emit_signal(L
, -3, "button-release", 2, 1);
53 /* User responded with TRUE, so do not propagate event any further */
54 if (ret
&& luaH_checkboolean(L
, -1)) {
59 /* propagate event further */
64 focus_cb(GtkWidget
*win
, GdkEventFocus
*ev
, widget_t
*w
)
67 lua_State
*L
= globalconf
.L
;
68 luaH_object_push(L
, w
->ref
);
70 luaH_object_emit_signal(L
, -1, "focus", 0, 0);
72 luaH_object_emit_signal(L
, -1, "unfocus", 0, 0);
77 /* gtk container add callback */
79 add_cb(GtkContainer
*c
, GtkWidget
*widget
, widget_t
*w
)
82 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
83 lua_State
*L
= globalconf
.L
;
84 luaH_object_push(L
, w
->ref
);
85 luaH_object_push(L
, child
->ref
);
86 luaH_object_emit_signal(L
, -2, "add", 1, 0);
90 /* gtk container remove callback */
92 remove_cb(GtkContainer
*c
, GtkWidget
*widget
, widget_t
*w
)
95 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
96 lua_State
*L
= globalconf
.L
;
97 luaH_object_push(L
, w
->ref
);
98 luaH_object_push(L
, child
->ref
);
99 luaH_object_emit_signal(L
, -2, "remove", 1, 0);
104 parent_set_cb(GtkWidget
*widget
, GtkObject
*old
, widget_t
*w
)
107 lua_State
*L
= globalconf
.L
;
108 widget_t
*parent
= NULL
;
110 g_object_get(G_OBJECT(widget
), "parent", &new, NULL
);
111 luaH_object_push(L
, w
->ref
);
112 if (new && (parent
= g_object_get_data(G_OBJECT(new), "widget")))
113 luaH_object_push(L
, parent
->ref
);
116 luaH_object_emit_signal(L
, -2, "parent-set", 1, 0);
126 /* set child method for gtk container widgets */
128 luaH_widget_set_child(lua_State
*L
)
130 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
131 widget_t
*child
= luaH_checkudataornil(L
, 2, &widget_class
);
133 /* remove old child */
134 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(w
->widget
));
136 g_object_ref(G_OBJECT(widget
));
137 gtk_container_remove(GTK_CONTAINER(w
->widget
), GTK_WIDGET(widget
));
140 /* add new child to container */
142 gtk_container_add(GTK_CONTAINER(w
->widget
), GTK_WIDGET(child
->widget
));
146 /* get child method for gtk container widgets */
148 luaH_widget_get_child(lua_State
*L
)
150 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
151 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(w
->widget
));
152 widget_t
*child
= NULL
;
157 child
= g_object_get_data(G_OBJECT(child
), "lua_widget");
158 luaH_object_push(L
, child
->ref
);
163 luaH_widget_show(lua_State
*L
)
165 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
166 gtk_widget_show(w
->widget
);
171 luaH_widget_hide(lua_State
*L
)
173 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
174 gtk_widget_hide(w
->widget
);
179 luaH_widget_focus(lua_State
*L
)
181 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
182 gtk_widget_grab_focus(w
->widget
);
187 luaH_widget_destroy(lua_State
*L
)
189 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
192 debug("unreffing widget %p of type '%s'", w
, w
->info
->name
);
193 luaH_object_unref(L
, w
->ref
);
198 widget_destructor(widget_t
*w
)
200 debug("destroying widget %p of type '%s'", w
, w
->info
->name
);
201 gtk_widget_destroy(GTK_WIDGET(w
->widget
));
205 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80