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