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