[gnome-notes/136-implement-the-list-row-pattern-in-main-view: 13/17] controller: add (un)select item option



commit ea404d0b856c7e8e017a98b4471e4f613e087d36
Author: Isaque Galdino <igaldino gmail com>
Date:   Sun Jun 14 23:36:39 2020 -0300

    controller: add (un)select item option

 src/bjb-controller.c | 42 +++++++++++++++++++++++++++++++++++++-----
 src/bjb-controller.h |  6 ++++++
 2 files changed, 43 insertions(+), 5 deletions(-)
---
diff --git a/src/bjb-controller.c b/src/bjb-controller.c
index 2a9d2c9..de08f0b 100644
--- a/src/bjb-controller.c
+++ b/src/bjb-controller.c
@@ -909,6 +909,16 @@ bjb_controller_get_selection (BjbController *self)
   return g_list_reverse (retval);
 }
 
+static void
+bjb_controller_set_selection (GtkListStore *store,
+                              GtkTreeIter  *iter,
+                              gboolean      selection)
+{
+  gtk_list_store_set (store, iter,
+                      BJB_MODEL_COLUMN_SELECTED, selection,
+                      -1);
+}
+
 static gboolean
 bjb_controller_set_selection_foreach (GtkTreeModel *model,
                                       GtkTreePath  *path,
@@ -917,10 +927,8 @@ bjb_controller_set_selection_foreach (GtkTreeModel *model,
 {
   gboolean selection = GPOINTER_TO_INT (user_data);
 
-  gtk_list_store_set (GTK_LIST_STORE (model),
-                      iter,
-                      BJB_MODEL_COLUMN_SELECTED, selection,
-                      -1);
+  bjb_controller_set_selection (GTK_LIST_STORE (model), iter, selection);
+
   return FALSE;
 }
 
@@ -933,6 +941,26 @@ bjb_controller_set_all_selection (BjbController *self,
                           GINT_TO_POINTER (selection));
 }
 
+void
+bjb_controller_select_item (BjbController *self,
+                            const char    *iter_string)
+{
+  GtkTreeIter iter;
+  if (!gtk_tree_model_get_iter_from_string (self->model, &iter, iter_string))
+    return;
+  bjb_controller_set_selection (GTK_LIST_STORE (self->model), &iter, TRUE);
+}
+
+void
+bjb_controller_unselect_item (BjbController *self,
+                              const char    *iter_string)
+{
+  GtkTreeIter iter;
+  if (!gtk_tree_model_get_iter_from_string (self->model, &iter, iter_string))
+    return;
+  bjb_controller_set_selection (GTK_LIST_STORE (self->model), &iter, FALSE);
+}
+
 void
 bjb_controller_select_all (BjbController *self)
 {
@@ -955,5 +983,9 @@ void
 bjb_controller_set_selection_mode (BjbController *self,
                                    gboolean       selection_mode)
 {
-  self->selection_mode = selection_mode;
+  if (self->selection_mode != selection_mode)
+    {
+      self->selection_mode = selection_mode;
+      bjb_controller_unselect_all (self);
+    }
 }
diff --git a/src/bjb-controller.h b/src/bjb-controller.h
index 558cd06..d3015c2 100644
--- a/src/bjb-controller.h
+++ b/src/bjb-controller.h
@@ -80,6 +80,12 @@ void      bjb_controller_set_selection_mode (BjbController *self,
 
 GList    *bjb_controller_get_selection      (BjbController *self);
 
+void      bjb_controller_select_item        (BjbController *self,
+                                             const char    *iter_string);
+
+void      bjb_controller_unselect_item      (BjbController *self,
+                                             const char    *iter_string);
+
 void      bjb_controller_select_all         (BjbController *self);
 
 void      bjb_controller_unselect_all       (BjbController *self);


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