fix load+save of plugin parameter automation
[ardour.git] / gtk2_ardour / ghostregion.cc
1 /*
2     Copyright (C) 2000-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
20 #include "evoral/Note.hpp"
21 #include "ardour_ui.h"
22 #include "automation_time_axis.h"
23 #include "canvas-hit.h"
24 #include "canvas-note.h"
25 #include "ghostregion.h"
26 #include "midi_streamview.h"
27 #include "midi_time_axis.h"
28 #include "rgb_macros.h"
29 #include "simplerect.h"
30 #include "waveview.h"
31
32 using namespace std;
33 using namespace Editing;
34 using namespace ArdourCanvas;
35 using namespace ARDOUR;
36
37 PBD::Signal1<void,GhostRegion*> GhostRegion::CatchDeletion;
38
39 GhostRegion::GhostRegion (ArdourCanvas::Group* parent, TimeAxisView& tv, TimeAxisView& source_tv, double initial_pos)
40         : trackview (tv)
41         , source_trackview (source_tv)
42 {
43         group = new ArdourCanvas::Group (*parent);
44         group->property_x() = initial_pos;
45         group->property_y() = 0.0;
46
47         base_rect = new ArdourCanvas::SimpleRect (*group);
48         base_rect->property_x1() = (double) 0.0;
49         base_rect->property_y1() = (double) 0.0;
50         base_rect->property_y2() = (double) trackview.current_height();
51         base_rect->property_outline_what() = (guint32) 0;
52
53         if (!is_automation_ghost()) {
54                 base_rect->hide();
55         }
56
57         GhostRegion::set_colors();
58
59         /* the parent group of a ghostregion is a dedicated group for ghosts,
60            so the new ghost would want to get to the top of that group*/
61         group->raise_to_top ();
62 }
63
64 GhostRegion::~GhostRegion ()
65 {
66         CatchDeletion (this);
67         delete base_rect;
68         delete group;
69 }
70
71 void
72 GhostRegion::set_duration (double units)
73 {
74         base_rect->property_x2() = units;
75 }
76
77 void
78 GhostRegion::set_height ()
79 {
80         base_rect->property_y2() = (double) trackview.current_height();
81 }
82
83 void
84 GhostRegion::set_colors ()
85 {
86         if (is_automation_ghost()) {
87                 base_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackBase.get();
88                 base_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackBase.get();
89         }
90 }
91
92 guint
93 GhostRegion::source_track_color(unsigned char alpha) {
94         Gdk::Color color = source_trackview.color();
95         unsigned char r,g,b ;
96         r = color.get_red()/256;
97         g = color.get_green()/256;
98         b = color.get_blue()/256;
99         return RGBA_TO_UINT(r, g, b, alpha);
100 }
101
102 bool
103 GhostRegion::is_automation_ghost() {
104         return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
105 }
106
107 AudioGhostRegion::AudioGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
108         : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos) {
109 }
110
111 void
112 AudioGhostRegion::set_samples_per_unit (double spu)
113 {
114         for (vector<WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
115                 (*i)->property_samples_per_unit() = spu;
116         }
117 }
118
119 void
120 AudioGhostRegion::set_height ()
121 {
122         gdouble ht;
123         vector<WaveView*>::iterator i;
124         uint32_t n;
125
126         GhostRegion::set_height();
127
128         ht = ((trackview.current_height()) / (double) waves.size());
129
130         for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
131                 gdouble yoff = n * ht;
132                 (*i)->property_height() = ht;
133                 (*i)->property_y() = yoff;
134         }
135 }
136
137 void
138 AudioGhostRegion::set_colors ()
139 {
140         GhostRegion::set_colors();
141         guint fill_color;
142
143         if (is_automation_ghost()) {
144                 fill_color = ARDOUR_UI::config()->canvasvar_GhostTrackWaveFill.get();
145         }
146         else {
147                 fill_color = source_track_color(200);
148         }
149
150         for (uint32_t n=0; n < waves.size(); ++n) {
151                 waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWave.get();
152                 waves[n]->property_fill_color() = fill_color;
153                 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWaveClip.get();
154                 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_GhostTrackZeroLine.get();
155         }
156 }
157
158 /*
159  * This is the general constructor, and is called when the destination timeaxisview doesn't have
160  * a midistreamview. But what to do when positioning the midi ghost here? For example, there is
161  * no range controller in these tracks. maybe show the whole range.
162  */
163 MidiGhostRegion::MidiGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
164         : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
165 {
166
167         base_rect->lower_to_bottom();
168 }
169
170 MidiGhostRegion::MidiGhostRegion(MidiStreamView& msv, TimeAxisView& source_tv, double initial_unit_pos)
171         : GhostRegion(msv.midi_underlay_group, msv.trackview(), source_tv, initial_unit_pos)
172 {
173         base_rect->lower_to_bottom();
174 }
175
176 MidiGhostRegion::~MidiGhostRegion()
177 {
178         //clear_events();
179 }
180
181 MidiGhostRegion::Event::Event(ArdourCanvas::CanvasNoteEvent* e)
182         : event(e)
183 {
184 }
185
186 MidiGhostRegion::Note::Note(ArdourCanvas::CanvasNote* n, ArdourCanvas::Group* g)
187         : Event(n)
188 {
189         rect = new ArdourCanvas::SimpleRect(*g, n->x1(), n->y1(), n->x2(), n->y2());
190 }
191
192 MidiGhostRegion::Note::~Note()
193 {
194         //delete rect;
195 }
196
197 void
198 MidiGhostRegion::Note::x_changed()
199 {
200         rect->property_x1() = event->x1();
201         rect->property_x2() = event->x2();
202 }
203
204 MidiGhostRegion::Hit::Hit(ArdourCanvas::CanvasHit* h, ArdourCanvas::Group*)
205         : Event(h)
206 {
207         cerr << "Hit ghost item does not work yet" << endl;
208 }
209
210 MidiGhostRegion::Hit::~Hit()
211 {
212 }
213
214 void
215 MidiGhostRegion::Hit::x_changed()
216 {
217 }
218
219 void
220 MidiGhostRegion::set_samples_per_unit (double /*spu*/)
221 {
222 }
223
224 MidiStreamView*
225 MidiGhostRegion::midi_view()
226 {
227         MidiTimeAxisView* mtv;
228
229         if ((mtv = dynamic_cast<MidiTimeAxisView*>(&trackview)) != 0) {
230                 return mtv->midi_view();
231         }
232         else {
233                 return 0;
234         }
235 }
236
237 void
238 MidiGhostRegion::set_height()
239 {
240         GhostRegion::set_height();
241         update_range();
242 }
243
244 void
245 MidiGhostRegion::set_colors()
246 {
247         MidiGhostRegion::Note* note;
248         guint fill = source_track_color(200);
249
250         GhostRegion::set_colors();
251
252         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
253                 if ((note = dynamic_cast<MidiGhostRegion::Note*>(*it)) != 0) {
254                         note->rect->property_fill_color_rgba() = fill;
255                         note->rect->property_outline_color_rgba() =  ARDOUR_UI::config()->canvasvar_GhostTrackMidiOutline.get();
256                 }
257         }
258 }
259
260 void
261 MidiGhostRegion::update_range()
262 {
263         MidiStreamView* mv = midi_view();
264
265         if (!mv) {
266                 return;
267         }
268
269         MidiGhostRegion::Note* note;
270         uint8_t note_num;
271         double y;
272
273         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
274                 if ((note = dynamic_cast<MidiGhostRegion::Note*>(*it)) != 0) {
275                         note_num = note->event->note()->note();
276
277                         if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
278                                 note->rect->hide();
279                         }
280                         else {
281                                 note->rect->show();
282                                 y = mv->note_to_y(note_num);
283                                 note->rect->property_y1() = y;
284                                 note->rect->property_y2() = y + mv->note_height();
285                         }
286                 }
287         }
288 }
289
290 void
291 MidiGhostRegion::add_note(ArdourCanvas::CanvasNote* n)
292 {
293         Note* note = new Note(n, group);
294         events.push_back(note);
295
296         note->rect->property_fill_color_rgba() = source_track_color(200);
297         note->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackMidiOutline.get();
298
299         MidiStreamView* mv = midi_view();
300
301         if (mv) {
302                 const uint8_t note_num = n->note()->note();
303
304                 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
305                         note->rect->hide();
306                 } else {
307                         const double y = mv->note_to_y(note_num);
308                         note->rect->property_y1() = y;
309                         note->rect->property_y2() = y + mv->note_height();
310                 }
311         }
312 }
313
314 void
315 MidiGhostRegion::add_hit(ArdourCanvas::CanvasHit* /*h*/)
316 {
317         //events.push_back(new Hit(h, group));
318 }
319
320 void
321 MidiGhostRegion::clear_events()
322 {
323         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
324                 delete *it;
325         }
326
327         events.clear();
328 }
329