Only show user-presets in favorite sidebar
[ardour.git] / gtk2_ardour / floating_text_entry.cc
1 /*
2   Copyright (C) 2014 Paul Davis
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
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/stacktrace.h"
21 #include "public_editor.h"
22
23
24 #include "floating_text_entry.h"
25 #include "gtkmm2ext/doi.h"
26 #include "gtkmm2ext/utils.h"
27
28 #include "pbd/i18n.h"
29
30 FloatingTextEntry::FloatingTextEntry (Gtk::Window* parent, const std::string& initial_contents)
31         : Gtk::Window ()
32         , entry_changed (false)
33 {
34         //set_name (X_("FloatingTextEntry"));
35         set_position (Gtk::WIN_POS_MOUSE);
36         set_border_width (0);
37         set_type_hint(Gdk::WINDOW_TYPE_HINT_POPUP_MENU);
38         set_resizable (false);
39
40         if (!initial_contents.empty()) {
41                 entry.set_text (initial_contents);
42         }
43
44         entry.show ();
45         _connections.push_back (entry.signal_changed().connect (sigc::mem_fun (*this, &FloatingTextEntry::changed)));
46         _connections.push_back (entry.signal_activate().connect (sigc::mem_fun (*this, &FloatingTextEntry::activated)));
47         _connections.push_back (entry.signal_key_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::key_press), false));
48         _connections.push_back (entry.signal_key_release_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::key_release), false));
49         _connections.push_back (entry.signal_button_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::button_press)));
50
51         entry.select_region (0, -1);
52
53         if (parent) {
54                 set_transient_for (*parent);
55         }
56
57         add (entry);
58 }
59
60 void
61 FloatingTextEntry::changed ()
62 {
63         entry_changed = true;
64 }
65
66 void
67 FloatingTextEntry::on_realize ()
68 {
69         Gtk::Window::on_realize ();
70         get_window()->set_decorations (Gdk::WMDecoration (0));
71         set_keep_above (true);
72         entry.add_modal_grab ();
73 }
74
75 bool
76 FloatingTextEntry::entry_focus_out (GdkEventFocus* ev)
77 {
78         entry.remove_modal_grab ();
79         if (entry_changed) {
80                 disconect_signals ();
81                 use_text (entry.get_text (), 0); /* EMIT SIGNAL */
82         }
83
84         idle_delete_self ();
85         return false;
86 }
87
88 bool
89 FloatingTextEntry::button_press (GdkEventButton* ev)
90 {
91         if (Gtkmm2ext::event_inside_widget_window (*this, (GdkEvent*) ev)) {
92                 return true;
93         }
94
95         /* Clicked outside widget window - edit is done */
96         entry.remove_modal_grab ();
97
98         /* arrange re-propagation of the event once we go idle */
99         Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::ptr_fun (gtk_main_do_event), gdk_event_copy ((GdkEvent*) ev)), false));
100
101         if (entry_changed) {
102                 disconect_signals ();
103                 use_text (entry.get_text (), 0); /* EMIT SIGNAL */
104         }
105
106         idle_delete_self ();
107
108         return false;
109 }
110
111 void
112 FloatingTextEntry::activated ()
113 {
114         disconect_signals ();
115         use_text (entry.get_text(), 0); // EMIT SIGNAL
116         idle_delete_self ();
117 }
118
119 bool
120 FloatingTextEntry::key_press (GdkEventKey* ev)
121 {
122         /* steal escape, tabs from GTK */
123
124         switch (ev->keyval) {
125         case GDK_Escape:
126         case GDK_ISO_Left_Tab:
127         case GDK_Tab:
128                 return true;
129         }
130         return false;
131 }
132
133 bool
134 FloatingTextEntry::key_release (GdkEventKey* ev)
135 {
136         switch (ev->keyval) {
137         case GDK_Escape:
138                 /* cancel edit */
139                 idle_delete_self ();
140                 return true;
141
142         case GDK_ISO_Left_Tab:
143                 /* Shift+Tab Keys Pressed. Note that for Shift+Tab, GDK actually
144                  * generates a different ev->keyval, rather than setting
145                  * ev->state.
146                  */
147                 disconect_signals ();
148                 use_text (entry.get_text(), -1); // EMIT SIGNAL, move to prev
149                 idle_delete_self ();
150                 return true;
151
152         case GDK_Tab:
153                 disconect_signals ();
154                 use_text (entry.get_text(), 1); // EMIT SIGNAL, move to next
155                 idle_delete_self ();
156                 return true;
157         default:
158                 break;
159         }
160
161         return false;
162 }
163
164
165 void
166 FloatingTextEntry::on_hide ()
167 {
168         entry.remove_modal_grab ();
169
170         /* No hide button is shown (no decoration on the window),
171          * so being hidden is equivalent to the Escape key or any other
172          * method of cancelling the edit.
173          *
174          * This is also used during disconect_signals() before calling
175          * use_text (). see note below.
176          *
177          * If signals are already disconnected, idle-delete must be
178          * in progress already.
179          */
180         if (!_connections.empty ()) {
181                 idle_delete_self ();
182         }
183         Gtk::Window::on_hide ();
184 }
185
186 void
187 FloatingTextEntry::disconect_signals ()
188 {
189         for (std::list<sigc::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
190                 i->disconnect ();
191         }
192          _connections.clear ();
193         /* the entry is floating on-top, emitting use_text()
194          * may result in another dialog being shown (cannot rename track)
195          * which would
196          *  - be stacked below the floating text entry
197          *  - return focus to the entry when closedA
198          * so we hide the entry here.
199          */
200         hide ();
201 }
202
203 void
204 FloatingTextEntry::idle_delete_self ()
205 {
206         disconect_signals ();
207         delete_when_idle (this);
208 }