Add control-focus notifications from bindable widgets.
[ardour.git] / libs / gtkmm2ext / binding_proxy.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17     $Id$
18 */
19
20 #include <string>
21 #include <climits>
22 #include <iostream>
23
24 #include <pbd/controllable.h>
25
26 #include <gtkmm2ext/binding_proxy.h>
27 #include <gtkmm2ext/keyboard.h>
28
29 #include "pbd/i18n.h"
30
31 using namespace Gtkmm2ext;
32 using namespace std;
33 using namespace PBD;
34
35 guint BindingProxy::bind_button = 2;
36 guint BindingProxy::bind_statemask = Gdk::CONTROL_MASK;
37
38 BindingProxy::BindingProxy (boost::shared_ptr<Controllable> c)
39         : prompter (0),
40           controllable (c)
41 {
42 }
43
44 BindingProxy::BindingProxy ()
45         : prompter (0)
46 {
47 }
48
49 BindingProxy::~BindingProxy ()
50 {
51         if (prompter) {
52                 delete prompter;
53         }
54 }
55
56 void
57 BindingProxy::set_controllable (boost::shared_ptr<Controllable> c)
58 {
59         learning_finished ();
60         controllable = c;
61 }
62
63 void
64 BindingProxy::set_bind_button_state (guint button, guint statemask)
65 {
66         bind_button = button;
67         bind_statemask = statemask;
68 }
69
70 bool
71 BindingProxy::is_bind_action (GdkEventButton *ev)
72 {
73         return (Keyboard::modifier_state_equals (ev->state, bind_statemask) && ev->button == bind_button );
74 }
75
76
77 bool
78 BindingProxy::button_press_handler (GdkEventButton *ev)
79 {
80         if ( controllable && is_bind_action(ev) ) {
81                 if (Controllable::StartLearning (controllable.get())) {
82                         string prompt = _("operate controller now");
83                         if (prompter == 0) {
84                                 prompter = new PopUp (Gtk::WIN_POS_MOUSE, 30000, false);
85                                 prompter->signal_unmap_event().connect (mem_fun (*this, &BindingProxy::prompter_hiding));
86                         }
87                         prompter->set_text (prompt);
88                         prompter->touch (); // shows popup
89                         controllable->LearningFinished.connect_same_thread (learning_connection, boost::bind (&BindingProxy::learning_finished, this));
90                 }
91                 return true;
92         }
93
94         return false;
95 }
96
97 void
98 BindingProxy::learning_finished ()
99 {
100         learning_connection.disconnect ();
101         if (prompter) {
102                 prompter->touch (); // hides popup
103         }
104 }
105
106
107 bool
108 BindingProxy::prompter_hiding (GdkEventAny* /*ev*/)
109 {
110         learning_connection.disconnect ();
111         if (controllable) {
112                 Controllable::StopLearning (controllable.get());
113         }
114         return false;
115 }
116