2 * widgets/notebook.c - gtk notebook widget
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/>.
23 * - In the notebook destructor function detach all child widgets
24 * - Add `get_children()` method which returns a table of the child widgets
28 #include "widgets/common.h"
31 luaH_notebook_current(lua_State
*L
)
33 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
34 gint n
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(w
->widget
));
38 lua_pushnumber(L
, gtk_notebook_get_current_page(
39 GTK_NOTEBOOK(w
->widget
)) + 1);
44 luaH_notebook_atindex(lua_State
*L
)
46 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
47 gint i
= luaL_checknumber(L
, 2);
51 GtkWidget
*widget
= gtk_notebook_get_nth_page(GTK_NOTEBOOK(w
->widget
), i
);
55 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
56 luaH_object_push(L
, child
->ref
);
61 luaH_notebook_indexof(lua_State
*L
)
63 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
64 widget_t
*child
= luaH_checkudata(L
, 2, &widget_class
);
66 gint i
= gtk_notebook_page_num(GTK_NOTEBOOK(w
->widget
), child
->widget
);
68 /* return index or nil */
75 luaH_notebook_remove(lua_State
*L
)
77 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
78 widget_t
*child
= luaH_checkudata(L
, 2, &widget_class
);
79 gint i
= gtk_notebook_page_num(GTK_NOTEBOOK(w
->widget
), child
->widget
);
84 GtkWidget
*widget
= gtk_notebook_get_nth_page(GTK_NOTEBOOK(w
->widget
), i
);
85 g_object_ref(G_OBJECT(widget
));
86 gtk_notebook_remove_page(GTK_NOTEBOOK(w
->widget
), i
);
90 /* Inserts a widget into the notebook widget at an index */
92 luaH_notebook_insert(lua_State
*L
)
94 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
95 widget_t
*child
= luaH_checkudata(L
, 3, &widget_class
);
96 gint i
= luaL_checknumber(L
, 2);
100 i
= gtk_notebook_insert_page(GTK_NOTEBOOK(w
->widget
),
101 child
->widget
, NULL
, i
);
103 /* return new index or nil */
105 lua_pushnumber(L
, i
);
109 /* Appends a widget to the notebook widget */
111 luaH_notebook_append(lua_State
*L
)
113 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
114 widget_t
*child
= luaH_checkudata(L
, 2, &widget_class
);
115 gint i
= gtk_notebook_append_page(GTK_NOTEBOOK(w
->widget
),
116 child
->widget
, NULL
);
118 /* return new index or nil */
120 lua_pushnumber(L
, i
);
124 /* Return the number of widgets in the notebook */
126 luaH_notebook_count(lua_State
*L
)
128 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
129 lua_pushnumber(L
, gtk_notebook_get_n_pages(GTK_NOTEBOOK(w
->widget
)));
134 luaH_notebook_set_title(lua_State
*L
)
137 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
138 widget_t
*child
= luaH_checkudata(L
, 2, &widget_class
);
139 const gchar
*title
= luaL_checklstring(L
, 3, &len
);
140 gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(w
->widget
),
141 child
->widget
, title
);
146 luaH_notebook_get_title(lua_State
*L
)
148 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
149 widget_t
*child
= luaH_checkudata(L
, 2, &widget_class
);
150 lua_pushstring(L
, gtk_notebook_get_tab_label_text(
151 GTK_NOTEBOOK(w
->widget
), child
->widget
));
156 luaH_notebook_switch(lua_State
*L
)
158 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
159 gint i
= luaL_checknumber(L
, 2);
162 gtk_notebook_set_current_page(GTK_NOTEBOOK(w
->widget
), i
);
163 lua_pushnumber(L
, gtk_notebook_get_current_page(GTK_NOTEBOOK(w
->widget
)));
168 luaH_notebook_index(lua_State
*L
, luakit_token_t token
)
170 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
175 lua_pushcfunction(L
, luaH_widget_destroy
);
179 lua_pushcfunction(L
, luaH_notebook_count
);
183 lua_pushcfunction(L
, luaH_notebook_insert
);
187 lua_pushcfunction(L
, luaH_notebook_append
);
191 lua_pushcfunction(L
, luaH_notebook_current
);
195 lua_pushcfunction(L
, luaH_notebook_remove
);
199 lua_pushcfunction(L
, luaH_notebook_indexof
);
203 lua_pushcfunction(L
, luaH_notebook_atindex
);
207 lua_pushcfunction(L
, luaH_notebook_switch
);
211 lua_pushcfunction(L
, luaH_notebook_set_title
);
215 lua_pushcfunction(L
, luaH_notebook_get_title
);
219 lua_pushboolean(L
, gtk_notebook_get_show_tabs(
220 GTK_NOTEBOOK(w
->widget
)));
223 case L_TK_SHOW_BORDER
:
224 lua_pushboolean(L
, gtk_notebook_get_show_border(
225 GTK_NOTEBOOK(w
->widget
)));
229 lua_pushcfunction(L
, luaH_widget_show
);
233 lua_pushcfunction(L
, luaH_widget_hide
);
237 lua_pushcfunction(L
, luaH_widget_focus
);
247 luaH_notebook_newindex(lua_State
*L
, luakit_token_t token
)
249 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
254 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(w
->widget
),
255 luaH_checkboolean(L
, 3));
258 case L_TK_SHOW_BORDER
:
259 gtk_notebook_set_show_border(GTK_NOTEBOOK(w
->widget
),
260 luaH_checkboolean(L
, 3));
267 return luaH_object_emit_property_signal(L
, 1);
271 page_added_cb(GtkNotebook
*nbook
, GtkWidget
*widget
, guint i
, widget_t
*w
)
275 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
276 lua_State
*L
= globalconf
.L
;
277 luaH_object_push(L
, w
->ref
);
278 luaH_object_push(L
, child
->ref
);
279 lua_pushnumber(L
, i
+ 1);
280 luaH_object_emit_signal(L
, -3, "page-added", 2, 0);
285 page_removed_cb(GtkNotebook
*nbook
, GtkWidget
*widget
, guint i
, widget_t
*w
)
290 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
291 lua_State
*L
= globalconf
.L
;
292 luaH_object_push(L
, w
->ref
);
293 luaH_object_push(L
, child
->ref
);
294 luaH_object_emit_signal(L
, -2, "page-removed", 1, 0);
299 switch_cb(GtkNotebook
*nbook
, GtkNotebookPage
*p
, guint i
, widget_t
*w
)
302 GtkWidget
*widget
= gtk_notebook_get_nth_page(GTK_NOTEBOOK(nbook
), i
);
303 widget_t
*child
= g_object_get_data(G_OBJECT(widget
), "widget");
305 lua_State
*L
= globalconf
.L
;
306 luaH_object_push(L
, w
->ref
);
307 luaH_object_push(L
, child
->ref
);
308 lua_pushnumber(L
, i
+ 1);
309 luaH_object_emit_signal(L
, -3, "switch-page", 2, 0);
314 notebook_destructor(widget_t
*w
)
316 gtk_widget_destroy(w
->widget
);
320 widget_notebook(widget_t
*w
)
322 w
->index
= luaH_notebook_index
;
323 w
->newindex
= luaH_notebook_newindex
;
324 w
->destructor
= notebook_destructor
;
326 /* create and setup notebook widget */
327 w
->widget
= gtk_notebook_new();
328 g_object_set_data(G_OBJECT(w
->widget
), "widget", (gpointer
) w
);
329 gtk_notebook_set_show_border(GTK_NOTEBOOK(w
->widget
), FALSE
);
330 gtk_notebook_set_scrollable(GTK_NOTEBOOK(w
->widget
), TRUE
);
332 g_object_connect((GObject
*)w
->widget
,
333 "signal::focus-in-event", (GCallback
)focus_cb
, w
,
334 "signal::focus-out-event", (GCallback
)focus_cb
, w
,
335 "signal::key-press-event", (GCallback
)key_press_cb
, w
,
336 "signal::page-added", (GCallback
)page_added_cb
, w
,
337 "signal::page-removed", (GCallback
)page_removed_cb
, w
,
338 "signal::switch-page", (GCallback
)switch_cb
, w
,
339 "signal::parent-set", (GCallback
)parent_set_cb
, w
,
342 gtk_widget_show(w
->widget
);
346 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80