Set destructor = NULL in destroy func to prevent it being called twice
[luakit.git] / widgets / window.c
blob01236fe86323a09b72e4dc0df2bb75342839e781
1 /*
2 * widgets/window.c - gtk window widget wrapper
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
5 * Copyright (C) 2007-2009 Julien Danjou <julien@danjou.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "luah.h"
23 #include "widgets/common.h"
25 static void
26 destroy_cb(GtkObject *win, widget_t *w)
28 (void) win;
30 /* remove window from global windows list */
31 g_ptr_array_remove(globalconf.windows, w);
33 lua_State *L = globalconf.L;
34 luaH_object_push(L, w->ref);
35 luaH_object_emit_signal(L, -1, "destroy", 0, 0);
36 lua_pop(L, 1);
39 static gint
40 luaH_window_index(lua_State *L, luakit_token_t token)
42 widget_t *w = luaH_checkudata(L, 1, &widget_class);
44 switch(token)
46 LUAKIT_WIDGET_INDEX_COMMON
47 LUAKIT_WIDGET_BIN_INDEX_COMMON
49 /* push string methods */
50 PS_CASE(TITLE, gtk_window_get_title(GTK_WINDOW(w->widget)))
52 default:
53 break;
55 return 0;
58 static gint
59 luaH_window_newindex(lua_State *L, luakit_token_t token)
61 size_t len;
62 widget_t *w = luaH_checkudata(L, 1, &widget_class);
64 switch(token)
66 case L_TK_TITLE:
67 gtk_window_set_title(GTK_WINDOW(w->widget),
68 luaL_checklstring(L, 3, &len));
69 break;
71 case L_TK_ICON:
72 gtk_window_set_icon_from_file(GTK_WINDOW(w->widget),
73 luaL_checklstring(L, 3, &len), NULL);
74 break;
76 default:
77 return 0;
80 return luaH_object_emit_property_signal(L, 1);
83 widget_t *
84 widget_window(widget_t *w)
86 w->index = luaH_window_index;
87 w->newindex = luaH_window_newindex;
88 w->destructor = widget_destructor;
90 /* create and setup window widget */
91 w->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
92 g_object_set_data(G_OBJECT(w->widget), "widget", (gpointer) w);
93 gtk_window_set_wmclass(GTK_WINDOW(w->widget), "luakit", "luakit");
94 gtk_window_set_default_size(GTK_WINDOW(w->widget), 800, 600);
95 gtk_window_set_title(GTK_WINDOW(w->widget), "luakit");
96 GdkGeometry hints;
97 hints.min_width = 1;
98 hints.min_height = 1;
99 gtk_window_set_geometry_hints(GTK_WINDOW(w->widget), NULL, &hints, GDK_HINT_MIN_SIZE);
101 g_object_connect((GObject*)w->widget,
102 "signal::add", (GCallback)add_cb, w,
103 "signal::destroy", (GCallback)destroy_cb, w,
104 "signal::key-press-event", (GCallback)key_press_cb, w,
105 "signal::remove", (GCallback)remove_cb, w,
106 NULL);
108 gtk_widget_show(w->widget);
109 gdk_window_set_events(GTK_WIDGET(w->widget)->window, GDK_ALL_EVENTS_MASK);
111 /* add to global windows list */
112 g_ptr_array_add(globalconf.windows, w);
114 return w;
117 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80