These are static functions
[luakit.git] / widget.c
blob51e356f05aba5ba5bc2a692bb7ef88aab03d36b3
1 /*
2 * widget.c - widget managing
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 #include "widget.h"
24 widget_info_t widgets_list[] = {
25 { L_TK_ENTRY, "entry", widget_entry },
26 { L_TK_EVENTBOX, "eventbox", widget_eventbox },
27 { L_TK_HBOX, "hbox", widget_hbox },
28 { L_TK_LABEL, "label", widget_label },
29 { L_TK_NOTEBOOK, "notebook", widget_notebook },
30 { L_TK_TEXTBUTTON, "textbutton", widget_textbutton },
31 { L_TK_VBOX, "vbox", widget_vbox },
32 { L_TK_WEBVIEW, "webview", widget_webview },
33 { L_TK_WINDOW, "window", widget_window },
34 { L_TK_UNKNOWN, NULL, NULL }
37 LUA_OBJECT_FUNCS(widget_class, widget_t, widget);
39 /** Collect a widget structure.
40 * \param L The Lua VM state.
41 * \return 0
43 static gint
44 luaH_widget_gc(lua_State *L)
46 widget_t *w = luaH_checkudata(L, 1, &widget_class);
47 if(w->destructor)
48 w->destructor(w);
49 return luaH_object_gc(L);
52 /** Create a new widget.
53 * \param L The Lua VM state.
55 * \luastack
56 * \lparam A table with at least a type value.
57 * \lreturn A brand new widget.
59 static gint
60 luaH_widget_new(lua_State *L)
62 luaH_class_new(L, &widget_class);
63 widget_t *w = luaH_checkudata(L, -1, &widget_class);
65 /* save ref to the lua class instance */
66 lua_pushvalue(L, -1);
67 w->ref = luaH_object_ref_class(L, -1, &widget_class);
69 return 1;
72 /** Generic widget.
73 * \param L The Lua VM state.
74 * \return The number of elements pushed on stack.
75 * \luastack
76 * \lfield visible The widget visibility.
77 * \lfield mouse_enter A function to execute when the mouse enter the widget.
78 * \lfield mouse_leave A function to execute when the mouse leave the widget.
80 static gint
81 luaH_widget_index(lua_State *L)
83 size_t len;
84 const char *prop = luaL_checklstring(L, 2, &len);
85 luakit_token_t token = l_tokenize(prop, len);
87 /* Try standard method */
88 if(luaH_class_index(L))
89 return 1;
91 /* Then call special widget index */
92 widget_t *widget = luaH_checkudata(L, 1, &widget_class);
93 return widget->index ? widget->index(L, token) : 0;
96 /** Generic widget newindex.
97 * \param L The Lua VM state.
98 * \return The number of elements pushed on stack.
100 static gint
101 luaH_widget_newindex(lua_State *L)
103 size_t len;
104 const char *prop = luaL_checklstring(L, 2, &len);
105 luakit_token_t token = l_tokenize(prop, len);
107 /* Try standard method */
108 luaH_class_newindex(L);
110 /* Then call special widget newindex */
111 widget_t *widget = luaH_checkudata(L, 1, &widget_class);
112 return widget->newindex ? widget->newindex(L, token) : 0;
115 static gint
116 luaH_widget_set_type(lua_State *L, widget_t *w)
118 if (w->info)
119 luaL_error(L, "widget is already of type: %s", w->info->name);
121 size_t len;
122 const gchar *type = luaL_checklstring(L, -1, &len);
123 luakit_token_t tok = l_tokenize(type, len);
124 widget_info_t *winfo;
126 for (guint i = 0; i < LENGTH(widgets_list); i++)
128 if (widgets_list[i].tok != tok)
129 continue;
131 winfo = &widgets_list[i];
132 w->info = winfo;
133 winfo->wc(w);
134 luaH_object_emit_signal(L, -3, "init", 0, 0);
135 return 0;
138 luaL_error(L, "unknown widget type: %s", type);
139 return 0;
142 static gint
143 luaH_widget_get_type(lua_State *L, widget_t *w)
145 if (!w->info)
146 return 0;
148 lua_pushstring(L, w->info->name);
149 return 1;
152 void
153 widget_class_setup(lua_State *L)
155 static const struct luaL_reg widget_methods[] =
157 LUA_CLASS_METHODS(widget)
158 { "__call", luaH_widget_new },
159 { NULL, NULL }
162 static const struct luaL_reg widget_meta[] =
164 LUA_OBJECT_META(widget)
165 { "__index", luaH_widget_index },
166 { "__newindex", luaH_widget_newindex },
167 { "__gc", luaH_widget_gc },
168 { NULL, NULL }
171 luaH_class_setup(L, &widget_class, "widget", (lua_class_allocator_t) widget_new,
172 NULL, NULL,
173 widget_methods, widget_meta);
175 luaH_class_add_property(&widget_class, L_TK_TYPE,
176 (lua_class_propfunc_t) luaH_widget_set_type,
177 (lua_class_propfunc_t) luaH_widget_get_type,
178 NULL);
181 // vim: ft=c:et:sw=4:ts=8:sts=4:enc=utf-8:tw=80