r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / zoombar.C
blob66ebbe15fe008e283e368504ddf58eb26edbe396
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 #define DEFAULT_TEXT "000.00 to 000.00"
63         add_subwindow(auto_zoom_text = new ZoomTextBox(
64                 mwindow, 
65                 this, 
66                 x, 
67                 y,
68                 DEFAULT_TEXT));
69         x += auto_zoom_text->get_w() + 5;
70         add_subwindow(auto_zoom = new AutoZoom(mwindow, this, x, y));
71         update_autozoom();
72         x += auto_zoom->get_w() + 5;
74         add_subwindow(from_value = new FromTextBox(mwindow, this, x, y));
75         x += from_value->get_w() + 5;
76         add_subwindow(length_value = new LengthTextBox(mwindow, this, x, y));
77         x += length_value->get_w() + 5;
78         add_subwindow(to_value = new ToTextBox(mwindow, this, x, y));
79         x += to_value->get_w() + 5;
81         update_formatting(from_value);
82         update_formatting(length_value);
83         update_formatting(to_value);
85         add_subwindow(playback_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, RED));
87         add_subwindow(zoom_value = new BC_Title(x, 100, _("--"), MEDIUMFONT, BLACK));
88         update();
89         return 0;
93 void ZoomBar::update_formatting(BC_TextBox *dst)
95         dst->set_separators(
96                 Units::format_to_separators(mwindow->edl->session->time_format));
99 void ZoomBar::resize_event()
101         reposition_window(mwindow->theme->mzoom_x,
102                 mwindow->theme->mzoom_y,
103                 mwindow->theme->mzoom_w,
104                 mwindow->theme->mzoom_h);
106         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
107 //      int x = 3, y = 1;
108 //      sample_zoom->reposition_window(x, y);
109 //      x += sample_zoom->get_w();
110 //      amp_zoom->reposition_window(x, y);
111 //      x += amp_zoom->get_w();
112 //      track_zoom->reposition_window(x, y);
113         flash();
116 void ZoomBar::redraw_time_dependancies()
118 // Recalculate sample zoom menu
119         sample_zoom->update_menu();
120         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
121         update_formatting(from_value);
122         update_formatting(length_value);
123         update_formatting(to_value);
124         update_autozoom();
125         update_clocks();
128 int ZoomBar::draw()
130         update();
131         return 0;
134 void ZoomBar::update_autozoom()
136         char string[BCTEXTLEN];
137         sprintf(string, "%0.02f to %0.02f\n", 
138                 mwindow->edl->local_session->automation_min, 
139                 mwindow->edl->local_session->automation_max);
140         auto_zoom_text->update(string);
143 int ZoomBar::update()
145         sample_zoom->update(mwindow->edl->local_session->zoom_sample);
146         amp_zoom->update(mwindow->edl->local_session->zoom_y);
147         track_zoom->update(mwindow->edl->local_session->zoom_track);
148         update_autozoom();
149         update_clocks();
150         return 0;
153 int ZoomBar::update_clocks()
155         from_value->update_position(mwindow->edl->local_session->get_selectionstart(1));
156         length_value->update_position(mwindow->edl->local_session->get_selectionend(1) - 
157                 mwindow->edl->local_session->get_selectionstart(1));
158         to_value->update_position(mwindow->edl->local_session->get_selectionend(1));
159         return 0;
162 int ZoomBar::update_playback(int64_t new_position)
164         if(new_position != old_position)
165         {
166                 Units::totext(string, 
167                                 new_position, 
168                                 mwindow->edl->session->sample_rate, 
169                                 mwindow->edl->session->time_format, 
170                                 mwindow->edl->session->frame_rate,
171                                 mwindow->edl->session->frames_per_foot);
172                 playback_value->update(string);
173                 old_position = new_position;
174         }
175         return 0;
178 int ZoomBar::resize_event(int w, int h)
180 // don't change anything but y and width
181         reposition_window(0, h - this->get_h(), w, this->get_h());
182         return 0;
186 // Values for which_one
187 #define SET_FROM 1
188 #define SET_LENGTH 2
189 #define SET_TO 3
192 int ZoomBar::set_selection(int which_one)
194         double start_position = mwindow->edl->local_session->get_selectionstart(1);
195         double end_position = mwindow->edl->local_session->get_selectionend(1);
196         double length = end_position - start_position;
198 // Fix bogus results
200         switch(which_one)
201         {
202                 case SET_LENGTH:
203                         start_position = Units::text_to_seconds(from_value->get_text(), 
204                                 mwindow->edl->session->sample_rate, 
205                                 mwindow->edl->session->time_format, 
206                                 mwindow->edl->session->frame_rate,
207                                 mwindow->edl->session->frames_per_foot);
208                         length = Units::text_to_seconds(length_value->get_text(), 
209                                 mwindow->edl->session->sample_rate, 
210                                 mwindow->edl->session->time_format, 
211                                 mwindow->edl->session->frame_rate,
212                                 mwindow->edl->session->frames_per_foot);
213                         end_position = start_position + length;
215                         if(end_position < start_position)
216                         {
217                                 start_position = end_position;
218                                 mwindow->edl->local_session->set_selectionend(
219                                         mwindow->edl->local_session->get_selectionstart(1));
220                         }
221                         break;
223                 case SET_FROM:
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                         end_position = Units::text_to_seconds(to_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);
235                         if(end_position < start_position)
236                         {
237                                 end_position = start_position;
238                                 mwindow->edl->local_session->set_selectionend(
239                                         mwindow->edl->local_session->get_selectionstart(1));
240                         }
241                         break;
243                 case SET_TO:
244                         start_position = Units::text_to_seconds(from_value->get_text(), 
245                                 mwindow->edl->session->sample_rate, 
246                                 mwindow->edl->session->time_format, 
247                                 mwindow->edl->session->frame_rate,
248                                 mwindow->edl->session->frames_per_foot);
249                         end_position = Units::text_to_seconds(to_value->get_text(), 
250                                 mwindow->edl->session->sample_rate, 
251                                 mwindow->edl->session->time_format, 
252                                 mwindow->edl->session->frame_rate,
253                                 mwindow->edl->session->frames_per_foot);
255                         if(end_position < start_position)
256                         {
257                                 start_position = end_position;
258                                 mwindow->edl->local_session->set_selectionend(
259                                         mwindow->edl->local_session->get_selectionstart(1));
260                         }
261                         break;
262         }
264         mwindow->edl->local_session->set_selectionstart(
265                 mwindow->edl->align_to_frame(start_position, 1));
266         mwindow->edl->local_session->set_selectionend(
267                 mwindow->edl->align_to_frame(end_position, 1));
270         mwindow->gui->timebar->update_highlights();
271         mwindow->gui->cursor->hide();
272         mwindow->gui->cursor->show();
273         update();
274         mwindow->sync_parameters(CHANGE_PARAMS);
275         mwindow->gui->canvas->flash();
277         return 0;
291 SampleZoomPanel::SampleZoomPanel(MWindow *mwindow, 
292         ZoomBar *zoombar, 
293         int x, 
294         int y)
295  : ZoomPanel(mwindow, 
296         zoombar, 
297         mwindow->edl->local_session->zoom_sample, 
298         x, 
299         y, 
300         110, 
301         MIN_ZOOM_TIME, 
302         MAX_ZOOM_TIME, 
303         ZOOM_TIME)
305         this->mwindow = mwindow;
306         this->zoombar = zoombar;
308 int SampleZoomPanel::handle_event()
310         mwindow->zoom_sample((int64_t)get_value());
311         return 1;
324 AmpZoomPanel::AmpZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
325  : ZoomPanel(mwindow, 
326         zoombar, 
327         mwindow->edl->local_session->zoom_y, 
328         x, 
329         y, 
330         80,
331         MIN_AMP_ZOOM, 
332         MAX_AMP_ZOOM, 
333         ZOOM_LONG)
335         this->mwindow = mwindow;
336         this->zoombar = zoombar;
338 int AmpZoomPanel::handle_event()
340         mwindow->zoom_amp((int64_t)get_value());
341         return 1;
344 TrackZoomPanel::TrackZoomPanel(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
345  : ZoomPanel(mwindow, 
346         zoombar, 
347         mwindow->edl->local_session->zoom_track, 
348         x, 
349         y, 
350         70,
351         MIN_TRACK_ZOOM, 
352         MAX_TRACK_ZOOM, 
353         ZOOM_LONG)
355         this->mwindow = mwindow;
356         this->zoombar = zoombar;
358 int TrackZoomPanel::handle_event()
360         mwindow->zoom_track((int64_t)get_value());
361         zoombar->amp_zoom->update(mwindow->edl->local_session->zoom_y);
362         return 1;
368 AutoZoom::AutoZoom(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
369  : BC_Tumbler(x,
370         y,
371         mwindow->theme->get_image_set("zoombar_tumbler"))
373         this->mwindow = mwindow;
374         this->zoombar = zoombar;
377 int AutoZoom::handle_up_event()
379         mwindow->expand_autos();
380         return 1;
383 int AutoZoom::handle_down_event()
385         mwindow->shrink_autos();
386         return 1;
391 ZoomTextBox::ZoomTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y, char *text)
392  : BC_TextBox(x, y, 130, 1, text)
394         this->mwindow = mwindow;
395         this->zoombar = zoombar;
398 int ZoomTextBox::handle_event()
400         float min, max;
401         if (sscanf(this->get_text(),"%f to%f",&min, &max) == 2)
402                 if (max > min) {
403                         mwindow->edl->local_session->automation_min = min;
404                         mwindow->edl->local_session->automation_max = max;
405                         mwindow->gui->zoombar->update_autozoom();
406                         mwindow->gui->canvas->draw_overlays();
407                         mwindow->gui->canvas->flash();
408                 }
409         return 0;
416 FromTextBox::FromTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
417  : BC_TextBox(x, y, 90, 1, "")
419         this->mwindow = mwindow;
420         this->zoombar = zoombar;
423 int FromTextBox::handle_event()
425         if(get_keypress() == 13)
426         {
427                 zoombar->set_selection(SET_FROM);
428                 return 1;
429         }
430         return 0;
433 int FromTextBox::update_position(double new_position)
435         new_position += mwindow->edl->session->get_frame_offset() / 
436                                                  mwindow->edl->session->frame_rate;;
437         Units::totext(string, 
438                 new_position, 
439                 mwindow->edl->session->time_format, 
440                 mwindow->edl->session->sample_rate, 
441                 mwindow->edl->session->frame_rate,
442                 mwindow->edl->session->frames_per_foot);
443 //printf("FromTextBox::update_position %f %s\n", new_position, string);
444         update(string);
445         return 0;
453 LengthTextBox::LengthTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
454  : BC_TextBox(x, y, 90, 1, "")
456         this->mwindow = mwindow;
457         this->zoombar = zoombar;
460 int LengthTextBox::handle_event()
462         if(get_keypress() == 13)
463         {
464                 zoombar->set_selection(SET_LENGTH);
465                 return 1;
466         }
467         return 0;
470 int LengthTextBox::update_position(double new_position)
472         Units::totext(string, 
473                 new_position, 
474                 mwindow->edl->session->time_format, 
475                 mwindow->edl->session->sample_rate, 
476                 mwindow->edl->session->frame_rate,
477                 mwindow->edl->session->frames_per_foot);
478         update(string);
479         return 0;
486 ToTextBox::ToTextBox(MWindow *mwindow, ZoomBar *zoombar, int x, int y)
487  : BC_TextBox(x, y, 90, 1, "")
489         this->mwindow = mwindow;
490         this->zoombar = zoombar;
493 int ToTextBox::handle_event()
495         if(get_keypress() == 13)
496         {
497                 zoombar->set_selection(SET_TO);
498                 return 1;
499         }
500         return 0;
503 int ToTextBox::update_position(double new_position)
505         new_position += mwindow->edl->session->get_frame_offset() /
506                                                  mwindow->edl->session->frame_rate;
507         Units::totext(string, 
508                 new_position, 
509                 mwindow->edl->session->time_format, 
510                 mwindow->edl->session->sample_rate, 
511                 mwindow->edl->session->frame_rate,
512                 mwindow->edl->session->frames_per_foot);
513         update(string);
514         return 0;