Fix recursion in FloatinTextEntry (amend f62c8c664d)
[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 {
35         //set_name (X_("FloatingTextEntry"));
36         set_position (Gtk::WIN_POS_MOUSE);
37         set_border_width (0);
38
39         if (!initial_contents.empty()) {
40                 entry.set_text (initial_contents);
41         }
42
43         entry.show ();
44         _connections.push_back (entry.signal_changed().connect (sigc::mem_fun (*this, &FloatingTextEntry::changed)));
45         _connections.push_back (entry.signal_activate().connect (sigc::mem_fun (*this, &FloatingTextEntry::activated)));
46         _connections.push_back (entry.signal_key_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::key_press), false));
47         _connections.push_back (entry.signal_key_release_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::key_release), false));
48         _connections.push_back (entry.signal_button_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::button_press)));
49         _connections.push_back (entry.signal_populate_popup().connect (sigc::mem_fun (*this, &FloatingTextEntry::populate_popup)));
50
51         entry.select_region (0, -1);
52
53         if (parent) {
54                 _connections.push_back (parent->signal_focus_out_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::entry_focus_out)));
55         }
56
57         add (entry);
58 }
59
60 void
61 FloatingTextEntry::populate_popup (Gtk::Menu *)
62 {
63         by_popup_menu = true;
64 }
65
66 void
67 FloatingTextEntry::changed ()
68 {
69         entry_changed = true;
70 }
71
72 void
73 FloatingTextEntry::on_realize ()
74 {
75         Gtk::Window::on_realize ();
76         get_window()->set_decorations (Gdk::WMDecoration (0));
77         entry.add_modal_grab ();
78 }
79
80 bool
81 FloatingTextEntry::entry_focus_out (GdkEventFocus* ev)
82 {
83         if (by_popup_menu) {
84                 by_popup_menu = false;
85                 return false;
86         }
87
88         entry.remove_modal_grab ();
89         if (entry_changed) {
90                 disconect_signals ();
91                 use_text (entry.get_text (), 0); /* EMIT SIGNAL */
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                 disconect_signals ();
113                 use_text (entry.get_text (), 0); /* EMIT SIGNAL */
114         }
115
116         idle_delete_self ();
117
118         return false;
119 }
120
121 void
122 FloatingTextEntry::activated ()
123 {
124         disconect_signals ();
125         use_text (entry.get_text(), 0); // EMIT SIGNAL
126         idle_delete_self ();
127 }
128
129 bool
130 FloatingTextEntry::key_press (GdkEventKey* ev)
131 {
132         /* steal escape, tabs from GTK */
133
134         switch (ev->keyval) {
135         case GDK_Escape:
136         case GDK_ISO_Left_Tab:
137         case GDK_Tab:
138                 return true;
139         }
140         return false;
141 }
142
143 bool
144 FloatingTextEntry::key_release (GdkEventKey* ev)
145 {
146         switch (ev->keyval) {
147         case GDK_Escape:
148                 /* cancel edit */
149                 idle_delete_self ();
150                 return true;
151
152         case GDK_ISO_Left_Tab:
153                 /* Shift+Tab Keys Pressed. Note that for Shift+Tab, GDK actually
154                  * generates a different ev->keyval, rather than setting
155                  * ev->state.
156                  */
157                 disconect_signals ();
158                 use_text (entry.get_text(), -1); // EMIT SIGNAL, move to prev
159                 idle_delete_self ();
160                 return true;
161
162         case GDK_Tab:
163                 disconect_signals ();
164                 use_text (entry.get_text(), 1); // EMIT SIGNAL, move to next
165                 idle_delete_self ();
166                 return true;
167         default:
168                 break;
169         }
170
171         return false;
172 }
173
174
175 void
176 FloatingTextEntry::on_hide ()
177 {
178         entry.remove_modal_grab ();
179
180         /* No hide button is shown (no decoration on the window),
181          * so being hidden is equivalent to the Escape key or any other
182          * method of cancelling the edit.
183          *
184          * This is also used during disconect_signals() before calling
185          * use_text (). see note below.
186          *
187          * If signals are already disconnected, idle-delete must be
188          * in progress already.
189          */
190         if (!_connections.empty ()) {
191                 idle_delete_self ();
192         }
193         Gtk::Window::on_hide ();
194 }
195
196 void
197 FloatingTextEntry::disconect_signals ()
198 {
199         for (std::list<sigc::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
200                 i->disconnect ();
201         }
202          _connections.clear ();
203         /* the entry is floating on-top, emitting use_text()
204          * may result in another dialog being shown (cannot rename track)
205          * which would
206          *  - be stacked below the floating text entry
207          *  - return focus to the entry when closedA
208          * so we hide the entry here.
209          */
210         hide ();
211 }
212
213 void
214 FloatingTextEntry::idle_delete_self ()
215 {
216         disconect_signals ();
217         delete_when_idle (this);
218 }