r899: autogrouptypes given better names in xml; Decibels / video fade units need...
[cinelerra_cv/mob.git] / cinelerra / zoombar.C
blobd91f9213b76d8877c7949c4d8a21e63cd80098b0
1 #include "clip.h"
2 #include "edl.h"
3 #include "edlsession.h"
4 #include "language.h"
5 #include "localsession.h"
6 #include "maincursor.h"
7 #include "mwindow.h"
8 #include "mwindowgui.h"
9 #include "mainsession.h"
10 #include "mtimebar.h"
11 #include "preferences.h"
12 #include "theme.h"
13 #include "trackcanvas.h"
14 #include "tracks.h"
15 #include "units.h"
16 #include "zoombar.h"
21 ZoomBar::ZoomBar(MWindow *mwindow, MWindowGUI *gui)
22  : BC_SubWindow(mwindow->theme->mzoom_x,
23         mwindow->theme->mzoom_y,
24         mwindow->theme->mzoom_w,
25         mwindow->theme->mzoom_h) 
27         this->gui = gui;
28         this->mwindow = mwindow;
29         old_position = 0;
32 ZoomBar::~ZoomBar()
34         delete sample_zoom;
35         delete amp_zoom;
36         delete track_zoom;
39 int ZoomBar::create_objects()
41         int x = 3;
42         int y = get_h() / 2 - 
43                 mwindow->theme->get_image_set("zoombar_menu", 0)[0]->get_h() / 2;
45         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
46         sample_zoom = new SampleZoomPanel(mwindow, this, x, y);
47         sample_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
48         sample_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
49         sample_zoom->create_objects();
50         x += sample_zoom->get_w();
51         amp_zoom = new AmpZoomPanel(mwindow, this, x, y);
52         amp_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
53         amp_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
54         amp_zoom->create_objects();
55         x += amp_zoom->get_w();
56         track_zoom = new TrackZoomPanel(mwindow, this, x, y);
57         track_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
58         track_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
59         track_zoom->create_objects();
60         x += track_zoom->get_w() + 10;
62         add_subwindow(auto_type = new AutoTypeMenu(mwindow, this, x, y));
63         auto_type->create_objects();
64         x += auto_type->get_w() + 10;
65 #define DEFAULT_TEXT "000.00 to 000.00"
66         add_subwindow(auto_zoom_text = new ZoomTextBox(
67                 mwindow, 
68                 this, 
69                 x, 
70                 y,
71                 DEFAULT_TEXT));
72         x += auto_zoom_text->get_w() + 5;
73         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y));
74         update_autozoom();
75         x += auto_zoom->get_w() + 5;
77         add_subwindow(from_value = new FromTextBox(mwindow, this, x, y));
78         x += from_value->get_w() + 5;
79         add_subwindow(length_value = new LengthTextBox(mwindow, this, x, y));
80         x += length_value->get_w() + 5;
81         add_subwindow(to_value = new ToTextBox(mwindow, this, x, y));
82         x += to_value->get_w() + 5;
84         update_formatting(from_value);
85         update_formatting(length_value);
86         update_formatting(to_value);
88         add_subwindow(playback_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, RED));
90         add_subwindow(zoom_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, BLACK));
91         update();
92         return 0;
96 void ZoomBar::update_formatting(BC_TextBox *dst)
98         dst->set_separators(
99                 Units::format_to_separators(mwindow->edl->session->time_format));
102 void ZoomBar::resize_event()
104         reposition_window(mwindow->theme->mzoom_x,
105                 mwindow->theme->mzoom_y,
106                 mwindow->theme->mzoom_w,
107                 mwindow->theme->mzoom_h);
109         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
110 //      int x = 3, y = 1;
111 //      sample_zoom->reposition_window(x, y);
112 //      x += sample_zoom->get_w();
113 //      amp_zoom->reposition_window(x, y);
114 //      x += amp_zoom->get_w();
115 //      track_zoom->reposition_window(x, y);
116         flash();
119 void ZoomBar::redraw_time_dependancies()
121 // Recalculate sample zoom menu
122         sample_zoom->update_menu();
123         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
124         update_formatting(from_value);
125         update_formatting(length_value);
126         update_formatting(to_value);
127         update_autozoom();
128         update_clocks();
131 int ZoomBar::draw()
133         update();
134         return 0;
137 void ZoomBar::update_autozoom()
139         char string[BCTEXTLEN];
140         switch (mwindow->edl->local_session->zoombar_showautotype) {
141         case AUTOGROUPTYPE_AUDIO_FADE:
142         case AUTOGROUPTYPE_VIDEO_FADE:
143                 sprintf(string, "%0.01f to %0.01f\n", 
144                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
145                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
146                 break;
147         case AUTOGROUPTYPE_ZOOM:
148                 sprintf(string, "%0.03f to %0.03f\n", 
149                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
150                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
151                 break;
152         case AUTOGROUPTYPE_X:
153         case AUTOGROUPTYPE_Y:
154                 sprintf(string, "%0.0f to %.0f\n", 
155                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
156                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
157                 break;
158         }
159         auto_zoom_text->update(string);
162 int ZoomBar::update()
164         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
165         amp_zoom->update(mwindow->edl->local_session->zoom_y);
166         track_zoom->update(mwindow->edl->local_session->zoom_track);
167         update_autozoom();
168         update_clocks();
169         return 0;
172 int ZoomBar::update_clocks()
174         from_value->update_position(mwindow->edl->local_session->get_selectionstart(1));
175         length_value->update_position(mwindow->edl->local_session->get_selectionend(1) - 
176                 mwindow->edl->local_session->get_selectionstart(1));
177         to_value->update_position(mwindow->edl->local_session->get_selectionend(1));
178         return 0;
181 int ZoomBar::update_playback(int64_t new_position)
183         if(new_position != old_position)
184         {
185                 Units::totext(string, 
186                                 new_position, 
187                                 mwindow->edl->session->sample_rate, 
188                                 mwindow->edl->session->time_format, 
189                                 mwindow->edl->session->frame_rate,
190                                 mwindow->edl->session->frames_per_foot);
191                 playback_value->update(string);
192                 old_position = new_position;
193         }
194         return 0;
197 int ZoomBar::resize_event(int w, int h)
199 // don't change anything but y and width
200         reposition_window(0, h - this->get_h(), w, this->get_h());
201         return 0;
205 // Values for which_one
206 #define SET_FROM 1
207 #define SET_LENGTH 2
208 #define SET_TO 3
211 int ZoomBar::set_selection(int which_one)
213         double start_position = mwindow->edl->local_session->get_selectionstart(1);
214         double end_position = mwindow->edl->local_session->get_selectionend(1);
215         double length = end_position - start_position;
217 // Fix bogus results
219         switch(which_one)
220         {
221                 case SET_LENGTH:
222                         start_position = Units::text_to_seconds(from_value->get_text(), 
223                                 mwindow->edl->session->sample_rate, 
224                                 mwindow->edl->session->time_format, 
225                                 mwindow->edl->session->frame_rate,
226                                 mwindow->edl->session->frames_per_foot);
227                         length = Units::text_to_seconds(length_value->get_text(), 
228                                 mwindow->edl->session->sample_rate, 
229                                 mwindow->edl->session->time_format, 
230                                 mwindow->edl->session->frame_rate,
231                                 mwindow->edl->session->frames_per_foot);
232                         end_position = start_position + length;
234                         if(end_position < start_position)
235                         {
236                                 start_position = end_position;
237                                 mwindow->edl->local_session->set_selectionend(
238                                         mwindow->edl->local_session->get_selectionstart(1));
239                         }
240                         break;
242                 case SET_FROM:
243                         start_position = Units::text_to_seconds(from_value->get_text(), 
244                                 mwindow->edl->session->sample_rate, 
245                                 mwindow->edl->session->time_format, 
246                                 mwindow->edl->session->frame_rate,
247                                 mwindow->edl->session->frames_per_foot);
248                         end_position = Units::text_to_seconds(to_value->get_text(), 
249                                 mwindow->edl->session->sample_rate, 
250                                 mwindow->edl->session->time_format, 
251                                 mwindow->edl->session->frame_rate,
252                                 mwindow->edl->session->frames_per_foot);
254                         if(end_position < start_position)
255                         {
256                                 end_position = start_position;
257                                 mwindow->edl->local_session->set_selectionend(
258                                         mwindow->edl->local_session->get_selectionstart(1));
259                         }
260                         break;
262                 case SET_TO:
263                         start_position = Units::text_to_seconds(from_value->get_text(), 
264                                 mwindow->edl->session->sample_rate, 
265                                 mwindow->edl->session->time_format, 
266                                 mwindow->edl->session->frame_rate,
267                                 mwindow->edl->session->frames_per_foot);
268                         end_position = Units::text_to_seconds(to_value->get_text(), 
269                                 mwindow->edl->session->sample_rate, 
270                                 mwindow->edl->session->time_format, 
271                                 mwindow->edl->session->frame_rate,
272                                 mwindow->edl->session->frames_per_foot);
274                         if(end_position < start_position)
275                         {
276                                 start_position = end_position;
277                                 mwindow->edl->local_session->set_selectionend(
278                                         mwindow->edl->local_session->get_selectionstart(1));
279                         }
280                         break;
281         }
283         mwindow->edl->local_session->set_selectionstart(
284                 mwindow->edl->align_to_frame(start_position, 1));
285         mwindow->edl->local_session->set_selectionend(
286                 mwindow->edl->align_to_frame(end_position, 1));
289         mwindow->gui->timebar->update_highlights();
290         mwindow->gui->cursor->hide();
291         mwindow->gui->cursor->show();
292         update();
293         mwindow->sync_parameters(CHANGE_PARAMS);
294         mwindow->gui->canvas->flash();
296         return 0;
310 SampleZoomPanel::SampleZoomPanel(MWindow *mwindow, 
311         ZoomBar *zoombar, 
312         int x, 
313         int y)
314  : ZoomPanel(mwindow, 
315         zoombar, 
316         mwindow->edl->local_session->zoom_sample, 
317         x, 
318         y, 
319         110, 
320         MIN_ZOOM_TIME, 
321         MAX_ZOOM_TIME, 
322         ZOOM_TIME)
324         this->mwindow = mwindow;
325         this->zoombar = zoombar;
327 int SampleZoomPanel::handle_event()
329         mwindow->zoom_sample((int64_t)get_value());
330         return 1;
343 AmpZoomPanel::AmpZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
344  : ZoomPanel(mwindow, 
345         zoombar, 
346         mwindow->edl->local_session->zoom_y, 
347         x, 
348         y, 
349         80,
350         MIN_AMP_ZOOM, 
351         MAX_AMP_ZOOM, 
352         ZOOM_LONG)
354         this->mwindow = mwindow;
355         this->zoombar = zoombar;
357 int AmpZoomPanel::handle_event()
359         mwindow->zoom_amp((int64_t)get_value());
360         return 1;
363 TrackZoomPanel::TrackZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
364  : ZoomPanel(mwindow, 
365         zoombar, 
366         mwindow->edl->local_session->zoom_track, 
367         x, 
368         y, 
369         70,
370         MIN_TRACK_ZOOM, 
371         MAX_TRACK_ZOOM, 
372         ZOOM_LONG)
374         this->mwindow = mwindow;
375         this->zoombar = zoombar;
377 int TrackZoomPanel::handle_event()
379         mwindow->zoom_track((int64_t)get_value());
380         zoombar->amp_zoom->update(mwindow->edl->local_session->zoom_y);
381         return 1;
387 AutoZoom::AutoZoom(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
388  : BC_Tumbler(x,
389         y,
390         mwindow->theme->get_image_set("zoombar_tumbler"))
392         this->mwindow = mwindow;
393         this->zoombar = zoombar;
396 int AutoZoom::handle_up_event()
398         mwindow->expand_autos();
399         return 1;
402 int AutoZoom::handle_down_event()
404         mwindow->shrink_autos();
405         return 1;
410 AutoTypeMenu::AutoTypeMenu(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
411         : BC_PopupMenu(x, y, 120,to_text(mwindow->edl->local_session->zoombar_showautotype), 1)
413         this->mwindow = mwindow;
414         this->zoombar = zoombar;
417 void AutoTypeMenu::create_objects()
419         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_AUDIO_FADE)));
420         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_VIDEO_FADE)));
421         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_ZOOM)));
422         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_X)));
423         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_Y)));
426 char* AutoTypeMenu::to_text(int mode)
428         switch(mode)
429         {
430         case AUTOGROUPTYPE_AUDIO_FADE:
431                 return _("Audio Fade:");
432         case AUTOGROUPTYPE_VIDEO_FADE:
433                 return _("Video Fade:");
434         case AUTOGROUPTYPE_ZOOM:
435                 return _("Zoom:");
436         case AUTOGROUPTYPE_X:
437                 return _("X:");
438         case AUTOGROUPTYPE_Y:
439                 return _("Y:");
440         default:
441                 return _("??");
442         }
445 int AutoTypeMenu::from_text(char *text)
447         if(!strcmp(text, to_text(AUTOGROUPTYPE_AUDIO_FADE)))
448                 return AUTOGROUPTYPE_AUDIO_FADE;
449         if(!strcmp(text, to_text(AUTOGROUPTYPE_VIDEO_FADE)))
450                 return AUTOGROUPTYPE_VIDEO_FADE;
451         if(!strcmp(text, to_text(AUTOGROUPTYPE_ZOOM)))
452                 return AUTOGROUPTYPE_ZOOM;
453         if(!strcmp(text, to_text(AUTOGROUPTYPE_X)))
454                 return AUTOGROUPTYPE_X;
455         if(!strcmp(text, to_text(AUTOGROUPTYPE_Y)))
456                 return AUTOGROUPTYPE_Y;
459 int AutoTypeMenu::handle_event()
461         mwindow->edl->local_session->zoombar_showautotype = from_text(this->get_text());
462         this->zoombar->update_autozoom();
466 ZoomTextBox::ZoomTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y, char *text)
467  : BC_TextBox(x, y, 130, 1, text)
469         this->mwindow = mwindow;
470         this->zoombar = zoombar;
473 int ZoomTextBox::button_press_event()
475         float val;
477         if (!(get_buttonpress() == 4 || get_buttonpress() == 5)) {
478                 BC_TextBox::button_press_event();
479                 return 0;
480         }
481         if (!is_event_win()) return 0;
483         if (get_relative_cursor_x() < get_w()/2)
484                 val = mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype];
485         else
486                 val = mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype];
487         
488         if (get_buttonpress() == 5) {
489                 switch (mwindow->edl->local_session->zoombar_showautotype) {
490                 case AUTOGROUPTYPE_AUDIO_FADE:
491                         val -= 2;
492                         break;
493                 case AUTOGROUPTYPE_VIDEO_FADE:
494                         val -= 1;
495                         if (val < 0) val = 0;
496                         break;
497                 case AUTOGROUPTYPE_ZOOM:
498                         if (val > 0) val = val/2;
499                         if (val < 0) val = 0;
500                         break;
501                 case AUTOGROUPTYPE_X:
502                 case AUTOGROUPTYPE_Y:
503                         val = floor(val-5);
504                         break;
505                 }
506         }
507         
508         if (get_buttonpress() == 4) {
509                 switch (mwindow->edl->local_session->zoombar_showautotype) {
510                 case AUTOGROUPTYPE_AUDIO_FADE:
511                         val += 2;
512                         break;
513                 case AUTOGROUPTYPE_VIDEO_FADE:
514                         val += 1;
515                         if (val < 0) val = 0;
516                         break;
517                 case AUTOGROUPTYPE_ZOOM:
518                         if (val == 0) 
519                                 val = 0.001;
520                         else 
521                                 val = val*2;
522                         break;
523                 case AUTOGROUPTYPE_X:
524                 case AUTOGROUPTYPE_Y:
525                         val = floor(val + 5);
526                         break;
527                 }
528         }
529         
530         if (get_relative_cursor_x() < get_w()/2) {
531                 if (val < mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype])
532                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype] = val;
533         }
534         else
535                 if (val > mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype])
536                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype] = val;
537         
538         mwindow->gui->zoombar->update_autozoom();
539         mwindow->gui->canvas->draw_overlays();
540         mwindow->gui->canvas->flash();
541         return 1;
544 int ZoomTextBox::handle_event()
546         float min, max;
547         if (sscanf(this->get_text(),"%f to%f",&min, &max) == 2)
548                 if (
549                         max > min
550                         && ( mwindow->edl->local_session->zoombar_showautotype != AUTOGROUPTYPE_ZOOM
551                              || (max >= 0.000 && min >= 0.000) )
552                         ) 
553                 {
554                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype] = min;
555                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype] = max;
556                         mwindow->gui->zoombar->update_autozoom();
557                         mwindow->gui->canvas->draw_overlays();
558                         mwindow->gui->canvas->flash();
559                 }
560         // TODO: Make the text turn red when it's a bad range..
561         return 0;
568 FromTextBox::FromTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
569  : BC_TextBox(x, y, 90, 1, "")
571         this->mwindow = mwindow;
572         this->zoombar = zoombar;
575 int FromTextBox::handle_event()
577         if(get_keypress() == 13)
578         {
579                 zoombar->set_selection(SET_FROM);
580                 return 1;
581         }
582         return 0;
585 int FromTextBox::update_position(double new_position)
587         new_position += mwindow->edl->session->get_frame_offset() / 
588                                                  mwindow->edl->session->frame_rate;;
589         Units::totext(string, 
590                 new_position, 
591                 mwindow->edl->session->time_format, 
592                 mwindow->edl->session->sample_rate, 
593                 mwindow->edl->session->frame_rate,
594                 mwindow->edl->session->frames_per_foot);
595 //printf("FromTextBox::update_position %f %s\n", new_position, string);
596         update(string);
597         return 0;
605 LengthTextBox::LengthTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
606  : BC_TextBox(x, y, 90, 1, "")
608         this->mwindow = mwindow;
609         this->zoombar = zoombar;
612 int LengthTextBox::handle_event()
614         if(get_keypress() == 13)
615         {
616                 zoombar->set_selection(SET_LENGTH);
617                 return 1;
618         }
619         return 0;
622 int LengthTextBox::update_position(double new_position)
624         Units::totext(string, 
625                 new_position, 
626                 mwindow->edl->session->time_format, 
627                 mwindow->edl->session->sample_rate, 
628                 mwindow->edl->session->frame_rate,
629                 mwindow->edl->session->frames_per_foot);
630         update(string);
631         return 0;
638 ToTextBox::ToTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
639  : BC_TextBox(x, y, 90, 1, "")
641         this->mwindow = mwindow;
642         this->zoombar = zoombar;
645 int ToTextBox::handle_event()
647         if(get_keypress() == 13)
648         {
649                 zoombar->set_selection(SET_TO);
650                 return 1;
651         }
652         return 0;
655 int ToTextBox::update_position(double new_position)
657         new_position += mwindow->edl->session->get_frame_offset() /
658                                                  mwindow->edl->session->frame_rate;
659         Units::totext(string, 
660                 new_position, 
661                 mwindow->edl->session->time_format, 
662                 mwindow->edl->session->sample_rate, 
663                 mwindow->edl->session->frame_rate,
664                 mwindow->edl->session->frames_per_foot);
665         update(string);
666         return 0;