Use util.escape to escape all buffer, prompt, uri & tab label texts
[luakit.git] / widgets / label.c
blob0382a4d3987dc0d90b5b68058d7c1f071087e5d5
1 /*
2 * widgets/label.c - gtk text area widget
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_label_set_alignment(lua_State *L)
28 widget_t *w = luaH_checkudata(L, 1, &widget_class);
29 gfloat xalign = luaL_checknumber(L, 2);
30 gfloat yalign = luaL_checknumber(L, 3);
31 gtk_misc_set_alignment(GTK_MISC(w->widget), xalign, yalign);
32 return 0;
35 static gint
36 luaH_label_get_alignment(lua_State *L)
38 widget_t *w = luaH_checkudata(L, 1, &widget_class);
39 gfloat xalign, yalign;
40 gtk_misc_get_alignment(GTK_MISC(w->widget), &xalign, &yalign);
41 lua_pushnumber(L, xalign);
42 lua_pushnumber(L, yalign);
43 return 2;
46 static gint
47 luaH_label_set_padding(lua_State *L)
49 widget_t *w = luaH_checkudata(L, 1, &widget_class);
50 gint xpad = luaL_checknumber(L, 2);
51 gint ypad = luaL_checknumber(L, 3);
52 gtk_misc_set_padding(GTK_MISC(w->widget), xpad, ypad);
53 return 0;
56 static gint
57 luaH_label_get_padding(lua_State *L)
59 widget_t *w = luaH_checkudata(L, 1, &widget_class);
60 gint xpad, ypad;
61 gtk_misc_get_padding(GTK_MISC(w->widget), &xpad, &ypad);
62 lua_pushnumber(L, xpad);
63 lua_pushnumber(L, ypad);
64 return 2;
67 static gint
68 luaH_label_index(lua_State *L, luakit_token_t token)
70 widget_t *w = luaH_checkudata(L, 1, &widget_class);
72 switch(token)
74 case L_TK_DESTROY:
75 lua_pushcfunction(L, luaH_widget_destroy);
76 return 1;
78 case L_TK_TEXT:
79 lua_pushstring(L, gtk_label_get_label(GTK_LABEL(w->widget)));
80 return 1;
82 case L_TK_SET_ALIGNMENT:
83 lua_pushcfunction(L, luaH_label_set_alignment);
84 return 1;
86 case L_TK_GET_ALIGNMENT:
87 lua_pushcfunction(L, luaH_label_get_alignment);
88 return 1;
90 case L_TK_SET_PADDING:
91 lua_pushcfunction(L, luaH_label_set_padding);
92 return 1;
94 case L_TK_GET_PADDING:
95 lua_pushcfunction(L, luaH_label_get_padding);
96 return 1;
98 case L_TK_FG:
99 lua_pushstring(L, g_object_get_data(G_OBJECT(w->widget), "fg"));
100 return 1;
102 case L_TK_FONT:
103 lua_pushstring(L, g_object_get_data(G_OBJECT(w->widget), "font"));
104 return 1;
106 case L_TK_SHOW:
107 lua_pushcfunction(L, luaH_widget_show);
108 return 1;
110 case L_TK_HIDE:
111 lua_pushcfunction(L, luaH_widget_hide);
112 return 1;
114 case L_TK_SELECTABLE:
115 lua_pushboolean(L, gtk_label_get_selectable(GTK_LABEL(w->widget)));
116 return 1;
118 default:
119 warn("unknown property: %s", luaL_checkstring(L, 2));
120 break;
122 return 0;
125 static gint
126 luaH_label_newindex(lua_State *L, luakit_token_t token)
128 size_t len;
129 widget_t *w = luaH_checkudata(L, 1, &widget_class);
130 const gchar *tmp;
131 GdkColor c;
132 PangoFontDescription *font;
134 switch(token)
136 case L_TK_TEXT:
137 gtk_label_set_markup(GTK_LABEL(w->widget),
138 luaL_checklstring(L, 3, &len));
139 break;
141 case L_TK_FG:
142 tmp = luaL_checklstring(L, 3, &len);
143 if (!gdk_color_parse(tmp, &c)) {
144 warn("invalid color: %s", tmp);
145 return 0;
148 gtk_widget_modify_fg(GTK_WIDGET(w->widget), GTK_STATE_NORMAL, &c);
149 g_object_set_data_full(G_OBJECT(w->widget), "fg", g_strdup(tmp), g_free);
150 break;
152 case L_TK_FONT:
153 tmp = luaL_checklstring(L, 3, &len);
154 font = pango_font_description_from_string(tmp);
155 gtk_widget_modify_font(GTK_WIDGET(w->widget), font);
156 g_object_set_data_full(G_OBJECT(w->widget), "font", g_strdup(tmp), g_free);
157 break;
159 case L_TK_SELECTABLE:
160 gtk_label_set_selectable(GTK_LABEL(w->widget), luaH_checkboolean(L, 3));
161 break;
163 default:
164 warn("unknown property: %s", luaL_checkstring(L, 2));
165 return 0;
168 return luaH_object_emit_property_signal(L, 1);
171 static void
172 label_destructor(widget_t *w)
174 gtk_widget_destroy(w->widget);
177 widget_t *
178 widget_label(widget_t *w)
180 w->index = luaH_label_index;
181 w->newindex = luaH_label_newindex;
182 w->destructor = label_destructor;
184 /* create gtk label widget as main widget */
185 w->widget = gtk_label_new(NULL);
186 g_object_set_data(G_OBJECT(w->widget), "widget", (gpointer) w);
188 /* setup default settings */
189 gtk_label_set_selectable(GTK_LABEL(w->widget), FALSE);
190 gtk_label_set_use_markup(GTK_LABEL(w->widget), TRUE);
191 gtk_misc_set_alignment(GTK_MISC(w->widget), 0, 0);
192 gtk_misc_set_padding(GTK_MISC(w->widget), 2, 2);
194 g_object_connect((GObject*)w->widget,
195 "signal::focus-in-event", (GCallback)focus_cb, w,
196 "signal::focus-out-event", (GCallback)focus_cb, w,
197 "signal::key-press-event", (GCallback)key_press_cb, w,
198 "signal::parent-set", (GCallback)parent_set_cb, w,
199 NULL);
201 gtk_widget_show(w->widget);
202 return w;
205 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80