da8b73453990293e798ab11265e6568b38fe7a77
[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 (Gtk::WINDOW_POPUP)
32         , entry_changed (false)
33         , by_popup_menu (false)
34         , _delete_queued (false)
35 {
36         //set_name (X_("FloatingTextEntry"));
37         set_position (Gtk::WIN_POS_MOUSE);
38         set_border_width (0);
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         _connections.push_back (entry.signal_populate_popup().connect (sigc::mem_fun (*this, &FloatingTextEntry::populate_popup)));
51
52         entry.select_region (0, -1);
53
54         if (parent) {
55                 _connections.push_back (parent->signal_focus_out_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::entry_focus_out)));
56         }
57
58         add (entry);
59 }
60
61 void
62 FloatingTextEntry::populate_popup (Gtk::Menu *)
63 {
64         by_popup_menu = true;
65 }
66
67 void
68 FloatingTextEntry::changed ()
69 {
70         entry_changed = true;
71 }
72
73 void
74 FloatingTextEntry::on_realize ()
75 {
76         Gtk::Window::on_realize ();
77         get_window()->set_decorations (Gdk::WMDecoration (0));
78         entry.add_modal_grab ();
79 }
80
81 bool
82 FloatingTextEntry::entry_focus_out (GdkEventFocus* ev)
83 {
84         if (by_popup_menu) {
85                 by_popup_menu = false;
86                 return false;
87         }
88
89         entry.remove_modal_grab ();
90         if (entry_changed) {
91                 use_text (entry.get_text (), 0);
92         }
93
94         idle_delete_self ();
95         return false;
96 }
97
98 bool
99 FloatingTextEntry::button_press (GdkEventButton* ev)
100 {
101         if (Gtkmm2ext::event_inside_widget_window (*this, (GdkEvent*) ev)) {
102                 return true;
103         }
104
105         /* Clicked outside widget window - edit is done */
106         entry.remove_modal_grab ();
107
108         /* arrange re-propagation of the event once we go idle */
109         Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::ptr_fun (gtk_main_do_event), gdk_event_copy ((GdkEvent*) ev)), false));
110
111         if (entry_changed) {
112                 use_text (entry.get_text (), 0);
113         }
114
115         idle_delete_self ();
116
117         return false;
118 }
119
120 void
121 FloatingTextEntry::activated ()
122 {
123         use_text (entry.get_text(), 0); // EMIT SIGNAL
124         idle_delete_self ();
125 }
126
127 bool
128 FloatingTextEntry::key_press (GdkEventKey* ev)
129 {
130         /* steal escape, tabs from GTK */
131
132         switch (ev->keyval) {
133         case GDK_Escape:
134         case GDK_ISO_Left_Tab:
135         case GDK_Tab:
136                 return true;
137         }
138         return false;
139 }
140
141 bool
142 FloatingTextEntry::key_release (GdkEventKey* ev)
143 {
144         switch (ev->keyval) {
145         case GDK_Escape:
146                 /* cancel edit */
147                 idle_delete_self ();
148                 return true;
149
150         case GDK_ISO_Left_Tab:
151                 /* Shift+Tab Keys Pressed. Note that for Shift+Tab, GDK actually
152                  * generates a different ev->keyval, rather than setting
153                  * ev->state.
154                  */
155                 use_text (entry.get_text(), -1); // EMIT SIGNAL, move to prev
156                 idle_delete_self ();
157                 return true;
158
159         case GDK_Tab:
160                 use_text (entry.get_text(), 1); // EMIT SIGNAL, move to next
161                 idle_delete_self ();
162                 return true;
163         default:
164                 break;
165         }
166
167         return false;
168 }
169
170
171 void
172 FloatingTextEntry::on_hide ()
173 {
174         entry.remove_modal_grab ();
175
176         /* No hide button is shown (no decoration on the window),
177            so being hidden is equivalent to the Escape key or any other
178            method of cancelling the edit.
179         */
180
181         idle_delete_self ();
182         Gtk::Window::on_hide ();
183 }
184
185 void
186 FloatingTextEntry::idle_delete_self ()
187 {
188         if (_delete_queued) {
189                 PBD::stacktrace (std::cerr, 20);
190                 return;
191         }
192         for (std::list<sigc::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
193                 i->disconnect ();
194         }
195         _delete_queued = true;
196         delete_when_idle (this);
197 }