Add back 2 patches for histogram and overlayframe that are working correctly and...
[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         playable = zwindow->playable;
51 }
52
53 ZWindowGUI::~ZWindowGUI()
54 {
55         delete playback_engine;
56         delete canvas;
57 }
58
59 void ZWindowGUI::create_objects()
60 {
61         lock_window("ZWindowGUI::create_objects");
62
63         canvas = new ZWindowCanvas(mwindow, this,
64                         xS(10),yS(10), get_w()-xS(20),get_h()-yS(20));
65         canvas->create_objects(mwindow->edl);
66         playback_engine = new PlaybackEngine(mwindow, canvas);
67         playback_engine->create_objects();
68
69         deactivate();
70         show_window();
71         unlock_window();
72 }
73
74 int ZWindowGUI::resize_event(int w, int h)
75 {
76         canvas->reposition_window(0,
77                         xS(10),yS(10), w-xS(20),h-yS(20));
78         zwindow->reposition(get_x(), get_y(), w, h);
79         BC_WindowBase::resize_event(w, h);
80         return 1;
81 }
82 int ZWindowGUI::translation_event()
83 {
84         return resize_event(get_w(),get_h());
85 }
86
87 int ZWindowGUI::close_event()
88 {
89         set_done(1);
90         return 1;
91 }
92
93 int ZWindowGUI::keypress_event()
94 {
95         int key = get_keypress();
96         if( key == 'w' || key == 'W' ) {
97                 close_event();
98                 return 1;
99         }
100         int result = 1;
101         switch( key ) {
102         case 'f': {
103                 int on = canvas->get_fullscreen() ? 0 : 1;
104                 canvas->set_fullscreen(on, 1);
105                 break; }
106         case ESC:
107                 canvas->set_fullscreen(0, 1);
108                 break;
109         default:
110                 unlock_window();
111                 mwindow->gui->lock_window("ZWindowGUI::keypress_event");
112                 result = mwindow->gui->mbuttons->transport->do_keypress(key);
113                 mwindow->gui->unlock_window();
114                 lock_window("ZWindowGUI::keypress_event 1");
115         }
116
117         return result;
118 }
119
120 int ZWindowGUI::button_press_event()
121 {
122         mwindow->select_zwindow(zwindow);
123         if( get_double_click() ) {
124                 unlock_window();
125                 mwindow->gui->lock_window("ZWindowGUI::button_press_event 0");
126                 LocalSession *local_session = mwindow->edl->local_session;
127                 double start = local_session->get_selectionstart(1);
128                 double end = local_session->get_selectionend(1);
129                 if( EQUIV(start, end) ) {
130                         start = mwindow->edl->tracks->total_recordable_length();
131                         if( start < 0 ) start = end;
132                 }
133                 if( (end-start) > 1e-4 ) {
134                         LocalSession *zlocal_session = zwindow->edl->local_session;
135                         zlocal_session->set_selectionstart(end);
136                         zlocal_session->set_selectionend(end);
137                         zlocal_session->set_inpoint(start);
138                         zlocal_session->set_outpoint(end);
139                         double orig_inpoint = local_session->get_inpoint();
140                         double orig_outpoint = local_session->get_outpoint();
141                         local_session->set_selectionstart(end);
142                         local_session->set_selectionend(end);
143                         local_session->set_inpoint(start);
144                         local_session->set_outpoint(end);
145                         mwindow->overwrite(zwindow->edl, 0);
146                         local_session->set_inpoint(orig_inpoint);
147                         local_session->set_outpoint(orig_outpoint);
148                         mwindow->gui->update_timebar(1);
149                 }
150                 mwindow->gui->unlock_window();
151                 lock_window("ZWindowGUI::button_press_event 1");
152         }
153         if( !canvas->get_canvas() ) return 0;
154         return canvas->button_press_event_base(canvas->get_canvas());
155 }
156
157 int ZWindowGUI::cursor_leave_event()
158 {
159         if( !canvas->get_canvas() ) return 0;
160         return canvas->cursor_leave_event_base(canvas->get_canvas());
161 }
162
163 int ZWindowGUI::cursor_enter_event()
164 {
165         if( !canvas->get_canvas() ) return 0;
166         return canvas->cursor_enter_event_base(canvas->get_canvas());
167 }
168
169 int ZWindowGUI::button_release_event()
170 {
171         if( !canvas->get_canvas() ) return 0;
172         return canvas->button_release_event();
173 }
174
175 int ZWindowGUI::cursor_motion_event()
176 {
177         BC_WindowBase *cvs = canvas->get_canvas();
178         if( !cvs ) return 0;
179         cvs->unhide_cursor();
180         return canvas->cursor_motion_event();
181 }
182
183 int ZWindowGUI::draw_overlays()
184 {
185         BC_WindowBase *cvs = canvas->get_canvas();
186         if( !cvs || cvs->get_video_on() ) return 0;
187         set_highlighted(zwindow->highlighted);
188         if( !playable ) set_playable(-1);
189         return 1;
190 }
191
192 void ZWindowGUI::set_highlighted(int v)
193 {
194         if( highlighted == v ) return;
195         highlighted = v;
196         BC_WindowBase *cvs = canvas->get_canvas();
197         cvs->set_color(WHITE);
198         cvs->set_inverse();
199         cvs->draw_rectangle(0, 0, cvs->get_w(), cvs->get_h());
200         cvs->draw_rectangle(1, 1, cvs->get_w() - 2, cvs->get_h() - 2);
201         cvs->set_opaque();
202 }
203
204 void ZWindowGUI::set_playable(int v)
205 {
206         if( playable == v ) return;
207         playable = v>0 ? 1 : 0;
208         zwindow->playable = playable;
209         BC_WindowBase *cvs = canvas->get_canvas();
210         cvs->set_color(WHITE);
211         cvs->set_inverse();
212         int dx = cvs->get_w()/16+1, dy = cvs->get_h()/16+1;
213         int x = xS(5), y = yS(5), lw = (dx + dy)/16+1;
214         cvs->set_line_width(lw);
215         cvs->draw_line(x, y, x+dx, y+dy);
216         cvs->draw_line(x, y+dy, x+dx, y);
217         cvs->set_opaque();
218         cvs->set_line_width(1);
219 }
220
221
222 ZWindowCanvasTileMixers::ZWindowCanvasTileMixers(ZWindowCanvas *canvas)
223  : BC_MenuItem(_("Tile Mixers"))
224 {
225         this->canvas = canvas;
226 }
227 int ZWindowCanvasTileMixers::handle_event()
228 {
229         canvas->mwindow->tile_mixers();
230         return 1;
231 }
232
233 ZWindowCanvasPlayable::ZWindowCanvasPlayable(ZWindowCanvas *canvas)
234  : BC_MenuItem(_("Playable"))
235 {
236         this->canvas = canvas;
237         set_checked(canvas->gui->zwindow->playable);
238 }
239 int ZWindowCanvasPlayable::handle_event()
240 {
241         int v = !get_checked() ? 1 : 0;
242         set_checked(v);
243         canvas->gui->set_playable(v);
244         canvas->get_canvas()->flash(1);
245         return 1;
246 }
247
248 ZWindowCanvas::ZWindowCanvas(MWindow *mwindow, ZWindowGUI *gui,
249                 int x, int y, int w, int h)
250  : Canvas(mwindow, gui, x,y, w,h, 0,0,0)
251 {
252         this->mwindow = mwindow;
253         this->gui = gui;
254 }
255
256 void ZWindowCanvas::create_objects(EDL *edl)
257 {
258         Canvas::create_objects(edl);
259         canvas_menu->add_item(new ZWindowCanvasTileMixers(this));
260         canvas_menu->add_item(new ZWindowCanvasPlayable(this));
261 }
262
263 void ZWindowCanvas::close_source()
264 {
265         gui->unlock_window();
266         gui->zwindow->zgui->playback_engine->interrupt_playback(1);
267         gui->lock_window("ZWindowCanvas::close_source");
268         EDL *edl = gui->zwindow->edl;
269         if( edl ) {
270                 gui->zwindow->edl = 0;
271                 edl->remove_user();
272         }
273 }
274
275 void ZWindowCanvas::draw_refresh(int flush)
276 {
277         EDL *edl = gui->zwindow->edl;
278         BC_WindowBase *cvs = get_canvas();
279         int dirty = 0;
280
281         if( !cvs->get_video_on() ) {
282                 cvs->clear_box(0, 0, cvs->get_w(), cvs->get_h());
283                 gui->highlighted = 0;
284                 dirty = 1;
285         }
286         if( !cvs->get_video_on() && refresh_frame && edl ) {
287                 float in_x1, in_y1, in_x2, in_y2;
288                 float out_x1, out_y1, out_x2, out_y2;
289                 get_transfers(edl,
290                         in_x1, in_y1, in_x2, in_y2,
291                         out_x1, out_y1, out_x2, out_y2);
292                 cvs->draw_vframe(refresh_frame,
293                         (int)out_x1, (int)out_y1, (int)(out_x2 - out_x1), (int)(out_y2 - out_y1),
294                         (int)in_x1, (int)in_y1, (int)(in_x2 - in_x1), (int)(in_y2 - in_y1), 0);
295                 dirty = 1;
296         }
297         
298         if( gui->draw_overlays() )
299                 dirty = 1;
300         if( dirty )
301                 cvs->flash(flush);
302 }
303
304 float ZWindowCanvas::get_auto_zoom()
305 {
306         EDL *edl = gui->zwindow->edl;
307         if( !edl ) edl = mwindow->edl;
308         float conformed_w, conformed_h;
309         edl->calculate_conformed_dimensions(0, conformed_w, conformed_h);
310         BC_WindowBase *window = get_canvas();
311         int cw = window ? window->get_w() : w;
312         int ch = window ? window->get_h() : h;
313         float zoom_x = cw / conformed_w;
314         float zoom_y = ch / conformed_h;
315         return zoom_x < zoom_y ? zoom_x : zoom_y;
316 }
317
318 float ZWindowCanvas::get_zoom()
319 {
320         return gui->zwindow->zoom;
321 }
322 void ZWindowCanvas::update_zoom(int x, int y, float zoom)
323 {
324         gui->zwindow->zoom = zoom;
325 }
326
327 void ZWindowCanvas::zoom_auto()
328 {
329         EDL *edl = gui->zwindow->edl;
330         if( !edl ) edl = mwindow->edl;
331         set_zoom(edl, 0);
332 }
333
334 void ZWindowCanvas::zoom_resize_window(float zoom)
335 {
336         if( !zoom ) zoom = get_auto_zoom();
337         EDL *edl = gui->zwindow->edl;
338         if( !edl ) edl = mwindow->edl;
339         int ow = edl->session->output_w, oh = edl->session->output_h;
340         int canvas_w, canvas_h;
341         calculate_sizes(mwindow->edl->get_aspect_ratio(), ow, oh,
342                 zoom, canvas_w, canvas_h);
343         int new_w = canvas_w + xS(20);
344         int new_h = canvas_h + yS(20);
345         gui->resize_window(new_w, new_h);
346         gui->resize_event(new_w, new_h);
347 }
348