Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / cinelerra / zoombar.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 "apatchgui.inc"
23 #include "clip.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "language.h"
27 #include "localsession.h"
28 #include "maincursor.h"
29 #include "mwindow.h"
30 #include "mwindowgui.h"
31 #include "mainsession.h"
32 #include "mtimebar.h"
33 #include "preferences.h"
34 #include "theme.h"
35 #include "trackcanvas.h"
36 #include "tracks.h"
37 #include "units.h"
38 #include "vpatchgui.inc"
39 #include "zoombar.h"
40
41
42
43
44 ZoomBar::ZoomBar(MWindow *mwindow, MWindowGUI *gui)
45  : BC_SubWindow(mwindow->theme->mzoom_x,
46         mwindow->theme->mzoom_y,
47         mwindow->theme->mzoom_w,
48         mwindow->theme->mzoom_h) 
49 {
50         this->gui = gui;
51         this->mwindow = mwindow;
52         old_position = 0;
53 }
54
55 ZoomBar::~ZoomBar()
56 {
57         delete sample_zoom;
58         delete amp_zoom;
59         delete track_zoom;
60 }
61
62 void ZoomBar::create_objects()
63 {
64         int x = 3;
65         int y = get_h() / 2 - 
66                 mwindow->theme->get_image_set("zoombar_menu", 0)[0]->get_h() / 2;
67
68         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
69         sample_zoom = new SampleZoomPanel(mwindow, this, x, y);
70         sample_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
71         sample_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
72         sample_zoom->create_objects();
73         sample_zoom->zoom_text->set_tooltip(_("Duration visible in the timeline"));
74         sample_zoom->zoom_tumbler->set_tooltip(_("Duration visible in the timeline"));
75         x += sample_zoom->get_w();
76         amp_zoom = new AmpZoomPanel(mwindow, this, x, y);
77         amp_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
78         amp_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
79         amp_zoom->create_objects();
80         amp_zoom->zoom_text->set_tooltip(_("Audio waveform scale"));
81         amp_zoom->zoom_tumbler->set_tooltip(_("Audio waveform scale"));
82         x += amp_zoom->get_w();
83         track_zoom = new TrackZoomPanel(mwindow, this, x, y);
84         track_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
85         track_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
86         track_zoom->create_objects();
87         track_zoom->zoom_text->set_tooltip(_("Height of tracks in the timeline"));
88         track_zoom->zoom_tumbler->set_tooltip(_("Height of tracks in the timeline"));
89         x += track_zoom->get_w() + 10;
90
91         add_subwindow(auto_type = new AutoTypeMenu(mwindow, this, x, y));
92         auto_type->create_objects();
93         x += auto_type->get_w() + 10;
94 #define DEFAULT_TEXT "000.00 to 000.00"
95         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y, 0));
96         x += auto_zoom->get_w();
97         add_subwindow(auto_zoom_text = new ZoomTextBox(
98                 mwindow, 
99                 this, 
100                 x, 
101                 y,
102                 DEFAULT_TEXT));
103         x += auto_zoom_text->get_w() + 5;
104         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y, 1));
105         update_autozoom();
106         x += auto_zoom->get_w() + 5;
107
108         add_subwindow(from_value = new FromTextBox(mwindow, this, x, y));
109         x += from_value->get_w() + 5;
110         add_subwindow(length_value = new LengthTextBox(mwindow, this, x, y));
111         x += length_value->get_w() + 5;
112         add_subwindow(to_value = new ToTextBox(mwindow, this, x, y));
113         x += to_value->get_w() + 5;
114
115         update_formatting(from_value);
116         update_formatting(length_value);
117         update_formatting(to_value);
118
119         add_subwindow(playback_value = new BC_Title(x, 100, "--", MEDIUMFONT, RED));
120
121         add_subwindow(zoom_value = new BC_Title(x, 100, "--", MEDIUMFONT, BLACK));
122         update();
123 }
124
125
126 void ZoomBar::update_formatting(BC_TextBox *dst)
127 {
128         dst->set_separators(
129                 Units::format_to_separators(mwindow->edl->session->time_format));
130 }
131
132 void ZoomBar::resize_event()
133 {
134         reposition_window(mwindow->theme->mzoom_x,
135                 mwindow->theme->mzoom_y,
136                 mwindow->theme->mzoom_w,
137                 mwindow->theme->mzoom_h);
138
139         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
140         int x = 3, y = 1;
141         if (sample_zoom) delete sample_zoom;
142         sample_zoom = new SampleZoomPanel(mwindow, this, x, y);
143         sample_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
144         sample_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
145         sample_zoom->create_objects();
146 //      x += sample_zoom->get_w();
147 //      amp_zoom->reposition_window(x, y);
148 //      x += amp_zoom->get_w();
149 //      track_zoom->reposition_window(x, y);
150         flash();
151 }
152
153 void ZoomBar::redraw_time_dependancies()
154 {
155 // Recalculate sample zoom menu
156         sample_zoom->update_menu();
157         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
158         update_formatting(from_value);
159         update_formatting(length_value);
160         update_formatting(to_value);
161         update_autozoom();
162         update_clocks();
163 }
164
165 int ZoomBar::draw()
166 {
167         update();
168         return 0;
169 }
170
171 void ZoomBar::update_autozoom()
172 {
173         char string[BCTEXTLEN];
174         switch (mwindow->edl->local_session->zoombar_showautotype) {
175         case AUTOGROUPTYPE_AUDIO_FADE:
176         case AUTOGROUPTYPE_VIDEO_FADE:
177                 sprintf(string, "%0.01f to %0.01f\n", 
178                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
179                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
180                 break;
181         case AUTOGROUPTYPE_ZOOM:
182                 sprintf(string, "%0.03f to %0.03f\n", 
183                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
184                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
185                 break;
186         case AUTOGROUPTYPE_X:
187         case AUTOGROUPTYPE_Y:
188                 sprintf(string, "%0.0f to %.0f\n", 
189                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
190                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
191                 break;
192         }
193         auto_zoom_text->update(string);
194 }
195
196 int ZoomBar::update()
197 {
198         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
199         amp_zoom->update(mwindow->edl->local_session->zoom_y);
200         track_zoom->update(mwindow->edl->local_session->zoom_track);
201         update_autozoom();
202         update_clocks();
203         return 0;
204 }
205
206 int ZoomBar::update_clocks()
207 {
208         from_value->update_position(mwindow->edl->local_session->get_selectionstart(1));
209         length_value->update_position(mwindow->edl->local_session->get_selectionend(1) - 
210                 mwindow->edl->local_session->get_selectionstart(1));
211         to_value->update_position(mwindow->edl->local_session->get_selectionend(1));
212         return 0;
213 }
214
215 int ZoomBar::update_playback(int64_t new_position)
216 {
217         if(new_position != old_position)
218         {
219                 Units::totext(string, 
220                                 new_position, 
221                                 mwindow->edl->session->sample_rate, 
222                                 mwindow->edl->session->time_format, 
223                                 mwindow->edl->session->frame_rate,
224                                 mwindow->edl->session->frames_per_foot);
225                 playback_value->update(string);
226                 old_position = new_position;
227         }
228         return 0;
229 }
230
231 int ZoomBar::resize_event(int w, int h)
232 {
233 // don't change anything but y and width
234         reposition_window(0, h - this->get_h(), w, this->get_h());
235         return 0;
236 }
237
238
239 // Values for which_one
240 #define SET_FROM 1
241 #define SET_LENGTH 2
242 #define SET_TO 3
243
244
245 int ZoomBar::set_selection(int which_one)
246 {
247         double start_position = mwindow->edl->local_session->get_selectionstart(1);
248         double end_position = mwindow->edl->local_session->get_selectionend(1);
249         double length = end_position - start_position;
250
251 // Fix bogus results
252
253         switch(which_one)
254         {
255                 case SET_LENGTH:
256                         start_position = Units::text_to_seconds(from_value->get_text(), 
257                                 mwindow->edl->session->sample_rate, 
258                                 mwindow->edl->session->time_format, 
259                                 mwindow->edl->session->frame_rate,
260                                 mwindow->edl->session->frames_per_foot);
261                         length = Units::text_to_seconds(length_value->get_text(), 
262                                 mwindow->edl->session->sample_rate, 
263                                 mwindow->edl->session->time_format, 
264                                 mwindow->edl->session->frame_rate,
265                                 mwindow->edl->session->frames_per_foot);
266                         end_position = start_position + length;
267
268                         if(end_position < start_position)
269                         {
270                                 start_position = end_position;
271                                 mwindow->edl->local_session->set_selectionend(
272                                         mwindow->edl->local_session->get_selectionstart(1));
273                         }
274                         break;
275
276                 case SET_FROM:
277                         start_position = Units::text_to_seconds(from_value->get_text(), 
278                                 mwindow->edl->session->sample_rate, 
279                                 mwindow->edl->session->time_format, 
280                                 mwindow->edl->session->frame_rate,
281                                 mwindow->edl->session->frames_per_foot);
282                         end_position = Units::text_to_seconds(to_value->get_text(), 
283                                 mwindow->edl->session->sample_rate, 
284                                 mwindow->edl->session->time_format, 
285                                 mwindow->edl->session->frame_rate,
286                                 mwindow->edl->session->frames_per_foot);
287
288                         if(end_position < start_position)
289                         {
290                                 end_position = start_position;
291                                 mwindow->edl->local_session->set_selectionend(
292                                         mwindow->edl->local_session->get_selectionstart(1));
293                         }
294                         break;
295
296                 case SET_TO:
297                         start_position = Units::text_to_seconds(from_value->get_text(), 
298                                 mwindow->edl->session->sample_rate, 
299                                 mwindow->edl->session->time_format, 
300                                 mwindow->edl->session->frame_rate,
301                                 mwindow->edl->session->frames_per_foot);
302                         end_position = Units::text_to_seconds(to_value->get_text(), 
303                                 mwindow->edl->session->sample_rate, 
304                                 mwindow->edl->session->time_format, 
305                                 mwindow->edl->session->frame_rate,
306                                 mwindow->edl->session->frames_per_foot);
307
308                         if(end_position < start_position)
309                         {
310                                 start_position = end_position;
311                                 mwindow->edl->local_session->set_selectionend(
312                                         mwindow->edl->local_session->get_selectionstart(1));
313                         }
314                         break;
315         }
316
317         mwindow->edl->local_session->set_selectionstart(
318                 mwindow->edl->align_to_frame(start_position, 1));
319         mwindow->edl->local_session->set_selectionend(
320                 mwindow->edl->align_to_frame(end_position, 1));
321
322
323         mwindow->gui->update_timebar_highlights();
324         mwindow->gui->hide_cursor(1);
325         mwindow->gui->show_cursor(1);
326         update();
327         mwindow->sync_parameters(CHANGE_PARAMS);
328         mwindow->gui->flash_canvas(1);
329
330         return 0;
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344 SampleZoomPanel::SampleZoomPanel(MWindow *mwindow, 
345         ZoomBar *zoombar, 
346         int x, 
347         int y)
348  : ZoomPanel(mwindow, 
349         zoombar, 
350         mwindow->edl->local_session->zoom_sample, 
351         x, 
352         y, 
353         110, 
354         MIN_ZOOM_TIME, 
355         MAX_ZOOM_TIME, 
356         ZOOM_TIME)
357 {
358         this->mwindow = mwindow;
359         this->zoombar = zoombar;
360 }
361 int SampleZoomPanel::handle_event()
362 {
363         mwindow->zoom_sample((int64_t)get_value());
364         return 1;
365 }
366
367
368 AmpZoomPanel::AmpZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
369  : ZoomPanel(mwindow, 
370         zoombar, 
371         mwindow->edl->local_session->zoom_y, 
372         x, 
373         y, 
374         80,
375         MIN_AMP_ZOOM, 
376         MAX_AMP_ZOOM, 
377         ZOOM_LONG)
378 {
379         this->mwindow = mwindow;
380         this->zoombar = zoombar;
381 }
382 int AmpZoomPanel::handle_event()
383 {
384         mwindow->zoom_amp((int64_t)get_value());
385         return 1;
386 }
387
388 TrackZoomPanel::TrackZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
389  : ZoomPanel(mwindow, 
390         zoombar, 
391         mwindow->edl->local_session->zoom_track, 
392         x, 
393         y, 
394         70,
395         MIN_TRACK_ZOOM, 
396         MAX_TRACK_ZOOM, 
397         ZOOM_LONG)
398 {
399         this->mwindow = mwindow;
400         this->zoombar = zoombar;
401 }
402 int TrackZoomPanel::handle_event()
403 {
404         mwindow->zoom_track((int64_t)get_value());
405         zoombar->amp_zoom->update(mwindow->edl->local_session->zoom_y);
406         return 1;
407 }
408
409
410
411
412 AutoZoom::AutoZoom(MWindow *mwindow, ZoomBar *zoombar, int x, int y, int changemax)
413  : BC_Tumbler(x,
414         y,
415         mwindow->theme->get_image_set("zoombar_tumbler"))
416 {
417         this->mwindow = mwindow;
418         this->zoombar = zoombar;
419         this->changemax = changemax;
420         if (changemax)
421                 set_tooltip(_("Automation range maximum"));
422         else
423                 set_tooltip(_("Automation range minimum"));
424 }
425
426 int AutoZoom::handle_up_event()
427 {
428         mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype,1,changemax);
429
430         mwindow->gui->zoombar->update_autozoom();
431         mwindow->gui->draw_overlays(0);
432         mwindow->gui->update_patchbay();
433         mwindow->gui->flash_canvas(1);
434         return 1;
435 }
436
437 int AutoZoom::handle_down_event()
438 {
439         mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype,0,changemax);
440
441         mwindow->gui->zoombar->update_autozoom();
442         mwindow->gui->draw_overlays(0);
443         mwindow->gui->update_patchbay();
444         mwindow->gui->flash_canvas(1);
445         return 1;
446 }
447
448
449
450 AutoTypeMenu::AutoTypeMenu(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
451         : BC_PopupMenu(x, y, 120,to_text(mwindow->edl->local_session->zoombar_showautotype), 1)
452 {
453         this->mwindow = mwindow;
454         this->zoombar = zoombar;
455         set_tooltip(_("Automation Type"));
456 }
457
458 void AutoTypeMenu::create_objects()
459 {
460         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_AUDIO_FADE)));
461         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_VIDEO_FADE)));
462         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_ZOOM)));
463         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_X)));
464         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_Y)));
465 }
466
467 const char* AutoTypeMenu::to_text(int mode)
468 {
469         switch(mode) {
470         case AUTOGROUPTYPE_AUDIO_FADE: return _("Audio Fade:");
471         case AUTOGROUPTYPE_VIDEO_FADE: return _("Video Fade:");
472         case AUTOGROUPTYPE_ZOOM: return _("Zoom:");
473         case AUTOGROUPTYPE_X: return "X:";
474         case AUTOGROUPTYPE_Y: return "Y:";
475         }
476         return "??";
477 }
478
479 int AutoTypeMenu::from_text(char *text)
480 {
481         if(!strcmp(text, to_text(AUTOGROUPTYPE_AUDIO_FADE)))
482                 return AUTOGROUPTYPE_AUDIO_FADE;
483         if(!strcmp(text, to_text(AUTOGROUPTYPE_VIDEO_FADE)))
484                 return AUTOGROUPTYPE_VIDEO_FADE;
485         if(!strcmp(text, to_text(AUTOGROUPTYPE_ZOOM)))
486                 return AUTOGROUPTYPE_ZOOM;
487         if(!strcmp(text, to_text(AUTOGROUPTYPE_X)))
488                 return AUTOGROUPTYPE_X;
489         if(!strcmp(text, to_text(AUTOGROUPTYPE_Y)))
490                 return AUTOGROUPTYPE_Y;
491         return AUTOGROUPTYPE_INT255;
492 }
493
494 int AutoTypeMenu::handle_event()
495 {
496         mwindow->edl->local_session->zoombar_showautotype = from_text(this->get_text());
497         this->zoombar->update_autozoom();
498         return 1;
499 }
500
501
502 ZoomTextBox::ZoomTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y, const char *text)
503  : BC_TextBox(x, y, 130, 1, text)
504 {
505         this->mwindow = mwindow;
506         this->zoombar = zoombar;
507         set_tooltip(_("Automation range"));
508 }
509
510 int ZoomTextBox::button_press_event()
511 {
512         if (!(get_buttonpress() == 4 || get_buttonpress() == 5)) {
513                 BC_TextBox::button_press_event();
514                 return 0;
515         }
516         if (!is_event_win()) return 0;
517
518         int changemax = 1;
519         if (get_relative_cursor_x() < get_w()/2)
520                 changemax = 0;
521
522         // increment
523         if (get_buttonpress() == 4)
524                 mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype, 1, changemax);
525
526         // decrement
527         if (get_buttonpress() == 5)
528                 mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype, 0, changemax);
529
530         mwindow->gui->zoombar->update_autozoom();
531         mwindow->gui->draw_overlays(0);
532         mwindow->gui->update_patchbay();
533         mwindow->gui->flash_canvas(1);
534         return 1;
535 }
536
537 int ZoomTextBox::handle_event()
538 {
539         float min, max;
540         if (sscanf(this->get_text(),"%f to%f",&min, &max) == 2)
541         {
542                 AUTOMATIONVIEWCLAMPS(min, mwindow->edl->local_session->zoombar_showautotype);
543                 AUTOMATIONVIEWCLAMPS(max, mwindow->edl->local_session->zoombar_showautotype);
544                 if (max > min) 
545                 {
546                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype] = min;
547                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype] = max;
548                         mwindow->gui->zoombar->update_autozoom();
549                         mwindow->gui->draw_overlays(0);
550                         mwindow->gui->update_patchbay();
551                         mwindow->gui->flash_canvas(1);
552                 }
553         }
554         // TODO: Make the text turn red when it's a bad range..
555         return 0;
556 }
557
558
559
560
561
562 FromTextBox::FromTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
563  : BC_TextBox(x, y, 90, 1, "")
564 {
565         this->mwindow = mwindow;
566         this->zoombar = zoombar;
567         set_tooltip(_("Selection start time"));
568 }
569
570 int FromTextBox::handle_event()
571 {
572         if(get_keypress() == 13)
573         {
574                 zoombar->set_selection(SET_FROM);
575                 return 1;
576         }
577         return 0;
578 }
579
580 int FromTextBox::update_position(double new_position)
581 {
582         new_position += mwindow->edl->session->get_frame_offset() / 
583                                                  mwindow->edl->session->frame_rate;;
584         Units::totext(string, 
585                 new_position, 
586                 mwindow->edl->session->time_format, 
587                 mwindow->edl->session->sample_rate, 
588                 mwindow->edl->session->frame_rate,
589                 mwindow->edl->session->frames_per_foot);
590 //printf("FromTextBox::update_position %f %s\n", new_position, string);
591         update(string);
592         return 0;
593 }
594
595
596
597
598
599
600 LengthTextBox::LengthTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
601  : BC_TextBox(x, y, 90, 1, "")
602 {
603         this->mwindow = mwindow;
604         this->zoombar = zoombar;
605         set_tooltip(_("Selection length"));
606 }
607
608 int LengthTextBox::handle_event()
609 {
610         if(get_keypress() == 13)
611         {
612                 zoombar->set_selection(SET_LENGTH);
613                 return 1;
614         }
615         return 0;
616 }
617
618 int LengthTextBox::update_position(double new_position)
619 {
620         Units::totext(string, 
621                 new_position, 
622                 mwindow->edl->session->time_format, 
623                 mwindow->edl->session->sample_rate, 
624                 mwindow->edl->session->frame_rate,
625                 mwindow->edl->session->frames_per_foot);
626         update(string);
627         return 0;
628 }
629
630
631
632
633
634 ToTextBox::ToTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
635  : BC_TextBox(x, y, 90, 1, "")
636 {
637         this->mwindow = mwindow;
638         this->zoombar = zoombar;
639         set_tooltip(_("Selection end time"));
640 }
641
642 int ToTextBox::handle_event()
643 {
644         if(get_keypress() == 13)
645         {
646                 zoombar->set_selection(SET_TO);
647                 return 1;
648         }
649         return 0;
650 }
651
652 int ToTextBox::update_position(double new_position)
653 {
654         new_position += mwindow->edl->session->get_frame_offset() /
655                                                  mwindow->edl->session->frame_rate;
656         Units::totext(string, 
657                 new_position, 
658                 mwindow->edl->session->time_format, 
659                 mwindow->edl->session->sample_rate, 
660                 mwindow->edl->session->frame_rate,
661                 mwindow->edl->session->frames_per_foot);
662         update(string);
663         return 0;
664 }