do not scan (discover) (new) plugins when running without a GUI
[ardour.git] / libs / widgets / searchbar.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <iostream>
20
21 #include "gtkmm2ext/keyboard.h"
22 #include "widgets/searchbar.h"
23
24 using namespace ArdourWidgets;
25
26 SearchBar::SearchBar (const std::string& label, bool icon_resets)
27         : placeholder_text (label)
28         , icon_click_resets (icon_resets)
29 {
30         set_text (placeholder_text);
31         set_alignment (Gtk::ALIGN_CENTER);
32         signal_key_press_event().connect (sigc::mem_fun (*this, &SearchBar::key_press_event));
33         signal_focus_in_event().connect (sigc::mem_fun (*this, &SearchBar::focus_in_event));
34         signal_focus_out_event().connect (sigc::mem_fun (*this, &SearchBar::focus_out_event));
35         signal_changed().connect (sigc::mem_fun (*this, &SearchBar::search_string_changed));
36         signal_icon_release().connect (sigc::mem_fun (*this, &SearchBar::icon_clicked_event));
37 }
38
39 bool
40 SearchBar::focus_in_event (GdkEventFocus*)
41 {
42         if (get_text ().compare (placeholder_text) == 0) {
43                 set_text ("");
44         }
45
46         icon = get_icon_pixbuf ();
47         if (icon) {
48                 set_icon_from_pixbuf (Glib::RefPtr<Gdk::Pixbuf> ());
49         }
50         return true;
51 }
52
53 bool
54 SearchBar::focus_out_event (GdkEventFocus*)
55 {
56         if (get_text ().empty ()) {
57                 set_text (placeholder_text);
58         }
59
60         if (icon) {
61                 set_icon_from_pixbuf (icon);
62                 icon.reset ();
63         }
64
65         search_string_changed ();
66         return false;
67 }
68
69 bool
70 SearchBar::key_press_event (GdkEventKey* ev)
71 {
72         switch (ev->keyval) {
73         case GDK_Escape: 
74                 set_text (placeholder_text);
75                 return true;
76         default:
77                 break;
78         }
79
80         return false;
81 }
82
83 void
84 SearchBar::icon_clicked_event (Gtk::EntryIconPosition, const GdkEventButton*)
85 {
86         if (icon_click_resets) {
87                 reset ();
88         }
89         else {
90                 search_string_changed ();
91         }
92 }
93
94 void
95 SearchBar::search_string_changed () const
96 {
97         const std::string& text = get_text ();
98         if (text.empty() || text.compare (placeholder_text) == 0) {
99                 sig_search_string_updated ("");
100                 return;
101         }
102         sig_search_string_updated (text);
103 }
104
105 void
106 SearchBar::reset ()
107 {
108         set_text (placeholder_text);
109         search_string_changed ();
110 }