2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include "evoral/Note.hpp"
21 #include "canvas/container.h"
22 #include "canvas/rectangle.h"
23 #include "canvas/wave_view.h"
24 #include "canvas/debug.h"
26 #include "ardour_ui.h"
27 #include "automation_time_axis.h"
28 #include "ghostregion.h"
29 #include "midi_streamview.h"
30 #include "midi_time_axis.h"
31 #include "rgb_macros.h"
35 using namespace Editing;
36 using namespace ArdourCanvas;
37 using namespace ARDOUR;
39 PBD::Signal1<void,GhostRegion*> GhostRegion::CatchDeletion;
41 GhostRegion::GhostRegion (ArdourCanvas::Container* parent, TimeAxisView& tv, TimeAxisView& source_tv, double initial_pos)
43 , source_trackview (source_tv)
45 group = new ArdourCanvas::Container (parent);
46 CANVAS_DEBUG_NAME (group, "ghost region");
47 group->set_position (ArdourCanvas::Duple (initial_pos, 0));
49 base_rect = new ArdourCanvas::Rectangle (group);
50 CANVAS_DEBUG_NAME (base_rect, "ghost region rect");
51 base_rect->set_x0 (0);
52 base_rect->set_y0 (0.0);
53 base_rect->set_y1 (trackview.current_height() - 1.0);
54 base_rect->set_outline_what (ArdourCanvas::Rectangle::What (0));
56 if (!is_automation_ghost()) {
60 GhostRegion::set_colors();
62 /* the parent group of a ghostregion is a dedicated group for ghosts,
63 so the new ghost would want to get to the top of that group*/
64 group->raise_to_top ();
67 GhostRegion::~GhostRegion ()
75 GhostRegion::set_duration (double units)
77 base_rect->set_x1 (units);
81 GhostRegion::set_height ()
83 base_rect->set_y1 (trackview.current_height());
87 GhostRegion::set_colors ()
89 if (is_automation_ghost()) {
90 base_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_GhostTrackBase());
91 base_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_GhostTrackBase());
96 GhostRegion::source_track_color(unsigned char alpha)
98 Gdk::Color color = source_trackview.color();
99 return RGBA_TO_UINT (color.get_red() / 256, color.get_green() / 256, color.get_blue() / 256, alpha);
103 GhostRegion::is_automation_ghost()
105 return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
108 AudioGhostRegion::AudioGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
109 : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
115 AudioGhostRegion::set_samples_per_pixel (double fpp)
117 for (vector<WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
118 (*i)->set_samples_per_pixel (fpp);
123 AudioGhostRegion::set_height ()
125 vector<WaveView*>::iterator i;
128 GhostRegion::set_height();
130 double const ht = ((trackview.current_height()) / (double) waves.size());
132 for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
133 (*i)->set_height (ht);
134 (*i)->set_y_position (n * ht);
139 AudioGhostRegion::set_colors ()
141 GhostRegion::set_colors();
144 if (is_automation_ghost()) {
145 fill_color = ARDOUR_UI::config()->get_canvasvar_GhostTrackWaveFill();
148 fill_color = source_track_color(200);
151 for (uint32_t n=0; n < waves.size(); ++n) {
152 waves[n]->set_outline_color (ARDOUR_UI::config()->get_canvasvar_GhostTrackWave());
153 waves[n]->set_fill_color (fill_color);
154 waves[n]->set_clip_color (ARDOUR_UI::config()->get_canvasvar_GhostTrackWaveClip());
155 waves[n]->set_zero_color (ARDOUR_UI::config()->get_canvasvar_GhostTrackZeroLine());
159 /** The general constructor; called when the destination timeaxisview doesn't have
162 * @param tv TimeAxisView that this ghost region is on.
163 * @param source_tv TimeAxisView that we are the ghost for.
165 MidiGhostRegion::MidiGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
166 : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
167 , _optimization_iterator (events.end ())
169 base_rect->lower_to_bottom();
172 midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
176 * @param msv MidiStreamView that this ghost region is on.
177 * @param source_tv TimeAxisView that we are the ghost for.
179 MidiGhostRegion::MidiGhostRegion(MidiStreamView& msv, TimeAxisView& source_tv, double initial_unit_pos)
180 : GhostRegion(msv.midi_underlay_group, msv.trackview(), source_tv, initial_unit_pos)
181 , _optimization_iterator (events.end ())
183 base_rect->lower_to_bottom();
186 midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
189 MidiGhostRegion::~MidiGhostRegion()
194 MidiGhostRegion::GhostEvent::GhostEvent (NoteBase* e, ArdourCanvas::Container* g)
197 rect = new ArdourCanvas::Rectangle (g, ArdourCanvas::Rect (e->x0(), e->y0(), e->x1(), e->y1()));
198 CANVAS_DEBUG_NAME (rect, "ghost note rect");
201 MidiGhostRegion::GhostEvent::~GhostEvent ()
203 /* event is not ours to delete */
208 MidiGhostRegion::set_samples_per_pixel (double /*spu*/)
212 /** @return MidiStreamView that we are providing a ghost for */
214 MidiGhostRegion::midi_view ()
216 StreamView* sv = source_trackview.view ();
218 MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
225 MidiGhostRegion::set_height ()
227 GhostRegion::set_height();
232 MidiGhostRegion::set_colors()
234 guint fill = source_track_color(200);
236 GhostRegion::set_colors();
238 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
239 (*it)->rect->set_fill_color (fill);
240 (*it)->rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_GhostTrackMidiOutline());
245 MidiGhostRegion::update_range ()
247 MidiStreamView* mv = midi_view();
253 double const h = trackview.current_height() / double (mv->contents_note_range ());
255 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
256 uint8_t const note_num = (*it)->event->note()->note();
258 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
262 double const y = trackview.current_height() - (note_num + 1 - mv->lowest_note()) * h + 1;
263 (*it)->rect->set_y0 (y);
264 (*it)->rect->set_y1 (y + h);
270 MidiGhostRegion::add_note (NoteBase* n)
272 GhostEvent* event = new GhostEvent (n, group);
273 events.push_back (event);
275 event->rect->set_fill_color (source_track_color(200));
276 event->rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_GhostTrackMidiOutline());
278 MidiStreamView* mv = midi_view();
281 const uint8_t note_num = n->note()->note();
283 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
286 const double y = mv->note_to_y(note_num);
287 event->rect->set_y0 (y);
288 event->rect->set_y1 (y + mv->note_height());
294 MidiGhostRegion::clear_events()
296 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
301 _optimization_iterator = events.end ();
304 /** Update the x positions of our representation of a parent's note.
305 * @param parent The CanvasNote from the parent MidiRegionView.
308 MidiGhostRegion::update_note (NoteBase* parent)
310 GhostEvent* ev = find_event (parent);
315 double const x1 = parent->x0 ();
316 double const x2 = parent->x1 ();
317 ev->rect->set_x0 (x1);
318 ev->rect->set_x1 (x2);
322 MidiGhostRegion::remove_note (NoteBase* note)
324 GhostEvent* ev = find_event (note);
331 _optimization_iterator = events.end ();
334 /** Given a note in our parent region (ie the actual MidiRegionView), find our
335 * representation of it.
336 * @return Our Event, or 0 if not found.
339 MidiGhostRegion::GhostEvent *
340 MidiGhostRegion::find_event (NoteBase* parent)
342 /* we are using _optimization_iterator to speed up the common case where a caller
343 is going through our notes in order.
346 if (_optimization_iterator != events.end()) {
347 ++_optimization_iterator;
350 if (_optimization_iterator != events.end() && (*_optimization_iterator)->event == parent) {
351 return *_optimization_iterator;
354 for (_optimization_iterator = events.begin(); _optimization_iterator != events.end(); ++_optimization_iterator) {
355 if ((*_optimization_iterator)->event == parent) {
356 return *_optimization_iterator;