Add P{B,F,N,S}_CASE & LUAKIT_WIDGET_{,BIN_}INDEX_COMMON macros
[luakit.git] / widgets / entry.c
blob6794f473b4dbe2a296bccb757c5d2ab87cc27ba1
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 LUAKIT_WIDGET_INDEX_COMMON
85 /* push class methods */
86 PF_CASE(APPEND, luaH_entry_append)
87 PF_CASE(INSERT, luaH_entry_insert)
88 PF_CASE(GET_POSITION, luaH_entry_get_position)
89 PF_CASE(SET_POSITION, luaH_entry_set_position)
90 /* push string properties */
91 PS_CASE(TEXT, gtk_entry_get_text(GTK_ENTRY(w->widget)))
92 PS_CASE(FG, g_object_get_data(G_OBJECT(w->widget), "fg"))
93 PS_CASE(BG, g_object_get_data(G_OBJECT(w->widget), "bg"))
94 PS_CASE(FONT, g_object_get_data(G_OBJECT(w->widget), "font"))
95 /* push boolean properties */
96 PB_CASE(SHOW_FRAME, gtk_entry_get_has_frame(GTK_ENTRY(w->widget)))
98 default:
99 warn("unknown property: %s", luaL_checkstring(L, 2));
100 break;
102 return 0;
105 static gint
106 luaH_entry_newindex(lua_State *L, luakit_token_t token)
108 size_t len;
109 widget_t *w = luaH_checkudata(L, 1, &widget_class);
110 const gchar *tmp;
111 GdkColor c;
112 PangoFontDescription *font;
114 switch(token)
116 case L_TK_TEXT:
117 gtk_entry_set_text(GTK_ENTRY(w->widget),
118 luaL_checklstring(L, 3, &len));
119 break;
121 case L_TK_FG:
122 case L_TK_BG:
123 tmp = luaL_checklstring(L, 3, &len);
124 if (!gdk_color_parse(tmp, &c))
125 luaL_argerror(L, 3, "unable to parse color");
126 if (token == L_TK_FG) {
127 gtk_widget_modify_text(GTK_WIDGET(w->widget), GTK_STATE_NORMAL, &c);
128 g_object_set_data_full(G_OBJECT(w->widget), "fg", g_strdup(tmp), g_free);
129 } else {
130 gtk_widget_modify_base(GTK_WIDGET(w->widget), GTK_STATE_NORMAL, &c);
131 g_object_set_data_full(G_OBJECT(w->widget), "bg", g_strdup(tmp), g_free);
133 break;
135 case L_TK_SHOW_FRAME:
136 gtk_entry_set_has_frame(GTK_ENTRY(w->widget), luaH_checkboolean(L, 3));
137 break;
139 case L_TK_FONT:
140 tmp = luaL_checklstring(L, 3, &len);
141 font = pango_font_description_from_string(tmp);
142 gtk_widget_modify_font(GTK_WIDGET(w->widget), font);
143 g_object_set_data_full(G_OBJECT(w->widget), "font", g_strdup(tmp), g_free);
144 break;
146 default:
147 warn("unknown property: %s", luaL_checkstring(L, 2));
148 return 0;
150 return luaH_object_emit_property_signal(L, 1);
153 static void
154 activate_cb(GtkEntry *e, widget_t *w)
156 (void) e;
157 lua_State *L = globalconf.L;
158 luaH_object_push(L, w->ref);
159 luaH_object_emit_signal(L, -1, "activate", 0, 0);
160 lua_pop(L, 1);
163 static void
164 changed_cb(GtkEditable *e, widget_t *w)
166 (void) e;
167 lua_State *L = globalconf.L;
168 luaH_object_push(L, w->ref);
169 luaH_object_emit_signal(L, -1, "changed", 0, 0);
170 lua_pop(L, 1);
173 widget_t *
174 widget_entry(widget_t *w)
176 w->index = luaH_entry_index;
177 w->newindex = luaH_entry_newindex;
178 w->destructor = widget_destructor;
180 /* create gtk label widget as main widget */
181 w->widget = gtk_entry_new();
182 g_object_set_data(G_OBJECT(w->widget), "widget", (gpointer) w);
184 /* setup default settings */
185 gtk_entry_set_inner_border(GTK_ENTRY(w->widget), NULL);
187 g_object_connect((GObject*)w->widget,
188 "signal::activate", (GCallback)activate_cb, w,
189 "signal::changed", (GCallback)changed_cb, w,
190 "signal::focus-in-event", (GCallback)focus_cb, w,
191 "signal::focus-out-event", (GCallback)focus_cb, w,
192 "signal::key-press-event", (GCallback)key_press_cb, w,
193 "signal::parent-set", (GCallback)parent_set_cb, w,
194 NULL);
196 gtk_widget_show(w->widget);
197 return w;
200 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80