add new floating text entry class (ported from Tracks)
[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 "floating_text_entry.h"
21 #include "gtkmm2ext/doi.h"
22
23 #include "i18n.h"
24
25 FloatingTextEntry::FloatingTextEntry ()
26         : ArdourWindow ("")
27 {
28         set_name (X_("FloatingTextEntry"));
29         set_position (Gtk::WIN_POS_MOUSE);
30         set_border_width (0);
31         
32         entry.show ();
33         entry.signal_activate().connect (sigc::mem_fun (*this, &FloatingTextEntry::activated));
34         entry.signal_key_press_event().connect (sigc::mem_fun (*this, &FloatingTextEntry::key_press));
35
36         add (entry);
37 }
38
39 void
40 FloatingTextEntry::on_realize ()
41 {
42         ArdourWindow::on_realize ();
43         get_window()->set_decorations (Gdk::WMDecoration (0));
44 }
45
46 void
47 FloatingTextEntry::activated ()
48 {
49         use_text (entry.get_text()); // EMIT SIGNAL
50         delete_when_idle (this);
51 }
52
53 bool
54 FloatingTextEntry::key_press (GdkEventKey* ev)
55 {
56         switch (ev->keyval) {
57         case GDK_Escape:
58                 delete_when_idle (this);
59                 return true;
60                 break;
61         default:
62                 break;
63         }
64         return false;
65 }
66
67 void
68 FloatingTextEntry::on_hide ()
69 {
70         /* No hide button is shown (no decoration on the window), 
71            so being hidden is equivalent to the Escape key or any other 
72            method of cancelling the edit.
73         */
74
75         delete_when_idle (this);
76         ArdourWindow::on_hide ();
77 }