summaryrefslogtreecommitdiff
path: root/plugins/gtk+/glade-label-editor.c
blob: d2334888b0a7881d35fc7c7453194d740462967d (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (C) 2008 Tristan Van Berkom.
 *
 * 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 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Tristan Van Berkom <tvb@gnome.org>
 */

#include <config.h>
#include <gladeui/glade.h>
#include <glib/gi18n-lib.h>
#include <gdk/gdkkeysyms.h>

#include "glade-label-editor.h"

/* GtkWidgetClass */
static void glade_label_editor_grab_focus    (GtkWidget          *widget);

/* GladeEditableInterface */
static void glade_label_editor_load          (GladeEditable      *editable,
                                              GladeWidget        *widget);
static void glade_label_editor_editable_init (GladeEditableInterface *iface);

/* Callbacks */
static void attributes_toggled (GtkWidget *widget, GladeLabelEditor *label_editor);
static void markup_toggled     (GtkWidget *widget, GladeLabelEditor *label_editor);
static void pattern_toggled    (GtkWidget *widget, GladeLabelEditor *label_editor);
static void wrap_free_toggled  (GtkWidget *widget, GladeLabelEditor *label_editor);
static void single_toggled     (GtkWidget *widget, GladeLabelEditor *label_editor);
static void wrap_mode_toggled  (GtkWidget *widget, GladeLabelEditor *label_editor);

struct _GladeLabelEditorPrivate
{
  GtkWidget *embed;

  GtkWidget *attributes_radio;    /* Set pango attributes manually (attributes eprop embedded) */
  GtkWidget *markup_radio;        /* Parse the label as a pango markup string (no showing eprop) */
  GtkWidget *pattern_radio;       /* Use a pattern string to underline portions of the text
                                   * (pattern eprop embedded) */

  /* These control whether to use single-line-mode, wrap & wrap-mode or neither */
  GtkWidget *wrap_free_label; /* Set boldness on this label for a fake property */
  GtkWidget *wrap_free_radio;
  GtkWidget *single_radio;
  GtkWidget *wrap_mode_radio;
};

static GladeEditableInterface *parent_editable_iface;

G_DEFINE_TYPE_WITH_CODE (GladeLabelEditor, glade_label_editor, GLADE_TYPE_EDITOR_SKELETON,
                         G_ADD_PRIVATE (GladeLabelEditor)
                         G_IMPLEMENT_INTERFACE (GLADE_TYPE_EDITABLE,
                                                glade_label_editor_editable_init))

static void
glade_label_editor_class_init (GladeLabelEditorClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  widget_class->grab_focus = glade_label_editor_grab_focus;

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/gladegtk/glade-label-editor.ui");

  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, embed);
  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, attributes_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, markup_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, pattern_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, wrap_free_label);
  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, wrap_free_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, single_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GladeLabelEditor, wrap_mode_radio);

  gtk_widget_class_bind_template_callback (widget_class, attributes_toggled);
  gtk_widget_class_bind_template_callback (widget_class, markup_toggled);
  gtk_widget_class_bind_template_callback (widget_class, pattern_toggled);
  gtk_widget_class_bind_template_callback (widget_class, wrap_free_toggled);
  gtk_widget_class_bind_template_callback (widget_class, single_toggled);
  gtk_widget_class_bind_template_callback (widget_class, wrap_mode_toggled);
}

static void
glade_label_editor_init (GladeLabelEditor *self)
{
  self->priv = glade_label_editor_get_instance_private (self);

  gtk_widget_init_template (GTK_WIDGET (self));
}

static void
glade_label_editor_load (GladeEditable *editable, GladeWidget *widget)
{
  GladeLabelEditor *label_editor = GLADE_LABEL_EDITOR (editable);
  GladeLabelEditorPrivate *priv = label_editor->priv;

  /* Chain up to default implementation */
  parent_editable_iface->load (editable, widget);

  if (widget)
    {
      GladeLabelContentMode content_mode;
      GladeLabelWrapMode wrap_mode;
      static PangoAttrList *italic_attr_list = NULL;
      gboolean use_max_width;

      if (!italic_attr_list)
        {
          PangoAttribute *attr;
          italic_attr_list = pango_attr_list_new ();
          attr = pango_attr_style_new (PANGO_STYLE_ITALIC);
          pango_attr_list_insert (italic_attr_list, attr);
        }

      glade_widget_property_get (widget, "label-content-mode", &content_mode);
      glade_widget_property_get (widget, "label-wrap-mode", &wrap_mode);
      glade_widget_property_get (widget, "use-max-width", &use_max_width);

      switch (content_mode)
        {
          case GLADE_LABEL_MODE_ATTRIBUTES:
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
                                          (priv->attributes_radio),
                                          TRUE);
            break;
          case GLADE_LABEL_MODE_MARKUP:
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
                                          (priv->markup_radio), TRUE);
            break;
          case GLADE_LABEL_MODE_PATTERN:
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
                                          (priv->pattern_radio), TRUE);
            break;
          default:
            break;
        }

      if (wrap_mode == GLADE_LABEL_WRAP_FREE)
        gtk_label_set_attributes (GTK_LABEL (priv->wrap_free_label),
                                  italic_attr_list);
      else
        gtk_label_set_attributes (GTK_LABEL (priv->wrap_free_label),
                                  NULL);

      switch (wrap_mode)
        {
          case GLADE_LABEL_WRAP_FREE:
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
                                          (priv->wrap_free_radio),
                                          TRUE);
            break;
          case GLADE_LABEL_SINGLE_LINE:
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
                                          (priv->single_radio), TRUE);
            break;
          case GLADE_LABEL_WRAP_MODE:
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
                                          (priv->wrap_mode_radio),
                                          TRUE);
            break;
          default:
            break;
        }
    }
}

static void
glade_label_editor_editable_init (GladeEditableInterface *iface)
{
  parent_editable_iface = g_type_interface_peek_parent (iface);

  iface->load = glade_label_editor_load;
}

static void
glade_label_editor_grab_focus (GtkWidget *widget)
{
  GladeLabelEditor        *label_editor = GLADE_LABEL_EDITOR (widget);
  GladeLabelEditorPrivate *priv = label_editor->priv;

  gtk_widget_grab_focus (priv->embed);
}

/**********************************************************************
                    label-content-mode radios
 **********************************************************************/
static void
attributes_toggled (GtkWidget *widget, GladeLabelEditor *label_editor)
{
  GladeLabelEditorPrivate *priv = label_editor->priv;
  GladeProperty *property;
  GladeWidget   *gwidget = glade_editable_loaded_widget (GLADE_EDITABLE (label_editor));

  if (glade_editable_loading (GLADE_EDITABLE (label_editor)) || !gwidget)
    return;

  if (!gtk_toggle_button_get_active
      (GTK_TOGGLE_BUTTON (priv->attributes_radio)))
    return;

  glade_editable_block (GLADE_EDITABLE (label_editor));

  glade_command_push_group (_("Setting %s to use an attribute list"),
                            glade_widget_get_name (gwidget));

  property = glade_widget_get_property (gwidget, "use-markup");
  glade_command_set_property (property, FALSE);
  property = glade_widget_get_property (gwidget, "pattern");
  glade_command_set_property (property, NULL);
  property = glade_widget_get_property (gwidget, "label-content-mode");
  glade_command_set_property (property, GLADE_LABEL_MODE_ATTRIBUTES);

  glade_command_pop_group ();

  glade_editable_unblock (GLADE_EDITABLE (label_editor));

  /* reload buttons and sensitivity and stuff... */
  glade_editable_load (GLADE_EDITABLE (label_editor), gwidget);
}

static void
markup_toggled (GtkWidget *widget, GladeLabelEditor *label_editor)
{
  GladeLabelEditorPrivate *priv = label_editor->priv;
  GladeProperty *property;
  GladeWidget   *gwidget = glade_editable_loaded_widget (GLADE_EDITABLE (label_editor));

  if (glade_editable_loading (GLADE_EDITABLE (label_editor)) || !gwidget)
    return;

  if (!gtk_toggle_button_get_active
      (GTK_TOGGLE_BUTTON (priv->markup_radio)))
    return;

  glade_editable_block (GLADE_EDITABLE (label_editor));

  glade_command_push_group (_("Setting %s to use a Pango markup string"),
                            glade_widget_get_name (gwidget));

  property = glade_widget_get_property (gwidget, "pattern");
  glade_command_set_property (property, NULL);
  property = glade_widget_get_property (gwidget, "glade-attributes");
  glade_command_set_property (property, NULL);

  property = glade_widget_get_property (gwidget, "use-markup");
  glade_command_set_property (property, TRUE);
  property = glade_widget_get_property (gwidget, "label-content-mode");
  glade_command_set_property (property, GLADE_LABEL_MODE_MARKUP);

  glade_command_pop_group ();

  glade_editable_unblock (GLADE_EDITABLE (label_editor));

  /* reload buttons and sensitivity and stuff... */
  glade_editable_load (GLADE_EDITABLE (label_editor), gwidget);
}

static void
pattern_toggled (GtkWidget *widget, GladeLabelEditor *label_editor)
{
  GladeLabelEditorPrivate *priv = label_editor->priv;
  GladeProperty *property;
  GladeWidget   *gwidget = glade_editable_loaded_widget (GLADE_EDITABLE (label_editor));

  if (glade_editable_loading (GLADE_EDITABLE (label_editor)) || !gwidget)
    return;

  if (!gtk_toggle_button_get_active
      (GTK_TOGGLE_BUTTON (priv->pattern_radio)))
    return;

  glade_editable_block (GLADE_EDITABLE (label_editor));

  glade_command_push_group (_("Setting %s to use a pattern string"),
                            glade_widget_get_name (gwidget));

  property = glade_widget_get_property (gwidget, "glade-attributes");
  glade_command_set_property (property, NULL);
  property = glade_widget_get_property (gwidget, "use-markup");
  glade_command_set_property (property, FALSE);
  property = glade_widget_get_property (gwidget, "label-content-mode");
  glade_command_set_property (property, GLADE_LABEL_MODE_PATTERN);

  glade_command_pop_group ();

  glade_editable_unblock (GLADE_EDITABLE (label_editor));

  /* reload buttons and sensitivity and stuff... */
  glade_editable_load (GLADE_EDITABLE (label_editor), gwidget);
}

/**********************************************************************
                    label-wrap-mode radios
 **********************************************************************/
static void
wrap_free_toggled (GtkWidget *widget, GladeLabelEditor *label_editor)
{
  GladeLabelEditorPrivate *priv = label_editor->priv;
  GladeProperty *property;
  GladeWidget   *gwidget = glade_editable_loaded_widget (GLADE_EDITABLE (label_editor));

  if (glade_editable_loading (GLADE_EDITABLE (label_editor)) || !gwidget)
    return;

  if (!gtk_toggle_button_get_active
      (GTK_TOGGLE_BUTTON (priv->wrap_free_radio)))
    return;

  glade_editable_block (GLADE_EDITABLE (label_editor));

  glade_command_push_group (_("Setting %s to use normal line wrapping"),
                            glade_widget_get_name (gwidget));

  property = glade_widget_get_property (gwidget, "single-line-mode");
  glade_command_set_property (property, FALSE);
  property = glade_widget_get_property (gwidget, "wrap-mode");
  glade_command_set_property (property, PANGO_WRAP_WORD);
  property = glade_widget_get_property (gwidget, "wrap");
  glade_command_set_property (property, FALSE);

  property = glade_widget_get_property (gwidget, "label-wrap-mode");
  glade_command_set_property (property, GLADE_LABEL_WRAP_FREE);

  glade_command_pop_group ();

  glade_editable_unblock (GLADE_EDITABLE (label_editor));

  /* reload buttons and sensitivity and stuff... */
  glade_editable_load (GLADE_EDITABLE (label_editor), gwidget);
}

static void
single_toggled (GtkWidget *widget, GladeLabelEditor *label_editor)
{
  GladeLabelEditorPrivate *priv = label_editor->priv;
  GladeProperty *property;
  GladeWidget   *gwidget = glade_editable_loaded_widget (GLADE_EDITABLE (label_editor));

  if (glade_editable_loading (GLADE_EDITABLE (label_editor)) || !gwidget)
    return;

  if (!gtk_toggle_button_get_active
      (GTK_TOGGLE_BUTTON (priv->single_radio)))
    return;

  glade_editable_block (GLADE_EDITABLE (label_editor));

  glade_command_push_group (_("Setting %s to use a single line"),
                            glade_widget_get_name (gwidget));

  property = glade_widget_get_property (gwidget, "wrap-mode");
  glade_command_set_property (property, PANGO_WRAP_WORD);
  property = glade_widget_get_property (gwidget, "wrap");
  glade_command_set_property (property, FALSE);

  property = glade_widget_get_property (gwidget, "single-line-mode");
  glade_command_set_property (property, TRUE);
  property = glade_widget_get_property (gwidget, "label-wrap-mode");
  glade_command_set_property (property, GLADE_LABEL_SINGLE_LINE);

  glade_command_pop_group ();

  glade_editable_unblock (GLADE_EDITABLE (label_editor));

  /* reload buttons and sensitivity and stuff... */
  glade_editable_load (GLADE_EDITABLE (label_editor), gwidget);
}

static void
wrap_mode_toggled (GtkWidget *widget, GladeLabelEditor *label_editor)
{
  GladeLabelEditorPrivate *priv = label_editor->priv;
  GladeProperty *property;
  GladeWidget   *gwidget = glade_editable_loaded_widget (GLADE_EDITABLE (label_editor));

  if (glade_editable_loading (GLADE_EDITABLE (label_editor)) || !gwidget)
    return;

  if (!gtk_toggle_button_get_active
      (GTK_TOGGLE_BUTTON (priv->wrap_mode_radio)))
    return;

  glade_editable_block (GLADE_EDITABLE (label_editor));

  glade_command_push_group (_("Setting %s to use specific Pango word wrapping"),
                            glade_widget_get_name (gwidget));

  property = glade_widget_get_property (gwidget, "single-line-mode");
  glade_command_set_property (property, FALSE);
  property = glade_widget_get_property (gwidget, "wrap");
  glade_command_set_property (property, TRUE);

  property = glade_widget_get_property (gwidget, "label-wrap-mode");
  glade_command_set_property (property, GLADE_LABEL_WRAP_MODE);

  glade_command_pop_group ();

  glade_editable_unblock (GLADE_EDITABLE (label_editor));

  /* reload buttons and sensitivity and stuff... */
  glade_editable_load (GLADE_EDITABLE (label_editor), gwidget);
}

GtkWidget *
glade_label_editor_new (void)
{
  return g_object_new (GLADE_TYPE_LABEL_EDITOR, NULL);
}