fb3e4d344954275a1eaacc279ca506e93ec2085a
[ardour.git] / gtk2_ardour / midi_region_view.h
1 /*
2     Copyright (C) 2001-2006 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
50 class MidiRegionView : public RegionView
51 {
52   public:
53         MidiRegionView (ArdourCanvas::Group *, 
54                         RouteTimeAxisView&,
55                         boost::shared_ptr<ARDOUR::MidiRegion>,
56                         double initial_samples_per_unit,
57                         Gdk::Color& basic_color);
58
59         ~MidiRegionView ();
60         
61         virtual void init (Gdk::Color& basic_color, bool wfd);
62         
63         inline const boost::shared_ptr<ARDOUR::MidiRegion> midi_region() const
64                 { return boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region); }
65
66         inline MidiTimeAxisView* midi_view() const
67                 { return dynamic_cast<MidiTimeAxisView*>(&trackview); }
68
69         inline MidiStreamView* midi_stream_view() const
70                 { return midi_view()->midi_view(); }
71         
72         void set_y_position_and_height (double, double);
73         
74         void redisplay_model();
75
76     GhostRegion* add_ghost (AutomationTimeAxisView&);
77
78         void add_event(const ARDOUR::MidiEvent& ev);
79         void add_note(const ARDOUR::MidiModel::Note& note);
80
81         void begin_write();
82         void end_write();
83         void extend_active_notes();
84
85         void create_note_at(double x, double y, double dur);
86
87         void display_model(boost::shared_ptr<ARDOUR::MidiModel> model);
88
89         /* This stuff is a bit boilerplatey ATM.  Work in progress. */
90
91         inline void start_remove_command() {
92                 if (!_delta_command)
93                         _delta_command = _model->new_delta_command();
94         }
95         
96         inline void start_delta_command() {
97                 if (!_delta_command)
98                         _delta_command = _model->new_delta_command();
99         }
100
101         void command_remove_note(ArdourCanvas::CanvasMidiEvent* ev) {
102                 if (_delta_command && ev->note()) {
103                         _delta_command->remove(*ev->note());
104                         ev->selected(true);
105                 }
106         }
107         
108         void command_add_note(ARDOUR::MidiModel::Note& note) {
109                 if (_delta_command) {
110                         _delta_command->add(note);
111                 }
112         }
113
114         void note_entered(ArdourCanvas::CanvasMidiEvent* ev) {
115                 cerr << "ENTERED, STATE = " << _mouse_state << endl;
116                 if (_mouse_state == EraseDragging) {
117                         start_delta_command();
118                         ev->selected(true);
119                         _delta_command->remove(*ev->note());
120                 }
121         }
122
123         void abort_command() {
124                 delete _delta_command;
125                 _delta_command = NULL;
126                 clear_selection();
127         }
128
129         void apply_command() {
130                 if (_delta_command) {
131                         _model->apply_command(_delta_command);
132                         _delta_command = NULL;
133                 }
134                 midi_view()->midi_track()->diskstream()->playlist_modified();
135         }
136
137         void   unique_select(ArdourCanvas::CanvasMidiEvent* ev);
138         void   note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
139         void   note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
140         void   delete_selection();
141         size_t selection_size() { return _selection.size(); }
142
143         void move_selection(double dx, double dy);
144         void note_dropped(ArdourCanvas::CanvasMidiEvent* ev, double dt, uint8_t dnote);
145
146   protected:
147
148     /* this constructor allows derived types
149        to specify their visibility requirements
150        to the TimeAxisViewItem parent class
151     */
152     
153     MidiRegionView (ArdourCanvas::Group *, 
154                         RouteTimeAxisView&,
155                         boost::shared_ptr<ARDOUR::MidiRegion>,
156                         double samples_per_unit,
157                         Gdk::Color& basic_color,
158                         TimeAxisViewItem::Visibility);
159     
160     void region_resized (ARDOUR::Change);
161
162     void set_flags (XMLNode *);
163     void store_flags ();
164     
165         void reset_width_dependent_items (double pixel_width);
166
167   private:
168
169         void clear_events();
170
171         bool canvas_event(GdkEvent* ev);
172         bool note_canvas_event(GdkEvent* ev);
173         
174         void clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev);
175         void clear_selection() { clear_selection_except(NULL); }
176         void update_drag_selection(double last_x, double x, double last_y, double y);
177
178         double _default_note_length;
179
180         boost::shared_ptr<ARDOUR::MidiModel>        _model;
181         std::vector<ArdourCanvas::CanvasMidiEvent*> _events;
182         ArdourCanvas::CanvasNote**                  _active_notes;
183         ArdourCanvas::Group*                        _note_group;
184         ARDOUR::MidiModel::DeltaCommand*            _delta_command;
185         
186         enum MouseState { None, Pressed, SelectDragging, AddDragging, EraseDragging };
187         MouseState _mouse_state;
188         int _pressed_button;
189
190         typedef std::set<ArdourCanvas::CanvasMidiEvent*> Selection;
191         Selection _selection;
192 };
193
194 #endif /* __gtk_ardour_midi_region_view_h__ */