Rename method `view_source` to `{set,get}_view_source` and update rc.lua
[luakit.git] / widgets / notebook.c
blob1c8ffe59b04b575748171cf7d2fb55b5b4419066
1 /*
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/>.
22 /* TODO
23 * - In the notebook destructor function detach all child widgets
24 * - Add `get_children()` method which returns a table of the child widgets
27 #include "luah.h"
28 #include "widgets/common.h"
30 static gint
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));
35 if (n == 1)
36 lua_pushnumber(L, 1);
37 else
38 lua_pushnumber(L, gtk_notebook_get_current_page(
39 GTK_NOTEBOOK(w->widget)) + 1);
40 return 1;
43 static gint
44 luaH_notebook_atindex(lua_State *L)
46 widget_t *w = luaH_checkudata(L, 1, &widget_class);
47 gint i = luaL_checknumber(L, 2);
48 /* correct index */
49 if (i != -1) i--;
51 GtkWidget *widget = gtk_notebook_get_nth_page(GTK_NOTEBOOK(w->widget), i);
52 if (!widget)
53 return 0;
55 widget_t *child = g_object_get_data(G_OBJECT(widget), "widget");
56 luaH_object_push(L, child->ref);
57 return 1;
60 static gint
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 */
69 if (!++i) return 0;
70 lua_pushnumber(L, i);
71 return 1;
74 static gint
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);
81 if (i != -1)
82 return 0;
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);
87 return 0;
90 /* Inserts a widget into the notebook widget at an index */
91 static gint
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);
97 /* correct index */
98 if (i != -1) i--;
100 i = gtk_notebook_insert_page(GTK_NOTEBOOK(w->widget),
101 child->widget, NULL, i);
103 /* return new index or nil */
104 if (!++i) return 0;
105 lua_pushnumber(L, i);
106 return 1;
109 /* Appends a widget to the notebook widget */
110 static gint
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 */
119 if (!++i) return 0;
120 lua_pushnumber(L, i);
121 return 1;
124 /* Return the number of widgets in the notebook */
125 static gint
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)));
130 return 1;
133 static gint
134 luaH_notebook_set_title(lua_State *L)
136 size_t len;
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);
142 return 0;
145 static gint
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));
152 return 1;
155 static gint
156 luaH_notebook_switch(lua_State *L)
158 widget_t *w = luaH_checkudata(L, 1, &widget_class);
159 gint i = luaL_checknumber(L, 2);
160 /* correct index */
161 if (i != -1) i--;
162 gtk_notebook_set_current_page(GTK_NOTEBOOK(w->widget), i);
163 lua_pushnumber(L, gtk_notebook_get_current_page(GTK_NOTEBOOK(w->widget)));
164 return 1;
167 static gint
168 luaH_notebook_index(lua_State *L, luakit_token_t token)
170 widget_t *w = luaH_checkudata(L, 1, &widget_class);
172 switch(token)
174 case L_TK_DESTROY:
175 lua_pushcfunction(L, luaH_widget_destroy);
176 return 1;
178 case L_TK_COUNT:
179 lua_pushcfunction(L, luaH_notebook_count);
180 return 1;
182 case L_TK_INSERT:
183 lua_pushcfunction(L, luaH_notebook_insert);
184 return 1;
186 case L_TK_APPEND:
187 lua_pushcfunction(L, luaH_notebook_append);
188 return 1;
190 case L_TK_CURRENT:
191 lua_pushcfunction(L, luaH_notebook_current);
192 return 1;
194 case L_TK_REMOVE:
195 lua_pushcfunction(L, luaH_notebook_remove);
196 return 1;
198 case L_TK_INDEXOF:
199 lua_pushcfunction(L, luaH_notebook_indexof);
200 return 1;
202 case L_TK_ATINDEX:
203 lua_pushcfunction(L, luaH_notebook_atindex);
204 return 1;
206 case L_TK_SWITCH:
207 lua_pushcfunction(L, luaH_notebook_switch);
208 return 1;
210 case L_TK_SET_TITLE:
211 lua_pushcfunction(L, luaH_notebook_set_title);
212 return 1;
214 case L_TK_GET_TITLE:
215 lua_pushcfunction(L, luaH_notebook_get_title);
216 return 1;
218 case L_TK_SHOW_TABS:
219 lua_pushboolean(L, gtk_notebook_get_show_tabs(
220 GTK_NOTEBOOK(w->widget)));
221 return 1;
223 case L_TK_SHOW_BORDER:
224 lua_pushboolean(L, gtk_notebook_get_show_border(
225 GTK_NOTEBOOK(w->widget)));
226 return 1;
228 case L_TK_SHOW:
229 lua_pushcfunction(L, luaH_widget_show);
230 return 1;
232 case L_TK_HIDE:
233 lua_pushcfunction(L, luaH_widget_hide);
234 return 1;
236 case L_TK_FOCUS:
237 lua_pushcfunction(L, luaH_widget_focus);
238 return 1;
240 default:
241 break;
243 return 0;
246 static gint
247 luaH_notebook_newindex(lua_State *L, luakit_token_t token)
249 widget_t *w = luaH_checkudata(L, 1, &widget_class);
251 switch(token)
253 case L_TK_SHOW_TABS:
254 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(w->widget),
255 luaH_checkboolean(L, 3));
256 break;
258 case L_TK_SHOW_BORDER:
259 gtk_notebook_set_show_border(GTK_NOTEBOOK(w->widget),
260 luaH_checkboolean(L, 3));
261 break;
263 default:
264 return 0;
267 return luaH_object_emit_property_signal(L, 1);
270 static void
271 page_added_cb(GtkNotebook *nbook, GtkWidget *widget, guint i, widget_t *w)
273 (void) nbook;
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);
281 lua_pop(L, 1);
284 static void
285 page_removed_cb(GtkNotebook *nbook, GtkWidget *widget, guint i, widget_t *w)
287 (void) i;
288 (void) nbook;
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);
295 lua_pop(L, 1);
298 static void
299 switch_cb(GtkNotebook *nbook, GtkNotebookPage *p, guint i, widget_t *w)
301 (void) p;
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);
310 lua_pop(L, 1);
313 static void
314 notebook_destructor(widget_t *w)
316 gtk_widget_destroy(w->widget);
319 widget_t *
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,
340 NULL);
342 gtk_widget_show(w->widget);
343 return w;
346 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80