Implement GtkSectionModel for all selection models
authorBenjamin Otte <otte@redhat.com>
Tue, 15 Feb 2022 01:03:00 +0000 (02:03 +0100)
committerBenjamin Otte <otte@redhat.com>
Tue, 9 May 2023 15:00:39 +0000 (17:00 +0200)
gtk/gtkmultiselection.c
gtk/gtknoselection.c
gtk/gtksectionmodel.c
gtk/gtksectionmodelprivate.h [new file with mode: 0644]
gtk/gtksingleselection.c

index 055472d3d45cb3aec091acc48c48e61ad2d0a97d..f549541baa6d7779f98b38ed75f4b7a08bcdda58 100644 (file)
@@ -22,6 +22,7 @@
 #include "gtkmultiselection.h"
 
 #include "gtkbitset.h"
+#include "gtksectionmodelprivate.h"
 #include "gtkselectionmodel.h"
 
 /**
@@ -94,6 +95,23 @@ gtk_multi_selection_list_model_init (GListModelInterface *iface)
   iface->get_item = gtk_multi_selection_get_item;
 }
 
+static void
+gtk_multi_selection_get_section (GtkSectionModel *model,
+                                 guint            position,
+                                 guint           *out_start,
+                                 guint           *out_end)
+{
+  GtkMultiSelection *self = GTK_MULTI_SELECTION (model);
+
+  gtk_list_model_get_section (self->model, position, out_start, out_end);
+}
+
+static void
+gtk_multi_selection_section_model_init (GtkSectionModelInterface *iface)
+{
+  iface->get_section = gtk_multi_selection_get_section;
+}
+
 static gboolean
 gtk_multi_selection_is_selected (GtkSelectionModel *model,
                                  guint              position)
@@ -205,6 +223,8 @@ gtk_multi_selection_selection_model_init (GtkSelectionModelInterface *iface)
 G_DEFINE_TYPE_EXTENDED (GtkMultiSelection, gtk_multi_selection, G_TYPE_OBJECT, 0,
                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
                                                gtk_multi_selection_list_model_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_SECTION_MODEL,
+                                               gtk_multi_selection_section_model_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_SELECTION_MODEL,
                                                gtk_multi_selection_selection_model_init))
 
index c0848f7c0e860479dc7efbe17e9878f05da8ca28..20bff24df292306bf787d5e4743974716a7eddf3 100644 (file)
@@ -22,6 +22,7 @@
 #include "gtknoselection.h"
 
 #include "gtkbitset.h"
+#include "gtksectionmodelprivate.h"
 #include "gtkselectionmodel.h"
 
 /**
@@ -92,6 +93,23 @@ gtk_no_selection_list_model_init (GListModelInterface *iface)
   iface->get_item = gtk_no_selection_get_item;
 }
 
+static void
+gtk_no_selection_get_section (GtkSectionModel *model,
+                              guint            position,
+                              guint           *out_start,
+                              guint           *out_end)
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (model);
+
+  gtk_list_model_get_section (self->model, position, out_start, out_end);
+}
+
+static void
+gtk_no_selection_section_model_init (GtkSectionModelInterface *iface)
+{
+  iface->get_section = gtk_no_selection_get_section;
+}
+
 static gboolean
 gtk_no_selection_is_selected (GtkSelectionModel *model,
                               guint              position)
@@ -117,6 +135,8 @@ gtk_no_selection_selection_model_init (GtkSelectionModelInterface *iface)
 G_DEFINE_TYPE_EXTENDED (GtkNoSelection, gtk_no_selection, G_TYPE_OBJECT, 0,
                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
                                                gtk_no_selection_list_model_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_SECTION_MODEL,
+                                               gtk_no_selection_section_model_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_SELECTION_MODEL,
                                                gtk_no_selection_selection_model_init))
 
index a3763a1c64e4a37c0b34147ff2d685d6349e49b9..2df8562e01795b5d5409751d5526aa0401053ad0 100644 (file)
@@ -19,7 +19,7 @@
 
 #include "config.h"
 
-#include "gtksectionmodel.h"
+#include "gtksectionmodelprivate.h"
 
 #include "gtkmarshalers.h"
 
@@ -146,6 +146,49 @@ gtk_section_model_get_section (GtkSectionModel *self,
   g_warn_if_fail (*out_start < *out_end);
 }
 
+/* A version of gtk_section_model_get_section() that handles NULL
+ * (treats it as the empty list) and any GListModel (treats it as
+ * a single section).
+ **/
+void
+gtk_list_model_get_section (GListModel *self,
+                            guint       position,
+                            guint      *out_start,
+                            guint      *out_end)
+{
+  g_return_if_fail (out_start != NULL);
+  g_return_if_fail (out_end != NULL);
+
+  if (self == NULL)
+    {
+      *out_start = 0;
+      *out_end = G_MAXUINT;
+      return;
+    }
+
+  g_return_if_fail (G_IS_LIST_MODEL (self));
+
+  if (!GTK_IS_SECTION_MODEL (self))
+    {
+      guint n_items = g_list_model_get_n_items (self);
+
+      if (position < n_items)
+        {
+          *out_start = 0;
+          *out_end = G_MAXUINT;
+        }
+      else
+        {
+          *out_start = n_items;
+          *out_end = G_MAXUINT;
+        }
+
+      return;
+    }
+
+  gtk_section_model_get_section (GTK_SECTION_MODEL (self), position, out_start, out_end);
+}
+
 /**
  * gtk_section_model_section_changed:
  * @self: a `GtkSectionModel`
diff --git a/gtk/gtksectionmodelprivate.h b/gtk/gtksectionmodelprivate.h
new file mode 100644 (file)
index 0000000..e6cfb0c
--- /dev/null
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "gtksectionmodel.h"
+
+G_BEGIN_DECLS
+
+void                    gtk_list_model_get_section              (GListModel           *self,
+                                                                 guint                 position,
+                                                                 guint                *out_start,
+                                                                 guint                *out_end);
+
+
+G_END_DECLS
+
index e4e148ec83fcb6f07a86b3d803bb9778494394b9..07f0c7eaee634cdfbf313e1338b558e49a5a7f90 100644 (file)
@@ -22,6 +22,7 @@
 #include "gtksingleselection.h"
 
 #include "gtkbitset.h"
+#include "gtksectionmodelprivate.h"
 #include "gtkselectionmodel.h"
 
 /**
@@ -103,6 +104,23 @@ gtk_single_selection_list_model_init (GListModelInterface *iface)
   iface->get_item = gtk_single_selection_get_item;
 }
 
+static void
+gtk_single_selection_get_section (GtkSectionModel *model,
+                                  guint            position,
+                                  guint           *out_start,
+                                  guint           *out_end)
+{
+  GtkSingleSelection *self = GTK_SINGLE_SELECTION (model);
+
+  gtk_list_model_get_section (self->model, position, out_start, out_end);
+}
+
+static void
+gtk_single_selection_section_model_init (GtkSectionModelInterface *iface)
+{
+  iface->get_section = gtk_single_selection_get_section;
+}
+
 static gboolean
 gtk_single_selection_is_selected (GtkSelectionModel *model,
                                   guint              position)
@@ -167,6 +185,8 @@ gtk_single_selection_selection_model_init (GtkSelectionModelInterface *iface)
 G_DEFINE_TYPE_EXTENDED (GtkSingleSelection, gtk_single_selection, G_TYPE_OBJECT, 0,
                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
                                                gtk_single_selection_list_model_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_SECTION_MODEL,
+                                               gtk_single_selection_section_model_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_SELECTION_MODEL,
                                                gtk_single_selection_selection_model_init))