Missing virtual destructor.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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 <list>
21 #include <wx/graphics.h>
22 #include <boost/weak_ptr.hpp>
23 #include "lib/film.h"
24 #include "lib/playlist.h"
25 #include "film_editor.h"
26 #include "timeline.h"
27 #include "wx_util.h"
28
29 using std::list;
30 using std::cout;
31 using std::max;
32 using boost::shared_ptr;
33 using boost::weak_ptr;
34 using boost::dynamic_pointer_cast;
35 using boost::bind;
36
37 class View : public boost::noncopyable
38 {
39 public:
40         View (Timeline& t)
41                 : _timeline (t)
42         {
43
44         }
45
46         virtual ~View () {}
47                 
48         void paint (wxGraphicsContext* g)
49         {
50                 _last_paint_bbox = bbox ();
51                 do_paint (g);
52         }
53         
54         void force_redraw ()
55         {
56                 _timeline.force_redraw (_last_paint_bbox);
57                 _timeline.force_redraw (bbox ());
58         }
59
60         virtual dcpomatic::Rect<int> bbox () const = 0;
61
62 protected:
63         virtual void do_paint (wxGraphicsContext *) = 0;
64         
65         int time_x (Time t) const
66         {
67                 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
68         }
69         
70         Timeline& _timeline;
71
72 private:
73         dcpomatic::Rect<int> _last_paint_bbox;
74 };
75
76 class ContentView : public View
77 {
78 public:
79         ContentView (Timeline& tl, shared_ptr<Content> c)
80                 : View (tl)
81                 , _content (c)
82                 , _track (0)
83                 , _selected (false)
84         {
85                 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
86         }
87
88         dcpomatic::Rect<int> bbox () const
89         {
90                 shared_ptr<const Film> film = _timeline.film ();
91                 shared_ptr<const Content> content = _content.lock ();
92                 if (!film || !content) {
93                         return dcpomatic::Rect<int> ();
94                 }
95                 
96                 return dcpomatic::Rect<int> (
97                         time_x (content->position ()) - 8,
98                         y_pos (_track) - 8,
99                         content->length_after_trim () * _timeline.pixels_per_time_unit() + 16,
100                         _timeline.track_height() + 16
101                         );
102         }
103
104         void set_selected (bool s) {
105                 _selected = s;
106                 force_redraw ();
107         }
108         
109         bool selected () const {
110                 return _selected;
111         }
112
113         shared_ptr<Content> content () const {
114                 return _content.lock ();
115         }
116
117         void set_track (int t) {
118                 _track = t;
119         }
120
121         int track () const {
122                 return _track;
123         }
124
125         virtual wxString type () const = 0;
126         virtual wxColour colour () const = 0;
127         
128 private:
129
130         void do_paint (wxGraphicsContext* gc)
131         {
132                 shared_ptr<const Film> film = _timeline.film ();
133                 shared_ptr<const Content> cont = content ();
134                 if (!film || !cont) {
135                         return;
136                 }
137
138                 Time const position = cont->position ();
139                 Time const len = cont->length_after_trim ();
140
141                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
142
143                 gc->SetPen (*wxBLACK_PEN);
144                 
145                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
146                 if (_selected) {
147                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
148                 } else {
149                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
150                 }
151
152                 wxGraphicsPath path = gc->CreatePath ();
153                 path.MoveToPoint    (time_x (position),       y_pos (_track) + 4);
154                 path.AddLineToPoint (time_x (position + len), y_pos (_track) + 4);
155                 path.AddLineToPoint (time_x (position + len), y_pos (_track + 1) - 4);
156                 path.AddLineToPoint (time_x (position),       y_pos (_track + 1) - 4);
157                 path.AddLineToPoint (time_x (position),       y_pos (_track) + 4);
158                 gc->StrokePath (path);
159                 gc->FillPath (path);
160
161                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->path().filename().string()).data(), type().data());
162                 wxDouble name_width;
163                 wxDouble name_height;
164                 wxDouble name_descent;
165                 wxDouble name_leading;
166                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
167                 
168                 gc->Clip (wxRegion (time_x (position), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
169                 gc->DrawText (name, time_x (position) + 12, y_pos (_track + 1) - name_height - 4);
170                 gc->ResetClip ();
171         }
172
173         int y_pos (int t) const
174         {
175                 return _timeline.tracks_position().y + t * _timeline.track_height();
176         }
177
178         void content_changed (int p, bool frequent)
179         {
180                 ensure_ui_thread ();
181                 
182                 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
183                         force_redraw ();
184                 }
185
186                 if (!frequent) {
187                         _timeline.setup_pixels_per_time_unit ();
188                         _timeline.Refresh ();
189                 }
190         }
191
192         boost::weak_ptr<Content> _content;
193         int _track;
194         bool _selected;
195
196         boost::signals2::scoped_connection _content_connection;
197 };
198
199 class AudioContentView : public ContentView
200 {
201 public:
202         AudioContentView (Timeline& tl, shared_ptr<Content> c)
203                 : ContentView (tl, c)
204         {}
205         
206 private:
207         wxString type () const
208         {
209                 return _("audio");
210         }
211
212         wxColour colour () const
213         {
214                 return wxColour (149, 121, 232, 255);
215         }
216 };
217
218 class VideoContentView : public ContentView
219 {
220 public:
221         VideoContentView (Timeline& tl, shared_ptr<Content> c)
222                 : ContentView (tl, c)
223         {}
224
225 private:        
226
227         wxString type () const
228         {
229                 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
230                         return _("video");
231                 } else {
232                         return _("still");
233                 }
234         }
235
236         wxColour colour () const
237         {
238                 return wxColour (242, 92, 120, 255);
239         }
240 };
241
242 class TimeAxisView : public View
243 {
244 public:
245         TimeAxisView (Timeline& tl, int y)
246                 : View (tl)
247                 , _y (y)
248         {}
249         
250         dcpomatic::Rect<int> bbox () const
251         {
252                 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
253         }
254
255         void set_y (int y)
256         {
257                 _y = y;
258                 force_redraw ();
259         }
260
261 private:        
262
263         void do_paint (wxGraphicsContext* gc)
264         {
265                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
266                 
267                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
268                 if (mark_interval > 5) {
269                         mark_interval -= mark_interval % 5;
270                 }
271                 if (mark_interval > 10) {
272                         mark_interval -= mark_interval % 10;
273                 }
274                 if (mark_interval > 60) {
275                         mark_interval -= mark_interval % 60;
276                 }
277                 if (mark_interval > 3600) {
278                         mark_interval -= mark_interval % 3600;
279                 }
280                 
281                 if (mark_interval < 1) {
282                         mark_interval = 1;
283                 }
284
285                 wxGraphicsPath path = gc->CreatePath ();
286                 path.MoveToPoint (_timeline.x_offset(), _y);
287                 path.AddLineToPoint (_timeline.width(), _y);
288                 gc->StrokePath (path);
289
290                 Time t = 0;
291                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
292                         wxGraphicsPath path = gc->CreatePath ();
293                         path.MoveToPoint (time_x (t), _y - 4);
294                         path.AddLineToPoint (time_x (t), _y + 4);
295                         gc->StrokePath (path);
296
297                         int tc = t / TIME_HZ;
298                         int const h = tc / 3600;
299                         tc -= h * 3600;
300                         int const m = tc / 60;
301                         tc -= m * 60;
302                         int const s = tc;
303                         
304                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
305                         wxDouble str_width;
306                         wxDouble str_height;
307                         wxDouble str_descent;
308                         wxDouble str_leading;
309                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
310                         
311                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
312                         if ((tx + str_width) < _timeline.width()) {
313                                 gc->DrawText (str, time_x (t), _y + 16);
314                         }
315                         
316                         t += mark_interval * TIME_HZ;
317                 }
318         }
319
320 private:
321         int _y;
322 };
323
324 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
325         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
326         , _film_editor (ed)
327         , _film (film)
328         , _time_axis_view (new TimeAxisView (*this, 32))
329         , _tracks (0)
330         , _pixels_per_time_unit (0)
331         , _left_down (false)
332         , _down_view_position (0)
333         , _first_move (false)
334         , _menu (film, this)
335 {
336 #ifndef __WXOSX__
337         SetDoubleBuffered (true);
338 #endif  
339
340         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
341         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
342         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
343         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
344         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
345         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
346
347         playlist_changed ();
348
349         SetMinSize (wxSize (640, tracks() * track_height() + 96));
350
351         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
352 }
353
354 void
355 Timeline::paint ()
356 {
357         wxPaintDC dc (this);
358
359         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
360         if (!gc) {
361                 return;
362         }
363
364         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
365
366         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
367                 (*i)->paint (gc);
368         }
369
370         delete gc;
371 }
372
373 void
374 Timeline::playlist_changed ()
375 {
376         ensure_ui_thread ();
377         
378         shared_ptr<const Film> fl = _film.lock ();
379         if (!fl) {
380                 return;
381         }
382
383         _views.clear ();
384         _views.push_back (_time_axis_view);
385
386         ContentList content = fl->playlist()->content ();
387
388         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
389                 if (dynamic_pointer_cast<VideoContent> (*i)) {
390                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
391                 }
392                 if (dynamic_pointer_cast<AudioContent> (*i)) {
393                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
394                 }
395         }
396
397         assign_tracks ();
398         setup_pixels_per_time_unit ();
399         Refresh ();
400 }
401
402 void
403 Timeline::assign_tracks ()
404 {
405         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
406                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
407                 if (cv) {
408                         cv->set_track (0);
409                         _tracks = 1;
410                 }
411         }
412
413         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
414                 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
415                 if (!acv) {
416                         continue;
417                 }
418         
419                 shared_ptr<Content> acv_content = acv->content();
420
421                 int t = 1;
422                 while (1) {
423                         ViewList::iterator j = _views.begin();
424                         while (j != _views.end()) {
425                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
426                                 if (!test) {
427                                         ++j;
428                                         continue;
429                                 }
430                                 
431                                 shared_ptr<Content> test_content = test->content();
432                                         
433                                 if (test && test->track() == t) {
434                                         bool const no_overlap =
435                                                 (acv_content->position() < test_content->position() && acv_content->end() < test_content->position()) ||
436                                                 (acv_content->position() > test_content->end()      && acv_content->end() > test_content->end());
437                                         
438                                         if (!no_overlap) {
439                                                 /* we have an overlap on track `t' */
440                                                 ++t;
441                                                 break;
442                                         }
443                                 }
444                                 
445                                 ++j;
446                         }
447
448                         if (j == _views.end ()) {
449                                 /* no overlap on `t' */
450                                 break;
451                         }
452                 }
453
454                 acv->set_track (t);
455                 _tracks = max (_tracks, t + 1);
456         }
457
458         _time_axis_view->set_y (tracks() * track_height() + 32);
459 }
460
461 int
462 Timeline::tracks () const
463 {
464         return _tracks;
465 }
466
467 void
468 Timeline::setup_pixels_per_time_unit ()
469 {
470         shared_ptr<const Film> film = _film.lock ();
471         if (!film) {
472                 return;
473         }
474
475         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
476 }
477
478 shared_ptr<View>
479 Timeline::event_to_view (wxMouseEvent& ev)
480 {
481         ViewList::iterator i = _views.begin();
482         Position<int> const p (ev.GetX(), ev.GetY());
483         while (i != _views.end() && !(*i)->bbox().contains (p)) {
484                 ++i;
485         }
486
487         if (i == _views.end ()) {
488                 return shared_ptr<View> ();
489         }
490
491         return *i;
492 }
493
494 void
495 Timeline::left_down (wxMouseEvent& ev)
496 {
497         shared_ptr<View> view = event_to_view (ev);
498         shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
499
500         _down_view.reset ();
501
502         if (content_view) {
503                 _down_view = content_view;
504                 _down_view_position = content_view->content()->position ();
505         }
506
507         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
508                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
509                 if (!cv) {
510                         continue;
511                 }
512                 
513                 if (!ev.ShiftDown ()) {
514                         cv->set_selected (view == *i);
515                 }
516                 
517                 if (view == *i) {
518                         _film_editor->set_selection (cv->content ());
519                 }
520         }
521
522         if (content_view && ev.ShiftDown ()) {
523                 content_view->set_selected (!content_view->selected ());
524         }
525
526         _left_down = true;
527         _down_point = ev.GetPosition ();
528         _first_move = false;
529
530         if (_down_view) {
531                 _down_view->content()->set_change_signals_frequent (true);
532         }
533 }
534
535 void
536 Timeline::left_up (wxMouseEvent& ev)
537 {
538         _left_down = false;
539
540         if (_down_view) {
541                 _down_view->content()->set_change_signals_frequent (false);
542         }
543
544         set_position_from_event (ev);
545 }
546
547 void
548 Timeline::mouse_moved (wxMouseEvent& ev)
549 {
550         if (!_left_down) {
551                 return;
552         }
553
554         set_position_from_event (ev);
555 }
556
557 void
558 Timeline::right_down (wxMouseEvent& ev)
559 {
560         shared_ptr<View> view = event_to_view (ev);
561         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
562         if (!cv) {
563                 return;
564         }
565
566         if (!cv->selected ()) {
567                 clear_selection ();
568                 cv->set_selected (true);
569         }
570
571         _menu.popup (selected_content (), ev.GetPosition ());
572 }
573
574 void
575 Timeline::set_position_from_event (wxMouseEvent& ev)
576 {
577         wxPoint const p = ev.GetPosition();
578
579         if (!_first_move) {
580                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
581                 if (dist < 8) {
582                         return;
583                 }
584                 _first_move = true;
585         }
586
587         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
588         if (_down_view) {
589                 _down_view->content()->set_position (max (static_cast<Time> (0), _down_view_position + time_diff));
590
591                 shared_ptr<Film> film = _film.lock ();
592                 assert (film);
593                 film->set_sequence_video (false);
594         }
595 }
596
597 void
598 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
599 {
600         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
601 }
602
603 shared_ptr<const Film>
604 Timeline::film () const
605 {
606         return _film.lock ();
607 }
608
609 void
610 Timeline::resized ()
611 {
612         setup_pixels_per_time_unit ();
613 }
614
615 void
616 Timeline::clear_selection ()
617 {
618         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
619                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
620                 if (cv) {
621                         cv->set_selected (false);
622                 }
623         }
624 }
625
626 Timeline::ContentViewList
627 Timeline::selected_views () const
628 {
629         ContentViewList sel;
630         
631         for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
632                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
633                 if (cv && cv->selected()) {
634                         sel.push_back (cv);
635                 }
636         }
637
638         return sel;
639 }
640
641 ContentList
642 Timeline::selected_content () const
643 {
644         ContentList sel;
645         ContentViewList views = selected_views ();
646         
647         for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
648                 sel.push_back ((*i)->content ());
649         }
650
651         return sel;
652 }