Add P{B,F,N,S}_CASE & LUAKIT_WIDGET_{,BIN_}INDEX_COMMON macros
[luakit.git] / widgets / label.c
blob89c5fba1e0096c0729435e8eb3741e3393b1353e
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 LUAKIT_WIDGET_INDEX_COMMON
76 /* push class methods */
77 PF_CASE(GET_ALIGNMENT, luaH_label_get_alignment);
78 PF_CASE(GET_PADDING, luaH_label_get_padding);
79 PF_CASE(SET_ALIGNMENT, luaH_label_set_alignment);
80 PF_CASE(SET_PADDING, luaH_label_set_padding);
81 /* push string properties */
82 PS_CASE(FG, g_object_get_data(G_OBJECT(w->widget), "fg"))
83 PS_CASE(FONT, g_object_get_data(G_OBJECT(w->widget), "font"))
84 PS_CASE(TEXT, gtk_label_get_label(GTK_LABEL(w->widget)))
85 /* push boolean properties */
86 PB_CASE(SELECTABLE, gtk_label_get_selectable(GTK_LABEL(w->widget)))
88 default:
89 warn("unknown property: %s", luaL_checkstring(L, 2));
90 break;
92 return 0;
95 static gint
96 luaH_label_newindex(lua_State *L, luakit_token_t token)
98 size_t len;
99 widget_t *w = luaH_checkudata(L, 1, &widget_class);
100 const gchar *tmp;
101 GdkColor c;
102 PangoFontDescription *font;
104 switch(token)
106 case L_TK_TEXT:
107 gtk_label_set_markup(GTK_LABEL(w->widget),
108 luaL_checklstring(L, 3, &len));
109 break;
111 case L_TK_FG:
112 tmp = luaL_checklstring(L, 3, &len);
113 if (!gdk_color_parse(tmp, &c)) {
114 warn("invalid color: %s", tmp);
115 return 0;
118 gtk_widget_modify_fg(GTK_WIDGET(w->widget), GTK_STATE_NORMAL, &c);
119 g_object_set_data_full(G_OBJECT(w->widget), "fg", g_strdup(tmp), g_free);
120 break;
122 case L_TK_FONT:
123 tmp = luaL_checklstring(L, 3, &len);
124 font = pango_font_description_from_string(tmp);
125 gtk_widget_modify_font(GTK_WIDGET(w->widget), font);
126 g_object_set_data_full(G_OBJECT(w->widget), "font", g_strdup(tmp), g_free);
127 break;
129 case L_TK_SELECTABLE:
130 gtk_label_set_selectable(GTK_LABEL(w->widget), luaH_checkboolean(L, 3));
131 break;
133 default:
134 warn("unknown property: %s", luaL_checkstring(L, 2));
135 return 0;
138 return luaH_object_emit_property_signal(L, 1);
141 widget_t *
142 widget_label(widget_t *w)
144 w->index = luaH_label_index;
145 w->newindex = luaH_label_newindex;
146 w->destructor = widget_destructor;
148 /* create gtk label widget as main widget */
149 w->widget = gtk_label_new(NULL);
150 g_object_set_data(G_OBJECT(w->widget), "widget", (gpointer) w);
152 /* setup default settings */
153 gtk_label_set_selectable(GTK_LABEL(w->widget), FALSE);
154 gtk_label_set_use_markup(GTK_LABEL(w->widget), TRUE);
155 gtk_misc_set_alignment(GTK_MISC(w->widget), 0, 0);
156 gtk_misc_set_padding(GTK_MISC(w->widget), 2, 2);
158 g_object_connect((GObject*)w->widget,
159 "signal::focus-in-event", (GCallback)focus_cb, w,
160 "signal::focus-out-event", (GCallback)focus_cb, w,
161 "signal::key-press-event", (GCallback)key_press_cb, w,
162 "signal::parent-set", (GCallback)parent_set_cb, w,
163 NULL);
165 gtk_widget_show(w->widget);
166 return w;
169 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80