2 * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "pbd/stacktrace.h"
21 #include "public_editor.h"
24 #include "floating_text_entry.h"
25 #include "gtkmm2ext/doi.h"
26 #include "gtkmm2ext/utils.h"
30 FloatingTextEntry::FloatingTextEntry (Gtk::Window* parent, const std::string& initial_contents)
32 , entry_changed (false)
34 //set_name (X_("FloatingTextEntry"));
35 set_position (Gtk::WIN_POS_MOUSE);
37 set_type_hint(Gdk::WINDOW_TYPE_HINT_POPUP_MENU);
38 set_resizable (false);
40 if (!initial_contents.empty()) {
41 entry.set_text (initial_contents);
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)));
51 entry.select_region (0, -1);
54 set_transient_for (*parent);
61 FloatingTextEntry::changed ()
67 FloatingTextEntry::on_realize ()
69 Gtk::Window::on_realize ();
70 get_window()->set_decorations (Gdk::WMDecoration (0));
71 set_keep_above (true);
72 entry.add_modal_grab ();
76 FloatingTextEntry::entry_focus_out (GdkEventFocus* ev)
78 entry.remove_modal_grab ();
81 use_text (entry.get_text (), 0); /* EMIT SIGNAL */
89 FloatingTextEntry::button_press (GdkEventButton* ev)
91 if (Gtkmm2ext::event_inside_widget_window (*this, (GdkEvent*) ev)) {
95 /* Clicked outside widget window - edit is done */
96 entry.remove_modal_grab ();
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));
102 disconect_signals ();
103 use_text (entry.get_text (), 0); /* EMIT SIGNAL */
112 FloatingTextEntry::activated ()
114 disconect_signals ();
115 use_text (entry.get_text(), 0); // EMIT SIGNAL
120 FloatingTextEntry::key_press (GdkEventKey* ev)
122 /* steal escape, tabs from GTK */
124 switch (ev->keyval) {
126 case GDK_ISO_Left_Tab:
134 FloatingTextEntry::key_release (GdkEventKey* ev)
136 switch (ev->keyval) {
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
147 disconect_signals ();
148 use_text (entry.get_text(), -1); // EMIT SIGNAL, move to prev
153 disconect_signals ();
154 use_text (entry.get_text(), 1); // EMIT SIGNAL, move to next
166 FloatingTextEntry::on_hide ()
168 entry.remove_modal_grab ();
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.
174 * This is also used during disconect_signals() before calling
175 * use_text (). see note below.
177 * If signals are already disconnected, idle-delete must be
178 * in progress already.
180 if (!_connections.empty ()) {
183 Gtk::Window::on_hide ();
187 FloatingTextEntry::disconect_signals ()
189 for (std::list<sigc::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
192 _connections.clear ();
193 /* the entry is floating on-top, emitting use_text()
194 * may result in another dialog being shown (cannot rename track)
196 * - be stacked below the floating text entry
197 * - return focus to the entry when closedA
198 * so we hide the entry here.
204 FloatingTextEntry::idle_delete_self ()
206 disconect_signals ();
207 delete_when_idle (this);