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