[gtk+/wip/baedert/children: 10/30] WIP: focus_tab_sort for GtkWIdget



commit 86f2bcc28ca43c51ee970d42c61ce8a98cdc38c5
Author: Timm Bäder <mail baedert org>
Date:   Sat Nov 26 08:53:18 2016 +0100

    WIP: focus_tab_sort for GtkWIdget

 gtk/gtkcontainer.c        |  144 ++++-----------------------------------------
 gtk/gtkcontainerprivate.h |    2 +-
 gtk/gtkmenubar.c          |    2 +-
 gtk/gtkradiobutton.c      |    2 +-
 gtk/gtkwidget.c           |   96 +++++++++++++++++++++++++++---
 gtk/gtkwindow.c           |    8 +-
 6 files changed, 107 insertions(+), 147 deletions(-)
---
diff --git a/gtk/gtkcontainer.c b/gtk/gtkcontainer.c
index 3b05b8f..6d57bf7 100644
--- a/gtk/gtkcontainer.c
+++ b/gtk/gtkcontainer.c
@@ -309,14 +309,8 @@ static void     gtk_container_real_check_resize    (GtkContainer      *container
 static void     gtk_container_compute_expand       (GtkWidget         *widget,
                                                     gboolean          *hexpand_p,
                                                     gboolean          *vexpand_p);
-static gboolean gtk_container_focus                (GtkWidget         *widget,
-                                                    GtkDirectionType   direction);
 static void     gtk_container_real_set_focus_child (GtkContainer      *container,
                                                     GtkWidget         *widget);
-
-static gboolean gtk_container_focus_move           (GtkContainer      *container,
-                                                    GList             *children,
-                                                    GtkDirectionType   direction);
 static void     gtk_container_children_callback    (GtkWidget         *widget,
                                                     gpointer           client_data);
 static gint     gtk_container_draw                 (GtkWidget         *widget,
@@ -456,7 +450,6 @@ gtk_container_class_init (GtkContainerClass *class)
   widget_class->destroy = gtk_container_destroy;
   widget_class->compute_expand = gtk_container_compute_expand;
   widget_class->draw = gtk_container_draw;
-  widget_class->focus = gtk_container_focus;
   widget_class->get_request_mode = gtk_container_get_request_mode;
 
   class->add = gtk_container_add_unimplemented;
@@ -2121,20 +2114,6 @@ get_focus_chain (GtkContainer *container)
   return g_object_get_qdata (G_OBJECT (container), quark_focus_chain);
 }
 
-/* same as gtk_container_get_children, except it includes internals
- */
-static GList *
-gtk_container_get_all_children (GtkContainer *container)
-{
-  GList *children = NULL;
-
-  gtk_container_forall (container,
-                         gtk_container_children_callback,
-                         &children);
-
-  return children;
-}
-
 static GtkWidgetPath *
 gtk_container_real_get_path_for_child (GtkContainer *container,
                                        GtkWidget    *child)
@@ -2149,62 +2128,6 @@ gtk_container_real_get_path_for_child (GtkContainer *container,
   return path;
 }
 
-static gboolean
-gtk_container_focus (GtkWidget        *widget,
-                     GtkDirectionType  direction)
-{
-  GList *children;
-  GList *sorted_children;
-  gint return_val;
-  GtkContainer *container;
-  GtkContainerPrivate *priv;
-
-  g_return_val_if_fail (GTK_IS_CONTAINER (widget), FALSE);
-
-  container = GTK_CONTAINER (widget);
-  priv = container->priv;
-
-  return_val = FALSE;
-
-  if (gtk_widget_get_can_focus (widget))
-    {
-      if (!gtk_widget_has_focus (widget))
-        {
-          gtk_widget_grab_focus (widget);
-          return_val = TRUE;
-        }
-    }
-  else
-    {
-      /* Get a list of the containers children, allowing focus
-       * chain to override.
-       */
-      if (priv->has_focus_chain)
-        children = g_list_copy (get_focus_chain (container));
-      else
-        children = gtk_container_get_all_children (container);
-
-      if (priv->has_focus_chain &&
-          (direction == GTK_DIR_TAB_FORWARD ||
-           direction == GTK_DIR_TAB_BACKWARD))
-        {
-          sorted_children = g_list_copy (children);
-
-          if (direction == GTK_DIR_TAB_BACKWARD)
-            sorted_children = g_list_reverse (sorted_children);
-        }
-      else
-        sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
-
-      return_val = gtk_container_focus_move (container, sorted_children, direction);
-
-      g_list_free (sorted_children);
-      g_list_free (children);
-    }
-
-  return return_val;
-}
-
 static gint
 tab_compare (gconstpointer a,
              gconstpointer b,
@@ -2237,7 +2160,7 @@ tab_compare (gconstpointer a,
 }
 
 static GList *
-gtk_container_focus_sort_tab (GtkContainer     *container,
+gtk_container_focus_sort_tab (GtkWidget        *container,
                               GList            *children,
                               GtkDirectionType  direction,
                               GtkWidget        *old_focus)
@@ -2258,13 +2181,13 @@ gtk_container_focus_sort_tab (GtkContainer     *container,
  * allocation of @container.
  */
 static gboolean
-get_allocation_coords (GtkContainer  *container,
+get_allocation_coords (GtkWidget     *container,
                        GtkWidget     *widget,
                        GdkRectangle  *allocation)
 {
   gtk_widget_get_allocation (widget, allocation);
 
-  return gtk_widget_translate_coordinates (widget, GTK_WIDGET (container),
+  return gtk_widget_translate_coordinates (widget, container,
                                            0, 0, &allocation->x, &allocation->y);
 }
 
@@ -2273,7 +2196,7 @@ get_allocation_coords (GtkContainer  *container,
  * acts as the starting widget for focus navigation.
  */
 static GtkWidget *
-find_old_focus (GtkContainer *container,
+find_old_focus (GtkWidget    *container,
                 GList        *children)
 {
   GList *tmp_list = children;
@@ -2282,13 +2205,13 @@ find_old_focus (GtkContainer *container,
       GtkWidget *child = tmp_list->data;
       GtkWidget *widget = child;
 
-      while (widget && widget != (GtkWidget *)container)
+      while (widget && widget != container)
         {
           GtkWidget *parent;
 
           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;
@@ -2304,11 +2227,10 @@ find_old_focus (GtkContainer *container,
 }
 
 static gboolean
-old_focus_coords (GtkContainer *container,
+old_focus_coords (GtkWidget    *container,
                   GdkRectangle *old_focus_rect)
 {
-  GtkWidget *widget = GTK_WIDGET (container);
-  GtkWidget *toplevel = _gtk_widget_get_toplevel (widget);
+  GtkWidget *toplevel = _gtk_widget_get_toplevel (container);
   GtkWidget *old_focus;
 
   if (GTK_IS_WINDOW (toplevel))
@@ -2325,7 +2247,7 @@ typedef struct _CompareInfo CompareInfo;
 
 struct _CompareInfo
 {
-  GtkContainer *container;
+  GtkWidget *container;
   gint x;
   gint y;
   gboolean reverse;
@@ -2362,7 +2284,7 @@ up_down_compare (gconstpointer a,
 }
 
 static GList *
-gtk_container_focus_sort_up_down (GtkContainer     *container,
+gtk_container_focus_sort_up_down (GtkWidget        *container,
                                   GList            *children,
                                   GtkDirectionType  direction,
                                   GtkWidget        *old_focus)
@@ -2375,7 +2297,7 @@ gtk_container_focus_sort_up_down (GtkContainer     *container,
   compare.reverse = (direction == GTK_DIR_UP);
 
   if (!old_focus)
-      old_focus = find_old_focus (container, children);
+    old_focus = find_old_focus (container, children);
 
   if (old_focus && get_allocation_coords (container, old_focus, &old_allocation))
     {
@@ -2492,7 +2414,7 @@ left_right_compare (gconstpointer a,
 }
 
 static GList *
-gtk_container_focus_sort_left_right (GtkContainer     *container,
+gtk_container_focus_sort_left_right (GtkWidget        *container,
                                      GList            *children,
                                      GtkDirectionType  direction,
                                      GtkWidget        *old_focus)
@@ -2611,7 +2533,7 @@ gtk_container_focus_sort_left_right (GtkContainer     *container,
  *   removed.
  **/
 GList *
-_gtk_container_focus_sort (GtkContainer     *container,
+_gtk_container_focus_sort (GtkWidget        *container,
                            GList            *children,
                            GtkDirectionType  direction,
                            GtkWidget        *old_focus)
@@ -2643,46 +2565,6 @@ _gtk_container_focus_sort (GtkContainer     *container,
   return NULL;
 }
 
-static gboolean
-gtk_container_focus_move (GtkContainer     *container,
-                          GList            *children,
-                          GtkDirectionType  direction)
-{
-  GtkWidget *focus_child;
-  GtkWidget *child;
-
-  focus_child = gtk_widget_get_focus_child (GTK_WIDGET (container));
-
-  while (children)
-    {
-      child = children->data;
-      children = children->next;
-
-      if (!child)
-        continue;
-
-      if (focus_child)
-        {
-          if (focus_child == child)
-            {
-              focus_child = NULL;
-
-                if (gtk_widget_child_focus (child, direction))
-                  return TRUE;
-            }
-        }
-      else if (_gtk_widget_is_drawable (child) &&
-               gtk_widget_is_ancestor (child, GTK_WIDGET (container)))
-        {
-          if (gtk_widget_child_focus (child, direction))
-            return TRUE;
-        }
-    }
-
-  return FALSE;
-}
-
-
 static void
 gtk_container_children_callback (GtkWidget *widget,
                                  gpointer   client_data)
diff --git a/gtk/gtkcontainerprivate.h b/gtk/gtkcontainerprivate.h
index 9a865d6..3f2eff9 100644
--- a/gtk/gtkcontainerprivate.h
+++ b/gtk/gtkcontainerprivate.h
@@ -32,7 +32,7 @@ void     _gtk_container_clear_resize_widgets   (GtkContainer *container);
 gchar*   _gtk_container_child_composite_name   (GtkContainer *container,
                                                 GtkWidget    *child);
 void     _gtk_container_dequeue_resize_handler (GtkContainer *container);
-GList *  _gtk_container_focus_sort             (GtkContainer     *container,
+GList *  _gtk_container_focus_sort             (GtkWidget        *container,
                                                 GList            *children,
                                                 GtkDirectionType  direction,
                                                 GtkWidget        *old_focus);
diff --git a/gtk/gtkmenubar.c b/gtk/gtkmenubar.c
index fa56e09..80723d7 100644
--- a/gtk/gtkmenubar.c
+++ b/gtk/gtkmenubar.c
@@ -694,7 +694,7 @@ _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
       GList *menubars;
       GList *current;
 
-      menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
+      menubars = _gtk_container_focus_sort (GTK_WIDGET (toplevel), tmp_menubars,
                                            dir, GTK_WIDGET (menubar));
       g_list_free (tmp_menubars);
 
diff --git a/gtk/gtkradiobutton.c b/gtk/gtkradiobutton.c
index d38fe95..bc1a441 100644
--- a/gtk/gtkradiobutton.c
+++ b/gtk/gtkradiobutton.c
@@ -650,7 +650,7 @@ gtk_radio_button_focus (GtkWidget         *widget,
       for (l = priv->group; l; l = l->next)
         children = g_list_prepend (children, l->data);
 
-      focus_list = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), children, direction, widget);
+      focus_list = _gtk_container_focus_sort (GTK_WIDGET (toplevel), children, direction, widget);
       tmp_list = g_list_find (focus_list, widget);
 
       if (tmp_list)
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index c5e9d1f..0a0c62a 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -7421,19 +7421,99 @@ gtk_widget_real_style_updated (GtkWidget *widget)
 }
 
 static gboolean
+gtk_widget_focus_move (GtkWidget        *widget,
+                       GList            *children,
+                       GtkDirectionType  direction)
+{
+  GtkWidget *focus_child;
+  GtkWidget *child;
+  GList *l;
+
+  focus_child = gtk_widget_get_focus_child (widget);
+
+  l = children;
+  while (l)
+    {
+      child = l->data;
+      l = l->next;
+
+      if (focus_child)
+        {
+          if (focus_child == child)
+            {
+              focus_child = NULL;
+
+              if (gtk_widget_child_focus (child, direction))
+                return TRUE;
+            }
+        }
+      else if (_gtk_widget_is_drawable (child) &&
+               gtk_widget_is_ancestor (child, widget))
+        {
+          if (gtk_widget_child_focus (child, direction))
+            return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
+static gboolean
 gtk_widget_real_focus (GtkWidget         *widget,
                        GtkDirectionType   direction)
 {
-  if (!gtk_widget_get_can_focus (widget))
-    return FALSE;
-
-  if (!gtk_widget_is_focus (widget))
+  if (gtk_widget_get_can_focus (widget))
     {
-      gtk_widget_grab_focus (widget);
-      return TRUE;
+      if (!gtk_widget_has_focus (widget))
+        {
+          gtk_widget_grab_focus (widget);
+          return TRUE;
+        }
     }
   else
-    return FALSE;
+    {
+      GtkWidget *child;
+      GList *children = NULL;
+      gboolean ret;
+
+      /* TODO: Focus chain support? */
+      for (child = gtk_widget_get_last_child (widget);
+           child != NULL;
+           child = gtk_widget_get_prev_sibling (child))
+        {
+          children = g_list_prepend (children, child);
+        }
+
+      children = _gtk_container_focus_sort (widget, children, direction, NULL);
+
+      /* Get a list of the containers children, allowing focus
+       * chain to override.
+       */
+      /*if (priv->has_focus_chain)*/
+        /*children = g_list_copy (get_focus_chain (container));*/
+      /*else*/
+        /*children = gtk_container_get_all_children (container);*/
+#if 0
+      if (priv->has_focus_chain &&
+          (direction == GTK_DIR_TAB_FORWARD ||
+           direction == GTK_DIR_TAB_BACKWARD))
+        {
+          sorted_children = g_list_copy (children);
+
+          if (direction == GTK_DIR_TAB_BACKWARD)
+            sorted_children = g_list_reverse (sorted_children);
+        }
+      else
+        sorted_children = _gtk_container_focus_sort (container, children, direction, NULL);
+#endif
+
+      ret = gtk_widget_focus_move (widget, children, direction);
+      g_list_free (children);
+
+      return ret;
+    }
+
+  return FALSE;
 }
 
 static void
@@ -15861,8 +15941,6 @@ gtk_widget_should_propagate_draw (GtkWidget *widget,
                                   GtkWidget *child,
                                   cairo_t   *cr)
 {
-  GdkWindow *child_in_window;
-
   if (!_gtk_widget_is_drawable (child))
     return FALSE;
 
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 2b8c52c..839700d 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -2539,7 +2539,7 @@ gtk_window_set_focus (GtkWindow *window,
          while ((parent = _gtk_widget_get_parent (widget)))
            {
              widget = parent;
-             gtk_container_set_focus_child (GTK_CONTAINER (widget), NULL);
+             gtk_widget_set_focus_child (widget, NULL);
            }
        }
       
@@ -8007,7 +8007,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);
        }
       
@@ -8212,7 +8212,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;
       
@@ -10410,7 +10410,7 @@ gtk_window_activate_menubar (GtkWindow   *window,
       if (tmp_menubars == NULL)
         return FALSE;
 
-      menubars = _gtk_container_focus_sort (GTK_CONTAINER (window), tmp_menubars,
+      menubars = _gtk_container_focus_sort (GTK_WIDGET (window), tmp_menubars,
                                             GTK_DIR_TAB_FORWARD, NULL);
       g_list_free (tmp_menubars);
 


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