permit initial contents of a FloatingTextEntry
[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
22 #include "floating_text_entry.h"
23 #include "gtkmm2ext/doi.h"
24 #include "gtkmm2ext/utils.h"
25
26 #include "i18n.h"
27
28 FloatingTextEntry::FloatingTextEntry (const std::string& initial_contents)
29         : ArdourWindow ("")
30         , entry_changed (false)
31 {
32         set_name (X_("FloatingTextEntry"));
33         set_position (Gtk::WIN_POS_MOUSE);
34         set_border_width (0);
35
36         if (!initial_contents.empty()) {
37                 entry.set_text (initial_contents);
38         }
39
40         entry.show ();
41         entry.signal_changed().connect (sigc::mem_fun (*this, &FloatingTextEntry::changed));
42         entry.signal_activate().connect (sigc::mem_fun (*this, &FloatingTextEntry::activated));
43         entry.signal_key_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::key_press));
44         entry.signal_button_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::button_press));
45
46         add (entry);
47 }
48
49 void
50 FloatingTextEntry::changed ()
51 {
52         entry_changed = true;
53 }
54
55 void
56 FloatingTextEntry::on_realize ()
57 {
58         ArdourWindow::on_realize ();
59         get_window()->set_decorations (Gdk::WMDecoration (0));
60         entry.add_modal_grab ();
61 }
62
63 bool
64 FloatingTextEntry::button_press (GdkEventButton* ev)
65 {
66         if (Gtkmm2ext::event_inside_widget_window (*this, (GdkEvent*) ev)) {
67                 return true;
68         }
69
70         /* Clicked outside widget window - edit is done */
71
72         entry.remove_modal_grab ();
73
74         /* arrange re-propagation of the event once we go idle */
75
76         Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::ptr_fun (gtk_main_do_event), gdk_event_copy ((GdkEvent*) ev)), false));
77
78         if (entry_changed) {
79                 use_text (entry.get_text ());
80         }
81         
82         delete_when_idle ( this);
83
84         return false;
85 }
86
87 void
88 FloatingTextEntry::activated ()
89 {
90         use_text (entry.get_text()); // EMIT SIGNAL
91         delete_when_idle (this);
92 }
93
94 bool
95 FloatingTextEntry::key_press (GdkEventKey* ev)
96 {
97         switch (ev->keyval) {
98         case GDK_Escape:
99                 delete_when_idle (this);
100                 return true;
101                 break;
102         default:
103                 break;
104         }
105         return false;
106 }
107
108 void
109 FloatingTextEntry::on_hide ()
110 {
111         entry.remove_modal_grab ();
112
113         /* No hide button is shown (no decoration on the window), 
114            so being hidden is equivalent to the Escape key or any other 
115            method of cancelling the edit.
116         */
117
118         delete_when_idle (this);
119         ArdourWindow::on_hide ();
120 }