[bijiben] mainToolbar : make some buttons insensitive when empty view



commit 2c472827cf5adf4cfef7da5c56f85a8a40e35688
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Sat Mar 16 20:55:01 2013 +0100

    mainToolbar : make some buttons insensitive when empty view
    
    If no note is displayed, no need to allow to go to selection mode
    or to change view mode

 src/bjb-controller.c   |   48 +++++++++++++++++++++++++++++++++++++-----------
 src/bjb-controller.h   |    9 +++++----
 src/bjb-main-toolbar.c |   37 ++++++++++++++++++++++++++++++++++---
 3 files changed, 76 insertions(+), 18 deletions(-)
---
diff --git a/src/bjb-controller.c b/src/bjb-controller.c
index 52527b9..a521b2b 100644
--- a/src/bjb-controller.c
+++ b/src/bjb-controller.c
@@ -1,6 +1,6 @@
 /*
  * bjb-controller.c
- * Copyright (C) Pierre-Yves Luyten 2012 <py luyten fr>
+ * Copyright (C) Pierre-Yves Luyten 2012, 2013 <py luyten fr>
  * 
  * bijiben is free software: you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -58,10 +58,10 @@ enum {
 
 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
 
-/* The Controller wants to inform the toolbar when search starts.
- * But other might be interested to know. */
+/* Currently used by toolbars */
 enum {
   SEARCH_CHANGED,
+  DISPLAY_NOTES_CHANGED, // either search or book change
   BJB_CONTROLLER_SIGNALS
 };
 
@@ -231,6 +231,8 @@ bjb_controller_add_note (BjbController *self,
 
     g_free (path);
   }
+
+
 }
 
 /* If the user searches for notes, is the note searched? */
@@ -326,8 +328,10 @@ sort_notes( BjbController *self)
 static void
 sort_and_update (BjbController *self)
 {
-  sort_notes (self) ;
+  sort_notes (self);
   bjb_controller_update_view (self);
+
+  g_signal_emit (G_OBJECT (self), bjb_controller_signals[DISPLAY_NOTES_CHANGED],0);
 }
 
 static void
@@ -341,22 +345,23 @@ update_controller_callback (GObject *source_object,
   result = biji_get_notes_with_strings_or_collection_finish (source_object, res, self->priv->book);
   self->priv->notes_to_show = result;
   sort_and_update (self);
-}
+}          
 
 void
-bjb_controller_apply_needle ( BjbController *self )
+bjb_controller_apply_needle (BjbController *self)
 {
-  gchar *needle ;
+  BjbControllerPrivate *priv = self->priv;
+  gchar *needle;
 
-  if (self->priv->notes_to_show)
-    g_clear_pointer (&self->priv->notes_to_show, g_list_free);
+  if (priv->notes_to_show)
+    g_clear_pointer (&priv->notes_to_show, g_list_free);
   
-  needle = self->priv->needle;
+  needle = priv->needle;
 
   /* Show all notes */
   if (needle == NULL || g_strcmp0 (needle,"") == 0)
   {
-    self->priv->notes_to_show = biji_note_book_get_notes (self->priv->book);
+    priv->notes_to_show = biji_note_book_get_notes (self->priv->book);
     sort_and_update (self);
     return;
   }
@@ -451,6 +456,7 @@ on_book_changed (BijiNoteBook           *book,
     case BIJI_BOOK_NOTE_ADDED:
         bjb_controller_add_note_if_needed (self, note, TRUE);
         priv->notes_to_show = g_list_prepend (priv->notes_to_show, note);
+        g_signal_emit (G_OBJECT (self), bjb_controller_signals[DISPLAY_NOTES_CHANGED],0);
       break;
 
     /* If the note is *amended*, then per definition we prepend.
@@ -474,6 +480,9 @@ on_book_changed (BijiNoteBook           *book,
     case BIJI_BOOK_NOTE_TRASHED:
       if (bjb_controller_get_iter_at_note (self, note, &p_iter))
         gtk_list_store_remove (GTK_LIST_STORE (priv->model), p_iter);
+
+      priv->notes_to_show = g_list_remove (priv->notes_to_show, note);
+      g_signal_emit (G_OBJECT (self), bjb_controller_signals[DISPLAY_NOTES_CHANGED],0);
       break;
 
     default:
@@ -534,6 +543,16 @@ bjb_controller_class_init (BjbControllerClass *klass)
                                                   G_TYPE_NONE,
                                                   0);
 
+  bjb_controller_signals[DISPLAY_NOTES_CHANGED] = g_signal_new ( "display-notes-changed" ,
+                                                  G_OBJECT_CLASS_TYPE (klass),
+                                                  G_SIGNAL_RUN_LAST,
+                                                  0, 
+                                                  NULL, 
+                                                  NULL,
+                                                  g_cclosure_marshal_VOID__BOOLEAN,
+                                                  G_TYPE_NONE,
+                                                  0);
+
   properties[PROP_BOOK] = g_param_spec_object ("book",
                                                "Book",
                                                "The BijiNoteBook",
@@ -621,4 +640,11 @@ bjb_controller_set_main_view (BjbController *self, GdMainView *current)
   bjb_controller_update_view (self);
 }
 
+gboolean
+bjb_controller_shows_notes (BjbController *self)
+{
+  if (self->priv->notes_to_show)
+    return TRUE;
 
+  return FALSE;
+}
diff --git a/src/bjb-controller.h b/src/bjb-controller.h
index 5091679..852467a 100644
--- a/src/bjb-controller.h
+++ b/src/bjb-controller.h
@@ -1,9 +1,8 @@
-/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
 /*
- * bijiben
- * Copyright (C) Pierre-Yves Luyten 2012 <py luyten fr>
+ * bjb-controller.h
+ * Copyright (C) Pierre-Yves Luyten 2012, 2013 <py luyten fr>
  * 
-bijiben is free software: you can redistribute it and/or modify it
+ * bijiben 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.
@@ -91,6 +90,8 @@ void bjb_controller_disconnect (BjbController *self);
 
 void bjb_controller_set_main_view (BjbController *self, GdMainView *current);
 
+gboolean bjb_controller_shows_notes (BjbController *self);
+
 G_END_DECLS
 
 #endif /* _BJB_CONTROLLER_H_ */
diff --git a/src/bjb-main-toolbar.c b/src/bjb-main-toolbar.c
index 19c31c0..c9d056c 100644
--- a/src/bjb-main-toolbar.c
+++ b/src/bjb-main-toolbar.c
@@ -1,5 +1,5 @@
 /* Bijiben
- * Copyright (C) Pierre-Yves Luyten 2012 <py luyten fr>
+ * Copyright (C) Pierre-Yves Luyten 2012, 2013 <py luyten fr>
  *
  * bijiben is free software: you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -49,6 +49,7 @@ struct _BjbMainToolbarPrivate
   gulong         finish_sig;
   gulong         update_selection;
   gulong         search_handler;
+  gulong         display_notes;
 };
 
 /* GObject properties */
@@ -160,6 +161,19 @@ on_button_press (GtkWidget* widget,
   return FALSE;
 }
 
+static void
+update_selection_buttons (BjbMainToolbarPrivate *priv)
+{
+  gboolean some_note_is_visible = bjb_controller_shows_notes (priv->controller);
+
+  if (priv->grid)
+    gtk_widget_set_sensitive (priv->grid, some_note_is_visible);
+
+  if (priv->list)
+    gtk_widget_set_sensitive (priv->list, some_note_is_visible);
+
+  gtk_widget_set_sensitive (priv->select, some_note_is_visible);
+}
 
 static gboolean
 update_selection_label (GdMainView *view, BjbMainToolbar *self)
@@ -183,7 +197,7 @@ update_selection_label (GdMainView *view, BjbMainToolbar *self)
 }
 
 static void
-populate_bar_for_selection(BjbMainToolbar *self)
+populate_bar_for_selection (BjbMainToolbar *self)
 {
   BjbMainToolbarPrivate *priv = self->priv;
   GtkStyleContext *context;
@@ -220,6 +234,11 @@ update_label_for_standard (BjbMainToolbar *self)
 
   gd_main_toolbar_set_labels (GD_MAIN_TOOLBAR (self), label, NULL);
   g_free (label);
+
+  self->priv->display_notes = g_signal_connect_swapped (self->priv->controller,
+                                                        "display-notes-changed",
+                                                        G_CALLBACK (update_selection_buttons),
+                                                        self->priv);
 }
 
 static void
@@ -267,6 +286,7 @@ populate_bar_for_icon_view(BjbMainToolbar *self)
   BjbMainToolbarPrivate *priv = self->priv;
 
   /* Switch to list */
+  priv->grid = NULL;
   priv->list= gd_main_toolbar_add_button(GD_MAIN_TOOLBAR (self),
                                          "view-list-symbolic",
                                          NULL,
@@ -284,6 +304,7 @@ populate_bar_for_list_view(BjbMainToolbar *self)
   BjbMainToolbarPrivate *priv = self->priv;
 
   /* Switch to icon view */
+  priv->list = NULL;
   priv->grid = gd_main_toolbar_add_button(GD_MAIN_TOOLBAR (self),
                                           "view-grid-symbolic",
                                           NULL,
@@ -301,15 +322,17 @@ populate_bar_switch(BjbMainToolbar *self)
   switch (self->priv->type)
   {
     case BJB_TOOLBAR_SELECT:
-      populate_bar_for_selection(self);
+      populate_bar_for_selection (self);
       break;
 
     case BJB_TOOLBAR_STD_ICON:
       populate_bar_for_icon_view(self);
+      update_selection_buttons (self->priv);
       break;
 
     case BJB_TOOLBAR_STD_LIST:
       populate_bar_for_list_view(self);
+      update_selection_buttons (self->priv);
       break;
 
     default:
@@ -347,6 +370,12 @@ populate_main_toolbar(BjbMainToolbar *self)
       priv->search_handler = 0;
     }
 
+    if (priv->display_notes != 0)
+    {
+      g_signal_handler_disconnect (priv->controller, priv->display_notes);
+      priv->display_notes = 0;
+    }
+
     populate_bar_switch (self);
   }
 }
@@ -362,6 +391,8 @@ bjb_main_toolbar_init (BjbMainToolbar *self)
 {
   self->priv = BJB_MAIN_TOOLBAR_GET_PRIVATE(self);
   self->priv->type = BJB_TOOLBAR_0 ;
+  self->priv->grid = NULL;
+  self->priv->list = NULL;
   g_signal_connect (self, "button-press-event", G_CALLBACK (on_button_press), NULL);
 }
 


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