[gtk/stack-fixes] Add a silly example



commit 16f889c459d6fa734acfb9ce3a5c6e7f7ef1a86f
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Feb 10 13:48:14 2019 -0500

    Add a silly example
    
    This demontrates a 'always two' selection with multiple
    stack switchers. Fun

 tests/gtkdoubleselection.c | 297 +++++++++++++++++++++++++++++++++++++++++++++
 tests/gtkdoubleselection.h |  18 +++
 tests/meson.build          |   1 +
 tests/testaltstack.c       | 107 ++++++++++++++++
 4 files changed, 423 insertions(+)
---
diff --git a/tests/gtkdoubleselection.c b/tests/gtkdoubleselection.c
new file mode 100644
index 0000000000..fedc5dfec8
--- /dev/null
+++ b/tests/gtkdoubleselection.c
@@ -0,0 +1,297 @@
+#include "config.h"
+
+#include "gtkdoubleselection.h"
+#include "gtkselectionmodel.h"
+
+struct _GtkDoubleSelection
+{
+  GObject parent_instance;
+
+  GListModel *model;
+  guint selected1;
+  guint selected2;
+};
+
+struct _GtkDoubleSelectionClass
+{
+  GObjectClass parent_class;
+};
+
+enum {
+  PROP_0,
+
+  /* selectionmodel */
+  PROP_MODEL,
+  N_PROPS = PROP_MODEL
+};
+
+static GParamSpec *properties[N_PROPS] = { NULL, };
+
+static GType
+gtk_double_selection_get_item_type (GListModel *list)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (list);
+
+  return g_list_model_get_item_type (self->model);
+}
+
+static guint
+gtk_double_selection_get_n_items (GListModel *list)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (list);
+
+  return g_list_model_get_n_items (self->model);
+}
+
+static gpointer
+gtk_double_selection_get_item (GListModel *list,
+                               guint       position)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (list);
+
+  return g_list_model_get_item (self->model, position);
+}
+
+static void
+gtk_double_selection_list_model_init (GListModelInterface *iface)
+{
+  iface->get_item_type = gtk_double_selection_get_item_type;
+  iface->get_n_items = gtk_double_selection_get_n_items;
+  iface->get_item = gtk_double_selection_get_item;
+}
+
+static gboolean
+gtk_double_selection_is_selected (GtkSelectionModel *model,
+                                  guint              position)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (model);
+
+  return self->selected1 == position || self->selected2 == position;
+}
+
+static gboolean
+gtk_double_selection_select_item (GtkSelectionModel *model,
+                                  guint              position,
+                                  gboolean           exclusive)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (model);
+  guint pos;
+  guint n_items;
+
+  if (position == self->selected1 || position == self->selected2)
+    {
+      /* no change */
+    }
+  else if (position < (self->selected1 + self->selected2) / 2)
+    {
+      pos = MIN (self->selected1, position);
+      n_items = MAX (self->selected1, position) - pos + 1;
+      self->selected1 = position;
+      gtk_selection_model_selection_changed (model, pos, n_items);
+    }
+  else
+    {
+      pos = MIN (self->selected2, position);
+      n_items = MAX (self->selected2, position) - pos + 1;
+      self->selected2 = position;
+      gtk_selection_model_selection_changed (model, pos, n_items);
+    }
+
+  return TRUE;
+}
+
+static gboolean
+gtk_double_selection_query_range (GtkSelectionModel *model,
+                                  guint             *position,
+                                  guint             *n_items)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (model);
+  guint size;
+
+  size = g_list_model_get_n_items (self->model);
+
+  if (*position < self->selected1)
+    {
+      *position = 0;
+      *n_items = MIN (self->selected1, size);
+      return FALSE;
+    }
+  else if (*position == self->selected1)
+    {
+      if (self->selected2 == self->selected1 + 1)
+        {
+          *position = self->selected1;
+          *n_items = 2;
+          return TRUE;
+        }
+      else
+        {
+          *position = self->selected1;
+          *n_items = 1;
+          return TRUE;
+        }
+    }
+  else if (*position > self->selected1 && *position < self->selected2)
+    {
+      *position = self->selected1 + 1;
+      *n_items = MIN(self->selected2, size) - *position;
+      return FALSE;
+    }
+  else if (*position == self->selected2)
+    {
+      if (self->selected2 == self->selected1 + 1)
+        {
+          *position = self->selected1;
+          *n_items = 2;
+          return TRUE;
+        }
+      else
+        {
+          *position = self->selected2;
+          *n_items = 1;
+          return TRUE;
+        }
+    }
+  else if (*position > self->selected2)
+    {
+      *position = self->selected2 + 1;
+      *n_items = size - *position;
+      return FALSE;
+    }
+
+  return FALSE;
+}
+
+static void
+gtk_double_selection_selection_model_init (GtkSelectionModelInterface *iface)
+{
+  iface->is_selected = gtk_double_selection_is_selected; 
+  iface->select_item = gtk_double_selection_select_item; 
+  iface->query_range = gtk_double_selection_query_range;
+}
+
+G_DEFINE_TYPE_EXTENDED (GtkDoubleSelection, gtk_double_selection, G_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
+                                               gtk_double_selection_list_model_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_SELECTION_MODEL,
+                                               gtk_double_selection_selection_model_init))
+
+static void
+gtk_double_selection_items_changed_cb (GListModel         *model,
+                                       guint               position,
+                                       guint               removed,
+                                       guint               added,
+                                       GtkDoubleSelection *self)
+{
+  /* FIXME: maintain selection here */
+  g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
+}
+
+static void
+gtk_double_selection_clear_model (GtkDoubleSelection *self)
+{
+  if (self->model == NULL)
+    return;
+
+  g_signal_handlers_disconnect_by_func (self->model, 
+                                        gtk_double_selection_items_changed_cb,
+                                        self);
+  g_clear_object (&self->model);
+}
+
+static void
+gtk_double_selection_set_property (GObject      *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (object);
+
+  switch (prop_id)
+    {
+    case PROP_MODEL:
+      gtk_double_selection_clear_model (self);
+      self->model = g_value_dup_object (value);
+      if (self->model)
+        g_signal_connect (self->model, "items-changed",
+                          G_CALLBACK (gtk_double_selection_items_changed_cb), self);
+      /* FIXME */
+      g_assert (g_list_model_get_n_items (self->model) >= 2);
+      self->selected1 = 0;
+      self->selected2 = 1;
+        
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_double_selection_get_property (GObject    *object,
+                                   guint       prop_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_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_double_selection_dispose (GObject *object)
+{
+  GtkDoubleSelection *self = GTK_DOUBLE_SELECTION (object);
+
+  gtk_double_selection_clear_model (self);
+
+  G_OBJECT_CLASS (gtk_double_selection_parent_class)->dispose (object);
+}
+
+static void
+gtk_double_selection_class_init (GtkDoubleSelectionClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->get_property = gtk_double_selection_get_property;
+  gobject_class->set_property = gtk_double_selection_set_property;
+  gobject_class->dispose = gtk_double_selection_dispose;
+
+  g_object_class_override_property (gobject_class, PROP_MODEL, "model");
+
+  //g_object_class_install_properties (gobject_class, N_PROPS, properties);
+}
+
+static void
+gtk_double_selection_init (GtkDoubleSelection *self)
+{
+}
+
+/**
+ * gtk_double_selection_new:
+ * @model: (transfer none): the #GListModel to manage
+ *
+ * Creates a new selection to handle @model.
+ *
+ * Returns: (transfer full) (type GtkDoubleSelection): a new #GtkDoubleSelection
+ **/
+GtkDoubleSelection *
+gtk_double_selection_new (GListModel *model)
+{
+  g_return_val_if_fail (G_IS_LIST_MODEL (model), NULL);
+
+  return g_object_new (GTK_TYPE_DOUBLE_SELECTION,
+                       "model", model,
+                       NULL);
+}
diff --git a/tests/gtkdoubleselection.h b/tests/gtkdoubleselection.h
new file mode 100644
index 0000000000..1803ada35f
--- /dev/null
+++ b/tests/gtkdoubleselection.h
@@ -0,0 +1,18 @@
+#ifndef __GTK_DOUBLE_SELECTION_H__
+#define __GTK_DOUBLE_SELECTION_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_DOUBLE_SELECTION (gtk_double_selection_get_type ())
+
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkDoubleSelection, gtk_double_selection, GTK, DOUBLE_SELECTION, GObject)
+
+GDK_AVAILABLE_IN_ALL
+GtkDoubleSelection * gtk_double_selection_new  (GListModel *model);
+
+G_END_DECLS
+
+#endif /* __GTK_DOUBLE_SELECTION_H__ */
diff --git a/tests/meson.build b/tests/meson.build
index 9e33e46912..ebe337caca 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,5 +1,6 @@
 gtk_tests = [
   # testname, optional extra sources
+  ['testaltstack', 'gtkdoubleselection.c'],
   ['rendernode'],
   ['rendernode-create-tests'],
   ['overlayscroll'],
diff --git a/tests/testaltstack.c b/tests/testaltstack.c
new file mode 100644
index 0000000000..3555b7def0
--- /dev/null
+++ b/tests/testaltstack.c
@@ -0,0 +1,107 @@
+#include <gtk/gtk.h>
+#include "gtkdoubleselection.h"
+
+static GtkSelectionModel *
+get_model (void)
+{
+  GListStore *store = g_list_store_new (GTK_TYPE_WIDGET);
+
+  g_list_store_append (store, gtk_label_new ("One"));
+  g_list_store_append (store, gtk_label_new ("Two"));
+  g_list_store_append (store, gtk_label_new ("Three"));
+  g_list_store_append (store, gtk_label_new ("Four"));
+  g_list_store_append (store, gtk_label_new ("Five"));
+
+  return GTK_SELECTION_MODEL (gtk_double_selection_new (G_LIST_MODEL (store)));
+}
+
+static void
+selection_changed_cb (GtkSelectionModel *model, guint position, guint n_items, gpointer data)
+{
+  guint i;
+  PangoAttrList *attrs;
+  PangoAttribute *attr;
+
+  attrs = pango_attr_list_new ();
+  attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
+  attr->start_index = 0;
+  attr->end_index = -1;
+  pango_attr_list_insert (attrs, attr);
+
+  for (i = position; i < position + n_items; i++)
+    {
+      GtkWidget *child = g_list_model_get_item (G_LIST_MODEL (model), i);
+      if (gtk_selection_model_is_selected (model, i))
+        gtk_label_set_attributes (GTK_LABEL (child), attrs);
+      else
+        gtk_label_set_attributes (GTK_LABEL (child), NULL);
+      g_object_unref (child);
+    }
+
+  pango_attr_list_unref (attrs);
+}
+
+static GtkWidget *
+get_view (GtkSelectionModel *model)
+{
+  GtkWidget *box;
+  guint i;
+
+  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
+  g_object_set (box, "margin", 10, NULL);
+  for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (model)); i++)
+    {
+      GtkWidget *child = g_list_model_get_item (G_LIST_MODEL (model), i);
+      gtk_container_add (GTK_CONTAINER (box), child);
+      g_object_unref (child);
+    }
+
+  selection_changed_cb (model, 0, g_list_model_get_n_items (G_LIST_MODEL (model)), NULL);
+  g_signal_connect (model, "selection-changed", G_CALLBACK (selection_changed_cb), NULL);
+  return box;
+}
+
+static void
+get_label_data (GObject *item,
+                gpointer user_data,
+                gboolean *visible,
+                char **title,
+                char **icon_name,
+                gboolean *needs_attention)
+{
+  GtkWidget *label = GTK_WIDGET (item);
+  *visible = gtk_widget_get_visible (label);
+  *title = g_strdup (gtk_label_get_label (GTK_LABEL (label)));
+  *icon_name = NULL;
+  *needs_attention = FALSE;
+}
+
+int main (int argc, char *argv[])
+{
+  GtkWidget *window, *box, *switcher1, *switcher2, *view;
+  GtkSelectionModel *model;
+
+  gtk_init ();
+
+  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+  switcher1 = gtk_stack_switcher_new ();
+  switcher2 = gtk_stack_switcher_new ();
+
+  model = get_model ();
+  view = get_view (model);
+
+  gtk_stack_switcher_set_model (GTK_STACK_SWITCHER (switcher1), model, get_label_data, NULL, NULL);
+  gtk_stack_switcher_set_model (GTK_STACK_SWITCHER (switcher2), model, get_label_data, NULL, NULL);
+
+  gtk_container_add (GTK_CONTAINER (window), box);
+  gtk_container_add (GTK_CONTAINER (box), switcher1);
+  gtk_container_add (GTK_CONTAINER (box), view);
+  gtk_container_add (GTK_CONTAINER (box), switcher2);
+
+  gtk_widget_show (window);
+
+  gtk_main ();
+
+  return 0; 
+}


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