1 /*
2 * GNT - The GLib Ncurses Toolkit
3 *
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.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "gntinternal.h"
24 #include "gntlabel.h"
25 #include "gntutils.h"
26
27 #include <string.h>
28
29 enum
30 {
31 PROP_0,
32 PROP_TEXT,
33 PROP_TEXT_FLAG
34 };
35
36 enum
37 {
38 SIGS = 1,
39 };
40
41 static GntWidgetClass *parent_class = NULL;
42
43 static void
gnt_label_destroy(GntWidget * widget)44 gnt_label_destroy(GntWidget *widget)
45 {
46 GntLabel *label = GNT_LABEL(widget);
47 g_free(label->text);
48 }
49
50 static void
gnt_label_draw(GntWidget * widget)51 gnt_label_draw(GntWidget *widget)
52 {
53 GntLabel *label = GNT_LABEL(widget);
54 chtype flag = gnt_text_format_flag_to_chtype(label->flags);
55
56 wbkgdset(widget->window, '\0' | flag);
57 mvwaddstr(widget->window, 0, 0, C_(label->text));
58
59 GNTDEBUG;
60 }
61
62 static void
gnt_label_size_request(GntWidget * widget)63 gnt_label_size_request(GntWidget *widget)
64 {
65 GntLabel *label = GNT_LABEL(widget);
66
67 gnt_util_get_text_bound(label->text,
68 &widget->priv.width, &widget->priv.height);
69 }
70
71 static void
gnt_label_set_property(GObject * obj,guint prop_id,const GValue * value,GParamSpec * spec)72 gnt_label_set_property(GObject *obj, guint prop_id, const GValue *value,
73 GParamSpec *spec)
74 {
75 GntLabel *label = GNT_LABEL(obj);
76 switch (prop_id) {
77 case PROP_TEXT:
78 g_free(label->text);
79 label->text = gnt_util_onscreen_fit_string(g_value_get_string(value), -1);
80 break;
81 case PROP_TEXT_FLAG:
82 label->flags = g_value_get_int(value);
83 break;
84 default:
85 g_return_if_reached();
86 break;
87 }
88 }
89
90 static void
gnt_label_get_property(GObject * obj,guint prop_id,GValue * value,GParamSpec * spec)91 gnt_label_get_property(GObject *obj, guint prop_id, GValue *value,
92 GParamSpec *spec)
93 {
94 GntLabel *label = GNT_LABEL(obj);
95 switch (prop_id) {
96 case PROP_TEXT:
97 g_value_set_string(value, label->text);
98 break;
99 case PROP_TEXT_FLAG:
100 g_value_set_int(value, label->flags);
101 break;
102 default:
103 break;
104 }
105 }
106
107 static void
gnt_label_class_init(GntLabelClass * klass)108 gnt_label_class_init(GntLabelClass *klass)
109 {
110 GObjectClass *gclass = G_OBJECT_CLASS(klass);
111
112 parent_class = GNT_WIDGET_CLASS(klass);
113 parent_class->destroy = gnt_label_destroy;
114 parent_class->draw = gnt_label_draw;
115 parent_class->map = NULL;
116 parent_class->size_request = gnt_label_size_request;
117
118 gclass->set_property = gnt_label_set_property;
119 gclass->get_property = gnt_label_get_property;
120
121 g_object_class_install_property(gclass,
122 PROP_TEXT,
123 g_param_spec_string("text", "Text",
124 "The text for the label.",
125 NULL,
126 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
127 )
128 );
129
130 g_object_class_install_property(gclass,
131 PROP_TEXT_FLAG,
132 g_param_spec_int("text-flag", "Text flag",
133 "Text attribute to use when displaying the text in the label.",
134 GNT_TEXT_FLAG_NORMAL,
135 GNT_TEXT_FLAG_NORMAL|GNT_TEXT_FLAG_BOLD|GNT_TEXT_FLAG_UNDERLINE|
136 GNT_TEXT_FLAG_BLINK|GNT_TEXT_FLAG_DIM|GNT_TEXT_FLAG_HIGHLIGHT,
137 GNT_TEXT_FLAG_NORMAL,
138 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
139 )
140 );
141 GNTDEBUG;
142 }
143
144 static void
gnt_label_init(GTypeInstance * instance,gpointer class)145 gnt_label_init(GTypeInstance *instance, gpointer class)
146 {
147 GntWidget *widget = GNT_WIDGET(instance);
148 gnt_widget_set_take_focus(widget, FALSE);
149 gnt_widget_set_has_border(widget, FALSE);
150 gnt_widget_set_has_shadow(widget, FALSE);
151 gnt_widget_set_grow_x(widget, TRUE);
152 widget->priv.minw = 3;
153 widget->priv.minh = 1;
154 GNTDEBUG;
155 }
156
157 /******************************************************************************
158 * GntLabel API
159 *****************************************************************************/
160 GType
gnt_label_get_gtype(void)161 gnt_label_get_gtype(void)
162 {
163 static GType type = 0;
164
165 if(type == 0)
166 {
167 static const GTypeInfo info = {
168 sizeof(GntLabelClass),
169 NULL, /* base_init */
170 NULL, /* base_finalize */
171 (GClassInitFunc)gnt_label_class_init,
172 NULL, /* class_finalize */
173 NULL, /* class_data */
174 sizeof(GntLabel),
175 0, /* n_preallocs */
176 gnt_label_init, /* instance_init */
177 NULL /* value_table */
178 };
179
180 type = g_type_register_static(GNT_TYPE_WIDGET,
181 "GntLabel",
182 &info, 0);
183 }
184
185 return type;
186 }
187
gnt_label_new(const char * text)188 GntWidget *gnt_label_new(const char *text)
189 {
190 return gnt_label_new_with_format(text, 0);
191 }
192
gnt_label_new_with_format(const char * text,GntTextFormatFlags flags)193 GntWidget *gnt_label_new_with_format(const char *text, GntTextFormatFlags flags)
194 {
195 GntWidget *widget = g_object_new(GNT_TYPE_LABEL, "text-flag", flags, "text", text, NULL);
196 return widget;
197 }
198
gnt_label_set_text(GntLabel * label,const char * text)199 void gnt_label_set_text(GntLabel *label, const char *text)
200 {
201 g_object_set(label, "text", text, NULL);
202
203 if (GNT_WIDGET(label)->window)
204 {
205 werase(GNT_WIDGET(label)->window);
206 gnt_widget_draw(GNT_WIDGET(label));
207 }
208 }
209
210