fix audiospect ratio in scale per andrew, upgrade dav1d to 0.6.0, fix shapewipe black...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / zwindowgui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program 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  * This program 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcwindow.h"
23 #include "canvas.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "keys.h"
27 #include "language.h"
28 #include "localsession.h"
29 #include "mainmenu.h"
30 #include "mainsession.h"
31 #include "mbuttons.h"
32 #include "mwindow.h"
33 #include "mwindowgui.h"
34 #include "playbackengine.h"
35 #include "theme.h"
36 #include "tracks.h"
37 #include "zwindow.h"
38 #include "zwindowgui.h"
39
40 ZWindowGUI::ZWindowGUI(MWindow *mwindow, ZWindow *zwindow, Mixer *mixer)
41  : BC_Window(zwindow->title, mixer->x, mixer->y, mixer->w, mixer->h,
42         xS(100), yS(75), 1, 1, 0)
43 {
44         this->mwindow = mwindow;
45         this->zwindow = zwindow;
46
47         canvas = 0;
48         playback_engine = 0;
49         highlighted = 0;
50 }
51
52 ZWindowGUI::~ZWindowGUI()
53 {
54         delete playback_engine;
55         delete canvas;
56 }
57
58 void ZWindowGUI::create_objects()
59 {
60         lock_window("ZWindowGUI::create_objects");
61
62         canvas = new ZWindowCanvas(mwindow, this,
63                         xS(10),yS(10), get_w()-xS(20),get_h()-yS(20));
64         canvas->create_objects(mwindow->edl);
65         playback_engine = new PlaybackEngine(mwindow, canvas);
66         playback_engine->create_objects();
67
68         deactivate();
69         show_window();
70         unlock_window();
71 }
72
73 int ZWindowGUI::resize_event(int w, int h)
74 {
75         canvas->reposition_window(0,
76                         xS(10),yS(10), w-xS(20),h-yS(20));
77         zwindow->reposition(get_x(), get_y(), w, h);
78         BC_WindowBase::resize_event(w, h);
79         return 1;
80 }
81 int ZWindowGUI::translation_event()
82 {
83         return resize_event(get_w(),get_h());
84 }
85
86 int ZWindowGUI::close_event()
87 {
88         set_done(1);
89         return 1;
90 }
91
92 int ZWindowGUI::keypress_event()
93 {
94         int key = get_keypress();
95         if( key == 'w' || key == 'W' ) {
96                 close_event();
97                 return 1;
98         }
99         int result = 1;
100         switch( key ) {
101         case 'f': {
102                 int on = canvas->get_fullscreen() ? 0 : 1;
103                 canvas->set_fullscreen(on, 1);
104                 break; }
105         case ESC:
106                 canvas->set_fullscreen(0, 1);
107                 break;
108         default:
109                 unlock_window();
110                 mwindow->gui->lock_window("ZWindowGUI::keypress_event");
111                 result = mwindow->gui->mbuttons->transport->do_keypress(key);
112                 mwindow->gui->unlock_window();
113                 lock_window("ZWindowGUI::keypress_event 1");
114         }
115
116         return result;
117 }
118
119 int ZWindowGUI::button_press_event()
120 {
121         mwindow->select_zwindow(zwindow);
122         if( get_double_click() ) {
123                 unlock_window();
124                 mwindow->gui->lock_window("ZWindowGUI::button_press_event 0");
125                 LocalSession *local_session = mwindow->edl->local_session;
126                 double start = local_session->get_selectionstart(1);
127                 double end = local_session->get_selectionend(1);
128                 if( EQUIV(start, end) ) {
129                         start = mwindow->edl->tracks->total_recordable_length();
130                         if( start < 0 ) start = end;
131                 }
132                 if( (end-start) > 1e-4 ) {
133                         LocalSession *zlocal_session = zwindow->edl->local_session;
134                         zlocal_session->set_selectionstart(end);
135                         zlocal_session->set_selectionend(end);
136                         zlocal_session->set_inpoint(start);
137                         zlocal_session->set_outpoint(end);
138                         double orig_inpoint = local_session->get_inpoint();
139                         double orig_outpoint = local_session->get_outpoint();
140                         local_session->set_selectionstart(end);
141                         local_session->set_selectionend(end);
142                         local_session->set_inpoint(start);
143                         local_session->set_outpoint(end);
144                         mwindow->overwrite(zwindow->edl, 0);
145                         local_session->set_inpoint(orig_inpoint);
146                         local_session->set_outpoint(orig_outpoint);
147                         mwindow->gui->update_timebar(1);
148                 }
149                 mwindow->gui->unlock_window();
150                 lock_window("ZWindowGUI::button_press_event 1");
151         }
152         if( !canvas->get_canvas() ) return 0;
153         return canvas->button_press_event_base(canvas->get_canvas());
154 }
155
156 int ZWindowGUI::cursor_leave_event()
157 {
158         if( !canvas->get_canvas() ) return 0;
159         return canvas->cursor_leave_event_base(canvas->get_canvas());
160 }
161
162 int ZWindowGUI::cursor_enter_event()
163 {
164         if( !canvas->get_canvas() ) return 0;
165         return canvas->cursor_enter_event_base(canvas->get_canvas());
166 }
167
168 int ZWindowGUI::button_release_event()
169 {
170         if( !canvas->get_canvas() ) return 0;
171         return canvas->button_release_event();
172 }
173
174 int ZWindowGUI::cursor_motion_event()
175 {
176         BC_WindowBase *cvs = canvas->get_canvas();
177         if( !cvs ) return 0;
178         cvs->unhide_cursor();
179         return canvas->cursor_motion_event();
180 }
181
182 int ZWindowGUI::draw_overlays()
183 {
184         BC_WindowBase *cvs = canvas->get_canvas();
185         if( !cvs || cvs->get_video_on() ) return 0;
186         if( highlighted != zwindow->highlighted ) {
187                 highlighted = zwindow->highlighted;
188                 cvs->set_color(WHITE);
189                 cvs->set_inverse();
190                 cvs->draw_rectangle(0, 0, cvs->get_w(), cvs->get_h());
191                 cvs->draw_rectangle(1, 1, cvs->get_w() - 2, cvs->get_h() - 2);
192                 cvs->set_opaque();
193         }
194         return 1;
195 }
196
197
198 ZWindowCanvasTileMixers::ZWindowCanvasTileMixers(ZWindowCanvas *canvas)
199  : BC_MenuItem(_("Tile Mixers"))
200 {
201         this->canvas = canvas;
202 }
203 int ZWindowCanvasTileMixers::handle_event()
204 {
205         canvas->mwindow->tile_mixers();
206         return 1;
207 }
208
209 ZWindowCanvas::ZWindowCanvas(MWindow *mwindow, ZWindowGUI *gui,
210                 int x, int y, int w, int h)
211  : Canvas(mwindow, gui, x,y, w,h, 0,0,0)
212 {
213         this->mwindow = mwindow;
214         this->gui = gui;
215 }
216
217 void ZWindowCanvas::create_objects(EDL *edl)
218 {
219         Canvas::create_objects(edl);
220         canvas_menu->add_item(new ZWindowCanvasTileMixers(this));
221 }
222
223 void ZWindowCanvas::close_source()
224 {
225         gui->unlock_window();
226         gui->zwindow->zgui->playback_engine->interrupt_playback(1);
227         gui->lock_window("ZWindowCanvas::close_source");
228         EDL *edl = gui->zwindow->edl;
229         if( edl ) {
230                 gui->zwindow->edl = 0;
231                 edl->remove_user();
232         }
233 }
234
235 void ZWindowCanvas::draw_refresh(int flush)
236 {
237         EDL *edl = gui->zwindow->edl;
238         BC_WindowBase *cvs = get_canvas();
239         int dirty = 0;
240
241         if( !cvs->get_video_on() ) {
242                 cvs->clear_box(0, 0, cvs->get_w(), cvs->get_h());
243                 gui->highlighted = 0;
244                 dirty = 1;
245         }
246         if( !cvs->get_video_on() && refresh_frame && edl ) {
247                 float in_x1, in_y1, in_x2, in_y2;
248                 float out_x1, out_y1, out_x2, out_y2;
249                 get_transfers(edl,
250                         in_x1, in_y1, in_x2, in_y2,
251                         out_x1, out_y1, out_x2, out_y2);
252                 cvs->draw_vframe(refresh_frame,
253                         (int)out_x1, (int)out_y1, (int)(out_x2 - out_x1), (int)(out_y2 - out_y1),
254                         (int)in_x1, (int)in_y1, (int)(in_x2 - in_x1), (int)(in_y2 - in_y1), 0);
255                 dirty = 1;
256         }
257         
258         if( gui->draw_overlays() )
259                 dirty = 1;
260
261         if( dirty )
262                 cvs->flash(flush);
263 }
264
265 float ZWindowCanvas::get_auto_zoom()
266 {
267         EDL *edl = gui->zwindow->edl;
268         if( !edl ) edl = mwindow->edl;
269         float conformed_w, conformed_h;
270         edl->calculate_conformed_dimensions(0, conformed_w, conformed_h);
271         BC_WindowBase *window = get_canvas();
272         int cw = window ? window->get_w() : w;
273         int ch = window ? window->get_h() : h;
274         float zoom_x = cw / conformed_w;
275         float zoom_y = ch / conformed_h;
276         return zoom_x < zoom_y ? zoom_x : zoom_y;
277 }
278
279 float ZWindowCanvas::get_zoom()
280 {
281         return gui->zwindow->zoom;
282 }
283 void ZWindowCanvas::update_zoom(int x, int y, float zoom)
284 {
285         gui->zwindow->zoom = zoom;
286 }
287
288 void ZWindowCanvas::zoom_auto()
289 {
290         EDL *edl = gui->zwindow->edl;
291         if( !edl ) edl = mwindow->edl;
292         set_zoom(edl, 0);
293 }
294
295 void ZWindowCanvas::zoom_resize_window(float zoom)
296 {
297         if( !zoom ) zoom = get_auto_zoom();
298         EDL *edl = gui->zwindow->edl;
299         if( !edl ) edl = mwindow->edl;
300         int ow = edl->session->output_w, oh = edl->session->output_h;
301         int canvas_w, canvas_h;
302         calculate_sizes(mwindow->edl->get_aspect_ratio(), ow, oh,
303                 zoom, canvas_w, canvas_h);
304         int new_w = canvas_w + xS(20);
305         int new_h = canvas_h + yS(20);
306         gui->resize_window(new_w, new_h);
307         gui->resize_event(new_w, new_h);
308 }