use new action map API instead of ActionManager::get_action
[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
22 #include "ardour/parameter_descriptor.h"
23
24 #include "canvas/container.h"
25 #include "canvas/polygon.h"
26 #include "canvas/rectangle.h"
27 #include "canvas/debug.h"
28
29 #include "waveview/wave_view.h"
30
31 #include "automation_time_axis.h"
32 #include "ghostregion.h"
33 #include "midi_streamview.h"
34 #include "midi_time_axis.h"
35 #include "region_view.h"
36 #include "midi_region_view.h"
37 #include "rgb_macros.h"
38 #include "note.h"
39 #include "hit.h"
40 #include "ui_config.h"
41
42 using namespace std;
43 using namespace Editing;
44 using namespace ARDOUR;
45 using ArdourCanvas::Duple;
46
47 GhostRegion::GhostRegion(RegionView& rv,
48                          ArdourCanvas::Container* parent,
49                          TimeAxisView& tv,
50                          TimeAxisView& source_tv,
51                          double initial_pos)
52     : parent_rv(rv)
53     , trackview(tv)
54     , source_trackview(source_tv)
55 {
56         group = new ArdourCanvas::Container (parent);
57         CANVAS_DEBUG_NAME (group, "ghost region");
58         group->set_position (ArdourCanvas::Duple (initial_pos, 0));
59
60         base_rect = new ArdourCanvas::Rectangle (group);
61         CANVAS_DEBUG_NAME (base_rect, "ghost region rect");
62         base_rect->set_x0 (0);
63         base_rect->set_y0 (1.0);
64         base_rect->set_y1 (trackview.current_height());
65         base_rect->set_outline (false);
66
67         if (!is_automation_ghost()) {
68                 base_rect->hide();
69         }
70
71         GhostRegion::set_colors();
72
73         /* the parent group of a ghostregion is a dedicated group for ghosts,
74            so the new ghost would want to get to the top of that group*/
75         group->raise_to_top ();
76 }
77
78 GhostRegion::~GhostRegion ()
79 {
80         parent_rv.remove_ghost(this);
81         trackview.erase_ghost(this);
82         delete base_rect;
83         delete group;
84 }
85
86 void
87 GhostRegion::set_duration (double units)
88 {
89         base_rect->set_x1 (units);
90 }
91
92 void
93 GhostRegion::set_height ()
94 {
95         base_rect->set_y1 (trackview.current_height());
96 }
97
98 void
99 GhostRegion::set_colors ()
100 {
101         if (is_automation_ghost()) {
102                 base_rect->set_fill_color (UIConfiguration::instance().color_mod ("ghost track base", "ghost track base"));
103         }
104 }
105
106 guint
107 GhostRegion::source_track_color(unsigned char alpha)
108 {
109         Gdk::Color color = source_trackview.color();
110         return RGBA_TO_UINT (color.get_red() / 256, color.get_green() / 256, color.get_blue() / 256, alpha);
111 }
112
113 bool
114 GhostRegion::is_automation_ghost()
115 {
116         return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
117 }
118
119 AudioGhostRegion::AudioGhostRegion(RegionView& rv,
120                                    TimeAxisView& tv,
121                                    TimeAxisView& source_tv,
122                                    double initial_unit_pos)
123     : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
124 {
125
126 }
127
128 void
129 AudioGhostRegion::set_samples_per_pixel (double fpp)
130 {
131         for (vector<ArdourWaveView::WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
132                 (*i)->set_samples_per_pixel (fpp);
133         }
134 }
135
136 void
137 AudioGhostRegion::set_height ()
138 {
139         vector<ArdourWaveView::WaveView*>::iterator i;
140         uint32_t n;
141
142         GhostRegion::set_height();
143
144         double const ht = ((trackview.current_height()) / (double) waves.size());
145
146         for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
147                 (*i)->set_height (ht);
148                 (*i)->set_y_position (n * ht);
149         }
150 }
151
152 void
153 AudioGhostRegion::set_colors ()
154 {
155         GhostRegion::set_colors();
156         guint fill_color;
157
158         if (is_automation_ghost()) {
159                 fill_color = UIConfiguration::instance().color ("ghost track wave fill");
160         }
161         else {
162                 fill_color = source_track_color(200);
163         }
164
165         for (uint32_t n=0; n < waves.size(); ++n) {
166                 waves[n]->set_outline_color (UIConfiguration::instance().color ("ghost track wave"));
167                 waves[n]->set_fill_color (fill_color);
168                 waves[n]->set_clip_color (UIConfiguration::instance().color ("ghost track wave clip"));
169                 waves[n]->set_zero_color (UIConfiguration::instance().color ("ghost track zero line"));
170         }
171 }
172
173 /** The general constructor; called when the destination timeaxisview doesn't have
174  *  a midistreamview.
175  *
176  *  @param rv The parent RegionView that is being ghosted.
177  *  @param tv TimeAxisView that this ghost region is on.
178  *  @param source_tv TimeAxisView that we are the ghost for.
179  */
180 MidiGhostRegion::MidiGhostRegion(MidiRegionView& rv,
181                                  TimeAxisView& tv,
182                                  TimeAxisView& source_tv,
183                                  double initial_unit_pos)
184     : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
185     , _note_group (new ArdourCanvas::Container (group))
186     ,  parent_mrv (rv)
187     , _optimization_iterator(events.end())
188 {
189         _outline = UIConfiguration::instance().color ("ghost track midi outline");
190
191         base_rect->lower_to_bottom();
192 }
193
194 /**
195  *  @param rv The parent RegionView being ghosted.
196  *  @param msv MidiStreamView that this ghost region is on.
197  *  @param source_tv TimeAxisView that we are the ghost for.
198  */
199 MidiGhostRegion::MidiGhostRegion(MidiRegionView& rv,
200                                  MidiStreamView& msv,
201                                  TimeAxisView& source_tv,
202                                  double initial_unit_pos)
203     : GhostRegion(rv,
204                   msv.midi_underlay_group,
205                   msv.trackview(),
206                   source_tv,
207                   initial_unit_pos)
208     , _note_group (new ArdourCanvas::Container (group))
209     ,   parent_mrv (rv)
210     , _optimization_iterator(events.end())
211 {
212         _outline = UIConfiguration::instance().color ("ghost track midi outline");
213
214         base_rect->lower_to_bottom();
215 }
216
217 MidiGhostRegion::~MidiGhostRegion()
218 {
219         clear_events ();
220         delete _note_group;
221 }
222
223 MidiGhostRegion::GhostEvent::GhostEvent (NoteBase* e, ArdourCanvas::Container* g)
224         : event (e)
225 {
226
227         if (dynamic_cast<Note*>(e)) {
228                 item = new ArdourCanvas::Rectangle(
229                         g, ArdourCanvas::Rect(e->x0(), e->y0(), e->x1(), e->y1()));
230                 is_hit = false;
231         } else {
232                 Hit* hit = dynamic_cast<Hit*>(e);
233                 if (!hit) {
234                         return;
235                 }
236                 ArdourCanvas::Polygon* poly = new ArdourCanvas::Polygon(g);
237                 poly->set(Hit::points(e->y1() - e->y0()));
238                 poly->set_position(hit->position());
239                 item = poly;
240                 is_hit = true;
241         }
242
243         CANVAS_DEBUG_NAME (item, "ghost note item");
244 }
245
246 MidiGhostRegion::GhostEvent::~GhostEvent ()
247 {
248         /* event is not ours to delete */
249         delete item;
250 }
251
252 void
253 MidiGhostRegion::set_samples_per_pixel (double /*spu*/)
254 {
255 }
256
257 /** @return MidiStreamView that we are providing a ghost for */
258 MidiStreamView*
259 MidiGhostRegion::midi_view ()
260 {
261         StreamView* sv = source_trackview.view ();
262         assert (sv);
263         MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
264         assert (msv);
265
266         return msv;
267 }
268
269 void
270 MidiGhostRegion::set_height ()
271 {
272         GhostRegion::set_height();
273         update_contents_height ();
274 }
275
276 void
277 MidiGhostRegion::set_colors()
278 {
279         GhostRegion::set_colors();
280         _outline = UIConfiguration::instance().color ("ghost track midi outline");
281
282         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
283                 it->second->item->set_fill_color (UIConfiguration::instance().color_mod((*it).second->event->base_color(), "ghost track midi fill"));
284                 it->second->item->set_outline_color (_outline);
285         }
286 }
287
288 static double
289 note_height(TimeAxisView& trackview, MidiStreamView* mv)
290 {
291         const double tv_height  = trackview.current_height();
292         const double note_range = mv->contents_note_range();
293
294         return std::max(1.0, floor(tv_height / note_range - 1.0));
295 }
296
297 static double
298 note_y(TimeAxisView& trackview, MidiStreamView* mv, uint8_t note_num)
299 {
300         const double tv_height  = trackview.current_height();
301         const double note_range = mv->contents_note_range();
302         const double s          = tv_height / note_range;
303
304         return tv_height - (note_num + 1 - mv->lowest_note()) * s;
305 }
306
307 void
308 MidiGhostRegion::update_contents_height ()
309 {
310         MidiStreamView* mv = midi_view();
311
312         if (!mv) {
313                 return;
314         }
315
316         double const h = note_height(trackview, mv);
317
318         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
319                 uint8_t const note_num = it->second->event->note()->note();
320
321                 double const y = note_y(trackview, mv, note_num);
322
323                 if (!it->second->is_hit) {
324                         _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(it->second->item);
325                         _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
326                 } else {
327                         _tmp_poly = static_cast<ArdourCanvas::Polygon*>(it->second->item);
328                         ArdourCanvas::Duple position = _tmp_poly->position();
329                         position.y = y;
330                         _tmp_poly->set_position(position);
331                         _tmp_poly->set(Hit::points(h));
332                 }
333         }
334 }
335
336 void
337 MidiGhostRegion::add_note (NoteBase* n)
338 {
339         GhostEvent* event = new GhostEvent (n, _note_group);
340         events.insert (make_pair (n->note(), event));
341
342         event->item->set_fill_color (UIConfiguration::instance().color_mod(n->base_color(), "ghost track midi fill"));
343         event->item->set_outline_color (_outline);
344
345         MidiStreamView* mv = midi_view();
346
347         if (mv) {
348
349                 if (!n->item()->visible()) {
350                         event->item->hide();
351                 } else {
352                         uint8_t const note_num = n->note()->note();
353                         double const  h        = note_height(trackview, mv);
354                         double const  y        = note_y(trackview, mv, note_num);
355                         if (!event->is_hit) {
356                                 _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(event->item);
357                                 _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
358                         } else {
359                                 _tmp_poly = static_cast<ArdourCanvas::Polygon*>(event->item);
360                                 Duple position = _tmp_poly->position();
361                                 position.y = y;
362                                 _tmp_poly->set_position(position);
363                                 _tmp_poly->set(Hit::points(h));
364                         }
365                 }
366         }
367 }
368
369 void
370 MidiGhostRegion::clear_events()
371 {
372         _note_group->clear (true);
373         events.clear ();
374         _optimization_iterator = events.end();
375 }
376
377 /** Update the  positions of our representation of a note.
378  *  @param ev The GhostEvent from the parent MidiRegionView.
379  */
380 void
381 MidiGhostRegion::update_note (GhostEvent* ev)
382 {
383         MidiStreamView* mv = midi_view();
384
385         if (!mv) {
386                 return;
387         }
388
389         _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(ev->item);
390
391         uint8_t const note_num = ev->event->note()->note();
392         double const y = note_y(trackview, mv, note_num);
393         double const h = note_height(trackview, mv);
394
395         _tmp_rect->set (ArdourCanvas::Rect (ev->event->x0(), y, ev->event->x1(), y + h));
396 }
397
398 /** Update the positions of our representation of a parent's hit.
399  *  @param ev The GhostEvent from the parent MidiRegionView.
400  */
401 void
402 MidiGhostRegion::update_hit (GhostEvent* ev)
403 {
404         MidiStreamView* mv = midi_view();
405
406         if (!mv) {
407                 return;
408         }
409
410         _tmp_poly = static_cast<ArdourCanvas::Polygon*>(ev->item);
411
412         uint8_t const note_num = ev->event->note()->note();
413         double const h = note_height(trackview, mv);
414         double const y = note_y(trackview, mv, note_num);
415
416         ArdourCanvas::Duple ppos = ev->item->position();
417         ArdourCanvas::Duple gpos = _tmp_poly->position();
418         gpos.x = ppos.x;
419         gpos.y = y;
420
421         _tmp_poly->set_position(gpos);
422         _tmp_poly->set(Hit::points(h));
423 }
424
425 void
426 MidiGhostRegion::remove_note (NoteBase* note)
427 {
428         EventList::iterator f = events.find (note->note());
429         if (f == events.end()) {
430                 return;
431         }
432
433         delete f->second;
434         events.erase (f);
435
436         _optimization_iterator = events.end ();
437 }
438 void
439 MidiGhostRegion::redisplay_model ()
440 {
441         /* we rely on the parent MRV having removed notes not in the model */
442         for (EventList::iterator i = events.begin(); i != events.end(); ) {
443
444                 boost::shared_ptr<NoteType> note = i->first;
445                 GhostEvent* cne = i->second;
446                 const bool visible = (note->note() >= parent_mrv._current_range_min) &&
447                         (note->note() <= parent_mrv._current_range_max);
448
449                 if (visible) {
450                         if (cne->is_hit) {
451                                 update_hit (cne);
452                         } else {
453                                 update_note (cne);
454                         }
455                         cne->item->show ();
456                 } else {
457                         cne->item->hide ();
458                 }
459
460                 ++i;
461         }
462 }
463
464 /** Given a note in our parent region (ie the actual MidiRegionView), find our
465  *  representation of it.
466  *  @return Our Event, or 0 if not found.
467  */
468
469 MidiGhostRegion::GhostEvent *
470 MidiGhostRegion::find_event (boost::shared_ptr<NoteType> parent)
471 {
472         /* we are using _optimization_iterator to speed up the common case where a caller
473            is going through our notes in order.
474         */
475
476         if (_optimization_iterator != events.end()) {
477                 ++_optimization_iterator;
478                 if (_optimization_iterator != events.end() && _optimization_iterator->first == parent) {
479                         return _optimization_iterator->second;
480                 }
481         }
482
483         _optimization_iterator = events.find (parent);
484         if (_optimization_iterator != events.end()) {
485                 return _optimization_iterator->second;
486         }
487
488         return 0;
489 }