more changes to broken-out tempo code
[ardour.git] / libs / widgets / 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
18 #include <string>
19 #include <climits>
20 #include <iostream>
21
22 #include "pbd/controllable.h"
23 #include "gtkmm2ext/gui_thread.h"
24 #include "gtkmm2ext/keyboard.h"
25 #include "widgets/binding_proxy.h"
26 #include "widgets/popup.h"
27
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace PBD;
32 using namespace Gtkmm2ext;
33 using namespace ArdourWidgets;
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         if (c) {
43                 c->DropReferences.connect (
44                                 _controllable_going_away_connection, invalidator (*this),
45                                 boost::bind (&BindingProxy::set_controllable, this, boost::shared_ptr<Controllable> ()),
46                                 gui_context());
47         }
48 }
49
50 BindingProxy::BindingProxy ()
51         : prompter (0)
52 {
53 }
54
55 BindingProxy::~BindingProxy ()
56 {
57         if (prompter) {
58                 delete prompter;
59         }
60 }
61
62 void
63 BindingProxy::set_controllable (boost::shared_ptr<Controllable> c)
64 {
65         learning_finished ();
66         controllable = c;
67
68         _controllable_going_away_connection.disconnect ();
69         if (c) {
70                 c->DropReferences.connect (
71                                 _controllable_going_away_connection, invalidator (*this),
72                                 boost::bind (&BindingProxy::set_controllable, this, boost::shared_ptr<Controllable> ()),
73                                 gui_context());
74         }
75 }
76
77 void
78 BindingProxy::set_bind_button_state (guint button, guint statemask)
79 {
80         bind_button = button;
81         bind_statemask = statemask;
82 }
83
84 bool
85 BindingProxy::is_bind_action (GdkEventButton *ev)
86 {
87         return (Keyboard::modifier_state_equals (ev->state, bind_statemask) && ev->button == bind_button );
88 }
89
90 bool
91 BindingProxy::button_press_handler (GdkEventButton *ev)
92 {
93         if ( controllable && is_bind_action(ev) ) {
94                 if (Controllable::StartLearning (controllable.get())) {
95                         string prompt = _("operate controller now");
96                         if (prompter == 0) {
97                                 prompter = new PopUp (Gtk::WIN_POS_MOUSE, 30000, false);
98                                 prompter->signal_unmap_event().connect (mem_fun (*this, &BindingProxy::prompter_hiding));
99                         }
100                         prompter->set_text (prompt);
101                         prompter->touch (); // shows popup
102                         controllable->LearningFinished.connect_same_thread (learning_connection, boost::bind (&BindingProxy::learning_finished, this));
103                 }
104                 return true;
105         }
106
107         return false;
108 }
109
110 void
111 BindingProxy::learning_finished ()
112 {
113         learning_connection.disconnect ();
114         if (prompter) {
115                 prompter->touch (); // hides popup
116         }
117 }
118
119 bool
120 BindingProxy::prompter_hiding (GdkEventAny* /*ev*/)
121 {
122         learning_connection.disconnect ();
123         if (controllable) {
124                 Controllable::StopLearning (controllable.get());
125         }
126         return false;
127 }