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 4
105 #define TICK_SPACING 5
106 #define LINE_MARGIN 3
107 #define TICK_MARGIN 16
108 void MTimeBar::draw_time()
111 char string[BCTEXTLEN];
112 int sample_rate = mwindow->edl->session->sample_rate;
113 double frame_rate = mwindow->edl->session->frame_rate;
114 // Seconds between text markings
115 double text_interval = 3600.0;
116 // Seconds between tick marks
117 double tick_interval = 3600.0;
120 // Calculate tick mark spacing, number spacing, and starting point based
121 // on zoom, time format, project settings.
123 // If the time format is for audio, mark round numbers of samples based on
125 // Fow low zoom, mark tens of samples.
127 // If the time format is for video, mark round number of frames based on
129 // For low zoom, mark individual frames.
131 //int64_t windowspan = mwindow->edl->local_session->zoom_sample * get_w();
139 // Number of seconds per pixel
140 double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
142 // Seconds in each frame
143 double frame_seconds = (double)1.0 / frame_rate;
144 // Starting time of view in seconds.
145 double view_start = mwindow->edl->local_session->view_start[pane->number] *
147 // Ending time of view in seconds
148 double view_end = (double)(mwindow->edl->local_session->view_start[pane->number] +
149 get_w()) * time_per_pixel;
150 // Get minimum distance between text marks
151 int min_pixels1 = get_text_width(MEDIUMFONT,
152 Units::totext(string,
154 mwindow->edl->session->time_format,
156 mwindow->edl->session->frame_rate,
157 mwindow->edl->session->frames_per_foot)) + TEXT_MARGIN;
158 int min_pixels2 = get_text_width(MEDIUMFONT,
159 Units::totext(string,
161 mwindow->edl->session->time_format,
163 mwindow->edl->session->frame_rate,
164 mwindow->edl->session->frames_per_foot)) + TEXT_MARGIN;
165 int min_pixels = (int)MAX(min_pixels1, min_pixels2);
168 // Minimum seconds between text marks
169 double min_time = (double)min_pixels *
170 mwindow->edl->local_session->zoom_sample /
174 // Get first text mark on or before window start
175 int64_t starting_mark = 0;
179 // Default text spacing
181 double prev_text_interval = 1.0;
183 while(text_interval >= min_time)
185 prev_text_interval = text_interval;
200 text_interval /= 2.5;
205 text_interval = prev_text_interval;
241 text_interval = 1200;
244 text_interval = 1800;
247 text_interval = 3600;
250 switch(mwindow->edl->session->time_format)
252 case TIME_FEET_FRAMES:
254 double foot_seconds = frame_seconds * mwindow->edl->session->frames_per_foot;
255 if(frame_seconds >= min_time)
256 text_interval = frame_seconds;
258 if(foot_seconds / 8.0 > min_time)
259 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 8.0;
261 if(foot_seconds / 4.0 > min_time)
262 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 4.0;
264 if(foot_seconds / 2.0 > min_time)
265 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 2.0;
267 if(foot_seconds > min_time)
268 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot;
270 if(foot_seconds * 2 >= min_time)
271 text_interval = foot_seconds * 2;
273 if(foot_seconds * 5 >= min_time)
274 text_interval = foot_seconds * 5;
278 for(int factor = 10, progression = 0; factor <= 100000; )
280 if(foot_seconds * factor >= min_time)
282 text_interval = foot_seconds * factor;
288 factor = (int)(factor * 2.5);
294 factor = (int)(factor * 2);
300 factor = (int)(factor * 2);
311 // One frame per text mark
312 if(frame_seconds >= min_time)
313 text_interval = frame_seconds;
315 if(frame_seconds * 2 >= min_time)
316 text_interval = frame_seconds * 2;
318 if(frame_seconds * 5 >= min_time)
319 text_interval = frame_seconds * 5;
323 for(int factor = 10, progression = 0; factor <= 100000; )
325 if(frame_seconds * factor >= min_time)
327 text_interval = frame_seconds * factor;
333 factor = (int)(factor * 2.5);
339 factor = (int)(factor * 2);
345 factor = (int)(factor * 2);
358 while(text_interval < min_time)
364 tick_interval = text_interval;
366 switch(mwindow->edl->session->time_format)
369 case TIME_FEET_FRAMES:
371 if(frame_seconds / time_per_pixel > TICK_SPACING)
372 tick_interval = frame_seconds;
376 // Get first text mark on or before window start
377 starting_mark = (int64_t)((double)mwindow->edl->local_session->view_start[pane->number] *
378 time_per_pixel / text_interval);
380 double start_position = (double)starting_mark * text_interval;
381 int64_t iteration = 0;
384 //printf("text_interval=%f\n", text_interval);
385 while(start_position + text_interval * iteration < view_end)
387 double position1 = start_position + text_interval * iteration;
388 int pixel = (int64_t)(position1 / time_per_pixel) -
389 mwindow->edl->local_session->view_start[pane->number];
392 Units::totext(string,
394 mwindow->edl->session->time_format,
396 mwindow->edl->session->frame_rate,
397 mwindow->edl->session->frames_per_foot);
398 set_color(get_resources()->default_text_color);
399 set_font(MEDIUMFONT);
401 draw_text(pixel + TEXT_MARGIN, get_text_ascent(MEDIUMFONT), string);
402 draw_line(pixel, LINE_MARGIN, pixel, get_h() - 2);
404 double position2 = start_position + text_interval * (iteration + 1);
405 int pixel2 = (int64_t)(position2 / time_per_pixel) -
406 mwindow->edl->local_session->view_start[pane->number];
408 for(double tick_position = position1;
409 tick_position < position2;
410 tick_position += tick_interval)
412 pixel = (int64_t)(tick_position / time_per_pixel) -
413 mwindow->edl->local_session->view_start[pane->number];
414 if(labs(pixel - pixel1) > 1 &&
415 labs(pixel - pixel2) > 1)
416 draw_line(pixel, TICK_MARGIN, pixel, get_h() - 2);
424 void MTimeBar::draw_range()
427 if( mwindow->brender_active && mwindow->preferences->use_brender &&
428 mwindow->edl->tracks->total_playable_vtracks() ) {
429 double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
430 mwindow->edl->session->sample_rate;
431 x1 = (int)(mwindow->edl->session->brender_start / time_per_pixel) -
432 mwindow->edl->local_session->view_start[pane->number];
433 x2 = (int)(mwindow->session->brender_end / time_per_pixel) -
434 mwindow->edl->local_session->view_start[pane->number];
437 if(x2 > x1 && x1 < get_w() && x2 > 0) {
438 draw_top_background(get_parent(), 0, 0, x1, get_h());
439 draw_3segmenth(x1, 0, x2 - x1, mwindow->theme->get_image("timebar_brender"));
440 draw_top_background(get_parent(), x2, 0, get_w() - x2, get_h());
443 draw_top_background(get_parent(), 0, 0, get_w(), get_h());
446 // int64_t pixel = position_to_pixel(
447 // mwindow->edl->local_session->get_selectionstart(1));
449 // set_color(mwindow->theme->timebar_cursor_colorg);
450 // draw_line(pixel, 0, pixel, get_h());
451 //printf("MTimeBar::draw_range %f %f\n", mwindow->session->brender_end, time_per_pixel);
454 void MTimeBar::select_label(double position)
458 EDL *edl = mwindow->edl;
459 position = edl->align_to_frame(position, 1);
463 if(position > edl->local_session->get_selectionend(1) / 2 +
464 edl->local_session->get_selectionstart(1) / 2)
467 edl->local_session->set_selectionend(position);
471 edl->local_session->set_selectionstart(position);
476 edl->local_session->set_selectionstart(position);
477 edl->local_session->set_selectionend(position);
481 mwindow->cwindow->update(1, 0, 0, 0, 1);
485 gui->flash_canvas(1);
486 gui->zoombar->update();
492 int MTimeBar::resize_event()
494 reposition_window(mwindow->theme->mtimebar_x,
495 mwindow->theme->mtimebar_y,
496 mwindow->theme->mtimebar_w,
497 mwindow->theme->mtimebar_h);
502 int MTimeBar::resize_event(int x, int y, int w, int h)
512 // int MTimeBar::test_preview(int buttonpress)
520 void MTimeBar::handle_mwindow_drag()
522 //printf("TimeBar::cursor_motion_event %d %d\n", __LINE__, current_operation);
523 int relative_cursor_x = pane->canvas->get_relative_cursor_x();
524 if(relative_cursor_x >= pane->canvas->get_w() ||
525 relative_cursor_x < 0)
527 pane->canvas->start_dragscroll();
530 if(relative_cursor_x < pane->canvas->get_w() &&
531 relative_cursor_x >= 0)
533 pane->canvas->stop_dragscroll();
539 void MTimeBar::update(int flush)
541 TimeBar::update(flush);
544 void MTimeBar::update_clock(double position)
546 if(!mwindow->cwindow->playback_engine->is_playing_back)
547 gui->mainclock->update(position);
550 void MTimeBar::update_cursor()
552 int rx = get_relative_cursor_x();
553 double position = pixel_to_position(rx);
555 position = mwindow->edl->align_to_frame(position, 0);
556 position = MAX(0, position);
558 mwindow->select_point(position);
562 double MTimeBar::test_highlight()
564 // Don't crash during initialization
567 if(mwindow->session->current_operation == NO_OPERATION)
569 if(pane->canvas->is_event_win() &&
570 pane->canvas->cursor_inside())
572 int cursor_x = pane->canvas->get_cursor_x();
573 double position = (double)cursor_x *
574 (double)mwindow->edl->local_session->zoom_sample /
575 (double)mwindow->edl->session->sample_rate +
576 (double)mwindow->edl->local_session->view_start[pane->number] *
577 (double)mwindow->edl->local_session->zoom_sample /
578 (double)mwindow->edl->session->sample_rate;
579 pane->canvas->timebar_position = mwindow->edl->align_to_frame(position, 0);
582 //printf("MTimeBar::test_highlight %d %d %f\n", __LINE__, pane->canvas->cursor_inside(), pane->canvas->timebar_position);
583 return pane->canvas->timebar_position;
586 if(mwindow->session->current_operation == SELECT_REGION ||
587 mwindow->session->current_operation == DRAG_EDITHANDLE2)
589 //printf("MTimeBar::test_highlight %d %f\n", __LINE__, mwindow->gui->canvas->timebar_position);
590 return pane->canvas->timebar_position;
601 int MTimeBar::repeat_event(int64_t duration)
603 if(!pane->canvas->drag_scroll) return 0;
604 if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
608 int relative_cursor_x = pane->canvas->get_relative_cursor_x();
609 if(current_operation == TIMEBAR_DRAG)
611 if(relative_cursor_x >= pane->canvas->get_w())
613 distance = relative_cursor_x - pane->canvas->get_w();
617 if(relative_cursor_x < 0)
619 distance = relative_cursor_x;
628 mwindow->samplemovement(
629 mwindow->edl->local_session->view_start[pane->number] + distance,
638 int MTimeBar::button_press_event()
642 if(is_event_win() && cursor_above() && get_buttonpress() == 3)
645 menu->activate_menu();
649 if(!result) return TimeBar::button_press_event();
654 void MTimeBar::activate_timeline()
664 TimeBarPopupItem::TimeBarPopupItem(MWindow *mwindow,
670 this->mwindow = mwindow;
675 int TimeBarPopupItem::handle_event()
677 mwindow->edl->session->time_format = value;
678 mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
679 mwindow->gui->redraw_time_dependancies();
685 TimeBarPopup::TimeBarPopup(MWindow *mwindow)
692 this->mwindow = mwindow;
695 TimeBarPopup::~TimeBarPopup()
700 void TimeBarPopup::create_objects()
702 add_item(items[0] = new TimeBarPopupItem(mwindow,
706 add_item(items[1] = new TimeBarPopupItem(mwindow,
710 add_item(items[2] = new TimeBarPopupItem(mwindow,
714 add_item(items[3] = new TimeBarPopupItem(mwindow,
718 add_item(items[4] = new TimeBarPopupItem(mwindow,
720 TIME_SAMPLES_HEX_TEXT,
722 add_item(items[5] = new TimeBarPopupItem(mwindow,
726 add_item(items[6] = new TimeBarPopupItem(mwindow,
728 TIME_FEET_FRAMES_TEXT,
732 void TimeBarPopup::update()
734 for(int i = 0; i < TOTAL_TIMEFORMATS; i++)
736 if(items[i]->value == mwindow->edl->session->time_format)
738 items[i]->set_checked(1);
742 items[i]->set_checked(0);