Some missing ui_signaller use.
[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, _3));
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, bool frequent)
178         {
179                 ensure_ui_thread ();
180                 
181                 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
182                         force_redraw ();
183                 }
184
185                 if (!frequent) {
186                         _timeline.setup_pixels_per_time_unit ();
187                         _timeline.Refresh ();
188                 }
189         }
190
191         boost::weak_ptr<Content> _content;
192         int _track;
193         bool _selected;
194
195         boost::signals2::scoped_connection _content_connection;
196 };
197
198 class AudioContentView : public ContentView
199 {
200 public:
201         AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
202                 : ContentView (tl, c, t)
203         {}
204         
205 private:
206         wxString type () const
207         {
208                 return _("audio");
209         }
210
211         wxColour colour () const
212         {
213                 return wxColour (149, 121, 232, 255);
214         }
215 };
216
217 class VideoContentView : public ContentView
218 {
219 public:
220         VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
221                 : ContentView (tl, c, t)
222         {}
223
224 private:        
225
226         wxString type () const
227         {
228                 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
229                         return _("video");
230                 } else {
231                         return _("still");
232                 }
233         }
234
235         wxColour colour () const
236         {
237                 return wxColour (242, 92, 120, 255);
238         }
239 };
240
241 class TimeAxisView : public View
242 {
243 public:
244         TimeAxisView (Timeline& tl, int y)
245                 : View (tl)
246                 , _y (y)
247         {}
248         
249         dcpomatic::Rect<int> bbox () const
250         {
251                 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
252         }
253
254         void set_y (int y)
255         {
256                 _y = y;
257                 force_redraw ();
258         }
259
260 private:        
261
262         void do_paint (wxGraphicsContext* gc)
263         {
264                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
265                 
266                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
267                 if (mark_interval > 5) {
268                         mark_interval -= mark_interval % 5;
269                 }
270                 if (mark_interval > 10) {
271                         mark_interval -= mark_interval % 10;
272                 }
273                 if (mark_interval > 60) {
274                         mark_interval -= mark_interval % 60;
275                 }
276                 if (mark_interval > 3600) {
277                         mark_interval -= mark_interval % 3600;
278                 }
279                 
280                 if (mark_interval < 1) {
281                         mark_interval = 1;
282                 }
283
284                 wxGraphicsPath path = gc->CreatePath ();
285                 path.MoveToPoint (_timeline.x_offset(), _y);
286                 path.AddLineToPoint (_timeline.width(), _y);
287                 gc->StrokePath (path);
288
289                 Time t = 0;
290                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
291                         wxGraphicsPath path = gc->CreatePath ();
292                         path.MoveToPoint (time_x (t), _y - 4);
293                         path.AddLineToPoint (time_x (t), _y + 4);
294                         gc->StrokePath (path);
295
296                         int tc = t / TIME_HZ;
297                         int const h = tc / 3600;
298                         tc -= h * 3600;
299                         int const m = tc / 60;
300                         tc -= m * 60;
301                         int const s = tc;
302                         
303                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
304                         wxDouble str_width;
305                         wxDouble str_height;
306                         wxDouble str_descent;
307                         wxDouble str_leading;
308                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
309                         
310                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
311                         if ((tx + str_width) < _timeline.width()) {
312                                 gc->DrawText (str, time_x (t), _y + 16);
313                         }
314                         
315                         t += mark_interval * TIME_HZ;
316                 }
317         }
318
319 private:
320         int _y;
321 };
322
323 enum {
324         ID_repeat,
325         ID_remove
326 };
327
328 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
329         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
330         , _film_editor (ed)
331         , _film (film)
332         , _time_axis_view (new TimeAxisView (*this, 32))
333         , _tracks (0)
334         , _pixels_per_time_unit (0)
335         , _left_down (false)
336         , _down_view_start (0)
337         , _first_move (false)
338         , _menu (0)
339 {
340 #ifndef __WXOSX__
341         SetDoubleBuffered (true);
342 #endif  
343
344         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
345         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
346         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
347         Connect (wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler (Timeline::right_down), 0, this);
348         Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
349         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
350
351         Connect (ID_repeat, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Timeline::repeat), 0, this);
352         Connect (ID_remove, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Timeline::remove), 0, this);
353
354         playlist_changed ();
355
356         SetMinSize (wxSize (640, tracks() * track_height() + 96));
357
358         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
359 }
360
361 void
362 Timeline::paint (wxPaintEvent &)
363 {
364         wxPaintDC dc (this);
365
366         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
367         if (!gc) {
368                 return;
369         }
370
371         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
372
373         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
374                 (*i)->paint (gc);
375         }
376
377         delete gc;
378 }
379
380 void
381 Timeline::playlist_changed ()
382 {
383         ensure_ui_thread ();
384         
385         shared_ptr<const Film> fl = _film.lock ();
386         if (!fl) {
387                 return;
388         }
389
390         _views.clear ();
391         _views.push_back (_time_axis_view);
392
393         ContentList content = fl->playlist()->content ();
394
395         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
396                 if (dynamic_pointer_cast<VideoContent> (*i)) {
397                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
398                 }
399                 if (dynamic_pointer_cast<AudioContent> (*i)) {
400                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
401                 }
402         }
403
404         assign_tracks ();
405         setup_pixels_per_time_unit ();
406         Refresh ();
407 }
408
409 void
410 Timeline::assign_tracks ()
411 {
412         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
413                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
414                 if (cv) {
415                         cv->set_track (0);
416                         _tracks = 1;
417                 }
418         }
419
420         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
421                 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
422                 if (!acv) {
423                         continue;
424                 }
425         
426                 shared_ptr<Content> acv_content = acv->content();
427                 
428                 int t = 1;
429                 while (1) {
430                         ViewList::iterator j = _views.begin();
431                         while (j != _views.end()) {
432                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
433                                 if (!test) {
434                                         ++j;
435                                         continue;
436                                 }
437                                 
438                                 shared_ptr<Content> test_content = test->content();
439                                         
440                                 if (test && test->track() == t) {
441                                         if ((acv_content->start() < test_content->start() && test_content->start() < acv_content->end()) ||
442                                             (acv_content->start() < test_content->end()   && test_content->end()   < acv_content->end())) {
443                                                 /* we have an overlap on track `t' */
444                                                 ++t;
445                                                 break;
446                                         }
447                                 }
448                                 
449                                 ++j;
450                         }
451
452                         if (j == _views.end ()) {
453                                 /* no overlap on `t' */
454                                 break;
455                         }
456                 }
457
458                 acv->set_track (t);
459                 _tracks = max (_tracks, t + 1);
460         }
461
462         _time_axis_view->set_y (tracks() * track_height() + 32);
463 }
464
465 int
466 Timeline::tracks () const
467 {
468         return _tracks;
469 }
470
471 void
472 Timeline::setup_pixels_per_time_unit ()
473 {
474         shared_ptr<const Film> film = _film.lock ();
475         if (!film) {
476                 return;
477         }
478
479         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
480 }
481
482 shared_ptr<View>
483 Timeline::event_to_view (wxMouseEvent& ev)
484 {
485         ViewList::iterator i = _views.begin();
486         Position<int> const p (ev.GetX(), ev.GetY());
487         while (i != _views.end() && !(*i)->bbox().contains (p)) {
488                 ++i;
489         }
490
491         if (i == _views.end ()) {
492                 return shared_ptr<View> ();
493         }
494
495         return *i;
496 }
497
498 void
499 Timeline::left_down (wxMouseEvent& ev)
500 {
501         shared_ptr<View> view = event_to_view (ev);
502         shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
503
504         _down_view.reset ();
505
506         if (content_view) {
507                 _down_view = content_view;
508                 _down_view_start = content_view->content()->start ();
509         }
510
511         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
512                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
513                 if (!cv) {
514                         continue;
515                 }
516                 
517                 if (!ev.ShiftDown ()) {
518                         cv->set_selected (view == *i);
519                 }
520                 
521                 if (view == *i) {
522                         _film_editor->set_selection (cv->content ());
523                 }
524         }
525
526         if (content_view && ev.ShiftDown ()) {
527                 content_view->set_selected (!content_view->selected ());
528         }
529
530         _left_down = true;
531         _down_point = ev.GetPosition ();
532         _first_move = false;
533
534         if (_down_view) {
535                 _down_view->content()->set_change_signals_frequent (true);
536         }
537 }
538
539 void
540 Timeline::left_up (wxMouseEvent& ev)
541 {
542         _left_down = false;
543
544         if (_down_view) {
545                 _down_view->content()->set_change_signals_frequent (false);
546         }
547
548         set_start_from_event (ev);
549 }
550
551 void
552 Timeline::mouse_moved (wxMouseEvent& ev)
553 {
554         if (!_left_down) {
555                 return;
556         }
557
558         set_start_from_event (ev);
559 }
560
561 void
562 Timeline::right_down (wxMouseEvent& ev)
563 {
564         shared_ptr<View> view = event_to_view (ev);
565         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
566         if (!cv) {
567                 return;
568         }
569
570         if (!cv->selected ()) {
571                 clear_selection ();
572                 cv->set_selected (true);
573         }
574
575         if (!_menu) {
576                 _menu = new wxMenu;
577                 _menu->Append (ID_repeat, _("Repeat..."));
578                 _menu->AppendSeparator ();
579                 _menu->Append (ID_remove, _("Remove"));
580         }
581
582         PopupMenu (_menu, ev.GetPosition ());
583 }
584
585 void
586 Timeline::set_start_from_event (wxMouseEvent& ev)
587 {
588         wxPoint const p = ev.GetPosition();
589
590         if (!_first_move) {
591                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
592                 if (dist < 8) {
593                         return;
594                 }
595                 _first_move = true;
596         }
597
598         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
599         if (_down_view) {
600                 _down_view->content()->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
601
602                 shared_ptr<Film> film = _film.lock ();
603                 assert (film);
604                 film->set_sequence_video (false);
605         }
606 }
607
608 void
609 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
610 {
611         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
612 }
613
614 shared_ptr<const Film>
615 Timeline::film () const
616 {
617         return _film.lock ();
618 }
619
620 void
621 Timeline::resized (wxSizeEvent &)
622 {
623         setup_pixels_per_time_unit ();
624 }
625
626 void
627 Timeline::clear_selection ()
628 {
629         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
630                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
631                 if (cv) {
632                         cv->set_selected (false);
633                 }
634         }
635 }
636
637 void
638 Timeline::repeat (wxCommandEvent &)
639 {
640         ContentList sel = selected_content ();
641         if (sel.empty ()) {
642                 return;
643         }
644                 
645         RepeatDialog d (this);
646         d.ShowModal ();
647
648         shared_ptr<const Film> film = _film.lock ();
649         if (!film) {
650                 return;
651         }
652
653         film->playlist()->repeat (sel, d.number ());
654         d.Destroy ();
655 }
656
657 void
658 Timeline::remove (wxCommandEvent &)
659 {
660         ContentList sel = selected_content ();
661         if (sel.empty ()) {
662                 return;
663         }
664
665         shared_ptr<const Film> film = _film.lock ();
666         if (!film) {
667                 return;
668         }
669
670         film->playlist()->remove (sel);
671 }
672
673 Timeline::ContentViewList
674 Timeline::selected_views () const
675 {
676         ContentViewList sel;
677         
678         for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
679                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
680                 if (cv && cv->selected()) {
681                         sel.push_back (cv);
682                 }
683         }
684
685         return sel;
686 }
687
688 ContentList
689 Timeline::selected_content () const
690 {
691         ContentList sel;
692         ContentViewList views = selected_views ();
693         
694         for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
695                 sel.push_back ((*i)->content ());
696         }
697
698         return sel;
699 }