4 * Copyright (C) 1997-2014 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
22 #include "bcsignals.h"
24 #include "cplayback.h"
27 #include "cwindowgui.h"
29 #include "edlsession.h"
33 #include "labeledit.h"
34 #include "localsession.h"
35 #include "maincursor.h"
39 #include "mwindowgui.h"
41 #include "preferences.h"
42 #include "recordlabel.h"
43 #include "localsession.h"
44 #include "mainsession.h"
47 #include "timelinepane.h"
48 #include "trackcanvas.h"
50 #include "transportque.h"
54 #include "vwindowgui.h"
58 LabelGUI::LabelGUI(MWindow *mwindow, TimeBar *timebar,
60 double position, VFrame **data)
61 : BC_Toggle(translate_pixel(mwindow, pixel), y,
62 data ? data : mwindow->theme->label_toggle, 0)
64 this->mwindow = mwindow;
65 this->timebar = timebar;
68 this->position = position;
76 int LabelGUI::get_y(MWindow *mwindow, TimeBar *timebar)
78 return timebar->get_h() -
79 mwindow->theme->label_toggle[0]->get_h();
82 int LabelGUI::translate_pixel(MWindow *mwindow, int pixel)
84 int result = pixel - mwindow->theme->label_toggle[0]->get_w() / 2;
88 void LabelGUI::reposition(int flush)
90 reposition_window(translate_pixel(mwindow, pixel),
94 int LabelGUI::button_press_event()
98 if( this->is_event_win() && get_buttonpress() == 3 ) {
101 get_abs_cursor(cur_x, cur_y, 0);
102 timebar->label_edit->start(label, cur_x, cur_y);
106 result = BC_Toggle::button_press_event();
109 set_tooltip(this->label->textstr);
113 int LabelGUI::handle_event()
115 timebar->select_label(position);
120 InPointGUI::InPointGUI(MWindow *mwindow, TimeBar *timebar,
121 int64_t pixel, double position)
122 : LabelGUI(mwindow, timebar,
123 pixel, get_y(mwindow, timebar),
124 position, mwindow->theme->in_point)
126 //printf("InPointGUI::InPointGUI %d %d\n", pixel, get_y(mwindow, timebar));
128 InPointGUI::~InPointGUI()
131 int InPointGUI::get_y(MWindow *mwindow, TimeBar *timebar)
134 result = timebar->get_h() -
135 mwindow->theme->in_point[0]->get_h();
140 OutPointGUI::OutPointGUI(MWindow *mwindow, TimeBar *timebar,
141 int64_t pixel, double position)
142 : LabelGUI(mwindow, timebar,
143 pixel, get_y(mwindow, timebar),
144 position, mwindow->theme->out_point)
146 //printf("OutPointGUI::OutPointGUI %d %d\n", pixel, get_y(mwindow, timebar));
148 OutPointGUI::~OutPointGUI()
151 int OutPointGUI::get_y(MWindow *mwindow, TimeBar *timebar)
153 return timebar->get_h() -
154 mwindow->theme->out_point[0]->get_h();
158 PresentationGUI::PresentationGUI(MWindow *mwindow, TimeBar *timebar,
159 int64_t pixel, double position)
160 : LabelGUI(mwindow, timebar, pixel, get_y(mwindow, timebar), position)
163 PresentationGUI::~PresentationGUI()
167 TimeBar::TimeBar(MWindow *mwindow, BC_WindowBase *gui,
168 int x, int y, int w, int h)
169 : BC_SubWindow(x, y, w, h)
171 //printf("TimeBar::TimeBar %d %d %d %d\n", x, y, w, h);
173 this->mwindow = mwindow;
174 label_edit = new LabelEdit(mwindow, mwindow->awindow, 0);
184 labels.remove_all_objects();
185 presentations.remove_all_objects();
188 void TimeBar::create_objects()
192 //printf("TimeBar::create_objects %d\n", __LINE__);
193 current_operation = TIMEBAR_NONE;
194 set_cursor(UPRIGHT_ARROW_CURSOR, 0, 0);
199 int64_t TimeBar::position_to_pixel(double position)
202 return (int64_t)(position / time_per_pixel);
206 double TimeBar::pixel_to_position(int pixel)
209 pixel += get_edl()->local_session->view_start[pane->number];
212 return (double)pixel *
213 get_edl()->local_session->zoom_sample /
214 get_edl()->session->sample_rate;
217 void TimeBar::update_labels()
220 EDL *edl = get_edl();
223 for( Label *current=edl->labels->first; current; current=NEXT ) {
224 int64_t pixel = position_to_pixel(current->position);
225 if( pixel >= 0 && pixel < get_w() ) {
227 if( output >= labels.total ) {
229 add_subwindow(new_label =
230 new LabelGUI(mwindow,
233 LabelGUI::get_y(mwindow, this),
235 new_label->set_cursor(INHERIT_CURSOR, 0, 0);
236 new_label->set_tooltip(current->textstr);
237 new_label->label = current;
238 labels.append(new_label);
241 // Reposition old label
243 LabelGUI *gui = labels.values[output];
244 if( gui->pixel != pixel ) {
252 labels.values[output]->position = current->position;
253 labels.values[output]->set_tooltip(current->textstr);
254 labels.values[output]->label = current;
257 if( edl->local_session->get_selectionstart(1) <= current->position &&
258 edl->local_session->get_selectionend(1) >= current->position )
259 labels.values[output]->update(1);
261 if( labels.values[output]->get_value() )
262 labels.values[output]->update(0);
269 // Delete excess labels
270 while(labels.total > output)
272 labels.remove_object();
275 // Get the labels to show
279 void TimeBar::update_highlights()
281 EDL *edl = get_edl();
283 for( int i = 0; i < labels.total; i++ ) {
284 LabelGUI *label = labels.values[i];
285 if( edl->equivalent(label->position,
286 edl->local_session->get_selectionstart(1)) ||
287 edl->equivalent(label->position,
288 edl->local_session->get_selectionend(1)) ) {
289 if( !label->get_value() ) label->update(1);
292 if( label->get_value() ) label->update(0);
295 if( edl->equivalent(edl->local_session->get_inpoint(),
296 edl->local_session->get_selectionstart(1)) ||
297 edl->equivalent(edl->local_session->get_inpoint(),
298 edl->local_session->get_selectionend(1)) ) {
299 if( in_point ) in_point->update(1);
302 if( in_point ) in_point->update(0);
304 if( edl->equivalent(edl->local_session->get_outpoint(),
305 edl->local_session->get_selectionstart(1)) ||
306 edl->equivalent(edl->local_session->get_outpoint(),
307 edl->local_session->get_selectionend(1)) ) {
308 if( out_point ) out_point->update(1);
311 if( out_point ) out_point->update(0);
313 draw_inout_highlight();
316 void TimeBar::draw_inout_highlight()
318 EDL *edl = get_edl();
319 if( !edl->local_session->inpoint_valid() ) return;
320 if( !edl->local_session->outpoint_valid() ) return;
321 double in_position = edl->local_session->get_inpoint();
322 double out_position = edl->local_session->get_outpoint();
323 if( in_position >= out_position ) return;
324 int in_x = position_to_pixel(in_position);
325 int out_x = position_to_pixel(out_position);
326 CLAMP(in_x, 0, get_w());
327 CLAMP(out_x, 0, get_w());
332 draw_line(in_x, get_h()-2*lw, out_x, get_h()-2*lw);
337 void TimeBar::update_points()
339 EDL *edl = get_edl();
340 int64_t pixel = !edl ? 0 :
341 position_to_pixel(edl->local_session->get_inpoint());
344 if( edl && edl->local_session->inpoint_valid() &&
345 pixel >= 0 && pixel < get_w() ) {
346 if( !EQUIV(edl->local_session->get_inpoint(), in_point->position) ||
347 in_point->pixel != pixel ) {
348 in_point->pixel = pixel;
349 in_point->position = edl->local_session->get_inpoint();
350 in_point->reposition(0);
353 in_point->draw_face(1, 0);
362 if( edl && edl->local_session->inpoint_valid() &&
363 pixel >= 0 && pixel < get_w() ) {
364 add_subwindow(in_point = new InPointGUI(mwindow,
365 this, pixel, edl->local_session->get_inpoint()));
366 in_point->set_cursor(ARROW_CURSOR, 0, 0);
370 position_to_pixel(edl->local_session->get_outpoint());
373 if( edl && edl->local_session->outpoint_valid() &&
374 pixel >= 0 && pixel < get_w()) {
375 if( !EQUIV(edl->local_session->get_outpoint(), out_point->position) ||
376 out_point->pixel != pixel ) {
377 out_point->pixel = pixel;
378 out_point->position = edl->local_session->get_outpoint();
379 out_point->reposition(0);
382 out_point->draw_face(1, 0);
391 if( edl && edl->local_session->outpoint_valid() &&
392 pixel >= 0 && pixel < get_w() ) {
393 add_subwindow(out_point = new OutPointGUI(mwindow,
394 this, pixel, edl->local_session->get_outpoint()));
395 out_point->set_cursor(ARROW_CURSOR, 0, 0);
401 void TimeBar::update_clock(double position)
405 void TimeBar::update(int flush)
408 // Need to redo these when range is drawn to get the background updated.
413 EDL *edl = get_edl();
415 int x = get_relative_cursor_x();
416 // Draw highlight position
417 if( edl && (highlighted || current_operation == TIMEBAR_DRAG) &&
418 x >= 0 && x < get_w() ) {
419 //printf("TimeBar::update %d %d\n", __LINE__, x);
420 double position = pixel_to_position(x);
422 position = mwindow->edl->align_to_frame(position, 0);
423 pixel = position_to_pixel(position);
424 update_clock(position);
428 double position = test_highlight();
429 if( position >= 0 ) pixel = position_to_pixel(position);
433 if( pixel >= 0 && pixel < get_w() ) {
434 set_color(mwindow->theme->timebar_cursor_color);
436 //printf("TimeBar::update %d pane=%d pixel=%jd\n", __LINE__, pane->number, pixel);
437 draw_line(pixel, 0, pixel, get_h());
443 double playback_start = edl->local_session->playback_start;
444 if( playback_start >= 0 ) {
445 int64_t pixel = position_to_pixel(playback_start);
446 set_color(mwindow->theme->timebar_cursor_color ^ 0x0000ff);
447 draw_line(pixel, 0, pixel, get_h());
448 double playback_end = edl->local_session->playback_end;
449 if( playback_end > playback_start ) {
450 pixel = position_to_pixel(playback_end);
451 set_color(mwindow->theme->timebar_cursor_color ^ 0x00ff00);
452 draw_line(pixel, 0, pixel, get_h());
456 double position = edl->local_session->get_selectionstart(1);
457 int64_t pixel = position_to_pixel(position);
458 // Draw insertion point position.
459 int color = mwindow->theme->timebar_cursor_color;
460 if( mwindow->preferences->forward_render_displacement )
463 draw_line(pixel, 0, pixel, get_h());
468 // Get the labels to show
471 //printf("TimeBar::update %d this=%p %d\n", __LINE__, this, current_operation);
476 int TimeBar::delete_project()
478 // labels->delete_all();
482 int TimeBar::save(FileXML *xml)
484 // labels->save(xml);
491 void TimeBar::draw_time()
495 EDL* TimeBar::get_edl()
502 void TimeBar::draw_range()
506 //printf("TimeBar::draw_range %d %p\n", __LINE__, get_edl());
507 if( has_preview() && get_edl() ) {
509 get_preview_pixels(x1, x2);
511 //printf("TimeBar::draw_range %f %d %d\n", edl_length, x1, x2);
512 draw_3segmenth(0, 0, x1, mwindow->theme->timebar_view_data);
513 draw_top_background(get_parent(), x1, 0, x2 - x1, get_h());
514 draw_3segmenth(x2, 0, get_w() - x2, mwindow->theme->timebar_view_data);
517 draw_line(x1, 0, x1, get_h());
518 draw_line(x2, 0, x2, get_h());
521 EDL *edl = get_edl();
523 int64_t pixel = position_to_pixel(
524 edl->local_session->get_selectionstart(1));
525 // Draw insertion point position if this timebar belongs to a window which
526 // has something other than the master EDL.
527 set_color(mwindow->theme->timebar_cursor_color);
528 draw_line(pixel, 0, pixel, get_h());
532 draw_top_background(get_parent(), 0, 0, get_w(), get_h());
535 void TimeBar::select_label(double position)
546 double TimeBar::get_edl_length()
548 edl_length = get_edl() ? get_edl()->tracks->total_length() : 0;
550 time_per_pixel = !EQUIV(edl_length, 0) ? edl_length/w1 : w1;
554 int TimeBar::get_preview_pixels(int &x1, int &x2)
556 x1 = 0; x2 = get_w();
558 EDL *edl = get_edl();
559 if( edl && !EQUIV(edl_length, 0) ) {
560 double preview_start = edl->local_session->preview_start;
561 double preview_end = edl->local_session->preview_end;
562 if( preview_end < 0 || preview_end > edl_length )
563 preview_end = edl_length;
564 if( preview_end >= preview_start ) {
565 x1 = (int)(preview_start / time_per_pixel);
566 x2 = (int)(preview_end / time_per_pixel);
573 int TimeBar::test_preview(int buttonpress)
578 if( get_edl() && cursor_inside() && buttonpress >= 0 ) {
579 int x1, x2, x = get_relative_cursor_x();
580 get_preview_pixels(x1, x2);
581 //printf("TimeBar::test_preview %d %d %d\n", x1, x2, x);
582 // Inside left handle
583 if( x >= x1 - HANDLE_W && x < x1 + HANDLE_W &&
584 // Ignore left handle if both handles are up against the left side
587 current_operation = TIMEBAR_DRAG_LEFT;
588 start_position = get_edl()->local_session->preview_start;
591 else if( get_cursor() != LEFT_CURSOR )
592 set_cursor(LEFT_CURSOR, 0, 1);
595 // Inside right handle
596 else if( x >= x2 - HANDLE_W && x < x2 + HANDLE_W &&
597 // Ignore right handle if both handles are up against the right side
598 x1 < get_w() - HANDLE_W ) {
600 current_operation = TIMEBAR_DRAG_RIGHT;
601 start_position = get_edl()->local_session->preview_end;
602 if( start_position < 0 || start_position > edl_length )
603 start_position = edl_length;
606 else if( get_cursor() != RIGHT_CURSOR )
607 set_cursor(RIGHT_CURSOR, 0, 1);
611 else if( get_button_down() && get_buttonpress() == 3 &&
612 x >= x1 && x < x2 ) {
614 current_operation = TIMEBAR_DRAG_CENTER;
615 starting_start_position = get_edl()->local_session->preview_start;
616 starting_end_position = get_edl()->local_session->preview_end;
617 if( starting_end_position < 0 || starting_end_position > edl_length )
618 starting_end_position = edl_length;
621 if( get_cursor() != HSEPARATE_CURSOR )
622 set_cursor(HSEPARATE_CURSOR, 0, 1);
627 if( !result && get_cursor() != ARROW_CURSOR )
628 set_cursor(ARROW_CURSOR, 0, 1);
634 int TimeBar::move_preview(int &redraw)
636 int result = 0, x = get_relative_cursor_x();
637 switch( current_operation ) {
638 case TIMEBAR_DRAG_LEFT: {
639 get_edl()->local_session->preview_start =
640 start_position + time_per_pixel * (x - start_cursor_x);
641 double preview_end = get_edl()->local_session->preview_end;
642 if( preview_end < 0 || preview_end > edl_length )
643 preview_end = get_edl()->local_session->preview_end = edl_length;
644 CLAMP(get_edl()->local_session->preview_start, 0, preview_end);
647 case TIMEBAR_DRAG_RIGHT: {
648 double preview_end = get_edl()->local_session->preview_end =
649 start_position + time_per_pixel * (x - start_cursor_x);
650 double preview_start = get_edl()->local_session->preview_start;
651 if( preview_end >= edl_length && !preview_start ) {
652 get_edl()->local_session->preview_end = -1;
653 if( preview_start > preview_end )
654 preview_start = get_edl()->local_session->preview_start = preview_end;
657 CLAMP(get_edl()->local_session->preview_end, preview_start, edl_length);
660 case TIMEBAR_DRAG_CENTER: {
661 double dt = time_per_pixel * (x - start_cursor_x);
662 get_edl()->local_session->preview_start = starting_start_position + dt;
663 get_edl()->local_session->preview_end = starting_end_position + dt;
664 if( get_edl()->local_session->preview_start < 0 ) {
665 get_edl()->local_session->preview_end -= get_edl()->local_session->preview_start;
666 get_edl()->local_session->preview_start = 0;
669 if( get_edl()->local_session->preview_end > edl_length ) {
670 get_edl()->local_session->preview_start -= get_edl()->local_session->preview_end - edl_length;
671 get_edl()->local_session->preview_end = edl_length;
677 //printf("TimeBar::move_preview %d %d\n", __LINE__, current_operation);
683 //printf("TimeBar::move_preview %d %d\n", __LINE__, current_operation);
688 void TimeBar::update_preview()
692 int TimeBar::samplemovement()
697 void TimeBar::stop_playback()
701 int TimeBar::button_press_event()
704 if( is_event_win() && cursor_above() ) {
705 if( has_preview() && get_buttonpress() == 3 ) {
706 result = test_preview(1);
708 // Change time format
709 else if( ctrl_down() ) {
710 if( get_buttonpress() == 1 )
711 mwindow->next_time_format();
713 if( get_buttonpress() == 2 )
714 mwindow->prev_time_format();
717 else if( get_buttonpress() == 1 ) {
720 // Select region between two labels
721 if( !is_vwindow() && get_double_click() ) {
722 int x = get_relative_cursor_x();
723 double position = pixel_to_position(x);
725 select_region(position);
729 // Reposition highlight cursor
731 current_operation = TIMEBAR_DRAG;
740 void TimeBar::activate_timeline()
742 mwindow->gui->activate_timeline();
745 int TimeBar::cursor_motion_event()
750 //printf("TimeBar::cursor_motion_event %d %p %d\n", __LINE__, this, current_operation);
751 switch( current_operation )
756 handle_mwindow_drag();
758 //printf("TimeBar::cursor_motion_event %d %d\n", __LINE__, current_operation);
763 case TIMEBAR_DRAG_LEFT:
764 case TIMEBAR_DRAG_RIGHT:
765 case TIMEBAR_DRAG_CENTER:
767 result = move_preview(redraw);
771 if( cursor_above() ) {
776 //printf("TimeBar::cursor_motion_event 20\n");
778 result = test_preview(0);
779 //printf("TimeBar::cursor_motion_event 30\n");
784 //printf("TimeBar::cursor_motion_event %d %d\n", __LINE__, current_operation);
788 //printf("TimeBar::cursor_motion_event %d %p %d\n", __LINE__, this, current_operation);
793 int TimeBar::cursor_leave_event()
802 int TimeBar::button_release_event()
804 //printf("TimeBar::button_release_event %d %d\n", __LINE__, current_operation);
807 switch( current_operation )
810 mwindow->gui->get_focused_pane()->canvas->stop_dragscroll();
811 current_operation = TIMEBAR_NONE;
817 if( current_operation != TIMEBAR_NONE ) {
818 current_operation = TIMEBAR_NONE;
824 if( (!cursor_above() && highlighted) || need_redraw ) {
832 // Update the selection cursor during a dragging operation
833 void TimeBar::update_cursor()
837 void TimeBar::handle_mwindow_drag()
841 int TimeBar::select_region(double position)
843 Label *start = 0, *end = 0, *current;
844 for( current = get_edl()->labels->first; current; current = NEXT ) {
845 if( current->position > position ) {
851 for( current = get_edl()->labels->last ; current; current = PREVIOUS ) {
852 if( current->position <= position ) {
861 get_edl()->local_session->set_selectionstart(0);
863 get_edl()->local_session->set_selectionstart(start->position);
866 get_edl()->local_session->set_selectionend(get_edl()->tracks->total_length());
868 get_edl()->local_session->set_selectionend(end->position);
872 get_edl()->local_session->set_selectionstart(start->position);
873 get_edl()->local_session->set_selectionend(start->position);
877 mwindow->cwindow->gui->lock_window("TimeBar::select_region");
878 mwindow->cwindow->update(1, 0, 0);
879 mwindow->cwindow->gui->unlock_window();
880 mwindow->gui->lock_window("TimeBar::select_region");
881 mwindow->gui->hide_cursor(0);
882 mwindow->gui->draw_cursor(1);
883 mwindow->gui->flash_canvas(0);
884 mwindow->gui->activate_timeline();
885 mwindow->gui->zoombar->update();
886 mwindow->gui->unlock_window();
894 int TimeBar::delete_arrows()
899 double TimeBar::test_highlight()