summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Otte <otte@redhat.com>2018-08-19 07:12:00 +0200
committerMatthias Clasen <mclasen@redhat.com>2020-03-22 11:14:07 -0400
commitd692cea113572dca796ea4e9b5abe172a2382884 (patch)
tree4e54d937842f17d1abd848dc8c8aeaff7cc1bee7
parent158333241bccf58de58e4c08b22ef43d6a918c96 (diff)
downloadgtk+-d692cea113572dca796ea4e9b5abe172a2382884.tar.gz
shortcutcontroller: Add gtk_shortcut_controller_new_for_model()
This is mainly for internal use, but I can't see a reason to not have it public for people who want to maintain their own lists. I'm sure gnome-builder will never ever find a way to misuse it.
-rw-r--r--docs/reference/gtk/gtk4-sections.txt1
-rw-r--r--gtk/gtkshortcutcontroller.c67
-rw-r--r--gtk/gtkshortcutcontroller.h2
3 files changed, 64 insertions, 6 deletions
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 19012d835a..76b80652f7 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6102,6 +6102,7 @@ GtkShortcutManagerInterface
<TITLE>GtkShortcutController</TITLE>
GtkShortcutController
gtk_shortcut_controller_new
+gtk_shortcut_controller_new_with_model
GtkShortcutScope
GtkShortcutManager
GtkShortcutManagerInterface
diff --git a/gtk/gtkshortcutcontroller.c b/gtk/gtkshortcutcontroller.c
index 9c11e452ed..d5d47b239d 100644
--- a/gtk/gtkshortcutcontroller.c
+++ b/gtk/gtkshortcutcontroller.c
@@ -52,6 +52,7 @@ struct _GtkShortcutController
GtkShortcutScope scope;
GdkModifierType mnemonics_modifiers;
+ guint custom_shortcuts : 1;
guint run_class : 1;
guint run_managed : 1;
};
@@ -64,6 +65,7 @@ struct _GtkShortcutControllerClass
enum {
PROP_0,
PROP_MNEMONICS_MODIFIERS,
+ PROP_MODEL,
PROP_SCOPE,
N_PROPS
@@ -131,6 +133,29 @@ gtk_shortcut_controller_set_property (GObject *object,
gtk_shortcut_controller_set_mnemonics_modifiers (self, g_value_get_flags (value));
break;
+ case PROP_MODEL:
+ {
+ GListModel *model = g_value_get_object (value);
+ if (model && g_list_model_get_item_type (model) != GTK_TYPE_SHORTCUT)
+ {
+ g_warning ("Setting a model with type '%s' on a shortcut controller that requires 'GtkShortcut'",
+ g_type_name (g_list_model_get_item_type (model)));
+ model = NULL;
+ }
+ if (model == NULL)
+ {
+ self->shortcuts = G_LIST_MODEL (g_list_store_new (GTK_TYPE_SHORTCUT));
+ self->custom_shortcuts = TRUE;
+ }
+ else
+ {
+ self->shortcuts = g_object_ref (model);
+ self->custom_shortcuts = FALSE;
+ }
+ g_signal_connect_swapped (self->shortcuts, "items-changed", G_CALLBACK (g_list_model_items_changed), self);
+ }
+ break;
+
case PROP_SCOPE:
gtk_shortcut_controller_set_scope (self, g_value_get_enum (value));
break;
@@ -168,7 +193,8 @@ gtk_shortcut_controller_dispose (GObject *object)
{
GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (object);
- g_list_store_remove_all (G_LIST_STORE (self->shortcuts));
+ if (self->custom_shortcuts)
+ g_list_store_remove_all (G_LIST_STORE (self->shortcuts));
G_OBJECT_CLASS (gtk_shortcut_controller_parent_class)->dispose (object);
}
@@ -328,6 +354,18 @@ gtk_shortcut_controller_class_init (GtkShortcutControllerClass *klass)
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
+ * GtkShortcutController:model:
+ *
+ * A list model to take shortcuts from
+ */
+ properties[PROP_MODEL] =
+ g_param_spec_object ("model",
+ P_("Model"),
+ P_("A list model to take shortcuts from"),
+ G_TYPE_LIST_MODEL,
+ G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
+ /**
* GtkShortcutController:scope:
*
* What scope the shortcuts will be handled in.
@@ -347,9 +385,6 @@ static void
gtk_shortcut_controller_init (GtkShortcutController *self)
{
self->mnemonics_modifiers = GDK_MOD1_MASK;
-
- self->shortcuts = G_LIST_MODEL (g_list_store_new (GTK_TYPE_SHORTCUT));
- g_signal_connect_swapped (self->shortcuts, "items-changed", G_CALLBACK (g_list_model_items_changed), self);
}
void
@@ -449,6 +484,17 @@ gtk_shortcut_controller_new (void)
NULL);
}
+GtkEventController *
+gtk_shortcut_controller_new_for_model (GListModel *model)
+{
+ g_return_val_if_fail (G_IS_LIST_MODEL (model), NULL);
+ g_return_val_if_fail (g_list_model_get_item_type (model) == GTK_TYPE_SHORTCUT, NULL);
+
+ return g_object_new (GTK_TYPE_SHORTCUT_CONTROLLER,
+ "model", model,
+ NULL);
+}
+
void
gtk_shortcut_controller_set_run_class (GtkShortcutController *controller,
gboolean run_class)
@@ -470,6 +516,9 @@ gtk_shortcut_controller_set_run_managed (GtkShortcutController *controller,
*
* Adds @shortcut to the list of shortcuts handled by @self.
*
+ * If this controller uses an external shortcut list, this function does
+ * nothing.
+ *
* The shortcut is added to the list so that it is triggered before
* all existing shortcuts.
*
@@ -482,6 +531,9 @@ gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
g_return_if_fail (GTK_IS_SHORTCUT (shortcut));
+ if (!self->custom_shortcuts)
+ return;
+
g_list_store_append (G_LIST_STORE (self->shortcuts), shortcut);
}
@@ -492,8 +544,8 @@ gtk_shortcut_controller_add_shortcut (GtkShortcutController *self,
*
* Removes @shortcut from the list of shortcuts handled by @self.
*
- * If @shortcut had not been added to @controller, this function does
- * nothing.
+ * If @shortcut had not been added to @controller or this controller
+ * uses an external shortcut list, this function does nothing.
**/
void
gtk_shortcut_controller_remove_shortcut (GtkShortcutController *self,
@@ -504,6 +556,9 @@ gtk_shortcut_controller_remove_shortcut (GtkShortcutController *self,
g_return_if_fail (GTK_IS_SHORTCUT_CONTROLLER (self));
g_return_if_fail (GTK_IS_SHORTCUT (shortcut));
+ if (!self->custom_shortcuts)
+ return;
+
for (i = 0; i < g_list_model_get_n_items (self->shortcuts); i++)
{
GtkShortcut *item = g_list_model_get_item (self->shortcuts, i);
diff --git a/gtk/gtkshortcutcontroller.h b/gtk/gtkshortcutcontroller.h
index fb31ffe8cb..aeb50e7f91 100644
--- a/gtk/gtkshortcutcontroller.h
+++ b/gtk/gtkshortcutcontroller.h
@@ -44,6 +44,8 @@ GType gtk_shortcut_controller_get_type (void) G
GDK_AVAILABLE_IN_ALL
GtkEventController * gtk_shortcut_controller_new (void);
+GDK_AVAILABLE_IN_ALL
+GtkEventController * gtk_shortcut_controller_new_for_model (GListModel *list);
GDK_AVAILABLE_IN_ALL
void gtk_shortcut_controller_set_mnemonics_modifiers (GtkShortcutController *self,