gtkshortcutlabel.c 11.9 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
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
/* gtkshortcutlabel.c
 *
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtkshortcutlabelprivate.h"
#include "gtklabel.h"
#include "gtkframe.h"
#include "gtkstylecontext.h"
#include "gtkprivate.h"
#include "gtkintl.h"

struct _GtkShortcutLabel
{
  GtkBox  parent_instance;
  gchar  *accelerator;
};

struct _GtkShortcutLabelClass
{
  GtkBoxClass parent_class;
};

G_DEFINE_TYPE (GtkShortcutLabel, gtk_shortcut_label, GTK_TYPE_BOX)

enum {
  PROP_0,
  PROP_ACCELERATOR,
  LAST_PROP
};

static GParamSpec *properties[LAST_PROP];

49 50 51 52 53 54 55 56 57 58 59 60 61 62
static gchar *
get_modifier_label (guint key)
{
  const gchar *subscript;
  const gchar *label;

  switch (key)
    {
    case GDK_KEY_Shift_L:
    case GDK_KEY_Control_L:
    case GDK_KEY_Alt_L:
    case GDK_KEY_Meta_L:
    case GDK_KEY_Super_L:
    case GDK_KEY_Hyper_L:
63 64 65 66 67 68
      /* Translators: This string is used to mark left/right variants of modifier
       * keys in the shortcut window (e.g. Control_L vs Control_R). Please keep
       * this string very short, ideally just a single character, since it will
       * be rendered as part of the key.
       */
      subscript = C_("keyboard side marker", "L");
69 70 71 72 73 74 75
      break;
    case GDK_KEY_Shift_R:
    case GDK_KEY_Control_R:
    case GDK_KEY_Alt_R:
    case GDK_KEY_Meta_R:
    case GDK_KEY_Super_R:
    case GDK_KEY_Hyper_R:
76 77 78 79 80 81
      /* Translators: This string is used to mark left/right variants of modifier
       * keys in the shortcut window (e.g. Control_L vs Control_R). Please keep
       * this string very short, ideally just a single character, since it will
       * be rendered as part of the key.
       */
      subscript = C_("keyboard side marker", "R");
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
      break;
    default:
      g_assert_not_reached ();
   }

 switch (key)
   {
   case GDK_KEY_Shift_L:   case GDK_KEY_Shift_R:
     label = C_("keyboard label", "Shift");
     break;
   case GDK_KEY_Control_L: case GDK_KEY_Control_R:
     label = C_("keyboard label", "Ctrl");
     break;
   case GDK_KEY_Alt_L:     case GDK_KEY_Alt_R:
     label = C_("keyboard label", "Alt");
     break;
   case GDK_KEY_Meta_L:    case GDK_KEY_Meta_R:
     label = C_("keyboard label", "Meta");
     break;
   case GDK_KEY_Super_L:   case GDK_KEY_Super_R:
     label = C_("keyboard label", "Super");
     break;
   case GDK_KEY_Hyper_L:   case GDK_KEY_Hyper_R:
     label = C_("keyboard label", "Hyper");
     break;
    default:
      g_assert_not_reached ();
   }

111
  return g_strdup_printf ("%s <small><b>%s</b></small>", label, subscript);
112 113
}

Matthias Clasen's avatar
Matthias Clasen committed
114 115 116 117
static gchar **
get_labels (guint key, GdkModifierType modifier, guint *n_mods)
{
  const gchar *labels[16];
118
  GList *freeme = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
119 120 121 122
  gchar key_label[6];
  gchar *tmp;
  gunichar ch;
  gint i = 0;
123
  gchar **retval;
Matthias Clasen's avatar
Matthias Clasen committed
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

  if (modifier & GDK_SHIFT_MASK)
    labels[i++] = C_("keyboard label", "Shift");
  if (modifier & GDK_CONTROL_MASK)
    labels[i++] = C_("keyboard label", "Ctrl");
  if (modifier & GDK_MOD1_MASK)
    labels[i++] = C_("keyboard label", "Alt");
  if (modifier & GDK_MOD2_MASK)
    labels[i++] = "Mod2";
  if (modifier & GDK_MOD3_MASK)
    labels[i++] = "Mod3";
  if (modifier & GDK_MOD4_MASK)
    labels[i++] = "Mod4";
  if (modifier & GDK_MOD5_MASK)
    labels[i++] = "Mod5";
  if (modifier & GDK_SUPER_MASK)
    labels[i++] = C_("keyboard label", "Super");
  if (modifier & GDK_HYPER_MASK)
    labels[i++] = C_("keyboard label", "Hyper");
  if (modifier & GDK_META_MASK)
    labels[i++] = C_("keyboard label", "Meta");

  *n_mods = i;

  ch = gdk_keyval_to_unicode (key);
  if (ch && ch < 0x80 && g_unichar_isgraph (ch))
    {
      switch (ch)
        {
        case '\\':
          labels[i++] = C_("keyboard label", "Backslash");
          break;
        default:
          memset (key_label, 0, 6);
          g_unichar_to_utf8 (g_unichar_toupper (ch), key_label);
          labels[i++] = key_label;
          break;
        }
    }
  else
    {
      switch (key)
        {
167 168 169 170 171 172 173 174 175
        case GDK_KEY_Shift_L:   case GDK_KEY_Shift_R:
        case GDK_KEY_Control_L: case GDK_KEY_Control_R:
        case GDK_KEY_Alt_L:     case GDK_KEY_Alt_R:
        case GDK_KEY_Meta_L:    case GDK_KEY_Meta_R:
        case GDK_KEY_Super_L:   case GDK_KEY_Super_R:
        case GDK_KEY_Hyper_L:   case GDK_KEY_Hyper_R:
          freeme = g_list_prepend (freeme, get_modifier_label (key));
          labels[i++] = (const gchar*)freeme->data;
           break;
Matthias Clasen's avatar
Matthias Clasen committed
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
        case GDK_KEY_Left:
          labels[i++] = "\xe2\x86\x90";
          break;
        case GDK_KEY_Up:
          labels[i++] = "\xe2\x86\x91";
          break;
        case GDK_KEY_Right:
          labels[i++] = "\xe2\x86\x92";
          break;
        case GDK_KEY_Down:
          labels[i++] = "\xe2\x86\x93";
          break;
        case GDK_KEY_space:
          labels[i++] = "\xe2\x90\xa3";
          break;
        case GDK_KEY_Return:
          labels[i++] = "\xe2\x8f\x8e";
          break;
        case GDK_KEY_Page_Up:
          labels[i++] = C_("keyboard label", "Page_Up");
          break;
        case GDK_KEY_Page_Down:
          labels[i++] = C_("keyboard label", "Page_Down");
          break;
        default:
          tmp = gdk_keyval_name (gdk_keyval_to_lower (key));
          if (tmp != NULL)
            {
              if (tmp[0] != 0 && tmp[1] == 0)
                {
                  key_label[0] = g_ascii_toupper (tmp[0]);
                  key_label[1] = '\0';
                  labels[i++] = key_label;
                }
              else
                {
                  labels[i++] = g_dpgettext2 (GETTEXT_PACKAGE, "keyboard label", tmp);
                }
            }
        }
    }

  labels[i] = NULL;

220 221 222 223 224
  retval = g_strdupv ((gchar **)labels);

  g_list_free_full (freeme, g_free);

  return retval;
Matthias Clasen's avatar
Matthias Clasen committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238
}

static GtkWidget *
dim_label (const gchar *text)
{
  GtkWidget *label;

  label = gtk_label_new (text);
  gtk_widget_show (label);
  gtk_style_context_add_class (gtk_widget_get_style_context (label), "dim-label");

  return label;
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static void
display_shortcut (GtkContainer    *self,
                  guint            key,
                  GdkModifierType  modifier)
{
  gchar **keys = NULL;
  gint i;
  guint n_mods;

  keys = get_labels (key, modifier, &n_mods);
  for (i = 0; keys[i]; i++)
    {
      GtkWidget *disp;

      if (i > 0)
        gtk_container_add (self, dim_label ("+"));

256
      disp = gtk_label_new (keys[i]);
257
      if (i < n_mods)
258
        gtk_widget_set_size_request (disp, 50, -1);
259

260
      gtk_style_context_add_class (gtk_widget_get_style_context (disp), "keycap");
261 262
      gtk_label_set_use_markup (GTK_LABEL (disp), TRUE);

263
      gtk_widget_show (disp);
264
      gtk_container_add (self, disp);
265 266 267 268
    }
  g_strfreev (keys);
}

269
static gboolean
270 271
parse_combination (GtkShortcutLabel *self,
                   const gchar      *str)
Matthias Clasen's avatar
Matthias Clasen committed
272
{
273 274
  gchar **accels;
  gint k;
Matthias Clasen's avatar
Matthias Clasen committed
275 276
  GdkModifierType modifier = 0;
  guint key = 0;
277 278
  gboolean retval = TRUE;

279
  accels = g_strsplit (str, "&", 0);
280 281 282 283 284 285 286 287
  for (k = 0; accels[k]; k++)
    {
      gtk_accelerator_parse (accels[k], &key, &modifier);
      if (key == 0 && modifier == 0)
        {
          retval = FALSE;
          break;
        }
288 289 290
      if (k > 0)
        gtk_container_add (GTK_CONTAINER (self), dim_label ("+"));

291 292 293 294 295 296 297
      display_shortcut (GTK_CONTAINER (self), key, modifier);
    }
  g_strfreev (accels);

  return retval;
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
static gboolean
parse_sequence (GtkShortcutLabel *self,
                const gchar      *str)
{
  gchar **accels;
  gint k;
  gboolean retval = TRUE;

  accels = g_strsplit (str, "+", 0);
  for (k = 0; accels[k]; k++)
    {
      if (!parse_combination (self, accels[k]))
        {
          retval = FALSE;
          break;
        }
    }

  g_strfreev (accels);

  return retval;
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
static gboolean
parse_range (GtkShortcutLabel *self,
             const gchar      *str)
{
  gchar *dots;

  dots = strstr (str, "...");
  if (!dots)
    return parse_sequence (self, str);

  dots[0] = '\0';
  if (!parse_sequence (self, str))
    return FALSE;

  gtk_container_add (GTK_CONTAINER (self), dim_label ("⋯"));
336

337 338 339 340 341 342 343 344 345 346 347
  if (!parse_sequence (self, dots + 3))
    return FALSE;

  return TRUE;
}

static void
gtk_shortcut_label_rebuild (GtkShortcutLabel *self)
{
  gchar **accels;
  gint k;
Matthias Clasen's avatar
Matthias Clasen committed
348 349 350 351 352 353 354 355 356

  gtk_container_foreach (GTK_CONTAINER (self), (GtkCallback)gtk_widget_destroy, NULL);

  if (self->accelerator == NULL)
    return;

  accels = g_strsplit (self->accelerator, " ", 0);
  for (k = 0; accels[k]; k++)
    {
357 358
      if (k > 0)
        gtk_container_add (GTK_CONTAINER (self), dim_label ("/"));
359

360
      if (!parse_range (self, accels[k]))
Matthias Clasen's avatar
Matthias Clasen committed
361
        {
362
          g_warning ("Failed to parse %s, part of accelerator '%s'", accels[k], self->accelerator);
363
          break;
Matthias Clasen's avatar
Matthias Clasen committed
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 460 461 462 463 464 465 466 467 468 469
        }
    }
  g_strfreev (accels);
}

static void
gtk_shortcut_label_finalize (GObject *object)
{
  GtkShortcutLabel *self = (GtkShortcutLabel *)object;

  g_free (self->accelerator);

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

static void
gtk_shortcut_label_get_property (GObject    *object,
                                 guint       prop_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  GtkShortcutLabel *self = GTK_SHORTCUT_LABEL (object);

  switch (prop_id)
    {
    case PROP_ACCELERATOR:
      g_value_set_string (value, gtk_shortcut_label_get_accelerator (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_shortcut_label_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  GtkShortcutLabel *self = GTK_SHORTCUT_LABEL (object);

  switch (prop_id)
    {
    case PROP_ACCELERATOR:
      gtk_shortcut_label_set_accelerator (self, g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_shortcut_label_class_init (GtkShortcutLabelClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gtk_shortcut_label_finalize;
  object_class->get_property = gtk_shortcut_label_get_property;
  object_class->set_property = gtk_shortcut_label_set_property;

  properties[PROP_ACCELERATOR] =
    g_param_spec_string ("accelerator", P_("Accelerator"), P_("Accelerator"),
                         NULL,
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, LAST_PROP, properties);
}

static void
gtk_shortcut_label_init (GtkShortcutLabel *self)
{
  gtk_box_set_spacing (GTK_BOX (self), 6);
}

GtkWidget *
gtk_shortcut_label_new (const gchar *accelerator)
{
  return g_object_new (GTK_TYPE_SHORTCUT_LABEL,
                       "accelerator", accelerator,
                       NULL);
}

const gchar *
gtk_shortcut_label_get_accelerator (GtkShortcutLabel *self)
{
  g_return_val_if_fail (GTK_IS_SHORTCUT_LABEL (self), NULL);

  return self->accelerator;
}

void
gtk_shortcut_label_set_accelerator (GtkShortcutLabel *self,
                                    const gchar      *accelerator)
{
  g_return_if_fail (GTK_IS_SHORTCUT_LABEL (self));

  if (g_strcmp0 (accelerator, self->accelerator) != 0)
    {
      g_free (self->accelerator);
      self->accelerator = g_strdup (accelerator);
      gtk_shortcut_label_rebuild (self);
      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACCELERATOR]);
    }
}