Implement GtkSectionModel for all selection models

This commit is contained in:
Benjamin Otte 2022-02-15 02:03:00 +01:00
parent 45c9e7aff4
commit bd999c72c9
5 changed files with 118 additions and 1 deletions

View 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))

View 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))

View 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`

View File

@ -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

View 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))