diff options
author | Mathias Buhr <napcode@apparatus.de> | 2016-03-07 23:56:03 +0100 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2016-03-08 08:44:58 -0500 |
commit | 1375c454fbf40a5c698f8e95d9dc4f85d0835ebd (patch) | |
tree | 0e7d0602f992d7990c9a4531547de296374b28ff /libs/gtkmm2ext | |
parent | 6565a1d0671f6953d210b11a32a284120ebbeb15 (diff) |
Implements filtering in bindings editor
Diffstat (limited to 'libs/gtkmm2ext')
-rw-r--r-- | libs/gtkmm2ext/gtkmm2ext/searchbar.h | 32 | ||||
-rw-r--r-- | libs/gtkmm2ext/searchbar.cc | 93 | ||||
-rw-r--r-- | libs/gtkmm2ext/wscript | 1 |
3 files changed, 126 insertions, 0 deletions
diff --git a/libs/gtkmm2ext/gtkmm2ext/searchbar.h b/libs/gtkmm2ext/gtkmm2ext/searchbar.h new file mode 100644 index 0000000000..5096ed53f9 --- /dev/null +++ b/libs/gtkmm2ext/gtkmm2ext/searchbar.h @@ -0,0 +1,32 @@ +#pragma once + +#include <gtkmm/entry.h> +#include <string> + +namespace Gtkmm2ext { + +class SearchBar : public Gtk::Entry +{ +public: + SearchBar(const std::string& placeholder_text = "Search...", bool icon_click_resets = true); + + void reset (); + // emitted when the filter has been updated + sigc::signal<void, const std::string&> signal_search_string_updated () { return sig_search_string_updated; } +protected: + bool focus_in_event (GdkEventFocus*); + bool focus_out_event (GdkEventFocus*); + + bool key_press_event (GdkEventKey*); + void icon_clicked_event (Gtk::EntryIconPosition, const GdkEventButton*); + + const std::string placeholder_text; + sigc::signal<void, const std::string&> sig_search_string_updated; +private: + void search_string_changed () const; + + Glib::RefPtr<Gdk::Pixbuf> icon; + bool icon_click_resets; +}; + +} diff --git a/libs/gtkmm2ext/searchbar.cc b/libs/gtkmm2ext/searchbar.cc new file mode 100644 index 0000000000..75915c1f2e --- /dev/null +++ b/libs/gtkmm2ext/searchbar.cc @@ -0,0 +1,93 @@ +#include "gtkmm2ext/searchbar.h" +#include "gtkmm2ext/keyboard.h" +#include <iostream> + +namespace Gtkmm2ext { + +SearchBar::SearchBar (const std::string& label, bool icon_resets) + : placeholder_text (label) + , icon_click_resets (icon_resets) +{ + set_text (placeholder_text); + set_alignment (Gtk::ALIGN_CENTER); + signal_key_press_event().connect (sigc::mem_fun (*this, &SearchBar::key_press_event)); + signal_focus_in_event().connect (sigc::mem_fun (*this, &SearchBar::focus_in_event)); + signal_focus_out_event().connect (sigc::mem_fun (*this, &SearchBar::focus_out_event)); + signal_changed().connect (sigc::mem_fun (*this, &SearchBar::search_string_changed)); + signal_icon_release().connect (sigc::mem_fun (*this, &SearchBar::icon_clicked_event)); +} + +bool +SearchBar::focus_in_event (GdkEventFocus*) +{ + if (get_text ().compare (placeholder_text) == 0) { + set_text (""); + } + + icon = get_icon_pixbuf (); + if (icon) { + set_icon_from_pixbuf (Glib::RefPtr<Gdk::Pixbuf> ()); + } + return true; +} + +bool +SearchBar::focus_out_event (GdkEventFocus*) +{ + if (get_text ().empty ()) { + set_text (placeholder_text); + } + + if (icon) { + set_icon_from_pixbuf (icon); + icon.reset (); + } + + search_string_changed (); + return false; +} + +bool +SearchBar::key_press_event (GdkEventKey* ev) +{ + switch (ev->keyval) { + case GDK_Escape: + set_text (placeholder_text); + return true; + default: + break; + } + + return false; +} + +void +SearchBar::icon_clicked_event (Gtk::EntryIconPosition, const GdkEventButton*) +{ + if (icon_click_resets) { + reset (); + } + else { + search_string_changed (); + } +} + +void +SearchBar::search_string_changed () const +{ + const std::string& text = get_text (); + if (text.empty() || text.compare (placeholder_text) == 0) { + sig_search_string_updated (""); + return; + } + sig_search_string_updated (text); +} + +void +SearchBar::reset () +{ + set_text (placeholder_text); + search_string_changed (); +} + +}
\ No newline at end of file diff --git a/libs/gtkmm2ext/wscript b/libs/gtkmm2ext/wscript index 0c8f454128..b5676d1423 100644 --- a/libs/gtkmm2ext/wscript +++ b/libs/gtkmm2ext/wscript @@ -57,6 +57,7 @@ gtkmm2ext_sources = [ 'popup.cc', 'prompter.cc', 'scroomer.cc', + 'searchbar.cc', 'selector.cc', 'slider_controller.cc', 'stateful_button.cc', |