Update.
[gnt.git] / gntlabel.c
blob5a256c9ecb8c1a19dbf2eea0db0d96b53bd7b08d
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, GNT_TEXT_FLAG_HIGHLIGHT, GNT_TEXT_FLAG_NORMAL,
134 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
137 GNTDEBUG;
140 static void
141 gnt_label_init(GTypeInstance *instance, gpointer class)
143 GntWidget *widget = GNT_WIDGET(instance);
144 gnt_widget_set_take_focus(widget, FALSE);
145 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
146 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X);
147 widget->priv.minw = 3;
148 widget->priv.minh = 1;
149 GNTDEBUG;
152 /******************************************************************************
153 * GntLabel API
154 *****************************************************************************/
155 GType
156 gnt_label_get_gtype(void)
158 static GType type = 0;
160 if(type == 0)
162 static const GTypeInfo info = {
163 sizeof(GntLabelClass),
164 NULL, /* base_init */
165 NULL, /* base_finalize */
166 (GClassInitFunc)gnt_label_class_init,
167 NULL, /* class_finalize */
168 NULL, /* class_data */
169 sizeof(GntLabel),
170 0, /* n_preallocs */
171 gnt_label_init, /* instance_init */
172 NULL /* value_table */
175 type = g_type_register_static(GNT_TYPE_WIDGET,
176 "GntLabel",
177 &info, 0);
180 return type;
183 GntWidget *gnt_label_new(const char *text)
185 return gnt_label_new_with_format(text, 0);
188 GntWidget *gnt_label_new_with_format(const char *text, GntTextFormatFlags flags)
190 GntWidget *widget = g_object_new(GNT_TYPE_LABEL, "text-flag", flags, "text", text, NULL);
191 return widget;
194 void gnt_label_set_text(GntLabel *label, const char *text)
196 g_object_set(label, "text", text, NULL);
198 if (GNT_WIDGET(label)->window)
200 werase(GNT_WIDGET(label)->window);
201 gnt_widget_draw(GNT_WIDGET(label));