widget: Save pointer to focus child

Do the same thing GtkContainer does.
This commit is contained in:
Timm Bäder 2017-02-08 09:00:29 +01:00
parent 2bab2048f9
commit 43cdeee3c4
14 changed files with 49 additions and 48 deletions

View File

@ -2114,10 +2114,9 @@ gtk_container_real_set_focus_child (GtkContainer *container,
{ {
focus_child = priv->focus_child; focus_child = priv->focus_child;
while (GTK_IS_CONTAINER (focus_child) && gtk_container_get_focus_child (GTK_CONTAINER (focus_child)))
{ while (gtk_widget_get_focus_child (focus_child))
focus_child = gtk_container_get_focus_child (GTK_CONTAINER (focus_child)); focus_child = gtk_widget_get_focus_child (focus_child);
}
gtk_widget_translate_coordinates (focus_child, priv->focus_child, gtk_widget_translate_coordinates (focus_child, priv->focus_child,
0, 0, &x, &y); 0, 0, &x, &y);
@ -2310,7 +2309,7 @@ find_old_focus (GtkContainer *container,
parent = _gtk_widget_get_parent (widget); 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; goto next;
widget = parent; widget = parent;

View File

@ -796,7 +796,7 @@ focus_current_site (GtkExpander *expander,
{ {
GtkWidget *current_focus; 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) if (!current_focus)
return FALSE; return FALSE;
@ -962,7 +962,7 @@ gtk_expander_focus (GtkWidget *widget,
FocusSite site = FOCUS_NONE; FocusSite site = FOCUS_NONE;
widget_is_focus = gtk_widget_is_focus (widget); 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) if (old_focus_child && old_focus_child == expander->priv->label_widget)
site = FOCUS_LABEL; site = FOCUS_LABEL;

View File

@ -342,7 +342,7 @@ gtk_flow_box_child_focus (GtkWidget *widget,
return FALSE; 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 */ /* Child has focus, always navigate inside it first */
if (gtk_widget_child_focus (child, direction)) 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); 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; next_focus_child = NULL;
if (focus_child != NULL) if (focus_child != NULL)

View File

@ -2016,7 +2016,7 @@ gtk_list_box_focus (GtkWidget *widget,
GtkWidget *row; GtkWidget *row;
GtkWidget *header; GtkWidget *header;
focus_child = gtk_container_get_focus_child ((GtkContainer *)box); focus_child = gtk_widget_get_focus_child (widget);
next_focus_row = NULL; next_focus_row = NULL;
if (focus_child != NULL) if (focus_child != NULL)
@ -3207,7 +3207,7 @@ gtk_list_box_row_focus (GtkWidget *widget,
return FALSE; 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 */ /* Child has focus, always navigate inside it first */
if (gtk_widget_child_focus (child, direction)) if (gtk_widget_child_focus (child, direction))

View File

@ -1436,7 +1436,7 @@ gtk_notebook_move_focus_out (GtkNotebook *notebook,
GtkDirectionType effective_direction = get_effective_direction (notebook, direction_type); GtkDirectionType effective_direction = get_effective_direction (notebook, direction_type);
GtkWidget *toplevel; 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)) if (focus_tabs_in (notebook))
return; return;
if (gtk_widget_is_focus (GTK_WIDGET (notebook)) && effective_direction == GTK_DIR_DOWN) 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; gint last_action;
gboolean widget_is_focus; gboolean widget_is_focus;
GtkContainer *container;
container = GTK_CONTAINER (widget);
if (priv->tab_pos == GTK_POS_TOP || if (priv->tab_pos == GTK_POS_TOP ||
priv->tab_pos == GTK_POS_LEFT) priv->tab_pos == GTK_POS_LEFT)
@ -4203,7 +4200,7 @@ gtk_notebook_focus (GtkWidget *widget,
} }
widget_is_focus = gtk_widget_is_focus (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); effective_direction = get_effective_direction (notebook, direction);

View File

@ -2449,7 +2449,7 @@ gtk_paned_set_focus_child (GtkContainer *container,
if (GTK_IS_PANED (w)) if (GTK_IS_PANED (w))
last_focus = 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) if (container_focus_child == priv->child1)
gtk_paned_set_last_child1_focus (paned, last_focus); gtk_paned_set_last_child1_focus (paned, last_focus);
else if (container_focus_child == priv->child2) else if (container_focus_child == priv->child2)
@ -2467,7 +2467,6 @@ gtk_paned_get_cycle_chain (GtkPaned *paned,
GList **widgets) GList **widgets)
{ {
GtkPanedPrivate *priv = paned->priv; GtkPanedPrivate *priv = paned->priv;
GtkContainer *container = GTK_CONTAINER (paned);
GtkWidget *ancestor = NULL; GtkWidget *ancestor = NULL;
GtkWidget *focus_child; GtkWidget *focus_child;
GtkWidget *parent; GtkWidget *parent;
@ -2504,7 +2503,7 @@ gtk_paned_get_cycle_chain (GtkPaned *paned,
* priv->last_child?_focus before priv->child?, both when we * priv->last_child?_focus before priv->child?, both when we
* are going forward and backward. * 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 (direction == GTK_DIR_TAB_FORWARD)
{ {
if (focus_child == priv->child1) if (focus_child == priv->child1)
@ -2880,14 +2879,13 @@ gtk_paned_cycle_handle_focus (GtkPaned *paned,
} }
else else
{ {
GtkContainer *container = GTK_CONTAINER (paned);
GtkPaned *focus; GtkPaned *focus;
GtkPaned *first; GtkPaned *first;
GtkWidget *toplevel; GtkWidget *toplevel;
GtkWidget *focus_child; GtkWidget *focus_child;
gtk_paned_find_neighbours (paned, &next, &prev); 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) if (focus_child == priv->child1)
{ {

View File

@ -1511,7 +1511,7 @@ gtk_popover_focus (GtkWidget *widget,
*/ */
while (focus && focus != 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); focus = gtk_widget_get_parent (focus);
} }

View File

@ -3630,7 +3630,7 @@ gtk_scrolled_window_focus (GtkWidget *widget,
GtkWidget *child; GtkWidget *child;
gboolean had_focus_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) if (priv->focus_out)
{ {

View File

@ -5959,13 +5959,10 @@ static gboolean
gtk_text_view_focus (GtkWidget *widget, gtk_text_view_focus (GtkWidget *widget,
GtkDirectionType direction) GtkDirectionType direction)
{ {
GtkContainer *container;
gboolean result; gboolean result;
container = GTK_CONTAINER (widget);
if (!gtk_widget_is_focus (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)) if (gtk_widget_get_can_focus (widget))
{ {

View File

@ -1836,7 +1836,7 @@ gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
{ {
GtkWidget *child = list->data; 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; break;
if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir)) if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
@ -1856,13 +1856,12 @@ gtk_toolbar_move_focus (GtkWidget *widget,
GtkDirectionType dir) GtkDirectionType dir)
{ {
GtkToolbar *toolbar = GTK_TOOLBAR (widget); GtkToolbar *toolbar = GTK_TOOLBAR (widget);
GtkContainer *container = GTK_CONTAINER (toolbar);
GtkWidget *focus_child; GtkWidget *focus_child;
GList *list; GList *list;
gboolean try_focus = FALSE; gboolean try_focus = FALSE;
GList *children; 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)) if (focus_child && gtk_widget_child_focus (focus_child, dir))
return; return;
@ -1899,7 +1898,7 @@ gtk_toolbar_focus (GtkWidget *widget,
* arrow keys or Ctrl TAB (both of which are handled by the * arrow keys or Ctrl TAB (both of which are handled by the
* gtk_toolbar_move_focus() keybinding function. * gtk_toolbar_move_focus() keybinding function.
*/ */
if (gtk_container_get_focus_child (GTK_CONTAINER (widget))) if (gtk_widget_get_focus_child (widget))
return FALSE; return FALSE;
children = gtk_toolbar_list_children_in_focus_order (toolbar, dir); children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);

View File

@ -8294,7 +8294,7 @@ gtk_tree_view_header_focus (GtkTreeView *tree_view,
if (! tree_view->priv->headers_visible) if (! tree_view->priv->headers_visible)
return FALSE; 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; first_column = tree_view->priv->columns;
while (first_column) while (first_column)
@ -8491,13 +8491,12 @@ gtk_tree_view_focus (GtkWidget *widget,
GtkDirectionType direction) GtkDirectionType direction)
{ {
GtkTreeView *tree_view = GTK_TREE_VIEW (widget); GtkTreeView *tree_view = GTK_TREE_VIEW (widget);
GtkContainer *container = GTK_CONTAINER (widget);
GtkWidget *focus_child; GtkWidget *focus_child;
if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_can_focus (widget)) if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_can_focus (widget))
return FALSE; 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); gtk_tree_view_stop_editing (GTK_TREE_VIEW (widget), FALSE);
/* Case 1. Headers currently have focus. */ /* Case 1. Headers currently have focus. */

View File

@ -4011,11 +4011,8 @@ gtk_widget_unparent (GtkWidget *widget)
if (_gtk_widget_is_toplevel (toplevel)) if (_gtk_widget_is_toplevel (toplevel))
_gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget); _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
if (GTK_IS_CONTAINER (priv->parent)) if (gtk_widget_get_focus_child (priv->parent) == widget)
{ gtk_widget_set_focus_child (priv->parent, NULL);
if (gtk_container_get_focus_child (GTK_CONTAINER (priv->parent)) == widget)
gtk_container_set_focus_child (GTK_CONTAINER (priv->parent), NULL);
}
if (_gtk_widget_is_drawable (priv->parent)) if (_gtk_widget_is_drawable (priv->parent))
gtk_widget_queue_draw_area (priv->parent, gtk_widget_queue_draw_area (priv->parent,
@ -7154,12 +7151,9 @@ reset_focus_recurse (GtkWidget *widget,
{ {
if (GTK_IS_CONTAINER (widget)) if (GTK_IS_CONTAINER (widget))
{ {
GtkContainer *container; gtk_widget_set_focus_child (widget, NULL);
container = GTK_CONTAINER (widget); gtk_container_foreach (GTK_CONTAINER (widget),
gtk_container_set_focus_child (container, NULL);
gtk_container_foreach (container,
reset_focus_recurse, reset_focus_recurse,
NULL); NULL);
} }
@ -15744,6 +15738,8 @@ void
gtk_widget_set_focus_child (GtkWidget *widget, gtk_widget_set_focus_child (GtkWidget *widget,
GtkWidget *child) GtkWidget *child)
{ {
GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
g_return_if_fail (GTK_IS_WIDGET (widget)); g_return_if_fail (GTK_IS_WIDGET (widget));
if (child != NULL) 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_return_if_fail (gtk_widget_get_parent (child) == widget);
} }
g_set_object (&priv->focus_child, child);
if (GTK_IS_CONTAINER (widget)) if (GTK_IS_CONTAINER (widget))
gtk_container_set_focus_child (GTK_CONTAINER (widget), child); gtk_container_set_focus_child (GTK_CONTAINER (widget), child);
/* TODO: ??? */ /* 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;
}

View File

@ -158,6 +158,8 @@ struct _GtkWidgetPrivate
GtkWidget *next_sibling; GtkWidget *next_sibling;
GtkWidget *first_child; GtkWidget *first_child;
GtkWidget *last_child; GtkWidget *last_child;
GtkWidget *focus_child;
}; };
GtkCssNode * gtk_widget_get_css_node (GtkWidget *widget); GtkCssNode * gtk_widget_get_css_node (GtkWidget *widget);
@ -303,6 +305,8 @@ void gtk_widget_forall (GtkWidget
GtkCallback callback, GtkCallback callback,
gpointer user_data); gpointer user_data);
GtkWidget *gtk_widget_get_focus_child (GtkWidget *widget);
/* inline getters */ /* inline getters */
static inline gboolean static inline gboolean

View File

@ -7977,7 +7977,7 @@ gtk_window_focus (GtkWidget *widget,
priv = window->priv; priv = window->priv;
bin = GTK_BIN (widget); 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 /* We need a special implementation here to deal properly with wrapping
* around in the tab chain without the danger of going into an * 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); parent = _gtk_widget_get_parent (priv->focus_widget);
while (parent) while (parent)
{ {
gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL); gtk_widget_set_focus_child (parent, NULL);
parent = _gtk_widget_get_parent (parent); parent = _gtk_widget_get_parent (parent);
} }
@ -8042,7 +8042,7 @@ gtk_window_move_focus (GtkWidget *widget,
gtk_widget_child_focus (widget, dir); 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); gtk_window_set_focus (GTK_WINDOW (widget), NULL);
} }
@ -8208,7 +8208,7 @@ _gtk_window_unset_focus_and_default (GtkWindow *window,
g_object_ref (widget); g_object_ref (widget);
parent = _gtk_widget_get_parent (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; child = priv->focus_widget;