improve delays created by vicon drawing locks, reset_cache segv fix, gang track toolt...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / mwindowmove.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 "automation.h"
23 #include "autos.h"
24 #include "clip.h"
25 #include "cplayback.h"
26 #include "cwindow.h"
27 #include "cwindowgui.h"
28 #include "edits.h"
29 #include "edl.h"
30 #include "edlsession.h"
31 #include "keyframe.h"
32 #include "labels.h"
33 #include "localsession.h"
34 #include "maincursor.h"
35 #include "mainmenu.h"
36 #include "mainsession.h"
37 #include "mtimebar.h"
38 #include "mwindow.h"
39 #include "mwindowgui.h"
40 #include "patchbay.h"
41 #include "playbackengine.h"
42 #include "plugin.h"
43 #include "pluginset.h"
44 #include "resourcethread.h"
45 #include "samplescroll.h"
46 #include "theme.h"
47 #include "trackcanvas.h"
48 #include "tracks.h"
49 #include "track.h"
50 #include "transportque.h"
51 #include "zoombar.h"
52
53
54 void MWindow::update_plugins()
55 {
56 // Show plugins which are visible and hide plugins which aren't
57 // Update plugin pointers in plugin servers
58 }
59
60
61 int MWindow::expand_sample()
62 {
63         if(gui)
64         {
65                 if(edl->local_session->zoom_sample < 0x100000)
66                 {
67                         edl->local_session->zoom_sample *= 2;
68                         gui->zoombar->sample_zoom->update(edl->local_session->zoom_sample);
69                         zoom_sample(edl->local_session->zoom_sample);
70                 }
71         }
72         return 0;
73 }
74
75 int MWindow::zoom_in_sample()
76 {
77         if(gui)
78         {
79                 if(edl->local_session->zoom_sample > 1)
80                 {
81                         edl->local_session->zoom_sample /= 2;
82                         gui->zoombar->sample_zoom->update(edl->local_session->zoom_sample);
83                         zoom_sample(edl->local_session->zoom_sample);
84                 }
85         }
86         return 0;
87 }
88
89 int MWindow::zoom_sample(int64_t zoom_sample)
90 {
91         CLAMP(zoom_sample, 1, 0x100000);
92         edl->local_session->zoom_sample = zoom_sample;
93         find_cursor();
94
95         TimelinePane *pane = gui->get_focused_pane();
96         samplemovement(edl->local_session->view_start[pane->number], pane->number);
97         return 0;
98 }
99
100 void MWindow::find_cursor()
101 {
102         TimelinePane *pane = gui->get_focused_pane();
103         edl->local_session->view_start[pane->number] =
104                 Units::round((edl->local_session->get_selectionend(1) +
105                 edl->local_session->get_selectionstart(1)) /
106                 2 *
107                 edl->session->sample_rate /
108                 edl->local_session->zoom_sample -
109                 (double)pane->canvas->get_w() /
110                 2);
111
112         if(edl->local_session->view_start[pane->number] < 0)
113                 edl->local_session->view_start[pane->number] = 0;
114 }
115
116
117 void MWindow::fit_selection()
118 {
119         if(EQUIV(edl->local_session->get_selectionstart(1),
120                 edl->local_session->get_selectionend(1)))
121         {
122                 double total_samples = edl->tracks->total_length() *
123                         edl->session->sample_rate;
124                 TimelinePane *pane = gui->get_focused_pane();
125                 for(edl->local_session->zoom_sample = 1;
126                         pane->canvas->get_w() * edl->local_session->zoom_sample < total_samples;
127                         edl->local_session->zoom_sample *= 2)
128                         ;
129         }
130         else
131         {
132                 double total_samples = (edl->local_session->get_selectionend(1) -
133                         edl->local_session->get_selectionstart(1)) *
134                         edl->session->sample_rate;
135                 TimelinePane *pane = gui->get_focused_pane();
136                 for(edl->local_session->zoom_sample = 1;
137                         pane->canvas->get_w() * edl->local_session->zoom_sample < total_samples;
138                         edl->local_session->zoom_sample *= 2)
139                         ;
140         }
141
142         edl->local_session->zoom_sample = MIN(0x100000,
143                 edl->local_session->zoom_sample);
144         zoom_sample(edl->local_session->zoom_sample);
145 }
146
147
148 void MWindow::fit_autos(int all)
149 {
150         float min = 0, max = 0;
151         double start, end;
152
153 // Test all autos
154         if(EQUIV(edl->local_session->get_selectionstart(1),
155                 edl->local_session->get_selectionend(1)))
156         {
157                 start = 0;
158                 end = edl->tracks->total_length();
159         }
160         else
161 // Test autos in highlighting only
162         {
163                 start = edl->local_session->get_selectionstart(1);
164                 end = edl->local_session->get_selectionend(1);
165         }
166
167         int forstart = edl->local_session->zoombar_showautotype;
168         int forend   = edl->local_session->zoombar_showautotype + 1;
169
170         if( all ) {
171                 forstart = 0;
172                 forend   = AUTOGROUPTYPE_COUNT;
173         }
174
175         for (int i = forstart; i < forend; i++)
176         {
177 // Adjust min and max
178                 edl->tracks->get_automation_extents(&min, &max, start, end, i);
179 //printf("MWindow::fit_autos %d %f %f results in ", i, min, max);
180
181                 float range = max - min;
182                 switch (i)
183                 {
184                 case AUTOGROUPTYPE_AUDIO_FADE:
185                         if (range < 1) {
186                                 min = MIN(min, edl->local_session->automation_mins[i]);
187                                 max = MAX(max, edl->local_session->automation_maxs[i]);
188                                 if( min >= max-0.1 ) { min = -80.0; min = 6.0; }
189                         }
190                         break;
191                 case AUTOGROUPTYPE_VIDEO_FADE:
192                         if (range < 1) {
193                                 min = MIN(min, edl->local_session->automation_mins[i]);
194                                 max = MAX(max, edl->local_session->automation_maxs[i]);
195                                 if( min >= max-0.1 ) { min = 0.0; min = 100.0; }
196                         }
197                         break;
198                 case AUTOGROUPTYPE_ZOOM:
199                         if (range < 0.001) {
200                                 min = floor(min*50)/100;
201                                 max = floor(max*200)/100;
202                         }
203                         break;
204                 case AUTOGROUPTYPE_SPEED:
205                         if (range < 0.001) {
206                                 min = floor(min*5)/100;
207                                 max = floor(max*300)/100;
208                         }
209                         break;
210                 case AUTOGROUPTYPE_X:
211                 case AUTOGROUPTYPE_Y:
212                         if (range < 1) {
213                                 float scale = bmin(edl->session->output_w, edl->session->output_h);
214                                 min = floor((min+max)/2) - 0.5*scale;
215                                 max = floor((min+max)/2) + 0.5*scale;
216                         }
217                         break;
218                 }
219 //printf("%f %f\n", min, max);
220                 if (!Automation::autogrouptypes_fixedrange[i])
221                 {
222                         edl->local_session->automation_mins[i] = min;
223                         edl->local_session->automation_maxs[i] = max;
224                 }
225         }
226
227 // Show range in zoombar
228         gui->zoombar->update();
229
230 // Draw
231         gui->draw_overlays(1);
232 }
233
234
235 void MWindow::change_currentautorange(int autogrouptype, int increment, int changemax)
236 {
237         float val;
238         if (changemax) {
239                 val = edl->local_session->automation_maxs[autogrouptype];
240         } else {
241                 val = edl->local_session->automation_mins[autogrouptype];
242         }
243
244         if (increment)
245         {
246                 switch (autogrouptype) {
247                 case AUTOGROUPTYPE_AUDIO_FADE:
248                         val += 2;
249                         break;
250                 case AUTOGROUPTYPE_VIDEO_FADE:
251                         val += 1;
252                         break;
253                 case AUTOGROUPTYPE_ZOOM:
254                 case AUTOGROUPTYPE_SPEED:
255                         if (val == 0)
256                                 val = 0.001;
257                         else
258                                 val = val*2;
259                         break;
260                 case AUTOGROUPTYPE_X:
261                 case AUTOGROUPTYPE_Y:
262                         val = floor(val + 5);
263                         break;
264                 }
265         }
266         else
267         { // decrement
268                 switch (autogrouptype) {
269                 case AUTOGROUPTYPE_AUDIO_FADE:
270                         val -= 2;
271                         break;
272                 case AUTOGROUPTYPE_VIDEO_FADE:
273                         val -= 1;
274                         break;
275                 case AUTOGROUPTYPE_ZOOM:
276                 case AUTOGROUPTYPE_SPEED:
277                         if (val > 0) val = val/2;
278                         break;
279                 case AUTOGROUPTYPE_X:
280                 case AUTOGROUPTYPE_Y:
281                         val = floor(val-5);
282                         break;
283                 }
284         }
285
286         AUTOMATIONVIEWCLAMPS(val, autogrouptype);
287
288         if (changemax) {
289                 if (val > edl->local_session->automation_mins[autogrouptype])
290                         edl->local_session->automation_maxs[autogrouptype] = val;
291         }
292         else
293         {
294                 if (val < edl->local_session->automation_maxs[autogrouptype])
295                         edl->local_session->automation_mins[autogrouptype] = val;
296         }
297 }
298
299
300 void MWindow::expand_autos(int changeall, int domin, int domax)
301 {
302         if (changeall)
303                 for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
304                         if (domin) change_currentautorange(i, 1, 0);
305                         if (domax) change_currentautorange(i, 1, 1);
306                 }
307         else
308         {
309                 if (domin) change_currentautorange(edl->local_session->zoombar_showautotype, 1, 0);
310                 if (domax) change_currentautorange(edl->local_session->zoombar_showautotype, 1, 1);
311         }
312         gui->zoombar->update_autozoom();
313         gui->draw_overlays(0);
314         gui->update_patchbay();
315         gui->flash_canvas(1);
316 }
317
318 void MWindow::shrink_autos(int changeall, int domin, int domax)
319 {
320         if (changeall)
321                 for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
322                         if (domin) change_currentautorange(i, 0, 0);
323                         if (domax) change_currentautorange(i, 0, 1);
324                 }
325         else
326         {
327                 if (domin) change_currentautorange(edl->local_session->zoombar_showautotype, 0, 0);
328                 if (domax) change_currentautorange(edl->local_session->zoombar_showautotype, 0, 1);
329         }
330         gui->zoombar->update_autozoom();
331         gui->draw_overlays(0);
332         gui->update_patchbay();
333         gui->flash_canvas(1);
334 }
335
336
337 void MWindow::zoom_autos(float min, float max)
338 {
339         int i = edl->local_session->zoombar_showautotype;
340         edl->local_session->automation_mins[i] = min;
341         edl->local_session->automation_maxs[i] = max;
342         gui->zoombar->update_autozoom();
343         gui->draw_overlays(1);
344 }
345
346
347 void MWindow::zoom_amp(int64_t zoom_amp)
348 {
349         edl->local_session->zoom_y = zoom_amp;
350         gui->draw_canvas(0, 0);
351         gui->flash_canvas(0);
352         gui->update_patchbay();
353         gui->flush();
354 }
355
356 void MWindow::zoom_atrack(int64_t zoom)
357 {
358         int64_t old_zoom = edl->local_session->zoom_atrack;
359         CLAMP(zoom, MIN_TRACK_ZOOM, MAX_TRACK_ZOOM);
360         edl->local_session->zoom_atrack = zoom;
361
362         edl->local_session->zoom_atrack = zoom;
363         for( Track *track= edl->tracks->first; track; track=track->next ) {
364                 if( track->data_type != TRACK_AUDIO ) continue;
365                 track->data_h = zoom;
366         }
367 // shift row position
368         for( int i=0; i<TOTAL_PANES; ++i ) edl->local_session->track_start[i] =
369                 (edl->local_session->track_start[i] * zoom) / old_zoom;
370         edl->tracks->update_y_pixels(theme);
371         gui->draw_trackmovement();
372 }
373
374 void MWindow::zoom_vtrack(int64_t zoom)
375 {
376         int64_t old_zoom = edl->local_session->zoom_vtrack;
377         CLAMP(zoom, MIN_TRACK_ZOOM, MAX_TRACK_ZOOM);
378         edl->local_session->zoom_vtrack = zoom;
379
380         for( Track *track= edl->tracks->first; track; track=track->next ) {
381                 if( track->data_type != TRACK_VIDEO ) continue;
382                 track->data_h = zoom;
383         }
384 // shift row position
385         for( int i=0; i<TOTAL_PANES; ++i ) edl->local_session->track_start[i] =
386                 (edl->local_session->track_start[i] * zoom) / old_zoom;
387         edl->tracks->update_y_pixels(theme);
388         gui->draw_trackmovement();
389 }
390
391 void MWindow::trackmovement(int offset, int pane_number)
392 {
393         edl->local_session->track_start[pane_number] += offset;
394         if(edl->local_session->track_start[pane_number] < 0)
395                 edl->local_session->track_start[pane_number] = 0;
396
397         if(pane_number == TOP_RIGHT_PANE ||
398                 pane_number == TOP_LEFT_PANE)
399         {
400                 edl->local_session->track_start[TOP_LEFT_PANE] =
401                         edl->local_session->track_start[TOP_RIGHT_PANE] =
402                         edl->local_session->track_start[pane_number];
403         }
404         else
405         if(pane_number == BOTTOM_RIGHT_PANE ||
406                 pane_number == BOTTOM_LEFT_PANE)
407         {
408                 edl->local_session->track_start[BOTTOM_LEFT_PANE] =
409                         edl->local_session->track_start[BOTTOM_RIGHT_PANE] =
410                         edl->local_session->track_start[pane_number];
411         }
412
413
414         edl->tracks->update_y_pixels(theme);
415         gui->draw_trackmovement();
416 }
417
418 void MWindow::move_up(int64_t distance)
419 {
420         TimelinePane *pane = gui->get_focused_pane();
421         if(distance == 0) distance = pane->canvas->get_h() / 10;
422         trackmovement(-distance, pane->number);
423 }
424
425 void MWindow::move_down(int64_t distance)
426 {
427         TimelinePane *pane = gui->get_focused_pane();
428         if(distance == 0) distance = pane->canvas->get_h() / 10;
429         trackmovement(distance, pane->number);
430 }
431
432 int MWindow::goto_end()
433 {
434         TimelinePane *pane = gui->get_focused_pane();
435         int64_t old_view_start = edl->local_session->view_start[pane->number];
436
437         if( edl->tracks->total_length() > (double)pane->canvas->get_w() *
438                 edl->local_session->zoom_sample / edl->session->sample_rate ) {
439                 edl->local_session->view_start[pane->number] =
440                         Units::round(edl->tracks->total_length() *
441                                 edl->session->sample_rate /
442                                 edl->local_session->zoom_sample -
443                                 pane->canvas->get_w() /
444                                 2);
445         }
446         else
447         {
448                 edl->local_session->view_start[pane->number] = 0;
449         }
450
451         if(gui->shift_down())
452         {
453                 edl->local_session->set_selectionend(edl->tracks->total_length());
454         }
455         else
456         {
457                 edl->local_session->set_selectionstart(edl->tracks->total_length());
458                 edl->local_session->set_selectionend(edl->tracks->total_length());
459         }
460
461         if(edl->local_session->view_start[pane->number] != old_view_start)
462         {
463                 samplemovement(edl->local_session->view_start[pane->number], pane->number);
464                 gui->draw_samplemovement();
465         }
466
467         update_plugin_guis();
468
469         gui->update_patchbay();
470         gui->update_cursor();
471         gui->activate_timeline();
472         gui->zoombar->update();
473         gui->update_timebar(1);
474         cwindow->update(1, 0, 0, 0, 1);
475         return 0;
476 }
477
478 int MWindow::goto_start()
479 {
480         TimelinePane *pane = gui->get_focused_pane();
481         int64_t old_view_start = edl->local_session->view_start[pane->number];
482
483         edl->local_session->view_start[pane->number] = 0;
484         if(gui->shift_down())
485         {
486                 edl->local_session->set_selectionstart(0);
487         }
488         else
489         {
490                 edl->local_session->set_selectionstart(0);
491                 edl->local_session->set_selectionend(0);
492         }
493
494         if(edl->local_session->view_start[pane->number] != old_view_start)
495         {
496                 samplemovement(edl->local_session->view_start[pane->number], pane->number);
497                 gui->draw_samplemovement();
498         }
499
500         update_plugin_guis();
501         gui->update_patchbay();
502         gui->update_cursor();
503         gui->activate_timeline();
504         gui->zoombar->update();
505         gui->update_timebar(1);
506         cwindow->update(1, 0, 0, 0, 1);
507         return 0;
508 }
509
510 int MWindow::goto_position(double position)
511 {
512         position = edl->align_to_frame(position, 0);
513         if( position < 0 ) position = 0;
514         select_point(position);
515         gui->activate_timeline();
516         return 0;
517 }
518
519 int MWindow::samplemovement(int64_t view_start, int pane_number)
520 {
521         if( view_start < 0 ) view_start = 0;
522         edl->local_session->view_start[pane_number] = view_start;
523         if(edl->local_session->view_start[pane_number] < 0)
524                 edl->local_session->view_start[pane_number] = 0;
525
526         if(pane_number == TOP_LEFT_PANE ||
527                 pane_number == BOTTOM_LEFT_PANE)
528         {
529                 edl->local_session->view_start[TOP_LEFT_PANE] =
530                         edl->local_session->view_start[BOTTOM_LEFT_PANE] =
531                         edl->local_session->view_start[pane_number];
532         }
533         else
534         {
535                 edl->local_session->view_start[TOP_RIGHT_PANE] =
536                         edl->local_session->view_start[BOTTOM_RIGHT_PANE] =
537                         edl->local_session->view_start[pane_number];
538         }
539
540         gui->draw_samplemovement();
541
542         return 0;
543 }
544
545 int MWindow::move_left(int64_t distance)
546 {
547         TimelinePane *pane = gui->get_focused_pane();
548         if(!distance)
549                 distance = pane->canvas->get_w() / 10;
550         edl->local_session->view_start[pane->number] -= distance;
551         samplemovement(edl->local_session->view_start[pane->number],
552                 pane->number);
553         return 0;
554 }
555
556 int MWindow::move_right(int64_t distance)
557 {
558         TimelinePane *pane = gui->get_focused_pane();
559         if(!distance)
560                 distance = pane->canvas->get_w() / 10;
561         edl->local_session->view_start[pane->number] += distance;
562         samplemovement(edl->local_session->view_start[pane->number],
563                 pane->number);
564         return 0;
565 }
566
567 void MWindow::select_all()
568 {
569         if( edl->local_session->get_selectionstart(1) == 0 &&
570             edl->local_session->get_selectionend(1) == edl->tracks->total_length() )
571                 edl->local_session->set_selectionend(0);
572         else {
573                 edl->local_session->set_selectionstart(0);
574                 edl->local_session->set_selectionend(edl->tracks->total_length());
575         }
576         gui->update(0, NORMAL_DRAW, 1, 1, 0, 1, 0);
577         gui->activate_timeline();
578         cwindow->update(1, 0, 0, 0, 1);
579         update_plugin_guis();
580 }
581
582 int MWindow::next_label(int shift_down)
583 {
584         double position = edl->local_session->get_selectionend(1);
585         double total_length = edl->tracks->total_length();
586         Label *current = edl->labels->next_label(position);
587         double new_position = current ? current->position : total_length;
588 // last playback endpoints as fake label positions
589         double playback_start = edl->local_session->playback_start;
590         double playback_end = edl->local_session->playback_end;
591         if( playback_start > position && playback_start < new_position )
592                 new_position = playback_start;
593         else if( playback_end > position && playback_end < new_position )
594                 new_position = playback_end;
595         if( new_position > total_length )
596                 new_position = total_length;
597         edl->local_session->set_selectionend(new_position);
598 //printf("MWindow::next_edit_handle %d\n", shift_down);
599         if( !shift_down )
600                 edl->local_session->set_selectionstart(new_position);
601         return find_selection(new_position);
602 }
603
604 int MWindow::prev_label(int shift_down)
605 {
606         double position = edl->local_session->get_selectionstart(1);
607         Label *current = edl->labels->prev_label(position);
608         double new_position = current ? current->position : 0;
609 // last playback endpoints as fake label positions
610         double playback_start = edl->local_session->playback_start;
611         double playback_end = edl->local_session->playback_end;
612         if( playback_end < position && playback_end > new_position )
613                 new_position = playback_end;
614         else if( playback_start < position && playback_start > new_position )
615                 new_position = playback_start;
616         if( new_position < 0 )
617                 new_position = 0;
618         edl->local_session->set_selectionstart(new_position);
619 //printf("MWindow::next_edit_handle %d\n", shift_down);
620         if( !shift_down )
621                 edl->local_session->set_selectionend(new_position);
622         return find_selection(new_position);
623 }
624
625 int MWindow::next_edit_handle(int shift_down)
626 {
627         double position = edl->local_session->get_selectionend(1);
628         double new_position = edl->next_edit(position);
629         double total_length = edl->tracks->total_length();
630         if( new_position >= total_length )
631                 new_position = total_length;
632         edl->local_session->set_selectionend(new_position);
633 //printf("MWindow::next_edit_handle %d\n", shift_down);
634         if( !shift_down )
635                 edl->local_session->set_selectionstart(new_position);
636         return find_selection(new_position);
637 }
638
639 int MWindow::prev_edit_handle(int shift_down)
640 {
641         double position = edl->local_session->get_selectionstart(1);
642         double new_position = edl->prev_edit(position);
643         if( new_position < 0 )
644                 new_position = 0;
645         edl->local_session->set_selectionstart(new_position);
646         if( !shift_down )
647                 edl->local_session->set_selectionend(new_position);
648         return find_selection(new_position);
649 }
650
651 int MWindow::nearest_plugin_keyframe(int shift_down, int dir)
652 {
653         KeyFrame *keyframe = 0;
654         double start = edl->local_session->get_selectionstart(1);
655         double end = edl->local_session->get_selectionend(1);
656         double position = dir == PLAY_FORWARD ? end : start;
657         double new_position = dir == PLAY_FORWARD ? start : end;
658         for( Track *track=edl->tracks->first; track; track=track->next ) {
659                 if( !track->is_armed() ) continue;
660                 for( int i=0; i<track->plugin_set.size(); ++i ) {
661                         PluginSet *plugin_set = track->plugin_set[i];
662                         int64_t pos = track->to_units(position, 0);
663                         KeyFrame *key = plugin_set->nearest_keyframe(pos, dir);
664                         if( !key ) continue;
665                         double key_position = track->from_units(key->position);
666                         if( keyframe && (dir == PLAY_FORWARD ?
667                                 key_position >= new_position :
668                                 new_position >= key_position ) ) continue;
669                         keyframe = key;  new_position = key_position;
670                 }
671         }
672
673         new_position = keyframe ?
674                 keyframe->autos->track->from_units(keyframe->position) :
675                 dir == PLAY_FORWARD ? edl->tracks->total_length() : 0;
676
677         if( !shift_down )
678                 start = end = new_position;
679         else if( dir == PLAY_FORWARD )
680                 end = new_position;
681         else
682                 start = new_position;
683
684         edl->local_session->set_selectionstart(start);
685         edl->local_session->set_selectionend(end);
686         return find_selection(new_position);
687 }
688
689 int MWindow::nearest_auto_keyframe(int shift_down, int dir)
690 {
691         Auto *keyframe = 0;
692         double start = edl->local_session->get_selectionstart(1);
693         double end = edl->local_session->get_selectionend(1);
694         double position = dir == PLAY_FORWARD ? end : start;
695         double new_position = dir == PLAY_FORWARD ? start : end;
696         for( Track *track=edl->tracks->first; track; track=track->next ) {
697                 if( !track->is_armed() ) continue;
698                 int64_t pos = track->to_units(position, 0);
699                 for( int i=0; i<AUTOMATION_TOTAL; ++i ) {
700                         Autos *autos = track->automation->autos[i];
701                         if( !autos ) continue;
702                         Auto *key = dir == PLAY_FORWARD ?
703                                  autos->nearest_after(pos) :
704                                  autos->nearest_before(pos);
705                         if( !key ) continue;
706                         double key_position = track->from_units(key->position);
707                         if( keyframe && (dir == PLAY_FORWARD ?
708                                 key_position >= new_position :
709                                 new_position >= key_position ) ) continue;
710                         keyframe = key;  new_position = key_position;
711                 }
712         }
713
714         new_position = keyframe ?
715                 keyframe->autos->track->from_units(keyframe->position) :
716                 dir == PLAY_FORWARD ? edl->tracks->total_length() : 0;
717
718         if( !shift_down )
719                 start = end = new_position;
720         else if( dir == PLAY_FORWARD )
721                 end = new_position;
722         else
723                 start = new_position;
724
725         edl->local_session->set_selectionstart(start);
726         edl->local_session->set_selectionend(end);
727         return find_selection(new_position);
728 }
729
730 int MWindow::find_selection(double position, int scroll_display)
731 {
732         update_plugin_guis();
733         TimelinePane *pane = gui->get_focused_pane();
734         double pixel_zoom = (double)edl->session->sample_rate / edl->local_session->zoom_sample;
735         if( !scroll_display ) {
736                 double timeline_start = edl->local_session->view_start[pane->number] / pixel_zoom;
737                 double timeline_end = timeline_start + pane->canvas->time_visible();
738                 if( position >= timeline_end || position < timeline_start )
739                         scroll_display = 1;
740         }
741         if( scroll_display ) {
742                 samplemovement((int64_t)(position * pixel_zoom - pane->canvas->get_w() / 2), pane->number);
743                 cwindow->update(1, 0, 0, 0, 0);
744         }
745         else {
746                 gui->update_patchbay();
747                 gui->update_timebar(0);
748                 gui->hide_cursor(0);
749                 gui->draw_cursor(0);
750                 gui->zoombar->update();
751                 gui->flash_canvas(1);
752                 cwindow->update(1, 0, 0, 0, 1);
753         }
754         return 0;
755 }
756
757 double MWindow::get_position()
758 {
759         return edl->local_session->get_selectionstart(1);
760 }
761
762 void MWindow::set_position(double position)
763 {
764         if( position != get_position() ) {
765                 if( position < 0 ) position = 0;
766                 edl->local_session->set_selectionstart(position);
767                 edl->local_session->set_selectionend(position);
768                 gui->lock_window();
769                 find_cursor();
770                 gui->update(1, NORMAL_DRAW, 1, 1, 1, 1, 0);
771                 gui->unlock_window();
772                 cwindow->update(1, 0, 0, 0, 0);
773         }
774 }
775
776
777 int MWindow::expand_y()
778 {
779         int result = edl->local_session->zoom_y * 2;
780         result = MIN(result, MAX_AMP_ZOOM);
781         zoom_amp(result);
782         gui->zoombar->update();
783         return 0;
784 }
785
786 int MWindow::zoom_in_y()
787 {
788         int result = edl->local_session->zoom_y / 2;
789         result = MAX(result, MIN_AMP_ZOOM);
790         zoom_amp(result);
791         gui->zoombar->update();
792         return 0;
793 }
794
795 int MWindow::expand_t()
796 {
797         int zoom = edl->local_session->zoom_atrack * 2;
798         zoom = MIN(zoom, MAX_TRACK_ZOOM);
799         zoom_atrack(zoom);
800         zoom = edl->local_session->zoom_vtrack * 2;
801         zoom = MIN(zoom, MAX_TRACK_ZOOM);
802         zoom_vtrack(zoom);
803         gui->zoombar->update();
804         return 0;
805 }
806
807 int MWindow::zoom_in_t()
808 {
809         int zoom = edl->local_session->zoom_atrack / 2;
810         zoom = MAX(zoom, MIN_TRACK_ZOOM);
811         zoom_atrack(zoom);
812         zoom = edl->local_session->zoom_vtrack / 2;
813         zoom = MAX(zoom, MIN_TRACK_ZOOM);
814         zoom_vtrack(zoom);
815         gui->zoombar->update();
816         return 0;
817 }
818
819 void MWindow::split_x()
820 {
821         gui->resource_thread->stop_draw(1);
822
823         if(gui->pane[TOP_RIGHT_PANE])
824         {
825                 gui->delete_x_pane(theme->mcanvas_w);
826                 edl->local_session->x_pane = -1;
827         }
828         else
829         {
830                 gui->create_x_pane(theme->mcanvas_w / 2);
831                 edl->local_session->x_pane = theme->mcanvas_w / 2;
832         }
833
834         gui->mainmenu->update_toggles(0);
835         gui->update_pane_dividers();
836         gui->update_cursor();
837         gui->draw_samplemovement();
838 // required to get new widgets to appear
839         gui->show_window();
840
841         gui->resource_thread->start_draw();
842 }
843
844 void MWindow::split_y()
845 {
846         gui->resource_thread->stop_draw(1);
847         if(gui->pane[BOTTOM_LEFT_PANE])
848         {
849                 gui->delete_y_pane(theme->mcanvas_h);
850                 edl->local_session->y_pane = -1;
851         }
852         else
853         {
854                 gui->create_y_pane(theme->mcanvas_h / 2);
855                 edl->local_session->y_pane = theme->mcanvas_h / 2;
856         }
857
858         gui->mainmenu->update_toggles(0);
859         gui->update_pane_dividers();
860         gui->update_cursor();
861         gui->draw_trackmovement();
862 // required to get new widgets to appear
863         gui->show_window();
864         gui->resource_thread->start_draw();
865 }
866