Apply MIDI keyboard and "scroomer" patch.
[ardour.git] / gtk2_ardour / midi_time_axis.cc
1 /*
2     Copyright (C) 2000 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 #include <cstdlib>
20 #include <cmath>
21
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25
26 #include <sigc++/bind.h>
27
28 #include <pbd/error.h>
29 #include <pbd/stl_delete.h>
30 #include <pbd/whitespace.h>
31
32 #include <gtkmm2ext/gtk_ui.h>
33 #include <gtkmm2ext/selector.h>
34 #include <gtkmm2ext/stop_signal.h>
35 #include <gtkmm2ext/bindable_button.h>
36 #include <gtkmm2ext/utils.h>
37
38 #include <ardour/midi_playlist.h>
39 #include <ardour/midi_diskstream.h>
40 #include <ardour/processor.h>
41 #include <ardour/ladspa_plugin.h>
42 #include <ardour/location.h>
43 #include <ardour/playlist.h>
44 #include <ardour/session.h>
45 #include <ardour/session_playlist.h>
46 #include <ardour/utils.h>
47
48 #include "ardour_ui.h"
49 #include "midi_time_axis.h"
50 #include "automation_time_axis.h"
51 #include "automation_line.h"
52 #include "add_midi_cc_track_dialog.h"
53 #include "canvas_impl.h"
54 #include "crossfade_view.h"
55 #include "enums.h"
56 #include "gui_thread.h"
57 #include "keyboard.h"
58 #include "playlist_selector.h"
59 #include "plugin_selector.h"
60 #include "plugin_ui.h"
61 #include "point_selection.h"
62 #include "prompter.h"
63 #include "public_editor.h"
64 #include "region_view.h"
65 #include "rgb_macros.h"
66 #include "selection.h"
67 #include "simplerect.h"
68 #include "midi_streamview.h"
69 #include "utils.h"
70 #include "midi_scroomer.h"
71 #include "piano_roll_header.h"
72
73 #include <ardour/midi_track.h>
74
75 #include "i18n.h"
76
77 using namespace ARDOUR;
78 using namespace PBD;
79 using namespace Gtk;
80 using namespace Editing;
81
82
83 MidiTimeAxisView::MidiTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
84         : AxisView(sess) // FIXME: won't compile without this, why??
85         , RouteTimeAxisView(ed, sess, rt, canvas)
86         , _range_scroomer(0)
87         , _piano_roll_header(0)
88         , _note_mode(Sustained)
89         , _note_mode_item(NULL)
90         , _percussion_mode_item(NULL)
91 {
92         subplugin_menu.set_name ("ArdourContextMenu");
93
94         _view = new MidiStreamView (*this);
95
96         ignore_toggle = false;
97
98         mute_button->set_active (false);
99         solo_button->set_active (false);
100         
101         if (is_midi_track()) {
102                 controls_ebox.set_name ("MidiTimeAxisViewControlsBaseUnselected");
103                 _note_mode = midi_track()->note_mode();
104         } else { // MIDI bus (which doesn't exist yet..)
105                 controls_ebox.set_name ("MidiBusControlsBaseUnselected");
106         }
107
108         /* map current state of the route */
109
110         processors_changed ();
111
112         ensure_xml_node ();
113
114         set_state (*xml_node);
115         
116         _route->processors_changed.connect (mem_fun(*this, &MidiTimeAxisView::processors_changed));
117
118         if (is_track()) {
119                 _piano_roll_header = new PianoRollHeader(*midi_view());
120                 _range_scroomer = new MidiScroomer(midi_view()->note_range_adjustment);
121
122                 controls_hbox.pack_start(*_range_scroomer);
123                 controls_hbox.pack_start(*_piano_roll_header);
124
125                 controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
126                 controls_base_selected_name = "MidiTrackControlsBaseSelected";
127                 controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
128
129                 /* ask for notifications of any new RegionViews */
130                 _view->attach ();
131
132         }
133 }
134
135 MidiTimeAxisView::~MidiTimeAxisView ()
136 {
137 }
138
139 MidiStreamView*
140 MidiTimeAxisView::midi_view()
141 {
142         return dynamic_cast<MidiStreamView*>(_view);
143 }
144
145 guint32
146 MidiTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
147 {
148         ensure_xml_node ();
149         xml_node->add_property ("shown_editor", "yes");
150                 
151         return TimeAxisView::show_at (y, nth, parent);
152 }
153
154 void
155 MidiTimeAxisView::hide ()
156 {
157         ensure_xml_node ();
158         xml_node->add_property ("shown_editor", "no");
159
160         TimeAxisView::hide ();
161 }
162
163 void
164 MidiTimeAxisView::append_extra_display_menu_items ()
165 {
166         using namespace Menu_Helpers;
167
168         MenuList& items = display_menu->items();
169
170         // Note range
171         Menu *range_menu = manage(new Menu);
172         MenuList& range_items = range_menu->items();
173         range_menu->set_name ("ArdourContextMenu");
174         
175         RadioMenuItem::Group range_group;
176
177         range_items.push_back (RadioMenuElem (range_group, _("Show Full Range"), bind (
178                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
179                         MidiStreamView::FullRange)));
180         
181         range_items.push_back (RadioMenuElem (range_group, _("Fit Contents"), bind (
182                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
183                         MidiStreamView::ContentsRange)));
184
185         ((Gtk::CheckMenuItem&)range_items.back()).set_active(true);
186
187         items.push_back (MenuElem (_("Note range"), *range_menu));
188 }
189
190 void
191 MidiTimeAxisView::build_automation_action_menu ()
192 {
193         using namespace Menu_Helpers;
194
195         RouteTimeAxisView::build_automation_action_menu ();
196
197         MenuList& automation_items = automation_action_menu->items();
198         
199         automation_items.push_back (SeparatorElem());
200
201         automation_items.push_back (MenuElem (_("Controller..."), 
202                                                    mem_fun(*this, &MidiTimeAxisView::add_controller_track)));
203 }
204
205 Gtk::Menu*
206 MidiTimeAxisView::build_mode_menu()
207 {
208         using namespace Menu_Helpers;
209
210         Menu* mode_menu = manage (new Menu);
211         MenuList& items = mode_menu->items();
212         mode_menu->set_name ("ArdourContextMenu");
213
214         RadioMenuItem::Group mode_group;
215         items.push_back (RadioMenuElem (mode_group, _("Sustained"),
216                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Sustained)));
217         _note_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
218         _note_mode_item->set_active(_note_mode == Sustained);
219
220         items.push_back (RadioMenuElem (mode_group, _("Percussive"),
221                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Percussive)));
222         _percussion_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
223         _percussion_mode_item->set_active(_note_mode == Percussive);
224
225         return mode_menu;
226 }
227         
228 void
229 MidiTimeAxisView::set_note_mode(NoteMode mode)
230 {
231         if (_note_mode != mode) {
232                 _note_mode = mode;
233                 midi_track()->set_note_mode(mode);
234                 _view->redisplay_diskstream();
235         }
236 }
237
238
239 void
240 MidiTimeAxisView::set_note_range(MidiStreamView::VisibleNoteRange range)
241 {
242         //if (midi_view()->note_range() != range) {
243                 midi_view()->set_note_range(range);
244                 midi_view()->redisplay_diskstream();
245         //}
246 }
247
248
249 /** Prompt for a controller with a dialog and add an automation track for it
250  */
251 void
252 MidiTimeAxisView::add_controller_track()
253 {
254         int response;
255         Parameter param;
256
257         {
258                 AddMidiCCTrackDialog dialog;
259                 dialog.set_transient_for(editor);
260                 response = dialog.run();
261                 
262                 if (response == Gtk::RESPONSE_ACCEPT)
263                         param = dialog.parameter();
264         }
265
266         if (response == Gtk::RESPONSE_ACCEPT)
267                 create_automation_child(param, true);
268 }
269
270 void
271 MidiTimeAxisView::create_automation_child (Parameter param, bool show)
272 {
273         if (param.type() == MidiCCAutomation) {
274         
275                 /* FIXME: don't create AutomationList for track itself */
276
277                 boost::shared_ptr<AutomationControl> c = _route->control(param);
278
279                 if (!c) {
280                         boost::shared_ptr<AutomationList> al(new ARDOUR::AutomationList(param, 0, 127, 64));
281                         c = boost::shared_ptr<AutomationControl>(_route->control_factory(al));
282                         _route->add_control(c);
283                 }
284
285                 boost::shared_ptr<AutomationTimeAxisView> track(new AutomationTimeAxisView (_session,
286                                 _route, _route, c,
287                                 editor,
288                                 *this,
289                                 true,
290                                 parent_canvas,
291                                 _route->describe_parameter(param)));
292                 
293                 add_automation_child(param, track, show);
294
295         } else {
296                 error << "MidiTimeAxisView: unknown automation child " << param.to_string() << endmsg;
297         }
298 }
299
300 void
301 MidiTimeAxisView::route_active_changed ()
302 {
303         RouteUI::route_active_changed ();
304
305         if (is_track()) {
306                 if (_route->active()) {
307                         controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
308                         controls_base_selected_name = "MidiTrackControlsBaseSelected";
309                         controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
310                 } else {
311                         controls_ebox.set_name ("MidiTrackControlsBaseInactiveUnselected");
312                         controls_base_selected_name = "MidiTrackControlsBaseInactiveSelected";
313                         controls_base_unselected_name = "MidiTrackControlsBaseInactiveUnselected";
314                 }
315         } else {
316
317                 throw; // wha?
318                 
319                 if (_route->active()) {
320                         controls_ebox.set_name ("BusControlsBaseUnselected");
321                         controls_base_selected_name = "BusControlsBaseSelected";
322                         controls_base_unselected_name = "BusControlsBaseUnselected";
323                 } else {
324                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
325                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
326                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
327                 }
328         }
329 }