r903: BC_FileBoxDirectoryText::handle_event - fix for the change in meaning of is_di...
[cinelerra_cv/mob.git] / cinelerra / zoombar.C
blob930097075a4c3c3b8defea4fc1377be1e2243466
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 = new AutoZoom(mwindow, this, x, y, 0));
67         x += auto_zoom->get_w();
68         add_subwindow(auto_zoom_text = new ZoomTextBox(
69                 mwindow, 
70                 this, 
71                 x, 
72                 y,
73                 DEFAULT_TEXT));
74         x += auto_zoom_text->get_w() + 5;
75         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y, 1));
76         update_autozoom();
77         x += auto_zoom->get_w() + 5;
79         add_subwindow(from_value = new FromTextBox(mwindow, this, x, y));
80         x += from_value->get_w() + 5;
81         add_subwindow(length_value = new LengthTextBox(mwindow, this, x, y));
82         x += length_value->get_w() + 5;
83         add_subwindow(to_value = new ToTextBox(mwindow, this, x, y));
84         x += to_value->get_w() + 5;
86         update_formatting(from_value);
87         update_formatting(length_value);
88         update_formatting(to_value);
90         add_subwindow(playback_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, RED));
92         add_subwindow(zoom_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, BLACK));
93         update();
94         return 0;
98 void ZoomBar::update_formatting(BC_TextBox *dst)
100         dst->set_separators(
101                 Units::format_to_separators(mwindow->edl->session->time_format));
104 void ZoomBar::resize_event()
106         reposition_window(mwindow->theme->mzoom_x,
107                 mwindow->theme->mzoom_y,
108                 mwindow->theme->mzoom_w,
109                 mwindow->theme->mzoom_h);
111         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
112 //      int x = 3, y = 1;
113 //      sample_zoom->reposition_window(x, y);
114 //      x += sample_zoom->get_w();
115 //      amp_zoom->reposition_window(x, y);
116 //      x += amp_zoom->get_w();
117 //      track_zoom->reposition_window(x, y);
118         flash();
121 void ZoomBar::redraw_time_dependancies()
123 // Recalculate sample zoom menu
124         sample_zoom->update_menu();
125         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
126         update_formatting(from_value);
127         update_formatting(length_value);
128         update_formatting(to_value);
129         update_autozoom();
130         update_clocks();
133 int ZoomBar::draw()
135         update();
136         return 0;
139 void ZoomBar::update_autozoom()
141         char string[BCTEXTLEN];
142         switch (mwindow->edl->local_session->zoombar_showautotype) {
143         case AUTOGROUPTYPE_AUDIO_FADE:
144         case AUTOGROUPTYPE_VIDEO_FADE:
145                 sprintf(string, "%0.01f to %0.01f\n", 
146                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
147                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
148                 break;
149         case AUTOGROUPTYPE_ZOOM:
150                 sprintf(string, "%0.03f to %0.03f\n", 
151                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
152                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
153                 break;
154         case AUTOGROUPTYPE_X:
155         case AUTOGROUPTYPE_Y:
156                 sprintf(string, "%0.0f to %.0f\n", 
157                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype],
158                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype]);
159                 break;
160         }
161         auto_zoom_text->update(string);
164 int ZoomBar::update()
166         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
167         amp_zoom->update(mwindow->edl->local_session->zoom_y);
168         track_zoom->update(mwindow->edl->local_session->zoom_track);
169         update_autozoom();
170         update_clocks();
171         return 0;
174 int ZoomBar::update_clocks()
176         from_value->update_position(mwindow->edl->local_session->get_selectionstart(1));
177         length_value->update_position(mwindow->edl->local_session->get_selectionend(1) - 
178                 mwindow->edl->local_session->get_selectionstart(1));
179         to_value->update_position(mwindow->edl->local_session->get_selectionend(1));
180         return 0;
183 int ZoomBar::update_playback(int64_t new_position)
185         if(new_position != old_position)
186         {
187                 Units::totext(string, 
188                                 new_position, 
189                                 mwindow->edl->session->sample_rate, 
190                                 mwindow->edl->session->time_format, 
191                                 mwindow->edl->session->frame_rate,
192                                 mwindow->edl->session->frames_per_foot);
193                 playback_value->update(string);
194                 old_position = new_position;
195         }
196         return 0;
199 int ZoomBar::resize_event(int w, int h)
201 // don't change anything but y and width
202         reposition_window(0, h - this->get_h(), w, this->get_h());
203         return 0;
207 // Values for which_one
208 #define SET_FROM 1
209 #define SET_LENGTH 2
210 #define SET_TO 3
213 int ZoomBar::set_selection(int which_one)
215         double start_position = mwindow->edl->local_session->get_selectionstart(1);
216         double end_position = mwindow->edl->local_session->get_selectionend(1);
217         double length = end_position - start_position;
219 // Fix bogus results
221         switch(which_one)
222         {
223                 case SET_LENGTH:
224                         start_position = Units::text_to_seconds(from_value->get_text(), 
225                                 mwindow->edl->session->sample_rate, 
226                                 mwindow->edl->session->time_format, 
227                                 mwindow->edl->session->frame_rate,
228                                 mwindow->edl->session->frames_per_foot);
229                         length = Units::text_to_seconds(length_value->get_text(), 
230                                 mwindow->edl->session->sample_rate, 
231                                 mwindow->edl->session->time_format, 
232                                 mwindow->edl->session->frame_rate,
233                                 mwindow->edl->session->frames_per_foot);
234                         end_position = start_position + length;
236                         if(end_position < start_position)
237                         {
238                                 start_position = end_position;
239                                 mwindow->edl->local_session->set_selectionend(
240                                         mwindow->edl->local_session->get_selectionstart(1));
241                         }
242                         break;
244                 case SET_FROM:
245                         start_position = Units::text_to_seconds(from_value->get_text(), 
246                                 mwindow->edl->session->sample_rate, 
247                                 mwindow->edl->session->time_format, 
248                                 mwindow->edl->session->frame_rate,
249                                 mwindow->edl->session->frames_per_foot);
250                         end_position = Units::text_to_seconds(to_value->get_text(), 
251                                 mwindow->edl->session->sample_rate, 
252                                 mwindow->edl->session->time_format, 
253                                 mwindow->edl->session->frame_rate,
254                                 mwindow->edl->session->frames_per_foot);
256                         if(end_position < start_position)
257                         {
258                                 end_position = start_position;
259                                 mwindow->edl->local_session->set_selectionend(
260                                         mwindow->edl->local_session->get_selectionstart(1));
261                         }
262                         break;
264                 case SET_TO:
265                         start_position = Units::text_to_seconds(from_value->get_text(), 
266                                 mwindow->edl->session->sample_rate, 
267                                 mwindow->edl->session->time_format, 
268                                 mwindow->edl->session->frame_rate,
269                                 mwindow->edl->session->frames_per_foot);
270                         end_position = Units::text_to_seconds(to_value->get_text(), 
271                                 mwindow->edl->session->sample_rate, 
272                                 mwindow->edl->session->time_format, 
273                                 mwindow->edl->session->frame_rate,
274                                 mwindow->edl->session->frames_per_foot);
276                         if(end_position < start_position)
277                         {
278                                 start_position = end_position;
279                                 mwindow->edl->local_session->set_selectionend(
280                                         mwindow->edl->local_session->get_selectionstart(1));
281                         }
282                         break;
283         }
285         mwindow->edl->local_session->set_selectionstart(
286                 mwindow->edl->align_to_frame(start_position, 1));
287         mwindow->edl->local_session->set_selectionend(
288                 mwindow->edl->align_to_frame(end_position, 1));
291         mwindow->gui->timebar->update_highlights();
292         mwindow->gui->cursor->hide();
293         mwindow->gui->cursor->show();
294         update();
295         mwindow->sync_parameters(CHANGE_PARAMS);
296         mwindow->gui->canvas->flash();
298         return 0;
312 SampleZoomPanel::SampleZoomPanel(MWindow *mwindow, 
313         ZoomBar *zoombar, 
314         int x, 
315         int y)
316  : ZoomPanel(mwindow, 
317         zoombar, 
318         mwindow->edl->local_session->zoom_sample, 
319         x, 
320         y, 
321         110, 
322         MIN_ZOOM_TIME, 
323         MAX_ZOOM_TIME, 
324         ZOOM_TIME)
326         this->mwindow = mwindow;
327         this->zoombar = zoombar;
329 int SampleZoomPanel::handle_event()
331         mwindow->zoom_sample((int64_t)get_value());
332         return 1;
345 AmpZoomPanel::AmpZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
346  : ZoomPanel(mwindow, 
347         zoombar, 
348         mwindow->edl->local_session->zoom_y, 
349         x, 
350         y, 
351         80,
352         MIN_AMP_ZOOM, 
353         MAX_AMP_ZOOM, 
354         ZOOM_LONG)
356         this->mwindow = mwindow;
357         this->zoombar = zoombar;
359 int AmpZoomPanel::handle_event()
361         mwindow->zoom_amp((int64_t)get_value());
362         return 1;
365 TrackZoomPanel::TrackZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
366  : ZoomPanel(mwindow, 
367         zoombar, 
368         mwindow->edl->local_session->zoom_track, 
369         x, 
370         y, 
371         70,
372         MIN_TRACK_ZOOM, 
373         MAX_TRACK_ZOOM, 
374         ZOOM_LONG)
376         this->mwindow = mwindow;
377         this->zoombar = zoombar;
379 int TrackZoomPanel::handle_event()
381         mwindow->zoom_track((int64_t)get_value());
382         zoombar->amp_zoom->update(mwindow->edl->local_session->zoom_y);
383         return 1;
389 AutoZoom::AutoZoom(MWindow *mwindow, ZoomBar *zoombar, int x, int y, int changemax)
390  : BC_Tumbler(x,
391         y,
392         mwindow->theme->get_image_set("zoombar_tumbler"))
394         this->mwindow = mwindow;
395         this->zoombar = zoombar;
396         this->changemax = changemax;
399 int AutoZoom::handle_up_event()
401         mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype,1,changemax);
403         mwindow->gui->zoombar->update_autozoom();
404         mwindow->gui->canvas->draw_overlays();
405         mwindow->gui->canvas->flash();
406         return 1;
409 int AutoZoom::handle_down_event()
411         mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype,0,changemax);
413         mwindow->gui->zoombar->update_autozoom();
414         mwindow->gui->canvas->draw_overlays();
415         mwindow->gui->canvas->flash();
416         return 1;
421 AutoTypeMenu::AutoTypeMenu(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
422         : BC_PopupMenu(x, y, 120,to_text(mwindow->edl->local_session->zoombar_showautotype), 1)
424         this->mwindow = mwindow;
425         this->zoombar = zoombar;
428 void AutoTypeMenu::create_objects()
430         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_AUDIO_FADE)));
431         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_VIDEO_FADE)));
432         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_ZOOM)));
433         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_X)));
434         add_item(new BC_MenuItem(to_text(AUTOGROUPTYPE_Y)));
437 char* AutoTypeMenu::to_text(int mode)
439         switch(mode)
440         {
441         case AUTOGROUPTYPE_AUDIO_FADE:
442                 return _("Audio Fade:");
443         case AUTOGROUPTYPE_VIDEO_FADE:
444                 return _("Video Fade:");
445         case AUTOGROUPTYPE_ZOOM:
446                 return _("Zoom:");
447         case AUTOGROUPTYPE_X:
448                 return _("X:");
449         case AUTOGROUPTYPE_Y:
450                 return _("Y:");
451         default:
452                 return _("??");
453         }
456 int AutoTypeMenu::from_text(char *text)
458         if(!strcmp(text, to_text(AUTOGROUPTYPE_AUDIO_FADE)))
459                 return AUTOGROUPTYPE_AUDIO_FADE;
460         if(!strcmp(text, to_text(AUTOGROUPTYPE_VIDEO_FADE)))
461                 return AUTOGROUPTYPE_VIDEO_FADE;
462         if(!strcmp(text, to_text(AUTOGROUPTYPE_ZOOM)))
463                 return AUTOGROUPTYPE_ZOOM;
464         if(!strcmp(text, to_text(AUTOGROUPTYPE_X)))
465                 return AUTOGROUPTYPE_X;
466         if(!strcmp(text, to_text(AUTOGROUPTYPE_Y)))
467                 return AUTOGROUPTYPE_Y;
470 int AutoTypeMenu::handle_event()
472         mwindow->edl->local_session->zoombar_showautotype = from_text(this->get_text());
473         this->zoombar->update_autozoom();
477 ZoomTextBox::ZoomTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y, char *text)
478  : BC_TextBox(x, y, 130, 1, text)
480         this->mwindow = mwindow;
481         this->zoombar = zoombar;
484 int ZoomTextBox::button_press_event()
486         if (!(get_buttonpress() == 4 || get_buttonpress() == 5)) {
487                 BC_TextBox::button_press_event();
488                 return 0;
489         }
490         if (!is_event_win()) return 0;
492         int changemax = 1;
493         if (get_relative_cursor_x() < get_w()/2)
494                 changemax = 0;
496         // increment
497         if (get_buttonpress() == 4)
498                 mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype, 1, changemax);
500         // decrement
501         if (get_buttonpress() == 5)
502                 mwindow->change_currentautorange(mwindow->edl->local_session->zoombar_showautotype, 0, changemax);
504         mwindow->gui->zoombar->update_autozoom();
505         mwindow->gui->canvas->draw_overlays();
506         mwindow->gui->canvas->flash();
507         return 1;
510 int ZoomTextBox::handle_event()
512         float min, max;
513         if (sscanf(this->get_text(),"%f to%f",&min, &max) == 2)
514                 if (
515                         max > min
516                         && ( mwindow->edl->local_session->zoombar_showautotype != AUTOGROUPTYPE_ZOOM
517                              || (max >= 0.000 && min >= 0.000) )
518                         ) 
519                 {
520                         mwindow->edl->local_session->automation_mins[mwindow->edl->local_session->zoombar_showautotype] = min;
521                         mwindow->edl->local_session->automation_maxs[mwindow->edl->local_session->zoombar_showautotype] = max;
522                         mwindow->gui->zoombar->update_autozoom();
523                         mwindow->gui->canvas->draw_overlays();
524                         mwindow->gui->canvas->flash();
525                 }
526         // TODO: Make the text turn red when it's a bad range..
527         return 0;
534 FromTextBox::FromTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
535  : BC_TextBox(x, y, 90, 1, "")
537         this->mwindow = mwindow;
538         this->zoombar = zoombar;
541 int FromTextBox::handle_event()
543         if(get_keypress() == 13)
544         {
545                 zoombar->set_selection(SET_FROM);
546                 return 1;
547         }
548         return 0;
551 int FromTextBox::update_position(double new_position)
553         new_position += mwindow->edl->session->get_frame_offset() / 
554                                                  mwindow->edl->session->frame_rate;;
555         Units::totext(string, 
556                 new_position, 
557                 mwindow->edl->session->time_format, 
558                 mwindow->edl->session->sample_rate, 
559                 mwindow->edl->session->frame_rate,
560                 mwindow->edl->session->frames_per_foot);
561 //printf("FromTextBox::update_position %f %s\n", new_position, string);
562         update(string);
563         return 0;
571 LengthTextBox::LengthTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
572  : BC_TextBox(x, y, 90, 1, "")
574         this->mwindow = mwindow;
575         this->zoombar = zoombar;
578 int LengthTextBox::handle_event()
580         if(get_keypress() == 13)
581         {
582                 zoombar->set_selection(SET_LENGTH);
583                 return 1;
584         }
585         return 0;
588 int LengthTextBox::update_position(double new_position)
590         Units::totext(string, 
591                 new_position, 
592                 mwindow->edl->session->time_format, 
593                 mwindow->edl->session->sample_rate, 
594                 mwindow->edl->session->frame_rate,
595                 mwindow->edl->session->frames_per_foot);
596         update(string);
597         return 0;
604 ToTextBox::ToTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
605  : BC_TextBox(x, y, 90, 1, "")
607         this->mwindow = mwindow;
608         this->zoombar = zoombar;
611 int ToTextBox::handle_event()
613         if(get_keypress() == 13)
614         {
615                 zoombar->set_selection(SET_TO);
616                 return 1;
617         }
618         return 0;
621 int ToTextBox::update_position(double new_position)
623         new_position += mwindow->edl->session->get_frame_offset() /
624                                                  mwindow->edl->session->frame_rate;
625         Units::totext(string, 
626                 new_position, 
627                 mwindow->edl->session->time_format, 
628                 mwindow->edl->session->sample_rate, 
629                 mwindow->edl->session->frame_rate,
630                 mwindow->edl->session->frames_per_foot);
631         update(string);
632         return 0;