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