/widgets/notebook.c

http://github.com/mason-larobina/luakit · C · 274 lines · 206 code · 35 blank · 33 comment · 19 complexity · 127516a67f0d39fbfc18dfda2ef5dcfa MD5 · raw file

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