[gtk/wip/ebassi/shortcut: 55/85] label: Implement mnemonics using shortcuts



commit 0003c9b8afedcd3f00404e4e4707e97a5910fa85
Author: Benjamin Otte <otte redhat com>
Date:   Thu Aug 16 05:43:37 2018 +0200

    label: Implement mnemonics using shortcuts

 gtk/gtklabel.c | 56 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 33 insertions(+), 23 deletions(-)
---
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 8b173eddfd..c56644ed96 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -41,6 +41,9 @@
 #include "gtknotebook.h"
 #include "gtkpango.h"
 #include "gtkprivate.h"
+#include "gtkshortcut.h"
+#include "gtkshortcutcontroller.h"
+#include "gtkshortcuttrigger.h"
 #include "gtkshow.h"
 #include "gtksnapshot.h"
 #include "gtkstylecontextprivate.h"
@@ -274,7 +277,7 @@ struct _GtkLabelPrivate
 {
   GtkLabelSelectionInfo *select_info;
   GtkWidget *mnemonic_widget;
-  GtkWindow *mnemonic_window;
+  GtkEventController *mnemonic_controller;
 
   PangoAttrList *attrs;
   PangoAttrList *markup_attrs;
@@ -412,7 +415,6 @@ static GParamSpec *label_props[NUM_PROPERTIES] = { NULL, };
 static guint signals[LAST_SIGNAL] = { 0 };
 
 static GQuark quark_shortcuts_connected;
-static GQuark quark_mnemonic_menu;
 static GQuark quark_mnemonics_visible_connected;
 static GQuark quark_gtk_signal;
 static GQuark quark_link;
@@ -497,9 +499,7 @@ static void gtk_label_update_active_link  (GtkWidget *widget,
 
 static gboolean gtk_label_mnemonic_activate (GtkWidget         *widget,
                                             gboolean           group_cycling);
-static void     gtk_label_setup_mnemonic    (GtkLabel          *label,
-                                             GtkWidget         *toplevel,
-                                            guint              last_key);
+static void     gtk_label_setup_mnemonic    (GtkLabel          *label);
 
 static void     gtk_label_buildable_interface_init   (GtkBuildableIface  *iface);
 static gboolean gtk_label_buildable_custom_tag_start (GtkBuildable       *buildable,
@@ -1131,7 +1131,6 @@ gtk_label_class_init (GtkLabelClass *class)
   gtk_widget_class_set_css_name (widget_class, I_("label"));
 
   quark_shortcuts_connected = g_quark_from_static_string ("gtk-label-shortcuts-connected");
-  quark_mnemonic_menu = g_quark_from_static_string ("gtk-mnemonic-menu");
   quark_mnemonics_visible_connected = g_quark_from_static_string ("gtk-label-mnemonics-visible-connected");
   quark_gtk_signal = g_quark_from_static_string ("gtk-signal");
   quark_link = g_quark_from_static_string ("link");
@@ -1334,7 +1333,6 @@ gtk_label_init (GtkLabel *label)
   priv->attrs = NULL;
 
   priv->mnemonic_widget = NULL;
-  priv->mnemonic_window = NULL;
 
   priv->mnemonics_visible = FALSE;
 }
@@ -1802,30 +1800,42 @@ gtk_label_mnemonic_activate (GtkWidget *widget,
 }
 
 static void
-gtk_label_setup_mnemonic (GtkLabel  *label,
-                          GtkWidget *toplevel,
-                         guint      last_key)
+gtk_label_setup_mnemonic (GtkLabel *label)
 {
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
   GtkWidget *widget = GTK_WIDGET (label);
+  GtkShortcut *shortcut;
   
-  if (last_key != GDK_KEY_VoidSymbol)
+  if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
     {
-      if (priv->mnemonic_window)
-       {
-         gtk_window_remove_mnemonic  (priv->mnemonic_window,
-                                      last_key,
-                                      widget);
-         priv->mnemonic_window = NULL;
-       }
+      if (priv->mnemonic_controller)
+        {
+          gtk_widget_remove_controller (widget, priv->mnemonic_controller);
+          priv->mnemonic_controller = NULL;
+        }
+      return;
     }
 
-  if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
-      goto done;
+  if (priv->mnemonic_controller == NULL)
+    {
+      priv->mnemonic_controller = gtk_shortcut_controller_new ();
+      gtk_event_controller_set_propagation_phase (priv->mnemonic_controller, GTK_PHASE_CAPTURE);
+      gtk_shortcut_controller_set_scope (GTK_SHORTCUT_CONTROLLER (priv->mnemonic_controller), 
GTK_SHORTCUT_SCOPE_MANAGED);
+      shortcut = gtk_shortcut_new ();
+      gtk_shortcut_set_trigger (shortcut, gtk_mnemonic_trigger_new (priv->mnemonic_keyval));
+      gtk_shortcut_set_mnemonic_activate (shortcut, TRUE);
+      gtk_shortcut_controller_add_shortcut (GTK_SHORTCUT_CONTROLLER (priv->mnemonic_controller), shortcut);
+      gtk_widget_add_controller (GTK_WIDGET (label), priv->mnemonic_controller);
+      g_object_unref (shortcut);
+    }
+  else
+    {
+      shortcut = g_list_model_get_item (G_LIST_MODEL (priv->mnemonic_controller), 0);
+      gtk_shortcut_set_trigger (shortcut, gtk_mnemonic_trigger_new (priv->mnemonic_keyval));
+      g_object_unref (shortcut);
+    }
 
   connect_mnemonics_visible_notify (GTK_LABEL (widget));
-
- done:;
 }
 
 static void
@@ -2151,7 +2161,7 @@ gtk_label_recalculate (GtkLabel *label)
 
   if (keyval != priv->mnemonic_keyval)
     {
-      gtk_label_setup_mnemonic (label, GTK_WIDGET (gtk_widget_get_root (GTK_WIDGET (label))), keyval);
+      gtk_label_setup_mnemonic (label);
       g_object_notify_by_pspec (G_OBJECT (label), label_props[PROP_MNEMONIC_KEYVAL]);
     }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]