widget: Return an array from list_controllers

Minimize the GList usage throughout the code base.
This commit is contained in:
Timm Bäder 2020-03-28 20:37:29 +01:00
parent af749dc1dc
commit 785b33f169
5 changed files with 41 additions and 28 deletions

View File

@ -846,7 +846,8 @@ gtk_combo_box_init (GtkComboBox *combo_box)
{ {
GtkComboBoxPrivate *priv = gtk_combo_box_get_instance_private (combo_box); GtkComboBoxPrivate *priv = gtk_combo_box_get_instance_private (combo_box);
GtkEventController *controller; GtkEventController *controller;
GList *controllers, *list; GtkEventController **controllers;
guint n_controllers, i;
priv->active = -1; priv->active = -1;
priv->active_row = NULL; priv->active_row = NULL;
@ -882,18 +883,20 @@ gtk_combo_box_init (GtkComboBox *combo_box)
combo_box); combo_box);
gtk_widget_add_controller (GTK_WIDGET (combo_box), controller); gtk_widget_add_controller (GTK_WIDGET (combo_box), controller);
controllers = gtk_widget_list_controllers (priv->popup_widget, GTK_PHASE_BUBBLE); controllers = gtk_widget_list_controllers (priv->popup_widget, GTK_PHASE_BUBBLE, &n_controllers);
for (list = controllers; list; list = list->next) for (i = 0; i < n_controllers; i ++)
{ {
if (GTK_IS_SHORTCUT_CONTROLLER (list->data)) controller = controllers[i];
if (GTK_IS_SHORTCUT_CONTROLLER (controller))
{ {
g_object_ref (list->data); g_object_ref (controller);
gtk_widget_remove_controller (priv->popup_widget, list->data); gtk_widget_remove_controller (priv->popup_widget, controller);
gtk_widget_add_controller (priv->popup_widget, list->data); gtk_widget_add_controller (priv->popup_widget, controller);
break; break;
} }
} }
g_list_free (controllers); g_free (controllers);
} }
static void static void

View File

@ -201,7 +201,8 @@ gtk_popover_menu_init (GtkPopoverMenu *popover)
{ {
GtkWidget *stack; GtkWidget *stack;
GtkEventController *controller; GtkEventController *controller;
GList *controllers, *l; GtkEventController **controllers;
guint n_controllers, i;
stack = gtk_stack_new (); stack = gtk_stack_new ();
gtk_stack_set_vhomogeneous (GTK_STACK (stack), FALSE); gtk_stack_set_vhomogeneous (GTK_STACK (stack), FALSE);
@ -221,15 +222,15 @@ gtk_popover_menu_init (GtkPopoverMenu *popover)
g_signal_connect (controller, "leave", G_CALLBACK (leave_cb), popover); g_signal_connect (controller, "leave", G_CALLBACK (leave_cb), popover);
gtk_widget_add_controller (GTK_WIDGET (popover), controller); gtk_widget_add_controller (GTK_WIDGET (popover), controller);
controllers = gtk_widget_list_controllers (GTK_WIDGET (popover), GTK_PHASE_CAPTURE); controllers = gtk_widget_list_controllers (GTK_WIDGET (popover), GTK_PHASE_CAPTURE, &n_controllers);
for (l = controllers; l; l = l->next) for (i = 0; i < n_controllers; i ++)
{ {
controller = l->data; controller = controllers[i];
if (GTK_IS_SHORTCUT_CONTROLLER (controller) && if (GTK_IS_SHORTCUT_CONTROLLER (controller) &&
strcmp (gtk_event_controller_get_name (controller), "gtk-shortcut-manager-capture") == 0) strcmp (gtk_event_controller_get_name (controller), "gtk-shortcut-manager-capture") == 0)
gtk_shortcut_controller_set_mnemonics_modifiers (GTK_SHORTCUT_CONTROLLER (controller), 0); gtk_shortcut_controller_set_mnemonics_modifiers (GTK_SHORTCUT_CONTROLLER (controller), 0);
} }
g_list_free (controllers); g_free (controllers);
gtk_popover_disable_auto_mnemonics (GTK_POPOVER (popover)); gtk_popover_disable_auto_mnemonics (GTK_POPOVER (popover));
} }

View File

@ -1704,7 +1704,8 @@ gtk_tree_view_init (GtkTreeView *tree_view)
GtkCssNode *widget_node; GtkCssNode *widget_node;
GtkGesture *gesture; GtkGesture *gesture;
GtkEventController *controller; GtkEventController *controller;
GList *list, *controllers; GtkEventController **controllers;
guint n_controllers, i;
gtk_widget_set_can_focus (GTK_WIDGET (tree_view), TRUE); gtk_widget_set_can_focus (GTK_WIDGET (tree_view), TRUE);
gtk_widget_set_overflow (GTK_WIDGET (tree_view), GTK_OVERFLOW_HIDDEN); gtk_widget_set_overflow (GTK_WIDGET (tree_view), GTK_OVERFLOW_HIDDEN);
@ -1771,18 +1772,19 @@ gtk_tree_view_init (GtkTreeView *tree_view)
G_CALLBACK (gtk_tree_view_forward_controller_key_pressed), tree_view); G_CALLBACK (gtk_tree_view_forward_controller_key_pressed), tree_view);
gtk_widget_add_controller (GTK_WIDGET (tree_view), controller); gtk_widget_add_controller (GTK_WIDGET (tree_view), controller);
controllers = gtk_widget_list_controllers (GTK_WIDGET (tree_view), GTK_PHASE_BUBBLE); controllers = gtk_widget_list_controllers (GTK_WIDGET (tree_view), GTK_PHASE_BUBBLE, &n_controllers);
for (list = controllers; list; list = list->next) for (i = 0; i < n_controllers; i ++)
{ {
if (GTK_IS_SHORTCUT_CONTROLLER (list->data)) controller = controllers[i];
if (GTK_IS_SHORTCUT_CONTROLLER (controller))
{ {
g_object_ref (list->data); g_object_ref (controller);
gtk_widget_remove_controller (GTK_WIDGET (tree_view), list->data); gtk_widget_remove_controller (GTK_WIDGET (tree_view), controller);
gtk_widget_add_controller (GTK_WIDGET (tree_view), list->data); gtk_widget_add_controller (GTK_WIDGET (tree_view), controller);
break; break;
} }
} }
g_list_free (controllers); g_free (controllers);
tree_view->click_gesture = gtk_gesture_click_new (); tree_view->click_gesture = gtk_gesture_click_new ();
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (tree_view->click_gesture), 0); gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (tree_view->click_gesture), 0);

View File

@ -11474,22 +11474,28 @@ gtk_widget_reset_controllers (GtkWidget *widget)
} }
} }
GList * GtkEventController **
gtk_widget_list_controllers (GtkWidget *widget, gtk_widget_list_controllers (GtkWidget *widget,
GtkPropagationPhase phase) GtkPropagationPhase phase,
guint *out_n_controllers)
{ {
GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget); GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
GList *res = NULL, *l; GPtrArray *controllers = g_ptr_array_new ();
GList *l;
g_assert (out_n_controllers);
for (l = priv->event_controllers; l; l = l->next) for (l = priv->event_controllers; l; l = l->next)
{ {
GtkEventController *controller = l->data; GtkEventController *controller = l->data;
if (gtk_event_controller_get_propagation_phase (controller) == phase) if (gtk_event_controller_get_propagation_phase (controller) == phase)
res = g_list_prepend (res, controller); g_ptr_array_add (controllers, controller);
} }
return g_list_reverse (res); *out_n_controllers = controllers->len;
return (GtkEventController **)g_ptr_array_free (controllers, FALSE);
} }
static inline void static inline void

View File

@ -295,8 +295,9 @@ gboolean gtk_widget_has_size_request (GtkWidget *widget);
void gtk_widget_reset_controllers (GtkWidget *widget); void gtk_widget_reset_controllers (GtkWidget *widget);
GList * gtk_widget_list_controllers (GtkWidget *widget, GtkEventController **gtk_widget_list_controllers (GtkWidget *widget,
GtkPropagationPhase phase); GtkPropagationPhase phase,
guint *out_n_controllers);
gboolean gtk_widget_query_tooltip (GtkWidget *widget, gboolean gtk_widget_query_tooltip (GtkWidget *widget,
gint x, gint x,