screw up MIDI control "automation" tracks quite a bit while trying to improve menu...
[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 #include "pbd/basename.h"
32 #include "pbd/enumwriter.h"
33 #include "pbd/memento_command.h"
34
35 #include <gtkmm2ext/gtk_ui.h>
36 #include <gtkmm2ext/selector.h>
37 #include <gtkmm2ext/stop_signal.h>
38 #include <gtkmm2ext/bindable_button.h>
39 #include <gtkmm2ext/utils.h>
40
41 #include "ardour/midi_playlist.h"
42 #include "ardour/midi_diskstream.h"
43 #include "ardour/midi_patch_manager.h"
44 #include "ardour/midi_source.h"
45 #include "ardour/processor.h"
46 #include "ardour/ladspa_plugin.h"
47 #include "ardour/location.h"
48 #include "ardour/playlist.h"
49 #include "ardour/region_factory.h"
50 #include "ardour/session.h"
51 #include "ardour/session_playlist.h"
52 #include "ardour/tempo.h"
53 #include "ardour/utils.h"
54
55 #include "add_midi_cc_track_dialog.h"
56 #include "ardour_ui.h"
57 #include "automation_line.h"
58 #include "automation_time_axis.h"
59 #include "canvas-note-event.h"
60 #include "canvas_impl.h"
61 #include "crossfade_view.h"
62 #include "editor.h"
63 #include "enums.h"
64 #include "ghostregion.h"
65 #include "gui_thread.h"
66 #include "keyboard.h"
67 #include "midi_scroomer.h"
68 #include "midi_streamview.h"
69 #include "midi_region_view.h"
70 #include "midi_time_axis.h"
71 #include "piano_roll_header.h"
72 #include "playlist_selector.h"
73 #include "plugin_selector.h"
74 #include "plugin_ui.h"
75 #include "point_selection.h"
76 #include "prompter.h"
77 #include "region_view.h"
78 #include "rgb_macros.h"
79 #include "selection.h"
80 #include "simplerect.h"
81 #include "utils.h"
82
83 #include "ardour/midi_track.h"
84
85 #include "i18n.h"
86
87 using namespace ARDOUR;
88 using namespace PBD;
89 using namespace Gtk;
90 using namespace Editing;
91
92 // Minimum height at which a control is displayed
93 static const uint32_t MIDI_CONTROLS_BOX_MIN_HEIGHT = 162;
94 static const uint32_t KEYBOARD_MIN_HEIGHT = 140;
95
96 MidiTimeAxisView::MidiTimeAxisView (PublicEditor& ed, Session* sess,
97                 boost::shared_ptr<Route> rt, Canvas& canvas)
98         : AxisView(sess) // virtually inherited
99         , RouteTimeAxisView(ed, sess, rt, canvas)
100         , _ignore_signals(false)
101         , _range_scroomer(0)
102         , _piano_roll_header(0)
103         , _note_mode(Sustained)
104         , _note_mode_item(0)
105         , _percussion_mode_item(0)
106         , _color_mode(MeterColors)
107         , _meter_color_mode_item(0)
108         , _channel_color_mode_item(0)
109         , _track_color_mode_item(0)
110         , _step_edit_item (0)
111         , _midi_thru_item (0)
112         , default_channel_menu (0)
113 {
114         subplugin_menu.set_name ("ArdourContextMenu");
115
116         _view = new MidiStreamView (*this);
117
118         ignore_toggle = false;
119         _ignore_toggle_parameter = false;
120         
121         mute_button->set_active (false);
122         solo_button->set_active (false);
123
124         step_edit_insert_position = 0;
125
126         if (is_midi_track()) {
127                 controls_ebox.set_name ("MidiTimeAxisViewControlsBaseUnselected");
128                 _note_mode = midi_track()->note_mode();
129         } else { // MIDI bus (which doesn't exist yet..)
130                 controls_ebox.set_name ("MidiBusControlsBaseUnselected");
131         }
132
133         /* map current state of the route */
134
135         processors_changed (RouteProcessorChange ());
136
137         ensure_xml_node ();
138
139         set_state (*xml_node, Stateful::loading_state_version);
140
141         _route->processors_changed.connect (*this, ui_bind (&MidiTimeAxisView::processors_changed, this, _1), gui_context());
142
143         if (is_track()) {
144                 _piano_roll_header = new PianoRollHeader(*midi_view());
145                 _range_scroomer = new MidiScroomer(midi_view()->note_range_adjustment);
146
147                 controls_hbox.pack_start(*_range_scroomer);
148                 controls_hbox.pack_start(*_piano_roll_header);
149
150                 controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
151                 controls_base_selected_name = "MidiTrackControlsBaseSelected";
152                 controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
153
154                 midi_view()->NoteRangeChanged.connect (sigc::mem_fun(*this, &MidiTimeAxisView::update_range));
155
156                 /* ask for notifications of any new RegionViews */
157                 _view->RegionViewAdded.connect (sigc::mem_fun(*this, &MidiTimeAxisView::region_view_added));
158                 _view->attach ();
159         }
160
161         HBox* midi_controls_hbox = manage(new HBox());
162
163         MIDI::Name::MidiPatchManager& patch_manager = MIDI::Name::MidiPatchManager::instance();
164
165         MIDI::Name::MasterDeviceNames::Models::const_iterator m = patch_manager.all_models().begin();
166         for (; m != patch_manager.all_models().end(); ++m) {
167                 _model_selector.append_text(m->c_str());
168         }
169
170         _model_selector.signal_changed().connect(sigc::mem_fun(*this, &MidiTimeAxisView::model_changed));
171
172         _custom_device_mode_selector.signal_changed().connect(
173                         sigc::mem_fun(*this, &MidiTimeAxisView::custom_device_mode_changed));
174
175         // TODO: persist the choice
176         // this initializes the comboboxes and sends out the signal
177         _model_selector.set_active(0);
178
179         midi_controls_hbox->pack_start(_channel_selector, true, false);
180         if (!patch_manager.all_models().empty()) {
181                 _midi_controls_box.pack_start(_model_selector, true, false);
182                 _midi_controls_box.pack_start(_custom_device_mode_selector, true, false);
183         }
184
185         _midi_controls_box.pack_start(*midi_controls_hbox, true, true);
186
187         controls_vbox.pack_start(_midi_controls_box, false, false);
188
189         boost::shared_ptr<MidiDiskstream> diskstream = midi_track()->midi_diskstream();
190
191         // restore channel selector settings
192         _channel_selector.set_channel_mode(diskstream->get_channel_mode(),
193                         diskstream->get_channel_mask());
194         _channel_selector.mode_changed.connect(
195                 sigc::mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_channel_mode));
196
197         XMLProperty *prop;
198         if ((prop = xml_node->property ("color-mode")) != 0) {
199                 _color_mode = ColorMode (string_2_enum(prop->value(), _color_mode));
200                 if (_color_mode == ChannelColors) {
201                         _channel_selector.set_channel_colors(CanvasNoteEvent::midi_channel_colors);
202                 }
203         }
204
205         if ((prop = xml_node->property ("note-mode")) != 0) {
206                 _note_mode = NoteMode (string_2_enum(prop->value(), _note_mode));
207                 if (_percussion_mode_item) {
208                         _percussion_mode_item->set_active (_note_mode == Percussive);
209                 }
210         }
211 }
212
213 MidiTimeAxisView::~MidiTimeAxisView ()
214 {
215         delete _piano_roll_header;
216         _piano_roll_header = 0;
217
218         delete _range_scroomer;
219         _range_scroomer = 0;
220 }
221
222 void MidiTimeAxisView::model_changed()
223 {
224         std::list<std::string> device_modes = MIDI::Name::MidiPatchManager::instance()
225                 .custom_device_mode_names_by_model(_model_selector.get_active_text());
226
227         _custom_device_mode_selector.clear_items();
228
229         for (std::list<std::string>::const_iterator i = device_modes.begin();
230                         i != device_modes.end(); ++i) {
231                 cerr << "found custom device mode " << *i << " thread_id: " << pthread_self() << endl;
232                 _custom_device_mode_selector.append_text(*i);
233         }
234
235         _custom_device_mode_selector.set_active(0);
236 }
237
238 void MidiTimeAxisView::custom_device_mode_changed()
239 {
240         _midi_patch_settings_changed.emit(_model_selector.get_active_text(),
241                         _custom_device_mode_selector.get_active_text());
242 }
243
244 MidiStreamView*
245 MidiTimeAxisView::midi_view()
246 {
247         return dynamic_cast<MidiStreamView*>(_view);
248 }
249
250 guint32
251 MidiTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
252 {
253         ensure_xml_node ();
254         xml_node->add_property ("shown-editor", "yes");
255
256         guint32 ret = TimeAxisView::show_at (y, nth, parent);
257         return ret;
258 }
259
260 void
261 MidiTimeAxisView::hide ()
262 {
263         ensure_xml_node ();
264         xml_node->add_property ("shown-editor", "no");
265
266         TimeAxisView::hide ();
267 }
268
269 void
270 MidiTimeAxisView::set_height (uint32_t h)
271 {
272         RouteTimeAxisView::set_height (h);
273
274         if (height >= MIDI_CONTROLS_BOX_MIN_HEIGHT) {
275                 _midi_controls_box.show();
276         } else {
277                 _midi_controls_box.hide();
278         }
279
280         if (height >= KEYBOARD_MIN_HEIGHT) {
281                 if (is_track() && _range_scroomer)
282                         _range_scroomer->show();
283                 if (is_track() && _piano_roll_header)
284                         _piano_roll_header->show();
285         } else {
286                 if (is_track() && _range_scroomer)
287                         _range_scroomer->hide();
288                 if (is_track() && _piano_roll_header)
289                         _piano_roll_header->hide();
290         }
291 }
292
293 void
294 MidiTimeAxisView::append_extra_display_menu_items ()
295 {
296         using namespace Menu_Helpers;
297
298         MenuList& items = display_menu->items();
299
300         // Note range
301         Menu *range_menu = manage(new Menu);
302         MenuList& range_items = range_menu->items();
303         range_menu->set_name ("ArdourContextMenu");
304
305         range_items.push_back (MenuElem (_("Show Full Range"), sigc::bind (
306                         sigc::mem_fun(*this, &MidiTimeAxisView::set_note_range),
307                         MidiStreamView::FullRange)));
308
309         range_items.push_back (MenuElem (_("Fit Contents"), sigc::bind (
310                         sigc::mem_fun(*this, &MidiTimeAxisView::set_note_range),
311                         MidiStreamView::ContentsRange)));
312
313         items.push_back (MenuElem (_("Note range"), *range_menu));
314         items.push_back (MenuElem (_("Note mode"), *build_note_mode_menu()));
315         items.push_back (MenuElem (_("Default Channel"), *build_def_channel_menu()));
316
317         items.push_back (CheckMenuElem (_("MIDI Thru"), sigc::mem_fun(*this, &MidiTimeAxisView::toggle_midi_thru)));
318         _midi_thru_item = dynamic_cast<CheckMenuItem*>(&items.back());
319 }
320
321 Gtk::Menu*
322 MidiTimeAxisView::build_def_channel_menu ()
323 {
324         using namespace Menu_Helpers;
325
326         default_channel_menu = manage (new Menu ());
327
328         uint8_t defchn = midi_track()->default_channel();
329         MenuList& def_channel_items = default_channel_menu->items();
330         RadioMenuItem* item;
331         RadioMenuItem::Group dc_group;
332
333         for (int i = 0; i < 16; ++i) {
334                 char buf[4];
335                 snprintf (buf, sizeof (buf), "%d", i+1);
336
337                 def_channel_items.push_back (RadioMenuElem (dc_group, buf,
338                                                             sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::set_default_channel), i)));
339                 item = dynamic_cast<RadioMenuItem*>(&def_channel_items.back());
340                 item->set_active ((i == defchn));
341         }
342
343         return default_channel_menu;
344 }
345
346 void
347 MidiTimeAxisView::set_default_channel (int chn)
348 {
349         midi_track()->set_default_channel (chn);
350 }
351
352 void
353 MidiTimeAxisView::toggle_midi_thru ()
354 {
355         if (!_midi_thru_item) {
356                 return;
357         }
358
359         bool view_yn = _midi_thru_item->get_active();
360         if (view_yn != midi_track()->midi_thru()) {
361                 midi_track()->set_midi_thru (view_yn);
362         }
363 }
364
365 void
366 MidiTimeAxisView::build_automation_action_menu ()
367 {
368         using namespace Menu_Helpers;
369
370         RouteTimeAxisView::build_automation_action_menu ();
371
372         MenuList& automation_items = automation_action_menu->items();
373
374         automation_items.push_back (SeparatorElem());
375         automation_items.push_back (MenuElem (_("Show Controller..."),
376                         sigc::mem_fun(*this, &MidiTimeAxisView::add_cc_track)));
377
378         add_basic_parameter_menu_item (automation_items, _("Program Change"), Evoral::Parameter (MidiPgmChangeAutomation));
379         add_basic_parameter_menu_item (automation_items, _("Bender"), Evoral::Parameter (MidiPitchBenderAutomation));
380         add_basic_parameter_menu_item (automation_items, _("Pressure"), Evoral::Parameter (MidiChannelPressureAutomation));
381 }
382
383 void
384 MidiTimeAxisView::add_basic_parameter_menu_item (Menu_Helpers::MenuList& items, const string& label, Evoral::Parameter param)
385 {
386         items.push_back (Menu_Helpers::CheckMenuElem 
387                          (label, sigc::bind (sigc::mem_fun 
388                                              (*this, &MidiTimeAxisView::toggle_parameter_track), param)));
389         
390         // cerr << "Create a new menu item from " << param.type() << '/' << param.id() << '/' << (int) param.channel() << endl;
391
392         uint16_t selected_channels = _channel_selector.get_selected_channels();
393         bool visible = false;
394
395         for (uint8_t i = 0; i < 16; i++) {
396                 if (selected_channels & (0x0001 << i)) {
397                         Evoral::Parameter param_with_channel(param.type(), i, param.id());
398
399                         // cerr << "\tChecking on channel " << (int) i << " via " << param_with_channel.type() << '/' << param_with_channel.id() << endl;
400                         
401                         RouteAutomationNode* node = automation_track (param_with_channel);
402
403                         if (node) {
404                                 if (node->track->marked_for_display()) {
405                                         visible = true;
406                                         // cerr << "\tGot a track, and it appears visible\n";
407                                         break;
408                                 } else {
409                                         // cerr << "\tGot a track, and it appears hidden\n";
410                                 }
411                         } else {
412                                 // cerr << "\tno track found\n";
413                         }
414                 }
415         }
416
417         CheckMenuItem* cmi = static_cast<CheckMenuItem*>(&items.back());
418         cmi->set_active (visible);
419
420         pair<ParameterMenuMap::iterator,bool> result;
421         result = parameter_menu_map.insert (pair<Evoral::Parameter,CheckMenuItem*> (param, cmi));
422         if (!result.second) {
423                 /* it already exists, but we're asssigning a new menu item to it */
424                 result.first->second = cmi;
425         }
426 }
427
428 Gtk::Menu*
429 MidiTimeAxisView::build_note_mode_menu()
430 {
431         using namespace Menu_Helpers;
432
433         Menu* mode_menu = manage (new Menu);
434         MenuList& items = mode_menu->items();
435         mode_menu->set_name ("ArdourContextMenu");
436
437         RadioMenuItem::Group mode_group;
438         items.push_back (RadioMenuElem (mode_group, _("Sustained"),
439                                 sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::set_note_mode), Sustained)));
440         _note_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
441         _note_mode_item->set_active(_note_mode == Sustained);
442
443         items.push_back (RadioMenuElem (mode_group, _("Percussive"),
444                                 sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::set_note_mode), Percussive)));
445         _percussion_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
446         _percussion_mode_item->set_active(_note_mode == Percussive);
447
448         return mode_menu;
449 }
450
451 Gtk::Menu*
452 MidiTimeAxisView::build_color_mode_menu()
453 {
454         using namespace Menu_Helpers;
455
456         Menu* mode_menu = manage (new Menu);
457         MenuList& items = mode_menu->items();
458         mode_menu->set_name ("ArdourContextMenu");
459
460         RadioMenuItem::Group mode_group;
461         items.push_back (RadioMenuElem (mode_group, _("Meter Colors"),
462                                 sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::set_color_mode), MeterColors)));
463         _meter_color_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
464         _meter_color_mode_item->set_active(_color_mode == MeterColors);
465
466         items.push_back (RadioMenuElem (mode_group, _("Channel Colors"),
467                                 sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::set_color_mode), ChannelColors)));
468         _channel_color_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
469         _channel_color_mode_item->set_active(_color_mode == ChannelColors);
470
471         items.push_back (RadioMenuElem (mode_group, _("Track Color"),
472                                 sigc::bind (sigc::mem_fun (*this, &MidiTimeAxisView::set_color_mode), TrackColor)));
473         _channel_color_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
474         _channel_color_mode_item->set_active(_color_mode == TrackColor);
475
476         return mode_menu;
477 }
478
479 void
480 MidiTimeAxisView::set_note_mode(NoteMode mode)
481 {
482         if (_note_mode != mode || midi_track()->note_mode() != mode) {
483                 _note_mode = mode;
484                 midi_track()->set_note_mode(mode);
485                 xml_node->add_property ("note-mode", enum_2_string(_note_mode));
486                 _view->redisplay_diskstream();
487         }
488 }
489
490 void
491 MidiTimeAxisView::set_color_mode(ColorMode mode)
492 {
493         if (_color_mode != mode) {
494                 if (mode == ChannelColors) {
495                         _channel_selector.set_channel_colors(CanvasNoteEvent::midi_channel_colors);
496                 } else {
497                         _channel_selector.set_default_channel_color();
498                 }
499
500                 _color_mode = mode;
501                 xml_node->add_property ("color-mode", enum_2_string(_color_mode));
502                 _view->redisplay_diskstream();
503         }
504 }
505
506 void
507 MidiTimeAxisView::set_note_range(MidiStreamView::VisibleNoteRange range)
508 {
509         if (!_ignore_signals)
510                 midi_view()->set_note_range(range);
511 }
512
513
514 void
515 MidiTimeAxisView::update_range()
516 {
517         MidiGhostRegion* mgr;
518
519         for(list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
520                 if ((mgr = dynamic_cast<MidiGhostRegion*>(*i)) != 0) {
521                         mgr->update_range();
522                 }
523         }
524 }
525
526 void
527 MidiTimeAxisView::show_all_automation ()
528 {
529         if (midi_track()) {
530                 const set<Evoral::Parameter> params = midi_track()->midi_diskstream()->
531                                 midi_playlist()->contained_automation();
532
533                 for (set<Evoral::Parameter>::const_iterator i = params.begin(); i != params.end(); ++i) {
534                         create_automation_child(*i, true);
535                 }
536         }
537
538         RouteTimeAxisView::show_all_automation ();
539 }
540
541 void
542 MidiTimeAxisView::show_existing_automation ()
543 {
544         if (midi_track()) {
545                 const set<Evoral::Parameter> params = midi_track()->midi_diskstream()->
546                                 midi_playlist()->contained_automation();
547
548                 for (set<Evoral::Parameter>::const_iterator i = params.begin(); i != params.end(); ++i) {
549                         create_automation_child(*i, true);
550                 }
551         }
552
553         RouteTimeAxisView::show_existing_automation ();
554 }
555
556 /** Prompt for a controller with a dialog and add an automation track for it
557  */
558 void
559 MidiTimeAxisView::add_cc_track()
560 {
561         int response;
562         Evoral::Parameter param(0, 0, 0);
563
564         {
565                 AddMidiCCTrackDialog dialog;
566                 dialog.set_transient_for (_editor);
567                 response = dialog.run();
568
569                 if (response == Gtk::RESPONSE_ACCEPT)
570                         param = dialog.parameter();
571         }
572
573         if (param.type() != 0 && response == Gtk::RESPONSE_ACCEPT)
574                 create_automation_child(param, true);
575 }
576
577 /** Toggle an automation track for the given parameter (pitch bend, channel pressure).
578  *  Will add track if necessary.
579  */
580 void
581 MidiTimeAxisView::toggle_parameter_track(const Evoral::Parameter& param)
582 {
583         if (_ignore_toggle_parameter) {
584                 return;
585         }
586
587         cerr << "CHANGE VISIBILITY OF " << param.type() << '/' << param.id() << '/' << (int) param.channel() << endl;
588
589         if ( !EventTypeMap::instance().is_midi_parameter(param) ) {
590                 error << "MidiTimeAxisView: unknown automation child "
591                         << ARDOUR::EventTypeMap::instance().to_symbol(param) << endmsg;
592                 return;
593         }
594
595         map<Evoral::Parameter,CheckMenuItem*>::iterator x = parameter_menu_map.find (param);
596         if (x == parameter_menu_map.end()) {
597                 cerr << "Param not found in pm map\n";
598                 return;
599         }
600
601         bool yn = x->second->get_active ();
602         cerr << "Menu item state for " << param.type() << '/' << param.id() << '/' << (int) param.channel() << ' ' << yn << endl;
603
604         cerr << "toggle param " << param.type() << '/' << param.id() << '/' << (int) param.channel() << " from " << !yn << " to " << yn << endl;
605
606         // create the parameter lane for each selected channel
607         uint16_t selected_channels = _channel_selector.get_selected_channels();
608
609         for (uint8_t i = 0; i < 16; i++) {
610                 if (selected_channels & (0x0001 << i)) {
611                         Evoral::Parameter param_with_channel(param.type(), i, param.id());
612
613                         RouteAutomationNode* node = automation_track (param_with_channel);
614                         
615                         if (!node) {
616                                 cerr << "\tNO EXISTING TRACK FOR chn " << (int) i << endl;
617                                 if (yn) {
618                                         create_automation_child (param_with_channel, true);
619                                 }
620                         } else {
621                                 cerr << "\tTRACK EXISTS, set its menu item to " << yn << " to change its visibilty\n";
622                                 node->menu_item->set_active (yn);
623                         }
624                 }
625         }
626
627         _ignore_toggle_parameter = true;
628         x->second->set_active (yn);
629         _ignore_toggle_parameter = false;
630 }
631
632 /** Hide an automation track for the given parameter (pitch bend, channel pressure).
633  */
634 void
635 MidiTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
636 {
637         /* These controllers are region "automation", so we do not create
638          * an AutomationList/Line for the track */
639
640         if (param.type() == NullAutomation) {
641                 cerr << "WARNING: Attempt to create NullAutomation child, ignoring" << endl;
642                 return;
643         }
644
645         AutomationTracks::iterator existing = _automation_tracks.find(param);
646         if (existing != _automation_tracks.end())
647                 return;
648
649         boost::shared_ptr<AutomationControl> c = _route->get_control (param);
650
651         assert(c);
652
653         boost::shared_ptr<AutomationTimeAxisView> track(new AutomationTimeAxisView (_session,
654                         _route, boost::shared_ptr<ARDOUR::Automatable>(), c,
655                         _editor,
656                         *this,
657                         true,
658                         parent_canvas,
659                         _route->describe_parameter(param)));
660
661         add_automation_child(param, track, show);
662 }
663
664
665 void
666 MidiTimeAxisView::route_active_changed ()
667 {
668         RouteUI::route_active_changed ();
669
670         if (is_track()) {
671                 if (_route->active()) {
672                         controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
673                         controls_base_selected_name = "MidiTrackControlsBaseSelected";
674                         controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
675                 } else {
676                         controls_ebox.set_name ("MidiTrackControlsBaseInactiveUnselected");
677                         controls_base_selected_name = "MidiTrackControlsBaseInactiveSelected";
678                         controls_base_unselected_name = "MidiTrackControlsBaseInactiveUnselected";
679                 }
680         } else {
681
682                 throw; // wha?
683
684                 if (_route->active()) {
685                         controls_ebox.set_name ("BusControlsBaseUnselected");
686                         controls_base_selected_name = "BusControlsBaseSelected";
687                         controls_base_unselected_name = "BusControlsBaseUnselected";
688                 } else {
689                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
690                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
691                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
692                 }
693         }
694 }
695
696 void
697 MidiTimeAxisView::start_step_editing ()
698 {
699         step_edit_insert_position = _editor.get_preferred_edit_position ();
700         step_edit_beat_pos = 0;
701         step_edit_region = playlist()->top_region_at (step_edit_insert_position);
702
703         if (step_edit_region) {
704                 RegionView* rv = view()->find_view (step_edit_region);
705                 step_edit_region_view = dynamic_cast<MidiRegionView*> (rv);
706         } else {
707                 step_edit_region_view = 0;
708         }
709
710         midi_track()->set_step_editing (true);
711 }
712
713 void
714 MidiTimeAxisView::stop_step_editing ()
715 {
716         midi_track()->set_step_editing (false);
717 }
718
719 void
720 MidiTimeAxisView::check_step_edit ()
721 {
722         MidiRingBuffer<nframes_t>& incoming (midi_track()->step_edit_ring_buffer());
723         Evoral::Note<Evoral::MusicalTime> note;
724         uint8_t* buf;
725         uint32_t bufsize = 32;
726
727         buf = new uint8_t[bufsize];
728
729         while (incoming.read_space()) {
730                 nframes_t time;
731                 Evoral::EventType type;
732                 uint32_t size;
733
734                 incoming.read_prefix (&time, &type, &size);
735
736                 if (size > bufsize) {
737                         delete [] buf;
738                         bufsize = size;
739                         buf = new uint8_t[bufsize];
740                 }
741
742                 incoming.read_contents (size, buf);
743
744                 if ((buf[0] & 0xf0) == MIDI_CMD_NOTE_ON) {
745
746                         if (step_edit_region == 0) {
747
748                                 step_edit_region = add_region (step_edit_insert_position);
749                                 RegionView* rv = view()->find_view (step_edit_region);
750
751                                 if (rv) {
752                                         step_edit_region_view = dynamic_cast<MidiRegionView*>(rv);
753                                 } else {
754                                         fatal << X_("programming error: no view found for new MIDI region") << endmsg;
755                                         /*NOTREACHED*/
756                                 }
757                         }
758
759                         if (step_edit_region_view) {
760
761                                 bool success;
762                                 Evoral::MusicalTime beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
763
764                                 if (!success) {
765                                         continue;
766                                 }
767
768                                 step_edit_region_view->add_note (buf[0] & 0xf, buf[1], buf[2], step_edit_beat_pos, beats);
769                                 step_edit_beat_pos += beats;
770                         }
771                 }
772
773         }
774 }
775
776 void
777 MidiTimeAxisView::step_edit_rest ()
778 {
779         bool success;
780         Evoral::MusicalTime beats = _editor.get_grid_type_as_beats (success, step_edit_insert_position);
781         step_edit_beat_pos += beats;
782 }
783
784 boost::shared_ptr<Region>
785 MidiTimeAxisView::add_region (nframes64_t pos)
786 {
787         Editor* real_editor = dynamic_cast<Editor*> (&_editor);
788
789         real_editor->begin_reversible_command (_("create region"));
790         XMLNode &before = playlist()->get_state();
791
792         nframes64_t start = pos;
793         real_editor->snap_to (start, -1);
794         const Meter& m = _session->tempo_map().meter_at(start);
795         const Tempo& t = _session->tempo_map().tempo_at(start);
796         double length = floor (m.frames_per_bar(t, _session->frame_rate()));
797
798         const boost::shared_ptr<MidiDiskstream> diskstream =
799                 boost::dynamic_pointer_cast<MidiDiskstream>(view()->trackview().track()->diskstream());
800
801         boost::shared_ptr<Source> src = _session->create_midi_source_for_session (*diskstream.get());
802
803         boost::shared_ptr<Region> region = (RegionFactory::create (src, 0, (nframes_t) length,
804                                                                    PBD::basename_nosuffix(src->name())));
805
806         playlist()->add_region (region, start);
807         XMLNode &after = playlist()->get_state();
808         _session->add_command (new MementoCommand<Playlist> (*playlist().get(), &before, &after));
809
810         real_editor->commit_reversible_command();
811
812         return region;
813 }