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