button hacks and more
[ardour.git] / libs / gtkmm2ext / bindable_button.cc
1 /*
2     Copyright (C) 2004 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 <midi++/controllable.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27 #include <gtkmm2ext/bindable_button.h>
28
29 #include "i18n.h"
30
31 using namespace Gtkmm2ext;
32 using namespace std;
33
34 BindableToggleButton::BindableToggleButton (MIDI::Controllable *mc)
35         : prompter (Gtk::WIN_POS_MOUSE, 30000, false),
36           midi_control (mc),
37           bind_button (2),
38           bind_statemask (Gdk::CONTROL_MASK)
39
40 {                         
41         init_events ();
42 }
43
44 BindableToggleButton::BindableToggleButton(MIDI::Controllable *mc, const string &label)
45         : ToggleButton (label),
46           prompter (Gtk::WIN_POS_MOUSE, 30000, false),
47           midi_control (mc),
48           bind_button (2),
49           bind_statemask (Gdk::CONTROL_MASK)
50 {                         
51         init_events ();
52 }
53
54
55 void
56 BindableToggleButton::init_events ()
57 {
58         prompter.signal_unmap_event().connect (mem_fun (*this, &BindableToggleButton::prompter_hiding));
59         
60         prompting = false;
61         unprompting = false;
62         
63         if (midi_control) {
64                 midi_control->learning_started.connect (mem_fun (*this, &BindableToggleButton::midicontrol_prompt));
65                 midi_control->learning_stopped.connect (mem_fun (*this, &BindableToggleButton::midicontrol_unprompt));
66         }
67 }
68
69 void
70 BindableToggleButton::set_bind_button_state (guint button, guint statemask)
71 {
72         bind_button = button;
73         bind_statemask = statemask;
74 }
75
76 void
77 BindableToggleButton::get_bind_button_state (guint &button, guint &statemask)
78 {
79         button = bind_button;
80         statemask = bind_statemask;
81 }
82
83 void
84 BindableToggleButton::midi_learn()
85 {
86         if (midi_control) {
87                 prompting = true;
88                 midi_control->learn_about_external_control ();
89         }
90 }
91
92 bool
93 BindableToggleButton::on_button_press_event (GdkEventButton *ev)
94 {
95         if ((ev->state & bind_statemask) && ev->button == bind_button) { 
96                 midi_learn ();
97                 return true;
98         }
99         
100         return false;
101 }
102
103 bool
104 BindableToggleButton::prompter_hiding (GdkEventAny *ev)
105 {
106         if (unprompting) {
107                 if (midi_control) {
108                         midi_control->stop_learning();
109                 }
110                 unprompting = false;
111         }
112         
113         return false;
114 }
115
116
117 void
118 BindableToggleButton::midicontrol_set_tip ()
119
120 {
121         if (midi_control) {
122                 // Gtkmm2ext::UI::instance()->set_tip (evbox, midi_control->control_description());
123         }
124 }
125
126 void
127 BindableToggleButton::midicontrol_prompt ()
128
129 {
130         if (prompting) {
131                 string prompt = _("operate MIDI controller now");
132                 prompter.set_text (prompt);
133                 Gtkmm2ext::UI::instance()->touch_display (&prompter);
134                 
135                 unprompting = true;
136                 prompting = false;
137         }
138 }
139
140 void
141 BindableToggleButton::midicontrol_unprompt ()
142
143 {
144         if (unprompting) {
145                 Gtkmm2ext::UI::instance()->touch_display (&prompter);
146                 unprompting = false;
147         }
148 }
149
150