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