forked from luakit/luakit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget.c
182 lines (157 loc) · 5.06 KB
/
widget.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* widget.c - widget managing
*
* Copyright (C) 2010 Mason Larobina <[email protected]>
* Copyright (C) 2007-2009 Julien Danjou <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "widget.h"
widget_info_t widgets_list[] = {
{ L_TK_ENTRY, "entry", widget_entry },
{ L_TK_EVENTBOX, "eventbox", widget_eventbox },
{ L_TK_HBOX, "hbox", widget_hbox },
{ L_TK_LABEL, "label", widget_label },
{ L_TK_NOTEBOOK, "notebook", widget_notebook },
{ L_TK_TEXTBUTTON, "textbutton", widget_textbutton },
{ L_TK_VBOX, "vbox", widget_vbox },
{ L_TK_WEBVIEW, "webview", widget_webview },
{ L_TK_WINDOW, "window", widget_window },
{ L_TK_UNKNOWN, NULL, NULL }
};
LUA_OBJECT_FUNCS(widget_class, widget_t, widget);
/** Collect a widget structure.
* \param L The Lua VM state.
* \return 0
*/
static gint
luaH_widget_gc(lua_State *L)
{
widget_t *w = luaH_checkudata(L, 1, &widget_class);
debug("collecting widget at %p of type '%s'", w, w->info->name);
if(w->destructor)
w->destructor(w);
return luaH_object_gc(L);
}
/** Create a new widget.
* \param L The Lua VM state.
*
* \luastack
* \lparam A table with at least a type value.
* \lreturn A brand new widget.
*/
static gint
luaH_widget_new(lua_State *L)
{
luaH_class_new(L, &widget_class);
widget_t *w = luaH_checkudata(L, -1, &widget_class);
/* save ref to the lua class instance */
lua_pushvalue(L, -1);
w->ref = luaH_object_ref_class(L, -1, &widget_class);
return 1;
}
/** Generic widget.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lfield visible The widget visibility.
* \lfield mouse_enter A function to execute when the mouse enter the widget.
* \lfield mouse_leave A function to execute when the mouse leave the widget.
*/
static gint
luaH_widget_index(lua_State *L)
{
size_t len;
const char *prop = luaL_checklstring(L, 2, &len);
luakit_token_t token = l_tokenize(prop, len);
/* Try standard method */
if(luaH_class_index(L))
return 1;
/* Then call special widget index */
widget_t *widget = luaH_checkudata(L, 1, &widget_class);
return widget->index ? widget->index(L, token) : 0;
}
/** Generic widget newindex.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static gint
luaH_widget_newindex(lua_State *L)
{
size_t len;
const char *prop = luaL_checklstring(L, 2, &len);
luakit_token_t token = l_tokenize(prop, len);
/* Try standard method */
luaH_class_newindex(L);
/* Then call special widget newindex */
widget_t *widget = luaH_checkudata(L, 1, &widget_class);
return widget->newindex ? widget->newindex(L, token) : 0;
}
static gint
luaH_widget_set_type(lua_State *L, widget_t *w)
{
if (w->info)
luaL_error(L, "widget is already of type: %s", w->info->name);
size_t len;
const gchar *type = luaL_checklstring(L, -1, &len);
luakit_token_t tok = l_tokenize(type, len);
widget_info_t *winfo;
for (guint i = 0; i < LENGTH(widgets_list); i++)
{
if (widgets_list[i].tok != tok)
continue;
winfo = &widgets_list[i];
w->info = winfo;
winfo->wc(w);
luaH_object_emit_signal(L, -3, "init", 0, 0);
return 0;
}
luaL_error(L, "unknown widget type: %s", type);
return 0;
}
static gint
luaH_widget_get_type(lua_State *L, widget_t *w)
{
if (!w->info)
return 0;
lua_pushstring(L, w->info->name);
return 1;
}
void
widget_class_setup(lua_State *L)
{
static const struct luaL_reg widget_methods[] =
{
LUA_CLASS_METHODS(widget)
{ "__call", luaH_widget_new },
{ NULL, NULL }
};
static const struct luaL_reg widget_meta[] =
{
LUA_OBJECT_META(widget)
{ "__index", luaH_widget_index },
{ "__newindex", luaH_widget_newindex },
{ "__gc", luaH_widget_gc },
{ NULL, NULL }
};
luaH_class_setup(L, &widget_class, "widget", (lua_class_allocator_t) widget_new,
NULL, NULL,
widget_methods, widget_meta);
luaH_class_add_property(&widget_class, L_TK_TYPE,
(lua_class_propfunc_t) luaH_widget_set_type,
(lua_class_propfunc_t) luaH_widget_get_type,
NULL);
}
// vim: ft=c:et:sw=4:ts=8:sts=4:tw=80