[gtk/wip/matthiasc/focus3: 4/32] Port widgets to the root focus API



commit 4e4e563fd49767e70bbdddfd9541954b2cf85fef
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Mar 2 08:49:00 2019 -0500

    Port widgets to the root focus API

 gtk/gtkcoloreditor.c       |  3 ++-
 gtk/gtkfilechooserwidget.c | 13 +++++++------
 gtk/gtklabel.c             |  2 +-
 gtk/gtkmain.c              |  4 ++--
 gtk/gtknotebook.c          |  8 +++++++-
 gtk/gtkpaned.c             | 12 +++---------
 gtk/gtkplacesview.c        |  2 +-
 gtk/gtkstack.c             | 34 ++++++++++++++++------------------
 gtk/gtktext.c              |  2 +-
 gtk/gtktreeviewcolumn.c    |  7 ++-----
 gtk/gtkwidget.c            | 25 +++++++------------------
 gtk/gtkwidgetfocus.c       | 10 +++-------
 gtk/inspector/misc-info.c  |  4 ++--
 tests/testtoolbar.c        |  2 +-
 14 files changed, 55 insertions(+), 73 deletions(-)
---
diff --git a/gtk/gtkcoloreditor.c b/gtk/gtkcoloreditor.c
index 34f5991ca2..8670027631 100644
--- a/gtk/gtkcoloreditor.c
+++ b/gtk/gtkcoloreditor.c
@@ -36,6 +36,7 @@
 #include "gtkspinbutton.h"
 #include "gtkstylecontext.h"
 #include "gtkeventcontrollerkey.h"
+#include "gtkroot.h"
 
 #include <math.h>
 
@@ -224,7 +225,7 @@ popup_edit (GtkWidget      *widget,
     {
       dismiss_current_popup (editor);
       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (editor));
-      g_set_object (&editor->priv->popdown_focus, gtk_window_get_focus (GTK_WINDOW (toplevel)));
+      g_set_object (&editor->priv->popdown_focus, gtk_root_get_focus (GTK_ROOT (toplevel)));
       editor->priv->current_popup = popup;
       editor->priv->popup_position = position;
       gtk_widget_show (popup);
diff --git a/gtk/gtkfilechooserwidget.c b/gtk/gtkfilechooserwidget.c
index 4d9ab00e05..7c7b32d119 100644
--- a/gtk/gtkfilechooserwidget.c
+++ b/gtk/gtkfilechooserwidget.c
@@ -82,6 +82,7 @@
 #include "gtkdebug.h"
 #include "gtkfilechoosererrorstackprivate.h"
 #include "gtkentryprivate.h"
+#include "gtkroot.h"
 
 #include <cairo-gobject.h>
 
@@ -1361,7 +1362,7 @@ key_press_cb (GtkEventController *controller,
           GtkWidget *default_widget, *focus_widget;
 
           default_widget = gtk_window_get_default_widget (window);
-          focus_widget = gtk_window_get_focus (window);
+          focus_widget = gtk_root_get_focus (GTK_ROOT (window));
 
           if (widget != default_widget &&
               !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
@@ -2709,7 +2710,7 @@ location_mode_set (GtkFileChooserWidget *impl,
           switch_to_file_list = FALSE;
           if (toplevel)
             {
-              current_focus = gtk_window_get_focus (toplevel);
+              current_focus = gtk_root_get_focus (GTK_ROOT (toplevel));
               if (!current_focus || current_focus == priv->location_entry)
                 switch_to_file_list = TRUE;
             }
@@ -3566,7 +3567,7 @@ toplevel_set_focus_cb (GtkWindow             *window,
 {
   GtkFileChooserWidgetPrivate *priv = impl->priv;
 
-  priv->toplevel_last_focus_widget = gtk_window_get_focus (window);
+  priv->toplevel_last_focus_widget = gtk_root_get_focus (GTK_ROOT (window));
 }
 
 /* We monitor the focus widget on our toplevel to be able to know which widget
@@ -3586,7 +3587,7 @@ gtk_file_chooser_widget_root (GtkWidget *widget)
   g_assert (priv->toplevel_set_focus_id == 0);
   priv->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
                                                   G_CALLBACK (toplevel_set_focus_cb), impl);
-  priv->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
+  priv->toplevel_last_focus_widget = gtk_root_get_focus (GTK_ROOT (toplevel));
 }
 
 static void
@@ -5806,7 +5807,7 @@ gtk_file_chooser_widget_get_files (GtkFileChooser *chooser)
 
   toplevel = get_toplevel (GTK_WIDGET (impl));
   if (toplevel)
-    current_focus = gtk_window_get_focus (toplevel);
+    current_focus = gtk_root_get_focus (GTK_ROOT (toplevel));
   else
     current_focus = NULL;
 
@@ -6655,7 +6656,7 @@ gtk_file_chooser_widget_should_respond (GtkFileChooserEmbed *chooser_embed)
 
   retval = FALSE;
 
-  current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
+  current_focus = gtk_root_get_focus (GTK_ROOT (toplevel));
 
   if (current_focus == priv->browse_files_tree_view)
     {
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 86442d16a1..e3be68e02e 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -6222,7 +6222,7 @@ gtk_label_activate_current_link (GtkLabel *label)
           if (window)
             {
               default_widget = gtk_window_get_default_widget (window);
-              focus_widget = gtk_window_get_focus (window);
+              focus_widget = gtk_root_get_focus (GTK_ROOT (window));
 
               if (default_widget != widget &&
                   !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive 
(default_widget))))
diff --git a/gtk/gtkmain.c b/gtk/gtkmain.c
index 8aa278e9eb..be19d7389c 100644
--- a/gtk/gtkmain.c
+++ b/gtk/gtkmain.c
@@ -1761,7 +1761,7 @@ gtk_main_do_event (GdkEvent *event)
 
   if (is_pointing_event (event))
     target_widget = handle_pointing_event (event);
-  else if (GTK_IS_WINDOW (target_widget) &&
+  else if (GTK_IS_ROOT (target_widget) &&
            (event->any.type == GDK_KEY_PRESS ||
             event->any.type == GDK_KEY_RELEASE))
     {
@@ -1771,7 +1771,7 @@ gtk_main_do_event (GdkEvent *event)
           gtk_window_activate_key (GTK_WINDOW (target_widget), (GdkEventKey *) event))
         goto cleanup;
 
-      focus_widget = gtk_window_get_focus (GTK_WINDOW (target_widget));
+      focus_widget = gtk_root_get_focus (GTK_ROOT (target_widget));
       if (focus_widget)
         target_widget = focus_widget;
     }
diff --git a/gtk/gtknotebook.c b/gtk/gtknotebook.c
index d11ee42584..a23f9c41b7 100644
--- a/gtk/gtknotebook.c
+++ b/gtk/gtknotebook.c
@@ -5426,7 +5426,13 @@ gtk_notebook_real_switch_page (GtkNotebook     *notebook,
   child_has_focus = priv->child_has_focus;
 
   if (priv->cur_page)
-    gtk_widget_unset_state_flags (priv->cur_page->tab_widget, GTK_STATE_FLAG_CHECKED);
+    {
+      GtkRoot *root = gtk_widget_get_root (GTK_WIDGET (notebook));
+      GtkWidget *focus = gtk_root_get_focus (root);
+      if (focus)
+        child_has_focus = gtk_widget_is_ancestor (focus, priv->cur_page->child);
+      gtk_widget_unset_state_flags (priv->cur_page->tab_widget, GTK_STATE_FLAG_CHECKED);
+    }
 
   priv->cur_page = page;
   gtk_widget_set_state_flags (page->tab_widget, GTK_STATE_FLAG_CHECKED, FALSE);
diff --git a/gtk/gtkpaned.c b/gtk/gtkpaned.c
index 9fc18c9667..c09b3d9142 100644
--- a/gtk/gtkpaned.c
+++ b/gtk/gtkpaned.c
@@ -2256,10 +2256,8 @@ gtk_paned_restore_focus (GtkPaned *paned)
          
          if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
            {
-             GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
-             
-             if (GTK_IS_WINDOW (toplevel))
-               gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
+              GtkRoot *root = gtk_widget_get_root (GTK_WIDGET (paned));
+              gtk_root_set_focus (root, NULL);
            }
        }
       
@@ -2364,7 +2362,6 @@ gtk_paned_cycle_handle_focus (GtkPaned *paned,
     {
       GtkPaned *focus;
       GtkPaned *first;
-      GtkWidget *toplevel;
       GtkWidget *focus_child;
 
       gtk_paned_find_neighbours (paned, &next, &prev);
@@ -2410,10 +2407,7 @@ gtk_paned_cycle_handle_focus (GtkPaned *paned,
            first = next;
        }
 
-      toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
-
-      if (GTK_IS_WINDOW (toplevel))
-        gtk_paned_set_saved_focus (focus, gtk_window_get_focus (GTK_WINDOW (toplevel)));
+      gtk_paned_set_saved_focus (focus, gtk_root_get_focus (gtk_widget_get_root (GTK_WIDGET (paned))));
       gtk_paned_set_first_paned (focus, first);
       priv->original_position = gtk_paned_get_position (focus);
 
diff --git a/gtk/gtkplacesview.c b/gtk/gtkplacesview.c
index bb03ef9d01..dabb486046 100644
--- a/gtk/gtkplacesview.c
+++ b/gtk/gtkplacesview.c
@@ -1766,7 +1766,7 @@ on_key_press_event (GtkEventController *controller,
       if (!toplevel)
         return FALSE;
 
-      focus_widget = gtk_window_get_focus (toplevel);
+      focus_widget = gtk_root_get_focus (GTK_ROOT (toplevel));
 
       if (!GTK_IS_PLACES_VIEW_ROW (focus_widget))
         return FALSE;
diff --git a/gtk/gtkstack.c b/gtk/gtkstack.c
index f9a7723448..d7222f9e5e 100644
--- a/gtk/gtkstack.c
+++ b/gtk/gtkstack.c
@@ -1123,7 +1123,6 @@ set_visible_child (GtkStack               *stack,
   GtkStackPage *info;
   GtkWidget *widget = GTK_WIDGET (stack);
   GList *l;
-  GtkWidget *toplevel;
   GtkWidget *focus;
   gboolean contains_focus = FALSE;
   guint old_pos = GTK_INVALID_LIST_POSITION;
@@ -1165,24 +1164,23 @@ set_visible_child (GtkStack               *stack,
         }
     }
 
-  toplevel = gtk_widget_get_toplevel (widget);
-  if (GTK_IS_WINDOW (toplevel))
+  if (gtk_widget_get_root (widget))
+    focus = gtk_root_get_focus (gtk_widget_get_root (widget));
+  else
+    focus = NULL;
+  if (focus &&
+      priv->visible_child &&
+      priv->visible_child->widget &&
+      gtk_widget_is_ancestor (focus, priv->visible_child->widget))
     {
-      focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
-      if (focus &&
-          priv->visible_child &&
-          priv->visible_child->widget &&
-          gtk_widget_is_ancestor (focus, priv->visible_child->widget))
-        {
-          contains_focus = TRUE;
-
-          if (priv->visible_child->last_focus)
-            g_object_remove_weak_pointer (G_OBJECT (priv->visible_child->last_focus),
-                                          (gpointer *)&priv->visible_child->last_focus);
-          priv->visible_child->last_focus = focus;
-          g_object_add_weak_pointer (G_OBJECT (priv->visible_child->last_focus),
-                                     (gpointer *)&priv->visible_child->last_focus);
-        }
+      contains_focus = TRUE;
+
+      if (priv->visible_child->last_focus)
+        g_object_remove_weak_pointer (G_OBJECT (priv->visible_child->last_focus),
+                                      (gpointer *)&priv->visible_child->last_focus);
+      priv->visible_child->last_focus = focus;
+      g_object_add_weak_pointer (G_OBJECT (priv->visible_child->last_focus),
+                                 (gpointer *)&priv->visible_child->last_focus);
     }
 
   if (priv->last_visible_child)
diff --git a/gtk/gtktext.c b/gtk/gtktext.c
index 99a5afba37..7a4d6cc2b9 100644
--- a/gtk/gtktext.c
+++ b/gtk/gtktext.c
@@ -3810,7 +3810,7 @@ gtk_text_real_activate (GtkText *self)
           if (window)
             {
               default_widget = gtk_window_get_default_widget (window);
-              focus_widget = gtk_window_get_focus (window);
+              focus_widget = gtk_root_get_focus (GTK_ROOT (window));
               if (widget != default_widget &&
                   !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive 
(default_widget))))
                 gtk_window_activate_default (window);
diff --git a/gtk/gtktreeviewcolumn.c b/gtk/gtktreeviewcolumn.c
index 5817a5f2f8..8c113ad3c4 100644
--- a/gtk/gtktreeviewcolumn.c
+++ b/gtk/gtktreeviewcolumn.c
@@ -1016,11 +1016,8 @@ gtk_tree_view_column_update_button (GtkTreeViewColumn *tree_column)
       gtk_widget_set_can_focus (priv->button, FALSE);
       if (gtk_widget_has_focus (priv->button))
        {
-         GtkWidget *toplevel = gtk_widget_get_toplevel (priv->tree_view);
-         if (gtk_widget_is_toplevel (toplevel))
-           {
-             gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
-           }
+          GtkRoot *root = gtk_widget_get_root (priv->tree_view);
+         gtk_root_set_focus (root, NULL);
        }
     }
   /* Queue a resize on the assumption that we always want to catch all changes
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 20628e3fe2..881cb9c2f1 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -5346,10 +5346,8 @@ gtk_widget_grab_focus (GtkWidget *widget)
 static void
 gtk_widget_real_grab_focus (GtkWidget *focus_widget)
 {
-  GtkWidget *toplevel;
-
-  toplevel = gtk_widget_get_toplevel (focus_widget);
-  gtk_window_set_focus (GTK_WINDOW (toplevel), focus_widget);
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (focus_widget);
+  gtk_root_set_focus (priv->root, focus_widget);
 }
 
 static gboolean
@@ -5634,16 +5632,14 @@ gtk_widget_has_visible_focus (GtkWidget *widget)
 gboolean
 gtk_widget_is_focus (GtkWidget *widget)
 {
-  GtkWidget *toplevel;
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
 
   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
 
-  toplevel = _gtk_widget_get_toplevel (widget);
+  if (priv->root)
+    return widget == gtk_root_get_focus (priv->root);
 
-  if (GTK_IS_WINDOW (toplevel))
-    return widget == gtk_window_get_focus (GTK_WINDOW (toplevel));
-  else
-    return FALSE;
+  return FALSE;
 }
 
 /**
@@ -8849,14 +8845,7 @@ gtk_widget_propagate_state (GtkWidget          *widget,
     priv->state_flags |= GTK_STATE_FLAG_INSENSITIVE;
 
   if (gtk_widget_is_focus (widget) && !gtk_widget_is_sensitive (widget))
-    {
-      GtkWidget *window;
-
-      window = _gtk_widget_get_toplevel (widget);
-
-      if (window && _gtk_widget_is_toplevel (window))
-        gtk_window_set_focus (GTK_WINDOW (window), NULL);
-    }
+    gtk_root_set_focus (priv->root, NULL);
 
   new_flags = priv->state_flags;
 
diff --git a/gtk/gtkwidgetfocus.c b/gtk/gtkwidgetfocus.c
index af68bd7a56..b1abae3894 100644
--- a/gtk/gtkwidgetfocus.c
+++ b/gtk/gtkwidgetfocus.c
@@ -155,15 +155,11 @@ static gboolean
 old_focus_coords (GtkWidget       *widget,
                   graphene_rect_t *old_focus_bounds)
 {
-  GtkWidget *toplevel = _gtk_widget_get_toplevel (widget);
   GtkWidget *old_focus;
 
-  if (GTK_IS_WINDOW (toplevel))
-    {
-      old_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
-      if (old_focus)
-        return gtk_widget_compute_bounds (old_focus, widget, old_focus_bounds);
-    }
+  old_focus = gtk_root_get_focus (gtk_widget_get_root (widget));
+  if (old_focus)
+    return gtk_widget_compute_bounds (old_focus, widget, old_focus_bounds);
 
   return FALSE;
 }
diff --git a/gtk/inspector/misc-info.c b/gtk/inspector/misc-info.c
index 9c3b3ec05f..f352894daf 100644
--- a/gtk/inspector/misc-info.c
+++ b/gtk/inspector/misc-info.c
@@ -222,7 +222,7 @@ update_focus_widget (GtkInspectorMiscInfo *sl)
 {
   GtkWidget *widget;
 
-  widget = gtk_window_get_focus (GTK_WINDOW (sl->priv->object));
+  widget = gtk_root_get_focus (GTK_ROOT (sl->priv->object));
   if (widget)
     {
       gchar *tmp;
@@ -249,7 +249,7 @@ show_focus_widget (GtkWidget *button, GtkInspectorMiscInfo *sl)
 {
   GtkWidget *widget;
 
-  widget = gtk_window_get_focus (GTK_WINDOW (sl->priv->object));
+  widget = gtk_root_get_focus (GTK_ROOT (sl->priv->object));
   if (widget)
     show_object (sl, G_OBJECT (widget), "properties");
 }
diff --git a/tests/testtoolbar.c b/tests/testtoolbar.c
index 57c7ceb1b3..12160c8115 100644
--- a/tests/testtoolbar.c
+++ b/tests/testtoolbar.c
@@ -344,7 +344,7 @@ popup_context_menu (GtkToolbar *toolbar, gint x, gint y, gint button_number)
       GtkWidget *widget;
 
       window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (toolbar)));
-      widget = gtk_window_get_focus (window);
+      widget = gtk_root_get_focus (GTK_ROOT (window));
       if (!widget)
         widget = GTK_WIDGET (toolbar);
 


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