set powermate env keys in SConstruct, so that POWERMATE is set even if SURFACES=false
[ardour.git] / gtk2_ardour / region_view.cc
1 /*
2     Copyright (C) 2001-2006 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 <cmath>
21 #include <cassert>
22 #include <algorithm>
23
24 #include <gtkmm.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27 #include <pbd/stacktrace.h>
28
29 #include <ardour/playlist.h>
30 #include <ardour/audioregion.h>
31 #include <ardour/audiosource.h>
32 #include <ardour/audio_diskstream.h>
33
34 #include "ardour_ui.h"
35 #include "streamview.h"
36 #include "region_view.h"
37 #include "route_time_axis.h"
38 #include "simplerect.h"
39 #include "simpleline.h"
40 #include "waveview.h"
41 #include "public_editor.h"
42 #include "region_editor.h"
43 #include "ghostregion.h"
44 #include "route_time_axis.h"
45 #include "utils.h"
46 #include "rgb_macros.h"
47 #include "gui_thread.h"
48
49 #include "i18n.h"
50
51 using namespace sigc;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Editing;
55 using namespace ArdourCanvas;
56
57 static const int32_t sync_mark_width = 9;
58
59 sigc::signal<void,RegionView*> RegionView::RegionViewGoingAway;
60
61 RegionView::RegionView (ArdourCanvas::Group* parent, 
62                         TimeAxisView&        tv,
63                         boost::shared_ptr<ARDOUR::Region> r,
64                         double               spu,
65                         Gdk::Color&          basic_color)
66         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(),
67                             TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
68                                                           TimeAxisViewItem::ShowNameHighlight|
69                                                           TimeAxisViewItem::ShowFrame))
70           , _region (r)
71           , sync_mark(0)
72           , editor(0)
73           , current_visible_sync_position(0.0)
74           , valid(false)
75           , _pixel_width(1.0)
76           , in_destructor(false)
77           , wait_for_data(false)
78 {
79 }
80
81 RegionView::RegionView (const RegionView& other)
82         : TimeAxisViewItem (other)
83 {
84         /* derived concrete type will call init () */
85
86         _region = other._region;
87         editor = other.editor;
88         current_visible_sync_position = other.current_visible_sync_position;
89         valid = false;
90         _pixel_width = other._pixel_width;
91 }
92
93 RegionView::RegionView (ArdourCanvas::Group*         parent, 
94                         TimeAxisView&                tv,
95                         boost::shared_ptr<ARDOUR::Region> r,
96                         double                       spu,
97                         Gdk::Color&                  basic_color,
98                         TimeAxisViewItem::Visibility visibility)
99         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), visibility)
100         , _region (r)
101         , sync_mark(0)
102         , editor(0)
103         , current_visible_sync_position(0.0)
104         , valid(false)
105         , _pixel_width(1.0)
106         , in_destructor(false)
107         , wait_for_data(false)
108 {
109 }
110
111 void
112 RegionView::init (Gdk::Color& basic_color, bool wfd)
113 {
114         valid         = true;
115         in_destructor = false;
116         wait_for_data = wfd;
117
118         compute_colors (basic_color);
119
120         name_highlight->set_data ("regionview", this);
121
122         if (name_text) {
123                 name_text->set_data ("regionview", this);
124         }
125
126         /* an equilateral triangle */
127
128         ArdourCanvas::Points shape;
129         shape.push_back (Gnome::Art::Point (-((sync_mark_width-1)/2), 1));
130         shape.push_back (Gnome::Art::Point ((sync_mark_width - 1)/2, 1));
131         shape.push_back (Gnome::Art::Point (0, sync_mark_width - 1));
132         shape.push_back (Gnome::Art::Point (-((sync_mark_width-1)/2), 1));
133
134         sync_mark =  new ArdourCanvas::Polygon (*group);
135         sync_mark->property_points() = shape;
136         sync_mark->property_fill_color_rgba() = fill_color;
137         sync_mark->hide();
138
139         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
140
141         set_y_position_and_height (0, trackview.height - 2);
142
143         _region->StateChanged.connect (mem_fun(*this, &RegionView::region_changed));
144
145         group->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
146         name_highlight->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
147
148         set_colors ();
149
150         ColorsChanged.connect (mem_fun (*this, &RegionView::color_handler));
151
152         /* XXX sync mark drag? */
153 }
154
155 RegionView::~RegionView ()
156 {
157         in_destructor = true;
158
159         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
160                 delete *g;
161         }
162
163         if (editor) {
164                 delete editor;
165         }
166 }
167
168 gint
169 RegionView::_lock_toggle (ArdourCanvas::Item* item, GdkEvent* ev, void* arg)
170 {
171         switch (ev->type) {
172         case GDK_BUTTON_RELEASE:
173                 static_cast<RegionView*>(arg)->lock_toggle ();
174                 return TRUE;
175                 break;
176         default:
177                 break;
178         } 
179         return FALSE;
180 }
181
182 void
183 RegionView::lock_toggle ()
184 {
185         _region->set_locked (!_region->locked());
186 }
187
188 void
189 RegionView::region_changed (Change what_changed)
190 {
191         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RegionView::region_changed), what_changed));
192
193         if (what_changed & BoundsChanged) {
194                 region_resized (what_changed);
195                 region_sync_changed ();
196         }
197         if (what_changed & Region::MuteChanged) {
198                 region_muted ();
199         }
200         if (what_changed & Region::OpacityChanged) {
201                 region_opacity ();
202         }
203         if (what_changed & ARDOUR::NameChanged) {
204                 region_renamed ();
205         }
206         if (what_changed & Region::SyncOffsetChanged) {
207                 region_sync_changed ();
208         }
209         if (what_changed & Region::LayerChanged) {
210                 region_layered ();
211         }
212         if (what_changed & Region::LockChanged) {
213                 region_locked ();
214         }
215 }
216
217 void
218 RegionView::region_locked ()
219 {
220         /* name will show locked status */
221         region_renamed ();
222 }
223
224 void
225 RegionView::region_resized (Change what_changed)
226 {
227         double unit_length;
228
229         if (what_changed & ARDOUR::PositionChanged) {
230                 set_position (_region->position(), 0);
231         }
232
233         if (what_changed & Change (StartChanged|LengthChanged)) {
234
235                 set_duration (_region->length(), 0);
236
237                 unit_length = _region->length() / samples_per_unit;
238                 
239                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
240
241                         (*i)->set_duration (unit_length);
242
243                 }
244         }
245 }
246
247 void
248 RegionView::reset_width_dependent_items (double pixel_width)
249 {
250         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
251         _pixel_width = pixel_width;
252 }
253
254 void
255 RegionView::region_layered ()
256 {
257         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*>(&get_time_axis_view());
258         assert(rtv);
259         rtv->view()->region_layered (this);
260 }
261         
262 void
263 RegionView::region_muted ()
264 {
265         set_frame_color ();
266         region_renamed ();
267 }
268
269 void
270 RegionView::region_opacity ()
271 {
272         set_frame_color ();
273 }
274
275 void
276 RegionView::raise_to_top ()
277 {
278         _region->raise_to_top ();
279 }
280
281 void
282 RegionView::lower_to_bottom ()
283 {
284         _region->lower_to_bottom ();
285 }
286
287 bool
288 RegionView::set_position (nframes_t pos, void* src, double* ignored)
289 {
290         double delta;
291         bool ret;
292
293         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
294                 return false;
295         }
296
297         if (ignored) {
298                 *ignored = delta;
299         }
300
301         if (delta) {
302                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
303                         (*i)->group->move (delta, 0.0);
304                 }
305         }
306
307         return ret;
308 }
309
310 void
311 RegionView::set_samples_per_unit (gdouble spu)
312 {
313         TimeAxisViewItem::set_samples_per_unit (spu);
314
315         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
316                 (*i)->set_samples_per_unit (spu);
317                 (*i)->set_duration (_region->length() / samples_per_unit);
318         }
319
320         region_sync_changed ();
321 }
322
323 bool
324 RegionView::set_duration (nframes_t frames, void *src)
325 {
326         if (!TimeAxisViewItem::set_duration (frames, src)) {
327                 return false;
328         }
329         
330         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
331                 (*i)->set_duration (_region->length() / samples_per_unit);
332         }
333
334         return true;
335 }
336
337 void
338 RegionView::compute_colors (Gdk::Color& basic_color)
339 {
340         TimeAxisViewItem::compute_colors (basic_color);
341 }
342
343 void
344 RegionView::set_colors ()
345 {
346         TimeAxisViewItem::set_colors ();
347         
348         if (sync_mark) {
349                 sync_mark->property_fill_color_rgba() = fill_color;
350         }
351 }
352
353 void
354 RegionView::set_frame_color ()
355 {
356         if (_region->opaque()) {
357                 fill_opacity = 130;
358         } else {
359                 fill_opacity = 60;
360         }
361
362         TimeAxisViewItem::set_frame_color ();
363 }
364
365 void
366 RegionView::fake_set_opaque (bool yn)
367 {
368        if (yn) {
369                fill_opacity = 130;
370        } else {
371                fill_opacity = 60;
372        }
373
374        TimeAxisViewItem::set_frame_color ();
375 }
376
377 void
378 RegionView::hide_region_editor()
379 {
380         if (editor) {
381                 editor->hide_all ();
382         }
383 }
384
385 Glib::ustring
386 RegionView::make_name () const
387 {
388         Glib::ustring str;
389
390         // XXX nice to have some good icons for this
391
392         if (_region->locked()) {
393                 str += '>';
394                 str += _region->name();
395                 str += '<';
396         } else if (_region->position_locked()) {
397                 str += '{';
398                 str += _region->name();
399                 str += '}';
400         } else {
401                 str = _region->name();
402         }
403
404         if (_region->muted()) {
405                 str = string ("!") + str;
406         }
407
408         return str;
409 }
410
411 void
412 RegionView::region_renamed ()
413 {
414         Glib::ustring str = make_name ();
415
416         set_item_name (str, this);
417         set_name_text (str);
418         reset_width_dependent_items (_pixel_width);
419 }
420
421 void
422 RegionView::region_sync_changed ()
423 {
424         if (sync_mark == 0) {
425                 return;
426         }
427
428         int sync_dir;
429         nframes_t sync_offset;
430
431         sync_offset = _region->sync_offset (sync_dir);
432
433         /* this has to handle both a genuine change of position, a change of samples_per_unit,
434            and a change in the bounds of the _region->
435          */
436
437         if (sync_offset == 0) {
438
439                 /* no sync mark - its the start of the region */
440
441                 sync_mark->hide();
442
443         } else {
444
445                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) { 
446
447                         /* no sync mark - its out of the bounds of the region */
448
449                         sync_mark->hide();
450
451                 } else {
452
453                         /* lets do it */
454
455                         Points points;
456                         
457                         //points = sync_mark->property_points().get_value();
458                         
459                         double offset = sync_offset / samples_per_unit;
460                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
461                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
462                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
463                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));     
464                         sync_mark->property_points().set_value (points);
465                         sync_mark->show();
466
467                 }
468         }
469 }
470
471 void
472 RegionView::move (double x_delta, double y_delta)
473 {
474         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
475                 return;
476         }
477
478         get_canvas_group()->move (x_delta, y_delta);
479
480         /* note: ghosts never leave their tracks so y_delta for them is always zero */
481
482         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
483                 (*i)->group->move (x_delta, 0.0);
484         }
485 }
486
487 void
488 RegionView::remove_ghost (GhostRegion* ghost)
489 {
490         if (in_destructor) {
491                 return;
492         }
493
494         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
495                 if (*i == ghost) {
496                         ghosts.erase (i);
497                         break;
498                 }
499         }
500 }
501
502 uint32_t
503 RegionView::get_fill_color ()
504 {
505         return fill_color;
506 }
507