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 "edlsession.h"
28 #include "localsession.h"
29 #include "mainclock.h"
30 #include "maincursor.h"
31 #include "mainsession.h"
34 #include "mwindowgui.h"
37 #include "preferences.h"
39 #include "trackcanvas.h"
41 #include "transportque.h"
46 MTimeBar::MTimeBar(MWindow *mwindow,
52 : TimeBar(mwindow, gui, x, y, w, h)
58 MTimeBar::MTimeBar(MWindow *mwindow,
64 : TimeBar(mwindow, mwindow->gui, x, y, w, h)
66 this->gui = mwindow->gui;
70 void MTimeBar::create_objects()
72 gui->add_subwindow(menu = new TimeBarPopup(mwindow));
73 menu->create_objects();
75 TimeBar::create_objects();
79 double MTimeBar::pixel_to_position(int pixel)
81 return (double)pixel *
82 mwindow->edl->local_session->zoom_sample /
83 mwindow->edl->session->sample_rate +
84 (double)mwindow->edl->local_session->view_start[pane->number] *
85 mwindow->edl->local_session->zoom_sample /
86 mwindow->edl->session->sample_rate;
90 int64_t MTimeBar::position_to_pixel(double position)
92 return (int64_t)(position *
93 mwindow->edl->session->sample_rate /
94 mwindow->edl->local_session->zoom_sample -
95 mwindow->edl->local_session->view_start[pane->number]);
99 void MTimeBar::stop_transport()
101 gui->stop_transport("MTimeBar::stop_transport");
104 #define TEXT_MARGIN xS(4)
105 #define TICK_SPACING xS(5)
106 #define LINE_MARGIN yS(3)
107 #define TICK_MARGIN yS(16)
109 void MTimeBar::draw_time()
112 char string[BCTEXTLEN];
113 int sample_rate = mwindow->edl->session->sample_rate;
114 double frame_rate = mwindow->edl->session->frame_rate;
115 // Seconds between text markings
116 double text_interval = 3600.0;
117 // Seconds between tick marks
118 double tick_interval = 3600.0;
121 // Calculate tick mark spacing, number spacing, and starting point based
122 // on zoom, time format, project settings.
124 // If the time format is for audio, mark round numbers of samples based on
126 // Fow low zoom, mark tens of samples.
128 // If the time format is for video, mark round number of frames based on
130 // For low zoom, mark individual frames.
132 //int64_t windowspan = mwindow->edl->local_session->zoom_sample * get_w();
140 // Number of seconds per pixel
141 double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
143 // Seconds in each frame
144 double frame_seconds = (double)1.0 / frame_rate;
145 // Starting time of view in seconds.
146 double view_start = mwindow->edl->local_session->view_start[pane->number] *
148 // Ending time of view in seconds
149 double view_end = (double)(mwindow->edl->local_session->view_start[pane->number] +
150 get_w()) * time_per_pixel;
151 // Get minimum distance between text marks
152 int min_pixels1 = get_text_width(MEDIUMFONT,
153 Units::totext(string,
155 mwindow->edl->session->time_format,
157 mwindow->edl->session->frame_rate,
158 mwindow->edl->session->frames_per_foot)) + TEXT_MARGIN;
159 int min_pixels2 = get_text_width(MEDIUMFONT,
160 Units::totext(string,
162 mwindow->edl->session->time_format,
164 mwindow->edl->session->frame_rate,
165 mwindow->edl->session->frames_per_foot)) + TEXT_MARGIN;
166 int min_pixels = (int)MAX(min_pixels1, min_pixels2);
169 // Minimum seconds between text marks
170 double min_time = (double)min_pixels *
171 mwindow->edl->local_session->zoom_sample /
175 // Get first text mark on or before window start
176 int64_t starting_mark = 0;
180 // Default text spacing
182 double prev_text_interval = 1.0;
184 while(text_interval >= min_time)
186 prev_text_interval = text_interval;
201 text_interval /= 2.5;
206 text_interval = prev_text_interval;
242 text_interval = 1200;
245 text_interval = 1800;
248 text_interval = 3600;
251 switch(mwindow->edl->session->time_format)
253 case TIME_FEET_FRAMES:
255 double foot_seconds = frame_seconds * mwindow->edl->session->frames_per_foot;
256 if(frame_seconds >= min_time)
257 text_interval = frame_seconds;
259 if(foot_seconds / 8.0 > min_time)
260 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 8.0;
262 if(foot_seconds / 4.0 > min_time)
263 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 4.0;
265 if(foot_seconds / 2.0 > min_time)
266 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 2.0;
268 if(foot_seconds > min_time)
269 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot;
271 if(foot_seconds * 2 >= min_time)
272 text_interval = foot_seconds * 2;
274 if(foot_seconds * 5 >= min_time)
275 text_interval = foot_seconds * 5;
279 for(int factor = 10, progression = 0; factor <= 100000; )
281 if(foot_seconds * factor >= min_time)
283 text_interval = foot_seconds * factor;
289 factor = (int)(factor * 2.5);
295 factor = (int)(factor * 2);
301 factor = (int)(factor * 2);
313 // One frame per text mark
314 if(frame_seconds >= min_time)
315 text_interval = frame_seconds;
317 if(frame_seconds * 2 >= min_time)
318 text_interval = frame_seconds * 2;
320 if(frame_seconds * 5 >= min_time)
321 text_interval = frame_seconds * 5;
325 for(int factor = 10, progression = 0; factor <= 100000; )
327 if(frame_seconds * factor >= min_time)
329 text_interval = frame_seconds * factor;
335 factor = (int)(factor * 2.5);
341 factor = (int)(factor * 2);
347 factor = (int)(factor * 2);
360 while(text_interval < min_time)
366 tick_interval = text_interval;
367 double timecode_offset = 0;
369 switch(mwindow->edl->session->time_format)
372 timecode_offset = mwindow->get_timecode_offset(); // fall thru
373 case TIME_FEET_FRAMES:
376 if(frame_seconds / time_per_pixel > TICK_SPACING)
377 tick_interval = frame_seconds;
381 // Get first text mark on or before window start
382 starting_mark = (int64_t)((double)mwindow->edl->local_session->view_start[pane->number] *
383 time_per_pixel / text_interval);
385 double start_position = (double)starting_mark * text_interval;
386 int64_t iteration = 0;
389 //printf("text_interval=%f\n", text_interval);
390 while(start_position + text_interval * iteration < view_end)
392 double position1 = start_position + text_interval * iteration;
393 int pixel = (int64_t)(position1 / time_per_pixel) -
394 mwindow->edl->local_session->view_start[pane->number];
397 Units::totext(string,
399 mwindow->edl->session->time_format,
401 mwindow->edl->session->frame_rate,
402 mwindow->edl->session->frames_per_foot,
404 set_color(get_resources()->default_text_color);
405 set_font(MEDIUMFONT);
407 draw_text(pixel + TEXT_MARGIN, get_text_ascent(MEDIUMFONT), string);
408 draw_line(pixel, LINE_MARGIN, pixel, get_h() - yS(2));
410 double position2 = start_position + text_interval * (iteration + 1);
411 int pixel2 = (int64_t)(position2 / time_per_pixel) -
412 mwindow->edl->local_session->view_start[pane->number];
414 for(double tick_position = position1;
415 tick_position < position2;
416 tick_position += tick_interval)
418 pixel = (int64_t)(tick_position / time_per_pixel) -
419 mwindow->edl->local_session->view_start[pane->number];
420 if(labs(pixel - pixel1) > 1 &&
421 labs(pixel - pixel2) > 1)
422 draw_line(pixel, TICK_MARGIN, pixel, get_h() - yS(2));
430 void MTimeBar::draw_range()
433 if( mwindow->brender_active && mwindow->preferences->use_brender &&
434 mwindow->edl->tracks->total_playable_vtracks() ) {
435 double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
436 mwindow->edl->session->sample_rate;
437 x1 = (int)(mwindow->edl->session->brender_start / time_per_pixel) -
438 mwindow->edl->local_session->view_start[pane->number];
439 x2 = (int)(mwindow->session->brender_end / time_per_pixel) -
440 mwindow->edl->local_session->view_start[pane->number];
443 if(x2 > x1 && x1 < get_w() && x2 > 0) {
444 draw_top_background(get_parent(), 0, 0, x1, get_h());
445 draw_3segmenth(x1, 0, x2 - x1, mwindow->theme->get_image("timebar_brender"));
446 draw_top_background(get_parent(), x2, 0, get_w() - x2, get_h());
449 draw_top_background(get_parent(), 0, 0, get_w(), get_h());
452 // int64_t pixel = position_to_pixel(
453 // mwindow->edl->local_session->get_selectionstart(1));
455 // set_color(mwindow->theme->timebar_cursor_colorg);
456 // draw_line(pixel, 0, pixel, get_h());
457 //printf("MTimeBar::draw_range %f %f\n", mwindow->session->brender_end, time_per_pixel);
460 void MTimeBar::select_label(double position)
464 EDL *edl = mwindow->edl;
465 position = edl->align_to_frame(position, 1);
469 if(position > edl->local_session->get_selectionend(1) / 2 +
470 edl->local_session->get_selectionstart(1) / 2)
473 edl->local_session->set_selectionend(position);
477 edl->local_session->set_selectionstart(position);
482 edl->local_session->set_selectionstart(position);
483 edl->local_session->set_selectionend(position);
487 mwindow->cwindow->update(1, 0, 0, 0, 1);
491 gui->flash_canvas(1);
492 gui->zoombar->update();
498 int MTimeBar::resize_event()
500 reposition_window(mwindow->theme->mtimebar_x,
501 mwindow->theme->mtimebar_y,
502 mwindow->theme->mtimebar_w,
503 mwindow->theme->mtimebar_h);
508 int MTimeBar::resize_event(int x, int y, int w, int h)
518 // int MTimeBar::test_preview(int buttonpress)
526 void MTimeBar::handle_mwindow_drag()
528 //printf("TimeBar::cursor_motion_event %d %d\n", __LINE__, current_operation);
529 int relative_cursor_x = pane->canvas->get_relative_cursor_x();
530 if(relative_cursor_x >= pane->canvas->get_w() ||
531 relative_cursor_x < 0)
533 pane->canvas->start_dragscroll();
536 if(relative_cursor_x < pane->canvas->get_w() &&
537 relative_cursor_x >= 0)
539 pane->canvas->stop_dragscroll();
545 void MTimeBar::update(int flush)
547 TimeBar::update(flush);
550 void MTimeBar::update_clock(double position)
552 if(!mwindow->cwindow->playback_engine->is_playing_back)
553 gui->mainclock->update(position);
556 void MTimeBar::update_cursor()
558 int rx = get_relative_cursor_x();
559 double position = pixel_to_position(rx);
561 position = mwindow->edl->align_to_frame(position, 0);
562 position = MAX(0, position);
564 mwindow->select_point(position);
568 double MTimeBar::test_highlight()
570 // Don't crash during initialization
573 if(mwindow->session->current_operation == NO_OPERATION)
575 if(pane->canvas->is_event_win() &&
576 pane->canvas->cursor_inside())
578 int cursor_x = pane->canvas->get_cursor_x();
579 double position = (double)cursor_x *
580 (double)mwindow->edl->local_session->zoom_sample /
581 (double)mwindow->edl->session->sample_rate +
582 (double)mwindow->edl->local_session->view_start[pane->number] *
583 (double)mwindow->edl->local_session->zoom_sample /
584 (double)mwindow->edl->session->sample_rate;
585 pane->canvas->timebar_position = mwindow->edl->align_to_frame(position, 0);
588 //printf("MTimeBar::test_highlight %d %d %f\n", __LINE__, pane->canvas->cursor_inside(), pane->canvas->timebar_position);
589 return pane->canvas->timebar_position;
592 if(mwindow->session->current_operation == SELECT_REGION ||
593 mwindow->session->current_operation == DRAG_EDITHANDLE2)
595 //printf("MTimeBar::test_highlight %d %f\n", __LINE__, mwindow->gui->canvas->timebar_position);
596 return pane->canvas->timebar_position;
607 int MTimeBar::repeat_event(int64_t duration)
609 if(!pane->canvas->drag_scroll) return 0;
610 if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
614 int relative_cursor_x = pane->canvas->get_relative_cursor_x();
615 if(current_operation == TIMEBAR_DRAG)
617 if(relative_cursor_x >= pane->canvas->get_w())
619 distance = relative_cursor_x - pane->canvas->get_w();
623 if(relative_cursor_x < 0)
625 distance = relative_cursor_x;
634 mwindow->samplemovement(
635 mwindow->edl->local_session->view_start[pane->number] + distance,
644 int MTimeBar::button_press_event()
648 if(is_event_win() && cursor_above() && get_buttonpress() == 3)
651 menu->activate_menu();
655 if(!result) return TimeBar::button_press_event();
660 void MTimeBar::activate_timeline()
666 TimeBarPopupItem::TimeBarPopupItem(MWindow *mwindow,
672 this->mwindow = mwindow;
677 int TimeBarPopupItem::handle_event()
679 mwindow->edl->session->time_format = value;
680 mwindow->gui->update(0, NO_DRAW, 1, 0, 0, 1, 0);
681 mwindow->gui->redraw_time_dependancies();
687 TimeBarPopup::TimeBarPopup(MWindow *mwindow)
688 : BC_PopupMenu(0, 0, 0, "", 0)
690 this->mwindow = mwindow;
693 TimeBarPopup::~TimeBarPopup()
698 void TimeBarPopup::create_objects()
700 add_item(items[0] = new TimeBarPopupItem(mwindow,
701 this, TIME_HMS_TEXT, TIME_HMS));
702 add_item(items[1] = new TimeBarPopupItem(mwindow,
703 this, TIME_HMSF_TEXT, TIME_HMSF));
704 add_item(items[2] = new TimeBarPopupItem(mwindow,
705 this, TIME_TIMECODE_TEXT, TIME_TIMECODE));
706 add_item(items[3] = new TimeBarPopupItem(mwindow,
707 this, TIME_FRAMES_TEXT, TIME_FRAMES));
708 add_item(items[4] = new TimeBarPopupItem(mwindow,
709 this, TIME_SAMPLES_TEXT, TIME_SAMPLES));
710 add_item(items[5] = new TimeBarPopupItem(mwindow,
711 this, TIME_SAMPLES_HEX_TEXT, TIME_SAMPLES_HEX));
712 add_item(items[6] = new TimeBarPopupItem(mwindow,
713 this, TIME_SECONDS_TEXT, TIME_SECONDS));
714 add_item(items[7] = new TimeBarPopupItem(mwindow,
715 this, TIME_FEET_FRAMES_TEXT, TIME_FEET_FRAMES));
718 void TimeBarPopup::update()
720 int time_format = mwindow->edl->session->time_format;
721 for( int i=0; i<TOTAL_TIMEFORMATS; ++i ) {
722 items[i]->set_checked(items[i]->value == time_format);