[gtk/widget-class-actions: 3/6] label: Use the new action machinery



commit 63be6b11359a66397a91ca07788109dcecad5f8e
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jun 14 20:05:51 2019 -0400

    label: Use the new action machinery
    
    Port GtkLabel to use widget class actions.
    
    Note that this also changes the names of
    the GtkLabel actions away from a generic
    "context" prefix.

 gtk/gtklabel.c | 291 +++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 202 insertions(+), 89 deletions(-)
---
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 6bc134edc2..1612325b14 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -285,7 +285,6 @@ struct _GtkLabelPrivate
   PangoAttrList *markup_attrs;
   PangoLayout   *layout;
 
-  GActionMap *context_actions;
   GtkWidget *popup_menu;
   GMenuModel *extra_menu;
 
@@ -575,8 +574,39 @@ static void   gtk_label_drag_gesture_update         (GtkGestureDrag *gesture,
                                                      gdouble         offset_y,
                                                      GtkLabel       *label);
 
-static void      gtk_label_add_context_actions (GtkLabel *label);
-static void      gtk_label_update_clipboard_actions (GtkLabel *label);
+/* Actions */
+
+static void      gtk_label_activate_clipboard_copy       (GtkWidget  *label,
+                                                          const char *name,
+                                                          GVariant   *parameter);
+static void      gtk_label_activate_selection_select_all (GtkWidget  *label,
+                                                          const char *name,
+                                                          GVariant   *parameter);
+static void      gtk_label_activate_link_open            (GtkWidget  *label,
+                                                          const char *name,
+                                                          GVariant   *parameter);
+static void      gtk_label_activate_link_copy            (GtkWidget  *label,
+                                                          const char *name,
+                                                          GVariant   *parameter);
+static void      gtk_label_nop                           (GtkWidget  *label,
+                                                          const char *name,
+                                                          GVariant   *parameter);
+
+static gboolean  gtk_label_query_clipboard_action (GtkWidget           *label,
+                                                   const char          *name,
+                                                   gboolean            *enabled,
+                                                   const GVariantType **parameter_type,
+                                                   const GVariantType **state_type,
+                                                   GVariant           **state_hint,
+                                                   GVariant           **state);
+
+static gboolean  gtk_label_query_link_action      (GtkWidget           *label,
+                                                   const char          *name,
+                                                   gboolean            *enabled,
+                                                   const GVariantType **parameter_type,
+                                                   const GVariantType **state_type,
+                                                   GVariant           **state_hint,
+                                                   GVariant           **state);
 
 static GtkSizeRequestMode gtk_label_get_request_mode                (GtkWidget           *widget);
 static void     gtk_label_measure (GtkWidget     *widget,
@@ -1143,6 +1173,35 @@ gtk_label_class_init (GtkLabelClass *class)
   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");
+
+  gtk_widget_class_install_action (widget_class, "clipboard.cut",
+                                   gtk_label_nop,
+                                   gtk_label_query_clipboard_action,
+                                   NULL);
+  gtk_widget_class_install_action (widget_class, "clipboard.copy",
+                                   gtk_label_activate_clipboard_copy,
+                                   gtk_label_query_clipboard_action,
+                                   NULL);
+  gtk_widget_class_install_action (widget_class, "clipboard.paste",
+                                   gtk_label_nop,
+                                   gtk_label_query_clipboard_action,
+                                   NULL);
+  gtk_widget_class_install_action (widget_class, "selection.delete",
+                                   gtk_label_nop,
+                                   gtk_label_query_clipboard_action,
+                                   NULL);
+  gtk_widget_class_install_action (widget_class, "selection.select-all",
+                                   gtk_label_activate_selection_select_all,
+                                   gtk_label_query_clipboard_action,
+                                   NULL);
+  gtk_widget_class_install_action (widget_class, "link.open",
+                                   gtk_label_activate_link_open,
+                                   gtk_label_query_link_action,
+                                   NULL);
+  gtk_widget_class_install_action (widget_class, "link.copy",
+                                   gtk_label_activate_link_copy,
+                                   gtk_label_query_link_action,
+                                   NULL);
 }
 
 static void 
@@ -1331,7 +1390,7 @@ gtk_label_init (GtkLabel *label)
 
   priv->mnemonics_visible = TRUE;
 
-  gtk_label_add_context_actions (label);
+  gtk_widget_add_class_actions (GTK_WIDGET (label));
 }
 
 
@@ -3205,7 +3264,6 @@ gtk_label_finalize (GObject *object)
   gtk_label_clear_links (label);
   g_free (priv->select_info);
 
-  g_clear_object (&priv->context_actions);
   g_clear_pointer (&priv->popup_menu, gtk_widget_unparent);
   g_clear_object (&priv->extra_menu);
 
@@ -5968,24 +6026,24 @@ gtk_label_select_all (GtkLabel *label)
 }
 
 static void
-open_link_activated (GSimpleAction *action,
-                     GVariant      *parameter,
-                     gpointer       user_data)
+gtk_label_activate_link_open (GtkWidget  *widget,
+                              const char *name,
+                              GVariant   *parameter)
 {
-  GtkLabel *label = GTK_LABEL (user_data);
+  GtkLabel *label = GTK_LABEL (widget);
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
-  GtkLabelLink *link = priv->select_info->context_link;
+   GtkLabelLink *link = priv->select_info->context_link;
 
   if (link)
     emit_activate_link (label, link);
 }
 
 static void
-copy_link_activated (GSimpleAction *action,
-                     GVariant      *parameter,
-                     gpointer       user_data)
+gtk_label_activate_link_copy (GtkWidget  *widget,
+                              const char *name,
+                              GVariant   *parameter)
 {
-  GtkLabel *label = GTK_LABEL (user_data);
+  GtkLabel *label = GTK_LABEL (widget);
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
   GtkLabelLink *link = priv->select_info->context_link;
 
@@ -5993,101 +6051,158 @@ copy_link_activated (GSimpleAction *action,
     {
       GdkClipboard *clipboard;
 
-      clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label));
+      clipboard = gtk_widget_get_clipboard (widget);
       gdk_clipboard_set_text (clipboard, link->uri);
     }
+  else
+    g_print ("no link ?!\n");
 }
 
 static void
-copy_clipboard_activated (GSimpleAction *action,
-                          GVariant      *parameter,
-                          gpointer       user_data)
+gtk_label_activate_clipboard_copy (GtkWidget  *widget,
+                                   const char *name,
+                                   GVariant   *parameter)
 {
-  g_signal_emit_by_name (user_data, "copy-clipboard");
+  g_signal_emit_by_name (widget, "copy-clipboard");
 }
 
 static void
-select_all_activated (GSimpleAction *action,
-                      GVariant      *parameter,
-                      gpointer       user_data)
+gtk_label_activate_selection_select_all (GtkWidget  *widget,
+                                         const char *name,
+                                         GVariant   *parameter)
 {
-  gtk_label_select_all (GTK_LABEL (user_data));
+  gtk_label_select_all (GTK_LABEL (widget));
 }
 
 static void
-gtk_label_update_clipboard_actions (GtkLabel *label)
+gtk_label_nop (GtkWidget  *widget,
+               const char *name,
+               GVariant   *parameter)
+{
+}
+
+static gboolean
+gtk_label_query_clipboard_action (GtkWidget           *widget,
+                                  const char          *name,
+                                  gboolean            *enabled,
+                                  const GVariantType **parameter_type,
+                                  const GVariantType **state_type,
+                                  GVariant           **state_hint,
+                                  GVariant           **state)
 {
+  GtkLabel *label = GTK_LABEL (widget);
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
-  gboolean have_selection = FALSE;
-  GAction *action;
+  gboolean has_selection = FALSE;
+  gboolean enable;
 
   if (priv->select_info)
-    have_selection = priv->select_info->selection_anchor != priv->select_info->selection_end;
+    has_selection = priv->select_info->selection_anchor != priv->select_info->selection_end;
+  else
+    has_selection = FALSE;
+
+  if (strcmp (name, "cut") == 0)
+    enable = FALSE;
+  else if (strcmp (name, "copy") == 0)
+    enable = has_selection;
+  else if (strcmp (name, "paste") == 0)
+    enable = FALSE;
+  else if (strcmp (name, "delete") == 0)
+    enable = FALSE;
+  else if (strcmp (name, "select-all") == 0)
+    enable = gtk_label_get_selectable (label);
+  else
+    return FALSE;
 
-  action = g_action_map_lookup_action (priv->context_actions, "copy-clipboard");
-  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), have_selection);
-  action = g_action_map_lookup_action (priv->context_actions, "select-all");
-  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), gtk_label_get_selectable (label));
+  if (enabled)
+    *enabled = enable;
+  if (parameter_type)
+    *parameter_type = NULL;
+  if (state_type)
+    *state_type = NULL;
+  if (state_hint)
+    *state_hint = NULL;
+  if (state)
+    *state = NULL;
+
+  return TRUE;
 }
 
-static void
-gtk_label_update_link_actions (GtkLabel *label)
+static gboolean
+gtk_label_query_link_action (GtkWidget           *widget,
+                             const char          *name,
+                             gboolean            *enabled,
+                             const GVariantType **parameter_type,
+                             const GVariantType **state_type,
+                             GVariant           **state_hint,
+                             GVariant           **state)
 {
+  GtkLabel *label = GTK_LABEL (widget);
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
-  gboolean have_selection = FALSE;
-  GAction *action;
+  gboolean has_selection = FALSE;
   GtkLabelLink *link;
+  gboolean enable;
+
+  if (priv->select_info)
+    has_selection = priv->select_info->selection_anchor != priv->select_info->selection_end;
+  else
+    has_selection = FALSE;
 
-  have_selection = priv->select_info->selection_anchor != priv->select_info->selection_end;
   if (priv->select_info->link_clicked)
     link = priv->select_info->active_link;
   else
     link = gtk_label_get_focus_link (label);
 
-  action = g_action_map_lookup_action (priv->context_actions, "open-link");
-  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), !have_selection && link);
-  action = g_action_map_lookup_action (priv->context_actions, "copy-link");
-  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), !have_selection && link);
-}
+  if (strcmp (name, "copy") == 0)
+    enable = !has_selection && link;
+  else if (strcmp (name, "open") == 0)
+    enable = !has_selection && link;
+  else
+    return FALSE;
+
+  if (enabled)
+    *enabled = enable;
+  if (parameter_type)
+    *parameter_type = NULL;
+  if (state_type)
+    *state_type = NULL;
+  if (state_hint)
+    *state_hint = NULL;
+  if (state)
+    *state = NULL;
 
+  return TRUE;
+}
+                       
 static void
-gtk_label_add_context_actions (GtkLabel *label)
+gtk_label_update_actions (GtkLabel *label)
 {
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
+  gboolean has_selection;
+  GActionGroup *group;
+  GtkLabelLink *link;
 
-  GActionEntry entries[] = {
-    { "cut-clipboard", NULL, NULL, NULL, NULL },
-    { "copy-clipboard", copy_clipboard_activated, NULL, NULL, NULL },
-    { "paste-clipboard", NULL, NULL, NULL, NULL },
-    { "delete-selection", NULL, NULL, NULL, NULL },
-    { "select-all", select_all_activated, NULL, NULL, NULL },
-    { "open-link", open_link_activated, NULL, NULL, NULL },
-    { "copy-link", copy_link_activated, NULL, NULL, NULL },
-  };
-
-  GSimpleActionGroup *actions = g_simple_action_group_new ();
-  GAction *action;
+  if (priv->select_info)
+    has_selection = priv->select_info->selection_anchor != priv->select_info->selection_end;
+  else
+    has_selection = FALSE;
 
-  priv->context_actions = G_ACTION_MAP (actions);
+  if (priv->select_info->link_clicked)
+    link = priv->select_info->active_link;
+  else
+    link = gtk_label_get_focus_link (label);
 
-  g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), label);
+  group = gtk_widget_get_action_group (GTK_WIDGET (label), "clipboard");
+  g_action_group_action_enabled_changed (group, "cut", FALSE);
+  g_action_group_action_enabled_changed (group, "copy", has_selection);
+  g_action_group_action_enabled_changed (group, "paste", FALSE);
 
-  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), "open-link");
-  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
-  action = g_action_map_lookup_action (G_ACTION_MAP (actions), "copy-link");
-  g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
+  group = gtk_widget_get_action_group (GTK_WIDGET (label), "selection");
+  g_action_group_action_enabled_changed (group, "delete", FALSE);
+  g_action_group_action_enabled_changed (group, "select-all", gtk_label_get_selectable (label));
 
-  gtk_widget_insert_action_group (GTK_WIDGET (label), "context", G_ACTION_GROUP (actions));
+  group = gtk_widget_get_action_group (GTK_WIDGET (label), "link");
+  g_action_group_action_enabled_changed (group, "open", !has_selection && link);
+  g_action_group_action_enabled_changed (group, "copy", !has_selection && link);
 }
 
 static GMenuModel *
@@ -6100,24 +6215,24 @@ gtk_label_get_menu_model (GtkLabel *label)
   menu = g_menu_new ();
 
   section = g_menu_new ();
-  g_menu_append (section, _("Cu_t"), "context.cut-clipboard");
-  g_menu_append (section, _("_Copy"), "context.copy-clipboard");
-  g_menu_append (section, _("_Paste"), "context.paste-clipboard");
-  g_menu_append (section, _("_Delete"), "context.delete-selection");
+  g_menu_append (section, _("Cu_t"), "clipboard.cut");
+  g_menu_append (section, _("_Copy"), "clipboard.copy");
+  g_menu_append (section, _("_Paste"), "clipboard.paste");
+  g_menu_append (section, _("_Delete"), "selection.delete");
   g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
   g_object_unref (section);
 
   section = g_menu_new ();
-  g_menu_append (section, _("Select _All"), "context.select-all");
+  g_menu_append (section, _("Select _All"), "selection.select-all");
   g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
   g_object_unref (section);
 
   section = g_menu_new ();
-  item = g_menu_item_new (_("_Open Link"), "context.open-link");
+  item = g_menu_item_new (_("_Open Link"), "link.open");
   g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
   g_menu_append_item (section, item);
   g_object_unref (item);
-  item = g_menu_item_new (_("Copy _Link Address"), "context.copy-link");
+  item = g_menu_item_new (_("Copy _Link Address"), "link.copy");
   g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
   g_menu_append_item (section, item);
   g_object_unref (item);
@@ -6137,8 +6252,15 @@ gtk_label_do_popup (GtkLabel *label,
 {
   GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
 
-  gtk_label_update_clipboard_actions (label);
-  gtk_label_update_link_actions (label);
+  if (!priv->select_info)
+    return;
+
+  if (priv->select_info->link_clicked)
+    priv->select_info->context_link = priv->select_info->active_link;
+  else
+    priv->select_info->context_link = gtk_label_get_focus_link (label);
+
+  gtk_label_update_actions (label);
 
   if (!priv->popup_menu)
     {
@@ -6169,15 +6291,6 @@ static gboolean
 gtk_label_popup_menu (GtkWidget *widget)
 {
   GtkLabel *label = GTK_LABEL (widget);
-  GtkLabelPrivate *priv = gtk_label_get_instance_private (label);
-
-  if (!priv->select_info)
-    return FALSE;
-
-  if (priv->select_info->link_clicked)
-    priv->select_info->context_link = priv->select_info->active_link;
-  else
-    priv->select_info->context_link = gtk_label_get_focus_link (label);
 
   gtk_label_do_popup (label, -1, -1);
   return TRUE;


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