Basics of making loop do something.
[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         void paint (wxGraphicsContext* g)
47         {
48                 cout << "base paint " << this << "\n";
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                 if (!film) {
91                         return dcpomatic::Rect<int> ();
92                 }
93                 
94                 return dcpomatic::Rect<int> (
95                         time_x (_content->start ()) - 8,
96                         y_pos (_track) - 8,
97                         _content->length () * _timeline.pixels_per_time_unit() + 16,
98                         _timeline.track_height() + 16
99                         );
100         }
101
102         void set_selected (bool s) {
103                 _selected = s;
104                 force_redraw ();
105         }
106         
107         bool selected () const {
108                 return _selected;
109         }
110
111         shared_ptr<Content> content () const {
112                 return _content;
113         }
114
115         void set_track (int t) {
116                 _track = t;
117         }
118
119         int track () const {
120                 return _track;
121         }
122
123         virtual wxString type () const = 0;
124         virtual wxColour colour () const = 0;
125         
126 private:
127
128         void do_paint (wxGraphicsContext* gc)
129         {
130                 shared_ptr<const Film> film = _timeline.film ();
131                 if (!film) {
132                         return;
133                 }
134
135                 Time const start = _content->start ();
136                 Time const len = _content->length ();
137
138                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
139
140                 gc->SetPen (*wxBLACK_PEN);
141                 
142                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
143                 if (_selected) {
144                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
145                 } else {
146                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
147                 }
148
149                 cout << "paint " << static_cast<View*> (this) << " " << _content.get() << " @ " << time_x (start) << "\n";
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 (_content->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         /* This must be a shared_ptr, not a weak_ptr, as in the looped case this
185            will be the only remaining pointer to the looped content that we get
186            from the playlist.
187         */
188         boost::shared_ptr<Content> _content;
189         int _track;
190         bool _selected;
191
192         boost::signals2::scoped_connection _content_connection;
193 };
194
195 class AudioContentView : public ContentView
196 {
197 public:
198         AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
199                 : ContentView (tl, c, t)
200         {}
201         
202 private:
203         wxString type () const
204         {
205                 return _("audio");
206         }
207
208         wxColour colour () const
209         {
210                 return wxColour (149, 121, 232, 255);
211         }
212 };
213
214 class VideoContentView : public ContentView
215 {
216 public:
217         VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
218                 : ContentView (tl, c, t)
219         {}
220
221 private:        
222
223         wxString type () const
224         {
225                 return _("video");
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 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
317         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
318         , _film_editor (ed)
319         , _film (film)
320         , _time_axis_view (new TimeAxisView (*this, 32))
321         , _tracks (0)
322         , _pixels_per_time_unit (0)
323         , _left_down (false)
324         , _down_view_start (0)
325         , _first_move (false)
326 {
327 #ifndef __WXOSX__
328         SetDoubleBuffered (true);
329 #endif  
330
331         setup_pixels_per_time_unit ();
332         
333         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
334         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
335         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
336         Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
337         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
338
339         playlist_changed ();
340
341         SetMinSize (wxSize (640, tracks() * track_height() + 96));
342
343         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
344
345         _views.push_back (_time_axis_view);
346 }
347
348 void
349 Timeline::paint (wxPaintEvent &)
350 {
351         wxPaintDC dc (this);
352
353         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
354         if (!gc) {
355                 return;
356         }
357
358         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
359
360         cout << "painting " << _views.size() << "\n";
361         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
362                 (*i)->paint (gc);
363         }
364
365         delete gc;
366 }
367
368 void
369 Timeline::playlist_changed ()
370 {
371         shared_ptr<const Film> fl = _film.lock ();
372         if (!fl) {
373                 return;
374         }
375
376         cout << "clearing views.\n";
377         _views.clear ();
378
379         Playlist::ContentList content = fl->playlist()->content_with_loop ();
380
381         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
382                 if (dynamic_pointer_cast<VideoContent> (*i)) {
383                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
384                 }
385                 if (dynamic_pointer_cast<AudioContent> (*i)) {
386                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
387                 }
388         }
389
390         assign_tracks ();
391         Refresh ();
392 }
393
394 void
395 Timeline::assign_tracks ()
396 {
397         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
398                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
399                 if (cv) {
400                         cv->set_track (0);
401                         _tracks = 1;
402                 }
403         }
404
405         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
406                 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
407                 if (!acv) {
408                         continue;
409                 }
410         
411                 shared_ptr<Content> acv_content = acv->content();
412                 
413                 int t = 1;
414                 while (1) {
415                         list<shared_ptr<View> >::iterator j = _views.begin();
416                         while (j != _views.end()) {
417                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
418                                 if (!test) {
419                                         ++j;
420                                         continue;
421                                 }
422                                 
423                                 shared_ptr<Content> test_content = test->content();
424                                         
425                                 if (test && test->track() == t) {
426                                         if ((acv_content->start() < test_content->start() && test_content->start() < acv_content->end()) ||
427                                             (acv_content->start() < test_content->end()   && test_content->end()   < acv_content->end())) {
428                                                 /* we have an overlap on track `t' */
429                                                 ++t;
430                                                 break;
431                                         }
432                                 }
433                                 
434                                 ++j;
435                         }
436
437                         if (j == _views.end ()) {
438                                 /* no overlap on `t' */
439                                 break;
440                         }
441                 }
442
443                 acv->set_track (t);
444                 _tracks = max (_tracks, t + 1);
445         }
446
447         _time_axis_view->set_y (tracks() * track_height() + 32);
448 }
449
450 int
451 Timeline::tracks () const
452 {
453         return _tracks;
454 }
455
456 void
457 Timeline::setup_pixels_per_time_unit ()
458 {
459         shared_ptr<const Film> film = _film.lock ();
460         if (!film) {
461                 return;
462         }
463
464         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length_with_loop();
465 }
466
467 void
468 Timeline::left_down (wxMouseEvent& ev)
469 {
470         list<shared_ptr<View> >::iterator i = _views.begin();
471         Position<int> const p (ev.GetX(), ev.GetY());
472         while (i != _views.end() && !(*i)->bbox().contains (p)) {
473                 ++i;
474         }
475
476         _down_view.reset ();
477
478         if (i != _views.end ()) {
479                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
480                 if (cv) {
481                         _down_view = cv;
482                         _down_view_start = cv->content()->start ();
483                 }
484         }
485
486         for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
487                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
488                 if (cv) {
489                         cv->set_selected (i == j);
490                         if (i == j) {
491                                 _film_editor->set_selection (cv->content ());
492                         }
493                 }
494         }
495
496         _left_down = true;
497         _down_point = ev.GetPosition ();
498         _first_move = false;
499
500         if (_down_view) {
501                 _down_view->content()->set_change_signals_frequent (true);
502         }
503 }
504
505 void
506 Timeline::left_up (wxMouseEvent& ev)
507 {
508         _left_down = false;
509
510         if (_down_view) {
511                 _down_view->content()->set_change_signals_frequent (false);
512         }
513
514         set_start_from_event (ev);
515 }
516
517 void
518 Timeline::mouse_moved (wxMouseEvent& ev)
519 {
520         if (!_left_down) {
521                 return;
522         }
523
524         set_start_from_event (ev);
525 }
526
527 void
528 Timeline::set_start_from_event (wxMouseEvent& ev)
529 {
530         wxPoint const p = ev.GetPosition();
531
532         if (!_first_move) {
533                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
534                 if (dist < 8) {
535                         return;
536                 }
537                 _first_move = true;
538         }
539
540         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
541         if (_down_view) {
542                 _down_view->content()->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
543
544                 shared_ptr<Film> film = _film.lock ();
545                 assert (film);
546                 film->set_sequence_video (false);
547         }
548 }
549
550 void
551 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
552 {
553         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
554 }
555
556 shared_ptr<const Film>
557 Timeline::film () const
558 {
559         return _film.lock ();
560 }
561
562 void
563 Timeline::resized (wxSizeEvent &)
564 {
565         setup_pixels_per_time_unit ();
566 }