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