r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / cinelerra / zoombar.C
blob932ed60da98c6365b4169d3f942cd8567e5c7131
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 "patchbay.h"
13 #include "theme.h"
14 #include "trackcanvas.h"
15 #include "tracks.h"
16 #include "units.h"
17 #include "zoombar.h"
22 ZoomBar::ZoomBar(MWindow *mwindow, MWindowGUI *gui)
23  : BC_SubWindow(mwindow->theme->mzoom_x,
24         mwindow->theme->mzoom_y,
25         mwindow->theme->mzoom_w,
26         mwindow->theme->mzoom_h) 
28         this->gui = gui;
29         this->mwindow = mwindow;
30         old_position = 0;
33 ZoomBar::~ZoomBar()
35         delete sample_zoom;
36         delete amp_zoom;
37         delete track_zoom;
40 int ZoomBar::create_objects()
42         int x = 3;
43         int y = get_h() / 2 - 
44                 mwindow->theme->get_image_set("zoombar_menu", 0)[0]->get_h() / 2;
46         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
47         sample_zoom = new SampleZoomPanel(mwindow, this, x, y);
48         sample_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
49         sample_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
50         sample_zoom->create_objects();
51         sample_zoom->zoom_text->set_tooltip(_("Duration visible in the timeline"));
52         sample_zoom->zoom_tumbler->set_tooltip(_("Duration visible in the timeline"));
53         x += sample_zoom->get_w();
54         amp_zoom = new AmpZoomPanel(mwindow, this, x, y);
55         amp_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
56         amp_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
57         amp_zoom->create_objects();
58         amp_zoom->zoom_text->set_tooltip(_("Audio waveform scale"));
59         amp_zoom->zoom_tumbler->set_tooltip(_("Audio waveform scale"));
60         x += amp_zoom->get_w();
61         track_zoom = new TrackZoomPanel(mwindow, this, x, y);
62         track_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
63         track_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
64         track_zoom->create_objects();
65         track_zoom->zoom_text->set_tooltip(_("Height of tracks in the timeline"));
66         track_zoom->zoom_tumbler->set_tooltip(_("Height of tracks in the timeline"));
67         x += track_zoom->get_w() + 10;
69         add_subwindow(auto_type = new AutoTypeMenu(mwindow, this, x, y));
70         auto_type->create_objects();
71         x += auto_type->get_w() + 10;
72 #define DEFAULT_TEXT "000.00 to 000.00"
73         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y, 0));
74         x += auto_zoom->get_w();
75         add_subwindow(auto_zoom_text = new ZoomTextBox(
76                 mwindow, 
77                 this, 
78                 x, 
79                 y,
80                 DEFAULT_TEXT));
81         x += auto_zoom_text->get_w() + 5;
82         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y, 1));
83         update_autozoom();
84         x += auto_zoom->get_w() + 5;
86         add_subwindow(from_value = new FromTextBox(mwindow, this, x, y));
87         x += from_value->get_w() + 5;
88         add_subwindow(length_value = new LengthTextBox(mwindow, this, x, y));
89         x += length_value->get_w() + 5;
90         add_subwindow(to_value = new ToTextBox(mwindow, this, x, y));
91         x += to_value->get_w() + 5;
93         update_formatting(from_value);
94         update_formatting(length_value);
95         update_formatting(to_value);
97         add_subwindow(playback_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, RED));
99         add_subwindow(zoom_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, BLACK));
100         update();
101         return 0;
105 void ZoomBar::update_formatting(BC_TextBox *dst)
107         dst->set_separators(
108                 Units::format_to_separators(mwindow->edl->session->time_format));
111 void ZoomBar::resize_event()
113         reposition_window(mwindow->theme->mzoom_x,
114                 mwindow->theme->mzoom_y,
115                 mwindow->theme->mzoom_w,
116                 mwindow->theme->mzoom_h);
118         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
119 //      int x = 3, y = 1;
120 //      sample_zoom->reposition_window(x, y);
121 //      x += sample_zoom->get_w();
122 //      amp_zoom->reposition_window(x, y);
123 //      x += amp_zoom->get_w();
124 //      track_zoom->reposition_window(x, y);
125         flash();
128 void ZoomBar::redraw_time_dependancies()
130 // Recalculate sample zoom menu
131         sample_zoom->update_menu();
132         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
133         update_formatting(from_value);
134         update_formatting(length_value);
135         update_formatting(to_value);
136         update_autozoom();
137         update_clocks();
140 int ZoomBar::draw()
142         update();
143         return 0;
146 void ZoomBar::update_autozoom()
148         char string[BCTEXTLEN];
149         switch (mwindow->edl->local_session->zoombar_showautotype) {
150         case AUTOGROUPTYPE_AUDIO_FADE:
151         case AUTOGROUPTYPE_VIDEO_FADE:
152                 sprintf(string, "%0.01f to %0.01f\n", 
153                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
154                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
155                 break;
156         case AUTOGROUPTYPE_ZOOM:
157                 sprintf(string, "%0.03f to %0.03f\n", 
158                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
159                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
160                 break;
161         case AUTOGROUPTYPE_X:
162         case AUTOGROUPTYPE_Y:
163                 sprintf(string, "%0.0f to %.0f\n", 
164                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
165                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
166                 break;
167         }
168         auto_zoom_text->update(string);
171 int ZoomBar::update()
173         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
174         amp_zoom->update(mwindow->edl->local_session->zoom_y);
175         track_zoom->update(mwindow->edl->local_session->zoom_track);
176         update_autozoom();
177         update_clocks();
178         return 0;
181 int ZoomBar::update_clocks()
183         from_value->update_position(mwindow->edl->local_session->get_selectionstart(1));
184         length_value->update_position(mwindow->edl->local_session->get_selectionend(1) - 
185                 mwindow->edl->local_session->get_selectionstart(1));
186         to_value->update_position(mwindow->edl->local_session->get_selectionend(1));
187         return 0;
190 int ZoomBar::update_playback(int64_t new_position)
192         if(new_position != old_position)
193         {
194                 Units::totext(string, 
195                                 new_position, 
196                                 mwindow->edl->session->sample_rate, 
197                                 mwindow->edl->session->time_format, 
198                                 mwindow->edl->session->frame_rate,
199                                 mwindow->edl->session->frames_per_foot);
200                 playback_value->update(string);
201                 old_position = new_position;
202         }
203         return 0;
206 int ZoomBar::resize_event(int w, int h)
208 // don't change anything but y and width
209         reposition_window(0, h - this->get_h(), w, this->get_h());
210         return 0;
214 // Values for which_one
215 #define SET_FROM 1
216 #define SET_LENGTH 2
217 #define SET_TO 3
220 int ZoomBar::set_selection(int which_one)
222         double start_position = mwindow->edl->local_session->get_selectionstart(1);
223         double end_position = mwindow->edl->local_session->get_selectionend(1);
224         double length = end_position - start_position;
226 // Fix bogus results
228         switch(which_one)
229         {
230                 case SET_LENGTH:
231                         start_position = Units::text_to_seconds(from_value->get_text(), 
232                                 mwindow->edl->session->sample_rate, 
233                                 mwindow->edl->session->time_format, 
234                                 mwindow->edl->session->frame_rate,
235                                 mwindow->edl->session->frames_per_foot);
236                         length = Units::text_to_seconds(length_value->get_text(), 
237                                 mwindow->edl->session->sample_rate, 
238                                 mwindow->edl->session->time_format, 
239                                 mwindow->edl->session->frame_rate,
240                                 mwindow->edl->session->frames_per_foot);
241                         end_position = start_position + length;
243                         if(end_position < start_position)
244                         {
245                                 start_position = end_position;
246                                 mwindow->edl->local_session->set_selectionend(
247                                         mwindow->edl->local_session->get_selectionstart(1));
248                         }
249                         break;
251                 case SET_FROM:
252                         start_position = Units::text_to_seconds(from_value->get_text(), 
253                                 mwindow->edl->session->sample_rate, 
254                                 mwindow->edl->session->time_format, 
255                                 mwindow->edl->session->frame_rate,
256                                 mwindow->edl->session->frames_per_foot);
257                         end_position = Units::text_to_seconds(to_value->get_text(), 
258                                 mwindow->edl->session->sample_rate, 
259                                 mwindow->edl->session->time_format, 
260                                 mwindow->edl->session->frame_rate,
261                                 mwindow->edl->session->frames_per_foot);
263                         if(end_position < start_position)
264                         {
265                                 end_position = start_position;
266                                 mwindow->edl->local_session->set_selectionend(
267                                         mwindow->edl->local_session->get_selectionstart(1));
268                         }
269                         break;
271                 case SET_TO:
272                         start_position = Units::text_to_seconds(from_value->get_text(), 
273                                 mwindow->edl->session->sample_rate, 
274                                 mwindow->edl->session->time_format, 
275                                 mwindow->edl->session->frame_rate,
276                                 mwindow->edl->session->frames_per_foot);
277                         end_position = Units::text_to_seconds(to_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);
283                         if(end_position < start_position)
284                         {
285                                 start_position = end_position;
286                                 mwindow->edl->local_session->set_selectionend(
287                                         mwindow->edl->local_session->get_selectionstart(1));
288                         }
289                         break;
290         }
292         mwindow->edl->local_session->set_selectionstart(
293                 mwindow->edl->align_to_frame(start_position, 1));
294         mwindow->edl->local_session->set_selectionend(
295                 mwindow->edl->align_to_frame(end_position, 1));
298         mwindow->gui->timebar->update_highlights();
299         mwindow->gui->cursor->hide();
300         mwindow->gui->cursor->show();
301         update();
302         mwindow->sync_parameters(CHANGE_PARAMS);
303         mwindow->gui->canvas->flash();
305         return 0;
319 SampleZoomPanel::SampleZoomPanel(MWindow *mwindow, 
320         ZoomBar *zoombar, 
321         int x, 
322         int y)
323  : ZoomPanel(mwindow, 
324         zoombar, 
325         mwindow->edl->local_session->zoom_sample, 
326         x, 
327         y, 
328         110, 
329         MIN_ZOOM_TIME, 
330         MAX_ZOOM_TIME, 
331         ZOOM_TIME)
333         this->mwindow = mwindow;
334         this->zoombar = zoombar;
336 int SampleZoomPanel::handle_event()
338         mwindow->zoom_sample((int64_t)get_value());
339         return 1;
352 AmpZoomPanel::AmpZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
353  : ZoomPanel(mwindow, 
354         zoombar, 
355         mwindow->edl->local_session->zoom_y, 
356         x, 
357         y, 
358         80,
359         MIN_AMP_ZOOM, 
360         MAX_AMP_ZOOM, 
361         ZOOM_LONG)
363         this->mwindow = mwindow;
364         this->zoombar = zoombar;
366 int AmpZoomPanel::handle_event()
368         mwindow->zoom_amp((int64_t)get_value());
369         return 1;
372 TrackZoomPanel::TrackZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
373  : ZoomPanel(mwindow, 
374         zoombar, 
375         mwindow->edl->local_session->zoom_track, 
376         x, 
377         y, 
378         70,
379         MIN_TRACK_ZOOM, 
380         MAX_TRACK_ZOOM, 
381         ZOOM_LONG)
383         this->mwindow = mwindow;
384         this->zoombar = zoombar;
386 int TrackZoomPanel::handle_event()
388         mwindow->zoom_track((int64_t)get_value());
389         zoombar->amp_zoom->update(mwindow->edl->local_session->zoom_y);
390         return 1;
396 AutoZoom::AutoZoom(MWindow *mwindow, ZoomBar *zoombar, int x, int y, int changemax)
397  : BC_Tumbler(x,
398         y,
399         mwindow->theme->get_image_set("zoombar_tumbler"))
401         this->mwindow = mwindow;
402         this->zoombar = zoombar;
403         this->changemax = changemax;
404         if (changemax)
405                 set_tooltip(_("Automation range maximum"));
406         else
407                 set_tooltip(_("Automation range minimum"));
410 int AutoZoom::handle_up_event()
412         mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype,1,changemax);
414         mwindow->gui->zoombar->update_autozoom();
415         mwindow->gui->canvas->draw_overlays();
416         mwindow->gui->patchbay->update();
417         mwindow->gui->canvas->flash();
418         return 1;
421 int AutoZoom::handle_down_event()
423         mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype,0,changemax);
425         mwindow->gui->zoombar->update_autozoom();
426         mwindow->gui->canvas->draw_overlays();
427         mwindow->gui->patchbay->update();
428         mwindow->gui->canvas->flash();
429         return 1;
434 AutoTypeMenu::AutoTypeMenu(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
435         : BC_PopupMenu(x, y, 120,to_text(mwindow->edl->local_session->zoombar_showautotype), 1)
437         this->mwindow = mwindow;
438         this->zoombar = zoombar;
439         set_tooltip(_("Automation Type"));
442 void AutoTypeMenu::create_objects()
444         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_AUDIO_FADE)));
445         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_VIDEO_FADE)));
446         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_ZOOM)));
447         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_X)));
448         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_Y)));
451 char* AutoTypeMenu::to_text(int mode)
453         switch(mode)
454         {
455         case AUTOGROUPTYPE_AUDIO_FADE:
456                 return _("Audio Fade:");
457         case AUTOGROUPTYPE_VIDEO_FADE:
458                 return _("Video Fade:");
459         case AUTOGROUPTYPE_ZOOM:
460                 return _("Zoom:");
461         case AUTOGROUPTYPE_X:
462                 return _("X:");
463         case AUTOGROUPTYPE_Y:
464                 return _("Y:");
465         default:
466                 return _("??");
467         }
470 int AutoTypeMenu::from_text(char *text)
472         if(!strcmp(text, to_text(AUTOGROUPTYPE_AUDIO_FADE)))
473                 return AUTOGROUPTYPE_AUDIO_FADE;
474         if(!strcmp(text, to_text(AUTOGROUPTYPE_VIDEO_FADE)))
475                 return AUTOGROUPTYPE_VIDEO_FADE;
476         if(!strcmp(text, to_text(AUTOGROUPTYPE_ZOOM)))
477                 return AUTOGROUPTYPE_ZOOM;
478         if(!strcmp(text, to_text(AUTOGROUPTYPE_X)))
479                 return AUTOGROUPTYPE_X;
480         if(!strcmp(text, to_text(AUTOGROUPTYPE_Y)))
481                 return AUTOGROUPTYPE_Y;
484 int AutoTypeMenu::handle_event()
486         mwindow->edl->local_session->zoombar_showautotype = from_text(this->get_text());
487         this->zoombar->update_autozoom();
491 ZoomTextBox::ZoomTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y, char *text)
492  : BC_TextBox(x, y, 130, 1, text)
494         this->mwindow = mwindow;
495         this->zoombar = zoombar;
496         set_tooltip(_("Automation range"));
499 int ZoomTextBox::button_press_event()
501         if (!(get_buttonpress() == 4 || get_buttonpress() == 5)) {
502                 BC_TextBox::button_press_event();
503                 return 0;
504         }
505         if (!is_event_win()) return 0;
507         int changemax = 1;
508         if (get_relative_cursor_x() < get_w()/2)
509                 changemax = 0;
511         // increment
512         if (get_buttonpress() == 4)
513                 mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype, 1, changemax);
515         // decrement
516         if (get_buttonpress() == 5)
517                 mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype, 0, changemax);
519         mwindow->gui->zoombar->update_autozoom();
520         mwindow->gui->canvas->draw_overlays();
521         mwindow->gui->patchbay->update();
522         mwindow->gui->canvas->flash();
523         return 1;
526 int ZoomTextBox::handle_event()
528         float min, max;
529         if (sscanf(this->get_text(),"%f to%f",&min, &max) == 2)
530         {
531                 AUTOMATIONVIEWCLAMPS(min, mwindow->edl->local_session->zoombar_showautotype);
532                 AUTOMATIONVIEWCLAMPS(max, mwindow->edl->local_session->zoombar_showautotype);
533                 if (max > min) 
534                 {
535                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype] = min;
536                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype] = max;
537                         mwindow->gui->zoombar->update_autozoom();
538                         mwindow->gui->canvas->draw_overlays();
539                         mwindow->gui->patchbay->update();
540                         mwindow->gui->canvas->flash();
541                 }
542         }
543         // TODO: Make the text turn red when it's a bad range..
544         return 0;
551 FromTextBox::FromTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
552  : BC_TextBox(x, y, 90, 1, "")
554         this->mwindow = mwindow;
555         this->zoombar = zoombar;
556         set_tooltip(_("Selection start time"));
559 int FromTextBox::handle_event()
561         if(get_keypress() == 13)
562         {
563                 zoombar->set_selection(SET_FROM);
564                 return 1;
565         }
566         return 0;
569 int FromTextBox::update_position(double new_position)
571         new_position += mwindow->edl->session->get_frame_offset() / 
572                                                  mwindow->edl->session->frame_rate;;
573         Units::totext(string, 
574                 new_position, 
575                 mwindow->edl->session->time_format, 
576                 mwindow->edl->session->sample_rate, 
577                 mwindow->edl->session->frame_rate,
578                 mwindow->edl->session->frames_per_foot);
579 //printf("FromTextBox::update_position %f %s\n", new_position, string);
580         update(string);
581         return 0;
589 LengthTextBox::LengthTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
590  : BC_TextBox(x, y, 90, 1, "")
592         this->mwindow = mwindow;
593         this->zoombar = zoombar;
594         set_tooltip(_("Selection length"));
597 int LengthTextBox::handle_event()
599         if(get_keypress() == 13)
600         {
601                 zoombar->set_selection(SET_LENGTH);
602                 return 1;
603         }
604         return 0;
607 int LengthTextBox::update_position(double new_position)
609         Units::totext(string, 
610                 new_position, 
611                 mwindow->edl->session->time_format, 
612                 mwindow->edl->session->sample_rate, 
613                 mwindow->edl->session->frame_rate,
614                 mwindow->edl->session->frames_per_foot);
615         update(string);
616         return 0;
623 ToTextBox::ToTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
624  : BC_TextBox(x, y, 90, 1, "")
626         this->mwindow = mwindow;
627         this->zoombar = zoombar;
628         set_tooltip(_("Selection end time"));
631 int ToTextBox::handle_event()
633         if(get_keypress() == 13)
634         {
635                 zoombar->set_selection(SET_TO);
636                 return 1;
637         }
638         return 0;
641 int ToTextBox::update_position(double new_position)
643         new_position += mwindow->edl->session->get_frame_offset() /
644                                                  mwindow->edl->session->frame_rate;
645         Units::totext(string, 
646                 new_position, 
647                 mwindow->edl->session->time_format, 
648                 mwindow->edl->session->sample_rate, 
649                 mwindow->edl->session->frame_rate,
650                 mwindow->edl->session->frames_per_foot);
651         update(string);
652         return 0;