OSC: Changed gainVCA to gainfader as VCA is already used.
[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 "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 {
34         set_name (X_("FloatingTextEntry"));
35         set_position (Gtk::WIN_POS_MOUSE);
36         set_border_width (0);
37
38         if (!initial_contents.empty()) {
39                 entry.set_text (initial_contents);
40         }
41
42         entry.show ();
43         entry.signal_changed().connect (sigc::mem_fun (*this, &FloatingTextEntry::changed));
44         entry.signal_activate().connect (sigc::mem_fun (*this, &FloatingTextEntry::activated));
45         entry.signal_key_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::key_press));
46         entry.signal_button_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::button_press));
47
48         if (parent) {
49                 parent->signal_focus_out_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::entry_focus_out));
50         }
51
52         add (entry);
53 }
54
55 void
56 FloatingTextEntry::changed ()
57 {
58         entry_changed = true;
59 }
60
61 void
62 FloatingTextEntry::on_realize ()
63 {
64         Gtk::Window::on_realize ();
65         get_window()->set_decorations (Gdk::WMDecoration (0));
66         entry.add_modal_grab ();
67 }
68
69 bool
70 FloatingTextEntry::entry_focus_out (GdkEventFocus* ev)
71 {
72         entry.remove_modal_grab ();
73         if (entry_changed) {
74                 use_text (entry.get_text ());
75         }
76
77         delete_when_idle ( this);
78         return false;
79 }
80
81 bool
82 FloatingTextEntry::button_press (GdkEventButton* ev)
83 {
84         if (Gtkmm2ext::event_inside_widget_window (*this, (GdkEvent*) ev)) {
85                 return true;
86         }
87
88         /* Clicked outside widget window - edit is done */
89         entry.remove_modal_grab ();
90
91         /* arrange re-propagation of the event once we go idle */
92         Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::ptr_fun (gtk_main_do_event), gdk_event_copy ((GdkEvent*) ev)), false));
93
94         if (entry_changed) {
95                 use_text (entry.get_text ());
96         }
97
98         delete_when_idle ( this);
99
100         return false;
101 }
102
103 void
104 FloatingTextEntry::activated ()
105 {
106         use_text (entry.get_text()); // EMIT SIGNAL
107         delete_when_idle (this);
108 }
109
110 bool
111 FloatingTextEntry::key_press (GdkEventKey* ev)
112 {
113         switch (ev->keyval) {
114         case GDK_Escape:
115                 delete_when_idle (this);
116                 return true;
117                 break;
118         default:
119                 break;
120         }
121         return false;
122 }
123
124 void
125 FloatingTextEntry::on_hide ()
126 {
127         entry.remove_modal_grab ();
128
129         /* No hide button is shown (no decoration on the window),
130            so being hidden is equivalent to the Escape key or any other
131            method of cancelling the edit.
132         */
133
134         delete_when_idle (this);
135         Gtk::Window::on_hide ();
136 }