-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathgimplabeled.c
274 lines (227 loc) · 8.23 KB
/
gimplabeled.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimplabeled.c
* Copyright (C) 2020 Jehan
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "libgimpbase/gimpbase.h"
#include "gimpwidgets.h"
/**
* SECTION: gimplabeled
* @title: GimpLabeled
* @short_description: Widget containing a label as mnemonic for another
* widget.
*
* This widget is a #GtkGrid showing a #GtkLabel used as mnemonic on
* another widget.
**/
enum
{
MNEMONIC_WIDGET_CHANGED,
LAST_SIGNAL
};
enum
{
PROP_0,
PROP_LABEL,
};
typedef struct _GimpLabeledPrivate
{
GtkWidget *label;
} GimpLabeledPrivate;
static void gimp_labeled_constructed (GObject *object);
static void gimp_labeled_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_labeled_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gimp_labeled_real_mnemonic_widget_changed (GimpLabeled *labeled,
GtkWidget *widget);
G_DEFINE_TYPE_WITH_PRIVATE (GimpLabeled, gimp_labeled, GTK_TYPE_GRID)
#define parent_class gimp_labeled_parent_class
static guint signals[LAST_SIGNAL] = { 0 };
static void
gimp_labeled_class_init (GimpLabeledClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = gimp_labeled_constructed;
object_class->set_property = gimp_labeled_set_property;
object_class->get_property = gimp_labeled_get_property;
signals[MNEMONIC_WIDGET_CHANGED] =
g_signal_new ("mnemonic-widget-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpLabeledClass, mnemonic_widget_changed),
NULL, NULL, NULL,
G_TYPE_NONE, 1,
GTK_TYPE_WIDGET);
klass->mnemonic_widget_changed = gimp_labeled_real_mnemonic_widget_changed;
/**
* GimpLabeled:label:
*
* Label text with pango markup and mnemonic.
*
* Since: 3.0
**/
g_object_class_install_property (object_class, PROP_LABEL,
g_param_spec_string ("label",
"Label text",
"The text of the label part of this widget",
NULL,
GIMP_PARAM_READWRITE));
}
static void
gimp_labeled_init (GimpLabeled *labeled)
{
}
static void
gimp_labeled_constructed (GObject *object)
{
GimpLabeledClass *klass;
GimpLabeled *labeled = GIMP_LABELED (object);
GimpLabeledPrivate *priv = gimp_labeled_get_instance_private (labeled);
GtkWidget *mnemonic_widget;
gint x = 0;
gint y = 0;
gint width = 1;
gint height = 1;
G_OBJECT_CLASS (parent_class)->constructed (object);
priv->label = gtk_label_new_with_mnemonic (NULL);
gtk_label_set_xalign (GTK_LABEL (priv->label), 0.0);
klass = GIMP_LABELED_GET_CLASS (labeled);
g_return_if_fail (klass->populate);
mnemonic_widget = klass->populate (labeled, &x, &y, &width, &height);
g_signal_emit (object, signals[MNEMONIC_WIDGET_CHANGED], 0,
mnemonic_widget);
gtk_grid_attach (GTK_GRID (labeled), priv->label, x, y, width, height);
gtk_widget_show (priv->label);
}
static void
gimp_labeled_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GimpLabeled *entry = GIMP_LABELED (object);
GimpLabeledPrivate *priv = gimp_labeled_get_instance_private (entry);
switch (property_id)
{
case PROP_LABEL:
{
/* This should not happen since the property is **not** set with
* G_PARAM_CONSTRUCT, hence the label should exist when the
* property is first set.
*/
g_return_if_fail (priv->label);
gtk_label_set_markup_with_mnemonic (GTK_LABEL (priv->label),
g_value_get_string (value));
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_labeled_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GimpLabeled *entry = GIMP_LABELED (object);
GimpLabeledPrivate *priv = gimp_labeled_get_instance_private (entry);
switch (property_id)
{
case PROP_LABEL:
g_value_set_string (value, gtk_label_get_label (GTK_LABEL (priv->label)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_labeled_real_mnemonic_widget_changed (GimpLabeled *labeled,
GtkWidget *widget)
{
GimpLabeledPrivate *priv = gimp_labeled_get_instance_private (labeled);
gtk_label_set_mnemonic_widget (GTK_LABEL (priv->label), widget);
}
/* Public functions */
/**
* gimp_labeled_get_label:
* @labeled: The #GimpLabeled.
*
* This function returns the #GtkLabel packed in @labeled. This can be
* useful if you need to customize some aspects of the widget.
*
* Returns: (transfer none) (type GtkLabel): The #GtkLabel contained in @labeled.
**/
GtkWidget *
gimp_labeled_get_label (GimpLabeled *labeled)
{
GimpLabeledPrivate *priv = gimp_labeled_get_instance_private (labeled);
g_return_val_if_fail (GIMP_IS_LABELED (labeled), NULL);
return priv->label;
}
/**
* gimp_labeled_get_text:
* @labeled: the #GimpLabeled.
*
* This function will return exactly what you entered with
* gimp_labeled_set_text() or through the "label" property because this
* class expects labels to have mnemonics (and allows Pango formatting).
* To obtain instead the text as displayed with mnemonics and markup
* removed, call:
* |[<!-- language="C" -->
* gtk_label_get_text (GTK_LABEL (gimp_labeled_get_label (@labeled)));
* ]|
*
* Returns: the label text as entered, which includes pango markup and
* mnemonics similarly to gtk_label_get_label().
*/
const gchar *
gimp_labeled_get_text (GimpLabeled *labeled)
{
GimpLabeledPrivate *priv = gimp_labeled_get_instance_private (labeled);
g_return_val_if_fail (GIMP_IS_LABELED (labeled), NULL);
return gtk_label_get_label (GTK_LABEL (priv->label));
}
/**
* gimp_labeled_set_text:
* @labeled: the #GimpLabeled.
* @text: label text with Pango markup and mnemonic.
*
* This is the equivalent of running
* gtk_label_set_markup_with_mnemonic() on the #GtkLabel as a
* #GimpLabeled expects a mnemonic. Pango markup are also allowed.
*/
void
gimp_labeled_set_text (GimpLabeled *labeled,
const gchar *text)
{
GimpLabeledPrivate *priv = gimp_labeled_get_instance_private (labeled);
g_return_if_fail (GIMP_IS_LABELED (labeled));
gtk_label_set_markup_with_mnemonic (GTK_LABEL (priv->label), text);
}