initial commit
[goodguy/history.git] / cinelerra-5.0 / 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(_("sample zoom"));
74         x += sample_zoom->get_w();
75         amp_zoom = new AmpZoomPanel(mwindow, this, x, y);
76         amp_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
77         amp_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
78         amp_zoom->create_objects();
79         amp_zoom->zoom_text->set_tooltip(_("amp zoom"));
80         x += amp_zoom->get_w();
81         track_zoom = new TrackZoomPanel(mwindow, this, x, y);
82         track_zoom->set_menu_images(mwindow->theme->get_image_set("zoombar_menu", 0));
83         track_zoom->set_tumbler_images(mwindow->theme->get_image_set("zoombar_tumbler", 0));
84         track_zoom->create_objects();
85         track_zoom->zoom_text->set_tooltip(_("track zoom"));
86         x += track_zoom->get_w() + 10;
87
88 #define DEFAULT_TEXT "000.00 - 000.00"
89         add_subwindow(auto_zoom_popup = new AutoZoomPopup(
90                 mwindow, 
91                 this, 
92                 x, 
93                 y,
94                 get_text_width(MEDIUMFONT, DEFAULT_TEXT) + 20));
95         auto_zoom_popup->create_objects();
96         auto_zoom_popup->set_tooltip(_("curve zoom (autofit Alt f)"));
97         x += auto_zoom_popup->get_w() + 5;
98 //      add_subwindow(auto_zoom_text = new BC_Title(
99 //              x, 
100 //              get_h() / 2 - BC_Title::calculate_h(this, "0") / 2, 
101 //              DEFAULT_TEXT));
102 //      x += auto_zoom_text->get_w() + 5;
103         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y));
104         update_autozoom();
105         x += auto_zoom->get_w() + 5;
106
107         add_subwindow(from_value = new FromTextBox(mwindow, this, x, y));
108         x += from_value->get_w() + 5;
109         add_subwindow(length_value = new LengthTextBox(mwindow, this, x, y));
110         x += length_value->get_w() + 5;
111         add_subwindow(to_value = new ToTextBox(mwindow, this, x, y));
112         x += to_value->get_w() + 5;
113
114         update_formatting(from_value);
115         update_formatting(length_value);
116         update_formatting(to_value);
117
118         add_subwindow(playback_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, RED));
119
120         add_subwindow(zoom_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, BLACK));
121         update();
122 }
123
124
125 void ZoomBar::update_formatting(BC_TextBox *dst)
126 {
127         dst->set_separators(
128                 Units::format_to_separators(mwindow->edl->session->time_format));
129 }
130
131 void ZoomBar::resize_event()
132 {
133         hide_window(0);
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 //      sample_zoom->reposition_window(x, y);
142 //      x += sample_zoom->get_w();
143 //      amp_zoom->reposition_window(x, y);
144 //      x += amp_zoom->get_w();
145 //      track_zoom->reposition_window(x, y);
146         flash(0);
147         show_window(0);
148 }
149
150 void ZoomBar::redraw_time_dependancies()
151 {
152 // Recalculate sample zoom menu
153         sample_zoom->update_menu();
154         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
155         update_formatting(from_value);
156         update_formatting(length_value);
157         update_formatting(to_value);
158         update_autozoom();
159         update_clocks();
160 }
161
162 int ZoomBar::draw()
163 {
164         update();
165         return 0;
166 }
167
168 void ZoomBar::update_autozoom()
169 {
170         char string[BCTEXTLEN];
171         sprintf(string, "%0.02f - %0.02f\n", 
172                 mwindow->edl->local_session->automation_min, 
173                 mwindow->edl->local_session->automation_max);
174 //      auto_zoom_text->update(string);
175         auto_zoom_popup->set_text(string);
176 }
177
178 int ZoomBar::update()
179 {
180         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
181         amp_zoom->update(mwindow->edl->local_session->zoom_y);
182         track_zoom->update(mwindow->edl->local_session->zoom_track);
183         update_autozoom();
184         update_clocks();
185         return 0;
186 }
187
188 int ZoomBar::update_clocks()
189 {
190         from_value->update_position(mwindow->edl->local_session->get_selectionstart(1));
191         length_value->update_position(mwindow->edl->local_session->get_selectionend(1) - 
192                 mwindow->edl->local_session->get_selectionstart(1));
193         to_value->update_position(mwindow->edl->local_session->get_selectionend(1));
194         return 0;
195 }
196
197 int ZoomBar::update_playback(int64_t new_position)
198 {
199         if(new_position != old_position)
200         {
201                 Units::totext(string, 
202                                 new_position, 
203                                 mwindow->edl->session->sample_rate, 
204                                 mwindow->edl->session->time_format, 
205                                 mwindow->edl->session->frame_rate,
206                                 mwindow->edl->session->frames_per_foot);
207                 playback_value->update(string);
208                 old_position = new_position;
209         }
210         return 0;
211 }
212
213 int ZoomBar::resize_event(int w, int h)
214 {
215 // don't change anything but y and width
216         reposition_window(0, h - this->get_h(), w, this->get_h());
217         return 0;
218 }
219
220
221 // Values for which_one
222 #define SET_FROM 1
223 #define SET_LENGTH 2
224 #define SET_TO 3
225
226
227 int ZoomBar::set_selection(int which_one)
228 {
229         double start_position = mwindow->edl->local_session->get_selectionstart(1);
230         double end_position = mwindow->edl->local_session->get_selectionend(1);
231         double length = end_position - start_position;
232
233 // Fix bogus results
234
235         switch(which_one)
236         {
237                 case SET_LENGTH:
238                         start_position = Units::text_to_seconds(from_value->get_text(), 
239                                 mwindow->edl->session->sample_rate, 
240                                 mwindow->edl->session->time_format, 
241                                 mwindow->edl->session->frame_rate,
242                                 mwindow->edl->session->frames_per_foot);
243                         length = Units::text_to_seconds(length_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 = start_position + length;
249
250                         if(end_position < start_position)
251                         {
252                                 start_position = end_position;
253                                 mwindow->edl->local_session->set_selectionend(
254                                         mwindow->edl->local_session->get_selectionstart(1));
255                         }
256                         break;
257
258                 case SET_FROM:
259                         start_position = Units::text_to_seconds(from_value->get_text(), 
260                                 mwindow->edl->session->sample_rate, 
261                                 mwindow->edl->session->time_format, 
262                                 mwindow->edl->session->frame_rate,
263                                 mwindow->edl->session->frames_per_foot);
264                         end_position = Units::text_to_seconds(to_value->get_text(), 
265                                 mwindow->edl->session->sample_rate, 
266                                 mwindow->edl->session->time_format, 
267                                 mwindow->edl->session->frame_rate,
268                                 mwindow->edl->session->frames_per_foot);
269
270                         if(end_position < start_position)
271                         {
272                                 end_position = start_position;
273                                 mwindow->edl->local_session->set_selectionend(
274                                         mwindow->edl->local_session->get_selectionstart(1));
275                         }
276                         break;
277
278                 case SET_TO:
279                         start_position = Units::text_to_seconds(from_value->get_text(), 
280                                 mwindow->edl->session->sample_rate, 
281                                 mwindow->edl->session->time_format, 
282                                 mwindow->edl->session->frame_rate,
283                                 mwindow->edl->session->frames_per_foot);
284                         end_position = Units::text_to_seconds(to_value->get_text(), 
285                                 mwindow->edl->session->sample_rate, 
286                                 mwindow->edl->session->time_format, 
287                                 mwindow->edl->session->frame_rate,
288                                 mwindow->edl->session->frames_per_foot);
289
290                         if(end_position < start_position)
291                         {
292                                 start_position = end_position;
293                                 mwindow->edl->local_session->set_selectionend(
294                                         mwindow->edl->local_session->get_selectionstart(1));
295                         }
296                         break;
297         }
298
299         mwindow->edl->local_session->set_selectionstart(
300                 mwindow->edl->align_to_frame(start_position, 1));
301         mwindow->edl->local_session->set_selectionend(
302                 mwindow->edl->align_to_frame(end_position, 1));
303
304
305         mwindow->gui->update_timebar_highlights();
306         mwindow->gui->hide_cursor(1);
307         mwindow->gui->show_cursor(1);
308         update();
309         mwindow->sync_parameters(CHANGE_PARAMS);
310         mwindow->gui->flash_canvas(1);
311
312         return 0;
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326 SampleZoomPanel::SampleZoomPanel(MWindow *mwindow, 
327         ZoomBar *zoombar, 
328         int x, 
329         int y)
330  : ZoomPanel(mwindow, 
331         zoombar, 
332         mwindow->edl->local_session->zoom_sample, 
333         x, 
334         y, 
335         110, 
336         MIN_ZOOM_TIME, 
337         MAX_ZOOM_TIME, 
338         ZOOM_TIME)
339 {
340         this->mwindow = mwindow;
341         this->zoombar = zoombar;
342 }
343 int SampleZoomPanel::handle_event()
344 {
345         mwindow->zoom_sample((int64_t)get_value());
346         return 1;
347 }
348
349
350
351
352
353
354
355
356
357
358
359 AmpZoomPanel::AmpZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
360  : ZoomPanel(mwindow, 
361         zoombar, 
362         mwindow->edl->local_session->zoom_y, 
363         x, 
364         y, 
365         80,
366         MIN_AMP_ZOOM, 
367         MAX_AMP_ZOOM, 
368         ZOOM_LONG)
369 {
370         this->mwindow = mwindow;
371         this->zoombar = zoombar;
372 }
373 int AmpZoomPanel::handle_event()
374 {
375         mwindow->zoom_amp((int64_t)get_value());
376         return 1;
377 }
378
379 TrackZoomPanel::TrackZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
380  : ZoomPanel(mwindow, 
381         zoombar, 
382         mwindow->edl->local_session->zoom_track, 
383         x, 
384         y, 
385         70,
386         MIN_TRACK_ZOOM, 
387         MAX_TRACK_ZOOM, 
388         ZOOM_LONG)
389 {
390         this->mwindow = mwindow;
391         this->zoombar = zoombar;
392 }
393 int TrackZoomPanel::handle_event()
394 {
395         mwindow->zoom_track((int64_t)get_value());
396         zoombar->amp_zoom->update(mwindow->edl->local_session->zoom_y);
397         return 1;
398 }
399
400
401
402
403 AutoZoom::AutoZoom(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
404  : BC_Tumbler(x,
405         y,
406         mwindow->theme->get_image_set("zoombar_tumbler"))
407 {
408         this->mwindow = mwindow;
409         this->zoombar = zoombar;
410 }
411
412 int AutoZoom::handle_up_event()
413 {
414         mwindow->expand_autos();
415         return 1;
416 }
417
418 int AutoZoom::handle_down_event()
419 {
420         mwindow->shrink_autos();
421         return 1;
422 }
423
424
425
426
427
428
429
430 AutoZoomPopup::AutoZoomPopup(MWindow *mwindow, 
431         ZoomBar *zoombar, 
432         int x, 
433         int y,
434         int w)
435  : BC_PopupMenu(x,
436         y,
437         w,
438         "",
439         1,
440         mwindow->theme->get_image_set("zoombar_menu", 0))
441 {
442         this->mwindow = mwindow;
443         this->zoombar = zoombar;
444 }
445
446
447 static const struct {
448         int min, max;
449 } auto_zooms[] = {
450         { 0, 1000, },
451         { 0, 100, },
452         { 0, 10, },
453         { 0, 1, },
454         { -1, 1, },
455         { -5, 5, },
456         { INFINITYGAIN, MAX_AUDIO_FADE, },
457 };
458
459 void AutoZoomPopup::create_objects()
460 {
461         char string[BCTEXTLEN];
462         int n = sizeof(auto_zooms)/sizeof(auto_zooms[0]);
463         for( int i=0; i<n; ++i ) {
464                 sprintf(string, "%d - %d", auto_zooms[i].min, auto_zooms[i].max);
465                 add_item(new BC_MenuItem(string));
466         }
467 }
468
469 int AutoZoomPopup::handle_event()
470 {
471         const char *cp = get_text();
472         int i = total_items();
473         while( --i >= 0 && strcmp(cp, get_item(i)->get_text()) );
474         if( i >= 0 )
475                 mwindow->zoom_autos(auto_zooms[i].min, auto_zooms[i].max);
476         return 1;
477 }
478
479
480
481
482 FromTextBox::FromTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
483  : BC_TextBox(x, y, 90, 1, "")
484 {
485         this->mwindow = mwindow;
486         this->zoombar = zoombar;
487 }
488
489 int FromTextBox::handle_event()
490 {
491         if(get_keypress() == 13)
492         {
493                 zoombar->set_selection(SET_FROM);
494                 return 1;
495         }
496         return 0;
497 }
498
499 int FromTextBox::update_position(double new_position)
500 {
501         Units::totext(string, 
502                 new_position, 
503                 mwindow->edl->session->time_format, 
504                 mwindow->edl->session->sample_rate, 
505                 mwindow->edl->session->frame_rate,
506                 mwindow->edl->session->frames_per_foot);
507 //printf("FromTextBox::update_position %f %s\n", new_position, string);
508         update(string);
509         return 0;
510 }
511
512
513
514
515
516
517 LengthTextBox::LengthTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
518  : BC_TextBox(x, y, 90, 1, "")
519 {
520         this->mwindow = mwindow;
521         this->zoombar = zoombar;
522 }
523
524 int LengthTextBox::handle_event()
525 {
526         if(get_keypress() == 13)
527         {
528                 zoombar->set_selection(SET_LENGTH);
529                 return 1;
530         }
531         return 0;
532 }
533
534 int LengthTextBox::update_position(double new_position)
535 {
536         Units::totext(string, 
537                 new_position, 
538                 mwindow->edl->session->time_format, 
539                 mwindow->edl->session->sample_rate, 
540                 mwindow->edl->session->frame_rate,
541                 mwindow->edl->session->frames_per_foot);
542         update(string);
543         return 0;
544 }
545
546
547
548
549
550 ToTextBox::ToTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
551  : BC_TextBox(x, y, 90, 1, "")
552 {
553         this->mwindow = mwindow;
554         this->zoombar = zoombar;
555 }
556
557 int ToTextBox::handle_event()
558 {
559         if(get_keypress() == 13)
560         {
561                 zoombar->set_selection(SET_TO);
562                 return 1;
563         }
564         return 0;
565 }
566
567 int ToTextBox::update_position(double new_position)
568 {
569         Units::totext(string, 
570                 new_position, 
571                 mwindow->edl->session->time_format, 
572                 mwindow->edl->session->sample_rate, 
573                 mwindow->edl->session->frame_rate,
574                 mwindow->edl->session->frames_per_foot);
575         update(string);
576         return 0;
577 }