summaryrefslogtreecommitdiff
path: root/src/ui/metaaccellabel.c
blob: f6903d4486c8bc8e440f9c08831fc16078f945ef (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/* Marco hacked-up GtkAccelLabel */
/* Copyright (C) 2002 Red Hat, Inc. */
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * MetaAccelLabel: GtkLabel with accelerator monitoring facilities.
 * Copyright (C) 1998 Tim Janik
 *
 * 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 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 library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include <config.h>
#include "metaaccellabel.h"
#include <gtk/gtk.h>
#include <string.h>
#include "util.h"

static void     meta_accel_label_class_init   (MetaAccelLabelClass *klass);
static void     meta_accel_label_init         (MetaAccelLabel      *accel_label);
static void     meta_accel_label_destroy      (GtkWidget           *object);
static void     meta_accel_label_finalize     (GObject             *object);
static void  meta_accel_label_update          (MetaAccelLabel *accel_label);
static int   meta_accel_label_get_accel_width (MetaAccelLabel *accel_label);

static void     meta_accel_label_get_preferred_width  (GtkWidget *widget,
                                                       gint      *minimum,
                                                       gint      *natural);
static void     meta_accel_label_get_preferred_height (GtkWidget *widget,
                                                       gint      *minimum,
                                                       gint      *natural);
static gboolean meta_accel_label_draw         (GtkWidget           *widget,
                                               cairo_t             *cr);

G_DEFINE_TYPE (MetaAccelLabel, meta_accel_label, GTK_TYPE_LABEL);

static void
meta_accel_label_class_init (MetaAccelLabelClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  gobject_class->finalize = meta_accel_label_finalize;

  widget_class->destroy = meta_accel_label_destroy;

  widget_class->get_preferred_width = meta_accel_label_get_preferred_width;
  widget_class->get_preferred_height = meta_accel_label_get_preferred_height;
  widget_class->draw = meta_accel_label_draw;

  class->signal_quote1 = g_strdup ("<:");
  class->signal_quote2 = g_strdup (":>");
  /* This is the text that should appear next to menu accelerators
   * that use the shift key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_shift = g_strdup (_("Shift"));
  /* This is the text that should appear next to menu accelerators
   * that use the control key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_control = g_strdup (_("Ctrl"));
  /* This is the text that should appear next to menu accelerators
   * that use the alt key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_alt = g_strdup (_("Alt"));
  /* This is the text that should appear next to menu accelerators
   * that use the meta key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_meta = g_strdup (_("Meta"));
  /* This is the text that should appear next to menu accelerators
   * that use the super key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_super = g_strdup (_("Super"));
  /* This is the text that should appear next to menu accelerators
   * that use the hyper key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_hyper = g_strdup (_("Hyper"));
  /* This is the text that should appear next to menu accelerators
   * that use the mod2 key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_mod2 = g_strdup (_("Mod2"));
  /* This is the text that should appear next to menu accelerators
   * that use the mod3 key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_mod3 = g_strdup (_("Mod3"));
  /* This is the text that should appear next to menu accelerators
   * that use the mod4 key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_mod4 = g_strdup (_("Mod4"));
  /* This is the text that should appear next to menu accelerators
   * that use the mod5 key. If the text on this key isn't typically
   * translated on keyboards used for your language, don't translate
   * this.
   */
  class->mod_name_mod5 = g_strdup (_("Mod5"));

  class->mod_separator = g_strdup ("+");
  class->accel_seperator = g_strdup (" / ");
  class->latin1_to_char = TRUE;
}

static void
meta_accel_label_init (MetaAccelLabel *accel_label)
{
  accel_label->accel_padding = 3;
  accel_label->accel_string = NULL;

  meta_accel_label_update (accel_label);
}

GtkWidget*
meta_accel_label_new_with_mnemonic (const gchar *string)
{
  MetaAccelLabel *accel_label;

  g_return_val_if_fail (string != NULL, NULL);

  accel_label = g_object_new (META_TYPE_ACCEL_LABEL, NULL);

  gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), string);

  return GTK_WIDGET (accel_label);
}

static void
meta_accel_label_destroy (GtkWidget *object)
{
  MetaAccelLabel *accel_label = META_ACCEL_LABEL (object);

  g_free (accel_label->accel_string);
  accel_label->accel_string = NULL;

  accel_label->accel_mods = 0;
  accel_label->accel_key = 0;

  GTK_WIDGET_CLASS (meta_accel_label_parent_class)->destroy (object);
}

static void
meta_accel_label_finalize (GObject *object)
{
  MetaAccelLabel *accel_label = META_ACCEL_LABEL (object);

  g_free (accel_label->accel_string);

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

void
meta_accel_label_set_accelerator (MetaAccelLabel         *accel_label,
                                  guint                   accelerator_key,
                                  MetaVirtualModifier     accelerator_mods)
{
  g_return_if_fail (META_IS_ACCEL_LABEL (accel_label));

  if (accelerator_key != accel_label->accel_key ||
      accelerator_mods != accel_label->accel_mods)
    {
      accel_label->accel_mods = accelerator_mods;
      accel_label->accel_key = accelerator_key;

      meta_accel_label_update (accel_label);
    }
}

static int
meta_accel_label_get_accel_width (MetaAccelLabel *accel_label)
{
  g_return_val_if_fail (META_IS_ACCEL_LABEL (accel_label), 0);

  return (accel_label->accel_string_width +
	  (accel_label->accel_string_width ? accel_label->accel_padding : 0));
}

static void
meta_accel_label_get_preferred_width (GtkWidget *widget,
                                      gint      *minimum,
                                      gint      *natural)
{
  MetaAccelLabel *accel_label = META_ACCEL_LABEL (widget);
  PangoLayout *layout;
  gint width;

  GTK_WIDGET_CLASS (meta_accel_label_parent_class)->get_preferred_width (widget, minimum, natural);

  layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);
  pango_layout_get_pixel_size (layout, &width, NULL);
  accel_label->accel_string_width = width;

  g_object_unref (G_OBJECT (layout));
}

static void
meta_accel_label_get_preferred_height (GtkWidget *widget,
                                       gint      *minimum,
                                       gint      *natural)
{
  GTK_WIDGET_CLASS (meta_accel_label_parent_class)->get_preferred_height (widget, minimum, natural);
}

static gboolean
meta_accel_label_draw (GtkWidget *widget,
                       cairo_t   *cr)
{
  MetaAccelLabel *accel_label = META_ACCEL_LABEL (widget);
  GtkTextDirection direction;
  int ac_width;
  GtkAllocation allocation;
  GtkRequisition requisition;

  direction = gtk_widget_get_direction (widget);
  ac_width = meta_accel_label_get_accel_width (accel_label);
  gtk_widget_get_allocation (widget, &allocation);
  gtk_widget_get_preferred_size (widget,
                                 &requisition, NULL);

  if (allocation.width >= requisition.width + ac_width)
    {
      GtkStyleContext *style;
      PangoLayout *label_layout;
      PangoLayout *accel_layout;
      GtkLabel *label = GTK_LABEL (widget);
      gint x, y, xpad, ypad;
      gint margin_start, margin_end, margin_top, margin_bottom;
      gfloat yalign;

      label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));
      yalign = gtk_label_get_yalign (GTK_LABEL (accel_label));

      cairo_save (cr);

      /* XXX: Mad hack: We modify the label's width so it renders
       * properly in its draw function that we chain to. */
      if (direction == GTK_TEXT_DIR_RTL)
        cairo_translate (cr, ac_width, 0);
      if (gtk_label_get_ellipsize (label))
        pango_layout_set_width (label_layout,
                                pango_layout_get_width (label_layout)
                                - ac_width * PANGO_SCALE);

      allocation.width -= ac_width;
      gtk_widget_set_allocation (widget, &allocation);
      if (GTK_WIDGET_CLASS (meta_accel_label_parent_class)->draw)
        GTK_WIDGET_CLASS (meta_accel_label_parent_class)->draw (widget,
                                                               cr);
      allocation.width += ac_width;
      gtk_widget_set_allocation (widget, &allocation);
      if (gtk_label_get_ellipsize (label))
        pango_layout_set_width (label_layout,
                                pango_layout_get_width (label_layout)
                                + ac_width * PANGO_SCALE);

      cairo_restore (cr);

      margin_start = gtk_widget_get_margin_start (widget);
      margin_end = gtk_widget_get_margin_end (widget);
      margin_top = gtk_widget_get_margin_top (widget);
      margin_bottom = gtk_widget_get_margin_bottom (widget);

      xpad = margin_start + margin_end;
      ypad = margin_top + margin_bottom;

      if (direction == GTK_TEXT_DIR_RTL)
        x = xpad;
      else
        x = gtk_widget_get_allocated_width (widget) - xpad - ac_width;

      gtk_label_get_layout_offsets (GTK_LABEL (accel_label), NULL, &y);

      accel_layout = gtk_widget_create_pango_layout (widget, accel_label->accel_string);

      y = (allocation.height - (requisition.height - ypad * 2)) * yalign + 1.5;

      style = gtk_widget_get_style_context (widget);
      gtk_style_context_save (style);
      gtk_style_context_set_state (style,
                                   gtk_widget_get_state_flags (widget));
      gtk_render_layout (gtk_widget_get_style_context (widget),
                         cr,
                         x, y,
                         accel_layout);
      gtk_style_context_restore (style);

      g_object_unref (accel_layout);
    }
  else
    {
      if (GTK_WIDGET_CLASS (meta_accel_label_parent_class)->draw)
        GTK_WIDGET_CLASS (meta_accel_label_parent_class)->draw (widget, cr);
    }

  return FALSE;
}

static void
meta_accel_label_update (MetaAccelLabel *accel_label)
{
  MetaAccelLabelClass *class;
  GString *gstring;
  gboolean seen_mod = FALSE;
  gunichar ch;

  g_return_if_fail (META_IS_ACCEL_LABEL (accel_label));

  class = META_ACCEL_LABEL_GET_CLASS (accel_label);

  g_free (accel_label->accel_string);
  accel_label->accel_string = NULL;

  gstring = g_string_new (accel_label->accel_string);
  g_string_append (gstring, gstring->len ? class->accel_seperator : "   ");

  if (accel_label->accel_mods & META_VIRTUAL_SHIFT_MASK)
    {
      g_string_append (gstring, class->mod_name_shift);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_CONTROL_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_control);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_ALT_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_alt);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_META_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_meta);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_SUPER_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_super);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_HYPER_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_hyper);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_MOD2_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_mod2);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_MOD3_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_mod3);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_MOD4_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_mod4);
      seen_mod = TRUE;
    }
  if (accel_label->accel_mods & META_VIRTUAL_MOD5_MASK)
    {
      if (seen_mod)
        g_string_append (gstring, class->mod_separator);
      g_string_append (gstring, class->mod_name_mod5);
      seen_mod = TRUE;
    }

  if (seen_mod)
    g_string_append (gstring, class->mod_separator);

  ch = gdk_keyval_to_unicode (accel_label->accel_key);
  if (ch && (g_unichar_isgraph (ch) || ch == ' ') &&
      (ch < 0x80 || class->latin1_to_char))
    {
      switch (ch)
        {
        case ' ':
          g_string_append (gstring, "Space");
          break;
        case '\\':
          g_string_append (gstring, "Backslash");
          break;
        default:
          g_string_append_unichar (gstring, g_unichar_toupper (ch));
          break;
        }
    }
  else
    {
      gchar *tmp;

      tmp = gtk_accelerator_name (accel_label->accel_key, 0);
      if (tmp[0] != 0 && tmp[1] == 0)
        tmp[0] = g_ascii_toupper (tmp[0]);
      g_string_append (gstring, tmp);
      g_free (tmp);
    }

  g_free (accel_label->accel_string);
  accel_label->accel_string = gstring->str;
  g_string_free (gstring, FALSE);

  g_assert (accel_label->accel_string);
  /* accel_label->accel_string = g_strdup ("-/-"); */

  gtk_widget_queue_resize (GTK_WIDGET (accel_label));
}