6bf60573421d48506628b286cc8c64a14d68eaed
[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_region.h>
27 #include <ardour/midi_model.h>
28 #include <ardour/types.h>
29
30 #include "region_view.h"
31 #include "midi_time_axis.h"
32 #include "time_axis_view_item.h"
33 #include "automation_line.h"
34 #include "enums.h"
35 #include "canvas.h"
36 #include "canvas-note.h"
37 #include "canvas-midi-event.h"
38
39 namespace ARDOUR {
40         class MidiRegion;
41         class MidiModel;
42 };
43
44 class MidiTimeAxisView;
45 class GhostRegion;
46 class AutomationTimeAxisView;
47
48 class MidiRegionView : public RegionView
49 {
50   public:
51         MidiRegionView (ArdourCanvas::Group *, 
52                         RouteTimeAxisView&,
53                         boost::shared_ptr<ARDOUR::MidiRegion>,
54                         double initial_samples_per_unit,
55                         Gdk::Color& basic_color);
56
57         ~MidiRegionView ();
58         
59         virtual void init (Gdk::Color& basic_color, bool wfd);
60         
61         inline const boost::shared_ptr<ARDOUR::MidiRegion> midi_region() const
62                 { return boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region); }
63
64         inline MidiTimeAxisView* midi_view() const
65                 { return dynamic_cast<MidiTimeAxisView*>(&trackview); }
66
67         inline MidiStreamView* midi_stream_view() const
68                 { return midi_view()->midi_view(); }
69         
70         void set_y_position_and_height (double, double);
71     
72     void show_region_editor ();
73
74     GhostRegion* add_ghost (AutomationTimeAxisView&);
75
76         void add_event(const ARDOUR::MidiEvent& ev);
77         void add_note(const ARDOUR::MidiModel::Note& note);
78
79         void begin_write();
80         void end_write();
81         void extend_active_notes();
82
83         void create_note_at(double x, double y, double dur);
84
85         void display_model(boost::shared_ptr<ARDOUR::MidiModel> model);
86
87         /* This stuff is a bit boilerplatey ATM.  Work in progress. */
88
89         inline void start_remove_command() {
90                 _command_mode = Remove;
91                 if (!_delta_command)
92                         _delta_command = _model->new_delta_command();
93         }
94         
95         inline void start_delta_command() {
96                 _command_mode = Delta;
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                 if (_command_mode == Remove && _delta_command && ev->note())
116                         _delta_command->remove(*ev->note());
117         }
118
119         //ARDOUR::MidiModel::DeltaCommand* delta_command() { return _delta_command; }
120
121         void abort_command() {
122                 delete _delta_command;
123                 _delta_command = NULL;
124                 _command_mode = None;
125         }
126
127         void apply_command() {
128                 if (_delta_command) {
129                         _model->apply_command(_delta_command);
130                         _delta_command = NULL;
131                 }
132                 _command_mode = None;
133         }
134
135         void   unique_select(ArdourCanvas::CanvasMidiEvent* ev);
136         void   note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
137         void   note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
138         void   delete_selection();
139         size_t selection_size() { return _selection.size(); }
140
141   protected:
142
143     /* this constructor allows derived types
144        to specify their visibility requirements
145        to the TimeAxisViewItem parent class
146     */
147     
148     MidiRegionView (ArdourCanvas::Group *, 
149                         RouteTimeAxisView&,
150                         boost::shared_ptr<ARDOUR::MidiRegion>,
151                         double samples_per_unit,
152                         Gdk::Color& basic_color,
153                         TimeAxisViewItem::Visibility);
154     
155     void region_resized (ARDOUR::Change);
156
157     void set_flags (XMLNode *);
158     void store_flags ();
159     
160         void reset_width_dependent_items (double pixel_width);
161
162   private:
163
164         void redisplay_model();
165         void clear_events();
166
167         bool canvas_event(GdkEvent* ev);
168         bool note_canvas_event(GdkEvent* ev);
169         
170         void clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev);
171
172         double _default_note_length;
173
174         boost::shared_ptr<ARDOUR::MidiModel> _model;
175         std::vector<ArdourCanvas::Item*>     _events;
176         ArdourCanvas::CanvasNote**           _active_notes;
177         ARDOUR::MidiModel::DeltaCommand*     _delta_command;
178
179         typedef std::set<ArdourCanvas::CanvasMidiEvent*> Selection;
180         Selection _selection;
181         
182         enum CommandMode { None, Remove, Delta };
183         CommandMode _command_mode;
184 };
185
186 #endif /* __gtk_ardour_midi_region_view_h__ */