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"
39 #include "localsession.h"
40 #include "mainclock.h"
42 #include "mainsession.h"
44 #include "meterpanel.h"
45 #include "mwindowgui.h"
47 #include "playtransport.h"
48 #include "preferences.h"
49 #include "renderengine.h"
54 #include "transportque.h"
56 #include "vplayback.h"
58 #include "vwindowgui.h"
64 VWindowGUI::VWindowGUI(MWindow *mwindow, VWindow *vwindow)
65 : BC_Window(_(PROGRAM_NAME ": Viewer"),
66 mwindow->session->vwindow_x,
67 mwindow->session->vwindow_y,
68 mwindow->session->vwindow_w,
69 mwindow->session->vwindow_h,
76 this->mwindow = mwindow;
77 this->vwindow = vwindow;
83 strcpy(loaded_title, "");
87 VWindowGUI::~VWindowGUI()
89 vwindow->playback_engine->interrupt_playback(1);
90 sources.remove_all_objects();
91 labels.remove_all_objects();
99 void VWindowGUI::draw_wave()
101 TransportCommand command;
102 command.command = NORMAL_FWD;
103 command.get_edl()->copy_all(vwindow->get_edl());
104 command.change_type = CHANGE_ALL;
105 command.realtime = 0;
106 RenderEngine *render_engine = new RenderEngine(0, mwindow->preferences, 0, 0);
107 CICache *cache = new CICache(mwindow->preferences);
108 render_engine->set_acache(cache);
109 render_engine->arm_command(&command);
111 double duration = 1.;
112 int w = mwindow->edl->session->output_w;
113 int h = mwindow->edl->session->output_h;
114 VFrame *vframe = new VFrame(w, h, BC_RGB888);
115 vframe->clear_frame();
116 int sample_rate = mwindow->edl->get_sample_rate();
117 int channels = mwindow->edl->session->audio_channels;
118 if( channels > 2 ) channels = 2;
119 int64_t bfrsz = sample_rate * duration;
120 Samples *samples[MAXCHANNELS];
122 while( ch < channels ) samples[ch++] = new Samples(bfrsz);
123 while( ch < MAXCHANNELS ) samples[ch++] = 0;
124 render_engine->arender->process_buffer(samples, bfrsz, 0);
126 static int line_colors[2] = { GREEN, YELLOW };
127 static int base_colors[2] = { RED, PINK };
128 for( int i=channels; --i>=0; ) {
129 AssetPicon::draw_wave(vframe, samples[i]->get_data(), bfrsz,
130 base_colors[i], line_colors[i]);
133 for( int i=channels; --i>=0; ) delete samples[i];
134 delete render_engine;
136 delete canvas->refresh_frame;
137 canvas->refresh_frame = vframe;
138 canvas->draw_refresh(1);
141 void VWindowGUI::change_source(EDL *edl, const char *title)
143 //printf("VWindowGUI::change_source %d\n", __LINE__);
145 update_sources(title);
146 strcpy(loaded_title, title);
147 char string[BCTEXTLEN];
149 sprintf(string, _(PROGRAM_NAME ": %s"), title);
151 sprintf(string, _(PROGRAM_NAME ": Viewer"));
153 lock_window("VWindowGUI::change_source");
156 !edl->tracks->playable_video_tracks() &&
157 edl->tracks->playable_audio_tracks() )
165 // Get source list from master EDL
166 void VWindowGUI::update_sources(const char *title)
168 lock_window("VWindowGUI::update_sources");
169 sources.remove_all_objects();
171 for( int i=0; i<mwindow->edl->clips.size(); ++i ) {
172 char *clip_title = mwindow->edl->clips.values[i]->local_session->clip_title;
175 for( int j=0; !exists && j<sources.size(); ++j ) {
176 if( !strcasecmp(sources.values[j]->get_text(), clip_title) )
181 sources.append(new BC_ListBoxItem(clip_title));
185 for( Asset *current=mwindow->edl->assets->first; current; current=NEXT ) {
186 char clip_title[BCTEXTLEN];
187 fs.extract_name(clip_title, current->path);
190 for( int j=0; !exists && j<sources.size(); ++j ) {
191 if( !strcasecmp(sources.values[j]->get_text(), clip_title) )
196 sources.append(new BC_ListBoxItem(clip_title));
202 void VWindowGUI::create_objects()
206 lock_window("VWindowGUI::create_objects");
207 set_icon(mwindow->theme->get_image("vwindow_icon"));
209 //printf("VWindowGUI::create_objects 1\n");
210 mwindow->theme->get_vwindow_sizes(this);
211 mwindow->theme->draw_vwindow_bg(this);
214 meters = new VWindowMeters(mwindow,
216 mwindow->theme->vmeter_x,
217 mwindow->theme->vmeter_y,
218 mwindow->theme->vmeter_h);
219 meters->create_objects();
221 //printf("VWindowGUI::create_objects 1\n");
222 // Requires meters to build
223 edit_panel = new VWindowEditing(mwindow, vwindow);
224 edit_panel->set_meters(meters);
225 edit_panel->create_objects();
227 //printf("VWindowGUI::create_objects 1\n");
228 transport = new VWindowTransport(mwindow,
230 mwindow->theme->vtransport_x,
231 mwindow->theme->vtransport_y);
232 transport->create_objects();
234 //printf("VWindowGUI::create_objects 1\n");
235 // add_subwindow(fps_title = new BC_Title(mwindow->theme->vedit_x, y, ""));
236 add_subwindow(clock = new MainClock(mwindow,
237 mwindow->theme->vtime_x,
238 mwindow->theme->vtime_y,
239 mwindow->theme->vtime_w));
241 canvas = new VWindowCanvas(mwindow, this);
242 canvas->create_objects(mwindow->edl);
243 canvas->use_vwindow();
246 //printf("VWindowGUI::create_objects 1\n");
247 add_subwindow(timebar = new VTimeBar(mwindow,
249 mwindow->theme->vtimebar_x,
250 mwindow->theme->vtimebar_y,
251 mwindow->theme->vtimebar_w,
252 mwindow->theme->vtimebar_h));
253 timebar->create_objects();
254 //printf("VWindowGUI::create_objects 2\n");
257 //printf("VWindowGUI::create_objects 1\n");
258 // source = new VWindowSource(mwindow,
260 // mwindow->theme->vsource_x,
261 // mwindow->theme->vsource_y);
262 // source->create_objects();
263 update_sources(_("None"));
265 //printf("VWindowGUI::create_objects 2\n");
272 int VWindowGUI::resize_event(int w, int h)
274 mwindow->session->vwindow_x = get_x();
275 mwindow->session->vwindow_y = get_y();
276 mwindow->session->vwindow_w = w;
277 mwindow->session->vwindow_h = h;
279 mwindow->theme->get_vwindow_sizes(this);
280 mwindow->theme->draw_vwindow_bg(this);
283 //printf("VWindowGUI::resize_event %d %d\n", __LINE__, mwindow->theme->vedit_y);
284 edit_panel->reposition_buttons(mwindow->theme->vedit_x,
285 mwindow->theme->vedit_y);
287 timebar->resize_event();
288 transport->reposition_buttons(mwindow->theme->vtransport_x,
289 mwindow->theme->vtransport_y);
290 clock->reposition_window(mwindow->theme->vtime_x,
291 mwindow->theme->vtime_y,
292 mwindow->theme->vtime_w,
294 canvas->reposition_window(0,
295 mwindow->theme->vcanvas_x,
296 mwindow->theme->vcanvas_y,
297 mwindow->theme->vcanvas_w,
298 mwindow->theme->vcanvas_h);
299 //printf("VWindowGUI::resize_event %d %d\n", __LINE__, mwindow->theme->vcanvas_x);
300 // source->reposition_window(mwindow->theme->vsource_x,
301 // mwindow->theme->vsource_y);
302 meters->reposition_window(mwindow->theme->vmeter_x,
303 mwindow->theme->vmeter_y,
305 mwindow->theme->vmeter_h);
307 BC_WindowBase::resize_event(w, h);
315 int VWindowGUI::translation_event()
317 mwindow->session->vwindow_x = get_x();
318 mwindow->session->vwindow_y = get_y();
322 int VWindowGUI::close_event()
325 int i = mwindow->vwindows.size();
326 while( --i >= 0 && mwindow->vwindows.get(i)->gui != this );
332 mwindow->session->show_vwindow = 0;
335 mwindow->gui->lock_window("VWindowGUI::close_event");
336 mwindow->gui->mainmenu->show_vwindow->set_checked(0);
337 mwindow->gui->unlock_window();
339 lock_window("VWindowGUI::close_event");
340 mwindow->save_defaults();
344 int VWindowGUI::keypress_event()
347 switch(get_keypress())
355 mwindow->undo_entry(this);
358 mwindow->redo_entry(this);
362 if(mwindow->session->vwindow_fullscreen)
363 canvas->stop_fullscreen();
365 canvas->start_fullscreen();
366 lock_window("VWindowGUI::keypress_event 1");
370 if(mwindow->session->vwindow_fullscreen)
371 canvas->stop_fullscreen();
372 lock_window("VWindowGUI::keypress_event 2");
375 if(!result) result = transport->keypress_event();
379 void VWindowGUI::stop_transport(const char *lock_msg)
381 if( !transport->is_stopped() ) {
382 if( lock_msg ) unlock_window();
383 transport->handle_transport(STOP, 1, 0, 0);
384 if( lock_msg ) lock_window(lock_msg);
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;
460 canvas->draw_refresh();
463 int VWindowGUI::drag_stop()
465 if(get_hidden()) return 0;
468 mwindow->session->current_operation == DRAG_ASSET)
471 canvas->draw_refresh();
474 Indexable *indexable = mwindow->session->drag_assets->size() ?
475 mwindow->session->drag_assets->get(0) :
477 EDL *edl = mwindow->session->drag_clips->size() ?
478 mwindow->session->drag_clips->get(0) :
481 vwindow->change_source(indexable);
484 vwindow->change_source(edl);
485 lock_window("VWindowGUI::drag_stop");
493 void VWindowGUI::update_meters()
495 if(mwindow->edl->session->vwindow_meter != meters->visible)
497 meters->set_meters(meters->meter_count,
498 mwindow->edl->session->vwindow_meter);
499 mwindow->theme->get_vwindow_sizes(this);
500 resize_event(get_w(), get_h());
506 VWindowMeters::VWindowMeters(MWindow *mwindow,
511 : MeterPanel(mwindow,
517 mwindow->edl->session->audio_channels,
518 mwindow->edl->session->vwindow_meter,
522 this->mwindow = mwindow;
526 VWindowMeters::~VWindowMeters()
530 int VWindowMeters::change_status_event(int new_status)
532 mwindow->edl->session->vwindow_meter = new_status;
533 gui->update_meters();
538 VWindowEditing::VWindowEditing(MWindow *mwindow, VWindow *vwindow)
539 : EditPanel(mwindow, vwindow->gui, VWINDOW_ID,
540 mwindow->theme->vedit_x, mwindow->theme->vedit_y,
542 0, // use_editing_mode
561 this->mwindow = mwindow;
562 this->vwindow = vwindow;
565 VWindowEditing::~VWindowEditing()
569 void VWindowEditing::copy_selection()
571 vwindow->copy(subwindow->shift_down());
574 void VWindowEditing::splice_selection()
576 if(vwindow->get_edl())
578 mwindow->gui->lock_window("VWindowEditing::splice_selection");
579 mwindow->splice(vwindow->get_edl(), subwindow->shift_down());
580 mwindow->gui->unlock_window();
584 void VWindowEditing::overwrite_selection()
586 if(vwindow->get_edl())
588 mwindow->gui->lock_window("VWindowEditing::overwrite_selection");
589 mwindow->overwrite(vwindow->get_edl(), subwindow->shift_down());
590 mwindow->gui->unlock_window();
594 void VWindowEditing::toggle_label()
596 if(vwindow->get_edl())
598 EDL *edl = vwindow->get_edl();
599 edl->labels->toggle_label(edl->local_session->get_selectionstart(1),
600 edl->local_session->get_selectionend(1));
601 vwindow->gui->timebar->update(1);
605 void VWindowEditing::prev_label()
607 if(vwindow->get_edl())
609 EDL *edl = vwindow->get_edl();
610 vwindow->gui->unlock_window();
611 vwindow->playback_engine->interrupt_playback(1);
612 vwindow->gui->lock_window("VWindowEditing::prev_label");
614 Label *current = edl->labels->prev_label(
615 edl->local_session->get_selectionstart(1));
620 edl->local_session->set_selectionstart(0);
621 edl->local_session->set_selectionend(0);
622 vwindow->update_position(CHANGE_NONE, 0, 1, 0);
623 vwindow->gui->timebar->update(1);
627 edl->local_session->set_selectionstart(current->position);
628 edl->local_session->set_selectionend(current->position);
629 vwindow->update_position(CHANGE_NONE, 0, 1, 0);
630 vwindow->gui->timebar->update(1);
635 void VWindowEditing::next_label()
637 if(vwindow->get_edl())
639 EDL *edl = vwindow->get_edl();
640 Label *current = edl->labels->next_label(
641 edl->local_session->get_selectionstart(1));
644 vwindow->gui->unlock_window();
645 vwindow->playback_engine->interrupt_playback(1);
646 vwindow->gui->lock_window("VWindowEditing::next_label 1");
648 double position = edl->tracks->total_length();
649 edl->local_session->set_selectionstart(position);
650 edl->local_session->set_selectionend(position);
651 vwindow->update_position(CHANGE_NONE, 0, 1, 0);
652 vwindow->gui->timebar->update(1);
656 vwindow->gui->unlock_window();
657 vwindow->playback_engine->interrupt_playback(1);
658 vwindow->gui->lock_window("VWindowEditing::next_label 2");
660 edl->local_session->set_selectionstart(current->position);
661 edl->local_session->set_selectionend(current->position);
662 vwindow->update_position(CHANGE_NONE, 0, 1, 0);
663 vwindow->gui->timebar->update(1);
668 double VWindowEditing::get_position()
670 EDL *edl = vwindow->get_edl();
671 double position = !edl ? 0 : edl->local_session->get_selectionstart(1);
675 void VWindowEditing::set_position(double position)
677 EDL *edl = vwindow->get_edl();
679 if( get_position() != position ) {
680 if( position < 0 ) position = 0;
681 edl->local_session->set_selectionstart(position);
682 edl->local_session->set_selectionend(position);
683 vwindow->update_position(CHANGE_NONE, 0, 1);
687 void VWindowEditing::set_inpoint()
689 vwindow->set_inpoint();
692 void VWindowEditing::set_outpoint()
694 vwindow->set_outpoint();
697 void VWindowEditing::unset_inoutpoint()
699 vwindow->unset_inoutpoint();
703 void VWindowEditing::to_clip()
705 EDL *edl = vwindow->get_edl();
707 mwindow->to_clip(edl, _("viewer window: "), subwindow->shift_down());
710 VWindowSource::VWindowSource(MWindow *mwindow, VWindowGUI *vwindow, int x, int y)
711 : BC_PopupTextBox(vwindow,
719 this->mwindow = mwindow;
720 this->vwindow = vwindow;
723 VWindowSource::~VWindowSource()
727 int VWindowSource::handle_event()
733 VWindowTransport::VWindowTransport(MWindow *mwindow,
737 : PlayTransport(mwindow,
745 EDL* VWindowTransport::get_edl()
747 return gui->vwindow->get_edl();
751 void VWindowTransport::goto_start()
753 gui->unlock_window();
754 handle_transport(REWIND, 1);
755 gui->lock_window("VWindowTransport::goto_start");
756 gui->vwindow->goto_start();
759 void VWindowTransport::goto_end()
761 gui->unlock_window();
762 handle_transport(GOTO_END, 1);
763 gui->lock_window("VWindowTransport::goto_end");
764 gui->vwindow->goto_end();
770 VWindowCanvas::VWindowCanvas(MWindow *mwindow, VWindowGUI *gui)
773 mwindow->theme->vcanvas_x,
774 mwindow->theme->vcanvas_y,
775 mwindow->theme->vcanvas_w,
776 mwindow->theme->vcanvas_h,
779 //printf("VWindowCanvas::VWindowCanvas %d %d\n", __LINE__, mwindow->theme->vcanvas_x);
780 this->mwindow = mwindow;
784 void VWindowCanvas::zoom_resize_window(float percentage)
786 EDL *edl = gui->vwindow->get_edl();
787 if(!edl) edl = mwindow->edl;
789 int canvas_w, canvas_h;
792 // Get required canvas size
793 calculate_sizes(edl->get_aspect_ratio(),
794 edl->session->output_w,
795 edl->session->output_h,
800 // Estimate window size from current borders
801 new_w = canvas_w + (gui->get_w() - mwindow->theme->vcanvas_w);
802 new_h = canvas_h + (gui->get_h() - mwindow->theme->vcanvas_h);
804 mwindow->session->vwindow_w = new_w;
805 mwindow->session->vwindow_h = new_h;
807 mwindow->theme->get_vwindow_sizes(gui);
809 // Estimate again from new borders
810 new_w = canvas_w + (mwindow->session->vwindow_w - mwindow->theme->vcanvas_w);
811 new_h = canvas_h + (mwindow->session->vwindow_h - mwindow->theme->vcanvas_h);
814 gui->resize_window(new_w, new_h);
815 gui->resize_event(new_w, new_h);
818 void VWindowCanvas::close_source()
820 gui->unlock_window();
821 gui->vwindow->playback_engine->interrupt_playback(1);
822 gui->lock_window("VWindowCanvas::close_source");
823 gui->vwindow->delete_source(1, 1);
827 void VWindowCanvas::draw_refresh(int flush)
829 EDL *edl = gui->vwindow->get_edl();
831 if(!get_canvas()->get_video_on()) get_canvas()->clear_box(0, 0, get_canvas()->get_w(), get_canvas()->get_h());
832 if(!get_canvas()->get_video_on() && refresh_frame && edl)
834 float in_x1, in_y1, in_x2, in_y2;
835 float out_x1, out_y1, out_x2, out_y2;
845 get_canvas()->draw_vframe(refresh_frame,
848 (int)(out_x2 - out_x1),
849 (int)(out_y2 - out_y1),
852 (int)(in_x2 - in_x1),
853 (int)(in_y2 - in_y1),
857 if(!get_canvas()->get_video_on())
860 get_canvas()->flash(flush);
864 void VWindowCanvas::draw_overlays()
866 if( gui->highlighted )
868 get_canvas()->set_color(WHITE);
869 get_canvas()->set_inverse();
870 get_canvas()->draw_rectangle(0, 0, get_canvas()->get_w(), get_canvas()->get_h());
871 get_canvas()->draw_rectangle(1, 1, get_canvas()->get_w() - 2, get_canvas()->get_h() - 2);
872 get_canvas()->set_opaque();
876 int VWindowCanvas::get_fullscreen()
878 return mwindow->session->vwindow_fullscreen;
881 void VWindowCanvas::set_fullscreen(int value)
883 mwindow->session->vwindow_fullscreen = value;