Genericificationalizeified AudioFilter (now Filter).
[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 redisplay_model();
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                         ev->selected(true);
117                         _delta_command->remove(*ev->note());
118                 }
119         }
120
121         //ARDOUR::MidiModel::DeltaCommand* delta_command() { return _delta_command; }
122
123         void abort_command() {
124                 delete _delta_command;
125                 _delta_command = NULL;
126                 _command_mode = None;
127                 clear_selection();
128         }
129
130         void apply_command() {
131                 if (_delta_command) {
132                         _model->apply_command(_delta_command);
133                         _delta_command = NULL;
134                 }
135                 _command_mode = None;
136         }
137
138         void   unique_select(ArdourCanvas::CanvasMidiEvent* ev);
139         void   note_selected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
140         void   note_deselected(ArdourCanvas::CanvasMidiEvent* ev, bool add);
141         void   delete_selection();
142         size_t selection_size() { return _selection.size(); }
143
144   protected:
145
146     /* this constructor allows derived types
147        to specify their visibility requirements
148        to the TimeAxisViewItem parent class
149     */
150     
151     MidiRegionView (ArdourCanvas::Group *, 
152                         RouteTimeAxisView&,
153                         boost::shared_ptr<ARDOUR::MidiRegion>,
154                         double samples_per_unit,
155                         Gdk::Color& basic_color,
156                         TimeAxisViewItem::Visibility);
157     
158     void region_resized (ARDOUR::Change);
159
160     void set_flags (XMLNode *);
161     void store_flags ();
162     
163         void reset_width_dependent_items (double pixel_width);
164
165   private:
166
167         void clear_events();
168
169         bool canvas_event(GdkEvent* ev);
170         bool note_canvas_event(GdkEvent* ev);
171         
172         void clear_selection_except(ArdourCanvas::CanvasMidiEvent* ev);
173         void clear_selection() { clear_selection_except(NULL); }
174
175         double _default_note_length;
176
177         boost::shared_ptr<ARDOUR::MidiModel> _model;
178         std::vector<ArdourCanvas::Item*>     _events;
179         ArdourCanvas::CanvasNote**           _active_notes;
180         ARDOUR::MidiModel::DeltaCommand*     _delta_command;
181
182         typedef std::set<ArdourCanvas::CanvasMidiEvent*> Selection;
183         Selection _selection;
184         
185         enum CommandMode { None, Remove, Delta };
186         CommandMode _command_mode;
187 };
188
189 #endif /* __gtk_ardour_midi_region_view_h__ */