std::shared_ptr
[dcpomatic.git] / src / wx / timeline_dialog.cc
1 /*
2     Copyright (C) 2013-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "film_editor.h"
22 #include "timeline_dialog.h"
23 #include "wx_util.h"
24 #include "content_panel.h"
25 #include "lib/playlist.h"
26 #include "lib/cross.h"
27 #include "lib/compose.hpp"
28 #include <wx/graphics.h>
29 #include <iostream>
30 #include <list>
31
32 using std::list;
33 using std::cout;
34 using std::string;
35 using std::shared_ptr;
36 using std::weak_ptr;
37 #if BOOST_VERSION >= 106100
38 using namespace boost::placeholders;
39 #endif
40
41 TimelineDialog::TimelineDialog (ContentPanel* cp, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer)
42         : wxDialog (
43                 cp->window(),
44                 wxID_ANY,
45                 _("Timeline"),
46                 wxDefaultPosition,
47                 wxSize (640, 512),
48 #ifdef DCPOMATIC_OSX
49                 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
50                    the window above all others (and not just our own) it's better than nothing for now.
51                 */
52                 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
53 #else
54                 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
55 #endif
56                 )
57         , _film (film)
58         , _timeline (this, cp, film, viewer)
59 {
60         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
61
62         wxBitmap select (bitmap_path("select"), wxBITMAP_TYPE_PNG);
63         wxBitmap zoom (bitmap_path("zoom"), wxBITMAP_TYPE_PNG);
64         wxBitmap zoom_all (bitmap_path("zoom_all"), wxBITMAP_TYPE_PNG);
65         wxBitmap snap (bitmap_path("snap"), wxBITMAP_TYPE_PNG);
66         wxBitmap sequence (bitmap_path("sequence"), wxBITMAP_TYPE_PNG);
67
68         _toolbar = new wxToolBar (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL);
69         _toolbar->SetMargins (4, 4);
70         _toolbar->AddRadioTool ((int) Timeline::SELECT, _("Select"), select, wxNullBitmap, _("Select and move content"));
71         _toolbar->AddRadioTool ((int) Timeline::ZOOM, _("Zoom"), zoom, wxNullBitmap, _("Zoom in / out"));
72         _toolbar->AddTool ((int) Timeline::ZOOM_ALL, _("Zoom all"), zoom_all, _("Zoom out to whole film"));
73         _toolbar->AddCheckTool ((int) Timeline::SNAP, _("Snap"), snap, wxNullBitmap, _("Snap"));
74         _toolbar->AddCheckTool ((int) Timeline::SEQUENCE, _("Sequence"), sequence, wxNullBitmap, _("Keep video and subtitles in sequence"));
75         _toolbar->Realize ();
76
77         _toolbar->Bind (wxEVT_TOOL, bind (&TimelineDialog::tool_clicked, this, _1));
78
79         sizer->Add (_toolbar, 0, wxALL, 12);
80         sizer->Add (&_timeline, 1, wxEXPAND | wxALL, 12);
81
82 #ifdef DCPOMATIC_LINUX
83         wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
84         if (buttons) {
85                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
86         }
87 #endif
88
89         SetSizer (sizer);
90         sizer->Layout ();
91         sizer->SetSizeHints (this);
92
93         _toolbar->ToggleTool ((int) Timeline::SNAP, _timeline.snap ());
94         film_change (CHANGE_TYPE_DONE, Film::SEQUENCE);
95
96         _film_changed_connection = film->Change.connect (bind (&TimelineDialog::film_change, this, _1, _2));
97 }
98
99 void
100 TimelineDialog::film_change (ChangeType type, Film::Property p)
101 {
102         if (type != CHANGE_TYPE_DONE) {
103                 return;
104         }
105
106         shared_ptr<Film> film = _film.lock ();
107         if (!film) {
108                 return;
109         }
110
111         if (p == Film::SEQUENCE) {
112                 _toolbar->ToggleTool ((int) Timeline::SEQUENCE, film->sequence ());
113         }
114 }
115
116 void
117 TimelineDialog::set_selection (ContentList selection)
118 {
119         _timeline.set_selection (selection);
120 }
121
122 void
123 TimelineDialog::tool_clicked (wxCommandEvent& ev)
124 {
125         Timeline::Tool t = (Timeline::Tool) ev.GetId();
126         _timeline.tool_clicked (t);
127         if (t == Timeline::SNAP) {
128                 _timeline.set_snap (_toolbar->GetToolState ((int) t));
129         } else if (t == Timeline::SEQUENCE) {
130                 shared_ptr<Film> film = _film.lock ();
131                 if (film) {
132                         film->set_sequence (_toolbar->GetToolState ((int) t));
133                 }
134         }
135 }