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_playback()
101 gui->unlock_window();
102 gui->mbuttons->transport->handle_transport(STOP, 1, 0, 0);
106 #define TEXT_MARGIN 4
107 #define TICK_SPACING 5
108 #define LINE_MARGIN 3
109 #define TICK_MARGIN 16
110 void MTimeBar::draw_time()
113 char string[BCTEXTLEN];
114 int sample_rate = mwindow->edl->session->sample_rate;
115 double frame_rate = mwindow->edl->session->frame_rate;
116 // Seconds between text markings
117 double text_interval = 3600.0;
118 // Seconds between tick marks
119 double tick_interval = 3600.0;
122 // Calculate tick mark spacing, number spacing, and starting point based
123 // on zoom, time format, project settings.
125 // If the time format is for audio, mark round numbers of samples based on
127 // Fow low zoom, mark tens of samples.
129 // If the time format is for video, mark round number of frames based on
131 // For low zoom, mark individual frames.
133 //int64_t windowspan = mwindow->edl->local_session->zoom_sample * get_w();
141 // Number of seconds per pixel
142 double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
144 // Seconds in each frame
145 double frame_seconds = (double)1.0 / frame_rate;
146 // Starting time of view in seconds.
147 double view_start = mwindow->edl->local_session->view_start[pane->number] *
149 // Ending time of view in seconds
150 double view_end = (double)(mwindow->edl->local_session->view_start[pane->number] +
151 get_w()) * time_per_pixel;
152 // Get minimum distance between text marks
153 int min_pixels1 = get_text_width(MEDIUMFONT,
154 Units::totext(string,
156 mwindow->edl->session->time_format,
158 mwindow->edl->session->frame_rate,
159 mwindow->edl->session->frames_per_foot)) + TEXT_MARGIN;
160 int min_pixels2 = get_text_width(MEDIUMFONT,
161 Units::totext(string,
163 mwindow->edl->session->time_format,
165 mwindow->edl->session->frame_rate,
166 mwindow->edl->session->frames_per_foot)) + TEXT_MARGIN;
167 int min_pixels = (int)MAX(min_pixels1, min_pixels2);
170 // Minimum seconds between text marks
171 double min_time = (double)min_pixels *
172 mwindow->edl->local_session->zoom_sample /
176 // Get first text mark on or before window start
177 int64_t starting_mark = 0;
181 // Default text spacing
183 double prev_text_interval = 1.0;
185 while(text_interval >= min_time)
187 prev_text_interval = text_interval;
202 text_interval /= 2.5;
207 text_interval = prev_text_interval;
243 text_interval = 1200;
246 text_interval = 1800;
249 text_interval = 3600;
252 switch(mwindow->edl->session->time_format)
254 case TIME_FEET_FRAMES:
256 double foot_seconds = frame_seconds * mwindow->edl->session->frames_per_foot;
257 if(frame_seconds >= min_time)
258 text_interval = frame_seconds;
260 if(foot_seconds / 8.0 > min_time)
261 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 8.0;
263 if(foot_seconds / 4.0 > min_time)
264 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 4.0;
266 if(foot_seconds / 2.0 > min_time)
267 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 2.0;
269 if(foot_seconds > min_time)
270 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot;
272 if(foot_seconds * 2 >= min_time)
273 text_interval = foot_seconds * 2;
275 if(foot_seconds * 5 >= min_time)
276 text_interval = foot_seconds * 5;
280 for(int factor = 10, progression = 0; factor <= 100000; )
282 if(foot_seconds * factor >= min_time)
284 text_interval = foot_seconds * factor;
290 factor = (int)(factor * 2.5);
296 factor = (int)(factor * 2);
302 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;
368 switch(mwindow->edl->session->time_format)
371 case TIME_FEET_FRAMES:
373 if(frame_seconds / time_per_pixel > TICK_SPACING)
374 tick_interval = frame_seconds;
378 // Get first text mark on or before window start
379 starting_mark = (int64_t)((double)mwindow->edl->local_session->view_start[pane->number] *
380 time_per_pixel / text_interval);
382 double start_position = (double)starting_mark * text_interval;
383 int64_t iteration = 0;
386 //printf("text_interval=%f\n", text_interval);
387 while(start_position + text_interval * iteration < view_end)
389 double position1 = start_position + text_interval * iteration;
390 int pixel = (int64_t)(position1 / time_per_pixel) -
391 mwindow->edl->local_session->view_start[pane->number];
394 Units::totext(string,
396 mwindow->edl->session->time_format,
398 mwindow->edl->session->frame_rate,
399 mwindow->edl->session->frames_per_foot);
400 set_color(get_resources()->default_text_color);
401 set_font(MEDIUMFONT);
403 draw_text(pixel + TEXT_MARGIN, get_text_ascent(MEDIUMFONT), string);
404 draw_line(pixel, LINE_MARGIN, pixel, get_h() - 2);
406 double position2 = start_position + text_interval * (iteration + 1);
407 int pixel2 = (int64_t)(position2 / time_per_pixel) -
408 mwindow->edl->local_session->view_start[pane->number];
410 for(double tick_position = position1;
411 tick_position < position2;
412 tick_position += tick_interval)
414 pixel = (int64_t)(tick_position / time_per_pixel) -
415 mwindow->edl->local_session->view_start[pane->number];
416 if(labs(pixel - pixel1) > 1 &&
417 labs(pixel - pixel2) > 1)
418 draw_line(pixel, TICK_MARGIN, pixel, get_h() - 2);
426 void MTimeBar::draw_range()
429 if( mwindow->brender_active && mwindow->preferences->use_brender &&
430 mwindow->edl->tracks->total_playable_vtracks() ) {
431 double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
432 mwindow->edl->session->sample_rate;
433 x1 = (int)(mwindow->edl->session->brender_start / time_per_pixel) -
434 mwindow->edl->local_session->view_start[pane->number];
435 x2 = (int)(mwindow->session->brender_end / time_per_pixel) -
436 mwindow->edl->local_session->view_start[pane->number];
439 if(x2 > x1 && x1 < get_w() && x2 > 0) {
440 draw_top_background(get_parent(), 0, 0, x1, get_h());
441 draw_3segmenth(x1, 0, x2 - x1, mwindow->theme->get_image("timebar_brender"));
442 draw_top_background(get_parent(), x2, 0, get_w() - x2, get_h());
445 draw_top_background(get_parent(), 0, 0, get_w(), get_h());
448 // int64_t pixel = position_to_pixel(
449 // mwindow->edl->local_session->get_selectionstart(1));
451 // set_color(mwindow->theme->timebar_cursor_colorg);
452 // draw_line(pixel, 0, pixel, get_h());
453 //printf("MTimeBar::draw_range %f %f\n", mwindow->session->brender_end, time_per_pixel);
456 void MTimeBar::select_label(double position)
458 EDL *edl = mwindow->edl;
460 gui->unlock_window();
461 gui->mbuttons->transport->handle_transport(STOP, 1, 0, 0);
464 position = mwindow->edl->align_to_frame(position, 1);
468 if(position > edl->local_session->get_selectionend(1) / 2 +
469 edl->local_session->get_selectionstart(1) / 2)
472 edl->local_session->set_selectionend(position);
476 edl->local_session->set_selectionstart(position);
481 edl->local_session->set_selectionstart(position);
482 edl->local_session->set_selectionend(position);
486 mwindow->cwindow->update(1, 0, 0, 0, 1);
490 gui->flash_canvas(1);
491 gui->zoombar->update();
497 int MTimeBar::resize_event()
499 reposition_window(mwindow->theme->mtimebar_x,
500 mwindow->theme->mtimebar_y,
501 mwindow->theme->mtimebar_w,
502 mwindow->theme->mtimebar_h);
507 int MTimeBar::resize_event(int x, int y, int w, int h)
517 // int MTimeBar::test_preview(int buttonpress)
525 void MTimeBar::handle_mwindow_drag()
527 //printf("TimeBar::cursor_motion_event %d %d\n", __LINE__, current_operation);
528 int relative_cursor_x = pane->canvas->get_relative_cursor_x();
529 if(relative_cursor_x >= pane->canvas->get_w() ||
530 relative_cursor_x < 0)
532 pane->canvas->start_dragscroll();
535 if(relative_cursor_x < pane->canvas->get_w() &&
536 relative_cursor_x >= 0)
538 pane->canvas->stop_dragscroll();
544 void MTimeBar::update(int flush)
546 TimeBar::update(flush);
549 void MTimeBar::update_clock(double position)
551 if(!mwindow->cwindow->playback_engine->is_playing_back)
552 gui->mainclock->update(position);
555 void MTimeBar::update_cursor()
557 int rx = get_relative_cursor_x();
558 double position = pixel_to_position(rx);
560 position = mwindow->edl->align_to_frame(position, 0);
561 position = MAX(0, position);
563 mwindow->select_point(position);
567 double MTimeBar::test_highlight()
569 // Don't crash during initialization
572 if(mwindow->session->current_operation == NO_OPERATION)
574 if(pane->canvas->is_event_win() &&
575 pane->canvas->cursor_inside())
577 int cursor_x = pane->canvas->get_cursor_x();
578 double position = (double)cursor_x *
579 (double)mwindow->edl->local_session->zoom_sample /
580 (double)mwindow->edl->session->sample_rate +
581 (double)mwindow->edl->local_session->view_start[pane->number] *
582 (double)mwindow->edl->local_session->zoom_sample /
583 (double)mwindow->edl->session->sample_rate;
584 pane->canvas->timebar_position = mwindow->edl->align_to_frame(position, 0);
587 //printf("MTimeBar::test_highlight %d %d %f\n", __LINE__, pane->canvas->cursor_inside(), pane->canvas->timebar_position);
588 return pane->canvas->timebar_position;
591 if(mwindow->session->current_operation == SELECT_REGION ||
592 mwindow->session->current_operation == DRAG_EDITHANDLE2)
594 //printf("MTimeBar::test_highlight %d %f\n", __LINE__, mwindow->gui->canvas->timebar_position);
595 return pane->canvas->timebar_position;
606 int MTimeBar::repeat_event(int64_t duration)
608 if(!pane->canvas->drag_scroll) return 0;
609 if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
613 int relative_cursor_x = pane->canvas->get_relative_cursor_x();
614 if(current_operation == TIMEBAR_DRAG)
616 if(relative_cursor_x >= pane->canvas->get_w())
618 distance = relative_cursor_x - pane->canvas->get_w();
622 if(relative_cursor_x < 0)
624 distance = relative_cursor_x;
633 mwindow->samplemovement(
634 mwindow->edl->local_session->view_start[pane->number] + distance,
643 int MTimeBar::button_press_event()
647 if(is_event_win() && cursor_above() && get_buttonpress() == 3)
650 menu->activate_menu();
654 if(!result) return TimeBar::button_press_event();
659 void MTimeBar::activate_timeline()
669 TimeBarPopupItem::TimeBarPopupItem(MWindow *mwindow,
675 this->mwindow = mwindow;
680 int TimeBarPopupItem::handle_event()
682 mwindow->edl->session->time_format = value;
683 mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
684 mwindow->gui->redraw_time_dependancies();
690 TimeBarPopup::TimeBarPopup(MWindow *mwindow)
697 this->mwindow = mwindow;
700 TimeBarPopup::~TimeBarPopup()
705 void TimeBarPopup::create_objects()
707 add_item(items[0] = new TimeBarPopupItem(mwindow,
711 add_item(items[1] = new TimeBarPopupItem(mwindow,
715 add_item(items[2] = new TimeBarPopupItem(mwindow,
719 add_item(items[3] = new TimeBarPopupItem(mwindow,
723 add_item(items[4] = new TimeBarPopupItem(mwindow,
725 TIME_SAMPLES_HEX_TEXT,
727 add_item(items[5] = new TimeBarPopupItem(mwindow,
731 add_item(items[6] = new TimeBarPopupItem(mwindow,
733 TIME_FEET_FRAMES_TEXT,
737 void TimeBarPopup::update()
739 for(int i = 0; i < TOTAL_TIMEFORMATS; i++)
741 if(items[i]->value == mwindow->edl->session->time_format)
743 items[i]->set_checked(1);
747 items[i]->set_checked(0);