kinda-sorta paste MIDI at the right spot
[ardour.git] / gtk2_ardour / midi_region_view.h
1 /*
2     Copyright (C) 2001-2007 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 #ifndef __gtk_ardour_midi_region_view_h__
20 #define __gtk_ardour_midi_region_view_h__
21
22 #include <string>
23 #include <vector>
24
25 #include <libgnomecanvasmm.h>
26 #include <libgnomecanvasmm/polygon.h>
27 #include "ardour/midi_track.h"
28 #include "ardour/midi_region.h"
29 #include "ardour/midi_model.h"
30 #include "ardour/diskstream.h"
31 #include "ardour/types.h"
32
33 #include "editing.h"
34 #include "region_view.h"
35 #include "midi_time_axis.h"
36 #include "time_axis_view_item.h"
37 #include "automation_line.h"
38 #include "enums.h"
39 #include "canvas.h"
40 #include "canvas-note.h"
41 #include "canvas-note-event.h"
42 #include "canvas-program-change.h"
43 #include "canvas-sysex.h"
44
45 namespace ARDOUR {
46         class MidiRegion;
47         class MidiModel;
48 };
49
50 namespace MIDI {
51         namespace Name {
52                 struct PatchPrimaryKey;
53         };
54 };
55
56 class MidiTimeAxisView;
57 class GhostRegion;
58 class AutomationTimeAxisView;
59 class AutomationRegionView;
60 class MidiCutBuffer;
61
62 class MidiRegionView : public RegionView
63 {
64   public:
65         typedef Evoral::Note<ARDOUR::MidiModel::TimeType> NoteType;
66
67         MidiRegionView (ArdourCanvas::Group *,
68                         RouteTimeAxisView&,
69                         boost::shared_ptr<ARDOUR::MidiRegion>,
70                         double initial_samples_per_unit,
71                         Gdk::Color const & basic_color);
72         
73         MidiRegionView (const MidiRegionView& other);
74         MidiRegionView (const MidiRegionView& other, boost::shared_ptr<ARDOUR::MidiRegion>);
75
76         ~MidiRegionView ();
77
78         virtual void init (Gdk::Color const & basic_color, bool wfd);
79
80         inline const boost::shared_ptr<ARDOUR::MidiRegion> midi_region() const
81                 { return boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region); }
82
83         inline MidiTimeAxisView* midi_view() const
84                 { return dynamic_cast<MidiTimeAxisView*>(&trackview); }
85
86         inline MidiStreamView* midi_stream_view() const
87                 { return midi_view()->midi_view(); }
88
89         void set_height (double);
90         void apply_note_range(uint8_t lowest, uint8_t highest, bool force=false);
91
92         inline ARDOUR::ColorMode color_mode() const { return midi_view()->color_mode(); }
93         
94         void set_frame_color();
95
96         void redisplay_model();
97
98         GhostRegion* add_ghost (TimeAxisView&);
99
100         void add_note(const boost::shared_ptr<NoteType> note);
101         void resolve_note(uint8_t note_num, double end_time);
102
103         void cut_copy_clear (Editing::CutCopyOp);
104         void paste (nframes64_t pos, const MidiCutBuffer&);
105
106         struct PCEvent {
107                 PCEvent(double a_time, uint8_t a_value, uint8_t a_channel) 
108                         : time(a_time), value(a_value), channel(a_channel) {}
109
110                 double  time;
111                 uint8_t value;
112                 uint8_t channel;
113         };
114         
115         /** Add a new program change flag to the canvas.
116          * @param program the MidiRegionView::PCEvent to add
117          * @param the text to display in the flag
118          */
119         void add_pgm_change(PCEvent& program, const std::string& displaytext);
120         
121         /** Look up the given time and channel in the 'automation' and set keys accordingly.
122          * @param time the time of the program change event
123          * @param channel the MIDI channel of the event
124          * @key a reference to an instance of MIDI::Name::PatchPrimaryKey whose fields will 
125          *        will be set according to the result of the lookup
126          */
127         void get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key);
128         
129         /** Change the 'automation' data of old_program to new values which correspond to new_patch.
130          * @param old_program the program change event which is to be altered
131          * @param new_patch the new lsb, msb and program number which are to be set
132          */
133         void alter_program_change(PCEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch);
134         
135         /** Alter a given program to the new given one.
136          * (Called on context menu select on CanvasProgramChange)
137          */
138         void program_selected(
139                 ArdourCanvas::CanvasProgramChange& program, 
140                 const MIDI::Name::PatchPrimaryKey& new_patch);
141         
142         /** Alter a given program to be its predecessor in the MIDNAM file.
143          */
144         void previous_program(ArdourCanvas::CanvasProgramChange& program);
145
146         /** Alters a given program to be its successor in the MIDNAM file.
147          */
148         void next_program(ArdourCanvas::CanvasProgramChange& program);
149         
150         /** Displays all program change events in the region as flags on the canvas.
151          */
152         void display_program_changes();
153         
154         /** Displays all system exclusive events in the region as flags on the canvas.
155          */
156         void display_sysexes();
157
158         void begin_write();
159         void end_write();
160         void extend_active_notes();
161
162         void create_note_at(double x, double y, double length);
163
164         void display_model(boost::shared_ptr<ARDOUR::MidiModel> model);
165
166         void start_delta_command(std::string name = "midi edit");
167         void command_add_note(const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity=false);
168         void command_remove_note(ArdourCanvas::CanvasNoteEvent* ev);
169
170         void apply_command();
171         void abort_command();
172
173         void   note_entered(ArdourCanvas::CanvasNoteEvent* ev);
174         void   unique_select(ArdourCanvas::CanvasNoteEvent* ev);
175         void   note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add);
176         void   note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add);
177         void   delete_selection();
178         size_t selection_size() { return _selection.size(); }
179         
180         void move_selection(double dx, double dy);
181         void note_dropped(ArdourCanvas::CanvasNoteEvent* ev, double d_pixels, uint8_t d_note);
182
183         /** Return true iff the note is within the currently visible range */
184         bool note_in_visible_range(const boost::shared_ptr<NoteType> note) const;
185
186         /** Get the region position in pixels relative to session. */
187         double get_position_pixels();
188
189         /** Begin resizing of some notes.
190          * Called by CanvasMidiNote when resizing starts.
191          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
192          */
193         void begin_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end);
194
195         /** Update resizing notes while user drags.
196          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
197          * @param x the difference in mouse motion, ie the motion difference if relative=true
198          *           or the absolute mouse position (track-relative) if relative is false
199          * @param relative true if relative resizing is taking place, false if absolute resizing
200          */
201         void update_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end, double x, bool relative);
202
203         /** Finish resizing notes when the user releases the mouse button.
204          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
205          * @param event_x the absolute mouse position (track-relative)
206          * @param relative true if relative resizing is taking place, false if absolute resizing
207          */
208         void commit_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end, double event_x, bool relative);
209
210         /** Adjust the velocity on a note, and the selection if applicable.
211          * @param velocity the relative or absolute velocity
212          * @param relative whether velocity is relative or absolute
213          */
214         void change_velocity(ArdourCanvas::CanvasNoteEvent* ev, int8_t velocity, bool relative=false);
215         
216         /** Change the channel of the selection.
217          * @param channel - the channel number of the new channel, zero-based
218          */
219         void change_channel(uint8_t channel);
220
221         enum MouseState {
222                 None,
223                 Pressed,
224                 SelectTouchDragging,
225                 SelectRectDragging,
226                 AddDragging
227         };
228
229         MouseState mouse_state() const { return _mouse_state; }
230
231         struct NoteResizeData {
232                 ArdourCanvas::CanvasNote  *canvas_note;
233                 ArdourCanvas::SimpleRect  *resize_rect;
234                 double                     current_x;
235         };
236         
237         /** Snap a region relative pixel coordinate to pixel units.
238          * @param x a pixel coordinate relative to region start
239          * @return the snapped pixel coordinate relative to region start
240          */
241         double snap_to_pixel(double x);
242
243         /** Snap a region relative pixel coordinate to frame units.
244          * @param x a pixel coordinate relative to region start
245          * @return the snapped nframes64_t coordinate relative to region start
246          */
247         nframes64_t snap_pixel_to_frame(double x);
248
249         /** Snap a region relative frame coordinate to frame units.
250          * @param x a pixel coordinate relative to region start
251          * @return the snapped nframes64_t coordinate relative to region start
252          */
253         nframes64_t snap_frame_to_frame(nframes64_t x);
254         
255         /** Convert a timestamp in beats to frames (both relative to region start) */
256         nframes64_t beats_to_frames(double beats) const;
257         
258         /** Convert a timestamp in frames to beats (both relative to region start) */
259         double frames_to_beats(nframes64_t beats) const;
260
261         /** Return the current selection as a MidiModel or null if there is no selection */
262         ARDOUR::MidiModel* selection_as_model () const;
263         
264   protected:
265         /** Allows derived types to specify their visibility requirements
266          * to the TimeAxisViewItem parent class.
267          */
268         MidiRegionView (ArdourCanvas::Group *,
269                         RouteTimeAxisView&,
270                         boost::shared_ptr<ARDOUR::MidiRegion>,
271                         double samples_per_unit,
272                         Gdk::Color& basic_color,
273                         TimeAxisViewItem::Visibility);
274         
275         void region_resized (ARDOUR::Change);
276         
277         void set_flags (XMLNode *);
278         void store_flags ();
279         
280         void reset_width_dependent_items (double pixel_width);
281
282   private:
283         /** Play the NoteOn event of the given note immediately
284          * and schedule the playback of the corresponding NoteOff event.
285          */
286         void play_midi_note(boost::shared_ptr<NoteType> note);
287         
288         /** Play the NoteOff-Event of the given note immediately
289          * (scheduled by @ref play_midi_note()).
290          */
291         bool play_midi_note_off(boost::shared_ptr<NoteType> note);
292
293         void clear_events();
294         void switch_source(boost::shared_ptr<ARDOUR::Source> src);
295
296         bool canvas_event(GdkEvent* ev);
297         bool note_canvas_event(GdkEvent* ev);
298         
299         void midi_channel_mode_changed(ARDOUR::ChannelMode mode, uint16_t mask);
300         void midi_patch_settings_changed(std::string model, std::string custom_device_mode);
301         
302         void change_note_velocity(ArdourCanvas::CanvasNoteEvent* ev, int8_t vel, bool relative=false);
303
304         void clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev);
305         void clear_selection() { clear_selection_except(NULL); }
306         void update_drag_selection(double last_x, double x, double last_y, double y);
307
308         void add_to_selection (ArdourCanvas::CanvasNoteEvent*);
309         void remove_from_selection (ArdourCanvas::CanvasNoteEvent*);
310
311         int8_t   _force_channel;
312         uint16_t _last_channel_selection;
313         double   _default_note_length;
314         uint8_t  _current_range_min;
315         uint8_t  _current_range_max;
316         
317         /// MIDNAM information of the current track: Model name of MIDNAM file
318         std::string _model_name;
319         
320         /// MIDNAM information of the current track: CustomDeviceMode
321         std::string _custom_device_mode;   
322
323         typedef std::vector<ArdourCanvas::CanvasNoteEvent*> Events;
324         typedef std::vector< boost::shared_ptr<ArdourCanvas::CanvasProgramChange> > PgmChanges;
325         typedef std::vector< boost::shared_ptr<ArdourCanvas::CanvasSysEx> > SysExes;
326         
327         boost::shared_ptr<ARDOUR::MidiModel> _model;
328         Events                               _events;
329         PgmChanges                           _pgm_changes;
330         SysExes                              _sys_exes;
331         ArdourCanvas::CanvasNote**           _active_notes;
332         ArdourCanvas::Group*                 _note_group;
333         ARDOUR::MidiModel::DeltaCommand*     _delta_command;
334
335         MouseState _mouse_state;
336         int _pressed_button;
337
338         typedef std::set<ArdourCanvas::CanvasNoteEvent*> Selection;
339         /// Currently selected CanvasNoteEvents
340         Selection _selection;
341
342         MidiCutBuffer* selection_as_cut_buffer () const;
343
344         /** New notes (created in the current command) which should be selected
345          * when they appear after the command is applied. */
346         std::set< boost::shared_ptr<NoteType> > _marked_for_selection;
347         
348         /** New notes (created in the current command) which should have visible velocity
349          * when they appear after the command is applied. */
350         std::set< boost::shared_ptr<NoteType> > _marked_for_velocity;
351
352         std::vector<NoteResizeData *> _resize_data;
353 };
354
355 #endif /* __gtk_ardour_midi_region_view_h__ */