diff --git a/demos/gtk-demo/listview_applauncher.c b/demos/gtk-demo/listview_applauncher.c index a6eb2f9863..92e4631689 100644 --- a/demos/gtk-demo/listview_applauncher.c +++ b/demos/gtk-demo/listview_applauncher.c @@ -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 diff --git a/demos/gtk-demo/listview_weather.c b/demos/gtk-demo/listview_weather.c index f17ff08921..23b83906cb 100644 --- a/demos/gtk-demo/listview_weather.c +++ b/demos/gtk-demo/listview_weather.c @@ -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; } diff --git a/demos/gtk-demo/listview_words.c b/demos/gtk-demo/listview_words.c index 4545ed48aa..50e40adc20 100644 --- a/demos/gtk-demo/listview_words.c +++ b/demos/gtk-demo/listview_words.c @@ -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); diff --git a/gtk/gtkcolumnview.c b/gtk/gtkcolumnview.c index 691c18e6ed..590c7073d3 100644 --- a/gtk/gtkcolumnview.c +++ b/gtk/gtkcolumnview.c @@ -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); diff --git a/gtk/gtkcustompaperunixdialog.c b/gtk/gtkcustompaperunixdialog.c index 138f4e4ef1..ef2cbeadd2 100644 --- a/gtk/gtkcustompaperunixdialog.c +++ b/gtk/gtkcustompaperunixdialog.c @@ -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; diff --git a/gtk/gtklistview.c b/gtk/gtklistview.c index 3a65706ee6..e0a33b42f4 100644 --- a/gtk/gtklistview.c +++ b/gtk/gtklistview.c @@ -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; } diff --git a/gtk/gtklistview.h b/gtk/gtklistview.h index afcfb6225f..e499cfb9d3 100644 --- a/gtk/gtklistview.h +++ b/gtk/gtklistview.h @@ -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); diff --git a/tests/testcolumnview.c b/tests/testcolumnview.c index e60eab6746..58638b9adf 100644 --- a/tests/testcolumnview.c +++ b/tests/testcolumnview.c @@ -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; } diff --git a/tests/testlistdnd.c b/tests/testlistdnd.c index 533f0a8d38..338b522842 100644 --- a/tests/testlistdnd.c +++ b/tests/testlistdnd.c @@ -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); diff --git a/tests/testlistview-animating.c b/tests/testlistview-animating.c index 7902948ab2..1baac2a7b9 100644 --- a/tests/testlistview-animating.c +++ b/tests/testlistview-animating.c @@ -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); diff --git a/tests/testlistview.c b/tests/testlistview.c index c0552d14af..567ab3224c 100644 --- a/tests/testlistview.c +++ b/tests/testlistview.c @@ -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)