Grab keyboard only with note selection.
[ardour.git] / gtk2_ardour / midi_region_view.h
1 /*
2     Copyright (C) 2001-2011 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 #include <stdint.h>
25
26 #include "pbd/signals.h"
27
28 #include "ardour/midi_model.h"
29 #include "ardour/types.h"
30
31 #include "editing.h"
32 #include "region_view.h"
33 #include "midi_time_axis.h"
34 #include "time_axis_view_item.h"
35 #include "automation_line.h"
36 #include "enums.h"
37
38 namespace ARDOUR {
39         class MidiRegion;
40         class MidiModel;
41         class Filter;
42 };
43
44 namespace MIDI {
45         namespace Name {
46                 struct PatchPrimaryKey;
47         };
48 };
49
50 class SysEx;
51 class NoteBase;
52 class Note;
53 class Hit;
54 class MidiTimeAxisView;
55 class GhostRegion;
56 class AutomationTimeAxisView;
57 class AutomationRegionView;
58 class MidiCutBuffer;
59 class MidiListEditor;
60 class EditNoteDialog;
61 class NotePlayer;
62 class PatchChange;
63 class ItemCounts;
64
65 class MidiRegionView : public RegionView
66 {
67 public:
68         typedef Evoral::Note<Evoral::MusicalTime> NoteType;
69         typedef Evoral::Sequence<Evoral::MusicalTime>::Notes Notes;
70
71         MidiRegionView (ArdourCanvas::Container *,
72                         RouteTimeAxisView&,
73                         boost::shared_ptr<ARDOUR::MidiRegion>,
74                         double initial_samples_per_pixel,
75                         uint32_t base_color);
76
77         MidiRegionView (const MidiRegionView& other);
78         MidiRegionView (const MidiRegionView& other, boost::shared_ptr<ARDOUR::MidiRegion>);
79
80         ~MidiRegionView ();
81
82         void init (bool wfd);
83
84         const boost::shared_ptr<ARDOUR::MidiRegion> midi_region() const;
85
86         inline MidiTimeAxisView* midi_view() const
87         { return dynamic_cast<MidiTimeAxisView*>(&trackview); }
88
89         inline MidiStreamView* midi_stream_view() const
90         { return midi_view()->midi_view(); }
91
92         void step_add_note (uint8_t channel, uint8_t number, uint8_t velocity,
93                             Evoral::MusicalTime pos, Evoral::MusicalTime len);
94         void step_sustain (Evoral::MusicalTime beats);
95         void set_height (double);
96         void apply_note_range(uint8_t lowest, uint8_t highest, bool force=false);
97
98         inline ARDOUR::ColorMode color_mode() const { return midi_view()->color_mode(); }
99
100         void set_frame_color();
101         void color_handler ();
102
103         void show_step_edit_cursor (Evoral::MusicalTime pos);
104         void move_step_edit_cursor (Evoral::MusicalTime pos);
105         void hide_step_edit_cursor ();
106         void set_step_edit_cursor_width (Evoral::MusicalTime beats);
107
108         void redisplay_model();
109
110         GhostRegion* add_ghost (TimeAxisView&);
111
112         void add_note(const boost::shared_ptr<NoteType> note, bool visible);
113         void resolve_note(uint8_t note_num, Evoral::MusicalTime end_time);
114
115         void cut_copy_clear (Editing::CutCopyOp);
116         bool paste (framepos_t pos, const ::Selection& selection, PasteContext& ctx);
117         void paste_internal (framepos_t pos, unsigned paste_count, float times, const MidiCutBuffer&);
118
119         void add_canvas_patch_change (ARDOUR::MidiModel::PatchChangePtr patch, const std::string& displaytext, bool);
120
121         /** Look up the given time and channel in the 'automation' and set keys accordingly.
122          * @param time the time of the patch 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 (Evoral::MusicalTime time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key) const;
128
129         /** Convert a given PatchChange into a PatchPrimaryKey
130          */
131         MIDI::Name::PatchPrimaryKey patch_change_to_patch_key (ARDOUR::MidiModel::PatchChangePtr);
132
133         /** Change old_patch to new_patch.
134          * @param old_patch the canvas patch change which is to be altered
135          * @param new_patch new patch
136          */
137         void change_patch_change (PatchChange& old_patch, const MIDI::Name::PatchPrimaryKey& new_patch);
138         void change_patch_change (ARDOUR::MidiModel::PatchChangePtr, Evoral::PatchChange<Evoral::MusicalTime> const &);
139
140         void add_patch_change (framecnt_t, Evoral::PatchChange<Evoral::MusicalTime> const &);
141         void move_patch_change (PatchChange &, Evoral::MusicalTime);
142         void delete_patch_change (PatchChange *);
143         void edit_patch_change (PatchChange *);
144
145         void delete_sysex (SysEx*);
146
147         /** Alter a given patch to be its predecessor in the MIDNAM file.
148          */
149         void previous_patch (PatchChange &);
150
151         /** Alters a given patch to be its successor in the MIDNAM file.
152          */
153         void next_patch (PatchChange &);
154
155         void previous_bank (PatchChange &);
156         void next_bank (PatchChange &);
157
158         /** Displays all patch change events in the region as flags on the canvas.
159          */
160         void display_patch_changes();
161
162         /** Displays all system exclusive events in the region as flags on the canvas.
163          */
164         void display_sysexes();
165
166         void begin_write();
167         void end_write();
168         void extend_active_notes();
169
170         void display_model(boost::shared_ptr<ARDOUR::MidiModel> model);
171
172         void start_note_diff_command (std::string name = "midi edit");
173         void note_diff_add_change (NoteBase* ev, ARDOUR::MidiModel::NoteDiffCommand::Property, uint8_t val);
174         void note_diff_add_change (NoteBase* ev, ARDOUR::MidiModel::NoteDiffCommand::Property, Evoral::MusicalTime val);
175         void note_diff_add_note (const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity = false);
176         void note_diff_remove_note (NoteBase* ev);
177
178         void apply_diff (bool as_subcommand = false);
179         void abort_command();
180
181         void   note_entered(NoteBase* ev);
182         void   note_left(NoteBase* ev);
183         void   patch_entered (PatchChange *);
184         void   patch_left (PatchChange *);
185         void   sysex_entered (SysEx* p);
186         void   sysex_left (SysEx* p);
187         void   note_mouse_position (float xfraction, float yfraction, bool can_set_cursor=true);
188         void   unique_select(NoteBase* ev);
189         void   note_selected(NoteBase* ev, bool add, bool extend=false);
190         void   note_deselected(NoteBase* ev);
191         void   delete_selection();
192         void   delete_note (boost::shared_ptr<NoteType>);
193         size_t selection_size() { return _selection.size(); }
194         void   select_all_notes ();
195         void   select_range(framepos_t start, framepos_t end);
196         void   invert_selection ();
197
198         void move_selection(double dx, double dy, double cumulative_dy);
199         void note_dropped (NoteBase* ev, ARDOUR::frameoffset_t, int8_t d_note);
200
201         void select_matching_notes (uint8_t notenum, uint16_t channel_mask, bool add, bool extend);
202         void toggle_matching_notes (uint8_t notenum, uint16_t channel_mask);
203
204         /** Return true iff the note is within the extent of the region.
205          * @param visible will be set to true if the note is within the visible note range, false otherwise.
206          */
207         bool note_in_region_range(const boost::shared_ptr<NoteType> note, bool& visible) const;
208
209         /** Get the region position in pixels relative to session. */
210         double get_position_pixels();
211
212         /** Get the region end position in pixels relative to session. */
213         double get_end_position_pixels();
214
215         /** Begin resizing of some notes.
216          * Called by CanvasMidiNote when resizing starts.
217          * @param at_front which end of the note (true == note on, false == note off)
218          */
219         void begin_resizing(bool at_front);
220
221         void update_resizing (NoteBase*, bool, double, bool);
222         void commit_resizing (NoteBase*, bool, double, bool);
223         void abort_resizing ();
224
225         /** Change the channel of the selection.
226          * @param channel - the channel number of the new channel, zero-based
227          */
228         void change_channel(uint8_t channel);
229
230         enum MouseState {
231                 None,
232                 Pressed,
233                 SelectTouchDragging,
234                 SelectRectDragging,
235                 SelectVerticalDragging,
236                 AddDragging
237         };
238
239         MouseState mouse_state() const { return _mouse_state; }
240
241         void note_button_release ();
242
243         struct NoteResizeData {
244                 Note                     *note;
245                 ArdourCanvas::Rectangle  *resize_rect;
246         };
247
248         /** Snap a region relative pixel coordinate to pixel units.
249          * @param x a pixel coordinate relative to region start
250          * @return the snapped pixel coordinate relative to region start
251          */
252         double snap_to_pixel(double x);
253
254         /** Snap a region relative pixel coordinate to frame units.
255          * @param x a pixel coordinate relative to region start
256          * @return the snapped framepos_t coordinate relative to region start
257          */
258         framepos_t snap_pixel_to_sample(double x);
259
260         /** Convert a timestamp in beats into frames (both relative to region position) */
261         framepos_t region_beats_to_region_frames(Evoral::MusicalTime beats) const;
262         /** Convert a timestamp in beats into absolute frames */
263         framepos_t region_beats_to_absolute_frames(Evoral::MusicalTime beats) const {
264                 return _region->position() + region_beats_to_region_frames (beats);
265         }
266         /** Convert a timestamp in frames to beats (both relative to region position) */
267         Evoral::MusicalTime region_frames_to_region_beats(framepos_t) const;
268
269         /** Convert a timestamp in beats measured from source start into absolute frames */
270         framepos_t source_beats_to_absolute_frames(Evoral::MusicalTime beats) const;
271         /** Convert a timestamp in beats measured from source start into region-relative frames */
272         framepos_t source_beats_to_region_frames(Evoral::MusicalTime beats) const {
273                 return source_beats_to_absolute_frames (beats) - _region->position();
274         }
275         /** Convert a timestamp in absolute frames to beats measured from source start*/
276         Evoral::MusicalTime absolute_frames_to_source_beats(framepos_t) const;
277
278         ARDOUR::BeatsFramesConverter const & region_relative_time_converter () const {
279                 return _region_relative_time_converter;
280         }
281
282         ARDOUR::BeatsFramesConverter const & source_relative_time_converter () const {
283                 return _source_relative_time_converter;
284         }
285
286         void goto_previous_note (bool add_to_selection);
287         void goto_next_note (bool add_to_selection);
288         void change_note_lengths (bool, bool, Evoral::MusicalTime beats, bool start, bool end);
289         void change_velocities (bool up, bool fine, bool allow_smush, bool all_together);
290         void transpose (bool up, bool fine, bool allow_smush);
291         void nudge_notes (bool forward);
292         void channel_edit ();
293         void velocity_edit ();
294
295         void show_list_editor ();
296
297         typedef std::set<NoteBase*> Selection;
298         Selection selection () const {
299                 return _selection;
300         }
301         
302         void selection_as_notelist (Notes& selected, bool allow_all_if_none_selected = false);
303
304         void enable_display (bool);
305
306         void set_channel_selector_scoped_note(NoteBase* note){ _channel_selection_scoped_note = note; }
307         NoteBase* channel_selector_scoped_note(){  return _channel_selection_scoped_note; }
308
309         void trim_front_starting ();
310         void trim_front_ending ();
311
312         /** Add a note to the model, and the view, at a canvas (click) coordinate.
313          * \param t time in frames relative to the position of the region
314          * \param y vertical position in pixels
315          * \param length duration of the note in beats
316          * \param snap_t true to snap t to the grid, otherwise false.
317          */
318         void create_note_at (framepos_t t, double y, Evoral::MusicalTime length, bool snap_t);
319
320         void clear_selection (bool signal = true) { clear_selection_except (0, signal); }
321
322         ARDOUR::InstrumentInfo& instrument_info() const;
323         
324 protected:
325         /** Allows derived types to specify their visibility requirements
326          * to the TimeAxisViewItem parent class.
327          */
328         MidiRegionView (ArdourCanvas::Container *,
329                         RouteTimeAxisView&,
330                         boost::shared_ptr<ARDOUR::MidiRegion>,
331                         double samples_per_pixel,
332                         uint32_t basic_color,
333                         TimeAxisViewItem::Visibility);
334
335         void region_resized (const PBD::PropertyChange&);
336
337         void set_flags (XMLNode *);
338         void store_flags ();
339
340         void reset_width_dependent_items (double pixel_width);
341
342         void parameter_changed (std::string const & p);
343
344 private:
345
346         friend class MidiRubberbandSelectDrag;
347         friend class MidiVerticalSelectDrag;
348
349         /** Emitted when the selection has been cleared in one MidiRegionView,
350          *  with the expectation that others will clear their selections in
351          *  sympathy.
352          */
353         static PBD::Signal1<void, MidiRegionView*> SelectionCleared;
354         PBD::ScopedConnection _selection_cleared_connection;
355         void selection_cleared (MidiRegionView *);
356
357         friend class EditNoteDialog;
358
359         /** Play the NoteOn event of the given note immediately
360          * and schedule the playback of the corresponding NoteOff event.
361          */
362         void play_midi_note (boost::shared_ptr<NoteType> note);
363         void start_playing_midi_note (boost::shared_ptr<NoteType> note);
364         void start_playing_midi_chord (std::vector<boost::shared_ptr<NoteType> > notes);
365
366         void clear_events (bool with_selection_signal = true);
367
368         bool canvas_group_event(GdkEvent* ev);
369         bool note_canvas_event(GdkEvent* ev);
370
371         void midi_channel_mode_changed ();
372         PBD::ScopedConnection _channel_mode_changed_connection;
373         void instrument_settings_changed ();
374         PBD::ScopedConnection _instrument_changed_connection;
375
376         void change_note_channel (NoteBase *, int8_t, bool relative=false);
377         void change_note_velocity(NoteBase* ev, int8_t vel, bool relative=false);
378         void change_note_note(NoteBase* ev, int8_t note, bool relative=false);
379         void change_note_time(NoteBase* ev, ARDOUR::MidiModel::TimeType, bool relative=false);
380         void change_note_length (NoteBase *, ARDOUR::MidiModel::TimeType);
381         void trim_note(NoteBase* ev, ARDOUR::MidiModel::TimeType start_delta,
382                        ARDOUR::MidiModel::TimeType end_delta);
383
384         void clear_selection_except (NoteBase* ev, bool signal = true);
385         void update_drag_selection (framepos_t start, framepos_t end, double y0, double y1, bool extend);
386         void update_vertical_drag_selection (double last_y, double y, bool extend);
387
388         void add_to_selection (NoteBase*);
389         void remove_from_selection (NoteBase*);
390
391         void show_verbose_cursor (std::string const &, double, double) const;
392         void show_verbose_cursor (boost::shared_ptr<NoteType>) const;
393
394         uint8_t  _current_range_min;
395         uint8_t  _current_range_max;
396         
397         typedef std::list<NoteBase*>                          Events;
398         typedef std::vector< boost::shared_ptr<PatchChange> > PatchChanges;
399         typedef std::vector< boost::shared_ptr<SysEx> >       SysExes;
400
401         ARDOUR::BeatsFramesConverter _region_relative_time_converter;
402         ARDOUR::BeatsFramesConverter _source_relative_time_converter;
403
404         boost::shared_ptr<ARDOUR::MidiModel> _model;
405         Events                               _events;
406         PatchChanges                         _patch_changes;
407         SysExes                              _sys_exes;
408         Note**                               _active_notes;
409         ArdourCanvas::Container*             _note_group;
410         ARDOUR::MidiModel::NoteDiffCommand*  _note_diff_command;
411         Note*                                _ghost_note;
412         double                               _last_ghost_x;
413         double                               _last_ghost_y;
414         ArdourCanvas::Rectangle*             _step_edit_cursor;
415         Evoral::MusicalTime                  _step_edit_cursor_width;
416         Evoral::MusicalTime                  _step_edit_cursor_position;
417         NoteBase*                            _channel_selection_scoped_note;
418
419         /** A group used to temporarily reparent _note_group to during start trims, so
420          *  that the notes don't move with the parent region view.
421          */
422         ArdourCanvas::Container*                 _temporary_note_group;
423
424         MouseState _mouse_state;
425         int _pressed_button;
426
427         /** Currently selected NoteBase objects */
428         Selection _selection;
429
430         bool _sort_needed;
431         void time_sort_events ();
432
433         MidiCutBuffer* selection_as_cut_buffer () const;
434
435         /** New notes (created in the current command) which should be selected
436          * when they appear after the command is applied. */
437         std::set< boost::shared_ptr<NoteType> > _marked_for_selection;
438
439         /** New notes (created in the current command) which should have visible velocity
440          * when they appear after the command is applied. */
441         std::set< boost::shared_ptr<NoteType> > _marked_for_velocity;
442
443         std::vector<NoteResizeData *> _resize_data;
444
445         /** connection used to connect to model's ContentChanged signal */
446         PBD::ScopedConnection content_connection;
447
448         NoteBase* find_canvas_note (boost::shared_ptr<NoteType>);
449         Events::iterator _optimization_iterator;
450
451         void update_note (Note *, bool update_ghost_regions = true);
452         void update_hit (Hit *);
453         void create_ghost_note (double, double);
454         void update_ghost_note (double, double);
455
456         MidiListEditor* _list_editor;
457         bool _no_sound_notes;
458
459         PBD::ScopedConnection note_delete_connection;
460         void maybe_remove_deleted_note_from_selection (NoteBase*);
461
462         void snap_changed ();
463         PBD::ScopedConnection snap_changed_connection;
464
465         bool motion (GdkEventMotion*);
466         bool scroll (GdkEventScroll*);
467         bool key_press (GdkEventKey*);
468         bool key_release (GdkEventKey*);
469         bool button_press (GdkEventButton*);
470         bool button_release (GdkEventButton*);
471         bool enter_notify (GdkEventCrossing*);
472         bool leave_notify (GdkEventCrossing*);
473
474         void drop_down_keys ();
475         void maybe_select_by_position (GdkEventButton* ev, double x, double y);
476         void get_events (Events& e, Evoral::Sequence<Evoral::MusicalTime>::NoteOperator op, uint8_t val, int chan_mask = 0);
477
478         void display_patch_changes_on_channel (uint8_t, bool);
479
480         void connect_to_diskstream ();
481         void data_recorded (boost::weak_ptr<ARDOUR::MidiSource>);
482
483         /** Get grid type as beats, or default to 1 if not snapped to beats. */
484         Evoral::MusicalTime get_grid_beats(framepos_t pos) const;
485
486         void remove_ghost_note ();
487         void mouse_mode_changed ();
488         void enter_internal ();
489         void leave_internal ();
490
491         double _last_event_x;
492         double _last_event_y;
493         bool   _grabbed_keyboard;
494         bool   _entered;
495
496         framepos_t snap_frame_to_grid_underneath (framepos_t p, framecnt_t &) const;
497         
498         PBD::ScopedConnection _mouse_mode_connection;
499
500         Gdk::Cursor* pre_enter_cursor;
501         Gdk::Cursor* pre_press_cursor;
502         Gdk::Cursor* pre_note_enter_cursor;
503
504         NotePlayer* _note_player;
505
506         ARDOUR::ChannelMode get_channel_mode() const;
507         uint16_t get_selected_channels () const;
508 };
509
510
511 #endif /* __gtk_ardour_midi_region_view_h__ */