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