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