Merge commit '2f8fb053' into develop
[luakit.git] / widgets / box.c
blobda3f653612d5b68d3eaf9b86b80988a465ca3f4c
1 /*
2 * widgets/box.c - gtk hbox & vbox container widgets
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 /* TODO
23 * - Add `remove(child)` method to remove child widgets from the box
24 * - Add `reorder(child, index)` method to re-order child widgets
25 * - Add `get_children()` method to return a table of widgets in the box
26 * - In the box destructor function detach all child windows
30 #include "luah.h"
31 #include "widgets/common.h"
33 /* direct wrapper around gtk_box_pack_start */
34 static gint
35 luaH_box_pack_start(lua_State *L)
37 widget_t *w = luaH_checkudata(L, 1, &widget_class);
38 widget_t *child = luaH_checkudata(L, 2, &widget_class);
39 gboolean expand = luaH_checkboolean(L, 3);
40 gboolean fill = luaH_checkboolean(L, 4);
41 guint padding = luaL_checknumber(L, 5);
42 gtk_box_pack_start(GTK_BOX(w->widget), GTK_WIDGET(child->widget),
43 expand, fill, padding);
44 return 0;
47 /* direct wrapper around gtk_box_pack_end */
48 static gint
49 luaH_box_pack_end(lua_State *L)
51 widget_t *w = luaH_checkudata(L, 1, &widget_class);
52 widget_t *child = luaH_checkudata(L, 2, &widget_class);
53 gboolean expand = luaH_checkboolean(L, 3);
54 gboolean fill = luaH_checkboolean(L, 4);
55 guint padding = luaL_checknumber(L, 5);
56 gtk_box_pack_end(GTK_BOX(w->widget), GTK_WIDGET(child->widget),
57 expand, fill, padding);
58 return 0;
61 static gint
62 luaH_box_index(lua_State *L, luakit_token_t token)
64 widget_t *w = luaH_checkudata(L, 1, &widget_class);
66 switch(token)
68 case L_TK_DESTROY:
69 lua_pushcfunction(L, luaH_widget_destroy);
70 return 1;
72 case L_TK_PACK_START:
73 lua_pushcfunction(L, luaH_box_pack_start);
74 return 1;
76 case L_TK_PACK_END:
77 lua_pushcfunction(L, luaH_box_pack_end);
78 return 1;
80 case L_TK_HOMOGENEOUS:
81 lua_pushboolean(L, gtk_box_get_homogeneous(GTK_BOX(w->widget)));
82 return 1;
84 case L_TK_SPACING:
85 lua_pushnumber(L, gtk_box_get_spacing(GTK_BOX(w->widget)));
86 return 1;
88 case L_TK_SHOW:
89 lua_pushcfunction(L, luaH_widget_show);
90 return 1;
92 case L_TK_HIDE:
93 lua_pushcfunction(L, luaH_widget_hide);
94 return 1;
96 default:
97 break;
99 return 0;
102 static gint
103 luaH_box_newindex(lua_State *L, luakit_token_t token)
105 widget_t *w = luaH_checkudata(L, 1, &widget_class);
107 switch(token)
109 case L_TK_HOMOGENEOUS:
110 gtk_box_set_homogeneous(GTK_BOX(w->widget), luaH_checkboolean(L, 3));
111 break;
113 case L_TK_SPACING:
114 gtk_box_set_spacing(GTK_BOX(w->widget), luaL_checknumber(L, 3));
115 break;
117 default:
118 return 0;
121 return luaH_object_emit_property_signal(L, 1);
124 void
125 box_destructor(widget_t *w)
127 gtk_widget_destroy(w->widget);
130 #define BOX_WIDGET_CONSTRUCTOR(type) \
131 widget_t * \
132 widget_##type(widget_t *w) \
134 w->index = luaH_box_index; \
135 w->newindex = luaH_box_newindex; \
136 w->destructor = box_destructor; \
137 w->widget = gtk_##type##_new(FALSE, 0); \
138 g_object_set_data(G_OBJECT(w->widget), "widget", (gpointer) w); \
139 gtk_widget_show(w->widget); \
140 g_object_connect((GObject*)w->widget, \
141 "signal::add", add_cb, w, \
142 "signal::parent-set", parent_set_cb, w, \
143 "signal::remove", remove_cb, w, \
144 NULL); \
145 return w; \
148 BOX_WIDGET_CONSTRUCTOR(vbox)
149 BOX_WIDGET_CONSTRUCTOR(hbox)
151 #undef BOX_WIDGET_CONSTRUCTOR
153 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80