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