gtk: Add GtkNoSelection
authorBenjamin Otte <otte@redhat.com>
Mon, 7 Oct 2019 04:28:28 +0000 (06:28 +0200)
committerBenjamin Otte <otte@redhat.com>
Tue, 15 Oct 2019 05:17:07 +0000 (07:17 +0200)
Allows not to have anything selected.

docs/reference/gtk/gtk4-docs.xml
docs/reference/gtk/gtk4-sections.txt
gtk/gtk.h
gtk/gtknoselection.c [new file with mode: 0644]
gtk/gtknoselection.h [new file with mode: 0644]
gtk/meson.build
testsuite/gtk/defaultvalue.c
testsuite/gtk/notify.c
testsuite/gtk/objects-finalize.c

index d220270a81c3281b377ae4a1c16415557c648696..1835c7b1befcec4f6d8a74419948fc3b6d6175d8 100644 (file)
@@ -51,6 +51,7 @@
       <xi:include href="xml/gtksortlistmodel.xml" />
       <xi:include href="xml/gtktreelistmodel.xml" />
       <xi:include href="xml/gtkselectionmodel.xml" />
+      <xi:include href="xml/gtknoselection.xml" />
       <xi:include href="xml/gtksingleselection.xml" />
     </chapter>
 
index 65693b1587af6d33b70d2cffa3398af808952a56..6ba53c0c561e6eae4019fda4af3d9e1c0e99e8f0 100644 (file)
@@ -465,6 +465,16 @@ GTK_TYPE_SELECTION_MODEL
 gtk_selection_model_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtknoselection</FILE>
+<TITLE>GtkNoSelection</TITLE>
+GtkNoSelection
+gtk_no_selection_new
+gtk_no_selection_get_model
+<SUBSECTION Private>
+gtk_no_selection_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtksingleselection</FILE>
 <TITLE>GtkSingleSelection</TITLE>
index 29a37ff5f38474b51dfe16be7a49af568f4faa1a..4ce7e58098c980113e1b9540f159eea16fa8ca99 100644 (file)
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
 #include <gtk/gtkmountoperation.h>
 #include <gtk/gtknative.h>
 #include <gtk/gtknativedialog.h>
+#include <gtk/gtknoselection.h>
 #include <gtk/gtknotebook.h>
 #include <gtk/gtkorientable.h>
 #include <gtk/gtkoverlay.h>
diff --git a/gtk/gtknoselection.c b/gtk/gtknoselection.c
new file mode 100644 (file)
index 0000000..c3706f2
--- /dev/null
@@ -0,0 +1,254 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#include "config.h"
+
+#include "gtknoselection.h"
+
+#include "gtkintl.h"
+#include "gtkselectionmodel.h"
+
+/**
+ * SECTION:gtknoselection
+ * @Short_description: A selection model that does not allow selecting anything
+ * @Title: GtkNoSelection
+ * @see_also: #GtkSelectionModel
+ *
+ * GtkNoSelection is an implementation of the #GtkSelectionModel interface 
+ * that does not allow selecting anything.
+ *
+ * This model is meant to be used as a simple wrapper to #GListModels when a
+ * #GtkSelectionModel is required.
+ */
+struct _GtkNoSelection
+{
+  GObject parent_instance;
+
+  GListModel *model;
+};
+
+struct _GtkNoSelectionClass
+{
+  GObjectClass parent_class;
+};
+
+enum {
+  PROP_0,
+  PROP_MODEL,
+  N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS] = { NULL, };
+
+static GType
+gtk_no_selection_get_item_type (GListModel *list)
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (list);
+
+  return g_list_model_get_item_type (self->model);
+}
+
+static guint
+gtk_no_selection_get_n_items (GListModel *list)
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (list);
+
+  return g_list_model_get_n_items (self->model);
+}
+
+static gpointer
+gtk_no_selection_get_item (GListModel *list,
+                               guint       position)
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (list);
+
+  return g_list_model_get_item (self->model, position);
+}
+
+static void
+gtk_no_selection_list_model_init (GListModelInterface *iface)
+{
+  iface->get_item_type = gtk_no_selection_get_item_type;
+  iface->get_n_items = gtk_no_selection_get_n_items;
+  iface->get_item = gtk_no_selection_get_item;
+}
+
+static gboolean
+gtk_no_selection_is_selected (GtkSelectionModel *model,
+                              guint              position)
+{
+  return FALSE;
+}
+
+static void
+gtk_no_selection_query_range (GtkSelectionModel *model,
+                              guint              position,
+                              guint             *start_range,
+                              guint             *n_range,
+                              gboolean          *selected)
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (model);
+
+  *start_range = 0;
+  *n_range = g_list_model_get_n_items (self->model);
+  *selected = FALSE;
+}
+
+static void
+gtk_no_selection_selection_model_init (GtkSelectionModelInterface *iface)
+{
+  iface->is_selected = gtk_no_selection_is_selected; 
+  iface->query_range = gtk_no_selection_query_range;
+}
+
+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_SELECTION_MODEL,
+                                               gtk_no_selection_selection_model_init))
+
+static void
+gtk_no_selection_clear_model (GtkNoSelection *self)
+{
+  if (self->model == NULL)
+    return;
+
+  g_signal_handlers_disconnect_by_func (self->model, 
+                                        g_list_model_items_changed,
+                                        self);
+  g_clear_object (&self->model);
+}
+
+static void
+gtk_no_selection_set_property (GObject      *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (object);
+
+  switch (prop_id)
+    {
+    case PROP_MODEL:
+      gtk_no_selection_clear_model (self);
+      self->model = g_value_dup_object (value);
+      g_signal_connect_swapped (self->model, "items-changed",
+                                G_CALLBACK (g_list_model_items_changed), self);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_no_selection_get_property (GObject    *object,
+                               guint       prop_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (object);
+
+  switch (prop_id)
+    {
+    case PROP_MODEL:
+      g_value_set_object (value, self->model);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_no_selection_dispose (GObject *object)
+{
+  GtkNoSelection *self = GTK_NO_SELECTION (object);
+
+  gtk_no_selection_clear_model (self);
+
+  G_OBJECT_CLASS (gtk_no_selection_parent_class)->dispose (object);
+}
+
+static void
+gtk_no_selection_class_init (GtkNoSelectionClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->get_property = gtk_no_selection_get_property;
+  gobject_class->set_property = gtk_no_selection_set_property;
+  gobject_class->dispose = gtk_no_selection_dispose;
+
+  /**
+   * GtkNoSelection:model:
+   *
+   * The model being managed
+   */
+  properties[PROP_MODEL] =
+    g_param_spec_object ("model",
+                       P_("The model"),
+                       P_("The model being managed"),
+                       G_TYPE_LIST_MODEL,
+                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (gobject_class, N_PROPS, properties);
+}
+
+static void
+gtk_no_selection_init (GtkNoSelection *self)
+{
+}
+
+/**
+ * gtk_no_selection_new:
+ * @model: (transfer none): the #GListModel to manage
+ *
+ * Creates a new selection to handle @model.
+ *
+ * Returns: (transfer full) (type GtkNoSelection): a new #GtkNoSelection
+ **/
+GtkNoSelection *
+gtk_no_selection_new (GListModel *model)
+{
+  g_return_val_if_fail (G_IS_LIST_MODEL (model), NULL);
+
+  return g_object_new (GTK_TYPE_NO_SELECTION,
+                       "model", model,
+                       NULL);
+}
+
+/**
+ * gtk_no_selection_get_model:
+ * @self: a #GtkNoSelection
+ *
+ * Gets the model that @self is wrapping.
+ *
+ * Returns: (transfer none): The model being wrapped
+ **/
+GListModel *
+gtk_no_selection_get_model (GtkNoSelection *self)
+{
+  g_return_val_if_fail (GTK_IS_NO_SELECTION (self), NULL);
+
+  return self->model;
+}
+
diff --git a/gtk/gtknoselection.h b/gtk/gtknoselection.h
new file mode 100644 (file)
index 0000000..e6341df
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GTK_NO_SELECTION_H__
+#define __GTK_NO_SELECTION_H__
+
+#include <gtk/gtktypes.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_NO_SELECTION (gtk_no_selection_get_type ())
+
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkNoSelection, gtk_no_selection, GTK, NO_SELECTION, GObject)
+
+GDK_AVAILABLE_IN_ALL
+GtkNoSelection *        gtk_no_selection_new                    (GListModel             *model);
+
+GDK_AVAILABLE_IN_ALL
+GListModel *            gtk_no_selection_get_model              (GtkNoSelection         *self);
+
+G_END_DECLS
+
+#endif /* __GTK_NO_SELECTION_H__ */
index ae399247b6e6e374e9f3364bde874ea8267dfa3b..3119e059343d1c7c0dbbd1ba4e020de5e19e08ef 100644 (file)
@@ -295,6 +295,7 @@ gtk_public_sources = files([
   'gtkmountoperation.c',
   'gtknativedialog.c',
   'gtknomediafile.c',
+  'gtknoselection.c',
   'gtknotebook.c',
   'gtkorientable.c',
   'gtkoverlay.c',
@@ -554,6 +555,7 @@ gtk_public_headers = files([
   'gtkmountoperation.h',
   'gtknative.h',
   'gtknativedialog.h',
+  'gtknoselection.h',
   'gtknotebook.h',
   'gtkorientable.h',
   'gtkoverlay.h',
index 556adb96934c09c61260042bcda8342abab44fbb..4c79386cac3f28643f968c32b820528b2fa723c1 100644 (file)
@@ -110,6 +110,7 @@ test_type (gconstpointer data)
                                                                 &(GdkRectangle) { 0, 0, 100, 100 })));
     }
   else if (g_type_is_a (type, GTK_TYPE_FILTER_LIST_MODEL) ||
+           g_type_is_a (type, GTK_TYPE_NO_SELECTION) ||
            g_type_is_a (type, GTK_TYPE_SINGLE_SELECTION))
     {
       GListStore *list_store = g_list_store_new (G_TYPE_OBJECT);
@@ -257,6 +258,7 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
 G_GNUC_END_IGNORE_DEPRECATIONS
 
       if ((g_type_is_a (type, GTK_TYPE_FILTER_LIST_MODEL) ||
+           g_type_is_a (type, GTK_TYPE_NO_SELECTION) ||
            g_type_is_a (type, GTK_TYPE_SINGLE_SELECTION)) &&
           strcmp (pspec->name, "model") == 0)
         continue;
index 83a04b0776ec3cdedd10393eb3208d31282e2c0b..4a845ed4470ec8b114245c7d36945f4fa75d57f2 100644 (file)
@@ -432,6 +432,7 @@ test_type (gconstpointer data)
       gdk_content_formats_unref (formats);
     }
   else if (g_type_is_a (type, GTK_TYPE_FILTER_LIST_MODEL) ||
+           g_type_is_a (type, GTK_TYPE_NO_SELECTION) ||
            g_type_is_a (type, GTK_TYPE_SINGLE_SELECTION))
     {
       GListStore *list_store = g_list_store_new (G_TYPE_OBJECT);
index 722067c1181afeb09d370937bb208891c90a23e4..5ee575bf3b2e06b193f72706af19e8a12efc795e 100644 (file)
@@ -65,6 +65,7 @@ test_finalize_object (gconstpointer data)
       gdk_content_formats_unref (formats);
     }
   else if (g_type_is_a (test_type, GTK_TYPE_FILTER_LIST_MODEL) ||
+           g_type_is_a (test_type, GTK_TYPE_NO_SELECTION) ||
            g_type_is_a (test_type, GTK_TYPE_SINGLE_SELECTION))
     {
       GListStore *list_store = g_list_store_new (G_TYPE_OBJECT);