* first working version of editing MIDI channels of individual notes, see: http:...
[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-midi-event.h"
40
41 namespace ARDOUR {
42         class MidiRegion;
43         class MidiModel;
44 };
45
46 class MidiTimeAxisView;
47 class GhostRegion;
48 class AutomationTimeAxisView;
49 class AutomationRegionView;
50
51 class MidiRegionView : public RegionView
52 {
53   public:
54         MidiRegionView (ArdourCanvas::Group *,
55                         RouteTimeAxisView&,
56                         boost::shared_ptr<ARDOUR::MidiRegion>,
57                         double initial_samples_per_unit,
58                         Gdk::Color& basic_color);
59
60         ~MidiRegionView ();
61
62         virtual void init (Gdk::Color& basic_color, bool wfd);
63
64         inline const boost::shared_ptr<ARDOUR::MidiRegion> midi_region() const
65                 { return boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region); }
66
67         inline MidiTimeAxisView* midi_view() const
68                 { return dynamic_cast<MidiTimeAxisView*>(&trackview); }
69
70         inline MidiStreamView* midi_stream_view() const
71                 { return midi_view()->midi_view(); }
72
73         void set_y_position_and_height (double, double);
74
75         void set_frame_color();
76
77         void redisplay_model();
78
79         GhostRegion* add_ghost (TimeAxisView&);
80
81         void add_note(const boost::shared_ptr<ARDOUR::Note> note);
82         void resolve_note(uint8_t note_num, double end_time);
83
84         void begin_write();
85         void end_write();
86         void extend_active_notes();
87
88         void create_note_at(double x, double y, double duration);
89
90         void display_model(boost::shared_ptr<ARDOUR::MidiModel> model);
91
92         /* This stuff is a bit boilerplatey ATM.  Work in progress. */
93
94         inline void start_delta_command(string name = "midi edit") {
95                 if (!_delta_command)
96                         _delta_command = _model->new_delta_command(name);
97         }
98
99         void command_add_note(const boost::shared_ptr<ARDOUR::Note> note) {
100                 if (_delta_command)
101                         _delta_command->add(note);
102         }
103
104         void command_remove_note(ArdourCanvas::CanvasMidiEvent* ev) {
105                 if (_delta_command && ev->note()) {
106                         _selection.erase(ev);
107                         _delta_command->remove(ev->note());
108                         ev->selected(true);
109                 }
110         }
111
112         void abort_command() {
113                 delete _delta_command;
114                 _delta_command = NULL;
115                 clear_selection();
116         }
117
118         void apply_command() {
119                 if (_delta_command) {
120                         _model->apply_command(_delta_command);
121                         _delta_command = NULL;
122                 }
123                 midi_view()->midi_track()->diskstream()->playlist_modified();
124         }
125
126         void   note_entered(ArdourCanvas::CanvasMidiEvent* ev);
127         void   unique_select(ArdourCanvas::CanvasMidiEvent* ev);
128         void   note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
129         void   note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
130         void   delete_selection();
131         size_t selection_size() { return _selection.size(); }
132
133         void move_selection(double dx, double dy);
134         void note_dropped(ArdourCanvas::CanvasMidiEvent* ev, double dt, uint8_t dnote);
135
136         /**
137          * This function is needed to subtract the region start in pixels
138          * from world coordinates submitted by the mouse
139          */
140         double get_position_pixels(void);
141
142         /**
143          * This function is called by CanvasMidiNote when resizing starts,
144          * i.e. when the user presses mouse-2 on the note
145          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
146          */
147         void  begin_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end);
148
149         /**
150          * This function is called while the user moves the mouse when resizing notes
151          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
152          * @param x the difference in mouse motion, ie the motion difference if relative=true
153          *           or the absolute mouse position (track-relative) if relative is false
154          * @param relative true if relative resizing is taking place, false if absolute resizing
155          */
156         void update_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end, double x, bool relative);
157
158         /**
159          * This function is called while the user releases the mouse button when resizing notes
160          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
161          * @param event_x the absolute mouse position (track-relative)
162          * @param relative true if relative resizing is taking place, false if absolute resizing
163          */
164         void commit_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end, double event_x, bool relative);
165
166         /**
167          * This function is called while the user adjusts the velocity on a selection of notes
168          * @param velocity the relative or absolute velocity, depending on the value of relative
169          * @param relative true if the given velocity represents a delta to be applied to all notes, false
170          *        if the absolute value of the note shoud be set
171          */
172         void change_velocity(uint8_t velocity, bool relative=false);
173         
174         /**
175          * This function is called when the user adjusts the midi channel of a selection of notes
176          * @param channel - the channel number of the new channel, zero-based
177          */
178         void change_channel(uint8_t channel);
179
180         enum MouseState { None, Pressed, SelectTouchDragging, SelectRectDragging, AddDragging, EraseTouchDragging };
181         MouseState mouse_state() const { return _mouse_state; }
182
183         struct NoteResizeData {
184                 ArdourCanvas::CanvasNote  *canvas_note;
185                 ArdourCanvas::SimpleRect  *resize_rect;
186                 double                     current_x;
187         };
188         
189         /**
190          * This function provides the snap function for region position relative coordinates
191          * for pixel units (double) instead of nframes_t
192          * @param x a pixel coordinate relative to region start
193          * @return the snapped pixel coordinate relative to region start
194          */
195         double snap_to_pixel(double x);
196
197         /**
198          * This function provides the snap function for region position relative coordinates
199          * for pixel units (double) instead of nframes_t
200          * @param x a pixel coordinate relative to region start
201          * @return the snapped nframes_t coordinate relative to region start
202          */
203         nframes_t snap_to_frame(double x);
204
205         /**
206          * This function provides the snap function for region position relative coordinates
207          * @param x a pixel coordinate relative to region start
208          * @return the snapped nframes_t coordinate relative to region start
209          */
210         nframes_t snap_to_frame(nframes_t x);
211         
212   protected:
213
214     /**
215      * this constructor allows derived types
216      * to specify their visibility requirements
217      * to the TimeAxisViewItem parent class
218     */
219     MidiRegionView (ArdourCanvas::Group *,
220                         RouteTimeAxisView&,
221                         boost::shared_ptr<ARDOUR::MidiRegion>,
222                         double samples_per_unit,
223                         Gdk::Color& basic_color,
224                         TimeAxisViewItem::Visibility);
225
226     void region_resized (ARDOUR::Change);
227
228     void set_flags (XMLNode *);
229     void store_flags ();
230
231         void reset_width_dependent_items (double pixel_width);
232
233   private:
234
235         void clear_events();
236         void switch_source(boost::shared_ptr<ARDOUR::Source> src);
237
238         bool canvas_event(GdkEvent* ev);
239         bool note_canvas_event(GdkEvent* ev);
240
241         void clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev);
242         void clear_selection() { clear_selection_except(NULL); }
243         void update_drag_selection(double last_x, double x, double last_y, double y);
244
245         double _default_note_length;
246
247         boost::shared_ptr<ARDOUR::MidiModel>        _model;
248         std::vector<ArdourCanvas::CanvasMidiEvent*> _events;
249         ArdourCanvas::CanvasNote**                  _active_notes;
250         ArdourCanvas::Group*                        _note_group;
251         ARDOUR::MidiModel::DeltaCommand*            _delta_command;
252
253         MouseState _mouse_state;
254         int _pressed_button;
255
256         /// currently selected CanvasMidiEvents
257         typedef std::set<ArdourCanvas::CanvasMidiEvent*> Selection;
258         Selection _selection;
259
260         /**
261          * this enables vanilla notes to be marked for selection
262          * they are added to _selection when redisplay_model is called
263          * this is necessary for selecting notes during/after model manipulations 
264          */
265         std::set< boost::shared_ptr<ARDOUR::Note> > _marked_for_selection;
266
267         std::vector<NoteResizeData *> _resize_data;
268 };
269
270 #endif /* __gtk_ardour_midi_region_view_h__ */