[bijiben/wip/sadiq/modernize] search-toolbar: Simplify code



commit 641c9fa49af7ed0c94c069962237c1eab7296430
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Sun Oct 29 09:10:33 2017 +0530

    search-toolbar: Simplify code
    
    use G_DECLARE_FINAL_TYPE.
    avoid gd_tagged_entry and use gtk_search_entry.
    let GtkSearchBar handle keypress events. Don't do it manually.

 src/bjb-main-toolbar.c   |   20 +----
 src/bjb-search-toolbar.c |  197 +++++++++++-----------------------------------
 src/bjb-search-toolbar.h |   29 +------
 src/bjb-window-base.c    |   28 +++----
 src/bjb-window-base.h    |    4 +-
 5 files changed, 65 insertions(+), 213 deletions(-)
---
diff --git a/src/bjb-main-toolbar.c b/src/bjb-main-toolbar.c
index 488af3b..06ee12f 100644
--- a/src/bjb-main-toolbar.c
+++ b/src/bjb-main-toolbar.c
@@ -238,20 +238,10 @@ on_button_press (GtkWidget* widget,
 }
 
 static void
-on_search_button_clicked (BjbMainToolbarPrivate *priv)
-{
-  gboolean show_bar;
-
-  show_bar = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->search));
-  bjb_window_base_set_show_search_bar (BJB_WINDOW_BASE (priv->window), show_bar);
-}
-
-static void
 add_search_button (BjbMainToolbar *self)
 {
   BjbMainToolbarPrivate *priv = self->priv;
   GtkWidget *search_image;
-  gboolean active;
 
   priv->search = gtk_toggle_button_new ();
   search_image = gtk_image_new_from_icon_name ("edit-find-symbolic", GTK_ICON_SIZE_MENU);
@@ -263,11 +253,11 @@ add_search_button (BjbMainToolbar *self)
   gtk_widget_set_tooltip_text (priv->search,
                                _("Search note titles, content and notebooks"));
 
-  active =  bjb_window_base_get_show_search_bar (BJB_WINDOW_BASE (self->priv->window));
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->search), active);
-
-  g_signal_connect_swapped (priv->search, "clicked",
-                            G_CALLBACK (on_search_button_clicked), priv);
+  g_object_bind_property (priv->search,
+                          "active",
+                          bjb_window_base_get_search_bar (BJB_WINDOW_BASE (priv->window)),
+                          "search-mode-enabled",
+                          G_BINDING_BIDIRECTIONAL);
 }
 
 static void
diff --git a/src/bjb-search-toolbar.c b/src/bjb-search-toolbar.c
index cf5f6f9..5a47bb8 100644
--- a/src/bjb-search-toolbar.c
+++ b/src/bjb-search-toolbar.c
@@ -1,5 +1,6 @@
 /* bjb-search-toolbar.c
  * Copyright © 2012, 2013 Pierre-Yves LUYTEN <py luyten fr>
+ * Copyright 2017 Mohammed Sadiq <sadiq sadiqpk org>
  *
  * 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
@@ -46,9 +47,11 @@ enum
 
 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
 
-struct _BjbSearchToolbarPrivate
+struct _BjbSearchToolbar
 {
-  GdTaggedEntry     *entry;
+  GtkSearchBar       parent_instance;
+
+  GtkWidget         *entry;
   gchar             *needle;
   GtkEntryBuffer    *entry_buf;
   BjbController     *controller;
@@ -62,100 +65,14 @@ struct _BjbSearchToolbarPrivate
   GtkWidget         *window;
 };
 
-G_DEFINE_TYPE (BjbSearchToolbar, bjb_search_toolbar, GTK_TYPE_SEARCH_BAR);
-
-
-static void
-bjb_search_toolbar_toggle_search_button (BjbSearchToolbar *self,
-                                         gboolean state)
-{
-  bjb_window_base_toggle_search_button (BJB_WINDOW_BASE (self->priv->window),
-                                        state);
-}
-
-
-
-void
-bjb_search_toolbar_fade_in (BjbSearchToolbar *self)
-{
-  if (gtk_search_bar_get_search_mode (GTK_SEARCH_BAR (self)) == TRUE)
-    return;
-
-  gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (self), TRUE);
-  bjb_search_toolbar_toggle_search_button (self, TRUE);
-}
-
-
-
-void
-bjb_search_toolbar_fade_out (BjbSearchToolbar *self)
-{
-  if (gtk_search_bar_get_search_mode (GTK_SEARCH_BAR (self)) == FALSE)
-    return;
-
-  /* clear the search before hiding */
-  gtk_entry_set_text (GTK_ENTRY (self->priv->entry), "");
-  bjb_controller_set_needle (self->priv->controller, "");
-
-
-  gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (self), FALSE);
-  bjb_search_toolbar_toggle_search_button (self, FALSE);
-}
-
-
+G_DEFINE_TYPE (BjbSearchToolbar, bjb_search_toolbar, GTK_TYPE_SEARCH_BAR)
 
 static gboolean
 on_key_pressed (GtkWidget *widget,GdkEvent  *event,gpointer user_data)
 {
-  BjbSearchToolbar *self;
-  GdkModifierType modifiers;
-
-  self = BJB_SEARCH_TOOLBAR (user_data);
-  modifiers = gtk_accelerator_get_default_mod_mask ();
-
+  GtkSearchBar *bar = GTK_SEARCH_BAR (user_data);
 
-  if ((event->key.state & modifiers) == GDK_CONTROL_MASK ||
-      (event->key.state & modifiers) == GDK_MOD1_MASK)
-    return FALSE;
-
-  /* Reveal the entry is text is input */
-  if (gtk_search_bar_get_search_mode (GTK_SEARCH_BAR (self)) == FALSE)
-  {
-    switch (event->key.keyval)
-    {
-      case GDK_KEY_Control_L :
-      case GDK_KEY_Control_R :
-      case GDK_KEY_Shift_L :
-      case GDK_KEY_Shift_R :
-      case GDK_KEY_Alt_L :
-      case GDK_KEY_Alt_R :
-      case GDK_KEY_Tab :
-      case GDK_KEY_space :
-      case GDK_KEY_BackSpace :
-      case GDK_KEY_Left :
-      case GDK_KEY_Right :
-      case GDK_KEY_Up :
-      case GDK_KEY_Down :
-      case GDK_KEY_Return :
-        return FALSE;
-
-      /* err, we still return false to get the key for search... */
-      default:
-        if (event->key.keyval != GDK_KEY_Escape)
-          bjb_search_toolbar_fade_in (self);
-        return FALSE;
-    }
-  }
-
-  /* If there is already an entry and escape pressed, hide entry
-   * Maybe should we use gtk_widget_has_focus (widget) */
-  else if (event->key.keyval == GDK_KEY_Escape)
-  {
-    bjb_search_toolbar_fade_out (self);
-    return TRUE;
-  }
-
-  return FALSE;
+  return gtk_search_bar_handle_event (bar, event);
 }
 
 
@@ -170,10 +87,10 @@ bjb_search_toolbar_get_property (GObject    *object,
   switch (property_id)
   {
     case PROP_WINDOW:
-      g_value_set_object (value, self->priv->window);
+      g_value_set_object (value, self->window);
       break;
     case PROP_CONTROLLER:
-      g_value_set_object(value, self->priv->controller);
+      g_value_set_object(value, self->controller);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -193,10 +110,10 @@ bjb_search_toolbar_set_property (GObject      *object,
   switch (property_id)
   {
     case PROP_WINDOW:
-      self->priv->window = g_value_get_object (value);
+      self->window = g_value_get_object (value);
       break;
     case PROP_CONTROLLER:
-      self->priv->controller = g_value_get_object (value);
+      self->controller = g_value_get_object (value);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -204,9 +121,6 @@ bjb_search_toolbar_set_property (GObject      *object,
   }
 }
 
-
-
-
 static void
 action_search_entry (GtkEntry *entry, BjbController *controller)
 {
@@ -222,8 +136,8 @@ action_entry_insert_callback (GtkEntryBuffer *buffer,
                               guint n_chars,
                               BjbSearchToolbar *self)
 {
-  action_search_entry (GTK_ENTRY (self->priv->entry),
-                       self->priv->controller);
+  action_search_entry (GTK_ENTRY (self->entry),
+                       self->controller);
 }
 
 
@@ -234,53 +148,42 @@ action_entry_delete_callback (GtkEntryBuffer *buffer,
                               guint position,
                               guint n_chars,
                               BjbSearchToolbar *self)
-{ 
-  action_search_entry (GTK_ENTRY (self->priv->entry),
-                       self->priv->controller);
+{
+  action_search_entry (GTK_ENTRY (self->entry),
+                       self->controller);
 }
 
 
 void
 bjb_search_toolbar_disconnect (BjbSearchToolbar *self)
 {
-  BjbSearchToolbarPrivate *priv = self->priv ;
-
-
-  g_signal_handler_disconnect (priv->window,priv->key_pressed);
-  g_signal_handler_disconnect (priv->entry_buf, priv->inserted);
-  g_signal_handler_disconnect (priv->entry_buf, priv->deleted);
-
-
-  priv->key_pressed = 0;
-  priv->inserted = 0;
-  priv->deleted = 0;
-
-}
-
-
-static void
-bjb_search_toolbar_finalize (GObject *obj)
-{
-  G_OBJECT_CLASS (bjb_search_toolbar_parent_class)->finalize (obj);
+  if (self->key_pressed)
+    g_signal_handler_disconnect (self->window, self->key_pressed);
+  if (self->inserted)
+    g_signal_handler_disconnect (self->entry_buf, self->inserted);
+  if (self->deleted)
+    g_signal_handler_disconnect (self->entry_buf, self->deleted);
+
+  self->key_pressed = 0;
+  self->inserted = 0;
+  self->deleted = 0;
 }
 
 void
 bjb_search_toolbar_connect (BjbSearchToolbar *self)
 {
-  BjbSearchToolbarPrivate *priv = self->priv ;
-
   /* Connect to set the text */
-  if (priv->key_pressed == 0)
-    priv->key_pressed = g_signal_connect(priv->window,"key-press-event",
-                                         G_CALLBACK(on_key_pressed),self);
+  if (self->key_pressed == 0)
+    self->key_pressed = g_signal_connect(self->window,"key-press-event",
+                                         G_CALLBACK(on_key_pressed), self);
 
 
-  if (priv->inserted ==0)
-    priv->inserted = g_signal_connect (priv->entry_buf, "inserted-text",
+  if (self->inserted == 0)
+    self->inserted = g_signal_connect (self->entry_buf, "inserted-text",
                         G_CALLBACK (action_entry_insert_callback), self);
 
-  if (priv->deleted ==0)
-    priv->deleted = g_signal_connect (priv->entry_buf, "deleted-text",
+  if (self->deleted ==0)
+    self->deleted = g_signal_connect (self->entry_buf, "deleted-text",
                         G_CALLBACK (action_entry_delete_callback), self);
 }
 
@@ -288,19 +191,17 @@ static void
 bjb_search_toolbar_constructed (GObject *obj)
 {
   BjbSearchToolbar        *self = BJB_SEARCH_TOOLBAR(obj);
-  BjbSearchToolbarPrivate *priv = self->priv ;
 
   G_OBJECT_CLASS (bjb_search_toolbar_parent_class)->constructed (obj);
 
   /* Get the needle from controller */
-  priv->needle = bjb_controller_get_needle (priv->controller);
-  priv->entry_buf = gtk_entry_get_buffer (GTK_ENTRY (priv->entry));
-
-  if (priv->needle && g_strcmp0 (priv->needle, "") != 0)
-  { 
-    gtk_entry_set_text (GTK_ENTRY (priv->entry), priv->needle);
-    bjb_search_toolbar_fade_in (self);
-    gtk_editable_set_position (GTK_EDITABLE (self->priv->entry), -1);
+  self->needle = bjb_controller_get_needle (self->controller);
+  self->entry_buf = gtk_entry_get_buffer (GTK_ENTRY (self->entry));
+
+  if (self->needle && g_strcmp0 (self->needle, "") != 0)
+  {
+    gtk_entry_set_text (GTK_ENTRY (self->entry), self->needle);
+    gtk_editable_set_position (GTK_EDITABLE (self->entry), -1);
   }
 }
 
@@ -308,15 +209,11 @@ bjb_search_toolbar_constructed (GObject *obj)
 static void
 bjb_search_toolbar_init (BjbSearchToolbar *self)
 {
-  BjbSearchToolbarPrivate    *priv;
-
-  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, BJB_TYPE_SEARCH_TOOLBAR, BjbSearchToolbarPrivate);
-  priv = self->priv;
-
-  priv->entry = gd_tagged_entry_new ();
-  g_object_set (priv->entry, "width_request", 500, NULL);
-  gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (priv->entry));
-  gtk_widget_show (GTK_WIDGET (priv->entry));
+  self->entry = gtk_search_entry_new ();
+  gtk_search_bar_connect_entry (GTK_SEARCH_BAR (self), GTK_ENTRY (self->entry));
+  g_object_set (self->entry, "width_request", 500, NULL);
+  gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->entry));
+  gtk_widget_show (GTK_WIDGET (self->entry));
 }
 
 
@@ -328,7 +225,6 @@ bjb_search_toolbar_class_init (BjbSearchToolbarClass *class)
   object_class->get_property = bjb_search_toolbar_get_property ;
   object_class->set_property = bjb_search_toolbar_set_property ;
   object_class->constructed = bjb_search_toolbar_constructed ;
-  object_class->finalize = bjb_search_toolbar_finalize ;
 
   properties[PROP_WINDOW] = g_param_spec_object ("window",
                                                  "Window",
@@ -347,7 +243,6 @@ bjb_search_toolbar_class_init (BjbSearchToolbarClass *class)
                                                      G_PARAM_STATIC_STRINGS);
 
   g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
-  g_type_class_add_private (class, sizeof (BjbSearchToolbarPrivate));
 }
 
 
diff --git a/src/bjb-search-toolbar.h b/src/bjb-search-toolbar.h
index 5145e2e..f7ee08b 100644
--- a/src/bjb-search-toolbar.h
+++ b/src/bjb-search-toolbar.h
@@ -1,5 +1,6 @@
 /* bjb-search-toolbar.h
  * Copyright © 2012, 2013 Pierre-Yves LUYTEN <py luyten fr>
+ * Copyright 2017 Mohammed Sadiq <sadiq sadiqpk org>
  *
  * 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
@@ -24,33 +25,7 @@ G_BEGIN_DECLS
 
 #define BJB_TYPE_SEARCH_TOOLBAR (bjb_search_toolbar_get_type ())
 
-#define BJB_SEARCH_TOOLBAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BJB_TYPE_SEARCH_TOOLBAR, 
BjbSearchToolbar))
-
-#define BJB_SEARCH_TOOLBAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), BJB_TYPE_SEARCH_TOOLBAR, 
BjbSearchToolbarClass))
-
-#define BJB_IS_SEARCH_TOOLBAR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BJB_TYPE_SEARCH_TOOLBAR))
-
-#define BJB_IS_SEARCH_TOOLBAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), BJB_TYPE_SEARCH_TOOLBAR))
-
-#define BJB_SEARCH_TOOLBAR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BJB_TYPE_SEARCH_TOOLBAR, 
BjbSearchToolbarClass))
-
-typedef struct _BjbSearchToolbar        BjbSearchToolbar;
-typedef struct _BjbSearchToolbarClass   BjbSearchToolbarClass;
-typedef struct _BjbSearchToolbarPrivate BjbSearchToolbarPrivate;
-
-struct _BjbSearchToolbar
-{
-  GtkSearchBar parent_instance;
-  BjbSearchToolbarPrivate *priv;
-};
-
-struct _BjbSearchToolbarClass
-{
-  GtkSearchBarClass parent_class;
-};
-
-
-GType              bjb_search_toolbar_get_type              (void) G_GNUC_CONST;
+G_DECLARE_FINAL_TYPE (BjbSearchToolbar, bjb_search_toolbar, BJB, SEARCH_TOOLBAR, GtkSearchBar)
 
 
 BjbSearchToolbar  *bjb_search_toolbar_new                   (GtkWidget *window,
diff --git a/src/bjb-window-base.c b/src/bjb-window-base.c
index ea590da..d910485 100644
--- a/src/bjb-window-base.c
+++ b/src/bjb-window-base.c
@@ -294,15 +294,15 @@ bjb_window_base_constructed (GObject *obj)
      GTK_WINDOW (obj),
      priv->entry );
 
+  /* Search entry toolbar */
+  priv->search_bar = bjb_search_toolbar_new (GTK_WIDGET (obj), priv->controller);
+  gtk_box_pack_start (GTK_BOX (priv->vbox), GTK_WIDGET (priv->search_bar), FALSE, FALSE, 0);
+
   /* Shared toolbar */
   priv->view = bjb_main_view_new (GTK_WIDGET (obj), priv->controller);
   priv->main_toolbar = bjb_main_toolbar_new (priv->view, priv->controller);
   gtk_window_set_titlebar (GTK_WINDOW (self), GTK_WIDGET (priv->main_toolbar));
 
-  /* Search entry toolbar */
-  priv->search_bar = bjb_search_toolbar_new (GTK_WIDGET (obj), priv->controller);
-  gtk_box_pack_start (GTK_BOX (priv->vbox), GTK_WIDGET (priv->search_bar), FALSE, FALSE, 0);
-
   /* UI : stack for different views */
   priv->stack = GTK_STACK (gtk_stack_new ());
   gtk_box_pack_start (GTK_BOX (priv->vbox), GTK_WIDGET (priv->stack), TRUE, TRUE, 0);
@@ -554,7 +554,6 @@ bjb_window_base_switch_to_item (BjbWindowBase *bwb, BijiItem *item)
   GtkWidget *w = GTK_WIDGET (bwb);
 
   bjb_search_toolbar_disconnect (priv->search_bar);
-  bjb_search_toolbar_fade_out (priv->search_bar);
   destroy_note_if_needed (bwb);
 
   if (BIJI_IS_NOTE_OBJ (item))
@@ -613,6 +612,12 @@ bjb_window_base_get_main_view (BjbWindowBase *self)
   return (gpointer) self->priv->view;
 }
 
+GtkWidget *
+bjb_window_base_get_search_bar (BjbWindowBase *self)
+{
+  return GTK_WIDGET (self->priv->search_bar);
+}
+
 gboolean
 bjb_window_base_get_show_search_bar (BjbWindowBase *self)
 {
@@ -627,19 +632,6 @@ bjb_window_base_get_show_search_bar (BjbWindowBase *self)
 }
 
 gboolean
-bjb_window_base_set_show_search_bar (BjbWindowBase *self,
-                                     gboolean show)
-{
-  if (show)
-    bjb_search_toolbar_fade_in (self->priv->search_bar);
-
-  else
-    bjb_search_toolbar_fade_out (self->priv->search_bar);
-
-  return TRUE;
-}
-
-gboolean
 bjb_window_base_toggle_search_button (BjbWindowBase *self,
                                       gboolean active)
 {
diff --git a/src/bjb-window-base.h b/src/bjb-window-base.h
index 7512ea1..8df4daf 100644
--- a/src/bjb-window-base.h
+++ b/src/bjb-window-base.h
@@ -79,10 +79,10 @@ BijiNoteObj           *bjb_window_base_get_note (BjbWindowBase *self);
 gboolean               switch_window_fullscreen (void);
 
 
-gboolean               bjb_window_base_get_show_search_bar (BjbWindowBase *self);
+GtkWidget             *bjb_window_base_get_search_bar (BjbWindowBase *self);
 
 
-gboolean               bjb_window_base_set_show_search_bar (BjbWindowBase *self, gboolean show);
+gboolean               bjb_window_base_get_show_search_bar (BjbWindowBase *self);
 
 
 gboolean               bjb_window_base_toggle_search_button (BjbWindowBase *self,


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