Add destroy method to all widgets which triggers its garbage collection
[luakit.git] / widgets / common.c
blobf1124f2862af87d0c1ed0d072d1c3de2f516693f
1 /*
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/>.
21 #include <gtk/gtk.h>
23 #include "luah.h"
24 #include "globalconf.h"
25 #include "common/luaobject.h"
26 #include "common/lualib.h"
27 #include "widgets/common.h"
29 gboolean
30 key_press_cb(GtkWidget *win, GdkEventKey *ev, widget_t *w)
32 (void) win;
33 gint ret;
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);
39 lua_pop(L, 1 + ret);
40 return ret ? TRUE : FALSE;
43 gboolean
44 focus_cb(GtkWidget *win, GdkEventFocus *ev, widget_t *w)
46 (void) win;
47 lua_State *L = globalconf.L;
48 luaH_object_push(L, w->ref);
49 if (ev->in)
50 luaH_object_emit_signal(L, -1, "focus", 0, 0);
51 else
52 luaH_object_emit_signal(L, -1, "unfocus", 0, 0);
53 lua_pop(L, 1);
54 return FALSE;
57 /* gtk container add callback */
58 void
59 add_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
61 (void) c;
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);
67 lua_pop(L, 1);
70 /* gtk container remove callback */
71 void
72 remove_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
74 (void) c;
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);
80 lua_pop(L, 1);
83 void
84 parent_set_cb(GtkWidget *widget, GtkObject *old, widget_t *w)
86 (void) old;
87 lua_State *L = globalconf.L;
88 widget_t *parent = NULL;
89 GtkContainer *new;
90 g_object_get(G_OBJECT(widget), "parent", &new, NULL);
91 luaH_object_push(L, w->ref);
92 if (new)
93 parent = g_object_get_data(G_OBJECT(new), "widget");
94 if (parent)
95 luaH_object_push(L, parent->ref);
96 else
97 lua_pushnil(L);
98 luaH_object_emit_signal(L, -2, "parent-set", 1, 0);
99 lua_pop(L, 1);
102 /* set child method for gtk container widgets */
103 gint
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));
111 if (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 */
117 if (child)
118 gtk_container_add(GTK_CONTAINER(w->widget), GTK_WIDGET(child->widget));
119 return 0;
122 /* get child method for gtk container widgets */
123 gint
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;
130 if (!widget)
131 return 0;
133 child = g_object_get_data(G_OBJECT(child), "lua_widget");
134 luaH_object_push(L, child->ref);
135 return 1;
138 gint
139 luaH_widget_show(lua_State *L)
141 widget_t *w = luaH_checkudata(L, 1, &widget_class);
142 gtk_widget_show(w->widget);
143 return 0;
146 gint
147 luaH_widget_hide(lua_State *L)
149 widget_t *w = luaH_checkudata(L, 1, &widget_class);
150 gtk_widget_hide(w->widget);
151 return 0;
154 gint
155 luaH_widget_focus(lua_State *L)
157 widget_t *w = luaH_checkudata(L, 1, &widget_class);
158 gtk_widget_grab_focus(w->widget);
159 return 0;
162 gint
163 luaH_widget_destroy(lua_State *L)
165 widget_t *w = luaH_checkudata(L, 1, &widget_class);
166 if (w->destructor)
167 w->destructor(w);
168 w->destructor = NULL;
169 luaH_object_unref(L, w->ref);
170 return 0;
173 // vim: ft=c:et:sw=4:ts=8:sts=4:enc=utf-8:tw=80