95b77abaf4f8922848f3567b62fdb12995a5df0c
[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) // FIXME: won't compile without this, why??
87         , RouteTimeAxisView(ed, sess, rt, canvas)
88         , _range_scroomer(0)
89         , _piano_roll_header(0)
90         , _note_mode(Sustained)
91         , _note_mode_item(NULL)
92         , _percussion_mode_item(NULL)
93         , _midi_expander("MIDI")
94 {
95         subplugin_menu.set_name ("ArdourContextMenu");
96
97         _view = new MidiStreamView (*this);
98
99         ignore_toggle = false;
100
101         mute_button->set_active (false);
102         solo_button->set_active (false);
103         
104         if (is_midi_track()) {
105                 controls_ebox.set_name ("MidiTimeAxisViewControlsBaseUnselected");
106                 _note_mode = midi_track()->note_mode();
107         } else { // MIDI bus (which doesn't exist yet..)
108                 controls_ebox.set_name ("MidiBusControlsBaseUnselected");
109         }
110
111         /* map current state of the route */
112
113         processors_changed ();
114
115         ensure_xml_node ();
116
117         set_state (*xml_node);
118         
119         _route->processors_changed.connect (mem_fun(*this, &MidiTimeAxisView::processors_changed));
120
121         if (is_track()) {
122                 _piano_roll_header = new PianoRollHeader(*midi_view());
123                 _range_scroomer = new MidiScroomer(midi_view()->note_range_adjustment);
124
125                 controls_hbox.pack_start(*_range_scroomer);
126                 controls_hbox.pack_start(*_piano_roll_header);
127
128                 controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
129                 controls_base_selected_name = "MidiTrackControlsBaseSelected";
130                 controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
131
132                 midi_view()->NoteRangeChanged.connect (mem_fun(*this, &MidiTimeAxisView::update_range));
133
134                 /* ask for notifications of any new RegionViews */
135                 _view->RegionViewAdded.connect (mem_fun(*this, &MidiTimeAxisView::region_view_added));
136                 _view->attach ();
137         }
138                 
139         // add channel selector expander
140         HBox *channel_selector_box = manage(new HBox());
141         channel_selector_box->pack_start(_channel_selector, SHRINK, 0);
142         _midi_expander.add(*channel_selector_box);
143         _midi_expander.property_expanded().signal_changed().connect(
144                         mem_fun(this, &MidiTimeAxisView::channel_selector_toggled));
145         controls_vbox.pack_end(_midi_expander, SHRINK, 0);
146         boost::shared_ptr<MidiDiskstream> diskstream = midi_track()->midi_diskstream();
147         // restore channel selector settings
148         _channel_selector.set_selected_channels(diskstream->get_channel_mask());
149         _channel_selector.set_force_channel(diskstream->get_force_channel());
150         _channel_selector.selection_changed.connect(
151                 mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_channel_mask));
152         _channel_selector.force_channel_changed.connect(
153                 mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_force_channel));
154
155 }
156
157 MidiTimeAxisView::~MidiTimeAxisView ()
158 {
159         delete _piano_roll_header;
160         _piano_roll_header = 0;
161
162         delete _range_scroomer;
163         _range_scroomer = 0;
164 }
165
166 MidiStreamView*
167 MidiTimeAxisView::midi_view()
168 {
169         return dynamic_cast<MidiStreamView*>(_view);
170 }
171
172 guint32
173 MidiTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
174 {
175         ensure_xml_node ();
176         xml_node->add_property ("shown_editor", "yes");
177                 
178         guint32 ret = TimeAxisView::show_at (y, nth, parent);
179         _piano_roll_header->show();
180         _range_scroomer->show();
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::append_extra_display_menu_items ()
195 {
196         using namespace Menu_Helpers;
197
198         MenuList& items = display_menu->items();
199
200         // Note range
201         Menu *range_menu = manage(new Menu);
202         MenuList& range_items = range_menu->items();
203         range_menu->set_name ("ArdourContextMenu");
204         
205         RadioMenuItem::Group range_group;
206
207         range_items.push_back (RadioMenuElem (range_group, _("Show Full Range"), bind (
208                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
209                         MidiStreamView::FullRange)));
210         
211         range_items.push_back (RadioMenuElem (range_group, _("Fit Contents"), bind (
212                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
213                         MidiStreamView::ContentsRange)));
214
215         ((Gtk::CheckMenuItem&)range_items.back()).set_active(true);
216
217         items.push_back (MenuElem (_("Note range"), *range_menu));
218 }
219
220 void
221 MidiTimeAxisView::build_automation_action_menu ()
222 {
223         using namespace Menu_Helpers;
224
225         RouteTimeAxisView::build_automation_action_menu ();
226
227         MenuList& automation_items = automation_action_menu->items();
228         
229         automation_items.push_back (SeparatorElem());
230
231         automation_items.push_back (MenuElem (_("Controller..."), 
232                                                    mem_fun(*this, &MidiTimeAxisView::add_controller_track)));
233 }
234
235 Gtk::Menu*
236 MidiTimeAxisView::build_mode_menu()
237 {
238         using namespace Menu_Helpers;
239
240         Menu* mode_menu = manage (new Menu);
241         MenuList& items = mode_menu->items();
242         mode_menu->set_name ("ArdourContextMenu");
243
244         RadioMenuItem::Group mode_group;
245         items.push_back (RadioMenuElem (mode_group, _("Sustained"),
246                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Sustained)));
247         _note_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
248         _note_mode_item->set_active(_note_mode == Sustained);
249
250         items.push_back (RadioMenuElem (mode_group, _("Percussive"),
251                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Percussive)));
252         _percussion_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
253         _percussion_mode_item->set_active(_note_mode == Percussive);
254
255         return mode_menu;
256 }
257         
258 void
259 MidiTimeAxisView::set_note_mode(NoteMode mode)
260 {
261         if (_note_mode != mode || midi_track()->note_mode() != mode) {
262                 _note_mode = mode;
263                 midi_track()->set_note_mode(mode);
264                 _view->redisplay_diskstream();
265         }
266 }
267
268
269 void
270 MidiTimeAxisView::set_note_range(MidiStreamView::VisibleNoteRange range)
271 {
272         //if (midi_view()->note_range() != range) {
273                 midi_view()->set_note_range(range);
274                 midi_view()->redisplay_diskstream();
275         //}
276 }
277
278
279 void
280 MidiTimeAxisView::update_range()
281 {
282         MidiGhostRegion* mgr;
283
284         for(list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
285                 if ((mgr = dynamic_cast<MidiGhostRegion*>(*i)) != 0) {
286                         mgr->update_range();
287                 }
288         }
289 }
290
291 void
292 MidiTimeAxisView::show_existing_automation ()
293 {
294         if (midi_track()) {
295                 const set<Parameter> params = midi_track()->midi_diskstream()->
296                                 midi_playlist()->contained_automation();
297
298                 for (set<Parameter>::const_iterator i = params.begin(); i != params.end(); ++i)
299                         create_automation_child(*i, true);
300         }
301
302         RouteTimeAxisView::show_existing_automation ();
303 }
304
305 /** Prompt for a controller with a dialog and add an automation track for it
306  */
307 void
308 MidiTimeAxisView::add_controller_track()
309 {
310         int response;
311         Parameter param;
312
313         {
314                 AddMidiCCTrackDialog dialog;
315                 dialog.set_transient_for(editor);
316                 response = dialog.run();
317                 
318                 if (response == Gtk::RESPONSE_ACCEPT)
319                         param = dialog.parameter();
320         }
321
322         if (response == Gtk::RESPONSE_ACCEPT)
323                 create_automation_child(param, true);
324 }
325
326 void
327 MidiTimeAxisView::create_automation_child (Parameter param, bool show)
328 {
329         if (
330                         param.type() == MidiCCAutomation ||
331                         param.type() == MidiPgmChangeAutomation ||
332                         param.type() == MidiPitchBenderAutomation ||
333                         param.type() == MidiChannelAftertouchAutomation
334            ) {
335         
336                 /* FIXME: don't create AutomationList for track itself
337                  * (not actually needed or used, since the automation is region-ey) */
338
339                 boost::shared_ptr<AutomationControl> c = _route->control(param);
340
341                 if (!c) {
342                         boost::shared_ptr<AutomationList> al(new ARDOUR::AutomationList(param,
343                                                 param.min(), param.max(), (param.max() - param.min() / 2)));
344                         c = boost::shared_ptr<AutomationControl>(_route->control_factory(al));
345                         _route->add_control(c);
346                 }
347
348                 AutomationTracks::iterator existing = _automation_tracks.find(param);
349                 if (existing != _automation_tracks.end())
350                         return;
351
352                 boost::shared_ptr<AutomationTimeAxisView> track(new AutomationTimeAxisView (_session,
353                                 _route, _route, c,
354                                 editor,
355                                 *this,
356                                 true,
357                                 parent_canvas,
358                                 _route->describe_parameter(param)));
359                 
360                 add_automation_child(param, track, show);
361
362         } else {
363                 error << "MidiTimeAxisView: unknown automation child " << param.to_string() << endmsg;
364         }
365 }
366
367 void
368 MidiTimeAxisView::route_active_changed ()
369 {
370         RouteUI::route_active_changed ();
371
372         if (is_track()) {
373                 if (_route->active()) {
374                         controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
375                         controls_base_selected_name = "MidiTrackControlsBaseSelected";
376                         controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
377                 } else {
378                         controls_ebox.set_name ("MidiTrackControlsBaseInactiveUnselected");
379                         controls_base_selected_name = "MidiTrackControlsBaseInactiveSelected";
380                         controls_base_unselected_name = "MidiTrackControlsBaseInactiveUnselected";
381                 }
382         } else {
383
384                 throw; // wha?
385                 
386                 if (_route->active()) {
387                         controls_ebox.set_name ("BusControlsBaseUnselected");
388                         controls_base_selected_name = "BusControlsBaseSelected";
389                         controls_base_unselected_name = "BusControlsBaseUnselected";
390                 } else {
391                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
392                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
393                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
394                 }
395         }
396 }
397
398 void 
399 MidiTimeAxisView::channel_selector_toggled()
400 {
401         static TimeAxisView::TrackHeight previous_height;
402         
403         if(_midi_expander.property_expanded()) {
404                 previous_height = height_style;
405                 if(previous_height != TimeAxisView::Largest) {
406                         set_height(TimeAxisView::Large);
407                 }
408         } else {
409                 set_height(previous_height);
410         }
411 }
412
413
414