2 * widgets/entry.c - gtk entry widget wrapper
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
5 * Copyright (C) 2007-2009 Julien Danjou <julien@danjou.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "widgets/common.h"
26 luaH_entry_append(lua_State
*L
)
29 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
30 const gchar
*text
= luaL_checklstring(L
, 2, &len
);
33 gtk_editable_insert_text(GTK_EDITABLE(w
->widget
),
34 text
, g_utf8_strlen(text
, len
), &pos
);
40 luaH_entry_insert(lua_State
*L
)
43 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
44 gint pos
= luaL_checknumber(L
, 2);
45 /* lua table indexes start at 1 */
47 const gchar
*text
= luaL_checklstring(L
, 3, &len
);
49 gtk_editable_insert_text(GTK_EDITABLE(w
->widget
),
50 text
, g_utf8_strlen(text
, len
), &pos
);
56 luaH_entry_set_position(lua_State
*L
)
58 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
59 gint pos
= luaL_checknumber(L
, 2);
60 /* lua table indexes start at 1 */
63 gtk_editable_set_position(GTK_EDITABLE(w
->widget
), pos
);
64 lua_pushnumber(L
, gtk_editable_get_position(GTK_EDITABLE(w
->widget
)));
69 luaH_entry_get_position(lua_State
*L
)
71 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
72 lua_pushnumber(L
, gtk_editable_get_position(GTK_EDITABLE(w
->widget
)));
77 luaH_entry_index(lua_State
*L
, luakit_token_t token
)
79 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
84 lua_pushcfunction(L
, luaH_widget_destroy
);
88 lua_pushstring(L
, gtk_entry_get_text(GTK_ENTRY(w
->widget
)));
92 lua_pushstring(L
, g_object_get_data(G_OBJECT(w
->widget
), "fg"));
96 lua_pushstring(L
, g_object_get_data(G_OBJECT(w
->widget
), "bg"));
100 lua_pushcfunction(L
, luaH_entry_append
);
104 lua_pushcfunction(L
, luaH_entry_insert
);
107 case L_TK_GET_POSITION
:
108 lua_pushcfunction(L
, luaH_entry_get_position
);
111 case L_TK_SET_POSITION
:
112 lua_pushcfunction(L
, luaH_entry_set_position
);
115 case L_TK_SHOW_FRAME
:
116 lua_pushboolean(L
, gtk_entry_get_has_frame(GTK_ENTRY(w
->widget
)));
120 lua_pushstring(L
, g_object_get_data(G_OBJECT(w
->widget
), "font"));
124 lua_pushcfunction(L
, luaH_widget_show
);
128 lua_pushcfunction(L
, luaH_widget_hide
);
132 lua_pushcfunction(L
, luaH_widget_focus
);
136 warn("unknown property: %s", luaL_checkstring(L
, 2));
143 luaH_entry_newindex(lua_State
*L
, luakit_token_t token
)
146 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
149 PangoFontDescription
*font
;
154 gtk_entry_set_text(GTK_ENTRY(w
->widget
),
155 luaL_checklstring(L
, 3, &len
));
160 tmp
= luaL_checklstring(L
, 3, &len
);
161 if (!gdk_color_parse(tmp
, &c
)) {
162 warn("invalid color: %s", tmp
);
166 if (token
== L_TK_FG
) {
167 gtk_widget_modify_text(GTK_WIDGET(w
->widget
), GTK_STATE_NORMAL
, &c
);
168 g_object_set_data_full(G_OBJECT(w
->widget
), "fg", g_strdup(tmp
), g_free
);
170 gtk_widget_modify_base(GTK_WIDGET(w
->widget
), GTK_STATE_NORMAL
, &c
);
171 g_object_set_data_full(G_OBJECT(w
->widget
), "bg", g_strdup(tmp
), g_free
);
175 case L_TK_SHOW_FRAME
:
176 gtk_entry_set_has_frame(GTK_ENTRY(w
->widget
), luaH_checkboolean(L
, 3));
180 tmp
= luaL_checklstring(L
, 3, &len
);
181 font
= pango_font_description_from_string(tmp
);
182 gtk_widget_modify_font(GTK_WIDGET(w
->widget
), font
);
183 g_object_set_data_full(G_OBJECT(w
->widget
), "font", g_strdup(tmp
), g_free
);
187 warn("unknown property: %s", luaL_checkstring(L
, 2));
191 return luaH_object_emit_property_signal(L
, 1);
195 activate_cb(GtkEntry
*e
, widget_t
*w
)
198 lua_State
*L
= globalconf
.L
;
199 luaH_object_push(L
, w
->ref
);
200 luaH_object_emit_signal(L
, -1, "activate", 0, 0);
205 changed_cb(GtkEditable
*e
, widget_t
*w
)
208 lua_State
*L
= globalconf
.L
;
209 luaH_object_push(L
, w
->ref
);
210 luaH_object_emit_signal(L
, -1, "changed", 0, 0);
215 entry_destructor(widget_t
*w
)
217 gtk_widget_destroy(w
->widget
);
221 widget_entry(widget_t
*w
)
223 w
->index
= luaH_entry_index
;
224 w
->newindex
= luaH_entry_newindex
;
225 w
->destructor
= entry_destructor
;
227 /* create gtk label widget as main widget */
228 w
->widget
= gtk_entry_new();
229 g_object_set_data(G_OBJECT(w
->widget
), "widget", (gpointer
) w
);
231 /* setup default settings */
232 gtk_entry_set_inner_border(GTK_ENTRY(w
->widget
), NULL
);
234 g_object_connect((GObject
*)w
->widget
,
235 "signal::activate", (GCallback
)activate_cb
, w
,
236 "signal::changed", (GCallback
)changed_cb
, w
,
237 "signal::focus-in-event", (GCallback
)focus_cb
, w
,
238 "signal::focus-out-event", (GCallback
)focus_cb
, w
,
239 "signal::key-press-event", (GCallback
)key_press_cb
, w
,
240 "signal::parent-set", (GCallback
)parent_set_cb
, w
,
243 gtk_widget_show(w
->widget
);
247 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80