Fix wibox.layout.rotate
[awesome.git] / objects / button.c
blob4eac0fc0598e2d350592056c0a497c222e8f98e4
1 /*
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.
22 #include "button.h"
23 #include "luaa.h"
24 #include "objects/key.h"
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.
31 static int
32 luaA_button_new(lua_State *L)
34 return luaA_class_new(L, &button_class);
37 /** Set a button array with a Lua table.
38 * \param L The Lua VM state.
39 * \param oidx The index of the object to store items into.
40 * \param idx The index of the Lua table.
41 * \param buttons The array button to fill.
43 void
44 luaA_button_array_set(lua_State *L, int oidx, int idx, button_array_t *buttons)
46 luaA_checktable(L, idx);
48 foreach(button, *buttons)
49 luaA_object_unref_item(L, oidx, *button);
51 button_array_wipe(buttons);
52 button_array_init(buttons);
54 lua_pushnil(L);
55 while(lua_next(L, idx))
56 if(luaA_toudata(L, -1, &button_class))
57 button_array_append(buttons, luaA_object_ref_item(L, oidx, -1));
58 else
59 lua_pop(L, 1);
62 /** Push an array of button as an Lua table onto the stack.
63 * \param L The Lua VM state.
64 * \param oidx The index of the object to get items from.
65 * \param buttons The button array to push.
66 * \return The number of elements pushed on stack.
68 int
69 luaA_button_array_get(lua_State *L, int oidx, button_array_t *buttons)
71 lua_createtable(L, buttons->len, 0);
72 for(int i = 0; i < buttons->len; i++)
74 luaA_object_push_item(L, oidx, buttons->tab[i]);
75 lua_rawseti(L, -2, i + 1);
77 return 1;
80 LUA_OBJECT_EXPORT_PROPERTY(button, button_t, button, lua_pushnumber);
81 LUA_OBJECT_EXPORT_PROPERTY(button, button_t, modifiers, luaA_pushmodifiers);
83 static int
84 luaA_button_set_modifiers(lua_State *L, button_t *b)
86 b->modifiers = luaA_tomodifiers(L, -1);
87 luaA_object_emit_signal(L, -3, "property::modifiers", 0);
88 return 0;
91 static int
92 luaA_button_set_button(lua_State *L, button_t *b)
94 b->button = luaL_checknumber(L, -1);
95 luaA_object_emit_signal(L, -3, "property::button", 0);
96 return 0;
99 void
100 button_class_setup(lua_State *L)
102 static const struct luaL_Reg button_methods[] =
104 LUA_CLASS_METHODS(button)
105 { "__call", luaA_button_new },
106 { NULL, NULL }
109 static const struct luaL_Reg button_meta[] =
111 LUA_OBJECT_META(button)
112 LUA_CLASS_META
113 { NULL, NULL }
116 luaA_class_setup(L, &button_class, "button", NULL,
117 (lua_class_allocator_t) button_new, NULL, NULL,
118 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
119 button_methods, button_meta);
120 luaA_class_add_property(&button_class, "button",
121 (lua_class_propfunc_t) luaA_button_set_button,
122 (lua_class_propfunc_t) luaA_button_get_button,
123 (lua_class_propfunc_t) luaA_button_set_button);
124 luaA_class_add_property(&button_class, "modifiers",
125 (lua_class_propfunc_t) luaA_button_set_modifiers,
126 (lua_class_propfunc_t) luaA_button_get_modifiers,
127 (lua_class_propfunc_t) luaA_button_set_modifiers);
129 signal_add(&button_class.signals, "press");
130 signal_add(&button_class.signals, "property::button");
131 signal_add(&button_class.signals, "property::modifiers");
132 signal_add(&button_class.signals, "release");
135 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80