Can't call the wrong function when there's only one of them: remove ARDOUR::Parameter...
[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 #include "ghostregion.h"
73
74 #include <ardour/midi_track.h>
75
76 #include "i18n.h"
77
78 using namespace ARDOUR;
79 using namespace PBD;
80 using namespace Gtk;
81 using namespace sigc;
82 using namespace Editing;
83
84
85 MidiTimeAxisView::MidiTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
86         : AxisView(sess) // virtually inherited
87         , RouteTimeAxisView(ed, sess, rt, canvas)
88         , _ignore_signals(false)  
89         , _range_scroomer(0)
90         , _piano_roll_header(0)
91         , _note_mode(Sustained)
92         , _note_mode_item(NULL)
93         , _percussion_mode_item(NULL)
94         , _midi_expander("MIDI")
95 {
96         subplugin_menu.set_name ("ArdourContextMenu");
97
98         _view = new MidiStreamView (*this);
99
100         ignore_toggle = false;
101
102         mute_button->set_active (false);
103         solo_button->set_active (false);
104         
105         if (is_midi_track()) {
106                 controls_ebox.set_name ("MidiTimeAxisViewControlsBaseUnselected");
107                 _note_mode = midi_track()->note_mode();
108         } else { // MIDI bus (which doesn't exist yet..)
109                 controls_ebox.set_name ("MidiBusControlsBaseUnselected");
110         }
111
112         /* map current state of the route */
113
114         processors_changed ();
115
116         ensure_xml_node ();
117
118         set_state (*xml_node);
119         
120         _route->processors_changed.connect (mem_fun(*this, &MidiTimeAxisView::processors_changed));
121
122         if (is_track()) {
123                 _piano_roll_header = new PianoRollHeader(*midi_view());
124                 _range_scroomer = new MidiScroomer(midi_view()->note_range_adjustment);
125
126                 controls_hbox.pack_start(*_range_scroomer);
127                 controls_hbox.pack_start(*_piano_roll_header);
128
129                 controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
130                 controls_base_selected_name = "MidiTrackControlsBaseSelected";
131                 controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
132
133                 midi_view()->NoteRangeChanged.connect (mem_fun(*this, &MidiTimeAxisView::update_range));
134
135                 /* ask for notifications of any new RegionViews */
136                 _view->RegionViewAdded.connect (mem_fun(*this, &MidiTimeAxisView::region_view_added));
137                 _view->attach ();
138         }
139                 
140         // add channel selector expander
141         HBox *channel_selector_box = manage(new HBox());
142         channel_selector_box->pack_start(_channel_selector, SHRINK, 0);
143         _midi_expander.add(*channel_selector_box);
144         _midi_expander.property_expanded().signal_changed().connect(
145                         mem_fun(this, &MidiTimeAxisView::channel_selector_toggled));
146         controls_vbox.pack_end(_midi_expander, SHRINK, 0);
147         boost::shared_ptr<MidiDiskstream> diskstream = midi_track()->midi_diskstream();
148
149         // restore channel selector settings
150         _channel_selector.set_channel_mode(diskstream->get_channel_mode(), diskstream->get_channel_mask());
151         _channel_selector.mode_changed.connect(
152                 mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_channel_mode));
153
154 }
155
156 MidiTimeAxisView::~MidiTimeAxisView ()
157 {
158         delete _piano_roll_header;
159         _piano_roll_header = 0;
160
161         delete _range_scroomer;
162         _range_scroomer = 0;
163 }
164
165 MidiStreamView*
166 MidiTimeAxisView::midi_view()
167 {
168         return dynamic_cast<MidiStreamView*>(_view);
169 }
170
171 guint32
172 MidiTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
173 {
174         ensure_xml_node ();
175         xml_node->add_property ("shown_editor", "yes");
176                 
177         guint32 ret = TimeAxisView::show_at (y, nth, parent);
178         _piano_roll_header->show();
179         _range_scroomer->show();
180         return ret;
181 }
182
183 void
184 MidiTimeAxisView::hide ()
185 {
186         ensure_xml_node ();
187         xml_node->add_property ("shown_editor", "no");
188
189         TimeAxisView::hide ();
190 }
191
192 void
193 MidiTimeAxisView::set_height (uint32_t h)
194 {
195         RouteTimeAxisView::set_height (h);
196
197         if (height >= hLarger) {
198                 _midi_expander.show();
199                 if (is_track() && _range_scroomer)
200                         _range_scroomer->show();
201                 if (is_track() && _piano_roll_header)
202                         _piano_roll_header->show();
203
204         } else if (height >= hLarge) {
205                 _midi_expander.show();
206                 _midi_expander.set_expanded(FALSE);
207                 if (is_track() && _range_scroomer)
208                         _range_scroomer->show();
209                 if (is_track() && _piano_roll_header)
210                         _piano_roll_header->show();
211
212         } else if (height >= hNormal) {
213                 _midi_expander.show();
214                 _midi_expander.set_expanded(FALSE);
215                 if (is_track() && _range_scroomer)
216                         _range_scroomer->hide();
217                 if (is_track() && _piano_roll_header)
218                         _piano_roll_header->hide();
219         } else {
220                 _midi_expander.hide();
221                 _midi_expander.set_expanded(FALSE);
222                 if (is_track() && _range_scroomer)
223                         _range_scroomer->hide();
224                 if (is_track() && _piano_roll_header)
225                         _piano_roll_header->hide();
226         }
227 }
228
229 void
230 MidiTimeAxisView::append_extra_display_menu_items ()
231 {
232         using namespace Menu_Helpers;
233
234         MenuList& items = display_menu->items();
235
236         // Note range
237         Menu *range_menu = manage(new Menu);
238         MenuList& range_items = range_menu->items();
239         range_menu->set_name ("ArdourContextMenu");
240         
241         range_items.push_back (MenuElem (_("Show Full Range"), bind (
242                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
243                         MidiStreamView::FullRange)));
244         
245         range_items.push_back (MenuElem (_("Fit Contents"), bind (
246                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
247                         MidiStreamView::ContentsRange)));
248
249         items.push_back (MenuElem (_("Note range"), *range_menu));
250 }
251
252 void
253 MidiTimeAxisView::build_automation_action_menu ()
254 {
255         using namespace Menu_Helpers;
256
257         RouteTimeAxisView::build_automation_action_menu ();
258
259         MenuList& automation_items = automation_action_menu->items();
260         
261         automation_items.push_back (SeparatorElem());
262         automation_items.push_back (MenuElem (_("Controller..."), 
263                         mem_fun(*this, &MidiTimeAxisView::add_cc_track)));
264         automation_items.push_back (MenuElem (_("Bender"), 
265                         sigc::bind(mem_fun(*this, &MidiTimeAxisView::add_parameter_track),
266                                 Evoral::Parameter(MidiPitchBenderAutomation))));
267         automation_items.push_back (MenuElem (_("Pressure"), 
268                         sigc::bind(mem_fun(*this, &MidiTimeAxisView::add_parameter_track),
269                                 Evoral::Parameter(MidiChannelPressureAutomation))));
270
271 }
272
273 Gtk::Menu*
274 MidiTimeAxisView::build_mode_menu()
275 {
276         using namespace Menu_Helpers;
277
278         Menu* mode_menu = manage (new Menu);
279         MenuList& items = mode_menu->items();
280         mode_menu->set_name ("ArdourContextMenu");
281
282         RadioMenuItem::Group mode_group;
283         items.push_back (RadioMenuElem (mode_group, _("Sustained"),
284                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Sustained)));
285         _note_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
286         _note_mode_item->set_active(_note_mode == Sustained);
287
288         items.push_back (RadioMenuElem (mode_group, _("Percussive"),
289                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Percussive)));
290         _percussion_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
291         _percussion_mode_item->set_active(_note_mode == Percussive);
292
293         return mode_menu;
294 }
295         
296 void
297 MidiTimeAxisView::set_note_mode(NoteMode mode)
298 {
299         if (_note_mode != mode || midi_track()->note_mode() != mode) {
300                 _note_mode = mode;
301                 midi_track()->set_note_mode(mode);
302                 _view->redisplay_diskstream();
303         }
304 }
305
306
307 void
308 MidiTimeAxisView::set_note_range(MidiStreamView::VisibleNoteRange range)
309 {
310         if (!_ignore_signals)
311                 midi_view()->set_note_range(range);
312 }
313
314
315 void
316 MidiTimeAxisView::update_range()
317 {
318         MidiGhostRegion* mgr;
319
320         for(list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
321                 if ((mgr = dynamic_cast<MidiGhostRegion*>(*i)) != 0) {
322                         mgr->update_range();
323                 }
324         }
325 }
326
327 void
328 MidiTimeAxisView::show_all_automation ()
329 {
330         if (midi_track()) {
331                 const set<Evoral::Parameter> params = midi_track()->midi_diskstream()->
332                                 midi_playlist()->contained_automation();
333
334                 for (set<Evoral::Parameter>::const_iterator i = params.begin(); i != params.end(); ++i) {
335                         create_automation_child(*i, true);
336                 }
337         }
338
339         RouteTimeAxisView::show_all_automation ();
340 }
341
342 void
343 MidiTimeAxisView::show_existing_automation ()
344 {
345         if (midi_track()) {
346                 const set<Evoral::Parameter> params = midi_track()->midi_diskstream()->
347                                 midi_playlist()->contained_automation();
348
349                 for (set<Evoral::Parameter>::const_iterator i = params.begin(); i != params.end(); ++i) {
350                         create_automation_child(*i, true);
351                 }
352         }
353
354         RouteTimeAxisView::show_existing_automation ();
355 }
356
357 /** Prompt for a controller with a dialog and add an automation track for it
358  */
359 void
360 MidiTimeAxisView::add_cc_track()
361 {
362         int response;
363         Evoral::Parameter param(0, 0, 0);
364
365         {
366                 AddMidiCCTrackDialog dialog;
367                 dialog.set_transient_for(editor);
368                 response = dialog.run();
369                 
370                 if (response == Gtk::RESPONSE_ACCEPT)
371                         param = dialog.parameter();
372         }
373
374         if (param.type() != 0 && response == Gtk::RESPONSE_ACCEPT)
375                 create_automation_child(param, true);
376 }
377
378
379 /** Add an automation track for the given parameter (pitch bend, channel pressure).
380  */
381 void
382 MidiTimeAxisView::add_parameter_track(const Evoral::Parameter& param)
383 {
384         create_automation_child(param, true);
385 }
386
387 void
388 MidiTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
389 {
390         if (    param.type() == MidiCCAutomation ||
391                         param.type() == MidiPgmChangeAutomation ||
392                         param.type() == MidiPitchBenderAutomation ||
393                         param.type() == MidiChannelPressureAutomation
394            ) {
395         
396                 /* These controllers are region "automation", so we do not create
397                  * an AutomationList/Line for the track */
398
399                 AutomationTracks::iterator existing = _automation_tracks.find(param);
400                 if (existing != _automation_tracks.end())
401                         return;
402
403                 boost::shared_ptr<AutomationControl> c
404                         = boost::dynamic_pointer_cast<AutomationControl>(_route->data().control(param));
405
406                 if (!c) {
407                         c = boost::dynamic_pointer_cast<AutomationControl>(_route->control_factory(param));
408                         _route->add_control(c);
409                 }
410
411                 boost::shared_ptr<AutomationTimeAxisView> track(new AutomationTimeAxisView (_session,
412                                 _route, boost::shared_ptr<ARDOUR::Automatable>(), c,
413                                 editor,
414                                 *this,
415                                 true,
416                                 parent_canvas,
417                                 _route->describe_parameter(param)));
418                 
419                 add_automation_child(param, track, show);
420
421         } else {
422                 error << "MidiTimeAxisView: unknown automation child "
423                         << ARDOUR::EventTypeMap::instance().to_symbol(param) << endmsg;
424         }
425 }
426
427 void
428 MidiTimeAxisView::route_active_changed ()
429 {
430         RouteUI::route_active_changed ();
431
432         if (is_track()) {
433                 if (_route->active()) {
434                         controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
435                         controls_base_selected_name = "MidiTrackControlsBaseSelected";
436                         controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
437                 } else {
438                         controls_ebox.set_name ("MidiTrackControlsBaseInactiveUnselected");
439                         controls_base_selected_name = "MidiTrackControlsBaseInactiveSelected";
440                         controls_base_unselected_name = "MidiTrackControlsBaseInactiveUnselected";
441                 }
442         } else {
443
444                 throw; // wha?
445                 
446                 if (_route->active()) {
447                         controls_ebox.set_name ("BusControlsBaseUnselected");
448                         controls_base_selected_name = "BusControlsBaseSelected";
449                         controls_base_unselected_name = "BusControlsBaseUnselected";
450                 } else {
451                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
452                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
453                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
454                 }
455         }
456 }
457
458 void 
459 MidiTimeAxisView::channel_selector_toggled()
460 {
461         static uint32_t previous_height;
462         
463         if(_midi_expander.property_expanded()) {
464
465                 previous_height = current_height();
466
467                 if (previous_height < TimeAxisView::hLargest) {
468                         set_height (TimeAxisView::hLarge);
469                 }
470
471         } else {
472
473                 set_height (previous_height);
474         }
475 }
476
477
478