Various color tweaks.
[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 "canvas/container.h"
22 #include "canvas/rectangle.h"
23 #include "canvas/wave_view.h"
24 #include "canvas/debug.h"
25
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"
32 #include "note.h"
33
34 using namespace std;
35 using namespace Editing;
36 using namespace ArdourCanvas;
37 using namespace ARDOUR;
38
39 PBD::Signal1<void,GhostRegion*> GhostRegion::CatchDeletion;
40
41 GhostRegion::GhostRegion (ArdourCanvas::Container* parent, TimeAxisView& tv, TimeAxisView& source_tv, double initial_pos)
42         : trackview (tv)
43         , source_trackview (source_tv)
44 {
45         group = new ArdourCanvas::Container (parent);
46         CANVAS_DEBUG_NAME (group, "ghost region");
47         group->set_position (ArdourCanvas::Duple (initial_pos, 0));
48
49         base_rect = new ArdourCanvas::TimeRectangle (group);
50         CANVAS_DEBUG_NAME (base_rect, "ghost region rect");
51         base_rect->set_x0 (0);
52         base_rect->set_y0 (1.0);
53         base_rect->set_y1 (trackview.current_height());
54         base_rect->set_outline (false);
55
56         if (!is_automation_ghost()) {
57                 base_rect->hide();
58         }
59
60         GhostRegion::set_colors();
61
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 ();
65 }
66
67 GhostRegion::~GhostRegion ()
68 {
69         CatchDeletion (this);
70         delete base_rect;
71         delete group;
72 }
73
74 void
75 GhostRegion::set_duration (double units)
76 {
77         base_rect->set_x1 (units);
78 }
79
80 void
81 GhostRegion::set_height ()
82 {
83         base_rect->set_y1 (trackview.current_height());
84 }
85
86 void
87 GhostRegion::set_colors ()
88 {
89         if (is_automation_ghost()) {
90                 base_rect->set_fill_color (ARDOUR_UI::config()->color_mod ("ghost track base", "ghost track base"));
91         }
92 }
93
94 guint
95 GhostRegion::source_track_color(unsigned char alpha)
96 {
97         Gdk::Color color = source_trackview.color();
98         return RGBA_TO_UINT (color.get_red() / 256, color.get_green() / 256, color.get_blue() / 256, alpha);
99 }
100
101 bool
102 GhostRegion::is_automation_ghost()
103 {
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 }
112
113 void
114 AudioGhostRegion::set_samples_per_pixel (double fpp)
115 {
116         for (vector<WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
117                 (*i)->set_samples_per_pixel (fpp);
118         }
119 }
120
121 void
122 AudioGhostRegion::set_height ()
123 {
124         vector<WaveView*>::iterator i;
125         uint32_t n;
126
127         GhostRegion::set_height();
128
129         double const ht = ((trackview.current_height()) / (double) waves.size());
130
131         for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
132                 (*i)->set_height (ht);
133                 (*i)->set_y_position (n * ht);
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()->color ("ghost track wave fill");
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]->set_outline_color (ARDOUR_UI::config()->color ("ghost track wave"));
152                 waves[n]->set_fill_color (fill_color);
153                 waves[n]->set_clip_color (ARDOUR_UI::config()->color ("ghost track wave clip"));
154                 waves[n]->set_zero_color (ARDOUR_UI::config()->color ("ghost track zero line"));
155         }
156 }
157
158 /** The general constructor; called when the destination timeaxisview doesn't have
159  *  a midistreamview.
160  *
161  *  @param tv TimeAxisView that this ghost region is on.
162  *  @param source_tv TimeAxisView that we are the ghost for.
163  */
164 MidiGhostRegion::MidiGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
165         : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
166         , _optimization_iterator (events.end ())
167 {
168         base_rect->lower_to_bottom();
169         update_range ();
170
171         midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
172 }
173
174 /**
175  *  @param msv MidiStreamView that this ghost region is on.
176  *  @param source_tv TimeAxisView that we are the ghost for.
177  */
178 MidiGhostRegion::MidiGhostRegion(MidiStreamView& msv, TimeAxisView& source_tv, double initial_unit_pos)
179         : GhostRegion(msv.midi_underlay_group, msv.trackview(), source_tv, initial_unit_pos)
180         , _optimization_iterator (events.end ())
181 {
182         base_rect->lower_to_bottom();
183         update_range ();
184
185         midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
186 }
187
188 MidiGhostRegion::~MidiGhostRegion()
189 {
190         clear_events ();
191 }
192
193 MidiGhostRegion::GhostEvent::GhostEvent (NoteBase* e, ArdourCanvas::Container* g)
194         : event (e)
195 {
196         rect = new ArdourCanvas::Rectangle (g, ArdourCanvas::Rect (e->x0(), e->y0(), e->x1(), e->y1()));
197         CANVAS_DEBUG_NAME (rect, "ghost note rect");
198 }
199
200 MidiGhostRegion::GhostEvent::~GhostEvent ()
201 {
202         /* event is not ours to delete */
203         delete rect;
204 }
205
206 void
207 MidiGhostRegion::set_samples_per_pixel (double /*spu*/)
208 {
209 }
210
211 /** @return MidiStreamView that we are providing a ghost for */
212 MidiStreamView*
213 MidiGhostRegion::midi_view ()
214 {
215         StreamView* sv = source_trackview.view ();
216         assert (sv);
217         MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
218         assert (msv);
219
220         return msv;
221 }
222
223 void
224 MidiGhostRegion::set_height ()
225 {
226         GhostRegion::set_height();
227         update_range();
228 }
229
230 void
231 MidiGhostRegion::set_colors()
232 {
233         GhostRegion::set_colors();
234
235         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
236                 (*it)->rect->set_fill_color (ARDOUR_UI::config()->color_mod((*it)->event->base_color(), "ghost track midi fill"));
237                 (*it)->rect->set_outline_color (ARDOUR_UI::config()->color ("ghost track midi outline"));
238         }
239 }
240
241 void
242 MidiGhostRegion::update_range ()
243 {
244         MidiStreamView* mv = midi_view();
245
246         if (!mv) {
247                 return;
248         }
249
250         double const h = std::max(1., floor (trackview.current_height() / double (mv->contents_note_range ())) -1);
251         double const s = trackview.current_height() / double (mv->contents_note_range ());
252
253         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
254                 uint8_t const note_num = (*it)->event->note()->note();
255
256                 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
257                         (*it)->rect->hide();
258                 } else {
259                         (*it)->rect->show();
260                         double const y = trackview.current_height() - (note_num + 1 - mv->lowest_note()) * s;
261                         (*it)->rect->set_y0 (y);
262                         (*it)->rect->set_y1 (y + h);
263                 }
264         }
265 }
266
267 void
268 MidiGhostRegion::add_note (NoteBase* n)
269 {
270         GhostEvent* event = new GhostEvent (n, group);
271         events.push_back (event);
272
273         event->rect->set_fill_color (ARDOUR_UI::config()->color_mod(n->base_color(), "ghost track midi fill"));
274         event->rect->set_outline_color (ARDOUR_UI::config()->color ("ghost track midi outline"));
275
276         MidiStreamView* mv = midi_view();
277
278         if (mv) {
279                 const uint8_t note_num = n->note()->note();
280
281                 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
282                         event->rect->hide();
283                 } else {
284                         const double y = mv->note_to_y(note_num);
285                         event->rect->set_y0 (y);
286                         event->rect->set_y1 (y + std::max(1., floor(mv->note_height()) -1));
287                 }
288         }
289 }
290
291 void
292 MidiGhostRegion::clear_events()
293 {
294         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
295                 delete *it;
296         }
297
298         events.clear();
299         _optimization_iterator = events.end ();
300 }
301
302 /** Update the x positions of our representation of a parent's note.
303  *  @param parent The CanvasNote from the parent MidiRegionView.
304  */
305 void
306 MidiGhostRegion::update_note (NoteBase* parent)
307 {
308         GhostEvent* ev = find_event (parent);
309         if (!ev) {
310                 return;
311         }
312
313         double const x1 = parent->x0 ();
314         double const x2 = parent->x1 ();
315         ev->rect->set_x0 (x1);
316         ev->rect->set_x1 (x2);
317 }
318
319 void
320 MidiGhostRegion::remove_note (NoteBase* note)
321 {
322         GhostEvent* ev = find_event (note);
323         if (!ev) {
324                 return;
325         }
326
327         events.remove (ev);
328         delete ev;
329         _optimization_iterator = events.end ();
330 }
331
332 /** Given a note in our parent region (ie the actual MidiRegionView), find our
333  *  representation of it.
334  *  @return Our Event, or 0 if not found.
335  */
336
337 MidiGhostRegion::GhostEvent *
338 MidiGhostRegion::find_event (NoteBase* parent)
339 {
340         /* we are using _optimization_iterator to speed up the common case where a caller
341            is going through our notes in order.
342         */
343
344         if (_optimization_iterator != events.end()) {
345                 ++_optimization_iterator;
346         }
347
348         if (_optimization_iterator != events.end() && (*_optimization_iterator)->event == parent) {
349                 return *_optimization_iterator;
350         }
351
352         for (_optimization_iterator = events.begin(); _optimization_iterator != events.end(); ++_optimization_iterator) {
353                 if ((*_optimization_iterator)->event == parent) {
354                         return *_optimization_iterator;
355                 }
356         }
357
358         return 0;
359 }