4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
25 #include "awindowgui.h"
32 #include "edlsession.h"
33 #include "filesystem.h"
40 #include "localsession.h"
41 #include "mainclock.h"
43 #include "mainsession.h"
45 #include "meterpanel.h"
46 #include "mwindowgui.h"
48 #include "playtransport.h"
49 #include "preferences.h"
50 #include "renderengine.h"
55 #include "transportque.h"
57 #include "vplayback.h"
59 #include "vwindowgui.h"
65 VWindowGUI::VWindowGUI(MWindow *mwindow, VWindow *vwindow)
66 : BC_Window(_(PROGRAM_NAME ": Viewer"),
67 mwindow->session->vwindow_x,
68 mwindow->session->vwindow_y,
69 mwindow->session->vwindow_w,
70 mwindow->session->vwindow_h,
71 xS(100), yS(100), 1, 1, 0) // Hide it
73 this->mwindow = mwindow;
74 this->vwindow = vwindow;
80 strcpy(loaded_title, "");
84 VWindowGUI::~VWindowGUI()
86 vwindow->stop_playback(1);
87 sources.remove_all_objects();
88 labels.remove_all_objects();
96 void VWindowGUI::draw_wave()
98 TransportCommand command;
99 command.command = NORMAL_FWD;
100 command.get_edl()->copy_all(vwindow->get_edl());
101 command.change_type = CHANGE_ALL;
102 command.realtime = 0;
103 RenderEngine *render_engine = new RenderEngine(0, mwindow->preferences, 0, 0);
104 CICache *cache = new CICache(mwindow->preferences);
105 render_engine->set_acache(cache);
106 render_engine->arm_command(&command);
108 double duration = 1.;
109 int w = mwindow->edl->session->output_w;
110 int h = mwindow->edl->session->output_h;
111 VFrame *vframe = new VFrame(w, h, BC_RGB888);
112 vframe->clear_frame();
113 int sample_rate = mwindow->edl->get_sample_rate();
114 int channels = mwindow->edl->session->audio_channels;
115 if( channels > 2 ) channels = 2;
116 int64_t bfrsz = sample_rate * duration;
117 Samples *samples[MAXCHANNELS];
119 while( ch < channels ) samples[ch++] = new Samples(bfrsz);
120 while( ch < MAXCHANNELS ) samples[ch++] = 0;
121 render_engine->arender->process_buffer(samples, bfrsz, 0);
123 static int line_colors[2] = { GREEN, YELLOW };
124 static int base_colors[2] = { RED, PINK };
125 for( int i=channels; --i>=0; ) {
126 AssetPicon::draw_wave(vframe, samples[i]->get_data(), bfrsz,
127 base_colors[i], line_colors[i]);
130 for( int i=channels; --i>=0; ) delete samples[i];
131 delete render_engine;
132 cache->remove_user();
133 delete canvas->refresh_frame;
134 canvas->refresh_frame = vframe;
138 void VWindowGUI::change_source(EDL *edl, const char *title)
140 //printf("VWindowGUI::change_source %d\n", __LINE__);
142 update_sources(title);
143 strcpy(loaded_title, title);
144 char string[BCTEXTLEN];
146 sprintf(string, _(PROGRAM_NAME ": %s"), title);
148 sprintf(string, _(PROGRAM_NAME ": Viewer"));
150 lock_window("VWindowGUI::change_source");
153 !edl->tracks->playable_video_tracks() &&
154 edl->tracks->playable_audio_tracks() )
162 // Get source list from master EDL
163 void VWindowGUI::update_sources(const char *title)
165 lock_window("VWindowGUI::update_sources");
166 sources.remove_all_objects();
168 for( int i=0; i<mwindow->edl->clips.size(); ++i ) {
169 char *clip_title = mwindow->edl->clips.values[i]->local_session->clip_title;
172 for( int j=0; !exists && j<sources.size(); ++j ) {
173 if( !strcasecmp(sources.values[j]->get_text(), clip_title) )
178 sources.append(new BC_ListBoxItem(clip_title));
182 for( Asset *current=mwindow->edl->assets->first; current; current=NEXT ) {
183 char clip_title[BCTEXTLEN];
184 fs.extract_name(clip_title, current->path);
187 for( int j=0; !exists && j<sources.size(); ++j ) {
188 if( !strcasecmp(sources.values[j]->get_text(), clip_title) )
193 sources.append(new BC_ListBoxItem(clip_title));
199 void VWindowGUI::create_objects()
201 lock_window("VWindowGUI::create_objects");
204 set_icon(mwindow->theme->get_image("vwindow_icon"));
206 //printf("VWindowGUI::create_objects 1\n");
207 mwindow->theme->get_vwindow_sizes(this);
208 mwindow->theme->draw_vwindow_bg(this);
211 meters = new VWindowMeters(mwindow,
213 mwindow->theme->vmeter_x,
214 mwindow->theme->vmeter_y,
215 mwindow->theme->vmeter_h);
216 meters->create_objects();
218 //printf("VWindowGUI::create_objects 1\n");
219 // Requires meters to build
220 edit_panel = new VWindowEditing(mwindow, vwindow);
221 edit_panel->set_meters(meters);
222 edit_panel->create_objects();
224 //printf("VWindowGUI::create_objects 1\n");
225 transport = new VWindowTransport(mwindow,
227 mwindow->theme->vtransport_x,
228 mwindow->theme->vtransport_y);
229 transport->create_objects();
230 transport->set_transport(LOOP_MODE);
232 //printf("VWindowGUI::create_objects 1\n");
233 // add_subwindow(fps_title = new BC_Title(mwindow->theme->vedit_x, y, ""));
234 add_subwindow(clock = new MainClock(mwindow,
235 mwindow->theme->vtime_x,
236 mwindow->theme->vtime_y,
237 mwindow->theme->vtime_w));
239 canvas = new VWindowCanvas(mwindow, this);
240 canvas->create_objects(mwindow->edl);
241 char vsplash_path[BCTEXTLEN];
242 int vsplash_len = sizeof(vsplash_path)-1;
243 snprintf(vsplash_path, vsplash_len, "%s/vsplash.png", File::get_cindat_path());
244 VFrame *vsplash = VFramePng::vframe_png(vsplash_path);
246 BC_WindowBase *vcanvas = canvas->get_canvas();
247 vcanvas->draw_vframe(vsplash,
248 0,0, vcanvas->get_w(), vcanvas->get_h(),
249 0,0, vsplash->get_w(), vsplash->get_h(), 0);
253 //printf("VWindowGUI::create_objects 1\n");
254 add_subwindow(timebar = new VTimeBar(mwindow,
256 mwindow->theme->vtimebar_x,
257 mwindow->theme->vtimebar_y,
258 mwindow->theme->vtimebar_w,
259 mwindow->theme->vtimebar_h));
260 timebar->create_objects();
261 //printf("VWindowGUI::create_objects 2\n");
264 //printf("VWindowGUI::create_objects 1\n");
265 // source = new VWindowSource(mwindow,
267 // mwindow->theme->vsource_x,
268 // mwindow->theme->vsource_y);
269 // source->create_objects();
270 update_sources(_("None"));
272 //printf("VWindowGUI::create_objects 2\n");
279 int VWindowGUI::resize_event(int w, int h)
281 mwindow->session->vwindow_x = get_x();
282 mwindow->session->vwindow_y = get_y();
283 mwindow->session->vwindow_w = w;
284 mwindow->session->vwindow_h = h;
286 mwindow->theme->get_vwindow_sizes(this);
287 mwindow->theme->draw_vwindow_bg(this);
290 //printf("VWindowGUI::resize_event %d %d\n", __LINE__, mwindow->theme->vedit_y);
291 edit_panel->reposition_buttons(mwindow->theme->vedit_x,
292 mwindow->theme->vedit_y);
294 timebar->resize_event();
295 transport->reposition_buttons(mwindow->theme->vtransport_x,
296 mwindow->theme->vtransport_y);
297 clock->reposition_window(mwindow->theme->vtime_x,
298 mwindow->theme->vtime_y,
299 mwindow->theme->vtime_w,
301 canvas->reposition_window(0,
302 mwindow->theme->vcanvas_x,
303 mwindow->theme->vcanvas_y,
304 mwindow->theme->vcanvas_w,
305 mwindow->theme->vcanvas_h);
306 //printf("VWindowGUI::resize_event %d %d\n", __LINE__, mwindow->theme->vcanvas_x);
307 // source->reposition_window(mwindow->theme->vsource_x,
308 // mwindow->theme->vsource_y);
309 meters->reposition_window(mwindow->theme->vmeter_x,
310 mwindow->theme->vmeter_y,
312 mwindow->theme->vmeter_h);
314 BC_WindowBase::resize_event(w, h);
322 int VWindowGUI::translation_event()
324 mwindow->session->vwindow_x = get_x();
325 mwindow->session->vwindow_y = get_y();
329 int VWindowGUI::close_event()
332 int i = mwindow->vwindows.size();
333 while( --i >= 0 && mwindow->vwindows.get(i)->gui != this );
339 mwindow->session->show_vwindow = 0;
342 mwindow->gui->lock_window("VWindowGUI::close_event");
343 mwindow->gui->mainmenu->show_vwindow->set_checked(0);
344 mwindow->gui->unlock_window();
346 lock_window("VWindowGUI::close_event");
347 mwindow->save_defaults();
351 int VWindowGUI::keypress_event()
354 switch( get_keypress() ) {
361 mwindow->undo_entry(this);
364 mwindow->redo_entry(this);
367 int on = canvas->get_fullscreen() ? 0 : 1;
368 canvas->set_fullscreen(on, 1);
371 canvas->set_fullscreen(0, 1);
377 if( ctrl_down() && shift_down() ) {
378 resend_event(mwindow->gui);
384 result = transport->keypress_event();
388 int VWindowGUI::button_press_event()
390 if( vwindow->get_edl() != 0 && canvas->get_canvas() &&
391 mwindow->edl->session->vwindow_click2play &&
392 canvas->get_canvas()->get_cursor_over_window() ) {
393 switch( get_buttonpress() ) {
395 if( !vwindow->playback_engine->is_playing_back ) {
396 double length = vwindow->get_edl()->tracks->total_playable_length();
397 double position = vwindow->playback_engine->get_tracking_position();
398 if( position >= length ) transport->goto_start();
400 return transport->forward_play->handle_event();
402 if( !vwindow->playback_engine->is_playing_back ) {
403 double position = vwindow->playback_engine->get_tracking_position();
404 if( position <= 0 ) transport->goto_end();
406 return transport->reverse_play->handle_event();
407 case RIGHT_BUTTON: // activates popup
410 return transport->frame_forward_play->handle_event();
412 return transport->frame_reverse_play->handle_event();
415 if(canvas->get_canvas())
416 return canvas->button_press_event_base(canvas->get_canvas());
420 int VWindowGUI::cursor_leave_event()
422 if(canvas->get_canvas())
423 return canvas->cursor_leave_event_base(canvas->get_canvas());
427 int VWindowGUI::cursor_enter_event()
429 if(canvas->get_canvas())
430 return canvas->cursor_enter_event_base(canvas->get_canvas());
434 int VWindowGUI::button_release_event()
436 if(canvas->get_canvas())
437 return canvas->button_release_event();
441 int VWindowGUI::cursor_motion_event()
443 if(canvas->get_canvas())
445 canvas->get_canvas()->unhide_cursor();
446 return canvas->cursor_motion_event();
452 void VWindowGUI::drag_motion()
455 if(get_hidden()) return;
456 if(mwindow->session->current_operation != DRAG_ASSET) return;
457 int need_highlight = cursor_above() && get_cursor_over_window() ? 1 : 0;
458 if( highlighted == need_highlight ) return;
459 highlighted = need_highlight;
463 int VWindowGUI::drag_stop()
465 if( get_hidden() ) return 0;
468 mwindow->session->current_operation == DRAG_ASSET ) {
473 Indexable *indexable =
474 mwindow->session->drag_assets->size() > 0 ?
475 (Indexable *)mwindow->session->drag_assets->get(0) :
476 mwindow->session->drag_clips->size() > 0 ?
477 (Indexable *)mwindow->session->drag_clips->get(0) : 0;
479 vwindow->change_source(indexable);
481 lock_window("VWindowGUI::drag_stop");
488 void VWindowGUI::stop_transport()
490 if( !transport->is_stopped() ) {
492 transport->handle_transport(STOP, 1);
493 lock_window("VWindowGUI::panel_stop_transport");
497 void VWindowGUI::update_meters()
499 if(mwindow->edl->session->vwindow_meter != meters->visible)
501 meters->set_meters(meters->meter_count,
502 mwindow->edl->session->vwindow_meter);
503 mwindow->theme->get_vwindow_sizes(this);
504 resize_event(get_w(), get_h());
510 VWindowMeters::VWindowMeters(MWindow *mwindow,
515 : MeterPanel(mwindow,
521 mwindow->edl->session->audio_channels,
522 mwindow->edl->session->vwindow_meter,
526 this->mwindow = mwindow;
530 VWindowMeters::~VWindowMeters()
534 int VWindowMeters::change_status_event(int new_status)
536 mwindow->edl->session->vwindow_meter = new_status;
537 gui->update_meters();
542 VWindowEditing::VWindowEditing(MWindow *mwindow, VWindow *vwindow)
543 : EditPanel(mwindow, vwindow->gui, VWINDOW_ID,
544 mwindow->theme->vedit_x, mwindow->theme->vedit_y,
546 0, // use_editing_mode
563 0) // use_gang_tracks
565 this->mwindow = mwindow;
566 this->vwindow = vwindow;
569 VWindowEditing::~VWindowEditing()
573 #define relock_vm(s) \
574 vwindow->gui->unlock_window(); \
575 mwindow->gui->lock_window("VWindowEditing::" s)
576 #define relock_mv(s) \
577 mwindow->gui->unlock_window(); \
578 vwindow->gui->lock_window("VWindowEditing::" s)
580 double VWindowEditing::get_position()
582 EDL *edl = vwindow->get_edl();
583 double position = !edl ? 0 : edl->local_session->get_selectionstart(1);
587 void VWindowEditing::set_position(double position)
589 EDL *edl = vwindow->get_edl();
591 if( get_position() != position ) {
592 if( position < 0 ) position = 0;
593 edl->local_session->set_selectionstart(position);
594 edl->local_session->set_selectionend(position);
595 vwindow->update_position();
599 void VWindowEditing::set_click_to_play(int v)
601 click2play->update(v);
602 relock_vm("set_click_to_play");
603 mwindow->edl->session->vwindow_click2play = v;
604 mwindow->update_vwindow();
605 relock_mv("set_click_to_play");
608 void VWindowEditing::panel_stop_transport()
610 vwindow->gui->stop_transport();
613 void VWindowEditing::panel_toggle_label()
615 if( !vwindow->get_edl() ) return;
616 EDL *edl = vwindow->get_edl();
617 edl->labels->toggle_label(edl->local_session->get_selectionstart(1),
618 edl->local_session->get_selectionend(1));
619 vwindow->gui->timebar->update(1);
622 void VWindowEditing::panel_next_label(int cut)
624 if( !vwindow->get_edl() ) return;
625 vwindow->interrupt_playback(1);
627 EDL *edl = vwindow->get_edl();
628 Label *current = edl->labels->next_label(
629 edl->local_session->get_selectionstart(1));
630 double position = current ? current->position :
631 edl->tracks->total_length();
632 edl->local_session->set_selectionstart(position);
633 edl->local_session->set_selectionend(position);
634 vwindow->update_position();
635 vwindow->gui->timebar->update(1);
638 void VWindowEditing::panel_prev_label(int cut)
640 if( !vwindow->get_edl() ) return;
641 vwindow->interrupt_playback(1);
643 EDL *edl = vwindow->get_edl();
644 Label *current = edl->labels->prev_label(
645 edl->local_session->get_selectionstart(1));
646 double position = !current ? 0 : current->position;
647 edl->local_session->set_selectionstart(position);
648 edl->local_session->set_selectionend(position);
649 vwindow->update_position();
650 vwindow->gui->timebar->update(1);
653 void VWindowEditing::panel_prev_edit(int cut) {} // not used
654 void VWindowEditing::panel_next_edit(int cut) {} // not used
656 void VWindowEditing::panel_copy_selection()
658 vwindow->copy(vwindow->gui->shift_down());
661 void VWindowEditing::panel_overwrite_selection()
663 if( !vwindow->get_edl() ) return;
664 relock_vm("overwrite_selection");
665 mwindow->overwrite(vwindow->get_edl(), vwindow->gui->shift_down());
666 relock_mv("overwrite_selection");
669 void VWindowEditing::panel_splice_selection()
671 if( !vwindow->get_edl() ) return;
672 relock_vm("splice_selection");
673 mwindow->splice(vwindow->get_edl(), vwindow->gui->shift_down());
674 relock_mv("splice_selection");
677 void VWindowEditing::panel_set_inpoint()
679 vwindow->set_inpoint();
682 void VWindowEditing::panel_set_outpoint()
684 vwindow->set_outpoint();
687 void VWindowEditing::panel_unset_inoutpoint()
689 vwindow->unset_inoutpoint();
692 void VWindowEditing::panel_to_clip()
694 EDL *edl = vwindow->get_edl();
696 mwindow->to_clip(edl, _("viewer window: "), subwindow->shift_down());
700 void VWindowEditing::panel_cut() {}
701 void VWindowEditing::panel_paste() {}
702 void VWindowEditing::panel_fit_selection() {}
703 void VWindowEditing::panel_fit_autos(int all) {}
704 void VWindowEditing::panel_set_editing_mode(int mode) {}
705 void VWindowEditing::panel_set_auto_keyframes(int v) {}
706 void VWindowEditing::panel_set_span_keyframes(int v) {}
707 void VWindowEditing::panel_set_labels_follow_edits(int v) {}
708 void VWindowEditing::panel_set_gang_tracks(int v) {}
711 VWindowSource::VWindowSource(MWindow *mwindow, VWindowGUI *vwindow, int x, int y)
712 : BC_PopupTextBox(vwindow, &vwindow->sources, "",
713 x, y, xS(200), yS(200))
715 this->mwindow = mwindow;
716 this->vwindow = vwindow;
719 VWindowSource::~VWindowSource()
723 int VWindowSource::handle_event()
729 VWindowTransport::VWindowTransport(MWindow *mwindow,
733 : PlayTransport(mwindow,
741 EDL* VWindowTransport::get_edl()
743 return gui->vwindow->get_edl();
747 void VWindowTransport::goto_start()
749 gui->unlock_window();
750 handle_transport(REWIND, 1);
751 gui->lock_window("VWindowTransport::goto_start");
752 gui->vwindow->goto_start();
755 void VWindowTransport::goto_end()
757 gui->unlock_window();
758 handle_transport(GOTO_END, 1);
759 gui->lock_window("VWindowTransport::goto_end");
760 gui->vwindow->goto_end();
766 VWindowCanvas::VWindowCanvas(MWindow *mwindow, VWindowGUI *gui)
769 mwindow->theme->vcanvas_x,
770 mwindow->theme->vcanvas_y,
771 mwindow->theme->vcanvas_w,
772 mwindow->theme->vcanvas_h,
775 //printf("VWindowCanvas::VWindowCanvas %d %d\n", __LINE__, mwindow->theme->vcanvas_x);
776 this->mwindow = mwindow;
780 void VWindowCanvas::create_objects(EDL *edl)
782 Canvas::create_objects(edl);
783 canvas_menu->add_item(new CanvasPopupRemoveSource(this));
786 void VWindowCanvas::zoom_resize_window(float percentage)
788 EDL *edl = gui->vwindow->get_edl();
789 if(!edl) edl = mwindow->edl;
791 int canvas_w, canvas_h;
794 // Get required canvas size
795 calculate_sizes(edl->get_aspect_ratio(),
796 edl->session->output_w,
797 edl->session->output_h,
802 // Estimate window size from current borders
803 new_w = canvas_w + (gui->get_w() - mwindow->theme->vcanvas_w);
804 new_h = canvas_h + (gui->get_h() - mwindow->theme->vcanvas_h);
806 mwindow->session->vwindow_w = new_w;
807 mwindow->session->vwindow_h = new_h;
809 mwindow->theme->get_vwindow_sizes(gui);
811 // Estimate again from new borders
812 new_w = canvas_w + (mwindow->session->vwindow_w - mwindow->theme->vcanvas_w);
813 new_h = canvas_h + (mwindow->session->vwindow_h - mwindow->theme->vcanvas_h);
816 gui->resize_window(new_w, new_h);
817 gui->resize_event(new_w, new_h);
820 void VWindowCanvas::zoom_auto()
822 EDL *edl = gui->vwindow->get_edl();
823 if(!edl) edl = mwindow->edl;
827 void VWindowCanvas::close_source()
829 gui->vwindow->interrupt_playback(1);
830 gui->vwindow->delete_source(1, 1);
833 int VWindowCanvas::scope_on()
835 EditPanelScopeDialog *scope_dialog = gui->edit_panel->scope_dialog;
836 if( !scope_dialog || !scope_dialog->scope_gui ) return 0;
837 if( scope_dialog->scope_gui->use_refresh ) return 0;
838 if( scope_dialog->scope_gui->use_release ) return 0;
839 return scope_dialog->running();
842 void VWindowCanvas::draw_scope(VFrame *output, int refresh)
844 if( !output ) return;
845 EditPanelScopeDialog *scope_dialog = gui->edit_panel->scope_dialog;
846 if( !scope_dialog || !scope_dialog->scope_gui ) return;
847 if( scope_dialog->scope_gui->use_refresh && !refresh ) return;
848 if( scope_dialog->scope_gui->use_release && refresh >= 0 ) return;
849 scope_dialog->process(output);
852 int VWindowCanvas::button_release_event()
854 BC_WindowBase *window = get_canvas();
855 if( window && !window->get_video_on() )
856 draw_scope(refresh_frame, -1);
857 return Canvas::button_release_event();
860 void VWindowCanvas::draw_refresh(int flush)
862 if( !get_canvas()->get_video_on() ) {
863 int cw = get_canvas()->get_w(), ch = get_canvas()->get_h();
864 get_canvas()->clear_box(0, 0, cw, ch);
866 EDL *edl = gui->vwindow->get_edl();
867 if( refresh_frame && edl ) {
868 int ow = get_output_w(edl), oh = get_output_h(edl);
869 if( ow > 0 && oh > 0 ) {
870 float in_x1, in_y1, in_x2, in_y2;
871 float out_x1, out_y1, out_x2, out_y2;
873 in_x1, in_y1, in_x2, in_y2,
874 out_x1, out_y1, out_x2, out_y2);
875 // input scaled from session to refresh frame coordinates
876 int rw = refresh_frame->get_w();
877 int rh = refresh_frame->get_h();
878 float xs = (float)rw / ow;
879 float ys = (float)rh / oh;
880 in_x1 *= xs; in_x2 *= xs;
881 in_y1 *= ys; in_y2 *= ys;
882 get_canvas()->draw_vframe(refresh_frame,
883 (int)out_x1, (int)out_y1,
884 (int)(out_x2 - out_x1), (int)(out_y2 - out_y1),
885 (int)in_x1, (int)in_y1,
886 (int)(in_x2 - in_x1), (int)(in_y2 - in_y1),
890 if( !get_canvas()->get_video_on() ) {
892 get_canvas()->flash(flush);
896 int VWindowCanvas::need_overlays()
898 if( gui->highlighted ) return 1;
902 void VWindowCanvas::draw_overlays()
904 if( gui->highlighted )
906 get_canvas()->set_color(WHITE);
907 get_canvas()->set_inverse();
908 get_canvas()->draw_rectangle(0, 0, get_canvas()->get_w(), get_canvas()->get_h());
909 get_canvas()->draw_rectangle(1, 1, get_canvas()->get_w() - 2, get_canvas()->get_h() - 2);
910 get_canvas()->set_opaque();