r1014: Enable horizontal scrolling with the mouse wheel by pressing Ctrl.
[cinelerra_cv/ct.git] / cinelerra / vwindow.C
blobfc21dab5e1dceb0ad5b69f36ee638c70c63c6044
1 #include "asset.h"
2 #include "assets.h"
3 #include "clipedit.h"
4 #include "bchash.h"
5 #include "edl.h"
6 #include "edlsession.h"
7 #include "filesystem.h"
8 #include "filexml.h"
9 #include "language.h"
10 #include "localsession.h"
11 #include "mainclock.h"
12 #include "mwindow.h"
13 #include "mwindowgui.h"
14 #include "playbackengine.h"
15 #include "tracks.h"
16 #include "transportque.h"
17 #include "vplayback.h"
18 #include "vtimebar.h"
19 #include "vtracking.h"
20 #include "vwindow.h"
21 #include "vwindowgui.h"
24 VWindow::VWindow(MWindow *mwindow) : Thread()
26         this->mwindow = mwindow;
27         asset = 0;
31 VWindow::~VWindow()
33 //printf("VWindow::~VWindow 1\n");
34         delete playback_engine;
35 //printf("VWindow::~VWindow 1\n");
36         delete playback_cursor;
37         delete_edl();
38         delete clip_edit;
39 //printf("VWindow::~VWindow 2\n");
42 void VWindow::delete_edl()
44 //printf("VWindow::delete_edl 1\n");
45         if(mwindow->edl->vwindow_edl && !mwindow->edl->vwindow_edl_shared)
46         {
47                 delete mwindow->edl->vwindow_edl;
48                 mwindow->edl->vwindow_edl = 0;
49                 mwindow->edl->vwindow_edl_shared = 0;
50         }
52         if(asset) Garbage::delete_object(asset);
53         asset = 0;
57 void VWindow::load_defaults()
61 int VWindow::create_objects()
63 //printf("VWindow::create_objects 1\n");
64         gui = new VWindowGUI(mwindow, this);
65 //printf("VWindow::create_objects 1\n");
66         gui->create_objects();
67 //printf("VWindow::create_objects 1\n");
69         playback_engine = new VPlayback(mwindow, this, gui->canvas);
70 //printf("VWindow::create_objects 1\n");
72 // Start command loop
73         playback_engine->create_objects();
74 //printf("VWindow::create_objects 1\n");
75         gui->transport->set_engine(playback_engine);
76 //printf("VWindow::create_objects 1\n");
77         playback_cursor = new VTracking(mwindow, this);
78 //printf("VWindow::create_objects 1\n");
79         playback_cursor->create_objects();
80 //printf("VWindow::create_objects 2\n");
82         clip_edit = new ClipEdit(mwindow, 0, this);
83         return 0;
86 void VWindow::run()
88         gui->run_window();
91 EDL* VWindow::get_edl()
93 //printf("VWindow::get_edl 1 %p\n", edl);
94         return mwindow->edl->vwindow_edl;
97 Asset* VWindow::get_asset()
99         return this->asset;
102 void VWindow::change_source()
104 //printf("VWindow::change_source() 1 %p\n", mwindow->edl->vwindow_edl);
105         if(mwindow->edl->vwindow_edl)
106         {
107                 gui->change_source(get_edl(), get_edl()->local_session->clip_title);
108                 update_position(CHANGE_ALL, 1, 1);
109         }
110         else
111         {
112                 if(asset) Garbage::delete_object(asset);
113                 asset = 0;
114                 mwindow->edl->vwindow_edl_shared = 0;
115         }
118 void VWindow::change_source(Asset *asset)
120 //printf("VWindow::change_source 1\n");
121 //      if(asset && this->asset &&
122 //              asset->id == this->asset->id &&
123 //              asset == this->asset) return;
125 //printf("VWindow::change_source(Asset *asset) 1\n");
127         char title[BCTEXTLEN];
128         FileSystem fs;
129         fs.extract_name(title, asset->path);
130 //printf("VWindow::change_source 1\n");
132         delete_edl();
133 //printf("VWindow::change_source 1\n");
135 // Generate EDL off of main EDL for cutting
136         this->asset = new Asset;
137         *this->asset = *asset;
138         mwindow->edl->vwindow_edl = new EDL(mwindow->edl);
139         mwindow->edl->vwindow_edl_shared = 0;
140         mwindow->edl->vwindow_edl->create_objects();
141         mwindow->asset_to_edl(mwindow->edl->vwindow_edl, asset);
142 //printf("VWindow::change_source 1 %d %d\n", edl->local_session->loop_playback, mwindow->edl->local_session->loop_playback);
143 //edl->dump();
145 // Update GUI
146         gui->change_source(mwindow->edl->vwindow_edl, title);
147         update_position(CHANGE_ALL, 1, 1);
150 // Update master session
151         strcpy(mwindow->edl->session->vwindow_folder, MEDIA_FOLDER);
152         mwindow->edl->session->vwindow_source = 0;
153         int i = 0;
154         for(Asset *current = mwindow->edl->assets->first; 
155                 current;
156                 current = NEXT)
157         {
158                 if(this->asset->equivalent(*current, 0, 0))
159                 {
160                         mwindow->edl->session->vwindow_source = i;
161                         break;
162                 }
163                 i++;
164         }
166 //printf("VWindow::change_source 2\n");
169 void VWindow::change_source(EDL *edl)
171 //printf("VWindow::change_source(EDL *edl) 1\n");
172 //printf("VWindow::change_source %p\n", edl);
173 // EDLs are identical
174         if(edl && mwindow->edl->vwindow_edl && 
175                 edl->id == mwindow->edl->vwindow_edl->id) return;
177         delete_edl();
179         if(edl)
180         {
181                 this->asset = 0;
182                 mwindow->edl->vwindow_edl = edl;
183 // in order not to later delete edl if it is shared
184                 mwindow->edl->vwindow_edl_shared = 1;
186 // Update GUI
187                 gui->change_source(edl, edl->local_session->clip_title);
188                 update_position(CHANGE_ALL, 1, 1);
190 // Update master session
191                 strcpy(mwindow->edl->session->vwindow_folder, CLIP_FOLDER);
192                 mwindow->edl->session->vwindow_source = 
193                         mwindow->edl->clips.number_of(edl);
194         }
195         else
196                 gui->change_source(edl, _("Viewer"));
200 void VWindow::remove_source()
202         delete_edl();
203         gui->change_source(0, _("Viewer"));
206 void VWindow::change_source(char *folder, int item)
208 //printf("VWindow::change_source(char *folder, int item) 1\n");
209         int result = 0;
210 // Search EDLs
211         if(!strcasecmp(folder, CLIP_FOLDER))
212         {
213                 if(item < mwindow->edl->clips.total)
214                 {
215                         change_source(mwindow->edl->clips.values[item]);
216                         result = 1;
217                 }
218         }
219         else
220 // Search media
221         if(!strcasecmp(folder, MEDIA_FOLDER))
222         {
223                 if(item < mwindow->edl->assets->total())
224                 {
225                         change_source(mwindow->edl->assets->get_item_number(item));
226                         result = 1;
227                 }
228         }
229         else
230 // Search extra clip folders
231         {
232         }
233         
234         if(!result)
235         {
236                 remove_source();
237         }
243 void VWindow::goto_start()
245         if(get_edl())
246         {
247                 get_edl()->local_session->set_selectionstart(0);
248                 get_edl()->local_session->set_selectionend(0);
249                 update_position(CHANGE_NONE, 
250                         0, 
251                         1);
252         }
255 void VWindow::goto_end()
257         if(get_edl())
258         {
259                 double position = get_edl()->tracks->total_length();
260                 get_edl()->local_session->set_selectionstart(position);
261                 get_edl()->local_session->set_selectionend(position);
262                 update_position(CHANGE_NONE, 
263                         0, 
264                         1);
265         }
268 void VWindow::update(int do_timebar)
270         if(do_timebar)
271                 gui->timebar->update();
274 void VWindow::update_position(int change_type, 
275         int use_slider, 
276         int update_slider)
278         EDL *edl = get_edl();
279         if(edl)
280         {
281                 Asset *asset = edl->assets->first;
282                 if(use_slider) 
283                 {
284                         edl->local_session->set_selectionstart(gui->slider->get_value());
285                         edl->local_session->set_selectionend(gui->slider->get_value());
286                 }
288                 if(update_slider)
289                 {
290                         gui->slider->set_position();
291                 }
293                 playback_engine->que->send_command(CURRENT_FRAME, 
294                         change_type,
295                         edl,
296                         1);
298                 gui->clock->update(edl->local_session->get_selectionstart(1) +
299                         asset->tcstart / 
300                         (asset->video_data ? asset->frame_rate : asset->sample_rate));
301         }
304 void VWindow::set_inpoint()
306         EDL *edl = get_edl();
307         if(edl)
308         {
309                 edl->set_inpoint(edl->local_session->get_selectionstart(1));
310                 gui->timebar->update();
311         }
314 void VWindow::set_outpoint()
316         EDL *edl = get_edl();
317         if(edl)
318         {
319                 edl->set_outpoint(edl->local_session->get_selectionstart(1));
320                 gui->timebar->update();
321         }
324 void VWindow::clear_inpoint()
326         EDL *edl = get_edl();
327         if(edl)
328         {
329                 edl->local_session->unset_inpoint();
330                 gui->timebar->update();
331         }
334 void VWindow::clear_outpoint()
336         EDL *edl = get_edl();
337         if(edl)
338         {
339                 edl->local_session->unset_outpoint();
340                 gui->timebar->update();
341         }
344 void VWindow::copy()
346         EDL *edl = get_edl();
347         if(edl)
348         {
349                 double start = edl->local_session->get_selectionstart();
350                 double end = edl->local_session->get_selectionend();
351                 FileXML file;
352                 edl->copy(start,
353                         end,
354                         0,
355                         0,
356                         0,
357                         &file,
358                         mwindow->plugindb,
359                         "",
360                         1);
361                 mwindow->gui->lock_window();
362                 mwindow->gui->get_clipboard()->to_clipboard(file.string,
363                         strlen(file.string),
364                         SECONDARY_SELECTION);
365                 mwindow->gui->unlock_window();
366         }
369 void VWindow::splice_selection()
373 void VWindow::overwrite_selection()