1 /*
2  * widgets/notebook.c - gtk notebook 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_notebook_current(lua_State * L)25 luaH_notebook_current(lua_State *L)
26 {
27     widget_t *w = luaH_checkwidget(L, 1);
28     gint n = gtk_notebook_get_n_pages(GTK_NOTEBOOK(w->widget));
29     if (n == 1)
30         lua_pushnumber(L, 1);
31     else
32         lua_pushnumber(L, gtk_notebook_get_current_page(
33             GTK_NOTEBOOK(w->widget)) + 1);
34     return 1;
35 }
36 
37 static gint
luaH_notebook_atindex(lua_State * L,widget_t * w,gint idx)38 luaH_notebook_atindex(lua_State *L, widget_t *w, gint idx)
39 {
40     /* correct index */
41     if (idx != -1) idx--;
42 
43     GtkWidget *widget = gtk_notebook_get_nth_page(GTK_NOTEBOOK(w->widget), idx);
44     if (!widget)
45         return 0;
46 
47     widget_t *child = GOBJECT_TO_LUAKIT_WIDGET(widget);
48     luaH_object_push(L, child->ref);
49     return 1;
50 }
51 
52 static gint
luaH_notebook_indexof(lua_State * L)53 luaH_notebook_indexof(lua_State *L)
54 {
55     widget_t *w = luaH_checkwidget(L, 1);
56     widget_t *child = luaH_checkwidget(L, 2);
57     gint i = gtk_notebook_page_num(GTK_NOTEBOOK(w->widget), child->widget);
58     /* return index or nil */
59     if (!++i) return 0;
60     lua_pushnumber(L, i);
61     return 1;
62 }
63 
64 /* Inserts a widget into the notebook widget at an index */
65 static gint
luaH_notebook_insert(lua_State * L)66 luaH_notebook_insert(lua_State *L)
67 {
68     widget_t *w = luaH_checkwidget(L, 1);
69 
70     /* get insert position (or append page) */
71     gint pos = -1, idx = 2;
72     if (lua_gettop(L) > 2) {
73         pos = luaL_checknumber(L, idx++);
74         if (pos > 0) pos--; /* correct lua index */
75     }
76 
77     pos = gtk_notebook_insert_page(GTK_NOTEBOOK(w->widget),
78         GTK_WIDGET(luaH_checkwidget(L, idx)->widget), NULL, pos);
79 
80     /* failed to insert page */
81     if (pos == -1)
82         return 0;
83 
84     /* return new (lua corrected) index */
85     lua_pushnumber(L, ++pos);
86     return 1;
87 }
88 
89 /* Return the number of widgets in the notebook */
90 static gint
luaH_notebook_count(lua_State * L)91 luaH_notebook_count(lua_State *L)
92 {
93     widget_t *w = luaH_checkwidget(L, 1);
94     lua_pushnumber(L, gtk_notebook_get_n_pages(GTK_NOTEBOOK(w->widget)));
95     return 1;
96 }
97 
98 static gint
luaH_notebook_set_title(lua_State * L)99 luaH_notebook_set_title(lua_State *L)
100 {
101     size_t len;
102     widget_t *w = luaH_checkwidget(L, 1);
103     widget_t *child = luaH_checkwidget(L, 2);
104     const gchar *title = luaL_checklstring(L, 3, &len);
105     GtkWidget *label = gtk_label_new(title);
106     gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_MIDDLE);
107     gtk_notebook_set_tab_label(GTK_NOTEBOOK(w->widget),
108         child->widget, label);
109     gtk_container_child_set(GTK_CONTAINER(w->widget), label, "tab-expand", TRUE, "tab-fill", TRUE, NULL);
110     return 0;
111 }
112 
113 static gint
luaH_notebook_get_title(lua_State * L)114 luaH_notebook_get_title(lua_State *L)
115 {
116     widget_t *w = luaH_checkwidget(L, 1);
117     widget_t *child = luaH_checkwidget(L, 2);
118     lua_pushstring(L, gtk_notebook_get_tab_label_text(
119         GTK_NOTEBOOK(w->widget), child->widget));
120     return 1;
121 }
122 
123 static gint
luaH_notebook_switch(lua_State * L)124 luaH_notebook_switch(lua_State *L)
125 {
126     widget_t *w = luaH_checkwidget(L, 1);
127     gint i = luaL_checknumber(L, 2);
128     /* correct index */
129     if (i != -1) i--;
130     gtk_notebook_set_current_page(GTK_NOTEBOOK(w->widget), i);
131     lua_pushnumber(L, gtk_notebook_get_current_page(GTK_NOTEBOOK(w->widget)));
132     return 1;
133 }
134 
135 static gint
luaH_notebook_reorder(lua_State * L)136 luaH_notebook_reorder(lua_State *L)
137 {
138     widget_t *w = luaH_checkwidget(L, 1);
139     widget_t *child = luaH_checkwidget(L, 2);
140     gint i = luaL_checknumber(L, 3);
141     /* correct lua index */
142     if (i != -1) i--;
143     gtk_notebook_reorder_child(GTK_NOTEBOOK(w->widget), child->widget, i);
144     lua_pushnumber(L, gtk_notebook_page_num(GTK_NOTEBOOK(w->widget), child->widget));
145     return 1;
146 }
147 
148 static gint
luaH_notebook_index(lua_State * L,widget_t * w,luakit_token_t token)149 luaH_notebook_index(lua_State *L, widget_t *w, luakit_token_t token)
150 {
151     /* handle numerical index lookups */
152     if (token == L_TK_UNKNOWN && lua_isnumber(L, 2))
153         return luaH_notebook_atindex(L, w, (gint)luaL_checknumber(L, 2));
154 
155     switch(token)
156     {
157       LUAKIT_WIDGET_INDEX_COMMON(w)
158       LUAKIT_WIDGET_CONTAINER_INDEX_COMMON(w)
159 
160       /* push class methods */
161       PF_CASE(COUNT,        luaH_notebook_count)
162       PF_CASE(CURRENT,      luaH_notebook_current)
163       PF_CASE(GET_TITLE,    luaH_notebook_get_title)
164       PF_CASE(INDEXOF,      luaH_notebook_indexof)
165       PF_CASE(INSERT,       luaH_notebook_insert)
166       PF_CASE(SET_TITLE,    luaH_notebook_set_title)
167       PF_CASE(SWITCH,       luaH_notebook_switch)
168       PF_CASE(REORDER,      luaH_notebook_reorder)
169 
170       /* push boolean properties */
171       PB_CASE(SHOW_TABS,    gtk_notebook_get_show_tabs(GTK_NOTEBOOK(w->widget)))
172       PB_CASE(SHOW_BORDER,  gtk_notebook_get_show_border(GTK_NOTEBOOK(w->widget)))
173 
174       default:
175         break;
176     }
177     return 0;
178 }
179 
180 static gint
luaH_notebook_newindex(lua_State * L,widget_t * w,luakit_token_t token)181 luaH_notebook_newindex(lua_State *L, widget_t *w, luakit_token_t token)
182 {
183     switch(token) {
184       LUAKIT_WIDGET_NEWINDEX_COMMON(w)
185 
186       case L_TK_SHOW_TABS:
187         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(w->widget), luaH_checkboolean(L, 3));
188         break;
189 
190       case L_TK_SHOW_BORDER:
191         gtk_notebook_set_show_border(GTK_NOTEBOOK(w->widget), luaH_checkboolean(L, 3));
192         break;
193 
194       default:
195         return 0;
196     }
197 
198     return luaH_object_property_signal(L, 1, token);
199 }
200 
201 static void
page_added_cb(GtkNotebook * UNUSED (n),GtkWidget * widget,guint i,widget_t * w)202 page_added_cb(GtkNotebook* UNUSED(n), GtkWidget *widget, guint i, widget_t *w)
203 {
204     widget_t *child = GOBJECT_TO_LUAKIT_WIDGET(widget);
205     lua_State *L = common.L;
206     luaH_object_push(L, w->ref);
207     luaH_object_push(L, child->ref);
208     lua_pushnumber(L, i + 1);
209     luaH_object_emit_signal(L, -3, "page-added", 2, 0);
210     lua_pop(L, 1);
211 }
212 
213 static void
page_removed_cb(GtkNotebook * UNUSED (n),GtkWidget * widget,guint UNUSED (i),widget_t * w)214 page_removed_cb(GtkNotebook* UNUSED(n), GtkWidget *widget, guint UNUSED(i),
215         widget_t *w)
216 {
217     widget_t *child = GOBJECT_TO_LUAKIT_WIDGET(widget);
218     lua_State *L = common.L;
219     luaH_object_push(L, w->ref);
220     luaH_object_push(L, child->ref);
221     luaH_object_emit_signal(L, -2, "page-removed", 1, 0);
222     lua_pop(L, 1);
223 }
224 
225 static void
switch_cb(GtkNotebook * n,GtkWidget * UNUSED (p),guint i,widget_t * w)226 switch_cb(GtkNotebook *n, GtkWidget* UNUSED(p), guint i, widget_t *w)
227 {
228     GtkWidget *widget = gtk_notebook_get_nth_page(GTK_NOTEBOOK(n), i);
229     widget_t *child = GOBJECT_TO_LUAKIT_WIDGET(widget);
230     lua_State *L = common.L;
231     luaH_object_push(L, w->ref);
232     luaH_object_push(L, child->ref);
233     lua_pushnumber(L, i + 1);
234     luaH_object_emit_signal(L, -3, "switch-page", 2, 0);
235     lua_pop(L, 1);
236 }
237 
238 static void
reorder_cb(GtkNotebook * UNUSED (n),GtkWidget * widget,guint i,widget_t * w)239 reorder_cb(GtkNotebook* UNUSED(n), GtkWidget *widget, guint i, widget_t *w)
240 {
241     widget_t *child = GOBJECT_TO_LUAKIT_WIDGET(widget);
242     lua_State *L = common.L;
243     luaH_object_push(L, w->ref);
244     luaH_object_push(L, child->ref);
245     lua_pushnumber(L, i + 1);
246     luaH_object_emit_signal(L, -3, "page-reordered", 2, 0);
247     lua_pop(L, 1);
248 }
249 
250 widget_t *
widget_notebook(lua_State * UNUSED (L),widget_t * w,luakit_token_t UNUSED (token))251 widget_notebook(lua_State *UNUSED(L), widget_t *w, luakit_token_t UNUSED(token))
252 {
253     w->index = luaH_notebook_index;
254     w->newindex = luaH_notebook_newindex;
255 
256     /* create and setup notebook widget */
257     w->widget = gtk_notebook_new();
258     gtk_notebook_set_show_border(GTK_NOTEBOOK(w->widget), FALSE);
259     gtk_notebook_set_scrollable(GTK_NOTEBOOK(w->widget), TRUE);
260 
261     g_object_connect(G_OBJECT(w->widget),
262       LUAKIT_WIDGET_SIGNAL_COMMON(w)
263       "signal::key-press-event",   G_CALLBACK(key_press_cb),    w,
264       "signal::page-added",        G_CALLBACK(page_added_cb),   w,
265       "signal::page-removed",      G_CALLBACK(page_removed_cb), w,
266       "signal::page-reordered",    G_CALLBACK(reorder_cb),      w,
267       "signal::switch-page",       G_CALLBACK(switch_cb),       w,
268       NULL);
269 
270     gtk_widget_show(w->widget);
271     return w;
272 }
273 
274 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80
275