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.
24 /** Create a new mouse button bindings.
25 * \param L The Lua VM state.
26 * \return The number of elements pushed on stack.
29 luaA_button_new(lua_State
*L
)
31 return luaA_class_new(L
, &button_class
);
34 /** Set a button array with a Lua table.
35 * \param L The Lua VM state.
36 * \param oidx The index of the object to store items into.
37 * \param idx The index of the Lua table.
38 * \param buttons The array button to fill.
41 luaA_button_array_set(lua_State
*L
, int oidx
, int idx
, button_array_t
*buttons
)
43 luaA_checktable(L
, idx
);
45 foreach(button
, *buttons
)
46 luaA_object_unref_item(L
, oidx
, *button
);
48 button_array_wipe(buttons
);
49 button_array_init(buttons
);
52 while(lua_next(L
, idx
))
53 if(luaA_toudata(L
, -1, &button_class
))
54 button_array_append(buttons
, luaA_object_ref_item(L
, oidx
, -1));
59 /** Push an array of button as an Lua table onto the stack.
60 * \param L The Lua VM state.
61 * \param oidx The index of the object to get items from.
62 * \param buttons The button array to push.
63 * \return The number of elements pushed on stack.
66 luaA_button_array_get(lua_State
*L
, int oidx
, button_array_t
*buttons
)
68 lua_createtable(L
, buttons
->len
, 0);
69 for(int i
= 0; i
< buttons
->len
; i
++)
71 luaA_object_push_item(L
, oidx
, buttons
->tab
[i
]);
72 lua_rawseti(L
, -2, i
+ 1);
77 LUA_OBJECT_EXPORT_PROPERTY(button
, button_t
, button
, lua_pushnumber
);
78 LUA_OBJECT_EXPORT_PROPERTY(button
, button_t
, modifiers
, luaA_pushmodifiers
);
81 luaA_button_set_modifiers(lua_State
*L
, button_t
*b
)
83 b
->modifiers
= luaA_tomodifiers(L
, -1);
84 luaA_object_emit_signal(L
, -3, "property::modifiers", 0);
89 luaA_button_set_button(lua_State
*L
, button_t
*b
)
91 b
->button
= luaL_checknumber(L
, -1);
92 luaA_object_emit_signal(L
, -3, "property::button", 0);
97 button_class_setup(lua_State
*L
)
99 static const struct luaL_Reg button_methods
[] =
101 LUA_CLASS_METHODS(button
)
102 { "__call", luaA_button_new
},
106 static const struct luaL_Reg button_meta
[] =
108 LUA_OBJECT_META(button
)
113 luaA_class_setup(L
, &button_class
, "button", NULL
,
114 (lua_class_allocator_t
) button_new
, NULL
, NULL
,
115 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
116 button_methods
, button_meta
);
117 luaA_class_add_property(&button_class
, "button",
118 (lua_class_propfunc_t
) luaA_button_set_button
,
119 (lua_class_propfunc_t
) luaA_button_get_button
,
120 (lua_class_propfunc_t
) luaA_button_set_button
);
121 luaA_class_add_property(&button_class
, "modifiers",
122 (lua_class_propfunc_t
) luaA_button_set_modifiers
,
123 (lua_class_propfunc_t
) luaA_button_get_modifiers
,
124 (lua_class_propfunc_t
) luaA_button_set_modifiers
);
126 signal_add(&button_class
.signals
, "press");
127 signal_add(&button_class
.signals
, "property::button");
128 signal_add(&button_class
.signals
, "property::modifiers");
129 signal_add(&button_class
.signals
, "release");
132 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80