1 /*
2  * widgets/stack.c - gtk stack widget
3  *
4  * Copyright © 2017 Aidan Holm <aidanholm@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_stack_pack(lua_State * L)25 luaH_stack_pack(lua_State *L)
26 {
27     widget_t *w = luaH_checkwidget(L, 1);
28     widget_t *child = luaH_checkwidget(L, 2);
29     gtk_container_add(GTK_CONTAINER (w->widget), GTK_WIDGET(child->widget));
30     return 0;
31 }
32 
33 static gint
luaH_stack_index(lua_State * L,widget_t * w,luakit_token_t token)34 luaH_stack_index(lua_State *L, widget_t *w, luakit_token_t token)
35 {
36     switch(token)
37     {
38         LUAKIT_WIDGET_INDEX_COMMON(w)
39         LUAKIT_WIDGET_CONTAINER_INDEX_COMMON(w)
40 
41         PF_CASE(PACK, luaH_stack_pack)
42         PB_CASE(HOMOGENEOUS, gtk_stack_get_homogeneous(GTK_STACK(w->widget)))
43 
44         case L_TK_VISIBLE_CHILD:
45         {
46             GtkWidget *widget = gtk_stack_get_visible_child(GTK_STACK(w->widget));
47             if (!widget)
48                 return 0;
49             widget_t *child = GOBJECT_TO_LUAKIT_WIDGET(widget);
50             luaH_object_push(L, child->ref);
51             return 1;
52         }
53 
54         default:
55             break;
56     }
57     return 0;
58 }
59 
60 static gint
luaH_stack_newindex(lua_State * L,widget_t * w,luakit_token_t token)61 luaH_stack_newindex(lua_State *L, widget_t *w, luakit_token_t token)
62 {
63     switch(token) {
64         LUAKIT_WIDGET_NEWINDEX_COMMON(w)
65 
66         case L_TK_HOMOGENEOUS:
67             gtk_stack_set_homogeneous(GTK_STACK(w->widget), luaH_checkboolean(L, 3));
68             break;
69 
70         case L_TK_VISIBLE_CHILD:
71         {
72             widget_t *child = luaH_checkwidget(L, 3);
73             gtk_stack_set_visible_child(GTK_STACK(w->widget), child->widget);
74             break;
75         }
76 
77         default:
78             return 0;
79     }
80 
81     return luaH_object_property_signal(L, 1, token);
82 }
83 
84 widget_t *
widget_stack(lua_State * UNUSED (L),widget_t * w,luakit_token_t UNUSED (token))85 widget_stack(lua_State *UNUSED(L), widget_t *w, luakit_token_t UNUSED(token))
86 {
87     w->index = luaH_stack_index;
88     w->newindex = luaH_stack_newindex;
89     w->widget = gtk_stack_new();
90 
91     g_object_connect(G_OBJECT(w->widget),
92         LUAKIT_WIDGET_SIGNAL_COMMON(w)
93     NULL);
94 
95     gtk_widget_show(w->widget);
96     return w;
97 }
98 
99 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80
100