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