16edd88f721db33542234dfc97ecce3a5b6bc4a5
[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         , _channel_selector(0xFFFF)
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         _channel_selector.selection_changed.connect(
148                 mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_channel_mask));
149         _channel_selector.force_channel_changed.connect(
150                 mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_force_channel));
151
152 }
153
154 MidiTimeAxisView::~MidiTimeAxisView ()
155 {
156         delete _piano_roll_header;
157         _piano_roll_header = 0;
158
159         delete _range_scroomer;
160         _range_scroomer = 0;
161 }
162
163 MidiStreamView*
164 MidiTimeAxisView::midi_view()
165 {
166         return dynamic_cast<MidiStreamView*>(_view);
167 }
168
169 guint32
170 MidiTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
171 {
172         ensure_xml_node ();
173         xml_node->add_property ("shown_editor", "yes");
174                 
175         guint32 ret = TimeAxisView::show_at (y, nth, parent);
176         _piano_roll_header->show();
177         _range_scroomer->show();
178         return ret;
179 }
180
181 void
182 MidiTimeAxisView::hide ()
183 {
184         ensure_xml_node ();
185         xml_node->add_property ("shown_editor", "no");
186
187         TimeAxisView::hide ();
188 }
189
190 void
191 MidiTimeAxisView::append_extra_display_menu_items ()
192 {
193         using namespace Menu_Helpers;
194
195         MenuList& items = display_menu->items();
196
197         // Note range
198         Menu *range_menu = manage(new Menu);
199         MenuList& range_items = range_menu->items();
200         range_menu->set_name ("ArdourContextMenu");
201         
202         RadioMenuItem::Group range_group;
203
204         range_items.push_back (RadioMenuElem (range_group, _("Show Full Range"), bind (
205                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
206                         MidiStreamView::FullRange)));
207         
208         range_items.push_back (RadioMenuElem (range_group, _("Fit Contents"), bind (
209                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
210                         MidiStreamView::ContentsRange)));
211
212         ((Gtk::CheckMenuItem&)range_items.back()).set_active(true);
213
214         items.push_back (MenuElem (_("Note range"), *range_menu));
215 }
216
217 void
218 MidiTimeAxisView::build_automation_action_menu ()
219 {
220         using namespace Menu_Helpers;
221
222         RouteTimeAxisView::build_automation_action_menu ();
223
224         MenuList& automation_items = automation_action_menu->items();
225         
226         automation_items.push_back (SeparatorElem());
227
228         automation_items.push_back (MenuElem (_("Controller..."), 
229                                                    mem_fun(*this, &MidiTimeAxisView::add_controller_track)));
230 }
231
232 Gtk::Menu*
233 MidiTimeAxisView::build_mode_menu()
234 {
235         using namespace Menu_Helpers;
236
237         Menu* mode_menu = manage (new Menu);
238         MenuList& items = mode_menu->items();
239         mode_menu->set_name ("ArdourContextMenu");
240
241         RadioMenuItem::Group mode_group;
242         items.push_back (RadioMenuElem (mode_group, _("Sustained"),
243                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Sustained)));
244         _note_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
245         _note_mode_item->set_active(_note_mode == Sustained);
246
247         items.push_back (RadioMenuElem (mode_group, _("Percussive"),
248                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Percussive)));
249         _percussion_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
250         _percussion_mode_item->set_active(_note_mode == Percussive);
251
252         return mode_menu;
253 }
254         
255 void
256 MidiTimeAxisView::set_note_mode(NoteMode mode)
257 {
258         if (_note_mode != mode || midi_track()->note_mode() != mode) {
259                 _note_mode = mode;
260                 midi_track()->set_note_mode(mode);
261                 _view->redisplay_diskstream();
262         }
263 }
264
265
266 void
267 MidiTimeAxisView::set_note_range(MidiStreamView::VisibleNoteRange range)
268 {
269         //if (midi_view()->note_range() != range) {
270                 midi_view()->set_note_range(range);
271                 midi_view()->redisplay_diskstream();
272         //}
273 }
274
275
276 void
277 MidiTimeAxisView::update_range()
278 {
279         MidiGhostRegion* mgr;
280
281         for(list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
282                 if ((mgr = dynamic_cast<MidiGhostRegion*>(*i)) != 0) {
283                         mgr->update_range();
284                 }
285         }
286 }
287
288 void
289 MidiTimeAxisView::show_existing_automation ()
290 {
291         if (midi_track()) {
292                 const set<Parameter> params = midi_track()->midi_diskstream()->
293                                 midi_playlist()->contained_automation();
294
295                 for (set<Parameter>::const_iterator i = params.begin(); i != params.end(); ++i)
296                         create_automation_child(*i, true);
297         }
298
299         RouteTimeAxisView::show_existing_automation ();
300 }
301
302 /** Prompt for a controller with a dialog and add an automation track for it
303  */
304 void
305 MidiTimeAxisView::add_controller_track()
306 {
307         int response;
308         Parameter param;
309
310         {
311                 AddMidiCCTrackDialog dialog;
312                 dialog.set_transient_for(editor);
313                 response = dialog.run();
314                 
315                 if (response == Gtk::RESPONSE_ACCEPT)
316                         param = dialog.parameter();
317         }
318
319         if (response == Gtk::RESPONSE_ACCEPT)
320                 create_automation_child(param, true);
321 }
322
323 void
324 MidiTimeAxisView::create_automation_child (Parameter param, bool show)
325 {
326         if (param.type() == MidiCCAutomation) {
327         
328                 /* FIXME: don't create AutomationList for track itself
329                  * (not actually needed or used, since the automation is region-ey) */
330
331                 boost::shared_ptr<AutomationControl> c = _route->control(param);
332
333                 if (!c) {
334                         boost::shared_ptr<AutomationList> al(new ARDOUR::AutomationList(param, 0, 127, 64));
335                         c = boost::shared_ptr<AutomationControl>(_route->control_factory(al));
336                         _route->add_control(c);
337                 }
338
339                 AutomationTracks::iterator existing = _automation_tracks.find(param);
340                 if (existing != _automation_tracks.end())
341                         return;
342
343                 boost::shared_ptr<AutomationTimeAxisView> track(new AutomationTimeAxisView (_session,
344                                 _route, _route, c,
345                                 editor,
346                                 *this,
347                                 true,
348                                 parent_canvas,
349                                 _route->describe_parameter(param)));
350                 
351                 add_automation_child(param, track, show);
352
353         } else {
354                 error << "MidiTimeAxisView: unknown automation child " << param.to_string() << endmsg;
355         }
356 }
357
358 void
359 MidiTimeAxisView::route_active_changed ()
360 {
361         RouteUI::route_active_changed ();
362
363         if (is_track()) {
364                 if (_route->active()) {
365                         controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
366                         controls_base_selected_name = "MidiTrackControlsBaseSelected";
367                         controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
368                 } else {
369                         controls_ebox.set_name ("MidiTrackControlsBaseInactiveUnselected");
370                         controls_base_selected_name = "MidiTrackControlsBaseInactiveSelected";
371                         controls_base_unselected_name = "MidiTrackControlsBaseInactiveUnselected";
372                 }
373         } else {
374
375                 throw; // wha?
376                 
377                 if (_route->active()) {
378                         controls_ebox.set_name ("BusControlsBaseUnselected");
379                         controls_base_selected_name = "BusControlsBaseSelected";
380                         controls_base_unselected_name = "BusControlsBaseUnselected";
381                 } else {
382                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
383                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
384                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
385                 }
386         }
387 }
388
389 void 
390 MidiTimeAxisView::channel_selector_toggled()
391 {
392         static TimeAxisView::TrackHeight previous_height;
393         
394         if(_midi_expander.property_expanded()) {
395                 previous_height = height_style;
396                 if(previous_height != TimeAxisView::Largest) {
397                         set_height(TimeAxisView::Large);
398                 }
399         } else {
400                 set_height(previous_height);
401         }
402 }
403
404
405