r851: Merge 2.1:
[cinelerra_cv/ct.git] / cinelerra / mtimebar.C
blob42a92c66c33c8b1e5f7557716c9305307edfd139
1 #include "bcsignals.h"
2 #include "cwindow.h"
3 #include "edl.h"
4 #include "edlsession.h"
5 #include "localsession.h"
6 #include "maincursor.h"
7 #include "mainsession.h"
8 #include "mbuttons.h"
9 #include "mtimebar.h"
10 #include "mwindowgui.h"
11 #include "mwindow.h"
12 #include "preferences.h"
13 #include "theme.h"
14 #include "trackcanvas.h"
15 #include "tracks.h"
16 #include "transportque.h"
17 #include "zoombar.h"
21 MTimeBar::MTimeBar(MWindow *mwindow, 
22         MWindowGUI *gui,
23         int x, 
24         int y,
25         int w,
26         int h)
27  : TimeBar(mwindow, gui, x, y, w, h)
29         this->gui = gui;
33 int64_t MTimeBar::position_to_pixel(double position)
35         return (int64_t)(position * 
36                 mwindow->edl->session->sample_rate / 
37                 mwindow->edl->local_session->zoom_sample - 
38                 mwindow->edl->local_session->view_start);
42 void MTimeBar::stop_playback()
44         gui->unlock_window();
45         gui->mbuttons->transport->handle_transport(STOP, 1);
46         gui->lock_window();
50 void MTimeBar::draw_time()
52         draw_range();
54 // fit the time
55         int64_t windowspan = 0;
56         int64_t timescale1 = 0;
57         int64_t timescale2 = 0;
58         int64_t sample_rate = 0;
59         int64_t sample = 0;
60         float timescale3 = 0;
61         int pixel = 0;
62         char string[BCTEXTLEN];
63 #define TIMESPACING 100
65         sample_rate = mwindow->edl->session->sample_rate;
66         windowspan = mwindow->edl->local_session->zoom_sample * get_w();
67         timescale2 = TIMESPACING * mwindow->edl->local_session->zoom_sample;
69         if(timescale2 <= sample_rate / 4) timescale1 = sample_rate / 4;
70         else
71         if(timescale2 <= sample_rate / 2) timescale1 = sample_rate / 2;
72         else
73         if(timescale2 <= sample_rate) timescale1 = sample_rate;
74         else
75         if(timescale2 <= sample_rate * 10) timescale1 = sample_rate * 10;
76         else
77         if(timescale2 <= sample_rate * 60) timescale1 = sample_rate * 60;
78         else
79         if(timescale2 <= sample_rate * 600) timescale1 = sample_rate * 600;
80         else
81         timescale1 = sample_rate * 3600;
83         for(timescale3 = timescale1; timescale3 > timescale2; timescale3 /= 2)
84                 ;
86         timescale1 = (int64_t)(timescale3 * 2);
88         sample = (int64_t)(mwindow->edl->local_session->view_start * 
89                 mwindow->edl->local_session->zoom_sample + 
90                 0.5);
92         sample /= timescale1;
93         sample *= timescale1;
94         pixel = (int64_t)(sample / 
95                 mwindow->edl->local_session->zoom_sample - 
96                 mwindow->edl->local_session->view_start);
97         sample += (int64_t) ((mwindow->edl->session->get_frame_offset() /
98                                                                 mwindow->edl->session->frame_rate) *
99                                                                 48000 /
100                                                                 timescale1 *
101                                                                 timescale1);
103         for( ; pixel < get_w(); pixel += timescale1 / mwindow->edl->local_session->zoom_sample, sample += timescale1)
104         {
105                 Units::totext(string, 
106                         sample, 
107                         sample_rate, 
108                         mwindow->edl->session->time_format, 
109                         mwindow->edl->session->frame_rate,
110                         mwindow->edl->session->frames_per_foot);
111                 set_color(get_resources()->default_text_color);
112                 set_font(MEDIUMFONT);
113                 draw_text(pixel + 4, get_text_ascent(MEDIUMFONT), string);
114                 draw_line(pixel, 3, pixel, get_h() - 4);
115         }
118 void MTimeBar::draw_range()
120         int x1 = 0, x2 = 0;
121         if(mwindow->edl->tracks->total_playable_vtracks() &&
122                 mwindow->preferences->use_brender)
123         {
124                 double time_per_pixel = (double)mwindow->edl->local_session->zoom_sample /
125                         mwindow->edl->session->sample_rate;
126                 x1 = (int)(mwindow->edl->session->brender_start / time_per_pixel) - 
127                         mwindow->edl->local_session->view_start;
128                 x2 = (int)(mwindow->session->brender_end / time_per_pixel) - 
129                         mwindow->edl->local_session->view_start;
130         }
132         if(x2 > x1 && 
133                 x1 < get_w() && 
134                 x2 > 0)
135         {
136                 draw_top_background(get_parent(), 0, 0, x1, get_h());
138                 draw_3segmenth(x1, 0, x2 - x1, mwindow->theme->get_image("timebar_brender"));
140                 draw_top_background(get_parent(), x2, 0, get_w() - x2, get_h());
141         }
142         else
143                 draw_top_background(get_parent(), 0, 0, get_w(), get_h());
144 //printf("MTimeBar::draw_range %f %f\n", mwindow->session->brender_end, time_per_pixel);
147 void MTimeBar::select_label(double position)
149         EDL *edl = mwindow->edl;
151         mwindow->gui->unlock_window();
152         mwindow->gui->mbuttons->transport->handle_transport(STOP, 1);
153         mwindow->gui->lock_window();
155         position = mwindow->edl->align_to_frame(position, 1);
157         if(shift_down())
158         {
159                 if(position > edl->local_session->get_selectionend(1) / 2 + 
160                         edl->local_session->get_selectionstart(1) / 2)
161                 {
162                 
163                         edl->local_session->set_selectionend(position);
164                 }
165                 else
166                 {
167                         edl->local_session->set_selectionstart(position);
168                 }
169         }
170         else
171         {
172                 edl->local_session->set_selectionstart(position);
173                 edl->local_session->set_selectionend(position);
174         }
176 // Que the CWindow
177         mwindow->cwindow->update(1, 0, 0);
178         mwindow->gui->cursor->hide(0);
179         mwindow->gui->cursor->draw(1);
180         mwindow->gui->canvas->activate();
181         mwindow->gui->zoombar->update();
182         update_highlights();
183         mwindow->gui->canvas->flash();
187 int MTimeBar::resize_event()
189         reposition_window(mwindow->theme->mtimebar_x,
190                 mwindow->theme->mtimebar_y,
191                 mwindow->theme->mtimebar_w,
192                 mwindow->theme->mtimebar_h);
193         update();
194         return 1;
197 int MTimeBar::test_preview(int buttonpress)
199         int result = 0;
200         return result;