[go: up one dir, main page]

File: gntlabel.c

package info (click to toggle)
libgnt 2.14.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 976 kB
  • sloc: ansic: 14,752; xml: 117; makefile: 10
file content (209 lines) | stat: -rw-r--r-- 5,125 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * GNT - The GLib Ncurses Toolkit
 *
 * GNT is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "gntinternal.h"
#include "gntlabel.h"
#include "gntutils.h"

#include <string.h>

enum
{
	PROP_0,
	PROP_TEXT,
	PROP_TEXT_FLAG
};

enum
{
	SIGS = 1,
};

static GntWidgetClass *parent_class = NULL;

static void
gnt_label_destroy(GntWidget *widget)
{
	GntLabel *label = GNT_LABEL(widget);
	g_free(label->text);
}

static void
gnt_label_draw(GntWidget *widget)
{
	GntLabel *label = GNT_LABEL(widget);
	chtype flag = gnt_text_format_flag_to_chtype(label->flags);

	wbkgdset(widget->window, '\0' | flag);
	mvwaddstr(widget->window, 0, 0, C_(label->text));

	GNTDEBUG;
}

static void
gnt_label_size_request(GntWidget *widget)
{
	GntLabel *label = GNT_LABEL(widget);

	gnt_util_get_text_bound(label->text,
			&widget->priv.width, &widget->priv.height);
}

static void
gnt_label_set_property(GObject *obj, guint prop_id, const GValue *value,
		GParamSpec *spec)
{
	GntLabel *label = GNT_LABEL(obj);
	switch (prop_id) {
		case PROP_TEXT:
			g_free(label->text);
			label->text = gnt_util_onscreen_fit_string(g_value_get_string(value), -1);
			break;
		case PROP_TEXT_FLAG:
			label->flags = g_value_get_int(value);
			break;
		default:
			g_return_if_reached();
			break;
	}
}

static void
gnt_label_get_property(GObject *obj, guint prop_id, GValue *value,
		GParamSpec *spec)
{
	GntLabel *label = GNT_LABEL(obj);
	switch (prop_id) {
		case PROP_TEXT:
			g_value_set_string(value, label->text);
			break;
		case PROP_TEXT_FLAG:
			g_value_set_int(value, label->flags);
			break;
		default:
			break;
	}
}

static void
gnt_label_class_init(GntLabelClass *klass)
{
	GObjectClass *gclass = G_OBJECT_CLASS(klass);

	parent_class = GNT_WIDGET_CLASS(klass);
	parent_class->destroy = gnt_label_destroy;
	parent_class->draw = gnt_label_draw;
	parent_class->map = NULL;
	parent_class->size_request = gnt_label_size_request;

	gclass->set_property = gnt_label_set_property;
	gclass->get_property = gnt_label_get_property;

	g_object_class_install_property(gclass,
			PROP_TEXT,
			g_param_spec_string("text", "Text",
				"The text for the label.",
				NULL,
				G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
			)
		);

	g_object_class_install_property(gclass,
			PROP_TEXT_FLAG,
			g_param_spec_int("text-flag", "Text flag",
				"Text attribute to use when displaying the text in the label.",
				GNT_TEXT_FLAG_NORMAL,
				GNT_TEXT_FLAG_NORMAL|GNT_TEXT_FLAG_BOLD|GNT_TEXT_FLAG_UNDERLINE|
				GNT_TEXT_FLAG_BLINK|GNT_TEXT_FLAG_DIM|GNT_TEXT_FLAG_HIGHLIGHT,
				GNT_TEXT_FLAG_NORMAL,
				G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
			)
		);
	GNTDEBUG;
}

static void
gnt_label_init(GTypeInstance *instance, gpointer class)
{
	GntWidget *widget = GNT_WIDGET(instance);
	gnt_widget_set_take_focus(widget, FALSE);
	gnt_widget_set_has_border(widget, FALSE);
	gnt_widget_set_has_shadow(widget, FALSE);
	gnt_widget_set_grow_x(widget, TRUE);
	widget->priv.minw = 3;
	widget->priv.minh = 1;
	GNTDEBUG;
}

/******************************************************************************
 * GntLabel API
 *****************************************************************************/
GType
gnt_label_get_gtype(void)
{
	static GType type = 0;

	if(type == 0)
	{
		static const GTypeInfo info = {
			sizeof(GntLabelClass),
			NULL,					/* base_init		*/
			NULL,					/* base_finalize	*/
			(GClassInitFunc)gnt_label_class_init,
			NULL,					/* class_finalize	*/
			NULL,					/* class_data		*/
			sizeof(GntLabel),
			0,						/* n_preallocs		*/
			gnt_label_init,			/* instance_init	*/
			NULL					/* value_table		*/
		};

		type = g_type_register_static(GNT_TYPE_WIDGET,
									  "GntLabel",
									  &info, 0);
	}

	return type;
}

GntWidget *gnt_label_new(const char *text)
{
	return gnt_label_new_with_format(text, 0);
}

GntWidget *gnt_label_new_with_format(const char *text, GntTextFormatFlags flags)
{
	GntWidget *widget = g_object_new(GNT_TYPE_LABEL, "text-flag", flags, "text", text, NULL);
	return widget;
}

void gnt_label_set_text(GntLabel *label, const char *text)
{
	g_object_set(label, "text", text, NULL);

	if (GNT_WIDGET(label)->window)
	{
		werase(GNT_WIDGET(label)->window);
		gnt_widget_draw(GNT_WIDGET(label));
	}
}