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