[gtk/wip/matthiasc/focus2: 55/91] entry: Redo select-on-focus handling



commit a844c9d6caff50fa427e1fdb7e6d4538ee3f8c76
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Mar 3 12:06:56 2019 -0500

    entry: Redo select-on-focus handling
    
    GtkText was handling this just on grab_focus, which is
    not the only way anymore that focus can reach it. Instead,
    shift this to focus-in, and add a property that can be
    used to override the select-on-focus setting. Remove
    the gtk_text_grab_focus_without_selecting(), but keep
    the GtkEntry variant of the function, reimplemented
    to use the properties. GtkSearchEntry sets the select-on-focus
    property of its text to FALSE, to match expectations.

 docs/reference/gtk/gtk4-sections.txt |   1 -
 gtk/gtkentry.c                       |  17 +++++-
 gtk/gtksearchentry.c                 |   3 +-
 gtk/gtktext.c                        | 112 +++++++++++++++++++++--------------
 gtk/gtktext.h                        |   3 -
 5 files changed, 86 insertions(+), 50 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 911135eb39..699cb2d56b 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -904,7 +904,6 @@ gtk_text_set_attributes
 gtk_text_get_attributes
 gtk_text_set_tabs
 gtk_text_get_tabs
-gtk_text_grab_focus_without_selecting
 <SUBSECTION Private>
 gtk_text_get_type
 </SECTION>
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index ff52184a41..a57fb5e50f 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -1652,10 +1652,23 @@ gtk_entry_snapshot (GtkWidget   *widget,
 void
 gtk_entry_grab_focus_without_selecting (GtkEntry *entry)
 {
-  g_return_if_fail (GTK_IS_ENTRY (entry));
   GtkEntryPrivate *priv = gtk_entry_get_instance_private (entry);
+  gboolean select, set;
+
+  g_return_if_fail (GTK_IS_ENTRY (entry));
+
+  g_object_get (priv->text,
+                "select-on-focus", &select,
+                "select-on-focus-set", &set,
+                NULL);
+  g_object_set (priv->text, "select-on-focus", FALSE, NULL);
+
+  gtk_widget_grab_focus (priv->text);
 
-  gtk_text_grab_focus_without_selecting (GTK_TEXT (priv->text));
+  g_object_set (priv->text,
+                "select-on-focus", select,
+                "select-on-focus-set", set,
+                NULL);
 }
 
 static void
diff --git a/gtk/gtksearchentry.c b/gtk/gtksearchentry.c
index fd6dfed05f..b9495241a5 100644
--- a/gtk/gtksearchentry.c
+++ b/gtk/gtksearchentry.c
@@ -255,7 +255,7 @@ gtk_search_entry_grab_focus (GtkWidget *widget)
   GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget);
   GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
 
-  gtk_text_grab_focus_without_selecting (GTK_TEXT (priv->entry));
+  gtk_widget_grab_focus (priv->entry);
 }
 
 static gboolean
@@ -523,6 +523,7 @@ gtk_search_entry_init (GtkSearchEntry *entry)
   gtk_widget_set_vexpand (priv->box, FALSE);
 
   priv->entry = gtk_text_new ();
+  g_object_set (priv->entry, "select-on-focus", FALSE, NULL);
   gtk_widget_set_hexpand (priv->entry, TRUE);
   gtk_widget_set_vexpand (priv->entry, TRUE);
   gtk_container_add (GTK_CONTAINER (priv->box), GTK_WIDGET (priv->entry));
diff --git a/gtk/gtktext.c b/gtk/gtktext.c
index ed721eca6b..b33889556e 100644
--- a/gtk/gtktext.c
+++ b/gtk/gtktext.c
@@ -225,6 +225,8 @@ struct _GtkTextPrivate
   guint         cursor_handle_dragged   : 1;
   guint         selection_handle_dragged : 1;
   guint         populate_all            : 1;
+  guint         select_on_focus         : 1;
+  guint         select_on_focus_set     : 1;
 };
 
 struct _GtkTextPasswordHint
@@ -268,6 +270,8 @@ enum {
   PROP_POPULATE_ALL,
   PROP_TABS,
   PROP_ENABLE_EMOJI_COMPLETION,
+  PROP_SELECT_ON_FOCUS,
+  PROP_SELECT_ON_FOCUS_SET,
   NUM_PROPERTIES
 };
 
@@ -321,7 +325,6 @@ static void   gtk_text_snapshot             (GtkWidget        *widget,
                                              GtkSnapshot      *snapshot);
 static void   gtk_text_focus_in             (GtkWidget        *widget);
 static void   gtk_text_focus_out            (GtkWidget        *widget);
-static void   gtk_text_grab_focus           (GtkWidget        *widget);
 static void   gtk_text_style_updated        (GtkWidget        *widget);
 static void   gtk_text_direction_changed    (GtkWidget        *widget,
                                              GtkTextDirection  previous_dir);
@@ -675,7 +678,6 @@ gtk_text_class_init (GtkTextClass *class)
   widget_class->measure = gtk_text_measure;
   widget_class->size_allocate = gtk_text_size_allocate;
   widget_class->snapshot = gtk_text_snapshot;
-  widget_class->grab_focus = gtk_text_grab_focus;
   widget_class->style_updated = gtk_text_style_updated;
   widget_class->drag_begin = gtk_text_drag_begin;
   widget_class->drag_end = gtk_text_drag_end;
@@ -898,6 +900,26 @@ gtk_text_class_init (GtkTextClass *class)
                             TRUE,
                             GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkEntry::select-on-focus:
+   *
+   * Whether to select the contents of the text when focus enters it.
+   * When set, this property overrides the system-widget setting for
+   * this feature. See #GtkEntry::select-on-enter-set
+   */
+  text_props[PROP_SELECT_ON_FOCUS] =
+      g_param_spec_boolean ("select-on-focus",
+                            P_("Selet on focus"),
+                            P_("Whether to select the text on focus"),
+                            FALSE,
+                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
+  text_props[PROP_SELECT_ON_FOCUS_SET] =
+      g_param_spec_boolean ("select-on-focus-set",
+                            P_("Select on focus set"),
+                            P_("Whether the select-on-focus property has been set"),
+                            FALSE,
+                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
+
   g_object_class_install_properties (gobject_class, NUM_PROPERTIES, text_props);
 
   gtk_editable_install_properties (gobject_class, NUM_PROPERTIES);
@@ -1515,6 +1537,27 @@ gtk_text_set_property (GObject      *object,
       set_enable_emoji_completion (self, g_value_get_boolean (value));
       break;
 
+    case PROP_SELECT_ON_FOCUS:
+      if (priv->select_on_focus != g_value_get_boolean (value))
+        {
+          priv->select_on_focus = g_value_get_boolean (value);
+          g_object_notify_by_pspec (object, pspec);
+        }
+      if (!priv->select_on_focus_set)
+        {
+          priv->select_on_focus_set = TRUE;
+          g_object_notify_by_pspec (object, text_props[PROP_SELECT_ON_FOCUS_SET]);
+        }
+      break;
+
+    case PROP_SELECT_ON_FOCUS_SET:
+      if (priv->select_on_focus_set != g_value_get_boolean (value))
+        {
+          priv->select_on_focus_set = g_value_get_boolean (value);
+          g_object_notify_by_pspec (object, pspec);
+        }
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1630,6 +1673,14 @@ gtk_text_get_property (GObject    *object,
       g_value_set_boolean (value, priv->enable_emoji_completion);
       break;
 
+    case PROP_SELECT_ON_FOCUS:
+      g_value_set_boolean (value, priv->select_on_focus);
+      break;
+
+    case PROP_SELECT_ON_FOCUS_SET:
+      g_value_set_boolean (value, priv->select_on_focus_set);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -2896,6 +2947,22 @@ gtk_text_focus_in (GtkWidget *widget)
   GtkTextPrivate *priv = gtk_text_get_instance_private (self);
   GdkKeymap *keymap;
 
+  if (priv->editable && !priv->in_click)
+    {
+      gboolean select_on_focus;
+
+      if (priv->select_on_focus_set)
+        select_on_focus = priv->select_on_focus;
+      else
+        g_object_get (gtk_widget_get_settings (widget),
+                      "gtk-entry-select-on-focus",
+                      &select_on_focus,
+                      NULL);
+
+      if (select_on_focus)
+        gtk_text_set_selection_bounds (self, 0, -1);
+    }
+
   gtk_widget_queue_draw (widget);
 
   keymap = gdk_display_get_keymap (gtk_widget_get_display (widget));
@@ -2941,47 +3008,6 @@ gtk_text_focus_out (GtkWidget *widget)
   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, self);
 }
 
-static void
-gtk_text_grab_focus (GtkWidget *widget)
-{
-  GtkText *self = GTK_TEXT (widget);
-  GtkTextPrivate *priv = gtk_text_get_instance_private (self);
-  gboolean select_on_focus;
-
-  GTK_WIDGET_CLASS (gtk_text_parent_class)->grab_focus (GTK_WIDGET (self));
-
-  if (priv->editable && !priv->in_click)
-    {
-      g_object_get (gtk_widget_get_settings (widget),
-                    "gtk-entry-select-on-focus",
-                    &select_on_focus,
-                    NULL);
-
-      if (select_on_focus)
-        gtk_text_set_selection_bounds (self, 0, -1);
-    }
-}
-
-/**
- * gtk_text_grab_focus_without_selecting:
- * @self: a #GtkText
- *
- * Causes @self to have keyboard focus.
- *
- * It behaves like gtk_widget_grab_focus(),
- * except that it doesn't select the contents of the self.
- * You only want to call this on some special entries
- * which the user usually doesn't want to replace all text in,
- * such as search-as-you-type entries.
- */
-void
-gtk_text_grab_focus_without_selecting (GtkText *self)
-{
-  g_return_if_fail (GTK_IS_TEXT (self));
-
-  GTK_WIDGET_CLASS (gtk_text_parent_class)->grab_focus (GTK_WIDGET (self));
-}
-
 static void
 gtk_text_direction_changed (GtkWidget        *widget,
                             GtkTextDirection  previous_dir)
diff --git a/gtk/gtktext.h b/gtk/gtktext.h
index 9907daee9d..96488a1f33 100644
--- a/gtk/gtktext.h
+++ b/gtk/gtktext.h
@@ -131,9 +131,6 @@ void            gtk_text_set_tabs                       (GtkText         *self,
 GDK_AVAILABLE_IN_ALL
 PangoTabArray * gtk_text_get_tabs                       (GtkText         *self);
 
-GDK_AVAILABLE_IN_ALL
-void            gtk_text_grab_focus_without_selecting   (GtkText         *self);
-
 G_END_DECLS
 
 #endif /* __GTK_TEXT_H__ */


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