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
);
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
)))
99 warn("unknown property: %s", luaL_checkstring(L
, 2));
106 luaH_entry_newindex(lua_State
*L
, luakit_token_t token
)
109 widget_t
*w
= luaH_checkudata(L
, 1, &widget_class
);
112 PangoFontDescription
*font
;
117 gtk_entry_set_text(GTK_ENTRY(w
->widget
),
118 luaL_checklstring(L
, 3, &len
));
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
);
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
);
135 case L_TK_SHOW_FRAME
:
136 gtk_entry_set_has_frame(GTK_ENTRY(w
->widget
), luaH_checkboolean(L
, 3));
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
);
147 warn("unknown property: %s", luaL_checkstring(L
, 2));
150 return luaH_object_emit_property_signal(L
, 1);
154 activate_cb(GtkEntry
*e
, widget_t
*w
)
157 lua_State
*L
= globalconf
.L
;
158 luaH_object_push(L
, w
->ref
);
159 luaH_object_emit_signal(L
, -1, "activate", 0, 0);
164 changed_cb(GtkEditable
*e
, widget_t
*w
)
167 lua_State
*L
= globalconf
.L
;
168 luaH_object_push(L
, w
->ref
);
169 luaH_object_emit_signal(L
, -1, "changed", 0, 0);
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
,
196 gtk_widget_show(w
->widget
);
200 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80