[gnome-documents/wip/ui-changes: 4/16] main-view: track selections with a model column



commit 263c37ba86bcc9b940f11cde0cb2e1115d97c79a
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Feb 27 19:32:06 2012 -0500

    main-view: track selections with a model column
    
    We want to render selections with a toggle cell renderer, so it's
    comfortable to have this in the model.

 src/lib/gd-main-view-generic.c |    4 +-
 src/lib/gd-main-view-generic.h |    4 +-
 src/lib/gd-main-view.c         |   60 +++++++++++++++++++++++++++++++++++----
 src/lib/gd-main-view.h         |    5 ++-
 src/lib/gd-utils.c             |    5 ++-
 5 files changed, 64 insertions(+), 14 deletions(-)
---
diff --git a/src/lib/gd-main-view-generic.c b/src/lib/gd-main-view-generic.c
index 254dd51..7496a0e 100644
--- a/src/lib/gd-main-view-generic.c
+++ b/src/lib/gd-main-view-generic.c
@@ -90,13 +90,13 @@ gd_main_view_generic_get_path_at_pos (GdMainViewGeneric *self,
 
 void
 gd_main_view_generic_set_selection_mode (GdMainViewGeneric *self,
-                                         GtkSelectionMode mode)
+                                         gboolean selection_mode)
 {
   GdMainViewGenericInterface *iface;
 
   iface = GD_MAIN_VIEW_GENERIC_GET_IFACE (self);
 
-  (* iface->set_selection_mode) (self, mode);
+  (* iface->set_selection_mode) (self, selection_mode);
 }
 
 void
diff --git a/src/lib/gd-main-view-generic.h b/src/lib/gd-main-view-generic.h
index 4a13e2c..89c5e3a 100644
--- a/src/lib/gd-main-view-generic.h
+++ b/src/lib/gd-main-view-generic.h
@@ -70,7 +70,7 @@ struct _GdMainViewGenericIface
   void          (* scroll_to_path)       (GdMainViewGeneric *view,
                                           GtkTreePath       *path);
   void          (* set_selection_mode)   (GdMainViewGeneric *view,
-                                          GtkSelectionMode   selection_mode);
+                                          gboolean           selection_mode);
   GList *       (* get_selection)        (GdMainViewGeneric *view);
   gboolean      (* path_is_selected)     (GdMainViewGeneric *view,
                                           GtkTreePath       *path);
@@ -88,7 +88,7 @@ void gd_main_view_generic_set_model (GdMainViewGeneric *self,
 void gd_main_view_generic_scroll_to_path (GdMainViewGeneric *self,
                                           GtkTreePath *path);
 void gd_main_view_generic_set_selection_mode (GdMainViewGeneric *self,
-                                              GtkSelectionMode mode);
+                                              gboolean selection_mode);
 GtkTreePath * gd_main_view_generic_get_path_at_pos (GdMainViewGeneric *self,
                                                     gint x,
                                                     gint y);
diff --git a/src/lib/gd-main-view.c b/src/lib/gd-main-view.c
index 0184ded..1ebd72b 100644
--- a/src/lib/gd-main-view.c
+++ b/src/lib/gd-main-view.c
@@ -222,13 +222,25 @@ on_button_release_selection_mode (GdMainView *self,
 {
   GdMainViewGeneric *generic = get_generic (self);
   gboolean selected;
+  GtkTreeIter iter;
 
-  selected = gd_main_view_generic_path_is_selected (generic, path);
+  if (!gtk_tree_model_get_iter (self->priv->model, &iter, path))
+    return FALSE;
+
+  gtk_tree_model_get (self->priv->model, &iter,
+                      GD_MAIN_COLUMN_SELECTED, &selected,
+                      -1);
 
   if (selected && !entered_mode)
-    gd_main_view_generic_unselect_path (generic, path);
+    gtk_list_store_set (GTK_LIST_STORE (self->priv->model), &iter,
+                        GD_MAIN_COLUMN_SELECTED, FALSE,
+                        -1);
   else if (!selected)
-    gd_main_view_generic_select_path (generic, path);
+    gtk_list_store_set (GTK_LIST_STORE (self->priv->model), &iter,
+                        GD_MAIN_COLUMN_SELECTED, TRUE,
+                        -1);
+
+  g_signal_emit_by_name (generic, "view-selection-changed");
 
   return TRUE;
 }
@@ -361,10 +373,7 @@ gd_main_view_apply_selection_mode (GdMainView *self)
 {
   GdMainViewGeneric *generic = get_generic (self);
 
-  if (self->priv->selection_mode)
-    gd_main_view_generic_set_selection_mode (generic, GTK_SELECTION_MULTIPLE);
-  else
-    gd_main_view_generic_set_selection_mode (generic, GTK_SELECTION_NONE);
+  gd_main_view_generic_set_selection_mode (generic, self->priv->selection_mode);
 }
 
 static void
@@ -488,3 +497,40 @@ gd_main_view_get_generic_view (GdMainView *self)
 {
   return self->priv->current_view;
 }
+
+static gboolean
+build_selection_list_foreach (GtkTreeModel *model,
+                              GtkTreePath *path,
+                              GtkTreeIter *iter,
+                              gpointer user_data)
+{
+  GList **sel = user_data;
+  gboolean is_selected;
+
+  gtk_tree_model_get (model, iter,
+                      GD_MAIN_COLUMN_SELECTED, &is_selected,
+                      -1);
+
+  if (is_selected)
+    *sel = g_list_prepend (*sel, gtk_tree_path_copy (path));
+
+  return FALSE;
+}
+
+/**
+ * gd_main_view_get_selection:
+ * @self:
+ *
+ * Returns: (element-type GtkTreePath) (transfer full):
+ */
+GList *
+gd_main_view_get_selection (GdMainView *self)
+{
+  GList *retval = NULL;
+
+  gtk_tree_model_foreach (self->priv->model,
+                          build_selection_list_foreach,
+                          &retval);
+
+  return g_list_reverse (retval);
+}
diff --git a/src/lib/gd-main-view.h b/src/lib/gd-main-view.h
index 4e814a8..641db26 100644
--- a/src/lib/gd-main-view.h
+++ b/src/lib/gd-main-view.h
@@ -59,7 +59,8 @@ typedef enum {
   GD_MAIN_COLUMN_TITLE,
   GD_MAIN_COLUMN_AUTHOR,
   GD_MAIN_COLUMN_ICON,
-  GD_MAIN_COLUMN_MTIME
+  GD_MAIN_COLUMN_MTIME,
+  GD_MAIN_COLUMN_SELECTED
 } GdMainColumns;
 
 typedef enum {
@@ -88,6 +89,8 @@ void gd_main_view_set_selection_mode (GdMainView *self,
                                       gboolean selection_mode);
 GdMainViewType gd_main_view_get_selection_mode (GdMainView *self);
 
+GList * gd_main_view_get_selection (GdMainView *self);
+
 GtkTreeModel * gd_main_view_get_model (GdMainView *self);
 void gd_main_view_set_model (GdMainView *self,
                              GtkTreeModel *model);
diff --git a/src/lib/gd-utils.c b/src/lib/gd-utils.c
index 971d52d..4d97316 100644
--- a/src/lib/gd-utils.c
+++ b/src/lib/gd-utils.c
@@ -39,12 +39,13 @@
 GtkListStore *
 gd_create_list_store (void)
 {
-  return gtk_list_store_new (5,
+  return gtk_list_store_new (6,
                              G_TYPE_STRING, // URN
                              G_TYPE_STRING, // TITLE
                              G_TYPE_STRING, // AUTHOR
                              GDK_TYPE_PIXBUF, // ICON
-                             G_TYPE_LONG); // MTIME
+                             G_TYPE_LONG, // MTIME
+                             G_TYPE_BOOLEAN); // SELECTED
 }
 
 void



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