[Gnucash-changes] r13271 - gnucash/trunk/src/gnome-utils - Convert the AccountType selection functions to work with GtkTreeSelections

Chris Shoemaker chris at cvs.gnucash.org
Wed Feb 15 17:06:40 EST 2006


Author: chris
Date: 2006-02-15 17:06:37 -0500 (Wed, 15 Feb 2006)
New Revision: 13271
Trac: http://svn.gnucash.org/trac/changeset/13271

Modified:
   gnucash/trunk/src/gnome-utils/dialog-account.c
   gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.c
   gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.h
Log:
   Convert the AccountType selection functions to work with GtkTreeSelections
     from views using the filtered GncTreeModelAccountTypes.
   Privatize the unfiltered GncTreeModelAccountTypes - now, every accessible
     account-types TreeModel is a new GtkTreeModelFilter, and their 
     ref-counting is always the same.
   Add some tweaks to gnc-tree-model-account-types.[ch] from 
     Andreas Köhler <andi5.py at gmx.net>, plus the conversion of the Account 
     Dialog's account-types field to use the filtered GncTreeModelAccountTypes
     instead of a GtkListStore, but simplified to use the AccountType 
     selection functions.


Modified: gnucash/trunk/src/gnome-utils/dialog-account.c
===================================================================
--- gnucash/trunk/src/gnome-utils/dialog-account.c	2006-02-15 16:42:29 UTC (rev 13270)
+++ gnucash/trunk/src/gnome-utils/dialog-account.c	2006-02-15 22:06:37 UTC (rev 13271)
@@ -41,6 +41,7 @@
 #include "gnc-engine.h"
 #include "gnc-gui-query.h"
 #include "gnc-session.h"
+#include "gnc-tree-model-account-types.h"
 #include "gnc-tree-view-account.h"
 #include "gnc-ui.h"
 #include "gnc-ui-util.h"
@@ -56,13 +57,6 @@
   EDIT_ACCOUNT
 } AccountDialogType;
 
-typedef enum
-{
-  COL_TYPE_TEXT,
-  COL_TYPE_ID,
-  NUM_TYPE_COLUMNS
-} AccountTypeColumns;
-
 typedef struct _AccountWindow
 {
   gboolean modal;
@@ -1087,22 +1081,19 @@
 gnc_account_type_changed_cb (GtkTreeSelection *selection, gpointer data)
 {
   AccountWindow *aw = data;
-  GtkTreeModel *model;
-  GtkTreeIter iter;
   gboolean sensitive;
-  guint type_id;
+  GNCAccountType type_id;
 
   g_return_if_fail (aw != NULL);
 
   sensitive = FALSE;
 
-  if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
+  type_id = gnc_tree_model_account_types_get_selection_single(selection);
+  if (type_id == NO_TYPE) {
     aw->type = BAD_TYPE;
   } else {
-
-    gtk_tree_model_get (model, &iter, COL_TYPE_ID, &type_id, -1);
     aw->type = type_id;
-    last_used_account_type = aw->type;
+    last_used_account_type = type_id;
 
     gnc_account_commodity_from_type (aw, TRUE);
 
@@ -1120,36 +1111,6 @@
   }
 }
 
-static void
-gnc_account_type_store_fill (GtkListStore *store, GList *types)
-{
-  GtkTreeIter iter;
-  gint acct_type;
-  gchar *text;
-
-  gtk_list_store_clear (store);
-
-  if (types == NULL) {
-    for (acct_type = 0; acct_type < NUM_ACCOUNT_TYPES; acct_type++) {
-      text = (gchar *) xaccAccountGetTypeStr (acct_type);
-      gtk_list_store_append (store, &iter);
-      gtk_list_store_set (store, &iter,
-			  COL_TYPE_TEXT, text,
-			  COL_TYPE_ID, GINT_TO_POINTER(acct_type),
-			  -1);
-    }
-  } else {
-    for ( ; types != NULL; types = types->next ) {
-      text = (gchar *) xaccAccountGetTypeStr ((GNCAccountType) (types->data));
-      gtk_list_store_append (store, &iter);
-      gtk_list_store_set (store, &iter,
-			  COL_TYPE_TEXT, text,
-			  COL_TYPE_ID, types->data,
-			  -1);
-    }
-  }
-}
-
 static GNCAccountType
 gnc_account_choose_new_acct_type (AccountWindow *aw)
 {
@@ -1163,40 +1124,16 @@
   return ((GNCAccountType)(aw->valid_types->data));
 }
 
-static gboolean
-gnc_account_type_get_iter (GtkTreeModel *model, GtkTreeIter *iter, GNCAccountType type)
-{
-  guint type_id;
-
-  gtk_tree_model_get_iter_first (model, iter);
-
-  do {
-    gtk_tree_model_get (model, iter, COL_TYPE_ID, &type_id, -1);
-    if (type == type_id)
-      return TRUE;
-  } while (gtk_tree_model_iter_next (model, iter));
-
-  return FALSE;
-}
-
 static void
 gnc_account_type_view_create (AccountWindow *aw)
 {
   GtkTreeModel *model;
   GtkTreeSelection *selection;
-  GtkTreeIter iter;
-  GtkTreePath *path;
   GtkCellRenderer *renderer;
   GtkTreeView *view;
+  GList *list;
+  guint32 types = 0;
 
-  model = GTK_TREE_MODEL (gtk_list_store_new (NUM_TYPE_COLUMNS,
-					      G_TYPE_STRING,
-					      G_TYPE_UINT));
-  gnc_account_type_store_fill (GTK_LIST_STORE (model), aw->valid_types);
-
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
-					COL_TYPE_TEXT, GTK_SORT_ASCENDING);
-
   switch (aw->dialog_type) {
     case NEW_ACCOUNT:
       aw->type = gnc_account_choose_new_acct_type (aw);
@@ -1206,24 +1143,29 @@
       break;
   }
 
+  if (aw->valid_types == NULL)
+      types = xaccAccountTypesValid () | (1 << aw->type);
+  else
+      for (list = aw->valid_types; list; list = list->next)
+          types |= (1 << (guint) list->data);
+
+  model = gnc_tree_model_account_types_filter_using_mask (types);
+
   view = GTK_TREE_VIEW (aw->type_view);
   gtk_tree_view_set_model (view, model);
+  g_object_unref (G_OBJECT (model));
+
   renderer = gtk_cell_renderer_text_new ();
-  gtk_tree_view_insert_column_with_attributes (view, -1, NULL,
-					       renderer, "text", COL_TYPE_TEXT,
-					       NULL);
+  gtk_tree_view_insert_column_with_attributes (
+    view, -1, NULL, renderer,
+    "text", GNC_TREE_MODEL_ACCOUNT_TYPES_COL_NAME,
+    NULL);
 
   selection = gtk_tree_view_get_selection (view);
   g_signal_connect (G_OBJECT (selection), "changed",
 		    G_CALLBACK (gnc_account_type_changed_cb), aw);
-
-  if (gnc_account_type_get_iter (model, &iter, aw->type)) {
-    gtk_tree_selection_select_iter (selection, &iter);
-
-    path = gtk_tree_model_get_path (model, &iter);
-    gtk_tree_view_scroll_to_cell (view, path, NULL, FALSE, 0.0, 0.0);
-    gtk_tree_path_free (path);
-  }
+  
+  gnc_tree_model_account_types_set_selection(selection, 1 << aw->type);
 }
 
 static void

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.c	2006-02-15 16:42:29 UTC (rev 13270)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.c	2006-02-15 22:06:37 UTC (rev 13271)
@@ -33,6 +33,7 @@
 #include "Account.h"
 
 static QofLogModule log_module = GNC_MOD_GUI;
+static GtkTreeModel *account_types_tree_model = NULL;
 
 /* Functions for the type system */
 static void
@@ -142,9 +143,7 @@
     return GTK_TREE_MODEL (model);
 }
 
-static GtkTreeModel *account_types_tree_model = NULL;
-
-GtkTreeModel *
+static GtkTreeModel *
 gnc_tree_model_account_types_master(void)
 {
     if (!account_types_tree_model)
@@ -199,7 +198,7 @@
 
 void
 gnc_tree_model_account_types_set_selected (GncTreeModelAccountTypes * model,
-					   guint32 selected)
+                                           guint32 selected)
 {
 	GncTreeModelAccountTypesPrivate *priv;
 
@@ -210,26 +209,28 @@
 }
 
 guint32
-gnc_tree_model_account_types_get_selection (GtkTreeView *view)
+gnc_tree_model_account_types_get_selection (GtkTreeSelection *sel)
 {
-    GtkTreeModel *model;
+    GtkTreeModel *f_model, *model;
     GtkTreePath *path;
-    GList *list, *iter;
-    GtkTreeSelection *sel;
+    GtkTreeView *view;
+    GList *list, *node;
     guint32 bits = 0;
 
-    g_return_val_if_fail(GTK_IS_TREE_VIEW(view), 0);
-    sel = gtk_tree_view_get_selection(view);
-    g_return_val_if_fail (sel, 0);
+    g_return_val_if_fail(GTK_IS_TREE_SELECTION(sel), 0);
+    view = gtk_tree_selection_get_tree_view(sel);
+    g_return_val_if_fail (view, 0);
 
-    list = gtk_tree_selection_get_selected_rows(sel, &model);
+    list = gtk_tree_selection_get_selected_rows(sel, &f_model);
+    model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
     if (model != account_types_tree_model)
        PERR("TreeSelection's TreeModel is not the account-types Model");
     else {
-        for (iter = list; iter; iter = iter->next) {
-            path = (GtkTreePath *)iter->data;
-            if (gtk_tree_path_get_depth(path) != 1) {
-                PERR("Account-types TreePath depth != 1 ?!?");
+        for (node = list; node; node = node->next) {
+            path = gtk_tree_model_filter_convert_path_to_child_path(
+                GTK_TREE_MODEL_FILTER(f_model), (GtkTreePath*)node->data);
+            if (!path || gtk_tree_path_get_depth(path) != 1) {
+                PERR("Invalid Account-types TreePath.");
                 continue;
             }
             bits |= (1 << gtk_tree_path_get_indices(path)[0]);
@@ -242,28 +243,44 @@
     return bits;
 }
 
+GNCAccountType
+gnc_tree_model_account_types_get_selection_single(GtkTreeSelection *sel)
+{
+    gint i;
+    guint32 selected = gnc_tree_model_account_types_get_selection(sel);
+    
+    for (i = 0; i < NUM_ACCOUNT_TYPES; i++)
+        if (selected & (1 << i))
+            return i;
+    return NO_TYPE;
+}
+
 void
-gnc_tree_model_account_types_set_selection (GtkTreeView *view,
+gnc_tree_model_account_types_set_selection (GtkTreeSelection *sel,
                                             guint32 selected)
 {
-    GtkTreePath *path;
-    gint *path_idx;
-    guint i;
-    GtkTreeSelection *sel;
+    GtkTreePath *path, *f_path;
+    GtkTreeModelFilter *f_model;
+    gint i;
+    GtkTreeView *view;
 
-
-    g_return_if_fail(GTK_IS_TREE_VIEW(view));
-    sel = gtk_tree_view_get_selection(view);
-    g_return_if_fail (sel);
+    g_return_if_fail(GTK_IS_TREE_SELECTION(sel));
+    view = gtk_tree_selection_get_tree_view(sel);
+    g_return_if_fail (view);
+    f_model = GTK_TREE_MODEL_FILTER(gtk_tree_view_get_model(view));
+    g_return_if_fail(gtk_tree_model_filter_get_model(f_model) == 
+                     account_types_tree_model);
     gtk_tree_selection_unselect_all(sel);
     path = gtk_tree_path_new_first();
-    path_idx = gtk_tree_path_get_indices(path);
 
     for (i = 0; i < NUM_ACCOUNT_TYPES; i++) {
         if (selected & (1 << i)) {
-            path_idx[0] = i;
-            gtk_tree_selection_select_path(sel, path);
+            f_path = gtk_tree_model_filter_convert_child_path_to_path(
+                f_model, path);
+            gtk_tree_selection_select_path(sel, f_path);
+            gtk_tree_view_scroll_to_cell(view, f_path, NULL, FALSE, 0.0, 0.0);
         }
+        gtk_tree_path_next(path);
     }
     gtk_tree_path_free(path);
 }
@@ -271,10 +288,10 @@
 
 /* Static functions implementing GtkTreeModel */
 
-static guint
+static GtkTreeModelFlags
 gnc_tree_model_account_types_get_flags (GtkTreeModel * tree_model)
 {
-    return 0;
+    return GTK_TREE_MODEL_ITERS_PERSIST | GTK_TREE_MODEL_LIST_ONLY;
 }
 
 static int
@@ -294,7 +311,7 @@
 
     switch (index) {
     case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_TYPE:
-        return G_TYPE_UINT;
+        return G_TYPE_INT;
     case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_NAME:
         return G_TYPE_STRING;
     case GNC_TREE_MODEL_ACCOUNT_TYPES_COL_SELECTED:
@@ -347,7 +364,7 @@
 
 static void
 gnc_tree_model_account_types_get_value (GtkTreeModel * tree_model,
-					GtkTreeIter * iter, int column,
+                                        GtkTreeIter * iter, int column,
                                         GValue * value)
 {
     GncTreeModelAccountTypes *model = GNC_TREE_MODEL_ACCOUNT_TYPES(tree_model);
@@ -400,7 +417,7 @@
 
 static gboolean
 gnc_tree_model_account_types_iter_children (GtkTreeModel * tree_model,
-					    GtkTreeIter * iter,
+                                            GtkTreeIter * iter,
                                             GtkTreeIter * parent)
 {
 
@@ -439,7 +456,7 @@
 
 static gboolean
 gnc_tree_model_account_types_iter_nth_child (GtkTreeModel * tree_model,
-					     GtkTreeIter * iter,
+                                             GtkTreeIter * iter,
                                              GtkTreeIter * parent, int n)
 {
     GncTreeModelAccountTypes *model;

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.h
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.h	2006-02-15 16:42:29 UTC (rev 13270)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-model-account-types.h	2006-02-15 22:06:37 UTC (rev 13271)
@@ -38,6 +38,8 @@
 #ifndef __GNC_TREE_MODEL_ACCOUNT_TYPES_H
 #define __GNC_TREE_MODEL_ACCOUNT_TYPES_H
 
+#include "Account.h"
+
 G_BEGIN_DECLS
 
 /* type macros */
@@ -92,32 +94,42 @@
 
 /*************** Method 1 functions ***************/
 
-/* Get the static GtkTreeModel representing the list of all possible
-   account types.  You may not modify this model, but you can use if
-   for multiple views.  You probably want gnc_tree_model_types_valid(). */
-GtkTreeModel * gnc_tree_model_account_types_master(void);
-
 /* Returns a GtkTreeModelFilter that wraps the model. Deprecated
-   account types will be filtered. Use this instead of
-   gnc_tree_model_account_types_master. Caller is responsible for
+   account types will be filtered. Caller is responsible for
    ref/unref. */
 GtkTreeModel * gnc_tree_model_account_types_valid (void);
 
 /* Returns a GtkTreeModelFilter that wraps the model. Only account
    types specified by the 'types' bitmask are visible.  To force the
    visibility of deprecated account types, pass
-   (xaccAccountTypesValid() & (1 << MY_DEPRECATED_ACCOUNT_TYPE)). 
+   (xaccAccountTypesValid() | (1 << xaccAccountGetType(acct))). 
 
+   To get the GtkTreeModel that shows all account types, including
+   deprecated account types, pass (-1).
+
+   To get the GtkTreeModel that only shows non-deprecated account types, 
+   use gnc_tree_model_account_types_valid().
+
    Caller is responsible for ref/unref. */
 GtkTreeModel * gnc_tree_model_account_types_filter_using_mask (guint32 types);
 
 /* Return the bitmask of the account type enums reflecting the state
-   of the tree selection */
-guint32 gnc_tree_model_account_types_get_selection(GtkTreeView *view);
+   of the tree selection.  If your view allows the selection of
+   multiple account types, use must use this function to get the
+   selection. */
+guint32 gnc_tree_model_account_types_get_selection(GtkTreeSelection *sel);
 
+/* Gets the selected account type.  Use the function if your view
+   allows the selection of only one account type. If no types are
+   selected, returns NO_TYPE.  If more than one type is selected,
+   arbitrarily returns one of the selected types. */
+GNCAccountType
+gnc_tree_model_account_types_get_selection_single(GtkTreeSelection *sel);
+
 /* Set the selection state of the tree selection to match the bitmask
-   of account-type enums in 'selected' */
-void gnc_tree_model_account_types_set_selection(GtkTreeView *view,
+   of account-type enums in 'selected'.  This will also scroll to a
+   selected row in the TreeView.*/
+void gnc_tree_model_account_types_set_selection(GtkTreeSelection *sel,
                                                 guint32 selected);
 
 



More information about the gnucash-changes mailing list