Use util.escape to escape all buffer, prompt, uri & tab label texts
[luakit.git] / widgets / entry.c
blob5fa004cfb3b62bc7b32ce71eeec9290dbd7667e3
1 /*
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/>.
22 #include "luah.h"
23 #include "widgets/common.h"
25 static gint
26 luaH_entry_append(lua_State *L)
28 size_t len;
29 widget_t *w = luaH_checkudata(L, 1, &widget_class);
30 const gchar *text = luaL_checklstring(L, 2, &len);
31 gint pos = -1;
33 gtk_editable_insert_text(GTK_EDITABLE(w->widget),
34 text, g_utf8_strlen(text, len), &pos);
36 return pos + 1;
39 static gint
40 luaH_entry_insert(lua_State *L)
42 size_t len;
43 widget_t *w = luaH_checkudata(L, 1, &widget_class);
44 gint pos = luaL_checknumber(L, 2);
45 /* lua table indexes start at 1 */
46 if (pos > 0) pos--;
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);
52 return pos + 1;
55 static gint
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 */
61 if (pos > 0) pos--;
63 gtk_editable_set_position(GTK_EDITABLE(w->widget), pos);
64 lua_pushnumber(L, gtk_editable_get_position(GTK_EDITABLE(w->widget)));
65 return 1;
68 static gint
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)));
73 return 1;
76 static gint
77 luaH_entry_index(lua_State *L, luakit_token_t token)
79 widget_t *w = luaH_checkudata(L, 1, &widget_class);
81 switch(token)
83 case L_TK_DESTROY:
84 lua_pushcfunction(L, luaH_widget_destroy);
85 return 1;
87 case L_TK_TEXT:
88 lua_pushstring(L, gtk_entry_get_text(GTK_ENTRY(w->widget)));
89 return 1;
91 case L_TK_FG:
92 lua_pushstring(L, g_object_get_data(G_OBJECT(w->widget), "fg"));
93 return 1;
95 case L_TK_BG:
96 lua_pushstring(L, g_object_get_data(G_OBJECT(w->widget), "bg"));
97 return 1;
99 case L_TK_APPEND:
100 lua_pushcfunction(L, luaH_entry_append);
101 return 1;
103 case L_TK_INSERT:
104 lua_pushcfunction(L, luaH_entry_insert);
105 return 1;
107 case L_TK_GET_POSITION:
108 lua_pushcfunction(L, luaH_entry_get_position);
109 return 1;
111 case L_TK_SET_POSITION:
112 lua_pushcfunction(L, luaH_entry_set_position);
113 return 1;
115 case L_TK_SHOW_FRAME:
116 lua_pushboolean(L, gtk_entry_get_has_frame(GTK_ENTRY(w->widget)));
117 return 1;
119 case L_TK_FONT:
120 lua_pushstring(L, g_object_get_data(G_OBJECT(w->widget), "font"));
121 return 1;
123 case L_TK_SHOW:
124 lua_pushcfunction(L, luaH_widget_show);
125 return 1;
127 case L_TK_HIDE:
128 lua_pushcfunction(L, luaH_widget_hide);
129 return 1;
131 case L_TK_FOCUS:
132 lua_pushcfunction(L, luaH_widget_focus);
133 return 1;
135 default:
136 warn("unknown property: %s", luaL_checkstring(L, 2));
137 break;
139 return 0;
142 static gint
143 luaH_entry_newindex(lua_State *L, luakit_token_t token)
145 size_t len;
146 widget_t *w = luaH_checkudata(L, 1, &widget_class);
147 const gchar *tmp;
148 GdkColor c;
149 PangoFontDescription *font;
151 switch(token)
153 case L_TK_TEXT:
154 gtk_entry_set_text(GTK_ENTRY(w->widget),
155 luaL_checklstring(L, 3, &len));
156 break;
158 case L_TK_FG:
159 case L_TK_BG:
160 tmp = luaL_checklstring(L, 3, &len);
161 if (!gdk_color_parse(tmp, &c)) {
162 warn("invalid color: %s", tmp);
163 return 0;
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);
169 } else {
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);
173 break;
175 case L_TK_SHOW_FRAME:
176 gtk_entry_set_has_frame(GTK_ENTRY(w->widget), luaH_checkboolean(L, 3));
177 break;
179 case L_TK_FONT:
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);
184 break;
186 default:
187 warn("unknown property: %s", luaL_checkstring(L, 2));
188 return 0;
191 return luaH_object_emit_property_signal(L, 1);
194 static void
195 activate_cb(GtkEntry *e, widget_t *w)
197 (void) e;
198 lua_State *L = globalconf.L;
199 luaH_object_push(L, w->ref);
200 luaH_object_emit_signal(L, -1, "activate", 0, 0);
201 lua_pop(L, 1);
204 static void
205 changed_cb(GtkEditable *e, widget_t *w)
207 (void) e;
208 lua_State *L = globalconf.L;
209 luaH_object_push(L, w->ref);
210 luaH_object_emit_signal(L, -1, "changed", 0, 0);
211 lua_pop(L, 1);
214 static void
215 entry_destructor(widget_t *w)
217 gtk_widget_destroy(w->widget);
220 widget_t *
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,
241 NULL);
243 gtk_widget_show(w->widget);
244 return w;
247 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80