[gtk+] widget: Save pointer to focus child



commit 43cdeee3c4a5486664e68394d08469c5a39770a3
Author: Timm Bäder <mail baedert org>
Date:   Wed Feb 8 09:00:29 2017 +0100

    widget: Save pointer to focus child
    
    Do the same thing GtkContainer does.

 gtk/gtkcontainer.c      |    9 ++++-----
 gtk/gtkexpander.c       |    4 ++--
 gtk/gtkflowbox.c        |    4 ++--
 gtk/gtklistbox.c        |    4 ++--
 gtk/gtknotebook.c       |    7 ++-----
 gtk/gtkpaned.c          |    8 +++-----
 gtk/gtkpopover.c        |    2 +-
 gtk/gtkscrolledwindow.c |    2 +-
 gtk/gtktextview.c       |    5 +----
 gtk/gtktoolbar.c        |    7 +++----
 gtk/gtktreeview.c       |    5 ++---
 gtk/gtkwidget.c         |   28 ++++++++++++++++++----------
 gtk/gtkwidgetprivate.h  |    4 ++++
 gtk/gtkwindow.c         |    8 ++++----
 14 files changed, 49 insertions(+), 48 deletions(-)
---
diff --git a/gtk/gtkcontainer.c b/gtk/gtkcontainer.c
index 8968983..2065892 100644
--- a/gtk/gtkcontainer.c
+++ b/gtk/gtkcontainer.c
@@ -2114,10 +2114,9 @@ gtk_container_real_set_focus_child (GtkContainer     *container,
         {
 
           focus_child = priv->focus_child;
-          while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER 
(focus_child)))
-            {
-              focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child));
-            }
+
+          while (gtk_widget_get_focus_child (focus_child))
+            focus_child = gtk_widget_get_focus_child (focus_child);
 
           gtk_widget_translate_coordinates (focus_child, priv->focus_child,
                                             0, 0, &x, &y);
@@ -2310,7 +2309,7 @@ find_old_focus (GtkContainer *container,
 
           parent = _gtk_widget_get_parent (widget);
 
-          if (parent && (gtk_container_get_focus_child (GTK_CONTAINER (parent)) != widget))
+          if (parent && (gtk_widget_get_focus_child (parent) != widget))
             goto next;
 
           widget = parent;
diff --git a/gtk/gtkexpander.c b/gtk/gtkexpander.c
index d17d2c5..a2aad91 100644
--- a/gtk/gtkexpander.c
+++ b/gtk/gtkexpander.c
@@ -796,7 +796,7 @@ focus_current_site (GtkExpander      *expander,
 {
   GtkWidget *current_focus;
 
-  current_focus = gtk_container_get_focus_child (GTK_CONTAINER (expander));
+  current_focus = gtk_widget_get_focus_child (GTK_WIDGET (expander));
 
   if (!current_focus)
     return FALSE;
@@ -962,7 +962,7 @@ gtk_expander_focus (GtkWidget        *widget,
       FocusSite site = FOCUS_NONE;
 
       widget_is_focus = gtk_widget_is_focus (widget);
-      old_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget));
+      old_focus_child = gtk_widget_get_focus_child (GTK_WIDGET (widget));
 
       if (old_focus_child && old_focus_child == expander->priv->label_widget)
         site = FOCUS_LABEL;
diff --git a/gtk/gtkflowbox.c b/gtk/gtkflowbox.c
index 6bb31a0..d443047 100644
--- a/gtk/gtkflowbox.c
+++ b/gtk/gtkflowbox.c
@@ -342,7 +342,7 @@ gtk_flow_box_child_focus (GtkWidget        *widget,
 
       return FALSE;
     }
-  else if (gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL)
+  else if (gtk_widget_get_focus_child (widget) != NULL)
     {
       /* Child has focus, always navigate inside it first */
       if (gtk_widget_child_focus (child, direction))
@@ -3188,7 +3188,7 @@ gtk_flow_box_focus (GtkWidget        *widget,
       return GTK_WIDGET_CLASS (gtk_flow_box_parent_class)->focus (widget, direction);
     }
 
-  focus_child = gtk_container_get_focus_child (GTK_CONTAINER (box));
+  focus_child = gtk_widget_get_focus_child (widget);
   next_focus_child = NULL;
 
   if (focus_child != NULL)
diff --git a/gtk/gtklistbox.c b/gtk/gtklistbox.c
index e3325d7..31cfc45 100644
--- a/gtk/gtklistbox.c
+++ b/gtk/gtklistbox.c
@@ -2016,7 +2016,7 @@ gtk_list_box_focus (GtkWidget        *widget,
   GtkWidget *row;
   GtkWidget *header;
 
-  focus_child = gtk_container_get_focus_child ((GtkContainer *)box);
+  focus_child = gtk_widget_get_focus_child (widget);
 
   next_focus_row = NULL;
   if (focus_child != NULL)
@@ -3207,7 +3207,7 @@ gtk_list_box_row_focus (GtkWidget        *widget,
 
       return FALSE;
     }
-  else if (gtk_container_get_focus_child (GTK_CONTAINER (row)) != NULL)
+  else if (gtk_widget_get_focus_child (widget) != NULL)
     {
       /* Child has focus, always navigate inside it first */
       if (gtk_widget_child_focus (child, direction))
diff --git a/gtk/gtknotebook.c b/gtk/gtknotebook.c
index 566f481..707bec6 100644
--- a/gtk/gtknotebook.c
+++ b/gtk/gtknotebook.c
@@ -1436,7 +1436,7 @@ gtk_notebook_move_focus_out (GtkNotebook      *notebook,
   GtkDirectionType effective_direction = get_effective_direction (notebook, direction_type);
   GtkWidget *toplevel;
 
-  if (gtk_container_get_focus_child (GTK_CONTAINER (notebook)) && effective_direction == GTK_DIR_UP)
+  if (gtk_widget_get_focus_child (GTK_WIDGET (notebook)) && effective_direction == GTK_DIR_UP)
     if (focus_tabs_in (notebook))
       return;
   if (gtk_widget_is_focus (GTK_WIDGET (notebook)) && effective_direction == GTK_DIR_DOWN)
@@ -4180,9 +4180,6 @@ gtk_notebook_focus (GtkWidget        *widget,
   gint last_action;
 
   gboolean widget_is_focus;
-  GtkContainer *container;
-
-  container = GTK_CONTAINER (widget);
 
   if (priv->tab_pos == GTK_POS_TOP ||
       priv->tab_pos == GTK_POS_LEFT)
@@ -4203,7 +4200,7 @@ gtk_notebook_focus (GtkWidget        *widget,
     }
 
   widget_is_focus = gtk_widget_is_focus (widget);
-  old_focus_child = gtk_container_get_focus_child (container);
+  old_focus_child = gtk_widget_get_focus_child (widget);
 
   effective_direction = get_effective_direction (notebook, direction);
 
diff --git a/gtk/gtkpaned.c b/gtk/gtkpaned.c
index 2393847..a17c907 100644
--- a/gtk/gtkpaned.c
+++ b/gtk/gtkpaned.c
@@ -2449,7 +2449,7 @@ gtk_paned_set_focus_child (GtkContainer *container,
            if (GTK_IS_PANED (w))
              last_focus = w;
 
-          container_focus_child = gtk_container_get_focus_child (container);
+          container_focus_child = gtk_widget_get_focus_child (GTK_WIDGET (container));
           if (container_focus_child == priv->child1)
            gtk_paned_set_last_child1_focus (paned, last_focus);
          else if (container_focus_child == priv->child2)
@@ -2467,7 +2467,6 @@ gtk_paned_get_cycle_chain (GtkPaned          *paned,
                           GList            **widgets)
 {
   GtkPanedPrivate *priv = paned->priv;
-  GtkContainer *container = GTK_CONTAINER (paned);
   GtkWidget *ancestor = NULL;
   GtkWidget *focus_child;
   GtkWidget *parent;
@@ -2504,7 +2503,7 @@ gtk_paned_get_cycle_chain (GtkPaned          *paned,
    * priv->last_child?_focus before priv->child?, both when we
    * are going forward and backward.
    */
-  focus_child = gtk_container_get_focus_child (container);
+  focus_child = gtk_widget_get_focus_child (GTK_WIDGET (paned));
   if (direction == GTK_DIR_TAB_FORWARD)
     {
       if (focus_child == priv->child1)
@@ -2880,14 +2879,13 @@ gtk_paned_cycle_handle_focus (GtkPaned *paned,
     }
   else
     {
-      GtkContainer *container = GTK_CONTAINER (paned);
       GtkPaned *focus;
       GtkPaned *first;
       GtkWidget *toplevel;
       GtkWidget *focus_child;
 
       gtk_paned_find_neighbours (paned, &next, &prev);
-      focus_child = gtk_container_get_focus_child (container);
+      focus_child = gtk_widget_get_focus_child (GTK_WIDGET (paned));
 
       if (focus_child == priv->child1)
        {
diff --git a/gtk/gtkpopover.c b/gtk/gtkpopover.c
index 94311ab..6c72402 100644
--- a/gtk/gtkpopover.c
+++ b/gtk/gtkpopover.c
@@ -1511,7 +1511,7 @@ gtk_popover_focus (GtkWidget        *widget,
        */
       while (focus && focus != widget)
         {
-          gtk_container_set_focus_child (GTK_CONTAINER (focus), NULL);
+          gtk_widget_set_focus_child (focus, NULL);
           focus = gtk_widget_get_parent (focus);
         }
 
diff --git a/gtk/gtkscrolledwindow.c b/gtk/gtkscrolledwindow.c
index 255c776..be15ef3 100644
--- a/gtk/gtkscrolledwindow.c
+++ b/gtk/gtkscrolledwindow.c
@@ -3630,7 +3630,7 @@ gtk_scrolled_window_focus (GtkWidget        *widget,
   GtkWidget *child;
   gboolean had_focus_child;
 
-  had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
+  had_focus_child = gtk_widget_get_focus_child (widget) != NULL;
 
   if (priv->focus_out)
     {
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index 03fdf97..6f4b4c8 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -5959,13 +5959,10 @@ static gboolean
 gtk_text_view_focus (GtkWidget        *widget,
                      GtkDirectionType  direction)
 {
-  GtkContainer *container;
   gboolean result;
 
-  container = GTK_CONTAINER (widget);
-
   if (!gtk_widget_is_focus (widget) &&
-      gtk_container_get_focus_child (container) == NULL)
+      gtk_widget_get_focus_child (widget) == NULL)
     {
       if (gtk_widget_get_can_focus (widget))
         {
diff --git a/gtk/gtktoolbar.c b/gtk/gtktoolbar.c
index 2eb4e08..2734934 100644
--- a/gtk/gtktoolbar.c
+++ b/gtk/gtktoolbar.c
@@ -1836,7 +1836,7 @@ gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
     {
       GtkWidget *child = list->data;
 
-      if (gtk_container_get_focus_child (GTK_CONTAINER (toolbar)) == child)
+      if (gtk_widget_get_focus_child (GTK_WIDGET (toolbar)) == child)
        break;
       
       if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
@@ -1856,13 +1856,12 @@ gtk_toolbar_move_focus (GtkWidget        *widget,
                        GtkDirectionType  dir)
 {
   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
-  GtkContainer *container = GTK_CONTAINER (toolbar);
   GtkWidget *focus_child;
   GList *list;
   gboolean try_focus = FALSE;
   GList *children;
 
-  focus_child = gtk_container_get_focus_child (container);
+  focus_child = gtk_widget_get_focus_child (widget);
 
   if (focus_child && gtk_widget_child_focus (focus_child, dir))
     return;
@@ -1899,7 +1898,7 @@ gtk_toolbar_focus (GtkWidget        *widget,
    * arrow keys or Ctrl TAB (both of which are handled by the
    * gtk_toolbar_move_focus() keybinding function.
    */
-  if (gtk_container_get_focus_child (GTK_CONTAINER (widget)))
+  if (gtk_widget_get_focus_child (widget))
     return FALSE;
 
   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index d7de695..72c687e 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -8294,7 +8294,7 @@ gtk_tree_view_header_focus (GtkTreeView      *tree_view,
   if (! tree_view->priv->headers_visible)
     return FALSE;
 
-  focus_child = gtk_container_get_focus_child (GTK_CONTAINER (tree_view));
+  focus_child = gtk_widget_get_focus_child (GTK_WIDGET (tree_view));
 
   first_column = tree_view->priv->columns;
   while (first_column)
@@ -8491,13 +8491,12 @@ gtk_tree_view_focus (GtkWidget        *widget,
                     GtkDirectionType  direction)
 {
   GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
-  GtkContainer *container = GTK_CONTAINER (widget);
   GtkWidget *focus_child;
 
   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_can_focus (widget))
     return FALSE;
 
-  focus_child = gtk_container_get_focus_child (container);
+  focus_child = gtk_widget_get_focus_child (widget);
 
   gtk_tree_view_stop_editing (GTK_TREE_VIEW (widget), FALSE);
   /* Case 1.  Headers currently have focus. */
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 4435954..d7a9951 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -4011,11 +4011,8 @@ gtk_widget_unparent (GtkWidget *widget)
   if (_gtk_widget_is_toplevel (toplevel))
     _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
 
-  if (GTK_IS_CONTAINER (priv->parent))
-    {
-      if (gtk_container_get_focus_child (GTK_CONTAINER (priv->parent)) == widget)
-        gtk_container_set_focus_child (GTK_CONTAINER (priv->parent), NULL);
-    }
+  if (gtk_widget_get_focus_child (priv->parent) == widget)
+    gtk_widget_set_focus_child (priv->parent, NULL);
 
   if (_gtk_widget_is_drawable (priv->parent))
     gtk_widget_queue_draw_area (priv->parent,
@@ -7154,12 +7151,9 @@ reset_focus_recurse (GtkWidget *widget,
 {
   if (GTK_IS_CONTAINER (widget))
     {
-      GtkContainer *container;
-
-      container = GTK_CONTAINER (widget);
-      gtk_container_set_focus_child (container, NULL);
+      gtk_widget_set_focus_child (widget, NULL);
 
-      gtk_container_foreach (container,
+      gtk_container_foreach (GTK_CONTAINER (widget),
                             reset_focus_recurse,
                             NULL);
     }
@@ -15744,6 +15738,8 @@ void
 gtk_widget_set_focus_child (GtkWidget *widget,
                             GtkWidget *child)
 {
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
   if (child != NULL)
@@ -15752,8 +15748,20 @@ gtk_widget_set_focus_child (GtkWidget *widget,
       g_return_if_fail (gtk_widget_get_parent (child) == widget);
     }
 
+  g_set_object (&priv->focus_child, child);
+
   if (GTK_IS_CONTAINER (widget))
     gtk_container_set_focus_child (GTK_CONTAINER (widget), child);
 
   /* TODO: ??? */
 }
+
+GtkWidget *
+gtk_widget_get_focus_child (GtkWidget *widget)
+{
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+
+  return priv->focus_child;
+}
diff --git a/gtk/gtkwidgetprivate.h b/gtk/gtkwidgetprivate.h
index ba46211..4723934 100644
--- a/gtk/gtkwidgetprivate.h
+++ b/gtk/gtkwidgetprivate.h
@@ -158,6 +158,8 @@ struct _GtkWidgetPrivate
   GtkWidget *next_sibling;
   GtkWidget *first_child;
   GtkWidget *last_child;
+
+  GtkWidget *focus_child;
 };
 
 GtkCssNode *  gtk_widget_get_css_node       (GtkWidget *widget);
@@ -303,6 +305,8 @@ void              gtk_widget_forall                        (GtkWidget
                                                             GtkCallback           callback,
                                                             gpointer              user_data);
 
+GtkWidget        *gtk_widget_get_focus_child               (GtkWidget            *widget);
+
 /* inline getters */
 
 static inline gboolean
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 50fe2ac..2cd55f2 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -7977,7 +7977,7 @@ gtk_window_focus (GtkWidget        *widget,
   priv = window->priv;
   bin = GTK_BIN (widget);
 
-  old_focus_child = gtk_container_get_focus_child (container);
+  old_focus_child = gtk_widget_get_focus_child (widget);
 
   /* We need a special implementation here to deal properly with wrapping
    * around in the tab chain without the danger of going into an
@@ -8003,7 +8003,7 @@ gtk_window_focus (GtkWidget        *widget,
       parent = _gtk_widget_get_parent (priv->focus_widget);
       while (parent)
        {
-         gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
+          gtk_widget_set_focus_child (parent, NULL);
          parent = _gtk_widget_get_parent (parent);
        }
       
@@ -8042,7 +8042,7 @@ gtk_window_move_focus (GtkWidget        *widget,
 
   gtk_widget_child_focus (widget, dir);
 
-  if (! gtk_container_get_focus_child (GTK_CONTAINER (widget)))
+  if (!gtk_widget_get_focus_child (widget))
     gtk_window_set_focus (GTK_WINDOW (widget), NULL);
 }
 
@@ -8208,7 +8208,7 @@ _gtk_window_unset_focus_and_default (GtkWindow *window,
   g_object_ref (widget);
 
   parent = _gtk_widget_get_parent (widget);
-  if (gtk_container_get_focus_child (GTK_CONTAINER (parent)) == widget)
+  if (gtk_widget_get_focus_child (parent) == widget)
     {
       child = priv->focus_widget;
       


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