Update to 24f58c58bb8d22c0e8e6c5ce43c536c47b719bc6
[gnt.git] / gntlabel.c
blob4fbfae643605a3dffb842e5ca82efcfc8ffe7809
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "gntlabel.h"
24 #include "gntutils.h"
26 #include <string.h>
28 enum
30 PROP_0,
31 PROP_TEXT,
32 PROP_TEXT_FLAG
35 enum
37 SIGS = 1,
40 static GntWidgetClass *parent_class = NULL;
42 static void
43 gnt_label_destroy(GntWidget *widget)
45 GntLabel *label = GNT_LABEL(widget);
46 g_free(label->text);
49 static void
50 gnt_label_draw(GntWidget *widget)
52 GntLabel *label = GNT_LABEL(widget);
53 chtype flag = gnt_text_format_flag_to_chtype(label->flags);
55 wbkgdset(widget->window, '\0' | flag);
56 mvwaddstr(widget->window, 0, 0, label->text);
58 GNTDEBUG;
61 static void
62 gnt_label_size_request(GntWidget *widget)
64 GntLabel *label = GNT_LABEL(widget);
66 gnt_util_get_text_bound(label->text,
67 &widget->priv.width, &widget->priv.height);
70 static void
71 gnt_label_set_property(GObject *obj, guint prop_id, const GValue *value,
72 GParamSpec *spec)
74 GntLabel *label = GNT_LABEL(obj);
75 switch (prop_id) {
76 case PROP_TEXT:
77 g_free(label->text);
78 label->text = gnt_util_onscreen_fit_string(g_value_get_string(value), -1);
79 break;
80 case PROP_TEXT_FLAG:
81 label->flags = g_value_get_int(value);
82 break;
83 default:
84 g_return_if_reached();
85 break;
89 static void
90 gnt_label_get_property(GObject *obj, guint prop_id, GValue *value,
91 GParamSpec *spec)
93 GntLabel *label = GNT_LABEL(obj);
94 switch (prop_id) {
95 case PROP_TEXT:
96 g_value_set_string(value, label->text);
97 break;
98 case PROP_TEXT_FLAG:
99 g_value_set_int(value, label->flags);
100 break;
101 default:
102 break;
106 static void
107 gnt_label_class_init(GntLabelClass *klass)
109 GObjectClass *gclass = G_OBJECT_CLASS(klass);
111 parent_class = GNT_WIDGET_CLASS(klass);
112 parent_class->destroy = gnt_label_destroy;
113 parent_class->draw = gnt_label_draw;
114 parent_class->map = NULL;
115 parent_class->size_request = gnt_label_size_request;
117 gclass->set_property = gnt_label_set_property;
118 gclass->get_property = gnt_label_get_property;
120 g_object_class_install_property(gclass,
121 PROP_TEXT,
122 g_param_spec_string("text", "Text",
123 "The text for the label.",
124 NULL,
125 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
129 g_object_class_install_property(gclass,
130 PROP_TEXT_FLAG,
131 g_param_spec_int("text-flag", "Text flag",
132 "Text attribute to use when displaying the text in the label.",
133 GNT_TEXT_FLAG_NORMAL,
134 GNT_TEXT_FLAG_NORMAL|GNT_TEXT_FLAG_BOLD|GNT_TEXT_FLAG_UNDERLINE|
135 GNT_TEXT_FLAG_BLINK|GNT_TEXT_FLAG_DIM|GNT_TEXT_FLAG_HIGHLIGHT,
136 GNT_TEXT_FLAG_NORMAL,
137 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
140 GNTDEBUG;
143 static void
144 gnt_label_init(GTypeInstance *instance, gpointer class)
146 GntWidget *widget = GNT_WIDGET(instance);
147 gnt_widget_set_take_focus(widget, FALSE);
148 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
149 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X);
150 widget->priv.minw = 3;
151 widget->priv.minh = 1;
152 GNTDEBUG;
155 /******************************************************************************
156 * GntLabel API
157 *****************************************************************************/
158 GType
159 gnt_label_get_gtype(void)
161 static GType type = 0;
163 if(type == 0)
165 static const GTypeInfo info = {
166 sizeof(GntLabelClass),
167 NULL, /* base_init */
168 NULL, /* base_finalize */
169 (GClassInitFunc)gnt_label_class_init,
170 NULL, /* class_finalize */
171 NULL, /* class_data */
172 sizeof(GntLabel),
173 0, /* n_preallocs */
174 gnt_label_init, /* instance_init */
175 NULL /* value_table */
178 type = g_type_register_static(GNT_TYPE_WIDGET,
179 "GntLabel",
180 &info, 0);
183 return type;
186 GntWidget *gnt_label_new(const char *text)
188 return gnt_label_new_with_format(text, 0);
191 GntWidget *gnt_label_new_with_format(const char *text, GntTextFormatFlags flags)
193 GntWidget *widget = g_object_new(GNT_TYPE_LABEL, "text-flag", flags, "text", text, NULL);
194 return widget;
197 void gnt_label_set_text(GntLabel *label, const char *text)
199 g_object_set(label, "text", text, NULL);
201 if (GNT_WIDGET(label)->window)
203 werase(GNT_WIDGET(label)->window);
204 gnt_widget_draw(GNT_WIDGET(label));