[gnote] Implement search-as-you-type in notes list



commit c2a77a2fb941cb9cec0f79b7995fdc01d0fe6900
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Sep 7 00:02:05 2013 +0300

    Implement search-as-you-type in notes list

 help/C/gnote-searching-notes.page |    4 +-
 src/mainwindow.hpp                |    2 +-
 src/recentchanges.cpp             |   45 +++++++++++++++++++++++++++++++++++-
 src/recentchanges.hpp             |    3 +-
 src/searchnoteswidget.cpp         |    1 +
 src/searchnoteswidget.hpp         |    4 +++
 6 files changed, 53 insertions(+), 6 deletions(-)
---
diff --git a/help/C/gnote-searching-notes.page b/help/C/gnote-searching-notes.page
index 95ee92b..ecbdcc7 100644
--- a/help/C/gnote-searching-notes.page
+++ b/help/C/gnote-searching-notes.page
@@ -13,7 +13,7 @@
     </credit>
     <credit type="editor">
       <name>Aurimas Cernius</name>
-      <years>2011-2012</years>
+      <years>2011-2013</years>
     </credit>
     <credit type="editor">
       <name>Adam Dingle</name>
@@ -25,7 +25,7 @@
   <title>Exploring Notes</title>
 
   <p>The main <app>Gnote</app> window shows an overview of all your notes. By default the window will 
display notes in the order in which they were last modified. Click the <gui>Note</gui> or <gui>Last 
Changed</gui> column headings to change the sort order. Click a column heading a second time to toggle 
between ascending and descending order.</p>
-  <p>You can find specific notes by entering text in the search field at the top of the window. The note 
list will automatically update to include only the notes which have matching text.</p>
+  <p>You can find specific notes by clicking search button and entering text in the search field that 
apears. The note list will automatically update to include only the notes which have matching text. If notes 
list has focus, you can simply type search text there, search will be automatically activated.</p>
   <p>To open a note, do any of the following:</p>
   <list>
     <item><p>Double-click on a note.</p></item>
diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp
index 491df2b..d13e4ba 100644
--- a/src/mainwindow.hpp
+++ b/src/mainwindow.hpp
@@ -42,7 +42,7 @@ public:
   explicit MainWindow(const std::string & title);
 
   virtual void set_search_text(const std::string & value) = 0;
-  virtual void show_search_bar() = 0;
+  virtual void show_search_bar(bool grab_focus = true) = 0;
   virtual void present_search() = 0;
   virtual void new_note() = 0;
   virtual void close_window() = 0;
diff --git a/src/recentchanges.cpp b/src/recentchanges.cpp
index 95ff6be..214cfe5 100644
--- a/src/recentchanges.cpp
+++ b/src/recentchanges.cpp
@@ -69,6 +69,8 @@ namespace gnote {
       .connect(sigc::mem_fun(*this, &NoteRecentChanges::on_open_note));
     m_search_notes_widget.signal_open_note_new_window
       .connect(sigc::mem_fun(*this, &NoteRecentChanges::on_open_note_new_window));
+    m_search_notes_widget.notes_widget().signal_key_press_event()
+      .connect(sigc::mem_fun(*this, &NoteRecentChanges::on_notes_widget_key_press));
 
     Gtk::Toolbar *toolbar = make_toolbar();
     make_search_box();
@@ -268,10 +270,15 @@ namespace gnote {
     }
   }
 
-  void NoteRecentChanges::show_search_bar()
+  void NoteRecentChanges::show_search_bar(bool focus)
   {
+    if(m_search_box.get_visible()) {
+      return;
+    }
     m_search_box.show();
-    m_search_entry.grab_focus();
+    if(focus) {
+      m_search_entry.grab_focus();
+    }
     Glib::ustring text = m_search_entry.get_text();
     if(text != "") {
       SearchableItem *searchable_widget = dynamic_cast<SearchableItem*>(currently_embedded());
@@ -736,5 +743,39 @@ namespace gnote {
     }
   }
 
+  bool NoteRecentChanges::on_notes_widget_key_press(GdkEventKey *evt)
+  {
+    switch(evt->keyval) {
+    case GDK_KEY_Escape:
+    case GDK_KEY_Delete:
+      return false;
+    case GDK_KEY_BackSpace:
+      if(m_search_button.get_active()) {
+        Glib::ustring s = m_search_entry.get_text();
+        if(s.size()) {
+          m_search_entry.set_text(s.substr(0, s.size() - 1));
+        }
+      }
+      return false;
+    default:
+      {
+        guint32 character = gdk_keyval_to_unicode(evt->keyval);
+        if(character) {  // ignore special keys
+          if(!m_search_button.get_active()) {
+            // first show search box, then activate button
+            // because we do not want the box to get selected
+            show_search_bar(false);
+            m_search_button.activate();
+          }
+          Glib::ustring s;
+          s += character;
+          g_signal_emit_by_name(m_search_entry.gobj(), "insert-at-cursor", s.c_str());
+          return true;
+        }
+        return false;
+      }
+    }
+  }
+
 }
 
diff --git a/src/recentchanges.hpp b/src/recentchanges.hpp
index 6f23d1e..c29bf76 100644
--- a/src/recentchanges.hpp
+++ b/src/recentchanges.hpp
@@ -44,7 +44,7 @@ class NoteRecentChanges
 public:
   NoteRecentChanges(NoteManager& m);
   virtual ~NoteRecentChanges();
-  virtual void show_search_bar() override;
+  virtual void show_search_bar(bool grab_focus = true) override;
   virtual void set_search_text(const std::string & value) override;
   virtual void new_note() override;
   virtual void present_search() override;
@@ -89,6 +89,7 @@ private:
                                                 const std::vector<Glib::RefPtr<Gtk::Action> > & actions);
   void on_main_window_actions_changed(Gtk::Menu **menu);
   void on_settings_changed(const Glib::ustring & key);
+  bool on_notes_widget_key_press(GdkEventKey*);
 
   NoteManager        &m_note_manager;
   SearchNotesWidget   m_search_notes_widget;
diff --git a/src/searchnoteswidget.cpp b/src/searchnoteswidget.cpp
index fa2d14b..ae7c8ca 100644
--- a/src/searchnoteswidget.cpp
+++ b/src/searchnoteswidget.cpp
@@ -76,6 +76,7 @@ SearchNotesWidget::SearchNotesWidget(NoteManager & m)
 
   make_recent_tree();
   m_tree = manage(m_tree);
+  m_tree->set_enable_search(false);
   m_tree->show();
 
   update_results();
diff --git a/src/searchnoteswidget.hpp b/src/searchnoteswidget.hpp
index 1db276a..634441e 100644
--- a/src/searchnoteswidget.hpp
+++ b/src/searchnoteswidget.hpp
@@ -60,6 +60,10 @@ public:
   void select_all_notes_notebook();
   void new_note();
   void delete_selected_notes();
+  Gtk::Widget & notes_widget() const
+    {
+      return *m_tree;
+    }
 
   sigc::signal<void, const Note::Ptr &> signal_open_note;
   sigc::signal<void, const Note::Ptr &> signal_open_note_new_window;


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