summaryrefslogtreecommitdiff
path: root/tui/newt/nmt-newt-button.c
blob: 97ad50ca67217aad7d1a024c3da4bd7dc7812a17 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This program 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, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2013 Red Hat, Inc.
 */

/**
 * SECTION:nmt-newt-button
 * @short_description: Push buttons
 *
 * #NmtNewtButton implements a button widget.
 */

#include "config.h"

#include "nmt-newt-button.h"
#include "nmt-newt-utils.h"

G_DEFINE_TYPE (NmtNewtButton, nmt_newt_button, NMT_TYPE_NEWT_COMPONENT)

#define NMT_NEWT_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NMT_TYPE_NEWT_BUTTON, NmtNewtButtonPrivate))

typedef struct {
	char *label;
} NmtNewtButtonPrivate;

enum {
	PROP_0,
	PROP_LABEL,

	LAST_PROP
};

enum {
	CLICKED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

/**
 * nmt_newt_button_new:
 * @label: the (initial) button label
 *
 * Creates a new button.
 *
 * Returns: a new #NmtNewtButton
 */
NmtNewtWidget *
nmt_newt_button_new (const char *label)
{
	return g_object_new (NMT_TYPE_NEWT_BUTTON,
	                     "label", label,
	                     NULL);
}

/**
 * nmt_newt_button_set_label:
 * @button: an #NmtNewtButton
 * @label: the new label
 *
 * Updates @button's label.
 */
void
nmt_newt_button_set_label (NmtNewtButton *button,
                           const char    *label)
{
	NmtNewtButtonPrivate *priv = NMT_NEWT_BUTTON_GET_PRIVATE (button);

	if (!g_strcmp0 (priv->label, label))
		return;

	g_free (priv->label);
	priv->label = g_strdup (label);
	nmt_newt_widget_needs_rebuild (NMT_NEWT_WIDGET (button));
}

/**
 * nmt_newt_button_get_label:
 * @button: an #NmtNewtButton
 *
 * Gets @button's label.
 *
 * Returns: @button's label.
 */
const char *
nmt_newt_button_get_label (NmtNewtButton *button)
{
	NmtNewtButtonPrivate *priv = NMT_NEWT_BUTTON_GET_PRIVATE (button);

	return priv->label;
}

static void
nmt_newt_button_init (NmtNewtButton *button)
{
}

static void
nmt_newt_button_finalize (GObject *object)
{
	NmtNewtButtonPrivate *priv = NMT_NEWT_BUTTON_GET_PRIVATE (object);

	g_free (priv->label);

	G_OBJECT_CLASS (nmt_newt_button_parent_class)->finalize (object);
}

static newtComponent
nmt_newt_button_build_component (NmtNewtComponent *component,
                                 gboolean          sensitive)
{
	NmtNewtButtonPrivate *priv = NMT_NEWT_BUTTON_GET_PRIVATE (component);
	newtComponent co;
	char *label = NULL, *label_lc;

	if (sensitive) {
		label_lc = nmt_newt_locale_from_utf8 (priv->label);
		co = newtCompactButton (-1, -1, label_lc);
		g_free (label_lc);
	} else {
		label = g_strdup_printf (" <%s>", priv->label);
		label_lc = nmt_newt_locale_from_utf8 (label);
		co = newtLabel (-1, -1, label_lc);
		g_free (label_lc);
		newtLabelSetColors (co, NMT_NEWT_COLORSET_DISABLED_BUTTON);
	}

	return co;
}

static void
nmt_newt_button_size_request (NmtNewtWidget *widget,
                              int           *width,
                              int           *height)
{
	NMT_NEWT_WIDGET_CLASS (nmt_newt_button_parent_class)->size_request (widget, width, height);

	/* remove the automatically-added left padding */
	(*width)--;
}

static void
nmt_newt_button_size_allocate (NmtNewtWidget *widget,
                               int            x,
                               int            y,
                               int            width,
                               int            height)
{
	/* account for the automatically-added left padding */
	x--;
	width++;

	NMT_NEWT_WIDGET_CLASS (nmt_newt_button_parent_class)->size_allocate (widget, x, y, width, height);
}

static void
nmt_newt_button_activated (NmtNewtWidget *widget)
{
	g_signal_emit (widget, signals[CLICKED], 0);

	NMT_NEWT_WIDGET_CLASS (nmt_newt_button_parent_class)->activated (widget);
}

static void
nmt_newt_button_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
	switch (prop_id) {
	case PROP_LABEL:
		nmt_newt_button_set_label (NMT_NEWT_BUTTON (object),
		                           g_value_get_string (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nmt_newt_button_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
	NmtNewtButtonPrivate *priv = NMT_NEWT_BUTTON_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_LABEL:
		g_value_set_string (value, priv->label);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nmt_newt_button_class_init (NmtNewtButtonClass *button_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (button_class);
	NmtNewtComponentClass *component_class = NMT_NEWT_COMPONENT_CLASS (button_class);
	NmtNewtWidgetClass *widget_class = NMT_NEWT_WIDGET_CLASS (button_class);

	g_type_class_add_private (button_class, sizeof (NmtNewtButtonPrivate));

	/* virtual methods */
	object_class->set_property = nmt_newt_button_set_property;
	object_class->get_property = nmt_newt_button_get_property;
	object_class->finalize     = nmt_newt_button_finalize;

	widget_class->size_request  = nmt_newt_button_size_request;
	widget_class->size_allocate = nmt_newt_button_size_allocate;
	widget_class->activated     = nmt_newt_button_activated;

	component_class->build_component = nmt_newt_button_build_component;

	/* signals */

	/**
	 * NmtNewtButton::clicked:
	 * @button: the #NmtNewtButton
	 *
	 * Emitted when the button is clicked.
	 */
	signals[CLICKED] =
		g_signal_new ("clicked",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_FIRST,
		              0, NULL, NULL, NULL,
		              G_TYPE_NONE, 0);

	/* properties */

	/**
	 * NmtNewtButton:label:
	 *
	 * The button's label
	 */
	g_object_class_install_property (object_class, PROP_LABEL,
	                                 g_param_spec_string ("label", "", "",
	                                                      NULL,
	                                                      G_PARAM_READWRITE |
	                                                      G_PARAM_STATIC_STRINGS));
}