forked from luakit/luakit
/
common.c
192 lines (172 loc) · 4.77 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* widgets/common.c - common widget functions or callbacks
*
* Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <gtk/gtk.h>
#include "luah.h"
#include "globalconf.h"
#include "common/luaobject.h"
#include "common/lualib.h"
#include "widgets/common.h"
gboolean
key_press_cb(GtkWidget *win, GdkEventKey *ev, widget_t *w)
{
(void) win;
gint ret;
lua_State *L = globalconf.L;
luaH_object_push(L, w->ref);
luaH_modifier_table_push(L, ev->state);
luaH_keystr_push(L, ev->keyval);
ret = luaH_object_emit_signal(L, -3, "key-press", 2, 1);
lua_pop(L, 1 + ret);
return ret ? TRUE : FALSE;
}
gboolean
button_press_cb(GtkWidget *win, GdkEventButton *ev, widget_t *w)
{
(void) win;
if (ev->type != GDK_BUTTON_PRESS)
return FALSE;
lua_State *L = globalconf.L;
luaH_object_push(L, w->ref);
luaH_object_emit_signal(L, -1, "clicked", 0, 0);
lua_pop(L, 1);
return FALSE;
}
gboolean
focus_cb(GtkWidget *win, GdkEventFocus *ev, widget_t *w)
{
(void) win;
lua_State *L = globalconf.L;
luaH_object_push(L, w->ref);
if (ev->in)
luaH_object_emit_signal(L, -1, "focus", 0, 0);
else
luaH_object_emit_signal(L, -1, "unfocus", 0, 0);
lua_pop(L, 1);
return FALSE;
}
/* gtk container add callback */
void
add_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
{
(void) c;
widget_t *child = g_object_get_data(G_OBJECT(widget), "widget");
lua_State *L = globalconf.L;
luaH_object_push(L, w->ref);
luaH_object_push(L, child->ref);
luaH_object_emit_signal(L, -2, "add", 1, 0);
lua_pop(L, 1);
}
/* gtk container remove callback */
void
remove_cb(GtkContainer *c, GtkWidget *widget, widget_t *w)
{
(void) c;
widget_t *child = g_object_get_data(G_OBJECT(widget), "widget");
lua_State *L = globalconf.L;
luaH_object_push(L, w->ref);
luaH_object_push(L, child->ref);
luaH_object_emit_signal(L, -2, "remove", 1, 0);
lua_pop(L, 1);
}
void
parent_set_cb(GtkWidget *widget, GtkObject *old, widget_t *w)
{
(void) old;
lua_State *L = globalconf.L;
widget_t *parent = NULL;
GtkContainer *new;
g_object_get(G_OBJECT(widget), "parent", &new, NULL);
luaH_object_push(L, w->ref);
if (new)
parent = g_object_get_data(G_OBJECT(new), "widget");
if (parent)
luaH_object_push(L, parent->ref);
else
lua_pushnil(L);
luaH_object_emit_signal(L, -2, "parent-set", 1, 0);
lua_pop(L, 1);
}
gboolean
true_cb()
{
return TRUE;
}
/* set child method for gtk container widgets */
gint
luaH_widget_set_child(lua_State *L)
{
widget_t *w = luaH_checkudata(L, 1, &widget_class);
widget_t *child = luaH_checkudataornil(L, 2, &widget_class);
/* remove old child */
GtkWidget *widget = gtk_bin_get_child(GTK_BIN(w->widget));
if (widget) {
g_object_ref(G_OBJECT(widget));
gtk_container_remove(GTK_CONTAINER(w->widget), GTK_WIDGET(widget));
}
/* add new child to container */
if (child)
gtk_container_add(GTK_CONTAINER(w->widget), GTK_WIDGET(child->widget));
return 0;
}
/* get child method for gtk container widgets */
gint
luaH_widget_get_child(lua_State *L)
{
widget_t *w = luaH_checkudata(L, 1, &widget_class);
GtkWidget *widget = gtk_bin_get_child(GTK_BIN(w->widget));
widget_t *child = NULL;
if (!widget)
return 0;
child = g_object_get_data(G_OBJECT(child), "lua_widget");
luaH_object_push(L, child->ref);
return 1;
}
gint
luaH_widget_show(lua_State *L)
{
widget_t *w = luaH_checkudata(L, 1, &widget_class);
gtk_widget_show(w->widget);
return 0;
}
gint
luaH_widget_hide(lua_State *L)
{
widget_t *w = luaH_checkudata(L, 1, &widget_class);
gtk_widget_hide(w->widget);
return 0;
}
gint
luaH_widget_focus(lua_State *L)
{
widget_t *w = luaH_checkudata(L, 1, &widget_class);
gtk_widget_grab_focus(w->widget);
return 0;
}
gint
luaH_widget_destroy(lua_State *L)
{
widget_t *w = luaH_checkudata(L, 1, &widget_class);
if (w->destructor)
w->destructor(w);
w->destructor = NULL;
luaH_object_unref(L, w->ref);
return 0;
}
// vim: ft=c:et:sw=4:ts=8:sts=4:tw=80