Basic model
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 26 Jan 2013 11:17:27 +0000 (12:17 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 16 Feb 2013 14:03:36 +0000 (15:03 +0100)
src/ui/gui/automake.mk
src/ui/gui/psppire-spreadsheet-model.c [new file with mode: 0644]
src/ui/gui/psppire-spreadsheet-model.h [new file with mode: 0644]
src/ui/gui/sheet-test.c

index 279db3111c448672e86c01de4054a6a449ca0f56..dafad5ed515da5475dc4b91c483127db2abce274 100644 (file)
@@ -93,7 +93,7 @@ src_ui_gui_spread_test_LDADD = \
        $(GTHREAD_LIBS)
 
 
-src_ui_gui_spread_test_SOURCES = src/ui/gui/sheet-test.c
+src_ui_gui_spread_test_SOURCES = src/ui/gui/sheet-test.c src/ui/gui/psppire-spreadsheet-model.c
 
 
 src_ui_gui_psppiredir = $(pkgdatadir)
diff --git a/src/ui/gui/psppire-spreadsheet-model.c b/src/ui/gui/psppire-spreadsheet-model.c
new file mode 100644 (file)
index 0000000..d5915a3
--- /dev/null
@@ -0,0 +1,248 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2013  Free Software Foundation
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+
+#include <config.h>
+#include <glib.h>
+
+#include "psppire-spreadsheet-model.h"
+
+static void psppire_spreadsheet_model_init           (PsppireSpreadsheetModel *spreadsheetModel);
+static void psppire_spreadsheet_model_class_init     (PsppireSpreadsheetModelClass *class);
+
+static void psppire_spreadsheet_model_finalize       (GObject   *object);
+static void psppire_spreadsheet_model_dispose        (GObject   *object);
+
+static GObjectClass *parent_class = NULL;
+
+
+static void spreadsheet_tree_model_init (GtkTreeModelIface *iface);
+
+
+GType
+psppire_spreadsheet_model_get_type (void)
+{
+  static GType object_type = 0;
+
+  if (!object_type)
+    {
+      static const GTypeInfo spreadsheet_model_info =
+      {
+       sizeof (PsppireSpreadsheetModelClass),
+       NULL,           /* base_init */
+       NULL,           /* base_finalize */
+        (GClassInitFunc) psppire_spreadsheet_model_class_init,
+       NULL,           /* class_finalize */
+       NULL,           /* class_data */
+        sizeof (PsppireSpreadsheetModel),
+       0,
+        (GInstanceInitFunc) psppire_spreadsheet_model_init,
+      };
+
+      static const GInterfaceInfo tree_model_info = {
+       (GInterfaceInitFunc) spreadsheet_tree_model_init,
+       NULL,
+       NULL
+      };
+      
+      object_type = g_type_register_static (G_TYPE_OBJECT,
+                                               "PsppireSpreadsheetModel",
+                                               &spreadsheet_model_info, 0);
+
+      g_type_add_interface_static (object_type, GTK_TYPE_TREE_MODEL,
+                                  &tree_model_info);
+
+    }
+
+  return object_type;
+}
+
+
+static void
+psppire_spreadsheet_model_dispose  (GObject *object)
+{
+}
+
+static void
+psppire_spreadsheet_model_finalize (GObject *object)
+{
+  //  PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (object);
+}
+
+static void
+psppire_spreadsheet_model_class_init (PsppireSpreadsheetModelClass *class)
+{
+  GObjectClass *object_class;
+
+  parent_class = g_type_class_peek_parent (class);
+  object_class = (GObjectClass*) class;
+
+  object_class->finalize = psppire_spreadsheet_model_finalize;
+  object_class->dispose = psppire_spreadsheet_model_dispose;
+}
+
+
+static void
+psppire_spreadsheet_model_init (PsppireSpreadsheetModel *spreadsheetModel)
+{
+  spreadsheetModel->dispose_has_run = FALSE;
+  spreadsheetModel->stamp = g_random_int ();
+}
+
+
+GtkTreeModel*
+psppire_spreadsheet_model_new (void)
+{
+  return g_object_new (psppire_spreadsheet_model_get_type (), NULL);
+}
+
+
+
+
+\f
+
+static const gint N_COLS = 2;
+
+static gint 
+tree_model_n_columns (GtkTreeModel *model)
+{
+  g_print ("%s\n", __FUNCTION__);
+  return N_COLS;
+}
+
+static GtkTreeModelFlags
+tree_model_get_flags (GtkTreeModel *model)
+{
+  g_print ("%s\n", __FUNCTION__);
+  g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model), (GtkTreeModelFlags) 0);
+
+  return GTK_TREE_MODEL_LIST_ONLY;
+}
+
+static GType
+tree_model_column_type (GtkTreeModel *model, gint index)
+{
+  g_print ("%s %d\n", __FUNCTION__, index);
+  g_return_val_if_fail (PSPPIRE_IS_SPREADSHEET_MODEL (model), (GType) 0);
+  g_return_val_if_fail (index < N_COLS, (GType) 0);
+  return G_TYPE_STRING;
+}
+
+
+static gboolean
+tree_model_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GtkTreePath *path)
+{
+  gint *indices, depth;
+
+  PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
+
+  g_return_val_if_fail (path, FALSE);
+
+  indices = gtk_tree_path_get_indices (path);
+
+  depth = gtk_tree_path_get_depth (path);
+
+  g_return_val_if_fail (depth == 1, FALSE);
+
+  g_print ("%s %d\n", __FUNCTION__, *indices);
+
+  iter->stamp = spreadsheetModel->stamp;
+  iter->user_data = *indices; // kludge
+
+  return TRUE;
+}
+
+static gboolean
+tree_model_iter_next (GtkTreeModel *model, GtkTreeIter *iter)
+{
+  PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
+  g_return_val_if_fail (iter->stamp == spreadsheetModel->stamp, FALSE);
+
+  g_print ("%s %d\n", __FUNCTION__, iter->user_data);
+
+  if ( iter->user_data >= 5 - 1)
+    return FALSE;
+
+  iter->user_data++;
+
+  return TRUE;
+}
+
+
+static void
+tree_model_get_value (GtkTreeModel *model, GtkTreeIter *iter,
+                     gint column, GValue *value)
+{
+  PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
+  g_return_if_fail (column < N_COLS);
+  g_return_if_fail (iter->stamp == spreadsheetModel->stamp);
+  g_print ("%s col %d\n", __FUNCTION__, column);
+
+  g_value_init (value, G_TYPE_STRING);
+  if ( column > 0)
+    g_value_set_string (value, "foo");
+  else
+    g_value_set_string (value, "bar");
+}
+
+
+static gboolean
+tree_model_nth_child (GtkTreeModel *model, GtkTreeIter *iter,
+                     GtkTreeIter *parent, gint n)
+{
+  PsppireSpreadsheetModel *spreadsheetModel = PSPPIRE_SPREADSHEET_MODEL (model);
+
+  if ( parent )
+    return FALSE;
+
+  if ( n >= 5)
+    return FALSE;
+
+  iter->stamp = spreadsheetModel->stamp;
+  /*
+  iter->user_data = psppire_dict_get_variable (dict, n);
+
+  if ( !iter->user_data)
+    return FALSE;
+  */
+
+  return TRUE;
+}
+
+
+static void
+spreadsheet_tree_model_init (GtkTreeModelIface *iface)
+{
+  iface->get_flags = tree_model_get_flags;
+  iface->get_n_columns = tree_model_n_columns;
+  iface->get_column_type = tree_model_column_type;
+  iface->get_iter = tree_model_get_iter;
+  iface->iter_next = tree_model_iter_next;
+
+  iface->get_value = tree_model_get_value;
+
+#if 0
+  iface->get_path = tree_model_get_path;
+  iface->iter_children = tree_model_iter_children ;
+  iface->iter_has_child = tree_model_iter_has_child ;
+  iface->iter_n_children = tree_model_n_children ;
+
+  iface->iter_parent = tree_model_iter_parent ;
+#endif
+
+  iface->iter_nth_child = tree_model_nth_child ;
+}
diff --git a/src/ui/gui/psppire-spreadsheet-model.h b/src/ui/gui/psppire-spreadsheet-model.h
new file mode 100644 (file)
index 0000000..5956f32
--- /dev/null
@@ -0,0 +1,81 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2013  Free Software Foundation
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+
+#include <glib-object.h>
+#include <glib.h>
+
+#include <gtk/gtk.h>
+
+#ifndef __PSPPIRE_SPREADSHEET_MODEL_H__
+#define __PSPPIRE_SPREADSHEET_MODEL_H__
+
+G_BEGIN_DECLS
+
+
+#define PSPPIRE_TYPE_SPREADSHEET_MODEL (psppire_spreadsheet_model_get_type ())
+
+#define PSPPIRE_SPREADSHEET_MODEL(obj) \
+                     (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+                   PSPPIRE_TYPE_SPREADSHEET_MODEL, PsppireSpreadsheetModel))
+
+#define PSPPIRE_SPREADSHEET_MODEL_CLASS(klass) \
+                     (G_TYPE_CHECK_CLASS_CAST ((klass), \
+                                PSPPIRE_TYPE_SPREADSHEET_MODEL, \
+                                 PsppireSpreadsheetModelClass))
+
+
+#define PSPPIRE_IS_SPREADSHEET_MODEL(obj) \
+                    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PSPPIRE_TYPE_SPREADSHEET_MODEL))
+
+#define PSPPIRE_IS_SPREADSHEET_MODEL_CLASS(klass) \
+                     (G_TYPE_CHECK_CLASS_TYPE ((klass), PSPPIRE_TYPE_SPREADSHEET_MODEL))
+
+
+#define PSPPIRE_SPREADSHEET_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+                                  PSPPIRE_TYPE_SPREADSHEET_MODEL, \
+                                  PsppireSpreadsheetModelClass))
+
+typedef struct _PsppireSpreadsheetModel       PsppireSpreadsheetModel;
+typedef struct _PsppireSpreadsheetModelClass  PsppireSpreadsheetModelClass;
+
+
+struct _PsppireSpreadsheetModel
+{
+  GObject parent;
+
+
+  /*< private >*/
+  gint stamp;
+
+  gboolean dispose_has_run ;
+};
+
+
+struct _PsppireSpreadsheetModelClass
+{
+  GObjectClass parent_class;
+};
+
+
+GType psppire_spreadsheet_model_get_type (void) G_GNUC_CONST;
+
+GtkTreeModel * psppire_spreadsheet_model_new (void);
+
+
+G_END_DECLS
+
+#endif /* __PSPPIRE_SPREADSHEET_MODEL_H__ */
index 3898fef677012f7f6a2188a6ca6a6d65ffc596dc..45f1061a0628074b4a8ebeb0480179e4c5ece143 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <gtk/gtk.h>
 
+#include "psppire-spreadsheet-model.h"
 
 #define N 10
 
@@ -15,7 +16,6 @@ make_store ()
     
     GtkListStore * list_store  = gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING);
 
-
     for (i = 0; i < N; ++i)
       {
        gtk_list_store_append (list_store, &iter);
@@ -28,22 +28,23 @@ make_store ()
   }
 
 
+
 int
 main (int argc, char *argv[] )
 {
-
-  /* GtkWidget is the storage type for widgets */
   GtkWidget *window;
   GtkWidget *treeview;
-    
+  GtkTreeModel *tm;
   gtk_init (&argc, &argv);
     
+
+  tm = psppire_spreadsheet_model_new ();
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     
   gtk_container_set_border_width (GTK_CONTAINER (window), 10);
 
-
-  treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (make_store ()));
+  
+  treeview = gtk_tree_view_new_with_model (tm);
 
   gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
                                               0, "sheet name",