5fa6e4c845fdc5c063b4e980ffdb61f3d551f62b
[goodguy/history.git] / cinelerra-5.1 / cinelerra / mtimebar.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcsignals.h"
23 #include "clip.h"
24 #include "cplayback.h"
25 #include "cwindow.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "localsession.h"
29 #include "mainclock.h"
30 #include "maincursor.h"
31 #include "mainsession.h"
32 #include "mbuttons.h"
33 #include "mtimebar.h"
34 #include "mwindowgui.h"
35 #include "mwindow.h"
36 #include "patchbay.h"
37 #include "preferences.h"
38 #include "theme.h"
39 #include "trackcanvas.h"
40 #include "tracks.h"
41 #include "transportque.h"
42 #include "zoombar.h"
43
44
45
46 MTimeBar::MTimeBar(MWindow *mwindow,
47         MWindowGUI *gui,
48         int x,
49         int y,
50         int w,
51         int h)
52  : TimeBar(mwindow, gui, x, y, w, h)
53 {
54         this->gui = gui;
55         this->pane = 0;
56 }
57
58 MTimeBar::MTimeBar(MWindow *mwindow,
59         TimelinePane *pane,
60         int x,
61         int y,
62         int w,
63         int h)
64  : TimeBar(mwindow, mwindow->gui, x, y, w, h)
65 {
66         this->gui = mwindow->gui;
67         this->pane = pane;
68 }
69
70 void MTimeBar::create_objects()
71 {
72         gui->add_subwindow(menu = new TimeBarPopup(mwindow));
73         menu->create_objects();
74
75         TimeBar::create_objects();
76 }
77
78
79 double MTimeBar::pixel_to_position(int pixel)
80 {
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;
87 }
88
89
90 int64_t MTimeBar::position_to_pixel(double position)
91 {
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]);
96 }
97
98
99 void MTimeBar::stop_playback()
100 {
101         gui->unlock_window();
102         gui->mbuttons->transport->handle_transport(STOP, 1, 0, 0);
103         gui->lock_window();
104 }
105
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()
111 {
112
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;
120
121
122 // Calculate tick mark spacing, number spacing, and starting point based
123 // on zoom, time format, project settings.
124
125 // If the time format is for audio, mark round numbers of samples based on
126 // samplerate.
127 // Fow low zoom, mark tens of samples.
128
129 // If the time format is for video, mark round number of frames based on
130 // framerate.
131 // For low zoom, mark individual frames.
132
133         //int64_t windowspan = mwindow->edl->local_session->zoom_sample * get_w();
134
135         draw_range();
136
137
138
139
140
141 // Number of seconds per pixel
142         double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
143                 sample_rate;
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] *
148                 time_per_pixel;
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,
155                         view_start,
156                         mwindow->edl->session->time_format,
157                         sample_rate,
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,
162                         view_end,
163                         mwindow->edl->session->time_format,
164                         sample_rate,
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);
168
169
170 // Minimum seconds between text marks
171         double min_time = (double)min_pixels *
172                 mwindow->edl->local_session->zoom_sample /
173                 sample_rate;
174
175
176 // Get first text mark on or before window start
177         int64_t starting_mark = 0;
178
179         int progression = 1;
180
181 // Default text spacing
182         text_interval = 0.5;
183         double prev_text_interval = 1.0;
184
185         while(text_interval >= min_time)
186         {
187                 prev_text_interval = text_interval;
188                 if(progression == 0)
189                 {
190                         text_interval /= 2;
191                         progression++;
192                 }
193                 else
194                 if(progression == 1)
195                 {
196                         text_interval /= 2;
197                         progression++;
198                 }
199                 else
200                 if(progression == 2)
201                 {
202                         text_interval /= 2.5;
203                         progression = 0;
204                 }
205         }
206
207         text_interval = prev_text_interval;
208
209         if(1 >= min_time)
210                 ;
211         else
212         if(2 >= min_time)
213                 text_interval = 2;
214         else
215         if(5 >= min_time)
216                 text_interval = 5;
217         else
218         if(10 >= min_time)
219                 text_interval = 10;
220         else
221         if(15 >= min_time)
222                 text_interval = 15;
223         else
224         if(20 >= min_time)
225                 text_interval = 20;
226         else
227         if(30 >= min_time)
228                 text_interval = 30;
229         else
230         if(60 >= min_time)
231                 text_interval = 60;
232         else
233         if(120 >= min_time)
234                 text_interval = 120;
235         else
236         if(300 >= min_time)
237                 text_interval = 300;
238         else
239         if(600 >= min_time)
240                 text_interval = 600;
241         else
242         if(1200 >= min_time)
243                 text_interval = 1200;
244         else
245         if(1800 >= min_time)
246                 text_interval = 1800;
247         else
248         if(3600 >= min_time)
249                 text_interval = 3600;
250
251 // Set text interval
252         switch(mwindow->edl->session->time_format)
253         {
254                 case TIME_FEET_FRAMES:
255                 {
256                         double foot_seconds = frame_seconds * mwindow->edl->session->frames_per_foot;
257                         if(frame_seconds >= min_time)
258                                 text_interval = frame_seconds;
259                         else
260                         if(foot_seconds / 8.0 > min_time)
261                                 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 8.0;
262                         else
263                         if(foot_seconds / 4.0 > min_time)
264                                 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 4.0;
265                         else
266                         if(foot_seconds / 2.0 > min_time)
267                                 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot / 2.0;
268                         else
269                         if(foot_seconds > min_time)
270                                 text_interval = frame_seconds * mwindow->edl->session->frames_per_foot;
271                         else
272                         if(foot_seconds * 2 >= min_time)
273                                 text_interval = foot_seconds * 2;
274                         else
275                         if(foot_seconds * 5 >= min_time)
276                                 text_interval = foot_seconds * 5;
277                         else
278                         {
279
280                                 for(int factor = 10, progression = 0; factor <= 100000; )
281                                 {
282                                         if(foot_seconds * factor >= min_time)
283                                         {
284                                                 text_interval = foot_seconds * factor;
285                                                 break;
286                                         }
287
288                                         if(progression == 0)
289                                         {
290                                                 factor = (int)(factor * 2.5);
291                                                 progression++;
292                                         }
293                                         else
294                                         if(progression == 1)
295                                         {
296                                                 factor = (int)(factor * 2);
297                                                 progression++;
298                                         }
299                                         else
300                                         if(progression == 2)
301                                         {
302                                                 factor = (int)(factor * 2);
303                                                 progression = 0;
304                                         }
305                                 }
306
307                         }
308                         break;
309                 }
310
311                 case TIME_FRAMES:
312                 case TIME_HMSF:
313 // One frame per text mark
314                         if(frame_seconds >= min_time)
315                                 text_interval = frame_seconds;
316                         else
317                         if(frame_seconds * 2 >= min_time)
318                                 text_interval = frame_seconds * 2;
319                         else
320                         if(frame_seconds * 5 >= min_time)
321                                 text_interval = frame_seconds * 5;
322                         else
323                         {
324
325                                 for(int factor = 10, progression = 0; factor <= 100000; )
326                                 {
327                                         if(frame_seconds * factor >= min_time)
328                                         {
329                                                 text_interval = frame_seconds * factor;
330                                                 break;
331                                         }
332
333                                         if(progression == 0)
334                                         {
335                                                 factor = (int)(factor * 2.5);
336                                                 progression++;
337                                         }
338                                         else
339                                         if(progression == 1)
340                                         {
341                                                 factor = (int)(factor * 2);
342                                                 progression++;
343                                         }
344                                         else
345                                         if(progression == 2)
346                                         {
347                                                 factor = (int)(factor * 2);
348                                                 progression = 0;
349                                         }
350                                 }
351
352                         }
353                         break;
354
355                 default:
356                         break;
357         }
358
359 // Sanity
360         while(text_interval < min_time)
361         {
362                 text_interval *= 2;
363         }
364
365 // Set tick interval
366         tick_interval = text_interval;
367
368         switch(mwindow->edl->session->time_format)
369         {
370                 case TIME_HMSF:
371                 case TIME_FEET_FRAMES:
372                 case TIME_FRAMES:
373                         if(frame_seconds / time_per_pixel > TICK_SPACING)
374                                 tick_interval = frame_seconds;
375                         break;
376         }
377
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);
381
382         double start_position = (double)starting_mark * text_interval;
383         int64_t iteration = 0;
384
385
386 //printf("text_interval=%f\n", text_interval);
387         while(start_position + text_interval * iteration < view_end)
388         {
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];
392                 int pixel1 = pixel;
393
394                 Units::totext(string,
395                         position1,
396                         mwindow->edl->session->time_format,
397                         sample_rate,
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);
402
403                 draw_text(pixel + TEXT_MARGIN, get_text_ascent(MEDIUMFONT), string);
404                 draw_line(pixel, LINE_MARGIN, pixel, get_h() - 2);
405
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];
409
410                 for(double tick_position = position1;
411                         tick_position < position2;
412                         tick_position += tick_interval)
413                 {
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);
419                 }
420                 iteration++;
421         }
422
423
424 }
425
426 void MTimeBar::draw_range()
427 {
428         int x1 = 0, x2 = 0;
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];
437         }
438
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());
443         }
444         else {
445                 draw_top_background(get_parent(), 0, 0, get_w(), get_h());
446         }
447
448 //      int64_t pixel = position_to_pixel(
449 //              mwindow->edl->local_session->get_selectionstart(1));
450 //
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);
454 }
455
456 void MTimeBar::select_label(double position)
457 {
458         EDL *edl = mwindow->edl;
459
460         gui->unlock_window();
461         gui->mbuttons->transport->handle_transport(STOP, 1, 0, 0);
462         gui->lock_window();
463
464         position = mwindow->edl->align_to_frame(position, 1);
465
466         if(shift_down())
467         {
468                 if(position > edl->local_session->get_selectionend(1) / 2 +
469                         edl->local_session->get_selectionstart(1) / 2)
470                 {
471
472                         edl->local_session->set_selectionend(position);
473                 }
474                 else
475                 {
476                         edl->local_session->set_selectionstart(position);
477                 }
478         }
479         else
480         {
481                 edl->local_session->set_selectionstart(position);
482                 edl->local_session->set_selectionend(position);
483         }
484
485 // Que the CWindow
486         mwindow->cwindow->update(1, 0, 0, 0, 1);
487
488         gui->hide_cursor(0);
489         gui->draw_cursor(1);
490         gui->flash_canvas(1);
491         gui->zoombar->update();
492         update_highlights();
493         activate_timeline();
494 }
495
496
497 int MTimeBar::resize_event()
498 {
499         reposition_window(mwindow->theme->mtimebar_x,
500                 mwindow->theme->mtimebar_y,
501                 mwindow->theme->mtimebar_w,
502                 mwindow->theme->mtimebar_h);
503         update(0);
504         return 1;
505 }
506
507 int MTimeBar::resize_event(int x, int y, int w, int h)
508 {
509         reposition_window(x,
510                 y,
511                 w,
512                 h);
513         update(0);
514         return 1;
515 }
516
517 // int MTimeBar::test_preview(int buttonpress)
518 // {
519 //      int result = 0;
520 //      return result;
521 // }
522 //
523
524
525 void MTimeBar::handle_mwindow_drag()
526 {
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)
531         {
532                 pane->canvas->start_dragscroll();
533         }
534         else
535         if(relative_cursor_x < pane->canvas->get_w() &&
536                 relative_cursor_x >= 0)
537         {
538                 pane->canvas->stop_dragscroll();
539         }
540
541         update(0);
542 }
543
544 void MTimeBar::update(int flush)
545 {
546         TimeBar::update(flush);
547 }
548
549 void MTimeBar::update_clock(double position)
550 {
551         if(!mwindow->cwindow->playback_engine->is_playing_back)
552                 gui->mainclock->update(position);
553 }
554
555 void MTimeBar::update_cursor()
556 {
557         int rx = get_relative_cursor_x();
558         double position = pixel_to_position(rx);
559
560         position = mwindow->edl->align_to_frame(position, 0);
561         position = MAX(0, position);
562
563         mwindow->select_point(position);
564         update(1);
565 }
566
567 double MTimeBar::test_highlight()
568 {
569 // Don't crash during initialization
570         if(pane->canvas)
571         {
572                 if(mwindow->session->current_operation == NO_OPERATION)
573                 {
574                         if(pane->canvas->is_event_win() &&
575                                 pane->canvas->cursor_inside())
576                         {
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);
585                         }
586
587 //printf("MTimeBar::test_highlight %d %d %f\n", __LINE__, pane->canvas->cursor_inside(), pane->canvas->timebar_position);
588                         return pane->canvas->timebar_position;
589                 }
590                 else
591                 if(mwindow->session->current_operation == SELECT_REGION ||
592                         mwindow->session->current_operation == DRAG_EDITHANDLE2)
593                 {
594 //printf("MTimeBar::test_highlight %d %f\n", __LINE__, mwindow->gui->canvas->timebar_position);
595                         return pane->canvas->timebar_position;
596                 }
597
598                 return -1;
599         }
600         else
601         {
602                 return -1;
603         }
604 }
605
606 int MTimeBar::repeat_event(int64_t duration)
607 {
608         if(!pane->canvas->drag_scroll) return 0;
609         if(duration != BC_WindowBase::get_resources()->scroll_repeat) return 0;
610
611         int distance = 0;
612         int x_movement = 0;
613         int relative_cursor_x = pane->canvas->get_relative_cursor_x();
614         if(current_operation == TIMEBAR_DRAG)
615         {
616                 if(relative_cursor_x >= pane->canvas->get_w())
617                 {
618                         distance = relative_cursor_x - pane->canvas->get_w();
619                         x_movement = 1;
620                 }
621                 else
622                 if(relative_cursor_x < 0)
623                 {
624                         distance = relative_cursor_x;
625                         x_movement = 1;
626                 }
627
628
629
630                 if(x_movement)
631                 {
632                         update_cursor();
633                         mwindow->samplemovement(
634                                 mwindow->edl->local_session->view_start[pane->number] + distance,
635                                 pane->number);
636                 }
637                 return 1;
638         }
639
640         return 0;
641 }
642
643 int MTimeBar::button_press_event()
644 {
645         int result = 0;
646
647         if(is_event_win() && cursor_above() && get_buttonpress() == 3)
648         {
649                 menu->update();
650                 menu->activate_menu();
651                 result = 1;
652         }
653
654         if(!result) return TimeBar::button_press_event();
655         return result;
656 }
657
658
659 void MTimeBar::activate_timeline()
660 {
661         pane->activate();
662 }
663
664
665
666
667
668
669 TimeBarPopupItem::TimeBarPopupItem(MWindow *mwindow,
670         TimeBarPopup *menu,
671         const char *text,
672         int value)
673  : BC_MenuItem(text)
674 {
675         this->mwindow = mwindow;
676         this->menu = menu;
677         this->value = value;
678 }
679
680 int TimeBarPopupItem::handle_event()
681 {
682         mwindow->edl->session->time_format = value;
683         mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
684         mwindow->gui->redraw_time_dependancies();
685         return 1;
686 }
687
688
689
690 TimeBarPopup::TimeBarPopup(MWindow *mwindow)
691  : BC_PopupMenu(0,
692                 0,
693                 0,
694                 "",
695                 0)
696 {
697         this->mwindow = mwindow;
698 }
699
700 TimeBarPopup::~TimeBarPopup()
701 {
702 }
703
704
705 void TimeBarPopup::create_objects()
706 {
707         add_item(items[0] = new TimeBarPopupItem(mwindow,
708                 this,
709                 TIME_HMS_TEXT,
710                 TIME_HMS));
711         add_item(items[1] = new TimeBarPopupItem(mwindow,
712                 this,
713                 TIME_HMSF_TEXT,
714                 TIME_HMSF));
715         add_item(items[2] = new TimeBarPopupItem(mwindow,
716                 this,
717                 TIME_FRAMES_TEXT,
718                 TIME_FRAMES));
719         add_item(items[3] = new TimeBarPopupItem(mwindow,
720                 this,
721                 TIME_SAMPLES_TEXT,
722                 TIME_SAMPLES));
723         add_item(items[4] = new TimeBarPopupItem(mwindow,
724                 this,
725                 TIME_SAMPLES_HEX_TEXT,
726                 TIME_SAMPLES_HEX));
727         add_item(items[5] = new TimeBarPopupItem(mwindow,
728                 this,
729                 TIME_SECONDS_TEXT,
730                 TIME_SECONDS));
731         add_item(items[6] = new TimeBarPopupItem(mwindow,
732                 this,
733                 TIME_FEET_FRAMES_TEXT,
734                 TIME_FEET_FRAMES));
735 }
736
737 void TimeBarPopup::update()
738 {
739         for(int i = 0; i < TOTAL_TIMEFORMATS; i++)
740         {
741                 if(items[i]->value == mwindow->edl->session->time_format)
742                 {
743                         items[i]->set_checked(1);
744                 }
745                 else
746                 {
747                         items[i]->set_checked(0);
748                 }
749         }
750 }
751
752
753
754
755