diff options
author | Benjamin Otte <otte@redhat.com> | 2022-02-15 02:03:00 +0100 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2023-05-09 17:00:39 +0200 |
commit | bd999c72c9b3ebf0a9dd5951e6a72a20d9b9914d (patch) | |
tree | 5b43bb0ed95ccf98ffcdd88d7f679ad411524855 /gtk | |
parent | 45c9e7aff4715208a77feaa4e9bd20a0c376c3cc (diff) | |
download | gtk+-bd999c72c9b3ebf0a9dd5951e6a72a20d9b9914d.tar.gz |
Implement GtkSectionModel for all selection models
Diffstat (limited to 'gtk')
-rw-r--r-- | gtk/gtkmultiselection.c | 20 | ||||
-rw-r--r-- | gtk/gtknoselection.c | 20 | ||||
-rw-r--r-- | gtk/gtksectionmodel.c | 45 | ||||
-rw-r--r-- | gtk/gtksectionmodelprivate.h | 14 | ||||
-rw-r--r-- | gtk/gtksingleselection.c | 20 |
5 files changed, 118 insertions, 1 deletions
diff --git a/gtk/gtkmultiselection.c b/gtk/gtkmultiselection.c index 055472d3d4..f549541baa 100644 --- a/gtk/gtkmultiselection.c +++ b/gtk/gtkmultiselection.c @@ -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)) diff --git a/gtk/gtknoselection.c b/gtk/gtknoselection.c index c0848f7c0e..20bff24df2 100644 --- a/gtk/gtknoselection.c +++ b/gtk/gtknoselection.c @@ -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)) diff --git a/gtk/gtksectionmodel.c b/gtk/gtksectionmodel.c index a3763a1c64..2df8562e01 100644 --- a/gtk/gtksectionmodel.c +++ b/gtk/gtksectionmodel.c @@ -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 index 0000000000..e6cfb0cb1b --- /dev/null +++ b/gtk/gtksectionmodelprivate.h @@ -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 + diff --git a/gtk/gtksingleselection.c b/gtk/gtksingleselection.c index e4e148ec83..07f0c7eaee 100644 --- a/gtk/gtksingleselection.c +++ b/gtk/gtksingleselection.c @@ -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)) |