Properly remember window position.
[ardour.git] / libs / gtkmm2ext / searchbar.cc
1 #include "gtkmm2ext/searchbar.h"
2 #include "gtkmm2ext/keyboard.h"
3 #include <iostream>
4
5 namespace Gtkmm2ext {
6
7 SearchBar::SearchBar (const std::string& label, bool icon_resets)
8         : placeholder_text (label)
9         , icon_click_resets (icon_resets)
10 {
11         set_text (placeholder_text);
12         set_alignment (Gtk::ALIGN_CENTER);
13         signal_key_press_event().connect (sigc::mem_fun (*this, &SearchBar::key_press_event));
14         signal_focus_in_event().connect (sigc::mem_fun (*this, &SearchBar::focus_in_event));
15         signal_focus_out_event().connect (sigc::mem_fun (*this, &SearchBar::focus_out_event));
16         signal_changed().connect (sigc::mem_fun (*this, &SearchBar::search_string_changed));
17         signal_icon_release().connect (sigc::mem_fun (*this, &SearchBar::icon_clicked_event));
18 }
19
20 bool
21 SearchBar::focus_in_event (GdkEventFocus*)
22 {
23         if (get_text ().compare (placeholder_text) == 0) {
24                 set_text ("");
25         }
26
27         icon = get_icon_pixbuf ();
28         if (icon) {
29                 set_icon_from_pixbuf (Glib::RefPtr<Gdk::Pixbuf> ());
30         }
31         return true;
32 }
33
34 bool
35 SearchBar::focus_out_event (GdkEventFocus*)
36 {
37         if (get_text ().empty ()) {
38                 set_text (placeholder_text);
39         }
40
41         if (icon) {
42                 set_icon_from_pixbuf (icon);
43                 icon.reset ();
44         }
45
46         search_string_changed ();
47         return false;
48 }
49
50 bool
51 SearchBar::key_press_event (GdkEventKey* ev)
52 {
53         switch (ev->keyval) {
54         case GDK_Escape: 
55                 set_text (placeholder_text);
56                 return true;
57         default:
58                 break;
59         }
60
61         return false;
62 }
63
64 void
65 SearchBar::icon_clicked_event (Gtk::EntryIconPosition, const GdkEventButton*)
66 {
67         if (icon_click_resets) {
68                 reset ();
69         }
70         else {
71                 search_string_changed ();
72         }
73 }
74
75 void
76 SearchBar::search_string_changed () const
77 {
78         const std::string& text = get_text ();
79         if (text.empty() || text.compare (placeholder_text) == 0) {
80                 sig_search_string_updated ("");
81                 return;
82         }
83         sig_search_string_updated (text);
84 }
85
86 void
87 SearchBar::reset ()
88 {
89         set_text (placeholder_text);
90         search_string_changed ();
91 }
92
93 }