Commit 1dfbae1a authored by Matthias Clasen's avatar Matthias Clasen

Add GtkShortcutsWindow

This is a toplevel window that is tailored towards showing
help for shortcuts in an application. The implementation closely
follows this design: https://wiki.gnome.org/Design/OS/HelpOverlay

This implementation is inspired by earlier work in gnome-builder,
thanks to Christian Hergert.

https://bugzilla.gnome.org/show_bug.cgi?id=756428
parent f254a4b6
......@@ -470,7 +470,10 @@ HTML_IMAGES = \
$(srcdir)/images/getting-started-app10.png \
$(srcdir)/images/exampleapp.png \
$(srcdir)/images/flow-box.png \
$(srcdir)/images/inspector.png
$(srcdir)/images/inspector.png \
$(srcdir)/images/gedit-shortcuts.png \
$(srcdir)/images/clocks-shortcuts.png \
$(srcdir)/images/builder-shortcuts.png
if ENABLE_DOC_CROSS_REFERENCES
# Extra options to supply to gtkdoc-fixref
......
......@@ -239,6 +239,15 @@
<xi:include href="xml/gtkpagesetupunixdialog.xml" />
</chapter>
<chapter id="ShortcutsOverview">
<title>Shortcuts Overview</title>
<xi:include href="xml/gtkshortcutswindow.xml" />
<xi:include href="xml/gtkshortcutssection.xml" />
<xi:include href="xml/gtkshortcutsgroup.xml" />
<xi:include href="xml/gtkshortcutsshortcut.xml" />
<xi:include href="xml/gtkshortcutsgesture.xml" />
</chapter>
<chapter id="MiscObjects">
<title>Miscellaneous</title>
<xi:include href="xml/gtkadjustment.xml" />
......
......@@ -8422,3 +8422,74 @@ GTK_IS_GL_AREA_CLASS
<SUBSECTION Private>
gtk_gl_area_get_type
</SECTION>
<SECTION>
<FILE>gtkshortcutswindow</FILE>
GtkShortcutsWindow
<SUBSECTION Standard>
GTK_TYPE_SHORTCUTS_WINDOW
GTK_SHORTCUTS_WINDOW
GTK_IS_SHORTCUTS_WINDOW
GTK_SHORTCUTS_WINDOW_CLASS
GTK_IS_SHORTCUTS_WINDOW_CLASS
GTK_GET_SHORTCUTS_WINDOW_CLASS
<SUBSECTION Private>
gtk_shortcuts_window_get_type
</SECTION>
<SECTION>
<FILE>gtkshortcutssection</FILE>
GtkShortcutsSection
<SUBSECTION Standard>
GTK_TYPE_SHORTCUTS_SECTION
GTK_SHORTCUTS_SECTION
GTK_IS_SHORTCUTS_SECTION
GTK_SHORTCUTS_SECTION_CLASS
GTK_IS_SHORTCUTS_SECTION_CLASS
GTK_GET_SHORTCUTS_SECTION_CLASS
<SUBSECTION Private>
gtk_shortcuts_section_get_type
</SECTION>
<SECTION>
<FILE>gtkshortcutsgroup</FILE>
GtkShortcutsGroup
<SUBSECTION Standard>
GTK_TYPE_SHORTCUTS_GROUP
GTK_SHORTCUTS_GROUP
GTK_IS_SHORTCUTS_GROUP
GTK_SHORTCUTS_GROUP_CLASS
GTK_IS_SHORTCUTS_GROUP_CLASS
GTK_GET_SHORTCUTS_GROUP_CLASS
<SUBSECTION Private>
gtk_shortcuts_group_get_type
</SECTION>
<SECTION>
<FILE>gtkshortcutsshortcut</FILE>
GtkShortcutsShortcut
<SUBSECTION Standard>
GTK_TYPE_SHORTCUTS_SHORTCUT
GTK_SHORTCUTS_SHORTCUT
GTK_IS_SHORTCUTS_SHORTCUT
GTK_SHORTCUTS_SHORTCUT_CLASS
GTK_IS_SHORTCUTS_SHORTCUT_CLASS
GTK_GET_SHORTCUTS_SHORTCUT_CLASS
<SUBSECTION Private>
<SUBSECTION Private>
gtk_shortcuts_shortcut_get_type
</SECTION>
<SECTION>
<FILE>gtkshortcutsgesture</FILE>
GtkShortcutsGesture
<SUBSECTION Standard>
GTK_TYPE_SHORTCUTS_GESTURE
GTK_SHORTCUTS_GESTURE
GTK_IS_SHORTCUTS_GESTURE
GTK_SHORTCUTS_GESTURE_CLASS
GTK_IS_SHORTCUTS_GESTURE_CLASS
GTK_GET_SHORTCUTS_GESTURE_CLASS
<SUBSECTION Private>
gtk_shortcuts_gesture_get_type
</SECTION>
......@@ -173,12 +173,17 @@ gtk_separator_get_type
gtk_separator_menu_item_get_type
gtk_separator_tool_item_get_type
gtk_settings_get_type
gtk_stack_sidebar_get_type
gtk_shortcuts_window_get_type
gtk_shortcuts_section_get_type
gtk_shortcuts_group_get_type
gtk_shortcuts_shortcut_get_type
gtk_shortcuts_gesture_get_type
gtk_size_group_get_type
@ENABLE_ON_X11@gtk_socket_get_type
gtk_spin_button_get_type
gtk_spinner_get_type
gtk_stack_get_type
gtk_stack_sidebar_get_type
gtk_stack_switcher_get_type
gtk_statusbar_get_type
gtk_status_icon_get_type
......
......@@ -269,6 +269,11 @@ gtk_public_h_sources = \
gtkseparatormenuitem.h \
gtkseparatortoolitem.h \
gtksettings.h \
gtkshortcutsgesture.h \
gtkshortcutsgroup.h \
gtkshortcutssection.h \
gtkshortcutsshortcut.h \
gtkshortcutswindow.h \
gtkshow.h \
gtkstacksidebar.h \
gtksizegroup.h \
......@@ -505,6 +510,9 @@ gtk_private_h_sources = \
gtkselectionprivate.h \
gtksidebarrowprivate.h \
gtksettingsprivate.h \
gtkshortcutsgestureprivate.h \
gtkshortcutlabelprivate.h \
gtkshortcutsshortcutprivate.h \
gtksizegroup-private.h \
gtksizerequestcacheprivate.h \
gtksocketprivate.h \
......@@ -806,6 +814,12 @@ gtk_base_c_sources = \
gtkseparatormenuitem.c \
gtkseparatortoolitem.c \
gtksettings.c \
gtkshortcutsgesture.c \
gtkshortcutsgroup.c \
gtkshortcutlabel.c \
gtkshortcutsshortcut.c \
gtkshortcutssection.c \
gtkshortcutswindow.c \
gtksidebarrow.c \
gtksizegroup.c \
gtksizerequest.c \
......
......@@ -183,6 +183,11 @@
#include <gtk/gtkseparatormenuitem.h>
#include <gtk/gtkseparatortoolitem.h>
#include <gtk/gtksettings.h>
#include <gtk/gtkshortcutsgesture.h>
#include <gtk/gtkshortcutsgroup.h>
#include <gtk/gtkshortcutssection.h>
#include <gtk/gtkshortcutsshortcut.h>
#include <gtk/gtkshortcutswindow.h>
#include <gtk/gtkshow.h>
#include <gtk/gtkstacksidebar.h>
#include <gtk/gtksizegroup.h>
......
/* 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];
static gchar **
get_labels (guint key, GdkModifierType modifier, guint *n_mods)
{
const gchar *labels[16];
gchar key_label[6];
gchar *tmp;
gunichar ch;
gint i = 0;
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)
{
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;
return g_strdupv ((gchar **)labels);
}
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;
}
static void
gtk_shortcut_label_rebuild (GtkShortcutLabel *self)
{
gchar **accels = NULL;
gchar **keys = NULL;
GdkModifierType modifier = 0;
guint key = 0;
guint i, k;
guint n_mods;
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++)
{
gtk_accelerator_parse (accels[k], &key, &modifier);
if ((key == 0) && (modifier == 0))
{
g_warning ("Failed to parse accelerator '%s'", self->accelerator);
goto out;
}
if (k > 0)
gtk_container_add (GTK_CONTAINER (self), dim_label ("/"));
keys = get_labels (key, modifier, &n_mods);
for (i = 0; keys[i]; i++)
{
GtkWidget *frame;
GtkWidget *disp;
if (i > 0)
gtk_container_add (GTK_CONTAINER (self), dim_label ("+"));
frame = gtk_frame_new (NULL);
gtk_widget_show (frame);
gtk_container_add (GTK_CONTAINER (self), frame);
if (i < n_mods)
gtk_widget_set_size_request (frame, 50, -1);
disp = gtk_label_new (keys[i]);
gtk_widget_show (disp);
gtk_container_add (GTK_CONTAINER (frame), disp);
}
g_strfreev (keys);
}
out:
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]);
}
}
/* gtkshortcutlabelprivate.h
*
* 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/>.
*/
#ifndef __GTK_SHORTCUT_LABEL_H__
#define __GTK_SHORTCUT_LABEL_H__
#include <gtk/gtkbox.h>
G_BEGIN_DECLS
#define GTK_TYPE_SHORTCUT_LABEL (gtk_shortcut_label_get_type())
#define GTK_SHORTCUT_LABEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SHORTCUT_LABEL, GtkShortcutLabel))
#define GTK_SHORTCUT_LABEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_SHORTCUT_LABEL, GtkShortcutLabelClass))
#define GTK_IS_SHORTCUT_LABEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SHORTCUT_LABEL))
#define GTK_IS_SHORTCUT_LABEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_SHORTCUT_LABEL))
#define GTK_SHORTCUT_LABEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_SHORTCUT_LABEL, GtkShortcutLabelClass))
typedef struct _GtkShortcutLabel GtkShortcutLabel;
typedef struct _GtkShortcutLabelClass GtkShortcutLabelClass;
GType gtk_shortcut_label_get_type (void) G_GNUC_CONST;
GtkWidget *gtk_shortcut_label_new (const gchar *accelerator);
const gchar *gtk_shortcut_label_get_accelerator (GtkShortcutLabel *self);
void gtk_shortcut_label_set_accelerator (GtkShortcutLabel *self,
const gchar *accelerator);
G_END_DECLS
#endif /* __GTK_SHORTCUT_LABEL_H__ */
/* gtkshortcutsgesture.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 "gtkshortcutsgesture.h"
#include "gtkimage.h"
#include "gtklabel.h"
#include "gtksizegroup.h"
#include "gtkorientable.h"
#include "gtkstylecontext.h"
#include "gtkprivate.h"
#include "gtkintl.h"
/**
* SECTION:gtkshortcutsgesture
* @Title: GtkShortcutsGesture
* @Short_description: Represents a gesture in a GtkShortcutsWindow
*
* A GtkShortcutsGesture represents a single gesture with an image
* an a short text.
*
* This widget is only meant to be used with #GtkShortcutsWindow.
*/
struct _GtkShortcutsGesture
{
GtkBox parent_instance;
GtkImage *image;
GtkLabel *title;
GtkLabel *subtitle;
GtkBox *title_box;
GtkSizeGroup *title_size_group;
GtkSizeGroup *icon_size_group;
};
struct _GtkShortcutsGestureClass
{
GtkBoxClass parent_class;
};
G_DEFINE_TYPE (GtkShortcutsGesture, gtk_shortcuts_gesture, GTK_TYPE_BOX)
enum {
PROP_0,
PROP_ICON,
PROP_TITLE,
PROP_SUBTITLE,
PROP_ICON_SIZE_GROUP,
PROP_TITLE_SIZE_GROUP,
LAST_PROP
};
static GParamSpec *properties[LAST_PROP];
static void
gtk_shortcuts_gesture_set_title_size_group (GtkShortcutsGesture *self,
GtkSizeGroup *group)
{
if (self->title_size_group)
gtk_size_group_remove_widget (self->title_size_group, GTK_WIDGET (self->title_box));
if (group)
gtk_size_group_add_widget (group, GTK_WIDGET (self->title_box));
g_set_object (&self->title_size_group, group);
}
static void
gtk_shortcuts_gesture_set_icon_size_group (GtkShortcutsGesture *self,
GtkSizeGroup *group)
{
if (self->icon_size_group)
gtk_size_group_remove_widget (self->icon_size_group, GTK_WIDGET (self->image));
if (group)
gtk_size_group_add_widget (group, GTK_WIDGET (self->image));
g_set_object (&self->icon_size_group, group);
}
static void
gtk_shortcuts_gesture_set_icon (GtkShortcutsGesture *self,
GIcon *gicon)
{
gtk_image_set_from_gicon (self->image, gicon, GTK_ICON_SIZE_DIALOG);
}
static void
gtk_shortcuts_gesture_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkShortcutsGesture *self = GTK_SHORTCUTS_GESTURE (object);
switch (prop_id)
{
case PROP_ICON:
{
GIcon *icon;
gtk_image_get_gicon (self->image, &icon, NULL);
g_value_set_object (value, icon);
}
break;
case PROP_TITLE:
g_value_set_string (value, gtk_label_get_label (self->title));
break;
case PROP_SUBTITLE:
g_value_set_string (value, gtk_label_get_label (self->subtitle));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gtk_shortcuts_gesture_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkShortcutsGesture *self = GTK_SHORTCUTS_GESTURE (object);
switch (prop_id)
{
case PROP_ICON:
gtk_shortcuts_gesture_set_icon (self, g_value_get_object (value));
break;
case PROP_TITLE:
gtk_label_set_label (self->title, g_value_get_string (value));
break;
case PROP_SUBTITLE:
gtk_label_set_label (self->subtitle, g_value_get_string (value));
break;
case PROP_TITLE_SIZE_GROUP:
gtk_shortcuts_gesture_set_title_size_group (self, GTK_SIZE_GROUP (g_value_get_object (value)));
break;
case PROP_ICON_SIZE_GROUP:
gtk_shortcuts_gesture_set_icon_size_group (self, GTK_SIZE_GROUP (g_value_get_object (value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gtk_shortcuts_gesture_finalize (GObject *object)
{
GtkShortcutsGesture *self = GTK_SHORTCUTS_GESTURE (object);
g_clear_object (&self->title_size_group);
g_clear_object (&self->icon_size_group);
G_OBJECT_CLASS (gtk_shortcuts_gesture_parent_class)->finalize (object);
}
static void
gtk_shortcuts_gesture_add (GtkContainer *container,
GtkWidget *widget)
{
g_warning ("Can't add children to %s", G_OBJECT_TYPE_NAME (container));
}
static GType
gtk_shortcuts_gesture_child_type (GtkContainer *container)
{
return G_TYPE_NONE;
}
static void
gtk_shortcuts_gesture_class_init (GtkShortcutsGestureClass *klass)
{
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gtk_shortcuts_gesture_finalize;
object_class->get_property = gtk_shortcuts_gesture_get_property;
object_class->set_property = gtk_shortcuts_gesture_set_property;
container_class->add = gtk_shortcuts_gesture_add;
container_class->child_type = gtk_shortcuts_gesture_child_type;