[gtk/wip/matthiasc/context-menu: 2/30] text, entry: Implement context menu api



commit 3ae0c9689af51365682cfdf16bf9de3b6ba933a7
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Apr 8 04:27:59 2019 +0000

    text, entry: Implement context menu api
    
    Drop the ::populate-popup signal and implement
    the new context menu api.

 gtk/gtkentry.c       |  28 +++
 gtk/gtktext.c        | 479 +++++++++++++++++++++++++++++----------------------
 gtk/gtktextprivate.h |   7 -
 3 files changed, 303 insertions(+), 211 deletions(-)
---
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index e1cf3131f0..9923f2ed17 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -325,6 +325,22 @@ gtk_entry_mnemonic_activate (GtkWidget *widget,
   return TRUE;
 }
 
+static void
+gtk_entry_notify (GObject    *object,
+                  GParamSpec *pspec)
+{
+  if (strcmp (pspec->name, "context-menu") == 0)
+    {
+      GtkEntry *entry = GTK_ENTRY (object);
+      GtkEntryPrivate *priv = gtk_entry_get_instance_private (entry);
+      GMenuModel *menu = gtk_widget_get_context_menu (GTK_WIDGET (entry));
+      gtk_widget_set_context_menu (priv->text, menu);
+    }
+
+  if (G_OBJECT_CLASS (gtk_entry_parent_class)->notify)
+    G_OBJECT_CLASS (gtk_entry_parent_class)->notify (object, pspec);
+}
+
 static void
 gtk_entry_class_init (GtkEntryClass *class)
 {
@@ -337,6 +353,7 @@ gtk_entry_class_init (GtkEntryClass *class)
   gobject_class->finalize = gtk_entry_finalize;
   gobject_class->set_property = gtk_entry_set_property;
   gobject_class->get_property = gtk_entry_get_property;
+  gobject_class->notify = gtk_entry_notify;
 
   widget_class->measure = gtk_entry_measure;
   widget_class->size_allocate = gtk_entry_size_allocate;
@@ -1276,6 +1293,9 @@ gtk_entry_init (GtkEntry *entry)
   connect_text_signals (entry);
 
   priv->editing_canceled = FALSE;
+
+  gtk_widget_set_context_menu (GTK_WIDGET (entry),
+                               gtk_widget_get_context_menu (priv->text));
 }
 
 static void
@@ -3471,6 +3491,8 @@ set_show_emoji_icon (GtkEntry *entry,
                      gboolean  value)
 {
   GtkEntryPrivate *priv = gtk_entry_get_instance_private (entry);
+  GActionGroup *actions;
+  GAction *action;
 
   if (priv->show_emoji_icon == value)
     return;
@@ -3512,6 +3534,12 @@ set_show_emoji_icon (GtkEntry *entry,
 
   g_object_notify_by_pspec (G_OBJECT (entry), entry_props[PROP_SHOW_EMOJI_ICON]);
   gtk_widget_queue_resize (GTK_WIDGET (entry));
+
+  actions = gtk_widget_get_action_group (priv->text, "context");
+  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "insert-emoji");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                               priv->show_emoji_icon ||
+                               (gtk_entry_get_input_hints (entry) & GTK_INPUT_HINT_NO_EMOJI) == 0);
 }
 
 GtkEventController *
diff --git a/gtk/gtktext.c b/gtk/gtktext.c
index f89b8555f4..0b354fb68f 100644
--- a/gtk/gtktext.c
+++ b/gtk/gtktext.c
@@ -23,6 +23,7 @@
 
 #include "gtktextprivate.h"
 
+#include "gtkactionable.h"
 #include "gtkadjustment.h"
 #include "gtkbindings.h"
 #include "gtkbox.h"
@@ -174,6 +175,8 @@ struct _GtkTextPrivate
   GtkCssNode    *block_cursor_node;
   GtkCssNode    *undershoot_node[2];
 
+  GActionMap    *context_actions;
+
   float         xalign;
 
   int           ascent;                     /* font ascent in pango units  */
@@ -233,7 +236,6 @@ struct _GtkTextPasswordHint
 
 enum {
   ACTIVATE,
-  POPULATE_POPUP,
   MOVE_CURSOR,
   INSERT_AT_CURSOR,
   DELETE_FROM_CURSOR,
@@ -263,7 +265,6 @@ enum {
   PROP_INPUT_PURPOSE,
   PROP_INPUT_HINTS,
   PROP_ATTRIBUTES,
-  PROP_POPULATE_ALL,
   PROP_TABS,
   PROP_ENABLE_EMOJI_COMPLETION,
   PROP_PROPAGATE_TEXT_WIDTH,
@@ -383,6 +384,7 @@ static void        gtk_text_set_alignment        (GtkText    *self,
 
 /* Default signal handlers
  */
+static GMenuModel *gtk_text_get_default_menu (void);
 static gboolean gtk_text_popup_menu         (GtkWidget       *widget);
 static void     gtk_text_move_cursor        (GtkText         *self,
                                              GtkMovementStep  step,
@@ -540,6 +542,10 @@ static void         begin_change                       (GtkText *self);
 static void         end_change                         (GtkText *self);
 static void         emit_changed                       (GtkText *self);
 
+static void         gtk_text_add_context_actions      (GtkText *self);
+static void         gtk_text_update_clipboard_actions (GtkText *self);
+static void         gtk_text_update_emoji_action      (GtkText *self);
+
 
 /* GtkTextContent implementation
  */
@@ -858,19 +864,6 @@ gtk_text_class_init (GtkTextClass *class)
                           PANGO_TYPE_ATTR_LIST,
                           GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
-  /**
-   * GtkText:populate-all:
-   *
-   * If :populate-all is %TRUE, the #GtkText::populate-popup
-   * signal is also emitted for touch popups.
-   */
-  text_props[PROP_POPULATE_ALL] =
-      g_param_spec_boolean ("populate-all",
-                            P_("Populate all"),
-                            P_("Whether to emit ::populate-popup for touch popups"),
-                            FALSE,
-                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
-
   /**
    * GtkText::tabs:
    *
@@ -908,34 +901,6 @@ gtk_text_class_init (GtkTextClass *class)
 
   gtk_editable_install_properties (gobject_class, NUM_PROPERTIES);
 
-  /**
-   * GtkText::populate-popup:
-   * @self: The self on which the signal is emitted
-   * @widget: the container that is being populated
-   *
-   * The ::populate-popup signal gets emitted before showing the
-   * context menu of the self.
-   *
-   * If you need to add items to the context menu, connect
-   * to this signal and append your items to the @widget, which
-   * will be a #GtkMenu in this case.
-   *
-   * If #GtkText:populate-all is %TRUE, this signal will
-   * also be emitted to populate touch popups. In this case,
-   * @widget will be a different container, e.g. a #GtkToolbar.
-   * The signal handler should not make assumptions about the
-   * type of @widget.
-   */
-  signals[POPULATE_POPUP] =
-    g_signal_new (I_("populate-popup"),
-                  G_OBJECT_CLASS_TYPE (gobject_class),
-                  G_SIGNAL_RUN_LAST,
-                  G_STRUCT_OFFSET (GtkTextClass, populate_popup),
-                  NULL, NULL,
-                  NULL,
-                  G_TYPE_NONE, 1,
-                  GTK_TYPE_WIDGET);
-  
  /* Action signals */
   
   /**
@@ -1505,14 +1470,6 @@ gtk_text_set_property (GObject      *object,
       gtk_text_set_attributes (self, g_value_get_boxed (value));
       break;
 
-    case PROP_POPULATE_ALL:
-      if (priv->populate_all != g_value_get_boolean (value))
-        {
-          priv->populate_all = g_value_get_boolean (value);
-          g_object_notify_by_pspec (object, pspec);
-        }
-      break;
-
     case PROP_TABS:
       gtk_text_set_tabs (self, g_value_get_boxed (value));
       break;
@@ -1633,10 +1590,6 @@ gtk_text_get_property (GObject    *object,
       g_value_set_boxed (value, priv->attrs);
       break;
 
-    case PROP_POPULATE_ALL:
-      g_value_set_boolean (value, priv->populate_all);
-      break;
-
     case PROP_TABS:
       g_value_set_boxed (value, priv->tabs);
       break;
@@ -1663,6 +1616,7 @@ gtk_text_init (GtkText *self)
   GtkGesture *gesture;
   GtkEventController *controller;
   int i;
+  GMenuModel *menu;
 
   gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
   gtk_widget_set_overflow (GTK_WIDGET (self), GTK_OVERFLOW_HIDDEN);
@@ -1746,6 +1700,11 @@ gtk_text_init (GtkText *self)
     }
 
   set_text_cursor (GTK_WIDGET (self));
+  gtk_text_add_context_actions (self);
+
+  menu = gtk_text_get_default_menu ();
+  gtk_widget_set_context_menu (GTK_WIDGET (self), menu);
+  g_object_unref (menu);
 }
 
 static void
@@ -1791,6 +1750,8 @@ gtk_text_dispose (GObject *object)
   keymap = gdk_display_get_keymap (gtk_widget_get_display (GTK_WIDGET (object)));
   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, self);
 
+  g_clear_object (&priv->context_actions);
+
   G_OBJECT_CLASS (gtk_text_parent_class)->dispose (object);
 }
 
@@ -5590,145 +5551,254 @@ gtk_text_set_alignment (GtkText *self,
     }
 }
 
-/* Quick hack of a popup menu
- */
 static void
-activate_cb (GtkWidget *menuitem,
-             GtkText   *self)
+hide_selection_bubble (GtkText *self)
 {
-  const char *signal;
+  GtkTextPrivate *priv = gtk_text_get_instance_private (self);
 
-  signal = g_object_get_qdata (G_OBJECT (menuitem), quark_gtk_signal);
-  g_signal_emit_by_name (self, signal);
+  if (priv->selection_bubble && gtk_widget_get_visible (priv->selection_bubble))
+    gtk_widget_hide (priv->selection_bubble);
 }
 
+static void
+cut_clipboard_activated (GSimpleAction *action,
+                         GVariant      *parameter,
+                         gpointer       user_data)
+{
+  g_signal_emit_by_name (user_data, "cut-clipboard");
+  hide_selection_bubble (GTK_TEXT (user_data));
+}
 
-static gboolean
-gtk_text_mnemonic_activate (GtkWidget *widget,
-                            gboolean   group_cycling)
+static void
+copy_clipboard_activated (GSimpleAction *action,
+                          GVariant      *parameter,
+                          gpointer       user_data)
 {
-  gtk_widget_grab_focus (widget);
-  return GDK_EVENT_STOP;
+  g_signal_emit_by_name (user_data, "copy-clipboard");
+  hide_selection_bubble (GTK_TEXT (user_data));
 }
 
 static void
-append_action_signal (GtkText     *self,
-                      GtkWidget   *menu,
-                      const char  *label,
-                      const char  *signal,
-                      gboolean     sensitive)
+paste_clipboard_activated (GSimpleAction *action,
+                           GVariant      *parameter,
+                           gpointer       user_data)
 {
-  GtkWidget *menuitem = gtk_menu_item_new_with_mnemonic (label);
+  g_signal_emit_by_name (user_data, "paste-clipboard");
+  hide_selection_bubble (GTK_TEXT (user_data));
+}
 
-  g_object_set_qdata (G_OBJECT (menuitem), quark_gtk_signal, (char *)signal);
-  g_signal_connect (menuitem, "activate",
-                    G_CALLBACK (activate_cb), self);
+static void
+delete_selection_activated (GSimpleAction *action,
+                            GVariant      *parameter,
+                            gpointer       user_data)
+{
+  gtk_text_delete_cb (GTK_TEXT (user_data));
+  hide_selection_bubble (GTK_TEXT (user_data));
+}
 
-  gtk_widget_set_sensitive (menuitem, sensitive);
-  
-  gtk_widget_show (menuitem);
-  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
+static void
+select_all_activated (GSimpleAction *action,
+                      GVariant      *parameter,
+                      gpointer       user_data)
+{
+  gtk_text_select_all (GTK_TEXT (user_data));
 }
-        
+
 static void
-popup_menu_detach (GtkWidget *attach_widget,
-                   GtkMenu   *menu)
+insert_emoji_activated (GSimpleAction *action,
+                        GVariant      *parameter,
+                        gpointer       user_data)
 {
-  GtkText *self_attach = GTK_TEXT (attach_widget);
-  GtkTextPrivate *priv_attach = gtk_text_get_instance_private (self_attach);
+  gtk_text_insert_emoji (GTK_TEXT (user_data));
+  hide_selection_bubble (GTK_TEXT (user_data));
+}
 
-  priv_attach->popup_menu = NULL;
+static void
+gtk_text_add_context_actions (GtkText *self)
+{
+  GtkTextPrivate *priv = gtk_text_get_instance_private (self);
+
+  GActionEntry entries[] = {
+    { "cut-clipboard", cut_clipboard_activated, NULL, NULL, NULL },
+    { "copy-clipboard", copy_clipboard_activated, NULL, NULL, NULL },
+    { "paste-clipboard", paste_clipboard_activated, NULL, NULL, NULL },
+    { "delete-selection", delete_selection_activated, NULL, NULL, NULL },
+    { "select-all", select_all_activated, NULL, NULL, NULL },
+    { "insert-emoji", insert_emoji_activated, NULL, NULL, NULL },
+  };
+
+  GSimpleActionGroup *actions = g_simple_action_group_new ();
+  GAction *action;
+
+  priv->context_actions = G_ACTION_MAP (actions);
+
+  g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), self);
+
+  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "cut-clipboard");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "copy-clipboard");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "paste-clipboard");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "delete-selection");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "select-all");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "insert-emoji");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+
+  gtk_widget_insert_action_group (GTK_WIDGET (self), "context", G_ACTION_GROUP (actions));
 }
 
 static void
-gtk_text_do_popup (GtkText        *self,
-                   const GdkEvent *event)
+gtk_text_update_clipboard_actions (GtkText *self)
+ {
+  GtkTextPrivate *priv = gtk_text_get_instance_private (self);
+  DisplayMode mode;
+  GdkClipboard *clipboard;
+  gboolean has_clipboard;
+  GAction *action;
+ 
+  clipboard = gtk_widget_get_clipboard (GTK_WIDGET (self));
+  has_clipboard = gdk_content_formats_contain_gtype (gdk_clipboard_get_formats (clipboard), G_TYPE_STRING);
+  mode = gtk_text_get_display_mode (self);
+
+  action = g_action_map_lookup_action (priv->context_actions, "cut-clipboard");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                               mode == DISPLAY_NORMAL &&
+                               priv->editable &&
+                               priv->current_pos != priv->selection_bound);
+
+  action = g_action_map_lookup_action (priv->context_actions, "copy-clipboard");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                               mode == DISPLAY_NORMAL &&
+                               priv->current_pos != priv->selection_bound);
+
+  action = g_action_map_lookup_action (priv->context_actions, "paste-clipboard");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                               priv->editable && has_clipboard);
+
+  action = g_action_map_lookup_action (priv->context_actions, "delete-selection");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                               priv->editable &&
+                               priv->current_pos != priv->selection_bound);
+
+  action = g_action_map_lookup_action (priv->context_actions, "select-all");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                               priv->buffer && (gtk_entry_buffer_get_length (priv->buffer) > 0));
+}
+
+static void
+gtk_text_update_emoji_action (GtkText *self)
 {
   GtkTextPrivate *priv = gtk_text_get_instance_private (self);
-  GdkEvent *trigger_event;
+  GAction *action;
 
-  /* In order to know what entries we should make sensitive, we
-   * ask for the current targets of the clipboard, and when
-   * we get them, then we actually pop up the menu.
-   */
-  trigger_event = event ? gdk_event_copy (event) : gtk_get_current_event ();
+  action = g_action_map_lookup_action (priv->context_actions, "insert-emoji");
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
+                               (gtk_text_get_input_hints (self) & GTK_INPUT_HINT_NO_EMOJI) == 0);
+}
 
-  if (gtk_widget_get_realized (GTK_WIDGET (self)))
-    {
-      DisplayMode mode;
-      gboolean clipboard_contains_text;
-      GtkWidget *menu;
-      GtkWidget *menuitem;
+static GMenuModel *
+gtk_text_get_default_menu (void)
+{
+  GMenu *menu, *section;
+  GMenuItem *item;
 
-      clipboard_contains_text = gdk_content_formats_contain_gtype (gdk_clipboard_get_formats 
(gtk_widget_get_clipboard (GTK_WIDGET (self))),
-                                                                   G_TYPE_STRING);
-      if (priv->popup_menu)
-        gtk_widget_destroy (priv->popup_menu);
+  menu = g_menu_new ();
 
-      priv->popup_menu = menu = gtk_menu_new ();
-      gtk_style_context_add_class (gtk_widget_get_style_context (menu),
-                                   GTK_STYLE_CLASS_CONTEXT_MENU);
+  section = g_menu_new ();
+  item = g_menu_item_new (_("Cu_t"), "context.cut-clipboard");
+  g_menu_item_set_attribute (item, "touch-icon", "s", "edit-cut-symbolic");
+  g_menu_append_item (section, item);
+  g_object_unref (item);
+  item = g_menu_item_new (_("_Copy"), "context.copy-clipboard");
+  g_menu_item_set_attribute (item, "touch-icon", "s", "edit-copy-symbolic");
+  g_menu_append_item (section, item);
+  g_object_unref (item);
+  item = g_menu_item_new (_("_Paste"), "context.paste-clipboard");
+  g_menu_item_set_attribute (item, "touch-icon", "s", "edit-paste-symbolic");
+  g_menu_append_item (section, item);
+  g_object_unref (item);
+  item = g_menu_item_new (_("_Delete"), "context.delete-selection");
+  g_menu_item_set_attribute (item, "touch-icon", "s", "edit-delete-symbolic");
+  g_menu_append_item (section, item);
+  g_object_unref (item);
+  g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
+  g_object_unref (section);
 
-      gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (self), popup_menu_detach);
+  section = g_menu_new ();
 
-      mode = gtk_text_get_display_mode (self);
-      append_action_signal (self, menu, _("Cu_t"), "cut-clipboard",
-                            priv->editable && mode == DISPLAY_NORMAL &&
-                            priv->current_pos != priv->selection_bound);
+  item = g_menu_item_new (_("Select _All"), "context.select-all");
+  g_menu_item_set_attribute (item, "touch-icon", "s", "edit-select-all-symbolic");
+  g_menu_append_item (section, item);
+  g_object_unref (item);
 
-      append_action_signal (self, menu, _("_Copy"), "copy-clipboard",
-                            mode == DISPLAY_NORMAL &&
-                            priv->current_pos != priv->selection_bound);
+  item = g_menu_item_new ( _("Insert _Emoji"), "context.insert-emoji");
+  g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
+  g_menu_item_set_attribute (item, "touch-icon", "s", "face-smile-symbolic");
+  g_menu_append_item (section, item);
+  g_object_unref (item);
+  g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
+  g_object_unref (section);
 
-      append_action_signal (self, menu, _("_Paste"), "paste-clipboard",
-                            priv->editable && clipboard_contains_text);
+  return G_MENU_MODEL (menu);
+}
 
-      menuitem = gtk_menu_item_new_with_mnemonic (_("_Delete"));
-      gtk_widget_set_sensitive (menuitem, priv->editable && priv->current_pos != priv->selection_bound);
-      g_signal_connect_swapped (menuitem, "activate",
-                                G_CALLBACK (gtk_text_delete_cb), self);
-      gtk_widget_show (menuitem);
-      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
+static gboolean
+gtk_text_mnemonic_activate (GtkWidget *widget,
+                            gboolean   group_cycling)
+{
+  gtk_widget_grab_focus (widget);
+  return GDK_EVENT_STOP;
+}
 
-      menuitem = gtk_separator_menu_item_new ();
-      gtk_widget_show (menuitem);
-      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
+static void
+popup_menu_detach (GtkWidget *attach_widget,
+                   GtkMenu   *menu)
+{
+  GtkText *self_attach = GTK_TEXT (attach_widget);
+  GtkTextPrivate *priv_attach = gtk_text_get_instance_private (self_attach);
 
-      menuitem = gtk_menu_item_new_with_mnemonic (_("Select _All"));
-      gtk_widget_set_sensitive (menuitem, gtk_entry_buffer_get_length (priv->buffer) > 0);
-      g_signal_connect_swapped (menuitem, "activate",
-                                G_CALLBACK (gtk_text_select_all), self);
-      gtk_widget_show (menuitem);
-      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
+  priv_attach->popup_menu = NULL;
+}
 
-      if ((gtk_text_get_input_hints (self) & GTK_INPUT_HINT_NO_EMOJI) == 0)
-        {
-          menuitem = gtk_menu_item_new_with_mnemonic (_("Insert _Emoji"));
-          gtk_widget_set_sensitive (menuitem,
-                                    mode == DISPLAY_NORMAL &&
-                                    priv->editable);
-          g_signal_connect_swapped (menuitem, "activate",
-                                    G_CALLBACK (gtk_text_insert_emoji), self);
-          gtk_widget_show (menuitem);
-          gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
-        }
+static void
+gtk_text_do_popup (GtkText        *self,
+                   const GdkEvent *event)
+{
+  GtkTextPrivate *priv = gtk_text_get_instance_private (self);
+  GMenuModel *model;
+  GtkWidget *menu;
+  GdkEvent *trigger_event;
 
-      g_signal_emit (self, signals[POPULATE_POPUP], 0, menu);
+  if (!gtk_widget_get_realized (GTK_WIDGET (self)))
+    return;
 
-      if (trigger_event && gdk_event_triggers_context_menu (trigger_event))
-        gtk_menu_popup_at_pointer (GTK_MENU (menu), trigger_event);
-      else
-        {
-          gtk_menu_popup_at_widget (GTK_MENU (menu),
-                                    GTK_WIDGET (self),
-                                    GDK_GRAVITY_SOUTH_EAST,
-                                    GDK_GRAVITY_NORTH_WEST,
-                                    trigger_event);
+  gtk_text_update_clipboard_actions (self);
 
-          gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
-        }
-    }
+  if (priv->popup_menu)
+    gtk_widget_destroy (priv->popup_menu);
+
+  model = gtk_widget_get_context_menu (GTK_WIDGET (self));
+  priv->popup_menu = menu = gtk_menu_new_from_model (model);
+
+  gtk_style_context_add_class (gtk_widget_get_style_context (menu), GTK_STYLE_CLASS_CONTEXT_MENU);
+  gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (self), popup_menu_detach);
 
+  trigger_event = event ? gdk_event_copy (event) : gtk_get_current_event ();
+  if (trigger_event && gdk_event_triggers_context_menu (trigger_event))
+    gtk_menu_popup_at_pointer (GTK_MENU (menu), trigger_event);
+  else
+    {
+      gtk_menu_popup_at_widget (GTK_MENU (menu),
+                                GTK_WIDGET (self),
+                                GDK_GRAVITY_SOUTH_EAST,
+                                GDK_GRAVITY_NORTH_WEST,
+                                trigger_event);
+      gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
+    }
+ 
   g_clear_object (&trigger_event);
 }
 
@@ -5766,40 +5836,56 @@ show_or_hide_handles (GtkWidget  *popover,
 }
 
 static void
-activate_bubble_cb (GtkWidget *item,
-                    GtkText   *self)
+append_bubble_item (GtkText    *self,
+                    GtkWidget  *toolbar,
+                    GMenuModel *model,
+                    int         index)
 {
   GtkTextPrivate *priv = gtk_text_get_instance_private (self);
-  const char *signal;
+  GtkWidget *item, *image;
+  GVariant *att;
+  const char *icon_name;
+  const char *action_name;
+  GAction *action;
+  GMenuModel *link;
+
+  link = g_menu_model_get_item_link (model, index, "section");
+  if (link)
+    {
+      int i;
+      for (i = 0; i < g_menu_model_get_n_items (link); i++)
+        append_bubble_item (self, toolbar, link, i);
+      g_object_unref (link);
+      return;
+    }
 
-  signal = g_object_get_qdata (G_OBJECT (item), quark_gtk_signal);
-  gtk_widget_hide (priv->selection_bubble);
-  if (strcmp (signal, "select-all") == 0)
-    gtk_text_select_all (self);
-  else
-    g_signal_emit_by_name (self, signal);
-}
+  att = g_menu_model_get_item_attribute_value (model, index, "touch-icon", G_VARIANT_TYPE_STRING);
+  if (att == NULL)
+    return;
 
-static void
-append_bubble_action (GtkText     *self,
-                      GtkWidget   *toolbar,
-                      const char  *label,
-                      const char  *icon_name,
-                      const char  *signal,
-                      gboolean     sensitive)
-{
-  GtkWidget *item, *image;
+  icon_name = g_variant_get_string (att, NULL);
+  g_variant_unref (att);
+
+  att = g_menu_model_get_item_attribute_value (model, index, "action", G_VARIANT_TYPE_STRING);
+  if (att == NULL)
+    return;
+  action_name = g_variant_get_string (att, NULL);
+  g_variant_unref (att);
+
+  if (g_str_has_prefix (action_name, "context."))
+    {
+      action = g_action_map_lookup_action (priv->context_actions, action_name + strlen ("context."));
+      if (action && !g_action_get_enabled (action))
+        return;
+    }
 
   item = gtk_button_new ();
   gtk_widget_set_focus_on_click (item, FALSE);
   image = gtk_image_new_from_icon_name (icon_name);
   gtk_widget_show (image);
   gtk_container_add (GTK_CONTAINER (item), image);
-  gtk_widget_set_tooltip_text (item, label);
   gtk_style_context_add_class (gtk_widget_get_style_context (item), "image-button");
-  g_object_set_qdata (G_OBJECT (item), quark_gtk_signal, (char *)signal);
-  g_signal_connect (item, "clicked", G_CALLBACK (activate_bubble_cb), self);
-  gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
+  gtk_actionable_set_action_name (GTK_ACTIONABLE (item), action_name);
   gtk_widget_show (GTK_WIDGET (item));
   gtk_container_add (GTK_CONTAINER (toolbar), item);
 }
@@ -5811,21 +5897,19 @@ gtk_text_selection_bubble_popup_show (gpointer user_data)
   GtkTextPrivate *priv = gtk_text_get_instance_private (self);
   cairo_rectangle_int_t rect;
   GtkAllocation allocation;
-  int start_x, end_x;
   gboolean has_selection;
-  gboolean has_clipboard;
-  gboolean all_selected;
-  DisplayMode mode;
+  int start_x, end_x;
   GtkWidget *box;
   GtkWidget *toolbar;
-  int length;
   GtkAllocation text_allocation;
+  GMenuModel *model;
+  int i;
+
+  gtk_text_update_clipboard_actions (self);
 
   gtk_text_get_text_allocation (self, &text_allocation);
 
   has_selection = priv->selection_bound != priv->current_pos;
-  length = gtk_entry_buffer_get_length (get_buffer (self));
-  all_selected = (priv->selection_bound == 0) && (priv->current_pos == length);
 
   if (!has_selection && !priv->editable)
     {
@@ -5851,24 +5935,10 @@ gtk_text_selection_bubble_popup_show (gpointer user_data)
   gtk_container_add (GTK_CONTAINER (priv->selection_bubble), box);
   gtk_container_add (GTK_CONTAINER (box), toolbar);
 
-  has_clipboard = gdk_content_formats_contain_gtype (gdk_clipboard_get_formats (gtk_widget_get_clipboard 
(GTK_WIDGET (self))),
-                                                     G_TYPE_STRING);
-  mode = gtk_text_get_display_mode (self);
-
-  if (priv->editable && has_selection && mode == DISPLAY_NORMAL)
-    append_bubble_action (self, toolbar, _("Select all"), "edit-select-all-symbolic", "select-all", 
!all_selected);
-
-  if (priv->editable && has_selection && mode == DISPLAY_NORMAL)
-    append_bubble_action (self, toolbar, _("Cut"), "edit-cut-symbolic", "cut-clipboard", TRUE);
-
-  if (has_selection && mode == DISPLAY_NORMAL)
-    append_bubble_action (self, toolbar, _("Copy"), "edit-copy-symbolic", "copy-clipboard", TRUE);
-
-  if (priv->editable)
-    append_bubble_action (self, toolbar, _("Paste"), "edit-paste-symbolic", "paste-clipboard", 
has_clipboard);
+  model = gtk_widget_get_context_menu (GTK_WIDGET (self));
 
-  if (priv->populate_all)
-    g_signal_emit (self, signals[POPULATE_POPUP], 0, box);
+  for (i = 0; i < g_menu_model_get_n_items (model); i++)
+    append_bubble_item (self, toolbar, model, i);
 
   gtk_widget_get_allocation (GTK_WIDGET (self), &allocation);
 
@@ -5944,7 +6014,7 @@ gtk_text_drag_begin (GtkWidget *widget,
 
   text = _gtk_text_get_selected_text (self);
 
-  if (text)
+  if (self)
     {
       int *ranges, n_ranges;
       GdkPaintable *paintable;
@@ -6492,6 +6562,7 @@ gtk_text_set_input_hints (GtkText       *self,
                     NULL);
 
       g_object_notify_by_pspec (G_OBJECT (self), text_props[PROP_INPUT_HINTS]);
+      gtk_text_update_emoji_action (self);
     }
 }
 
diff --git a/gtk/gtktextprivate.h b/gtk/gtktextprivate.h
index ec1d0af6d2..daeed71bce 100644
--- a/gtk/gtktextprivate.h
+++ b/gtk/gtktextprivate.h
@@ -34,9 +34,6 @@ typedef struct _GtkTextClass         GtkTextClass;
 /*<private>
  * GtkTextClass:
  * @parent_class: The parent class.
- * @populate_popup: Class handler for the #GtkText::populate-popup signal. If
- *   non-%NULL, this will be called to add additional entries to the context
- *   menu when it is displayed.
  * @activate: Class handler for the #GtkText::activate signal. The default
  *   implementation activates the gtk.activate-default action.
  * @move_cursor: Class handler for the #GtkText::move-cursor signal. The
@@ -70,10 +67,6 @@ struct _GtkTextClass
 {
   GtkWidgetClass parent_class;
 
-  /* Hook to customize right-click popup */
-  void (* populate_popup)     (GtkText         *self,
-                               GtkWidget       *popup);
-
   /* Action signals
    */
   void (* activate)           (GtkText         *self);


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