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