1 /*
2 * widgets/paned.c - gtk paned container widget
3 *
4 * Copyright © 2010 Mason Larobina <mason.larobina@gmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include "luah.h"
22 #include "widgets/common.h"
23
24 static gint
luaH_paned_pack(lua_State * L)25 luaH_paned_pack(lua_State *L)
26 {
27 widget_t *w = luaH_checkwidget(L, 1);
28 widget_t *child = luaH_checkwidget(L, 2);
29 gint top = lua_gettop(L);
30 gboolean resize = TRUE, shrink = TRUE;
31 if (top > 2 && !lua_isnil(L, 3)) {
32 luaH_checktable(L, 3);
33 if (luaH_rawfield(L, 3, "resize"))
34 resize = lua_toboolean(L, -1) ? TRUE : FALSE;
35 if (luaH_rawfield(L, 3, "shrink"))
36 shrink = lua_toboolean(L, -1) ? TRUE : FALSE;
37 lua_settop(L, top);
38 }
39
40 /* get packing position from C closure upvalue */
41 luakit_token_t t = (luakit_token_t)lua_tonumber(L, lua_upvalueindex(1));
42
43 if (t == L_TK_PACK1)
44 gtk_paned_pack1(GTK_PANED(w->widget), GTK_WIDGET(child->widget),
45 resize, shrink);
46 else
47 gtk_paned_pack2(GTK_PANED(w->widget), GTK_WIDGET(child->widget),
48 resize, shrink);
49 return 0;
50 }
51
52 static gint
luaH_paned_get_child(lua_State * L,widget_t * w,gint n)53 luaH_paned_get_child(lua_State *L, widget_t *w, gint n)
54 {
55 GtkWidget *widget = NULL;
56 if (n == 1)
57 widget = gtk_paned_get_child1(GTK_PANED(w->widget));
58 else
59 widget = gtk_paned_get_child2(GTK_PANED(w->widget));
60
61 if (!widget)
62 return 0;
63
64 widget_t *child = GOBJECT_TO_LUAKIT_WIDGET(widget);
65 luaH_object_push(L, child->ref);
66 return 1;
67 }
68
69 static gint
luaH_paned_get_position(lua_State * L,widget_t * w)70 luaH_paned_get_position(lua_State *L, widget_t *w)
71 {
72 gint position = gtk_paned_get_position(GTK_PANED(w->widget));
73 lua_pushnumber(L, position);
74 return 1;
75 }
76
77 static gint
luaH_paned_set_position(lua_State * L,widget_t * w)78 luaH_paned_set_position(lua_State *L, widget_t *w)
79 {
80 gint position = lua_tonumber(L, -1);
81 gtk_paned_set_position(GTK_PANED(w->widget), position);
82 return 0;
83 }
84
85 static gint
luaH_paned_index(lua_State * L,widget_t * w,luakit_token_t token)86 luaH_paned_index(lua_State *L, widget_t *w, luakit_token_t token)
87 {
88 switch(token) {
89 LUAKIT_WIDGET_INDEX_COMMON(w)
90 LUAKIT_WIDGET_CONTAINER_INDEX_COMMON(w)
91
92 /* push paned widget methods */
93 case L_TK_PACK1:
94 case L_TK_PACK2:
95 lua_pushnumber(L, (gint) token);
96 lua_pushcclosure(L, luaH_paned_pack, 1);
97 return 1;
98
99 case L_TK_TOP:
100 case L_TK_LEFT:
101 return luaH_paned_get_child(L, w, 1);
102
103 case L_TK_BOTTOM:
104 case L_TK_RIGHT:
105 return luaH_paned_get_child(L, w, 2);
106
107 case L_TK_POSITION:
108 return luaH_paned_get_position(L, w);
109
110 default:
111 break;
112 }
113 return 0;
114 }
115
116 static gint
luaH_paned_newindex(lua_State * L,widget_t * w,luakit_token_t token)117 luaH_paned_newindex(lua_State *L, widget_t *w, luakit_token_t token)
118 {
119 switch(token) {
120 LUAKIT_WIDGET_NEWINDEX_COMMON(w)
121
122 case L_TK_POSITION:
123 luaH_paned_set_position(L, w);
124 break;
125
126 default:
127 return 0;
128 }
129
130 return luaH_object_property_signal(L, 1, token);
131 }
132
133 widget_t *
widget_paned(lua_State * UNUSED (L),widget_t * w,luakit_token_t token)134 widget_paned(lua_State *UNUSED(L), widget_t *w, luakit_token_t token)
135 {
136 w->index = luaH_paned_index;
137 w->newindex = luaH_paned_newindex;
138
139 w->widget = (token == L_TK_VPANED) ? gtk_paned_new(GTK_ORIENTATION_VERTICAL) :
140 gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
141
142 g_object_connect(G_OBJECT(w->widget),
143 LUAKIT_WIDGET_SIGNAL_COMMON(w)
144 "signal::add", G_CALLBACK(add_cb), w,
145 NULL);
146 gtk_widget_show(w->widget);
147 return w;
148 }
149
150 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80
151