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