[gtk/view-model-selection] Change list widget to use a GtkSelectionModel



commit ab87b703cf25941e1ef311c8992ece46e9daefee
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jul 16 16:16:02 2020 -0400

    Change list widget to use a GtkSelectionModel
    
    Change the type of the model property in GtkListView,
    GtkColumnView and GtkGridView to be GtkSelectionModel.
    
    The convenience of automatic interposition of a selection
    model is minor, compared to the confusion.

 gtk/gtkcolumnview.c            | 15 ++++++---------
 gtk/gtkcolumnview.h            |  5 +++--
 gtk/gtkcustompaperunixdialog.c | 16 ++++++++--------
 gtk/gtkdropdown.c              |  2 +-
 gtk/gtkgridview.c              | 12 ++++++------
 gtk/gtkgridview.h              |  5 +++--
 gtk/gtklistbase.c              | 24 +++++++-----------------
 gtk/gtklistbaseprivate.h       |  5 +++--
 gtk/gtklistview.c              | 20 +++++++++-----------
 gtk/gtklistview.h              |  5 +++--
 gtk/gtkprintunixdialog.c       | 24 +++++++++++++-----------
 11 files changed, 62 insertions(+), 71 deletions(-)
---
diff --git a/gtk/gtkcolumnview.c b/gtk/gtkcolumnview.c
index acaa2b9b07..0328c136bd 100644
--- a/gtk/gtkcolumnview.c
+++ b/gtk/gtkcolumnview.c
@@ -628,7 +628,7 @@ gtk_column_view_class_init (GtkColumnViewClass *klass)
     g_param_spec_object ("model",
                          P_("Model"),
                          P_("Model for the items displayed"),
-                         G_TYPE_LIST_MODEL,
+                         GTK_TYPE_SELECTION_MODEL,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
   /**
@@ -1202,7 +1202,7 @@ gtk_column_view_new (void)
  *
  * Returns: (nullable) (transfer none): The model in use
  **/
-GListModel *
+GtkSelectionModel *
 gtk_column_view_get_model (GtkColumnView *self)
 {
   g_return_val_if_fail (GTK_IS_COLUMN_VIEW (self), NULL);
@@ -1215,17 +1215,14 @@ gtk_column_view_get_model (GtkColumnView *self)
  * @self: a #GtkColumnView
  * @model: (allow-none) (transfer none): the model to use or %NULL for none
  *
- * Sets the #GListModel to use.
- *
- * If the @model is a #GtkSelectionModel, it is used for managing the selection.
- * Otherwise, @self creates a #GtkSingleSelection for the selection.
+ * Sets the #GtkSelectionModel to use.
  **/
 void
-gtk_column_view_set_model (GtkColumnView *self,
-                           GListModel  *model)
+gtk_column_view_set_model (GtkColumnView     *self,
+                           GtkSelectionModel *model)
 {
   g_return_if_fail (GTK_IS_COLUMN_VIEW (self));
-  g_return_if_fail (model == NULL || G_IS_LIST_MODEL (model));
+  g_return_if_fail (model == NULL || GTK_IS_SELECTION_MODEL (model));
 
   if (gtk_list_view_get_model (self->listview) == model)
     return;
diff --git a/gtk/gtkcolumnview.h b/gtk/gtkcolumnview.h
index 0983ea2c39..9b49f85a19 100644
--- a/gtk/gtkcolumnview.h
+++ b/gtk/gtkcolumnview.h
@@ -26,6 +26,7 @@
 
 #include <gtk/gtktypes.h>
 #include <gtk/gtksortlistmodel.h>
+#include <gtk/gtkselectionmodel.h>
 #include <gtk/gtksorter.h>
 
 G_BEGIN_DECLS
@@ -67,10 +68,10 @@ void            gtk_column_view_insert_column                   (GtkColumnView
                                                                  GtkColumnViewColumn    *column);
 
 GDK_AVAILABLE_IN_ALL
-GListModel *    gtk_column_view_get_model                       (GtkColumnView          *self);
+GtkSelectionModel * gtk_column_view_get_model                   (GtkColumnView          *self);
 GDK_AVAILABLE_IN_ALL
 void            gtk_column_view_set_model                       (GtkColumnView          *self,
-                                                                 GListModel             *model);
+                                                                 GtkSelectionModel      *model);
 
 GDK_AVAILABLE_IN_ALL
 gboolean        gtk_column_view_get_show_row_separators         (GtkColumnView          *self);
diff --git a/gtk/gtkcustompaperunixdialog.c b/gtk/gtkcustompaperunixdialog.c
index 75e471689c..b337a01425 100644
--- a/gtk/gtkcustompaperunixdialog.c
+++ b/gtk/gtkcustompaperunixdialog.c
@@ -513,7 +513,7 @@ update_combo_sensitivity_from_printers (GtkCustomPaperUnixDialog *dialog)
 static void
 update_custom_widgets_from_list (GtkCustomPaperUnixDialog *dialog)
 {
-  GListModel *model;
+  GtkSelectionModel *model;
   GtkPageSetup *page_setup;
 
   model = gtk_list_view_get_model (GTK_LIST_VIEW (dialog->listview));
@@ -559,7 +559,7 @@ static void
 unit_widget_changed (GtkCustomPaperUnixDialog *dialog)
 {
   double w, h, top, bottom, left, right;
-  GListModel *model;
+  GtkSelectionModel *model;
   GtkPageSetup *page_setup;
   GtkPaperSize *paper_size;
 
@@ -648,7 +648,7 @@ add_custom_paper (GtkCustomPaperUnixDialog *dialog)
 static void
 remove_custom_paper (GtkCustomPaperUnixDialog *dialog)
 {
-  GListModel *model;
+  GtkSelectionModel *model;
   guint selected;
 
   model = gtk_list_view_get_model (GTK_LIST_VIEW (dialog->listview));
@@ -870,7 +870,7 @@ populate_dialog (GtkCustomPaperUnixDialog *dialog)
   GtkWidget *grid, *label, *widget, *frame, *combo;
   GtkWidget *hbox, *vbox, *listview, *scrolled, *toolbar, *button;
   GtkUnit user_units;
-  GListModel *model;
+  GtkSingleSelection *selection;
   GtkListItemFactory *factory;
 
   content_area = gtk_dialog_get_content_area (cpu_dialog);
@@ -899,10 +899,10 @@ populate_dialog (GtkCustomPaperUnixDialog *dialog)
   listview = gtk_list_view_new ();
   gtk_widget_set_size_request (listview, 140, -1);
 
-  model = G_LIST_MODEL (gtk_single_selection_new (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);
+  selection = gtk_single_selection_new (G_LIST_MODEL (dialog->custom_paper_list));
+  gtk_list_view_set_model (GTK_LIST_VIEW (listview), GTK_SELECTION_MODEL (selection));
+  g_signal_connect (selection, "notify::selected", G_CALLBACK (selected_custom_paper_changed), dialog);
+  g_object_unref (selection);
 
   factory = gtk_signal_list_item_factory_new ();
   g_signal_connect (factory, "setup", G_CALLBACK (setup_item), NULL);
diff --git a/gtk/gtkdropdown.c b/gtk/gtkdropdown.c
index de1ba763cb..087dffc5e9 100644
--- a/gtk/gtkdropdown.c
+++ b/gtk/gtkdropdown.c
@@ -673,7 +673,7 @@ gtk_drop_down_set_model (GtkDropDown *self,
 
       selection = G_LIST_MODEL (gtk_single_selection_new (filter_model));
       g_set_object (&self->popup_selection, selection);
-      gtk_list_view_set_model (GTK_LIST_VIEW (self->popup_list), selection);
+      gtk_list_view_set_model (GTK_LIST_VIEW (self->popup_list), GTK_SELECTION_MODEL (selection));
       g_object_unref (selection);
 
       selection = G_LIST_MODEL (gtk_single_selection_new (model));
diff --git a/gtk/gtkgridview.c b/gtk/gtkgridview.c
index 049b3c2906..f5349767d5 100644
--- a/gtk/gtkgridview.c
+++ b/gtk/gtkgridview.c
@@ -1092,7 +1092,7 @@ gtk_grid_view_class_init (GtkGridViewClass *klass)
     g_param_spec_object ("model",
                          P_("Model"),
                          P_("Model for the items displayed"),
-                         G_TYPE_LIST_MODEL,
+                         GTK_TYPE_SELECTION_MODEL,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
   /**
@@ -1235,7 +1235,7 @@ gtk_grid_view_new_with_factory (GtkListItemFactory *factory)
  *
  * Returns: (nullable) (transfer none): The model in use
  **/
-GListModel *
+GtkSelectionModel *
 gtk_grid_view_get_model (GtkGridView *self)
 {
   g_return_val_if_fail (GTK_IS_GRID_VIEW (self), NULL);
@@ -1248,14 +1248,14 @@ gtk_grid_view_get_model (GtkGridView *self)
  * @self: a #GtkGridView
  * @model: (allow-none) (transfer none): the model to use or %NULL for none
  *
- * Sets the #GListModel to use for
+ * Sets the #GtkSelectionModel to use for
  **/
 void
-gtk_grid_view_set_model (GtkGridView *self,
-                         GListModel  *model)
+gtk_grid_view_set_model (GtkGridView       *self,
+                         GtkSelectionModel *model)
 {
   g_return_if_fail (GTK_IS_GRID_VIEW (self));
-  g_return_if_fail (model == NULL || G_IS_LIST_MODEL (model));
+  g_return_if_fail (model == NULL || GTK_IS_SELECTION_MODEL (model));
 
   if (!gtk_list_base_set_model (GTK_LIST_BASE (self), model))
     return;
diff --git a/gtk/gtkgridview.h b/gtk/gtkgridview.h
index 6fb035ce12..39931fa5fa 100644
--- a/gtk/gtkgridview.h
+++ b/gtk/gtkgridview.h
@@ -25,6 +25,7 @@
 #endif
 
 #include <gtk/gtklistbase.h>
+#include <gtk/gtkselectionmodel.h>
 
 G_BEGIN_DECLS
 
@@ -53,10 +54,10 @@ GDK_AVAILABLE_IN_ALL
 GtkWidget *     gtk_grid_view_new_with_factory                  (GtkListItemFactory     *factory);
 
 GDK_AVAILABLE_IN_ALL
-GListModel *    gtk_grid_view_get_model                         (GtkGridView            *self);
+GtkSelectionModel * gtk_grid_view_get_model                     (GtkGridView            *self);
 GDK_AVAILABLE_IN_ALL
 void            gtk_grid_view_set_model                         (GtkGridView            *self,
-                                                                 GListModel             *model);
+                                                                 GtkSelectionModel      *model);
 GDK_AVAILABLE_IN_ALL
 void            gtk_grid_view_set_factory                       (GtkGridView            *self,
                                                                  GtkListItemFactory     *factory);
diff --git a/gtk/gtklistbase.c b/gtk/gtklistbase.c
index 5a1a38e40a..2fab8c2700 100644
--- a/gtk/gtklistbase.c
+++ b/gtk/gtklistbase.c
@@ -2102,40 +2102,30 @@ gtk_list_base_grab_focus_on_item (GtkListBase *self,
   return TRUE;
 }
 
-GListModel *
+GtkSelectionModel *
 gtk_list_base_get_model (GtkListBase *self)
 {
   GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
 
-  return priv->model;
+  return GTK_SELECTION_MODEL (priv->model);
 }
 
 gboolean
-gtk_list_base_set_model (GtkListBase *self,
-                         GListModel  *model)
+gtk_list_base_set_model (GtkListBase       *self,
+                         GtkSelectionModel *model)
 {
   GtkListBasePrivate *priv = gtk_list_base_get_instance_private (self);
 
-  if (priv->model == model)
+  if (priv->model == G_LIST_MODEL (model))
     return FALSE;
 
   g_clear_object (&priv->model);
 
   if (model)
     {
-      GtkSelectionModel *selection_model;
-
-      priv->model = g_object_ref (model);
-
-      if (GTK_IS_SELECTION_MODEL (model))
-        selection_model = GTK_SELECTION_MODEL (g_object_ref (model));
-      else
-        selection_model = GTK_SELECTION_MODEL (gtk_single_selection_new (model));
-
-      gtk_list_item_manager_set_model (priv->item_manager, selection_model);
+      priv->model = g_object_ref (G_LIST_MODEL (model));
+      gtk_list_item_manager_set_model (priv->item_manager, model);
       gtk_list_base_set_anchor (self, 0, 0.0, GTK_PACK_START, 0.0, GTK_PACK_START);
-
-      g_object_unref (selection_model);
     }
   else
     {
diff --git a/gtk/gtklistbaseprivate.h b/gtk/gtklistbaseprivate.h
index c714c63c0c..f53a4c6efb 100644
--- a/gtk/gtklistbaseprivate.h
+++ b/gtk/gtklistbaseprivate.h
@@ -23,6 +23,7 @@
 #include "gtklistbase.h"
 
 #include "gtklistitemmanagerprivate.h"
+#include "gtkselectionmodel.h"
 #include "gtkprivate.h"
 
 struct _GtkListBase
@@ -71,9 +72,9 @@ GtkListItemManager *   gtk_list_base_get_manager                (GtkListBase
 GtkScrollablePolicy    gtk_list_base_get_scroll_policy          (GtkListBase            *self,
                                                                  GtkOrientation          orientation);
 guint                  gtk_list_base_get_n_items                (GtkListBase            *self);
-GListModel *           gtk_list_base_get_model                  (GtkListBase            *self);
+GtkSelectionModel *    gtk_list_base_get_model                  (GtkListBase            *self);
 gboolean               gtk_list_base_set_model                  (GtkListBase            *self,
-                                                                 GListModel             *model);
+                                                                 GtkSelectionModel      *model);
 void                   gtk_list_base_update_adjustments         (GtkListBase            *self,
                                                                  int                     total_across,
                                                                  int                     total_along,
diff --git a/gtk/gtklistview.c b/gtk/gtklistview.c
index 3a65706ee6..28164cf174 100644
--- a/gtk/gtklistview.c
+++ b/gtk/gtklistview.c
@@ -831,13 +831,14 @@ gtk_list_view_class_init (GtkListViewClass *klass)
   /**
    * GtkListView:model:
    *
-   * Model for the items displayed
+   * Model for the items displayed.
+   * This must be a #GtkSelectionModel
    */
   properties[PROP_MODEL] =
     g_param_spec_object ("model",
                          P_("Model"),
                          P_("Model for the items displayed"),
-                         G_TYPE_LIST_MODEL,
+                         GTK_TYPE_SELECTION_MODEL,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
   /**
@@ -989,7 +990,7 @@ gtk_list_view_new_with_factory (GtkListItemFactory *factory)
  *
  * Returns: (nullable) (transfer none): The model in use
  **/
-GListModel *
+GtkSelectionModel *
 gtk_list_view_get_model (GtkListView *self)
 {
   g_return_val_if_fail (GTK_IS_LIST_VIEW (self), NULL);
@@ -1002,17 +1003,14 @@ gtk_list_view_get_model (GtkListView *self)
  * @self: a #GtkListView
  * @model: (allow-none) (transfer none): the model to use or %NULL for none
  *
- * Sets the #GListModel to use.
- *
- * If the @model is a #GtkSelectionModel, it is used for managing the selection.
- * Otherwise, @self creates a #GtkSingleSelection for the selection.
- **/
+ * Sets the #GtkSelectionModel to use.
+ */
 void
-gtk_list_view_set_model (GtkListView *self,
-                         GListModel  *model)
+gtk_list_view_set_model (GtkListView       *self,
+                         GtkSelectionModel *model)
 {
   g_return_if_fail (GTK_IS_LIST_VIEW (self));
-  g_return_if_fail (model == NULL || G_IS_LIST_MODEL (model));
+  g_return_if_fail (model == NULL || GTK_IS_SELECTION_MODEL (model));
 
   if (!gtk_list_base_set_model (GTK_LIST_BASE (self), model))
     return;
diff --git a/gtk/gtklistview.h b/gtk/gtklistview.h
index afcfb6225f..d3c8cec583 100644
--- a/gtk/gtklistview.h
+++ b/gtk/gtklistview.h
@@ -25,6 +25,7 @@
 #endif
 
 #include <gtk/gtklistbase.h>
+#include <gtk/gtkselectionmodel.h>
 
 G_BEGIN_DECLS
 
@@ -52,10 +53,10 @@ GDK_AVAILABLE_IN_ALL
 GtkWidget *     gtk_list_view_new_with_factory                  (GtkListItemFactory     *factory);
 
 GDK_AVAILABLE_IN_ALL
-GListModel *    gtk_list_view_get_model                         (GtkListView            *self);
+GtkSelectionModel * gtk_list_view_get_model                     (GtkListView            *self);
 GDK_AVAILABLE_IN_ALL
 void            gtk_list_view_set_model                         (GtkListView            *self,
-                                                                 GListModel             *model);
+                                                                 GtkSelectionModel      *model);
 GDK_AVAILABLE_IN_ALL
 void            gtk_list_view_set_factory                       (GtkListView            *self,
                                                                  GtkListItemFactory     *factory);
diff --git a/gtk/gtkprintunixdialog.c b/gtk/gtkprintunixdialog.c
index eac69cecaf..49a188b426 100644
--- a/gtk/gtkprintunixdialog.c
+++ b/gtk/gtkprintunixdialog.c
@@ -740,7 +740,7 @@ gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
   GListModel *model;
   GListModel *sorted;
   GListModel *filtered;
-  GListModel *selection;
+  GtkSingleSelection *selection;
   GtkSorter *sorter;
   GtkFilter *filter;
   GtkFilter *filter1;
@@ -833,10 +833,10 @@ gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
   filtered = G_LIST_MODEL (gtk_filter_list_model_new (sorted, filter));
   g_object_unref (filter);
 
-  selection = G_LIST_MODEL (gtk_single_selection_new (filtered));
-  gtk_single_selection_set_autoselect (GTK_SINGLE_SELECTION (selection), FALSE);
-  gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (selection), GTK_INVALID_LIST_POSITION);
-  gtk_column_view_set_model (GTK_COLUMN_VIEW (dialog->printer_list), selection);
+  selection = gtk_single_selection_new (filtered);
+  gtk_single_selection_set_autoselect (selection, FALSE);
+  gtk_single_selection_set_selected (selection, GTK_INVALID_LIST_POSITION);
+  gtk_column_view_set_model (GTK_COLUMN_VIEW (dialog->printer_list), GTK_SELECTION_MODEL (selection));
   g_signal_connect (selection, "items-changed", G_CALLBACK (printer_added_cb), dialog);
   g_signal_connect_swapped (selection, "notify::selected", G_CALLBACK (selected_printer_changed), dialog);
   g_object_unref (selection);
@@ -983,18 +983,18 @@ printer_status_cb (GtkPrintBackend    *backend,
                    GtkPrinter         *printer,
                    GtkPrintUnixDialog *dialog)
 {
-  GListModel *model;
+  GtkSingleSelection *selection;
 
   /* When the pause state change then we need to update sensitive property
    * of GTK_RESPONSE_OK button inside of selected_printer_changed function.
    */
   selected_printer_changed (dialog);
 
-  model = gtk_column_view_get_model (GTK_COLUMN_VIEW (dialog->printer_list));
+  selection = GTK_SINGLE_SELECTION (gtk_column_view_get_model (GTK_COLUMN_VIEW (dialog->printer_list)));
 
   if (gtk_print_backend_printer_list_is_done (backend) &&
       gtk_printer_is_default (printer) &&
-      gtk_single_selection_get_selected (GTK_SINGLE_SELECTION (model)) == GTK_INVALID_LIST_POSITION)
+      gtk_single_selection_get_selected (selection) == GTK_INVALID_LIST_POSITION)
     set_active_printer (dialog, gtk_printer_get_name (printer));
 }
 
@@ -1822,9 +1822,11 @@ printer_details_acquired (GtkPrinter         *printer,
 static void
 selected_printer_changed (GtkPrintUnixDialog *dialog)
 {
-  GListModel *model = gtk_column_view_get_model (GTK_COLUMN_VIEW (dialog->printer_list));
+  GtkSingleSelection *selection;
   GtkPrinter *printer;
 
+  selection = GTK_SINGLE_SELECTION (gtk_column_view_get_model (GTK_COLUMN_VIEW (dialog->printer_list)));
+
   /* Whenever the user selects a printer we stop looking for
    * the printer specified in the initial settings
    */
@@ -1837,7 +1839,7 @@ selected_printer_changed (GtkPrintUnixDialog *dialog)
 
   disconnect_printer_details_request (dialog, FALSE);
 
-  printer = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (model));
+  printer = gtk_single_selection_get_selected_item (selection);
 
   /* sets GTK_RESPONSE_OK button sensitivity depending on whether the printer
    * accepts/rejects jobs
@@ -3170,7 +3172,7 @@ set_active_printer (GtkPrintUnixDialog *dialog,
   GtkPrinter *printer;
   guint i;
 
-  model = gtk_column_view_get_model (GTK_COLUMN_VIEW (dialog->printer_list));
+  model = G_LIST_MODEL (gtk_column_view_get_model (GTK_COLUMN_VIEW (dialog->printer_list)));
 
   for (i = 0; i < g_list_model_get_n_items (model); i++)
     {


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