listview: Revise constructors

Make both gtk_list_view_new and gtk_list_view_new_with_factory
take a model as first argument, and make all arguments
allow-none and transfer full.

Update all callers.
This commit is contained in:
Matthias Clasen 2020-07-26 18:27:23 -04:00
parent 72bb7fc701
commit 43000abeff
11 changed files with 57 additions and 63 deletions

View File

@ -166,14 +166,6 @@ do_listview_applauncher (GtkWidget *do_widget)
g_signal_connect (factory, "setup", G_CALLBACK (setup_listitem_cb), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_listitem_cb), NULL);
/* Create the list widget here.
*/
list = gtk_list_view_new_with_factory (factory);
/* We connect the activate signal here. It's the function we defined
* above for launching the selected application.
*/
g_signal_connect (list, "activate", G_CALLBACK (activate_cb), NULL);
/* And of course we need to set the data model. Here we call the function
* we wrote above that gives us the list of applications. Then we set
* it on the list widget.
@ -181,8 +173,15 @@ do_listview_applauncher (GtkWidget *do_widget)
* to create as many listitems as it needs to show itself to the user.
*/
model = create_application_list ();
gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
g_object_unref (model);
/* Create the list widget here.
*/
list = gtk_list_view_new_with_factory (model, factory);
/* We connect the activate signal here. It's the function we defined
* above for launching the selected application.
*/
g_signal_connect (list, "activate", G_CALLBACK (activate_cb), NULL);
/* List widgets should always be contained in a #GtkScrolledWindow,
* because otherwise they might get too large or they might not

View File

@ -281,18 +281,16 @@ GtkWidget *
create_weather_view (void)
{
GtkWidget *listview;
GListModel *selection;
GListModel *model;
GtkListItemFactory *factory;
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_widget), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_widget), NULL);
listview = gtk_list_view_new_with_factory (factory);
model = G_LIST_MODEL (gtk_no_selection_new (create_weather_model ()));
listview = gtk_list_view_new_with_factory (model, factory);
gtk_orientable_set_orientation (GTK_ORIENTABLE (listview), GTK_ORIENTATION_HORIZONTAL);
gtk_list_view_set_show_separators (GTK_LIST_VIEW (listview), TRUE);
selection = G_LIST_MODEL (gtk_no_selection_new (create_weather_model ()));
gtk_list_view_set_model (GTK_LIST_VIEW (listview), selection);
g_object_unref (selection);
return listview;
}

View File

@ -157,7 +157,6 @@ do_listview_words (GtkWidget *do_widget)
{
GtkWidget *header, *listview, *sw, *vbox, *search_entry, *open_button, *overlay;
GtkFilterListModel *filter_model;
GtkNoSelection *selection;
GtkStringList *stringlist;
GtkFilter *filter;
GFile *file;
@ -215,12 +214,10 @@ do_listview_words (GtkWidget *do_widget)
gtk_overlay_set_child (GTK_OVERLAY (overlay), sw);
listview = gtk_list_view_new_with_factory (
G_LIST_MODEL (gtk_no_selection_new (G_LIST_MODEL (filter_model))),
gtk_builder_list_item_factory_new_from_bytes (NULL,
g_bytes_new_static (factory_text, strlen (factory_text))));
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
selection = gtk_no_selection_new (G_LIST_MODEL (filter_model));
gtk_list_view_set_model (GTK_LIST_VIEW (listview), G_LIST_MODEL (selection));
g_object_unref (selection);
g_signal_connect (filter_model, "items-changed", G_CALLBACK (update_title_cb), progress);
g_signal_connect (filter_model, "notify::pending", G_CALLBACK (update_title_cb), progress);

View File

@ -1161,7 +1161,7 @@ gtk_column_view_init (GtkColumnView *self)
self->sorter = gtk_column_view_sorter_new ();
self->factory = gtk_column_list_item_factory_new (self);
self->listview = GTK_LIST_VIEW (gtk_list_view_new_with_factory (
self->listview = GTK_LIST_VIEW (gtk_list_view_new_with_factory (NULL,
GTK_LIST_ITEM_FACTORY (g_object_ref (self->factory))));
gtk_widget_set_hexpand (GTK_WIDGET (self->listview), TRUE);
gtk_widget_set_vexpand (GTK_WIDGET (self->listview), TRUE);

View File

@ -893,20 +893,16 @@ populate_dialog (GtkCustomPaperUnixDialog *dialog)
gtk_box_append (GTK_BOX (vbox), scrolled);
gtk_widget_show (scrolled);
listview = gtk_list_view_new ();
gtk_widget_set_size_request (listview, 140, -1);
model = G_LIST_MODEL (gtk_single_selection_new (g_object_ref (G_LIST_MODEL (dialog->custom_paper_list))));
gtk_list_view_set_model (GTK_LIST_VIEW (listview), model);
g_signal_connect (model, "notify::selected", G_CALLBACK (selected_custom_paper_changed), dialog);
g_object_unref (model);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_item), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_item), NULL);
g_signal_connect (factory, "unbind", G_CALLBACK (unbind_item), NULL);
gtk_list_view_set_factory (GTK_LIST_VIEW (listview), factory);
g_object_unref (factory);
listview = gtk_list_view_new_with_factory (model, factory);
gtk_widget_set_size_request (listview, 140, -1);
dialog->listview = listview;

View File

@ -102,18 +102,16 @@
*
* ...
*
* model = create_application_list ();
*
* factory = gtk_signal_list_item_factory_new ();
* g_signal_connect (factory, "setup", G_CALLBACK (setup_listitem_cb), NULL);
* g_signal_connect (factory, "bind", G_CALLBACK (bind_listitem_cb), NULL);
*
* list = gtk_list_view_new_with_factory (factory);
* list = gtk_list_view_new_with_factory (model, factory);
*
* g_signal_connect (list, "activate", G_CALLBACK (activate_cb), NULL);
*
* model = create_application_list ();
* gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
* g_object_unref (model);
*
* gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
* ]|
*
@ -931,52 +929,66 @@ gtk_list_view_init (GtkListView *self)
/**
* gtk_list_view_new:
* @model: (allow-none) (transfer full): the model to use, or %NULL
*
* Creates a new empty #GtkListView.
* Creates a new #GtkListView.
*
* You most likely want to call gtk_list_view_set_factory() to
* set up a way to map its items to widgets and gtk_list_view_set_model()
* to set a model to provide items next.
* You most likely want to call gtk_list_view_set_factory()
* to set up a way to map its items to widgets.
*
* Returns: a new #GtkListView
**/
GtkWidget *
gtk_list_view_new (void)
gtk_list_view_new (GListModel *model)
{
return g_object_new (GTK_TYPE_LIST_VIEW, NULL);
GtkWidget *result;
g_return_val_if_fail (model == NULL || G_IS_LIST_MODEL (model), NULL);
result = g_object_new (GTK_TYPE_LIST_VIEW,
"model", model,
NULL);
/* consume the reference */
g_clear_object (&model);
return result;
}
/**
* gtk_list_view_new_with_factory:
* @factory: (transfer full): The factory to populate items with
* @model: (allow-none) (transfer full): the model to use, or %NULL
* @factory: (allow-none) (transfer full): The factory to populate items with, or %NULL
*
* Creates a new #GtkListView that uses the given @factory for
* mapping items to widgets.
*
* You most likely want to call gtk_list_view_set_model() to set
* a model next.
*
* The function takes ownership of the
* argument, so you can write code like
* ```
* list_view = gtk_list_view_new_with_factory (
* gtk_builder_list_item_factory_newfrom_resource ("/resource.ui"));
* list_view = gtk_list_view_new_with_factory (create_model (),
* gtk_builder_list_item_factory_new_from_resource ("/resource.ui"));
* ```
*
* Returns: a new #GtkListView using the given @factory
**/
GtkWidget *
gtk_list_view_new_with_factory (GtkListItemFactory *factory)
gtk_list_view_new_with_factory (GListModel *model,
GtkListItemFactory *factory)
{
GtkWidget *result;
g_return_val_if_fail (GTK_IS_LIST_ITEM_FACTORY (factory), NULL);
g_return_val_if_fail (model == NULL || G_IS_LIST_MODEL (model), NULL);
g_return_val_if_fail (factory == NULL || GTK_IS_LIST_ITEM_FACTORY (factory), NULL);
result = g_object_new (GTK_TYPE_LIST_VIEW,
"model", model,
"factory", factory,
NULL);
g_object_unref (factory);
/* consume the references */
g_clear_object (&model);
g_clear_object (&factory);
return result;
}

View File

@ -47,9 +47,10 @@ GDK_AVAILABLE_IN_ALL
GType gtk_list_view_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
GtkWidget * gtk_list_view_new (void);
GtkWidget * gtk_list_view_new (GListModel *model);
GDK_AVAILABLE_IN_ALL
GtkWidget * gtk_list_view_new_with_factory (GtkListItemFactory *factory);
GtkWidget * gtk_list_view_new_with_factory (GListModel *model,
GtkListItemFactory *factory);
GDK_AVAILABLE_IN_ALL
GListModel * gtk_list_view_get_model (GtkListView *self);

View File

@ -771,8 +771,8 @@ main (int argc, char *argv[])
g_object_unref (filter);
list = gtk_list_view_new_with_factory (
g_object_ref (gtk_column_view_get_columns (GTK_COLUMN_VIEW (view))),
gtk_builder_list_item_factory_new_from_bytes (scope, g_bytes_new_static (factory_ui, strlen (factory_ui))));
gtk_list_view_set_model (GTK_LIST_VIEW (list), gtk_column_view_get_columns (GTK_COLUMN_VIEW (view)));
gtk_box_append (GTK_BOX (hbox), list);
g_object_unref (scope);
@ -783,6 +783,5 @@ main (int argc, char *argv[])
while (g_list_model_get_n_items (toplevels))
g_main_context_iteration (NULL, TRUE);
return 0;
}

View File

@ -365,13 +365,9 @@ main (int argc, char *argv[])
gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
gtk_stack_add_titled (GTK_STACK (stack), sw, "list", "GtkListView");
list = gtk_list_view_new ();
list = gtk_list_view_new (create_model (0, 400, 1, FALSE));
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
model = create_model (0, 400, 1, FALSE);
gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
g_object_unref (model);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_item), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_item), NULL);
@ -415,13 +411,9 @@ main (int argc, char *argv[])
gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
gtk_stack_add_titled (GTK_STACK (stack), sw, "tree", "Tree");
list = gtk_list_view_new ();
list = gtk_list_view_new (create_tree_model (20, 20));
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
model = create_tree_model (20, 20);
gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
g_object_unref (model);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_tree_item), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_tree_item), NULL);

View File

@ -148,7 +148,7 @@ main (int argc,
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_list_item), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (bind_list_item), NULL);
listview = gtk_list_view_new_with_factory (factory);
listview = gtk_list_view_new_with_factory (NULL, factory);
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);

View File

@ -615,7 +615,7 @@ main (int argc, char *argv[])
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_widget), NULL);
listview = gtk_list_view_new_with_factory (factory);
listview = gtk_list_view_new_with_factory (NULL, factory);
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
if (argc > 1)