Use common widget destructor for all simple gtk widgets
[luakit.git] / widgets / common.c
blobc0985bd592bb84f4f49ce6884fb06eeabed6f759
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, ret + 1);
40 return ret ? TRUE : FALSE;
43 gboolean
44 button_release_cb(GtkWidget *win, GdkEventButton *ev, widget_t *w)
46 (void) win;
47 gint ret;
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)) {
55 lua_pop(L, ret + 1);
56 return TRUE;
58 lua_pop(L, ret + 1);
59 /* propagate event further */
60 return FALSE;
63 gboolean
64 focus_cb(GtkWidget *win, GdkEventFocus *ev, widget_t *w)
66 (void) win;
67 lua_State *L = globalconf.L;
68 luaH_object_push(L, w->ref);
69 if (ev->in)
70 luaH_object_emit_signal(L, -1, "focus", 0, 0);
71 else
72 luaH_object_emit_signal(L, -1, "unfocus", 0, 0);
73 lua_pop(L, 1);
74 return FALSE;
77 /* gtk container add callback */
78 void
79 add_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
81 (void) c;
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);
87 lua_pop(L, 1);
90 /* gtk container remove callback */
91 void
92 remove_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
94 (void) c;
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);
100 lua_pop(L, 1);
103 void
104 parent_set_cb(GtkWidget *widget, GtkObject *old, widget_t *w)
106 (void) old;
107 lua_State *L = globalconf.L;
108 widget_t *parent = NULL;
109 GtkContainer *new;
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);
114 else
115 lua_pushnil(L);
116 luaH_object_emit_signal(L, -2, "parent-set", 1, 0);
117 lua_pop(L, 1);
120 gboolean
121 true_cb()
123 return TRUE;
126 /* set child method for gtk container widgets */
127 gint
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));
135 if (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 */
141 if (child)
142 gtk_container_add(GTK_CONTAINER(w->widget), GTK_WIDGET(child->widget));
143 return 0;
146 /* get child method for gtk container widgets */
147 gint
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;
154 if (!widget)
155 return 0;
157 child = g_object_get_data(G_OBJECT(child), "lua_widget");
158 luaH_object_push(L, child->ref);
159 return 1;
162 gint
163 luaH_widget_show(lua_State *L)
165 widget_t *w = luaH_checkudata(L, 1, &widget_class);
166 gtk_widget_show(w->widget);
167 return 0;
170 gint
171 luaH_widget_hide(lua_State *L)
173 widget_t *w = luaH_checkudata(L, 1, &widget_class);
174 gtk_widget_hide(w->widget);
175 return 0;
178 gint
179 luaH_widget_focus(lua_State *L)
181 widget_t *w = luaH_checkudata(L, 1, &widget_class);
182 gtk_widget_grab_focus(w->widget);
183 return 0;
186 gint
187 luaH_widget_destroy(lua_State *L)
189 widget_t *w = luaH_checkudata(L, 1, &widget_class);
190 if (w->destructor)
191 w->destructor(w);
192 debug("unreffing widget %p of type '%s'", w, w->info->name);
193 luaH_object_unref(L, w->ref);
194 return 0;
197 void
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));
202 w->widget = NULL;
205 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80