2 * button.c - button managing
4 * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
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.
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.
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.
25 #include "common/luaobject.h"
27 /** Create a new mouse button bindings.
28 * \param L The Lua VM state.
29 * \return The number of elements pushed on stack.
32 luaA_button_new(lua_State
*L
)
38 if(lua_istable(L
, 2) && lua_isnumber(L
, 3))
40 luaA_deprecate(L
, "new syntax");
44 luaA_checktable(L
, 2);
45 /* arg 3 is mouse button */
46 xbutton
= luaL_checknumber(L
, 3);
48 /* arg 4 and 5 are callback functions, check they are functions... */
50 luaA_checkfunction(L
, 4);
52 luaA_checkfunction(L
, 5);
54 button
= button_new(L
);
55 button
->button
= xbutton
;
56 button
->modifiers
= luaA_tomodifiers(L
, 2);
61 luaA_object_add_signal(L
, -2, "press", -1);
67 luaA_object_add_signal(L
, -2, "release", -1);
73 return luaA_class_new(L
, &button_class
);
76 /** Set a button array with a Lua table.
77 * \param L The Lua VM state.
78 * \param oidx The index of the object to store items into.
79 * \param idx The index of the Lua table.
80 * \param buttons The array button to fill.
83 luaA_button_array_set(lua_State
*L
, int oidx
, int idx
, button_array_t
*buttons
)
85 luaA_checktable(L
, idx
);
87 foreach(button
, *buttons
)
88 luaA_object_unref_item(L
, oidx
, *button
);
90 button_array_wipe(buttons
);
91 button_array_init(buttons
);
94 while(lua_next(L
, idx
))
95 if(luaA_toudata(L
, -1, &button_class
))
96 button_array_append(buttons
, luaA_object_ref_item(L
, oidx
, -1));
101 /** Push an array of button as an Lua table onto the stack.
102 * \param L The Lua VM state.
103 * \param oidx The index of the object to get items from.
104 * \param buttons The button array to push.
105 * \return The number of elements pushed on stack.
108 luaA_button_array_get(lua_State
*L
, int oidx
, button_array_t
*buttons
)
110 lua_createtable(L
, buttons
->len
, 0);
111 for(int i
= 0; i
< buttons
->len
; i
++)
113 luaA_object_push_item(L
, oidx
, buttons
->tab
[i
]);
114 lua_rawseti(L
, -2, i
+ 1);
119 LUA_OBJECT_EXPORT_PROPERTY(button
, button_t
, button
, lua_pushnumber
);
120 LUA_OBJECT_EXPORT_PROPERTY(button
, button_t
, modifiers
, luaA_pushmodifiers
);
123 luaA_button_set_modifiers(lua_State
*L
, button_t
*b
)
125 b
->modifiers
= luaA_tomodifiers(L
, -1);
126 luaA_object_emit_signal(L
, -3, "property::modifiers", 0);
131 luaA_button_set_button(lua_State
*L
, button_t
*b
)
133 b
->button
= luaL_checknumber(L
, -1);
134 luaA_object_emit_signal(L
, -3, "property::button", 0);
139 button_class_setup(lua_State
*L
)
141 static const struct luaL_reg button_methods
[] =
143 LUA_CLASS_METHODS(button
)
144 { "__call", luaA_button_new
},
148 static const struct luaL_reg button_meta
[] =
150 LUA_OBJECT_META(button
)
152 { "__gc", luaA_object_gc
},
156 luaA_class_setup(L
, &button_class
, "button", (lua_class_allocator_t
) button_new
,
157 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
158 button_methods
, button_meta
);
159 luaA_class_add_property(&button_class
, A_TK_BUTTON
,
160 (lua_class_propfunc_t
) luaA_button_set_button
,
161 (lua_class_propfunc_t
) luaA_button_get_button
,
162 (lua_class_propfunc_t
) luaA_button_set_button
);
163 luaA_class_add_property(&button_class
, A_TK_MODIFIERS
,
164 (lua_class_propfunc_t
) luaA_button_set_modifiers
,
165 (lua_class_propfunc_t
) luaA_button_get_modifiers
,
166 (lua_class_propfunc_t
) luaA_button_set_modifiers
);
169 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80