nested clips, big rework and cleanup, sams new icons, leaks and tweaks
[goodguy/history.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         awindow_folder = AW_CLIP_FOLDER;
64         sprintf(clip_title, _("Program"));
65         strcpy(clip_notes, _("Hello world"));
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 = preview_end = 0;
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
105         floatauto_type = FloatAuto::SMOOTH;
106
107         red = green = blue = 0;
108         red_max = green_max = blue_max = 0;
109         use_max = 0;
110 }
111
112 LocalSession::~LocalSession()
113 {
114 }
115
116 void LocalSession::copy_from(LocalSession *that)
117 {
118         strcpy(clip_title, that->clip_title);
119         strcpy(clip_notes, that->clip_notes);
120         awindow_folder = that->awindow_folder;
121         in_point = that->in_point;
122         loop_playback = that->loop_playback;
123         loop_start = that->loop_start;
124         loop_end = that->loop_end;
125         out_point = that->out_point;
126         selectionend = that->selectionend;
127         selectionstart = that->selectionstart;
128         x_pane = that->x_pane;
129         y_pane = that->y_pane;
130
131         for(int i = 0; i < TOTAL_PANES; i++)
132         {
133                 view_start[i] = that->view_start[i];
134                 track_start[i] = that->track_start[i];
135         }
136
137         zoom_sample = that->zoom_sample;
138         zoom_y = that->zoom_y;
139         zoom_track = that->zoom_track;
140         preview_start = that->preview_start;
141         preview_end = that->preview_end;
142         red = that->red;
143         green = that->green;
144         blue = that->blue;
145         red_max = that->red_max;
146         green_max = that->green_max;
147         blue_max = that->blue_max;
148         use_max = that->use_max;
149
150         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
151                 automation_mins[i] = that->automation_mins[i];
152                 automation_maxs[i] = that->automation_maxs[i];
153         }
154         floatauto_type = that->floatauto_type;
155 }
156
157 void LocalSession::save_xml(FileXML *file, double start)
158 {
159         file->tag.set_title("LOCALSESSION");
160
161         file->tag.set_property("IN_POINT", in_point - start);
162         file->tag.set_property("LOOP_PLAYBACK", loop_playback);
163         file->tag.set_property("LOOP_START", loop_start - start);
164         file->tag.set_property("LOOP_END", loop_end - start);
165         file->tag.set_property("OUT_POINT", out_point - start);
166         file->tag.set_property("SELECTION_START", selectionstart - start);
167         file->tag.set_property("SELECTION_END", selectionend - start);
168         file->tag.set_property("CLIP_TITLE", clip_title);
169         file->tag.set_property("CLIP_NOTES", clip_notes);
170         file->tag.set_property("AWINDOW_FOLDER", awindow_folder);
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 < 0) preview_end = 0;
193
194         file->tag.set_property("PREVIEW_START", preview_start);
195         file->tag.set_property("PREVIEW_END", preview_end);
196         file->tag.set_property("FLOATAUTO_TYPE", floatauto_type);
197
198         file->tag.set_property("RED", red);
199         file->tag.set_property("GREEN", green);
200         file->tag.set_property("BLUE", blue);
201         file->tag.set_property("RED_MAX", red_max);
202         file->tag.set_property("GREEN_MAX", green_max);
203         file->tag.set_property("BLUE_MAX", blue_max);
204         file->tag.set_property("USE_MAX", use_max);
205
206         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
207                 if (!Automation::autogrouptypes_fixedrange[i]) {
208                         file->tag.set_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
209                         file->tag.set_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
210                 }
211         }
212         file->append_tag();
213
214         file->tag.set_title("/LOCALSESSION");
215         file->append_tag();
216         file->append_newline();
217         file->append_newline();
218 }
219
220 void LocalSession::synchronize_params(LocalSession *that)
221 {
222         loop_playback = that->loop_playback;
223         loop_start = that->loop_start;
224         loop_end = that->loop_end;
225         preview_start = that->preview_start;
226         preview_end = that->preview_end;
227         red = that->red;
228         green = that->green;
229         blue = that->blue;
230         red_max = that->red_max;
231         green_max = that->green_max;
232         blue_max = that->blue_max;
233 }
234
235
236 void LocalSession::load_xml(FileXML *file, unsigned long load_flags)
237 {
238         if(load_flags & LOAD_SESSION)
239         {
240 // moved to EDL::load_xml for paste to fill silence.
241 //              clipboard_length = 0;
242 // Overwritten by MWindow::load_filenames
243                 file->tag.get_property("CLIP_TITLE", clip_title);
244                 file->tag.get_property("CLIP_NOTES", clip_notes);
245                 const char *folder = file->tag.get_property("FOLDER");
246                 if( folder ) {
247                         awindow_folder = AWindowGUI::folder_number(folder);
248                         if( awindow_folder < 0 ) awindow_folder = AW_MEDIA_FOLDER;
249                 }
250                 awindow_folder = file->tag.get_property("AWINDOW_FOLDER", awindow_folder);
251                 loop_playback = file->tag.get_property("LOOP_PLAYBACK", 0);
252                 loop_start = file->tag.get_property("LOOP_START", (double)0);
253                 loop_end = file->tag.get_property("LOOP_END", (double)0);
254                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
255                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
256                 x_pane = file->tag.get_property("X_PANE", -1);
257                 y_pane = file->tag.get_property("Y_PANE", -1);
258
259
260                 char string[BCTEXTLEN];
261                 for(int i = 0; i < TOTAL_PANES; i++)
262                 {
263                         sprintf(string, "TRACK_START%d", i);
264                         track_start[i] = file->tag.get_property(string, track_start[i]);
265                         sprintf(string, "VIEW_START%d", i);
266                         view_start[i] = file->tag.get_property(string, view_start[i]);
267                 }
268
269                 zoom_sample = file->tag.get_property("ZOOM_SAMPLE", zoom_sample);
270                 zoom_y = file->tag.get_property("ZOOMY", zoom_y);
271                 zoom_track = file->tag.get_property("ZOOM_TRACK", zoom_track);
272                 preview_start = file->tag.get_property("PREVIEW_START", preview_start);
273                 preview_end = file->tag.get_property("PREVIEW_END", preview_end);
274                 red = file->tag.get_property("RED", red);
275                 green = file->tag.get_property("GREEN", green);
276                 blue = file->tag.get_property("BLUE", blue);
277                 red_max = file->tag.get_property("RED_MAX", red_max);
278                 green_max = file->tag.get_property("GREEN_MAX", green_max);
279                 blue_max = file->tag.get_property("BLUE_MAX", blue_max);
280                 use_max = file->tag.get_property("USE_MAX", use_max);
281
282                 for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
283                         if (!Automation::autogrouptypes_fixedrange[i]) {
284                                 automation_mins[i] = file->tag.get_property(xml_autogrouptypes_titlesmin[i],automation_mins[i]);
285                                 automation_maxs[i] = file->tag.get_property(xml_autogrouptypes_titlesmax[i],automation_maxs[i]);
286                         }
287                 }
288                 floatauto_type = file->tag.get_property("FLOATAUTO_TYPE", floatauto_type);
289         }
290
291
292 // on operations like cut, paste, slice, clear... we should also undo the cursor position as users
293 // expect - this is additionally important in keyboard-only editing in viewer window
294         if(load_flags & LOAD_SESSION || load_flags & LOAD_TIMEBAR)
295         {
296                 selectionstart = file->tag.get_property("SELECTION_START", (double)0);
297                 selectionend = file->tag.get_property("SELECTION_END", (double)0);
298         }
299
300
301
302         if(load_flags & LOAD_TIMEBAR)
303         {
304                 in_point = file->tag.get_property("IN_POINT", (double)-1);
305                 out_point = file->tag.get_property("OUT_POINT", (double)-1);
306         }
307 }
308
309 void LocalSession::boundaries()
310 {
311         zoom_sample = MAX(1, zoom_sample);
312 }
313
314 int LocalSession::load_defaults(BC_Hash *defaults)
315 {
316         loop_playback = defaults->get("LOOP_PLAYBACK", 0);
317         loop_start = defaults->get("LOOP_START", (double)0);
318         loop_end = defaults->get("LOOP_END", (double)0);
319         selectionstart = defaults->get("SELECTIONSTART", selectionstart);
320         selectionend = defaults->get("SELECTIONEND", selectionend);
321 //      track_start = defaults->get("TRACK_START", 0);
322 //      view_start = defaults->get("VIEW_START", 0);
323         zoom_sample = defaults->get("ZOOM_SAMPLE", DEFAULT_ZOOM_TIME);
324         zoom_y = defaults->get("ZOOMY", 64);
325         zoom_track = defaults->get("ZOOM_TRACK", 64);
326         red = defaults->get("RED", 0.0);
327         green = defaults->get("GREEN", 0.0);
328         blue = defaults->get("BLUE", 0.0);
329         red_max = defaults->get("RED_MAX", 0.0);
330         green_max = defaults->get("GREEN_MAX", 0.0);
331         blue_max = defaults->get("BLUE_MAX", 0.0);
332         use_max = defaults->get("USE_MAX", 0);
333
334         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
335                 if (!Automation::autogrouptypes_fixedrange[i]) {
336                         automation_mins[i] = defaults->get(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
337                         automation_maxs[i] = defaults->get(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
338                 }
339         }
340
341         floatauto_type = defaults->get("FLOATAUTO_TYPE", floatauto_type);
342         x_pane = defaults->get("X_PANE", x_pane);
343         y_pane = defaults->get("Y_PANE", y_pane);
344
345         return 0;
346 }
347
348 int LocalSession::save_defaults(BC_Hash *defaults)
349 {
350         defaults->update("LOOP_PLAYBACK", loop_playback);
351         defaults->update("LOOP_START", loop_start);
352         defaults->update("LOOP_END", loop_end);
353         defaults->update("SELECTIONSTART", selectionstart);
354         defaults->update("SELECTIONEND", selectionend);
355 //      defaults->update("TRACK_START", track_start);
356 //      defaults->update("VIEW_START", view_start);
357         defaults->update("ZOOM_SAMPLE", zoom_sample);
358         defaults->update("ZOOMY", zoom_y);
359         defaults->update("ZOOM_TRACK", zoom_track);
360         defaults->update("RED", red);
361         defaults->update("GREEN", green);
362         defaults->update("BLUE", blue);
363         defaults->update("RED_MAX", red_max);
364         defaults->update("GREEN_MAX", green_max);
365         defaults->update("BLUE_MAX", blue_max);
366         defaults->update("USE_MAX", use_max);
367
368         for (int i = 0; i < AUTOGROUPTYPE_COUNT; i++) {
369                 if (!Automation::autogrouptypes_fixedrange[i]) {
370                         defaults->update(xml_autogrouptypes_titlesmin[i], automation_mins[i]);
371                         defaults->update(xml_autogrouptypes_titlesmax[i], automation_maxs[i]);
372                 }
373         }
374
375         defaults->update("FLOATAUTO_TYPE", floatauto_type);
376         defaults->update("X_PANE", x_pane);
377         defaults->update("Y_PANE", y_pane);
378         return 0;
379 }
380
381 void LocalSession::set_selectionstart(double value)
382 {
383         this->selectionstart = value;
384 }
385
386 void LocalSession::set_selectionend(double value)
387 {
388         this->selectionend = value;
389 }
390
391 void LocalSession::set_inpoint(double value)
392 {
393         in_point = value;
394 }
395
396 void LocalSession::set_outpoint(double value)
397 {
398         out_point = value;
399 }
400
401 void LocalSession::unset_inpoint()
402 {
403         in_point = -1;
404 }
405
406 void LocalSession::unset_outpoint()
407 {
408         out_point = -1;
409 }
410
411 void LocalSession::set_playback_start(double value)
412 {
413         if( playback_end < 0 ) return;
414         playback_start = value;
415         playback_end = -1;
416 }
417
418 void LocalSession::set_playback_end(double value)
419 {
420         if( value < playback_start ) {
421                 if( playback_end < 0 )
422                         playback_end = playback_start;
423                 playback_start = value;
424         }
425         else if( playback_end < 0 || value > playback_end )
426                 playback_end = value;
427 }
428
429
430 double LocalSession::get_selectionstart(int highlight_only)
431 {
432         if(highlight_only || !EQUIV(selectionstart, selectionend))
433                 return selectionstart;
434
435         if(in_point >= 0)
436                 return in_point;
437         else
438         if(out_point >= 0)
439                 return out_point;
440         else
441                 return selectionstart;
442 }
443
444 double LocalSession::get_selectionend(int highlight_only)
445 {
446         if(highlight_only || !EQUIV(selectionstart, selectionend))
447                 return selectionend;
448
449         if(out_point >= 0)
450                 return out_point;
451         else
452         if(in_point >= 0)
453                 return in_point;
454         else
455                 return selectionend;
456 }
457
458 double LocalSession::get_inpoint()
459 {
460         return in_point;
461 }
462
463 double LocalSession::get_outpoint()
464 {
465         return out_point;
466 }
467
468 int LocalSession::inpoint_valid()
469 {
470         return in_point >= 0;
471 }
472
473 int LocalSession::outpoint_valid()
474 {
475         return out_point >= 0;
476 }
477
478 void LocalSession::set_clip_path(Indexable *indexable)
479 {
480         char string[BCTEXTLEN];
481         FileSystem fs;
482         fs.extract_name(string, indexable->path);
483         strcpy(clip_title, string);
484 }
485
486