switch move/swap tracks, add mv trk shortcut, update msg
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / localsession.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "awindowgui.h"
24 #include "clip.h"
25 #include "bchash.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "filesystem.h"
29 #include "filexml.h"
30 #include "floatauto.h"
31 #include "language.h"
32 #include "localsession.h"
33
34
35 static const char *xml_autogrouptypes_titlesmax[] =
36 {
37         "AUTOGROUPTYPE_AUDIO_FADE_MAX",
38         "AUTOGROUPTYPE_VIDEO_FADE_MAX",
39         "AUTOGROUPTYPE_ZOOM_MAX",
40         "AUTOGROUPTYPE_SPEED_MAX",
41         "AUTOGROUPTYPE_X_MAX",
42         "AUTOGROUPTYPE_Y_MAX",
43         "AUTOGROUPTYPE_INT255_MAX"
44 };
45
46 static const char *xml_autogrouptypes_titlesmin[] =
47 {
48         "AUTOGROUPTYPE_AUDIO_FADE_MIN",
49         "AUTOGROUPTYPE_VIDEO_FADE_MIN",
50         "AUTOGROUPTYPE_ZOOM_MIN",
51         "AUTOGROUPTYPE_SPEED_MIN",
52         "AUTOGROUPTYPE_X_MIN",
53         "AUTOGROUPTYPE_Y_MIN",
54         "AUTOGROUPTYPE_INT255_MIN"
55 };
56
57
58 LocalSession::LocalSession(EDL *edl)
59 {
60         this->edl = edl;
61
62         selectionstart = selectionend = 0;
63         in_point = out_point = -1;
64         sprintf(clip_title, _("Program"));
65         strcpy(clip_notes, _("Hello world"));
66         strcpy(clip_icon, "");
67         clipboard_length = 0;
68         asset2edl = 0;
69         loop_playback = 0;
70         loop_start = loop_end = 0;
71         playback_start = -1;
72         playback_end = 0;
73         preview_start = 0;  preview_end = -1;
74         zoom_sample = DEFAULT_ZOOM_TIME;
75         zoom_y = 0;
76         zoom_atrack = 0;
77         zoom_vtrack = 0;
78         x_pane = y_pane = -1;
79
80         for(int i = 0; i < TOTAL_PANES; i++) {
81                 view_start[i] = 0;
82                 track_start[i] = 0;
83         }
84
85         reset_view_limits();
86         automation_mins[AUTOGROUPTYPE_AUDIO_FADE] = -80;
87         automation_maxs[AUTOGROUPTYPE_AUDIO_FADE] = 6;
88         automation_mins[AUTOGROUPTYPE_VIDEO_FADE] = 0;
89         automation_maxs[AUTOGROUPTYPE_VIDEO_FADE] = 100;
90         automation_mins[AUTOGROUPTYPE_SPEED] = SPEED_MIN;
91         automation_maxs[AUTOGROUPTYPE_SPEED] = 5.000;
92         automation_mins[AUTOGROUPTYPE_INT255] = 0;
93         automation_maxs[AUTOGROUPTYPE_INT255] = 255;
94
95         zoombar_showautotype = AUTOGROUPTYPE_AUDIO_FADE;
96         zoombar_showautocolor = -1;
97
98         floatauto_type = FloatAuto::SMOOTH;
99
100         red = green = blue = 0;
101         red_max = green_max = blue_max = 0;
102         use_max = 0;
103         solo_track_id = -1;
104 }
105
106 LocalSession::~LocalSession()
107 {
108 }
109
110 void LocalSession::copy_from(LocalSession *that)
111 {
112         strcpy(clip_title, that->clip_title);
113         strcpy(clip_notes, that->clip_notes);
114         strcpy(clip_icon, that->clip_icon);
115         in_point = that->in_point;
116         loop_playback = that->loop_playback;
117         loop_start = that->loop_start;
118         loop_end = that->loop_end;
119         out_point = that->out_point;
120         selectionend = that->selectionend;
121         selectionstart = that->selectionstart;
122         x_pane = that->x_pane;
123         y_pane = that->y_pane;
124
125         for(int i = 0; i < TOTAL_PANES; i++)
126         {
127                 view_start[i] = that->view_start[i];
128                 track_start[i] = that->track_start[i];
129         }
130
131         zoom_sample = that->zoom_sample;
132         zoom_y = that->zoom_y;
133         zoom_atrack = that->zoom_atrack;
134         zoom_vtrack = that->zoom_vtrack;
135         preview_start = that->preview_start;
136         preview_end = that->preview_end;
137         red = that->red;
138         green = that->green;
139         blue = that->blue;
140         red_max = that->red_max;
141         green_max = that->green_max;
142         blue_max = that->blue_max;
143         use_max = that->use_max;
144         solo_track_id = that->solo_track_id;
145
146         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
147                 automation_mins[i] = that->automation_mins[i];
148                 automation_maxs[i] = that->automation_maxs[i];
149         }
150         floatauto_type = that->floatauto_type;
151 }
152
153 void LocalSession::save_xml(FileXML *file, double start)
154 {
155         file->tag.set_title("LOCALSESSION");
156
157         file->tag.set_property("IN_POINT", in_point - start);
158         file->tag.set_property("LOOP_PLAYBACK", loop_playback);
159         file->tag.set_property("LOOP_START", loop_start - start);
160         file->tag.set_property("LOOP_END", loop_end - start);
161         file->tag.set_property("OUT_POINT", out_point - start);
162         file->tag.set_property("SELECTION_START", selectionstart - start);
163         file->tag.set_property("SELECTION_END", selectionend - start);
164         file->tag.set_property("CLIP_TITLE", clip_title);
165         file->tag.set_property("CLIP_ICON", clip_icon);
166         file->tag.set_property("X_PANE", x_pane);
167         file->tag.set_property("Y_PANE", y_pane);
168
169         char string[BCTEXTLEN];
170         for(int i = 0; i < TOTAL_PANES; i++)
171         {
172                 sprintf(string, "TRACK_START%d", i);
173                 file->tag.set_property(string, track_start[i]);
174                 sprintf(string, "VIEW_START%d", i);
175                 file->tag.set_property(string, view_start[i]);
176         }
177
178         file->tag.set_property("ZOOM_SAMPLE", zoom_sample);
179 //printf("EDLSession::save_session 1\n");
180         file->tag.set_property("ZOOMY", zoom_y);
181         file->tag.set_property("ZOOM_ATRACK", zoom_atrack);
182         file->tag.set_property("ZOOM_VTRACK", zoom_vtrack);
183
184         double preview_start = this->preview_start - start;
185         if( preview_start < 0 ) preview_start = 0;
186         double preview_end = this->preview_end - start;
187         if( preview_end < preview_start ) preview_end = -1;
188         file->tag.set_property("PREVIEW_START", preview_start);
189         file->tag.set_property("PREVIEW_END", preview_end);
190         file->tag.set_property("FLOATAUTO_TYPE", floatauto_type);
191
192         file->tag.set_property("RED", red);
193         file->tag.set_property("GREEN", green);
194         file->tag.set_property("BLUE", blue);
195         file->tag.set_property("RED_MAX", red_max);
196         file->tag.set_property("GREEN_MAX", green_max);
197         file->tag.set_property("BLUE_MAX", blue_max);
198         file->tag.set_property("USE_MAX", use_max);
199
200         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
201                 if (!Automation::autogrouptypes_fixedrange[i]) {
202                         file->tag.set_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
203                         file->tag.set_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
204                 }
205         }
206         file->append_tag();
207         file->append_newline();
208
209 //this used to be a property, now used as tag member
210 //      file->tag.set_property("CLIP_NOTES", clip_notes);
211         file->tag.set_title("CLIP_NOTES");   file->append_tag();
212         file->append_text(clip_notes);
213         file->tag.set_title("/CLIP_NOTES");  file->append_tag();
214         file->append_newline();
215
216         file->tag.set_title("/LOCALSESSION");
217         file->append_tag();
218         file->append_newline();
219         file->append_newline();
220 }
221
222 void LocalSession::synchronize_params(LocalSession *that)
223 {
224         loop_playback = that->loop_playback;
225         loop_start = that->loop_start;
226         loop_end = that->loop_end;
227         preview_start = that->preview_start;
228         preview_end = that->preview_end;
229         red = that->red;
230         green = that->green;
231         blue = that->blue;
232         red_max = that->red_max;
233         green_max = that->green_max;
234         blue_max = that->blue_max;
235         if( solo_track_id < 0 || that->solo_track_id < 0 )
236                 solo_track_id = that->solo_track_id;
237 }
238
239
240 void LocalSession::load_xml(FileXML *file, unsigned long load_flags)
241 {
242         if(load_flags & LOAD_SESSION)
243         {
244 // moved to EDL::load_xml for paste to fill silence.
245 //              clipboard_length = 0;
246 // Overwritten by MWindow::load_filenames
247                 file->tag.get_property("CLIP_TITLE", clip_title);
248                 clip_notes[0] = 0;
249                 file->tag.get_property("CLIP_NOTES", clip_notes);
250                 clip_icon[0] = 0;
251                 file->tag.get_property("CLIP_ICON", clip_icon);
252 // kludge if possible
253                 if( !clip_icon[0] ) {
254                         char *cp = clip_notes;
255                         int year, mon, mday, hour, min, sec;
256                         while( *cp && *cp++ != ':' );
257                         if( *cp && sscanf(cp, "%d/%02d/%02d %02d:%02d:%02d,",
258                                         &year, &mon, &mday, &hour, &min, &sec) == 6 ) {
259                                 sprintf(clip_icon, "clip_%02d%02d%02d-%02d%02d%02d.png",
260                                         year, mon, mday, hour, min, sec);
261                         }
262                 }
263                 loop_playback = file->tag.get_property("LOOP_PLAYBACK", 0);
264                 loop_start = file->tag.get_property("LOOP_START", (double)0);
265                 loop_end = file->tag.get_property("LOOP_END", (double)0);
266                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
267                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
268                 x_pane = file->tag.get_property("X_PANE", -1);
269                 y_pane = file->tag.get_property("Y_PANE", -1);
270
271
272                 char string[BCTEXTLEN];
273                 for(int i = 0; i < TOTAL_PANES; i++)
274                 {
275                         sprintf(string, "TRACK_START%d", i);
276                         track_start[i] = file->tag.get_property(string, track_start[i]);
277                         sprintf(string, "VIEW_START%d", i);
278                         view_start[i] = file->tag.get_property(string, view_start[i]);
279                 }
280
281                 zoom_sample = file->tag.get_property("ZOOM_SAMPLE", zoom_sample);
282                 zoom_y = file->tag.get_property("ZOOMY", zoom_y);
283                 int64_t zoom_track = file->tag.get_property("ZOOM_TRACK", 0);
284                 if( zoom_track > 0 ) zoom_atrack = zoom_vtrack = zoom_track;
285                 zoom_atrack = file->tag.get_property("ZOOM_ATRACK", zoom_atrack);
286                 zoom_vtrack = file->tag.get_property("ZOOM_VTRACK", zoom_vtrack);
287                 preview_start = file->tag.get_property("PREVIEW_START", preview_start);
288                 preview_end = file->tag.get_property("PREVIEW_END", preview_end);
289                 red = file->tag.get_property("RED", red);
290                 green = file->tag.get_property("GREEN", green);
291                 blue = file->tag.get_property("BLUE", blue);
292                 red_max = file->tag.get_property("RED_MAX", red_max);
293                 green_max = file->tag.get_property("GREEN_MAX", green_max);
294                 blue_max = file->tag.get_property("BLUE_MAX", blue_max);
295                 use_max = file->tag.get_property("USE_MAX", use_max);
296
297                 for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
298                         if (!Automation::autogrouptypes_fixedrange[i]) {
299                                 automation_mins[i] = file->tag.get_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
300                                 AUTOMATIONCLAMPS(automation_mins[i], i);
301                                 automation_maxs[i] = file->tag.get_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
302                                 AUTOMATIONCLAMPS(automation_maxs[i], i);
303                         }
304                 }
305                 floatauto_type = file->tag.get_property("FLOATAUTO_TYPE", floatauto_type);
306         }
307
308
309 // on operations like cut, paste, slice, clear... we should also undo the cursor position as users
310 // expect - this is additionally important in keyboard-only editing in viewer window
311         if(load_flags & LOAD_SESSION || load_flags & LOAD_TIMEBAR)
312         {
313                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
314                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
315         }
316
317
318
319         if(load_flags & LOAD_TIMEBAR)
320         {
321                 in_point = file->tag.get_property("IN_POINT", (double)-1);
322                 out_point = file->tag.get_property("OUT_POINT", (double)-1);
323         }
324
325         while( !file->read_tag() ) {
326                 if( file->tag.title_is("/LOCALSESSION") ) break;
327                 if( file->tag.title_is("CLIP_NOTES") ) {
328                         XMLBuffer notes;
329                         file->read_text_until("/CLIP_NOTES", &notes, 1);
330                         memset(clip_notes, 0, sizeof(clip_notes));
331                         strncpy(clip_notes, notes.cstr(), sizeof(clip_notes)-1);
332                 }
333         }
334 }
335
336 void LocalSession::boundaries()
337 {
338         zoom_sample = MAX(1, zoom_sample);
339 }
340
341 int LocalSession::load_defaults(BC_Hash *defaults)
342 {
343         loop_playback = defaults->get("LOOP_PLAYBACK", 0);
344         loop_start = defaults->get("LOOP_START", (double)0);
345         loop_end = defaults->get("LOOP_END", (double)0);
346         selectionstart = defaults->get("SELECTIONSTART", selectionstart);
347         selectionend = defaults->get("SELECTIONEND", selectionend);
348 //      track_start = defaults->get("TRACK_START", 0);
349 //      view_start = defaults->get("VIEW_START", 0);
350         zoom_sample = defaults->get("ZOOM_SAMPLE", DEFAULT_ZOOM_TIME);
351         zoom_y = defaults->get("ZOOMY", DEFAULT_ZOOM_TRACK);
352         int64_t zoom_track = defaults->get("ZOOM_TRACK", 0);
353         if( zoom_track == 0 ) zoom_track = DEFAULT_ZOOM_TRACK;
354         zoom_atrack = defaults->get("ZOOM_ATRACK", zoom_track);
355         zoom_vtrack = defaults->get("ZOOM_VTRACK", zoom_track);
356         red = defaults->get("RED", 0.0);
357         green = defaults->get("GREEN", 0.0);
358         blue = defaults->get("BLUE", 0.0);
359         red_max = defaults->get("RED_MAX", 0.0);
360         green_max = defaults->get("GREEN_MAX", 0.0);
361         blue_max = defaults->get("BLUE_MAX", 0.0);
362         use_max = defaults->get("USE_MAX", 0);
363
364         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
365                 if (!Automation::autogrouptypes_fixedrange[i]) {
366                         automation_mins[i] = defaults->get(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
367                         AUTOMATIONCLAMPS(automation_mins[i], i);
368                         automation_maxs[i] = defaults->get(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
369                         AUTOMATIONCLAMPS(automation_maxs[i], i);
370                 }
371         }
372
373         floatauto_type = defaults->get("FLOATAUTO_TYPE", floatauto_type);
374         x_pane = defaults->get("X_PANE", x_pane);
375         y_pane = defaults->get("Y_PANE", y_pane);
376
377         return 0;
378 }
379
380 int LocalSession::save_defaults(BC_Hash *defaults)
381 {
382         defaults->update("LOOP_PLAYBACK", loop_playback);
383         defaults->update("LOOP_START", loop_start);
384         defaults->update("LOOP_END", loop_end);
385         defaults->update("SELECTIONSTART", selectionstart);
386         defaults->update("SELECTIONEND", selectionend);
387 //      defaults->update("TRACK_START", track_start);
388 //      defaults->update("VIEW_START", view_start);
389         defaults->update("ZOOM_SAMPLE", zoom_sample);
390         defaults->update("ZOOMY", zoom_y);
391         defaults->update("ZOOM_ATRACK", zoom_atrack);
392         defaults->update("ZOOM_VTRACK", zoom_vtrack);
393         defaults->update("RED", red);
394         defaults->update("GREEN", green);
395         defaults->update("BLUE", blue);
396         defaults->update("RED_MAX", red_max);
397         defaults->update("GREEN_MAX", green_max);
398         defaults->update("BLUE_MAX", blue_max);
399         defaults->update("USE_MAX", use_max);
400
401         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
402                 if (!Automation::autogrouptypes_fixedrange[i]) {
403                         defaults->update(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
404                         defaults->update(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
405                 }
406         }
407
408         defaults->update("FLOATAUTO_TYPE", floatauto_type);
409         defaults->update("X_PANE", x_pane);
410         defaults->update("Y_PANE", y_pane);
411         return 0;
412 }
413
414 void LocalSession::set_selectionstart(double value)
415 {
416         this->selectionstart = value;
417 }
418
419 void LocalSession::set_selectionend(double value)
420 {
421         this->selectionend = value;
422 }
423
424 void LocalSession::set_inpoint(double value)
425 {
426         in_point = value;
427 }
428
429 void LocalSession::set_outpoint(double value)
430 {
431         out_point = value;
432 }
433
434 void LocalSession::unset_inpoint()
435 {
436         in_point = -1;
437 }
438
439 void LocalSession::unset_outpoint()
440 {
441         out_point = -1;
442 }
443
444 void LocalSession::set_playback_start(double value)
445 {
446         if( playback_end < 0 ) return;
447         playback_start = value;
448         playback_end = -1;
449 }
450
451 void LocalSession::set_playback_end(double value)
452 {
453         if( value < playback_start ) {
454                 if( playback_end < 0 )
455                         playback_end = playback_start;
456                 playback_start = value;
457         }
458         else if( playback_end < 0 || value > playback_end )
459                 playback_end = value;
460 }
461
462
463 double LocalSession::get_selectionstart(int highlight_only)
464 {
465         if(highlight_only || !EQUIV(selectionstart, selectionend))
466                 return selectionstart;
467
468         if(in_point >= 0)
469                 return in_point;
470         else
471         if(out_point >= 0)
472                 return out_point;
473         else
474                 return selectionstart;
475 }
476
477 double LocalSession::get_selectionend(int highlight_only)
478 {
479         if(highlight_only || !EQUIV(selectionstart, selectionend))
480                 return selectionend;
481
482         if(out_point >= 0)
483                 return out_point;
484         else
485         if(in_point >= 0)
486                 return in_point;
487         else
488                 return selectionend;
489 }
490
491 double LocalSession::get_inpoint()
492 {
493         return in_point;
494 }
495
496 double LocalSession::get_outpoint()
497 {
498         return out_point;
499 }
500
501 int LocalSession::inpoint_valid()
502 {
503         return in_point >= 0;
504 }
505
506 int LocalSession::outpoint_valid()
507 {
508         return out_point >= 0;
509 }
510
511 void LocalSession::reset_view_limits()
512 {
513         automation_mins[AUTOGROUPTYPE_ZOOM] = 0.005;
514         automation_maxs[AUTOGROUPTYPE_ZOOM] = 5.000;
515         int out_w = edl->session->output_w;
516         automation_mins[AUTOGROUPTYPE_X] = -out_w;
517         automation_maxs[AUTOGROUPTYPE_X] = out_w;
518         int out_h = edl->session->output_h;
519         automation_mins[AUTOGROUPTYPE_Y] = -out_h;
520         automation_maxs[AUTOGROUPTYPE_Y] = out_h;
521
522 }
523