gtkaccellabel.c 27.5 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Tim Janik's avatar
Tim Janik committed
2 3 4 5 6 7
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * GtkAccelLabel: GtkLabel with accelerator monitoring facilities.
 * Copyright (C) 1998 Tim Janik
 *
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
9 10 11 12 13 14
 * 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
15
 * Lesser General Public License for more details.
Tim Janik's avatar
Tim Janik committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
18 19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
22 23

/*
24
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
25 26 27 28
 * 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/. 
 */
Manish Singh's avatar
Manish Singh committed
29

30
#include "config.h"
Manish Singh's avatar
Manish Singh committed
31 32
#include <string.h>

33 34
#include "gtkaccellabel.h"
#include "gtkaccelmap.h"
Tim Janik's avatar
Tim Janik committed
35
#include "gtkmain.h"
36
#include "gtksizerequest.h"
37
#include "gtkprivate.h"
38
#include "gtkintl.h"
39

40
#include <gdk/gdkkeysyms.h>
Tim Janik's avatar
Tim Janik committed
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
/**
 * SECTION:gtkaccellabel
 * @Short_description: A label which displays an accelerator key on the right of the text
 * @Title: GtkAccelLabel
 * @See_also: #GtkItemFactory, #GtkAccelGroup
 *
 * The #GtkAccelLabel widget is a subclass of #GtkLabel that also displays an
 * accelerator key on the right of the label text, e.g. 'Ctl+S'.
 * It is commonly used in menus to show the keyboard short-cuts for commands.
 *
 * The accelerator key to display is not set explicitly.
 * Instead, the #GtkAccelLabel displays the accelerators which have been added to
 * a particular widget. This widget is set by calling
 * gtk_accel_label_set_accel_widget().
 *
 * For example, a #GtkMenuItem widget may have an accelerator added to emit the
 * "activate" signal when the 'Ctl+S' key combination is pressed.
 * A #GtkAccelLabel is created and added to the #GtkMenuItem, and
 * gtk_accel_label_set_accel_widget() is called with the #GtkMenuItem as the
 * second argument. The #GtkAccelLabel will now display 'Ctl+S' after its label.
 *
 * Note that creating a #GtkMenuItem with gtk_menu_item_new_with_label() (or
 * one of the similar functions for #GtkCheckMenuItem and #GtkRadioMenuItem)
 * automatically adds a #GtkAccelLabel to the #GtkMenuItem and calls
 * gtk_accel_label_set_accel_widget() to set it up for you.
 *
 * A #GtkAccelLabel will only display accelerators which have %GTK_ACCEL_VISIBLE
 * set (see #GtkAccelFlags).
 * A #GtkAccelLabel can display multiple accelerators and even signal names,
 * though it is almost always used to display just one accelerator key.
 * <example>
 * <title>Creating a simple menu item with an accelerator key.</title>
 * <programlisting>
 *   GtkWidget *save_item;
 *   GtkAccelGroup *accel_group;
 *
 *   /<!---->* Create a GtkAccelGroup and add it to the window. *<!---->/
 *   accel_group = gtk_accel_group_new (<!-- -->);
 *   gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
 *
 *   /<!---->* Create the menu item using the convenience function. *<!---->/
 *   save_item = gtk_menu_item_new_with_label ("Save");
 *   gtk_widget_show (save_item);
 *   gtk_container_add (GTK_CONTAINER (menu), save_item);
 *
 *   /<!---->* Now add the accelerator to the GtkMenuItem. Note that since we called
 *      gtk_menu_item_new_with_label(<!-- -->) to create the GtkMenuItem the
 *      GtkAccelLabel is automatically set up to display the GtkMenuItem
 *      accelerators. We just need to make sure we use GTK_ACCEL_VISIBLE here. *<!---->/
 *   gtk_widget_add_accelerator (save_item, "activate", accel_group,
 *                               GDK_s, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
 * </programlisting>
 * </example>
 */

Tim Janik's avatar
Tim Janik committed
97
enum {
98
  PROP_0,
99 100
  PROP_ACCEL_CLOSURE,
  PROP_ACCEL_WIDGET
Tim Janik's avatar
Tim Janik committed
101 102
};

103 104 105 106 107 108 109 110 111 112
struct _GtkAccelLabelPrivate
{
  guint          accel_padding;      /* should be style property? */
  GtkWidget     *accel_widget;       /* done*/
  GClosure      *accel_closure;      /* has set function */
  GtkAccelGroup *accel_group;        /* set by set_accel_closure() */
  gchar         *accel_string;       /* has set function */
  guint16        accel_string_width; /* seems to be private */
};

113 114 115 116 117 118 119 120 121 122 123 124 125
static void         gtk_accel_label_set_property (GObject            *object,
						  guint               prop_id,
						  const GValue       *value,
						  GParamSpec         *pspec);
static void         gtk_accel_label_get_property (GObject            *object,
						  guint               prop_id,
						  GValue             *value,
						  GParamSpec         *pspec);
static void         gtk_accel_label_destroy      (GtkObject          *object);
static void         gtk_accel_label_finalize     (GObject            *object);
static gboolean     gtk_accel_label_expose_event (GtkWidget          *widget,
						  GdkEventExpose     *event);
static const gchar *gtk_accel_label_get_string   (GtkAccelLabel      *accel_label);
Tim Janik's avatar
Tim Janik committed
126

127 128 129 130 131 132

static void         gtk_accel_label_size_request_init (GtkSizeRequestIface *iface);
static void         gtk_accel_label_get_width         (GtkSizeRequest      *widget,
						       gint                *min_width,
						       gint                *nat_width);

133
#define GTK_ACCEL_LABEL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_ACCEL_LABEL, GtkAccelLabelPrivate))
Tim Janik's avatar
Tim Janik committed
134

135 136 137 138 139 140

static GtkSizeRequestIface *parent_size_request_iface;

G_DEFINE_TYPE_WITH_CODE (GtkAccelLabel, gtk_accel_label, GTK_TYPE_LABEL,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
						gtk_accel_label_size_request_init))
Tim Janik's avatar
Tim Janik committed
141 142 143 144

static void
gtk_accel_label_class_init (GtkAccelLabelClass *class)
{
145
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
146 147
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
Tim Janik's avatar
Tim Janik committed
148
  
149
  gobject_class->finalize = gtk_accel_label_finalize;
150 151 152
  gobject_class->set_property = gtk_accel_label_set_property;
  gobject_class->get_property = gtk_accel_label_get_property;
  
Tim Janik's avatar
Tim Janik committed
153
  object_class->destroy = gtk_accel_label_destroy;
154
   
Tim Janik's avatar
Tim Janik committed
155 156 157 158
  widget_class->expose_event = gtk_accel_label_expose_event;

  class->signal_quote1 = g_strdup ("<:");
  class->signal_quote2 = g_strdup (":>");
159 160

#ifndef GDK_WINDOWING_QUARTZ
161 162 163 164 165
  /* 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.
   */
166
  class->mod_name_shift = g_strdup (C_("keyboard label", "Shift"));
167 168 169 170 171
  /* 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.
   */
172
  class->mod_name_control = g_strdup (C_("keyboard label", "Ctrl"));
173 174 175 176 177
  /* 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.
   */
178
  class->mod_name_alt = g_strdup (C_("keyboard label", "Alt"));
Tim Janik's avatar
Tim Janik committed
179
  class->mod_separator = g_strdup ("+");
180 181 182 183 184 185 186 187 188 189 190 191
#else /* GDK_WINDOWING_QUARTZ */

  /* U+21E7 UPWARDS WHITE ARROW */
  class->mod_name_shift = g_strdup ("\xe2\x87\xa7");
  /* U+2303 UP ARROWHEAD */
  class->mod_name_control = g_strdup ("\xe2\x8c\x83");
  /* U+2325 OPTION KEY */
  class->mod_name_alt = g_strdup ("\xe2\x8c\xa5");
  class->mod_separator = g_strdup ("");

#endif /* GDK_WINDOWING_QUARTZ */

Manish Singh's avatar
Manish Singh committed
192
  g_object_class_install_property (gobject_class,
193
                                   PROP_ACCEL_CLOSURE,
194
                                   g_param_spec_boxed ("accel-closure",
195 196
						       P_("Accelerator Closure"),
						       P_("The closure to be monitored for accelerator changes"),
197
						       G_TYPE_CLOSURE,
198
						       GTK_PARAM_READWRITE));
Manish Singh's avatar
Manish Singh committed
199
  g_object_class_install_property (gobject_class,
200
                                   PROP_ACCEL_WIDGET,
201
                                   g_param_spec_object ("accel-widget",
202 203
                                                        P_("Accelerator Widget"),
                                                        P_("The widget to be monitored for accelerator changes"),
204
                                                        GTK_TYPE_WIDGET,
205
                                                        GTK_PARAM_READWRITE));
206 207

  g_type_class_add_private (gobject_class, sizeof (GtkAccelLabelPrivate));
Tim Janik's avatar
Tim Janik committed
208 209 210
}

static void
211 212 213 214
gtk_accel_label_set_property (GObject      *object,
			      guint         prop_id,
			      const GValue *value,
			      GParamSpec   *pspec)
Tim Janik's avatar
Tim Janik committed
215
{
216 217 218 219
  GtkAccelLabel  *accel_label;

  accel_label = GTK_ACCEL_LABEL (object);

220
  switch (prop_id)
Tim Janik's avatar
Tim Janik committed
221
    {
222 223 224 225 226
    case PROP_ACCEL_CLOSURE:
      gtk_accel_label_set_accel_closure (accel_label, g_value_get_boxed (value));
      break;
    case PROP_ACCEL_WIDGET:
      gtk_accel_label_set_accel_widget (accel_label, g_value_get_object (value));
Tim Janik's avatar
Tim Janik committed
227 228
      break;
    default:
229
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
230 231 232 233
      break;
    }
}

234 235 236 237 238
static void
gtk_accel_label_get_property (GObject    *object,
			      guint       prop_id,
			      GValue     *value,
			      GParamSpec *pspec)
Tim Janik's avatar
Tim Janik committed
239
{
240 241 242 243
  GtkAccelLabel  *accel_label;

  accel_label = GTK_ACCEL_LABEL (object);

244
  switch (prop_id)
Tim Janik's avatar
Tim Janik committed
245
    {
246
    case PROP_ACCEL_CLOSURE:
247
      g_value_set_boxed (value, accel_label->priv->accel_closure);
248 249
      break;
    case PROP_ACCEL_WIDGET:
250
      g_value_set_object (value, accel_label->priv->accel_widget);
Tim Janik's avatar
Tim Janik committed
251 252
      break;
    default:
253
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
254 255 256 257 258 259 260
      break;
    }
}

static void
gtk_accel_label_init (GtkAccelLabel *accel_label)
{
261 262 263 264 265 266 267 268 269
  GtkAccelLabelPrivate *priv = GTK_ACCEL_LABEL_GET_PRIVATE (accel_label);

  priv->accel_padding = 3;
  priv->accel_widget = NULL;
  priv->accel_closure = NULL;
  priv->accel_group = NULL;
  priv->accel_string = NULL;

  accel_label->priv = priv;
Tim Janik's avatar
Tim Janik committed
270 271
}

272 273 274 275 276 277 278 279
/**
 * gtk_accel_label_new:
 * @string: the label string. Must be non-%NULL.
 *
 * Creates a new #GtkAccelLabel.
 *
 * Returns: a new #GtkAccelLabel.
 */
Tim Janik's avatar
Tim Janik committed
280 281 282 283 284 285 286
GtkWidget*
gtk_accel_label_new (const gchar *string)
{
  GtkAccelLabel *accel_label;
  
  g_return_val_if_fail (string != NULL, NULL);
  
Manish Singh's avatar
Manish Singh committed
287
  accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
Tim Janik's avatar
Tim Janik committed
288
  
289
  gtk_label_set_text (GTK_LABEL (accel_label), string);
Tim Janik's avatar
Tim Janik committed
290 291 292 293 294 295 296
  
  return GTK_WIDGET (accel_label);
}

static void
gtk_accel_label_destroy (GtkObject *object)
{
297
  GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);
Tim Janik's avatar
Tim Janik committed
298

299 300
  gtk_accel_label_set_accel_widget (accel_label, NULL);
  gtk_accel_label_set_accel_closure (accel_label, NULL);
Tim Janik's avatar
Tim Janik committed
301
  
Matthias Clasen's avatar
Matthias Clasen committed
302
  GTK_OBJECT_CLASS (gtk_accel_label_parent_class)->destroy (object);
Tim Janik's avatar
Tim Janik committed
303 304 305
}

static void
306
gtk_accel_label_finalize (GObject *object)
Tim Janik's avatar
Tim Janik committed
307
{
308 309
  GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (object);

310 311
  g_free (accel_label->priv->accel_string);

Matthias Clasen's avatar
Matthias Clasen committed
312
  G_OBJECT_CLASS (gtk_accel_label_parent_class)->finalize (object);
Tim Janik's avatar
Tim Janik committed
313 314
}

315
/**
316
 * gtk_accel_label_get_accel_widget:
317 318 319
 * @accel_label: a #GtkAccelLabel
 *
 * Fetches the widget monitored by this accelerator label. See
320
 * gtk_accel_label_set_accel_widget().
321
 *
322
 * Returns: the object monitored by the accelerator label, or %NULL.
323
 **/
324 325
GtkWidget*
gtk_accel_label_get_accel_widget (GtkAccelLabel *accel_label)
326 327 328
{
  g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), NULL);

329
  return accel_label->priv->accel_widget;
330 331
}

332 333 334 335 336 337 338 339 340 341
/**
 * gtk_accel_label_get_accel_width:
 * @accel_label: a #GtkAccelLabel.
 *
 * Returns the width needed to display the accelerator key(s).
 * This is used by menus to align all of the #GtkMenuItem widgets, and shouldn't
 * be needed by applications.
 *
 * Returns: the width needed to display the accelerator key(s).
 */
342
guint
343
gtk_accel_label_get_accel_width (GtkAccelLabel *accel_label)
344
{
345
  g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), 0);
346 347 348

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

Tim Janik's avatar
Tim Janik committed
351
static void
352 353 354 355 356 357 358 359 360 361
gtk_accel_label_size_request_init (GtkSizeRequestIface *iface)
{
  parent_size_request_iface = g_type_interface_peek_parent (iface);
  iface->get_width = gtk_accel_label_get_width;
}

static void
gtk_accel_label_get_width (GtkSizeRequest  *widget,
			   gint            *min_width,
			   gint            *nat_width)
Tim Janik's avatar
Tim Janik committed
362
{
363
  GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
364 365
  PangoLayout   *layout;
  gint           width;
366

367
  parent_size_request_iface->get_width (widget, min_width, nat_width);
368

369 370
  layout = gtk_widget_create_pango_layout (GTK_WIDGET (widget), 
					   gtk_accel_label_get_string (accel_label));
371
  pango_layout_get_pixel_size (layout, &width, NULL);
372 373
  accel_label->priv->accel_string_width = width;

Manish Singh's avatar
Manish Singh committed
374
  g_object_unref (layout);
Tim Janik's avatar
Tim Janik committed
375 376
}

377 378 379 380 381 382 383 384 385 386 387 388 389
static gint
get_first_baseline (PangoLayout *layout)
{
  PangoLayoutIter *iter;
  gint result;

  iter = pango_layout_get_iter (layout);
  result = pango_layout_iter_get_baseline (iter);
  pango_layout_iter_free (iter);

  return PANGO_PIXELS (result);
}

390
static gboolean 
Tim Janik's avatar
Tim Janik committed
391 392 393
gtk_accel_label_expose_event (GtkWidget      *widget,
			      GdkEventExpose *event)
{
394 395
  GtkAccelLabel *accel_label = GTK_ACCEL_LABEL (widget);
  GtkMisc *misc = GTK_MISC (accel_label);
396 397 398 399
  GtkTextDirection direction;

  direction = gtk_widget_get_direction (widget);

400
  if (gtk_widget_is_drawable (widget)) 
Tim Janik's avatar
Tim Janik committed
401 402
    {
      guint ac_width;
403 404 405
      GtkAllocation allocation;
      GtkRequisition requisition;

406
      ac_width = gtk_accel_label_get_accel_width (accel_label);
407 408 409 410
      gtk_widget_get_allocation (widget, &allocation);
      gtk_widget_size_request (widget, &requisition);

      if (allocation.width >= requisition.width + ac_width)
Tim Janik's avatar
Tim Janik committed
411
	{
412 413
	  PangoLayout *label_layout;
	  PangoLayout *accel_layout;
414
	  GtkLabel *label = GTK_LABEL (widget);
415 416 417

	  gint x;
	  gint y;
418 419
	  gint xpad;

420 421
	  label_layout = gtk_label_get_layout (GTK_LABEL (accel_label));

422
	  if (direction == GTK_TEXT_DIR_RTL)
423 424
	    allocation.x += ac_width;
	  allocation.width -= ac_width;
425
	  if (gtk_label_get_ellipsize (label))
426 427
	    pango_layout_set_width (label_layout,
				    pango_layout_get_width (label_layout) 
428 429
				    - ac_width * PANGO_SCALE);
	  
Matthias Clasen's avatar
Matthias Clasen committed
430 431
	  if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
	    GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
432
	  if (direction == GTK_TEXT_DIR_RTL)
433 434
	    allocation.x -= ac_width;
	  allocation.width += ac_width;
435
	  if (gtk_label_get_ellipsize (label))
436 437
	    pango_layout_set_width (label_layout,
				    pango_layout_get_width (label_layout) 
438
				    + ac_width * PANGO_SCALE);
439 440 441

	  gtk_misc_get_padding (misc, &xpad, NULL);

442
	  if (direction == GTK_TEXT_DIR_RTL)
443
	    x = allocation.x + xpad;
444
	  else
445
	    x = allocation.x + allocation.width - xpad - ac_width;
446 447 448

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

449
	  accel_layout = gtk_widget_create_pango_layout (widget, gtk_accel_label_get_string (accel_label));
450 451

	  y += get_first_baseline (label_layout) - get_first_baseline (accel_layout);
452

453 454
          gtk_paint_layout (gtk_widget_get_style (widget),
                            gtk_widget_get_window (widget),
455
                            gtk_widget_get_state (widget),
456
			    FALSE,
457 458 459 460
                            &event->area,
                            widget,
                            "accellabel",
                            x, y,
461
                            accel_layout);                            
462

463
          g_object_unref (accel_layout);
Tim Janik's avatar
Tim Janik committed
464 465 466
	}
      else
	{
Matthias Clasen's avatar
Matthias Clasen committed
467 468
	  if (GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event)
	    GTK_WIDGET_CLASS (gtk_accel_label_parent_class)->expose_event (widget, event);
Tim Janik's avatar
Tim Janik committed
469 470 471
	}
    }
  
472
  return FALSE;
Tim Janik's avatar
Tim Janik committed
473 474
}

475 476 477
static void
refetch_widget_accel_closure (GtkAccelLabel *accel_label)
{
478 479 480
  GClosure *closure = NULL;
  GList *clist, *list;
  
481
  g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
482
  g_return_if_fail (GTK_IS_WIDGET (accel_label->priv->accel_widget));
483
  
484
  clist = gtk_widget_list_accel_closures (accel_label->priv->accel_widget);
485 486 487 488 489 490 491 492
  for (list = clist; list; list = list->next)
    {
      /* we just take the first closure used */
      closure = list->data;
      break;
    }
  g_list_free (clist);
  gtk_accel_label_set_accel_closure (accel_label, closure);
493 494
}

Matthias Clasen's avatar
Matthias Clasen committed
495 496 497 498 499 500 501
/**
 * gtk_accel_label_set_accel_widget:
 * @accel_label: a #GtkAccelLabel
 * @accel_widget: the widget to be monitored.
 *
 * Sets the widget to be monitored by this accelerator label. 
 **/
502 503 504 505 506 507 508 509
void
gtk_accel_label_set_accel_widget (GtkAccelLabel *accel_label,
				  GtkWidget     *accel_widget)
{
  g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
  if (accel_widget)
    g_return_if_fail (GTK_IS_WIDGET (accel_widget));
    
510
  if (accel_widget != accel_label->priv->accel_widget)
511
    {
512
      if (accel_label->priv->accel_widget)
513 514
	{
	  gtk_accel_label_set_accel_closure (accel_label, NULL);
515
	  g_signal_handlers_disconnect_by_func (accel_label->priv->accel_widget,
Manish Singh's avatar
Manish Singh committed
516
						refetch_widget_accel_closure,
517
						accel_label);
518
	  g_object_unref (accel_label->priv->accel_widget);
519
	}
520 521
      accel_label->priv->accel_widget = accel_widget;
      if (accel_label->priv->accel_widget)
522
	{
523 524
	  g_object_ref (accel_label->priv->accel_widget);
	  g_signal_connect_object (accel_label->priv->accel_widget, "accel-closures-changed",
525 526 527 528
				   G_CALLBACK (refetch_widget_accel_closure),
				   accel_label, G_CONNECT_SWAPPED);
	  refetch_widget_accel_closure (accel_label);
	}
529
      g_object_notify (G_OBJECT (accel_label), "accel-widget");
530 531 532
    }
}

533 534 535
static void
gtk_accel_label_reset (GtkAccelLabel *accel_label)
{
536
  if (accel_label->priv->accel_string)
537
    {
538 539
      g_free (accel_label->priv->accel_string);
      accel_label->priv->accel_string = NULL;
540 541 542 543 544
    }
  
  gtk_widget_queue_resize (GTK_WIDGET (accel_label));
}

545 546 547 548 549 550 551
static void
check_accel_changed (GtkAccelGroup  *accel_group,
		     guint           keyval,
		     GdkModifierType modifier,
		     GClosure       *accel_closure,
		     GtkAccelLabel  *accel_label)
{
552
  if (accel_closure == accel_label->priv->accel_closure)
553
    gtk_accel_label_reset (accel_label);
554 555
}

556 557 558 559 560 561 562 563
/**
 * gtk_accel_label_set_accel_closure:
 * @accel_label: a #GtkAccelLabel
 * @accel_closure: the closure to monitor for accelerator changes.
 *
 * Sets the closure to be monitored by this accelerator label. The closure
 * must be connected to an accelerator group; see gtk_accel_group_connect().
 **/
Tim Janik's avatar
Tim Janik committed
564
void
565 566
gtk_accel_label_set_accel_closure (GtkAccelLabel *accel_label,
				   GClosure      *accel_closure)
Tim Janik's avatar
Tim Janik committed
567 568
{
  g_return_if_fail (GTK_IS_ACCEL_LABEL (accel_label));
569 570 571
  if (accel_closure)
    g_return_if_fail (gtk_accel_group_from_accel_closure (accel_closure) != NULL);

572
  if (accel_closure != accel_label->priv->accel_closure)
Tim Janik's avatar
Tim Janik committed
573
    {
574
      if (accel_label->priv->accel_closure)
Tim Janik's avatar
Tim Janik committed
575
	{
576
	  g_signal_handlers_disconnect_by_func (accel_label->priv->accel_group,
Manish Singh's avatar
Manish Singh committed
577
						check_accel_changed,
578
						accel_label);
579 580
	  accel_label->priv->accel_group = NULL;
	  g_closure_unref (accel_label->priv->accel_closure);
Tim Janik's avatar
Tim Janik committed
581
	}
582 583
      accel_label->priv->accel_closure = accel_closure;
      if (accel_label->priv->accel_closure)
Tim Janik's avatar
Tim Janik committed
584
	{
585 586 587
	  g_closure_ref (accel_label->priv->accel_closure);
	  accel_label->priv->accel_group = gtk_accel_group_from_accel_closure (accel_closure);
	  g_signal_connect_object (accel_label->priv->accel_group, "accel-changed",
588 589
				   G_CALLBACK (check_accel_changed),
				   accel_label, 0);
Tim Janik's avatar
Tim Janik committed
590
	}
591
      gtk_accel_label_reset (accel_label);
592
      g_object_notify (G_OBJECT (accel_label), "accel-closure");
Tim Janik's avatar
Tim Janik committed
593 594 595
    }
}

596 597 598 599 600 601 602 603
static gboolean
find_accel (GtkAccelKey *key,
	    GClosure    *closure,
	    gpointer     data)
{
  return data == (gpointer) closure;
}

604 605 606
static const gchar *
gtk_accel_label_get_string (GtkAccelLabel *accel_label)
{
607
  if (!accel_label->priv->accel_string)
608 609
    gtk_accel_label_refetch (accel_label);
  
610
  return accel_label->priv->accel_string;
611 612
}

613 614 615 616 617 618 619 620 621 622 623 624 625
/* Underscores in key names are better displayed as spaces
 * E.g., Page_Up should be "Page Up"
 */
static void
substitute_underscores (char *str)
{
  char *p;

  for (p = str; *p; p++)
    if (*p == '_')
      *p = ' ';
}

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
/* On Mac, if the key has symbolic representation (e.g. arrow keys),
 * append it to gstring and return TRUE; otherwise return FALSE.
 * See http://docs.info.apple.com/article.html?path=Mac/10.5/en/cdb_symbs.html 
 * for the list of special keys. */
static gboolean
append_keyval_symbol (guint    accelerator_key,
                      GString *gstring)
{
#ifdef GDK_WINDOWING_QUARTZ
  switch (accelerator_key)
  {
  case GDK_Return:
    /* U+21A9 LEFTWARDS ARROW WITH HOOK */
    g_string_append (gstring, "\xe2\x86\xa9");
    return TRUE;

  case GDK_ISO_Enter:
    /* U+2324 UP ARROWHEAD BETWEEN TWO HORIZONTAL BARS */
    g_string_append (gstring, "\xe2\x8c\xa4");
    return TRUE;

  case GDK_Left:
    /* U+2190 LEFTWARDS ARROW */
    g_string_append (gstring, "\xe2\x86\x90");
    return TRUE;

  case GDK_Up:
    /* U+2191 UPWARDS ARROW */
    g_string_append (gstring, "\xe2\x86\x91");
    return TRUE;

  case GDK_Right:
    /* U+2192 RIGHTWARDS ARROW */
    g_string_append (gstring, "\xe2\x86\x92");
    return TRUE;

  case GDK_Down:
    /* U+2193 DOWNWARDS ARROW */
    g_string_append (gstring, "\xe2\x86\x93");
    return TRUE;

  case GDK_Page_Up:
    /* U+21DE UPWARDS ARROW WITH DOUBLE STROKE */
    g_string_append (gstring, "\xe2\x87\x9e");
    return TRUE;

  case GDK_Page_Down:
    /* U+21DF DOWNWARDS ARROW WITH DOUBLE STROKE */
    g_string_append (gstring, "\xe2\x87\x9f");
    return TRUE;

  case GDK_Home:
    /* U+2196 NORTH WEST ARROW */
    g_string_append (gstring, "\xe2\x86\x96");
    return TRUE;

  case GDK_End:
    /* U+2198 SOUTH EAST ARROW */
    g_string_append (gstring, "\xe2\x86\x98");
    return TRUE;

  case GDK_Escape:
    /* U+238B BROKEN CIRCLE WITH NORTHWEST ARROW */
    g_string_append (gstring, "\xe2\x8e\x8b");
    return TRUE;

  case GDK_BackSpace:
    /* U+232B ERASE TO THE LEFT */
    g_string_append (gstring, "\xe2\x8c\xab");
    return TRUE;

  case GDK_Delete:
    /* U+2326 ERASE TO THE RIGHT */
    g_string_append (gstring, "\xe2\x8c\xa6");
    return TRUE;

  default:
    return FALSE;
  }
#else /* !GDK_WINDOWING_QUARTZ */
  return FALSE;
#endif
}

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
gchar *
_gtk_accel_label_class_get_accelerator_label (GtkAccelLabelClass *klass,
					      guint               accelerator_key,
					      GdkModifierType     accelerator_mods)
{
  GString *gstring;
  gboolean seen_mod = FALSE;
  gunichar ch;
  
  gstring = g_string_new ("");
  
  if (accelerator_mods & GDK_SHIFT_MASK)
    {
      g_string_append (gstring, klass->mod_name_shift);
      seen_mod = TRUE;
    }
  if (accelerator_mods & GDK_CONTROL_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);
      g_string_append (gstring, klass->mod_name_control);
      seen_mod = TRUE;
    }
733
  if (accelerator_mods & GDK_MOD1_MASK)
734 735 736 737 738 739
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);
      g_string_append (gstring, klass->mod_name_alt);
      seen_mod = TRUE;
    }
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
  if (accelerator_mods & GDK_MOD2_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);

      g_string_append (gstring, "Mod2");
      seen_mod = TRUE;
    }
  if (accelerator_mods & GDK_MOD3_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);

      g_string_append (gstring, "Mod3");
      seen_mod = TRUE;
    }
  if (accelerator_mods & GDK_MOD4_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);

      g_string_append (gstring, "Mod4");
      seen_mod = TRUE;
    }
  if (accelerator_mods & GDK_MOD5_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);

      g_string_append (gstring, "Mod5");
      seen_mod = TRUE;
    }
  if (accelerator_mods & GDK_SUPER_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);

      /* 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.
       */
782
      g_string_append (gstring, C_("keyboard label", "Super"));
783 784 785 786 787 788 789 790 791 792 793 794
      seen_mod = TRUE;
    }
  if (accelerator_mods & GDK_HYPER_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);

      /* 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.
       */
795
      g_string_append (gstring, C_("keyboard label", "Hyper"));
796 797 798 799 800 801 802
      seen_mod = TRUE;
    }
  if (accelerator_mods & GDK_META_MASK)
    {
      if (seen_mod)
	g_string_append (gstring, klass->mod_separator);

803
#ifndef GDK_WINDOWING_QUARTZ
804 805 806 807 808
      /* 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.
       */
809
      g_string_append (gstring, C_("keyboard label", "Meta"));
810 811 812 813
#else
      /* Command key symbol U+2318 PLACE OF INTEREST SIGN */
      g_string_append (gstring, "\xe2\x8c\x98");
#endif
814 815
      seen_mod = TRUE;
    }
816 817 818 819
  if (seen_mod)
    g_string_append (gstring, klass->mod_separator);
  
  ch = gdk_keyval_to_unicode (accelerator_key);
820
  if (ch && ch < 0x80 && (g_unichar_isgraph (ch) || ch == ' '))
821 822 823 824
    {
      switch (ch)
	{
	case ' ':
825
	  g_string_append (gstring, C_("keyboard label", "Space"));
826 827
	  break;
	case '\\':
828
	  g_string_append (gstring, C_("keyboard label", "Backslash"));
829 830 831 832 833 834
	  break;
	default:
	  g_string_append_unichar (gstring, g_unichar_toupper (ch));
	  break;
	}
    }
835
  else if (!append_keyval_symbol (accelerator_key, gstring))
836 837
    {
      gchar *tmp;
838 839

      tmp = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
840
      if (tmp != NULL)
841
	{
842 843
	  if (tmp[0] != 0 && tmp[1] == 0)
	    g_string_append_c (gstring, g_ascii_toupper (tmp[0]));
844
	  else
845
	    {
846 847 848
	      const gchar *str;
              str = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
	      if (str == tmp)
849 850 851 852 853 854 855
		{
		  g_string_append (gstring, tmp);
		  substitute_underscores (gstring->str);
		}
	      else
		g_string_append (gstring, str);
	    }
856
	}
857 858 859 860 861
    }

  return g_string_free (gstring, FALSE);
}

862 863 864 865 866 867 868 869 870 871
/**
 * gtk_accel_label_refetch:
 * @accel_label: a #GtkAccelLabel.
 *
 * Recreates the string representing the accelerator keys.
 * This should not be needed since the string is automatically updated whenever
 * accelerators are added or removed from the associated widget.
 *
 * Returns: always returns %FALSE.
 */
Tim Janik's avatar
Tim Janik committed
872 873 874
gboolean
gtk_accel_label_refetch (GtkAccelLabel *accel_label)
{
875 876
  gboolean enable_accels;

Tim Janik's avatar
Tim Janik committed
877 878
  g_return_val_if_fail (GTK_IS_ACCEL_LABEL (accel_label), FALSE);

879
  if (accel_label->priv->accel_string)
880
    {
881 882
      g_free (accel_label->priv->accel_string);
      accel_label->priv->accel_string = NULL;
883
    }
884

885 886 887 888
  g_object_get (gtk_widget_get_settings (GTK_WIDGET (accel_label)),
                "gtk-enable-accels", &enable_accels,
                NULL);

889
  if (enable_accels && accel_label->priv->accel_closure)
Tim Janik's avatar
Tim Janik committed
890
    {
891
      GtkAccelKey *key = gtk_accel_group_find (accel_label->priv->accel_group, find_accel, accel_label->priv->accel_closure);
892 893

      if (key && key->accel_flags & GTK_ACCEL_VISIBLE)
Tim Janik's avatar
Tim Janik committed
894
	{
895 896 897 898 899 900 901
	  GtkAccelLabelClass *klass;
	  gchar *tmp;

	  klass = GTK_ACCEL_LABEL_GET_CLASS (accel_label);
	  tmp = _gtk_accel_label_class_get_accelerator_label (klass,
							      key->accel_key,
							      key->accel_mods);
902
	  accel_label->priv->accel_string = g_strconcat ("   ", tmp, NULL);
903
	  g_free (tmp);
Tim Janik's avatar
Tim Janik committed
904
	}
905 906
      if (!accel_label->priv->accel_string)
	accel_label->priv->accel_string = g_strdup ("-/-");
Tim Janik's avatar
Tim Janik committed
907
    }
908
  
909 910
  if (!accel_label->priv->accel_string)
    accel_label->priv->accel_string = g_strdup ("");
Tim Janik's avatar
Tim Janik committed
911 912 913 914 915

  gtk_widget_queue_resize (GTK_WIDGET (accel_label));

  return FALSE;
}