2015-10-11 20:39:18 +00:00
|
|
|
/* gtkshortcutsgroup.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 "gtkshortcutsgroup.h"
|
|
|
|
|
2018-02-08 05:15:10 +00:00
|
|
|
#include "gtkbox.h"
|
2020-05-09 01:08:20 +00:00
|
|
|
#include "gtkbuildable.h"
|
2018-02-08 05:15:10 +00:00
|
|
|
#include "gtkintl.h"
|
2015-10-11 20:39:18 +00:00
|
|
|
#include "gtklabel.h"
|
|
|
|
#include "gtkorientable.h"
|
|
|
|
#include "gtkprivate.h"
|
2018-02-08 05:15:10 +00:00
|
|
|
#include "gtkshortcutsshortcut.h"
|
|
|
|
#include "gtksizegroup.h"
|
2015-10-11 20:39:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gtkshortcutsgroup
|
|
|
|
* @Title: GtkShortcutsGroup
|
|
|
|
* @Short_description: Represents a group of shortcuts in a GtkShortcutsWindow
|
|
|
|
*
|
|
|
|
* A GtkShortcutsGroup represents a group of related keyboard shortcuts
|
|
|
|
* or gestures. The group has a title. It may optionally be associated with
|
|
|
|
* a view of the application, which can be used to show only relevant shortcuts
|
|
|
|
* depending on the application context.
|
|
|
|
*
|
|
|
|
* This widget is only meant to be used with #GtkShortcutsWindow.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct _GtkShortcutsGroup
|
|
|
|
{
|
|
|
|
GtkBox parent_instance;
|
|
|
|
|
|
|
|
GtkLabel *title;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *view;
|
2015-10-11 20:39:18 +00:00
|
|
|
guint height;
|
2015-11-22 19:16:12 +00:00
|
|
|
|
|
|
|
GtkSizeGroup *accel_size_group;
|
|
|
|
GtkSizeGroup *title_size_group;
|
2015-10-11 20:39:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkShortcutsGroupClass
|
|
|
|
{
|
|
|
|
GtkBoxClass parent_class;
|
|
|
|
};
|
|
|
|
|
2020-05-09 01:08:20 +00:00
|
|
|
static void gtk_shortcuts_group_buildable_iface_init (GtkBuildableIface *iface);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkShortcutsGroup, gtk_shortcuts_group, GTK_TYPE_BOX,
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
|
|
|
|
gtk_shortcuts_group_buildable_iface_init))
|
2015-10-11 20:39:18 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_TITLE,
|
|
|
|
PROP_VIEW,
|
|
|
|
PROP_ACCEL_SIZE_GROUP,
|
|
|
|
PROP_TITLE_SIZE_GROUP,
|
|
|
|
PROP_HEIGHT,
|
|
|
|
LAST_PROP
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[LAST_PROP];
|
|
|
|
|
2015-11-22 19:16:12 +00:00
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_apply_accel_size_group (GtkShortcutsGroup *group,
|
|
|
|
GtkWidget *child)
|
|
|
|
{
|
|
|
|
if (GTK_IS_SHORTCUTS_SHORTCUT (child))
|
|
|
|
g_object_set (child, "accel-size-group", group->accel_size_group, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_apply_title_size_group (GtkShortcutsGroup *group,
|
|
|
|
GtkWidget *child)
|
|
|
|
{
|
|
|
|
if (GTK_IS_SHORTCUTS_SHORTCUT (child))
|
|
|
|
g_object_set (child, "title-size-group", group->title_size_group, NULL);
|
|
|
|
}
|
|
|
|
|
2015-10-11 20:39:18 +00:00
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_set_accel_size_group (GtkShortcutsGroup *group,
|
|
|
|
GtkSizeGroup *size_group)
|
|
|
|
{
|
2020-05-09 12:26:52 +00:00
|
|
|
GtkWidget *child;
|
2015-10-11 20:39:18 +00:00
|
|
|
|
2015-11-22 19:16:12 +00:00
|
|
|
g_set_object (&group->accel_size_group, size_group);
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
for (child = gtk_widget_get_first_child (GTK_WIDGET (group));
|
|
|
|
child != NULL;
|
|
|
|
child = gtk_widget_get_next_sibling (child))
|
|
|
|
gtk_shortcuts_group_apply_accel_size_group (group, child);
|
2015-10-11 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_set_title_size_group (GtkShortcutsGroup *group,
|
|
|
|
GtkSizeGroup *size_group)
|
|
|
|
{
|
2020-05-09 12:26:52 +00:00
|
|
|
GtkWidget *child;
|
2015-10-11 20:39:18 +00:00
|
|
|
|
2015-11-22 19:16:12 +00:00
|
|
|
g_set_object (&group->title_size_group, size_group);
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
for (child = gtk_widget_get_first_child (GTK_WIDGET (group));
|
|
|
|
child != NULL;
|
|
|
|
child = gtk_widget_get_next_sibling (child))
|
|
|
|
gtk_shortcuts_group_apply_title_size_group (group, child);
|
2015-10-11 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_shortcuts_group_get_height (GtkShortcutsGroup *group)
|
|
|
|
{
|
2020-05-09 12:26:52 +00:00
|
|
|
GtkWidget *child;
|
2015-10-11 20:39:18 +00:00
|
|
|
guint height;
|
|
|
|
|
|
|
|
height = 1;
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
for (child = gtk_widget_get_first_child (GTK_WIDGET (group));
|
|
|
|
child != NULL;
|
|
|
|
child = gtk_widget_get_next_sibling (child))
|
2015-10-11 20:39:18 +00:00
|
|
|
{
|
2015-11-15 03:34:19 +00:00
|
|
|
if (!gtk_widget_get_visible (child))
|
|
|
|
continue;
|
|
|
|
else if (GTK_IS_SHORTCUTS_SHORTCUT (child))
|
2015-10-11 20:39:18 +00:00
|
|
|
height += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2020-05-09 01:08:20 +00:00
|
|
|
static GtkBuildableIface *parent_buildable_iface;
|
|
|
|
|
2015-10-11 20:39:18 +00:00
|
|
|
static void
|
2020-05-09 01:08:20 +00:00
|
|
|
gtk_shortcuts_group_buildable_add_child (GtkBuildable *buildable,
|
|
|
|
GtkBuilder *builder,
|
|
|
|
GObject *child,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *type)
|
2015-10-11 20:39:18 +00:00
|
|
|
{
|
2020-05-09 01:08:20 +00:00
|
|
|
if (GTK_IS_SHORTCUTS_SHORTCUT (child))
|
2015-11-22 19:16:12 +00:00
|
|
|
{
|
2020-05-09 01:08:20 +00:00
|
|
|
gtk_box_append (GTK_BOX (buildable), GTK_WIDGET (child));
|
|
|
|
gtk_shortcuts_group_apply_accel_size_group (GTK_SHORTCUTS_GROUP (buildable), GTK_WIDGET (child));
|
|
|
|
gtk_shortcuts_group_apply_title_size_group (GTK_SHORTCUTS_GROUP (buildable), GTK_WIDGET (child));
|
2015-11-22 19:16:12 +00:00
|
|
|
}
|
2015-10-11 20:39:18 +00:00
|
|
|
else
|
2020-05-09 01:08:20 +00:00
|
|
|
parent_buildable_iface->add_child (buildable, builder, child, type);
|
2015-11-21 03:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-09 01:08:20 +00:00
|
|
|
gtk_shortcuts_group_buildable_iface_init (GtkBuildableIface *iface)
|
2015-11-21 03:58:06 +00:00
|
|
|
{
|
2020-05-09 01:08:20 +00:00
|
|
|
parent_buildable_iface = g_type_interface_peek_parent (iface);
|
2015-11-21 03:58:06 +00:00
|
|
|
|
2020-05-09 01:08:20 +00:00
|
|
|
iface->add_child = gtk_shortcuts_group_buildable_add_child;
|
2015-11-21 03:58:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 20:39:18 +00:00
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkShortcutsGroup *self = GTK_SHORTCUTS_GROUP (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_TITLE:
|
|
|
|
g_value_set_string (value, gtk_label_get_label (self->title));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_VIEW:
|
|
|
|
g_value_set_string (value, self->view);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HEIGHT:
|
|
|
|
g_value_set_uint (value, gtk_shortcuts_group_get_height (self));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 03:34:19 +00:00
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_direction_changed (GtkWidget *widget,
|
|
|
|
GtkTextDirection previous_dir)
|
|
|
|
{
|
|
|
|
GTK_WIDGET_CLASS (gtk_shortcuts_group_parent_class)->direction_changed (widget, previous_dir);
|
|
|
|
g_object_notify (G_OBJECT (widget), "height");
|
|
|
|
}
|
|
|
|
|
2015-10-11 20:39:18 +00:00
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkShortcutsGroup *self = GTK_SHORTCUTS_GROUP (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_TITLE:
|
|
|
|
gtk_label_set_label (self->title, g_value_get_string (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_VIEW:
|
|
|
|
g_free (self->view);
|
|
|
|
self->view = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ACCEL_SIZE_GROUP:
|
|
|
|
gtk_shortcuts_group_set_accel_size_group (self, GTK_SIZE_GROUP (g_value_get_object (value)));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TITLE_SIZE_GROUP:
|
|
|
|
gtk_shortcuts_group_set_title_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_group_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkShortcutsGroup *self = GTK_SHORTCUTS_GROUP (object);
|
|
|
|
|
|
|
|
g_free (self->view);
|
2015-11-22 19:16:12 +00:00
|
|
|
g_set_object (&self->accel_size_group, NULL);
|
|
|
|
g_set_object (&self->title_size_group, NULL);
|
2015-10-11 20:39:18 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_shortcuts_group_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2016-10-12 15:16:57 +00:00
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GtkShortcutsGroup *self = GTK_SHORTCUTS_GROUP (object);
|
|
|
|
|
2020-05-09 14:33:02 +00:00
|
|
|
g_clear_pointer ((GtkWidget **)&self->title, gtk_widget_unparent);
|
2016-10-12 15:16:57 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_shortcuts_group_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2015-10-11 20:39:18 +00:00
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_class_init (GtkShortcutsGroupClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2015-11-15 03:34:19 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2015-10-11 20:39:18 +00:00
|
|
|
|
|
|
|
object_class->finalize = gtk_shortcuts_group_finalize;
|
|
|
|
object_class->get_property = gtk_shortcuts_group_get_property;
|
|
|
|
object_class->set_property = gtk_shortcuts_group_set_property;
|
2016-10-12 15:16:57 +00:00
|
|
|
object_class->dispose = gtk_shortcuts_group_dispose;
|
2015-10-11 20:39:18 +00:00
|
|
|
|
2015-11-15 03:34:19 +00:00
|
|
|
widget_class->direction_changed = gtk_shortcuts_group_direction_changed;
|
2015-10-11 20:39:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkShortcutsGroup:title:
|
|
|
|
*
|
|
|
|
* The title for this group of shortcuts.
|
|
|
|
*/
|
|
|
|
properties[PROP_TITLE] =
|
|
|
|
g_param_spec_string ("title", P_("Title"), P_("Title"),
|
2015-10-27 12:48:08 +00:00
|
|
|
"",
|
2015-10-11 20:39:18 +00:00
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkShortcutsGroup:view:
|
|
|
|
*
|
|
|
|
* An optional view that the shortcuts in this group are relevant for.
|
|
|
|
* The group will be hidden if the #GtkShortcutsWindow:view-name property
|
|
|
|
* does not match the view of this group.
|
|
|
|
*
|
|
|
|
* Set this to %NULL to make the group always visible.
|
|
|
|
*/
|
|
|
|
properties[PROP_VIEW] =
|
|
|
|
g_param_spec_string ("view", P_("View"), P_("View"),
|
|
|
|
NULL,
|
|
|
|
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkShortcutsGroup:accel-size-group:
|
|
|
|
*
|
|
|
|
* The size group for the accelerator portion of shortcuts in this group.
|
|
|
|
*
|
2020-09-12 16:01:04 +00:00
|
|
|
* This is used internally by GTK, and must not be modified by applications.
|
2015-10-11 20:39:18 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_ACCEL_SIZE_GROUP] =
|
|
|
|
g_param_spec_object ("accel-size-group",
|
|
|
|
P_("Accelerator Size Group"),
|
|
|
|
P_("Accelerator Size Group"),
|
|
|
|
GTK_TYPE_SIZE_GROUP,
|
|
|
|
(G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkShortcutsGroup:title-size-group:
|
|
|
|
*
|
|
|
|
* The size group for the textual portion of shortcuts in this group.
|
|
|
|
*
|
2020-09-12 16:01:04 +00:00
|
|
|
* This is used internally by GTK, and must not be modified by applications.
|
2015-10-11 20:39:18 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_TITLE_SIZE_GROUP] =
|
|
|
|
g_param_spec_object ("title-size-group",
|
|
|
|
P_("Title Size Group"),
|
|
|
|
P_("Title Size Group"),
|
|
|
|
GTK_TYPE_SIZE_GROUP,
|
|
|
|
(G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkShortcutsGroup:height:
|
|
|
|
*
|
|
|
|
* A rough measure for the number of lines in this group.
|
|
|
|
*
|
2020-09-12 16:01:04 +00:00
|
|
|
* This is used internally by GTK, and is not useful for applications.
|
2015-10-11 20:39:18 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_HEIGHT] =
|
|
|
|
g_param_spec_uint ("height", P_("Height"), P_("Height"),
|
2015-10-27 12:48:08 +00:00
|
|
|
0, G_MAXUINT, 1,
|
2015-10-11 20:39:18 +00:00
|
|
|
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_properties (object_class, LAST_PROP, properties);
|
2017-10-08 00:12:18 +00:00
|
|
|
|
2017-11-18 03:49:57 +00:00
|
|
|
gtk_widget_class_set_css_name (widget_class, I_("shortcuts-group"));
|
2015-10-11 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_shortcuts_group_init (GtkShortcutsGroup *self)
|
|
|
|
{
|
|
|
|
PangoAttrList *attrs;
|
|
|
|
|
|
|
|
gtk_orientable_set_orientation (GTK_ORIENTABLE (self), GTK_ORIENTATION_VERTICAL);
|
|
|
|
gtk_box_set_spacing (GTK_BOX (self), 10);
|
|
|
|
|
|
|
|
attrs = pango_attr_list_new ();
|
|
|
|
pango_attr_list_insert (attrs, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
|
|
|
|
self->title = g_object_new (GTK_TYPE_LABEL,
|
|
|
|
"attributes", attrs,
|
|
|
|
"visible", TRUE,
|
|
|
|
"xalign", 0.0f,
|
|
|
|
NULL);
|
|
|
|
pango_attr_list_unref (attrs);
|
|
|
|
|
2020-05-09 01:08:20 +00:00
|
|
|
gtk_box_append (GTK_BOX (self), GTK_WIDGET (self->title));
|
2015-10-11 20:39:18 +00:00
|
|
|
}
|