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 focus_cb(GtkWidget
*win
, GdkEventFocus
*ev
, widget_t
*w
)
47 lua_State
*L
= globalconf
.L
;
48 luaH_object_push(L
, w
->ref
);
50 luaH_object_emit_signal(L
, -1, "focus", 0, 0);
52 luaH_object_emit_signal(L
, -1, "unfocus", 0, 0);
57 /* gtk container add callback */
59 add_cb(GtkContainer
*c
, GtkWidget
*widget
, widget_t
*w
)
62 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
63 lua_State
*L
= globalconf
.L
;
64 luaH_object_push(L
, w
->ref
);
65 luaH_object_push(L
, child
->ref
);
66 luaH_object_emit_signal(L
, -2, "add", 1, 0);
70 /* gtk container remove callback */
72 remove_cb(GtkContainer
*c
, GtkWidget
*widget
, widget_t
*w
)
75 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
76 lua_State
*L
= globalconf
.L
;
77 luaH_object_push(L
, w
->ref
);
78 luaH_object_push(L
, child
->ref
);
79 luaH_object_emit_signal(L
, -2, "remove", 1, 0);
84 parent_set_cb(GtkWidget
*widget
, GtkObject
*old
, widget_t
*w
)
87 lua_State
*L
= globalconf
.L
;
88 widget_t
*parent
= NULL
;
90 g_object_get(G_OBJECT(widget
), "parent", &new, NULL
);
91 luaH_object_push(L
, w
->ref
);
93 parent
= g_object_get_data(G_OBJECT(new), "widget");
95 luaH_object_push(L
, parent
->ref
);
98 luaH_object_emit_signal(L
, -2, "parent-set", 1, 0);
102 /* set child method for gtk container widgets */
104 luaH_widget_set_child(lua_State
*L
)
106 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
107 widget_t
*child
= luaH_checkudataornil(L
, 2, &widget_class
);
109 /* remove old child */
110 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(w
->widget
));
112 g_object_ref(G_OBJECT(widget
));
113 gtk_container_remove(GTK_CONTAINER(w
->widget
), GTK_WIDGET(widget
));
116 /* add new child to container */
118 gtk_container_add(GTK_CONTAINER(w
->widget
), GTK_WIDGET(child
->widget
));
122 /* get child method for gtk container widgets */
124 luaH_widget_get_child(lua_State
*L
)
126 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
127 GtkWidget
*widget
= gtk_bin_get_child(GTK_BIN(w
->widget
));
128 widget_t
*child
= NULL
;
133 child
= g_object_get_data(G_OBJECT(child
), "lua_widget");
134 luaH_object_push(L
, child
->ref
);
139 luaH_widget_show(lua_State
*L
)
141 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
142 gtk_widget_show(w
->widget
);
147 luaH_widget_hide(lua_State
*L
)
149 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
150 gtk_widget_hide(w
->widget
);
155 luaH_widget_focus(lua_State
*L
)
157 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
158 gtk_widget_grab_focus(w
->widget
);
163 luaH_widget_destroy(lua_State
*L
)
165 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
168 w
->destructor
= NULL
;
169 luaH_object_unref(L
, w
->ref
);
173 // vim: ft=c:et:sw=4:ts=8:sts=4:enc=utf-8:tw=80