1 /*
2 * button.c - button managing
3 *
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
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 2 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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22 /** awesome button API
23 *
24 * Furthermore to the classes described here, one can also use signals as
25 * described in @{signals}.
26 *
27 * Some signal names are starting with a dot. These dots are artefacts from
28 * the documentation generation, you get the real signal name by
29 * removing the starting dot.
30 *
31 * @author Julien Danjou <julien@danjou.info>
32 * @copyright 2008-2009 Julien Danjou
33 * @classmod button
34 */
35
36 #include "button.h"
37
38 lua_class_t button_class;
39
40 /** Button object.
41 *
42 * @tfield int button The mouse button number, or 0 for any button.
43 * @tfield table modifiers The modifier key table that should be pressed while the
44 * button is pressed.
45 * @table button
46 */
47
48 /** Get the number of instances.
49 * @treturn int The number of button objects alive.
50 * @function instances
51 */
52
53 /** Set a __index metamethod for all button instances.
54 * @tparam function cb The meta-method
55 * @function set_index_miss_handler
56 */
57
58 /** Set a __newindex metamethod for all button instances.
59 * @tparam function cb The meta-method
60 * @function set_newindex_miss_handler
61 */
62
63 /** When bound mouse button + modifiers are pressed.
64 * @param ... One or more arguments are possible
65 * @signal press
66 */
67
68 /** When property changes.
69 * @signal property::button
70 */
71
72 /** When property changes.
73 * @signal property::modifiers
74 */
75
76 /** When bound mouse button + modifiers are pressed.
77 * @param ... One or more arguments are possible
78 * @signal release
79 */
80
81 /** Create a new mouse button bindings.
82 * \param L The Lua VM state.
83 * \return The number of elements pushed on stack.
84 */
85 static int
luaA_button_new(lua_State * L)86 luaA_button_new(lua_State *L)
87 {
88 return luaA_class_new(L, &button_class);
89 }
90
91 /** Set a button array with a Lua table.
92 * \param L The Lua VM state.
93 * \param oidx The index of the object to store items into.
94 * \param idx The index of the Lua table.
95 * \param buttons The array button to fill.
96 */
97 void
luaA_button_array_set(lua_State * L,int oidx,int idx,button_array_t * buttons)98 luaA_button_array_set(lua_State *L, int oidx, int idx, button_array_t *buttons)
99 {
100 luaA_checktable(L, idx);
101
102 foreach(button, *buttons)
103 luaA_object_unref_item(L, oidx, *button);
104
105 button_array_wipe(buttons);
106 button_array_init(buttons);
107
108 lua_pushnil(L);
109 while(lua_next(L, idx))
110 if(luaA_toudata(L, -1, &button_class))
111 button_array_append(buttons, luaA_object_ref_item(L, oidx, -1));
112 else
113 lua_pop(L, 1);
114 }
115
116 /** Push an array of button as an Lua table onto the stack.
117 * \param L The Lua VM state.
118 * \param oidx The index of the object to get items from.
119 * \param buttons The button array to push.
120 * \return The number of elements pushed on stack.
121 */
122 int
luaA_button_array_get(lua_State * L,int oidx,button_array_t * buttons)123 luaA_button_array_get(lua_State *L, int oidx, button_array_t *buttons)
124 {
125 lua_createtable(L, buttons->len, 0);
126 for(int i = 0; i < buttons->len; i++)
127 {
128 luaA_object_push_item(L, oidx, buttons->tab[i]);
129 lua_rawseti(L, -2, i + 1);
130 }
131 return 1;
132 }
133
134 LUA_OBJECT_EXPORT_PROPERTY(button, button_t, button, lua_pushinteger);
135 LUA_OBJECT_EXPORT_PROPERTY(button, button_t, modifiers, luaA_pushmodifiers);
136
137 static int
luaA_button_set_modifiers(lua_State * L,button_t * b)138 luaA_button_set_modifiers(lua_State *L, button_t *b)
139 {
140 b->modifiers = luaA_tomodifiers(L, -1);
141 luaA_object_emit_signal(L, -3, "property::modifiers", 0);
142 return 0;
143 }
144
145 static int
luaA_button_set_button(lua_State * L,button_t * b)146 luaA_button_set_button(lua_State *L, button_t *b)
147 {
148 b->button = luaL_checkinteger(L, -1);
149 luaA_object_emit_signal(L, -3, "property::button", 0);
150 return 0;
151 }
152
153 void
button_class_setup(lua_State * L)154 button_class_setup(lua_State *L)
155 {
156 static const struct luaL_Reg button_methods[] =
157 {
158 LUA_CLASS_METHODS(button)
159 { "__call", luaA_button_new },
160 { NULL, NULL }
161 };
162
163 static const struct luaL_Reg button_meta[] =
164 {
165 LUA_OBJECT_META(button)
166 LUA_CLASS_META
167 { NULL, NULL }
168 };
169
170 luaA_class_setup(L, &button_class, "button", NULL,
171 (lua_class_allocator_t) button_new, NULL, NULL,
172 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
173 button_methods, button_meta);
174 luaA_class_add_property(&button_class, "button",
175 (lua_class_propfunc_t) luaA_button_set_button,
176 (lua_class_propfunc_t) luaA_button_get_button,
177 (lua_class_propfunc_t) luaA_button_set_button);
178 luaA_class_add_property(&button_class, "modifiers",
179 (lua_class_propfunc_t) luaA_button_set_modifiers,
180 (lua_class_propfunc_t) luaA_button_get_modifiers,
181 (lua_class_propfunc_t) luaA_button_set_modifiers);
182 }
183
184 /* @DOC_cobject_COMMON@ */
185
186 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
187