Use common widget destructor for all simple gtk widgets
[luakit.git] / widgets / textbutton.c
blob5734781d801f01f6187183c4ba8a657e92498d96
1 /*
2 * widgets/textbutton.c - gtk button with label
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 gint
26 luaH_textbutton_index(lua_State *L, luakit_token_t token)
28 widget_t *w = luaH_checkudata(L, 1, &widget_class);
30 switch (token)
32 case L_TK_DESTROY:
33 lua_pushcfunction(L, luaH_widget_destroy);
34 return 1;
36 case L_TK_LABEL:
37 lua_pushstring(L, gtk_button_get_label(GTK_BUTTON(w->widget)));
38 return 1;
40 case L_TK_SHOW:
41 lua_pushcfunction(L, luaH_widget_show);
42 return 1;
44 case L_TK_HIDE:
45 lua_pushcfunction(L, luaH_widget_hide);
46 return 1;
48 default:
49 break;
51 return 0;
54 static gint
55 luaH_textbutton_newindex(lua_State *L, luakit_token_t token)
57 size_t len;
58 widget_t *w = luaH_checkudata(L, 1, &widget_class);
60 switch(token)
62 case L_TK_LABEL:
63 gtk_button_set_label(GTK_BUTTON(w->widget),
64 luaL_checklstring(L, 3, &len));
65 break;
67 default:
68 return 0;
71 return luaH_object_emit_property_signal(L, 1);
74 static void
75 clicked_cb(GtkWidget *b, widget_t *w)
77 (void) b;
78 lua_State *L = globalconf.L;
79 luaH_object_push(L, w->ref);
80 luaH_object_emit_signal(L, -1, "clicked", 0, 0);
81 lua_pop(L, 1);
84 widget_t *
85 widget_textbutton(widget_t *w)
87 w->index = luaH_textbutton_index;
88 w->newindex = luaH_textbutton_newindex;
89 w->destructor = widget_destructor;
91 w->widget = gtk_button_new();
92 g_object_set_data(G_OBJECT(w->widget), "widget", (gpointer) w);
93 gtk_button_set_focus_on_click(GTK_BUTTON(w->widget), FALSE);
95 g_object_connect((GObject*)w->widget,
96 "signal::clicked", (GCallback)clicked_cb, w,
97 "signal::focus-in-event", (GCallback)focus_cb, w,
98 "signal::focus-out-event", (GCallback)focus_cb, w,
99 "signal::parent-set", (GCallback)parent_set_cb, w,
100 NULL);
102 gtk_widget_show(w->widget);
103 return w;
106 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80