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