Add `bind.but({mods}, num, func)` for mouse bindings & add mouse binds
[luakit.git] / widgets / common.c
blob5b0fe33641a5ceab375afa39cb848e9068e23385
1 /*
2 * widgets/common.c - common widget functions or callbacks
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
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 3 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
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <gtk/gtk.h>
23 #include "luah.h"
24 #include "globalconf.h"
25 #include "common/luaobject.h"
26 #include "common/lualib.h"
27 #include "widgets/common.h"
29 gboolean
30 key_press_cb(GtkWidget *win, GdkEventKey *ev, widget_t *w)
32 (void) win;
33 gint ret;
34 lua_State *L = globalconf.L;
35 luaH_object_push(L, w->ref);
36 luaH_modifier_table_push(L, ev->state);
37 luaH_keystr_push(L, ev->keyval);
38 ret = luaH_object_emit_signal(L, -3, "key-press", 2, 1);
39 lua_pop(L, 1 + ret);
40 return ret ? TRUE : FALSE;
44 gboolean
45 button_press_cb(GtkWidget *win, GdkEventButton *ev, widget_t *w)
47 (void) win;
48 gint ret = 0;
49 lua_State *L = globalconf.L;
50 luaH_object_push(L, w->ref);
51 luaH_modifier_table_push(L, ev->state);
52 lua_pushinteger(L, ev->button);
53 ret = luaH_object_emit_signal(L, -3, "button-press", 2, 1);
54 /* User responded with TRUE, so do not propagate event any further */
55 if (ret && luaH_checkboolean(L, -1)) {
56 lua_pop(L, ret + 1);
57 return TRUE;
59 lua_pop(L, ret + 1);
60 /* propagate event further */
61 return FALSE;
64 gboolean
65 button_release_cb(GtkWidget *win, GdkEventButton *ev, widget_t *w)
67 (void) win;
68 gint ret = 0;
69 lua_State *L = globalconf.L;
70 luaH_object_push(L, w->ref);
71 luaH_modifier_table_push(L, ev->state);
72 lua_pushinteger(L, ev->button);
73 ret = luaH_object_emit_signal(L, -3, "button-release", 2, 1);
74 /* User responded with TRUE, so do not propagate event any further */
75 if (ret && luaH_checkboolean(L, -1)) {
76 lua_pop(L, ret + 1);
77 return TRUE;
79 lua_pop(L, ret + 1);
80 /* propagate event further */
81 return FALSE;
84 gboolean
85 focus_cb(GtkWidget *win, GdkEventFocus *ev, widget_t *w)
87 (void) win;
88 lua_State *L = globalconf.L;
89 luaH_object_push(L, w->ref);
90 if (ev->in)
91 luaH_object_emit_signal(L, -1, "focus", 0, 0);
92 else
93 luaH_object_emit_signal(L, -1, "unfocus", 0, 0);
94 lua_pop(L, 1);
95 return FALSE;
98 /* gtk container add callback */
99 void
100 add_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
102 (void) c;
103 widget_t *child = g_object_get_data(G_OBJECT(widget), "widget");
104 lua_State *L = globalconf.L;
105 luaH_object_push(L, w->ref);
106 luaH_object_push(L, child->ref);
107 luaH_object_emit_signal(L, -2, "add", 1, 0);
108 lua_pop(L, 1);
111 /* gtk container remove callback */
112 void
113 remove_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
115 (void) c;
116 widget_t *child = g_object_get_data(G_OBJECT(widget), "widget");
117 lua_State *L = globalconf.L;
118 luaH_object_push(L, w->ref);
119 luaH_object_push(L, child->ref);
120 luaH_object_emit_signal(L, -2, "remove", 1, 0);
121 lua_pop(L, 1);
124 void
125 parent_set_cb(GtkWidget *widget, GtkObject *old, widget_t *w)
127 (void) old;
128 lua_State *L = globalconf.L;
129 widget_t *parent = NULL;
130 GtkContainer *new;
131 g_object_get(G_OBJECT(widget), "parent", &new, NULL);
132 luaH_object_push(L, w->ref);
133 if (new)
134 parent = g_object_get_data(G_OBJECT(new), "widget");
135 if (parent)
136 luaH_object_push(L, parent->ref);
137 else
138 lua_pushnil(L);
139 luaH_object_emit_signal(L, -2, "parent-set", 1, 0);
140 lua_pop(L, 1);
143 gboolean
144 true_cb()
146 return TRUE;
149 /* set child method for gtk container widgets */
150 gint
151 luaH_widget_set_child(lua_State *L)
153 widget_t *w = luaH_checkudata(L, 1, &widget_class);
154 widget_t *child = luaH_checkudataornil(L, 2, &widget_class);
156 /* remove old child */
157 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(w->widget));
158 if (widget) {
159 g_object_ref(G_OBJECT(widget));
160 gtk_container_remove(GTK_CONTAINER(w->widget), GTK_WIDGET(widget));
163 /* add new child to container */
164 if (child)
165 gtk_container_add(GTK_CONTAINER(w->widget), GTK_WIDGET(child->widget));
166 return 0;
169 /* get child method for gtk container widgets */
170 gint
171 luaH_widget_get_child(lua_State *L)
173 widget_t *w = luaH_checkudata(L, 1, &widget_class);
174 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(w->widget));
175 widget_t *child = NULL;
177 if (!widget)
178 return 0;
180 child = g_object_get_data(G_OBJECT(child), "lua_widget");
181 luaH_object_push(L, child->ref);
182 return 1;
185 gint
186 luaH_widget_show(lua_State *L)
188 widget_t *w = luaH_checkudata(L, 1, &widget_class);
189 gtk_widget_show(w->widget);
190 return 0;
193 gint
194 luaH_widget_hide(lua_State *L)
196 widget_t *w = luaH_checkudata(L, 1, &widget_class);
197 gtk_widget_hide(w->widget);
198 return 0;
201 gint
202 luaH_widget_focus(lua_State *L)
204 widget_t *w = luaH_checkudata(L, 1, &widget_class);
205 gtk_widget_grab_focus(w->widget);
206 return 0;
209 gint
210 luaH_widget_destroy(lua_State *L)
212 widget_t *w = luaH_checkudata(L, 1, &widget_class);
213 if (w->destructor)
214 w->destructor(w);
215 w->destructor = NULL;
216 luaH_object_unref(L, w->ref);
217 return 0;
220 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80