fixes for errs in nested edl rework, proxy popup delete
[goodguy/history.git] / cinelerra-5.1 / cinelerra / clippopup.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 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 "assetedit.h"
23 #include "clippopup.h"
24 #include "assetremove.h"
25 #include "awindow.h"
26 #include "awindowgui.h"
27 #include "bcsignals.h"
28 #include "clipedit.h"
29 #include "clipedls.h"
30 #include "cwindow.h"
31 #include "cwindowgui.h"
32 #include "edit.h"
33 #include "edits.h"
34 #include "edl.h"
35 #include "filexml.h"
36 #include "language.h"
37 #include "localsession.h"
38 #include "mainerror.h"
39 #include "mainindexes.h"
40 #include "mainsession.h"
41 #include "mwindow.h"
42 #include "mwindowgui.h"
43 #include "track.h"
44 #include "tracks.h"
45 #include "vwindow.h"
46 #include "vwindowgui.h"
47
48
49
50 ClipPopup::ClipPopup(MWindow *mwindow, AWindowGUI *gui)
51  : BC_PopupMenu(0, 0, 0, "", 0)
52 {
53         this->mwindow = mwindow;
54         this->gui = gui;
55 }
56
57 ClipPopup::~ClipPopup()
58 {
59 }
60
61 void ClipPopup::create_objects()
62 {
63         BC_MenuItem *menu_item;
64         BC_SubMenu *submenu;
65         add_item(info = new ClipPopupInfo(mwindow, this));
66         add_item(format = new AWindowListFormat(mwindow, gui));
67         add_item(new ClipPopupSort(mwindow, this));
68         add_item(view = new ClipPopupView(mwindow, this));
69         add_item(view_window = new ClipPopupViewWindow(mwindow, this));
70         add_item(new ClipPopupCopy(mwindow, this));
71         add_item(new ClipPopupNest(mwindow, this));
72         add_item(new ClipPopupUnNest(mwindow, this));
73         add_item(new ClipPopupPaste(mwindow, this));
74         add_item(menu_item = new BC_MenuItem(_("Match...")));
75         menu_item->add_submenu(submenu = new BC_SubMenu());
76         submenu->add_submenuitem(new ClipMatchSize(mwindow, this));
77         submenu->add_submenuitem(new ClipMatchRate(mwindow, this));
78         submenu->add_submenuitem(new ClipMatchAll(mwindow, this));
79         add_item(new ClipPopupDelete(mwindow, this));
80 }
81
82 void ClipPopup::paste_assets()
83 {
84 // Collect items into the drag vectors for temporary storage
85         gui->lock_window("ClipPopup::paste_assets");
86         mwindow->gui->lock_window("ClipPopup::paste_assets");
87         mwindow->cwindow->gui->lock_window("ClipPopup::paste_assets");
88
89         gui->collect_assets();
90         mwindow->paste_assets(mwindow->edl->local_session->get_selectionstart(1),
91                 mwindow->edl->tracks->first,
92                 0);   // do not overwrite
93
94         gui->unlock_window();
95         mwindow->gui->unlock_window();
96         mwindow->cwindow->gui->unlock_window();
97 }
98
99 void ClipPopup::match_size()
100 {
101 // Collect items into the drag vectors for temporary storage
102         gui->collect_assets();
103         mwindow->gui->lock_window("ClipPopup::match_size");
104         mwindow->asset_to_size();
105         mwindow->gui->unlock_window();
106 }
107
108 void ClipPopup::match_rate()
109 {
110 // Collect items into the drag vectors for temporary storage
111         gui->collect_assets();
112         mwindow->gui->lock_window("ClipPopup::match_rate");
113         mwindow->asset_to_rate();
114         mwindow->gui->unlock_window();
115 }
116
117 void ClipPopup::match_all()
118 {
119 // Collect items into the drag vectors for temporary storage
120         gui->collect_assets();
121         mwindow->gui->lock_window("ClipPopup::match_rate");
122         mwindow->asset_to_all();
123         mwindow->gui->unlock_window();
124 }
125
126 int ClipPopup::update()
127 {
128         format->update();
129         gui->collect_assets();
130         return 0;
131 }
132
133
134 ClipPopupInfo::ClipPopupInfo(MWindow *mwindow, ClipPopup *popup)
135  : BC_MenuItem(_("Info..."))
136 {
137         this->mwindow = mwindow;
138         this->popup = popup;
139 }
140
141 ClipPopupInfo::~ClipPopupInfo()
142 {
143 }
144
145 int ClipPopupInfo::handle_event()
146 {
147         int cur_x, cur_y;
148         popup->gui->get_abs_cursor(cur_x, cur_y, 0);
149
150         if( mwindow->session->drag_assets->total ) {
151                 AssetEdit *asset_edit = mwindow->awindow->get_asset_editor();
152                 asset_edit->edit_asset(
153                         mwindow->session->drag_assets->values[0], cur_x, cur_y);
154         }
155         else
156         if( mwindow->session->drag_clips->total ) {
157                 popup->gui->awindow->clip_edit->edit_clip(
158                         mwindow->session->drag_clips->values[0], cur_x, cur_y);
159         }
160         return 1;
161 }
162
163
164 ClipPopupSort::ClipPopupSort(MWindow *mwindow, ClipPopup *popup)
165  : BC_MenuItem(_("Sort items"))
166 {
167         this->mwindow = mwindow;
168         this->popup = popup;
169 }
170
171 ClipPopupSort::~ClipPopupSort()
172 {
173 }
174
175 int ClipPopupSort::handle_event()
176 {
177         mwindow->awindow->gui->sort_assets(0);
178         return 1;
179 }
180
181
182 ClipPopupView::ClipPopupView(MWindow *mwindow, ClipPopup *popup)
183  : BC_MenuItem(_("View"))
184 {
185         this->mwindow = mwindow;
186         this->popup = popup;
187 }
188
189 ClipPopupView::~ClipPopupView()
190 {
191 }
192
193 int ClipPopupView::handle_event()
194 {
195         VWindow *vwindow = mwindow->get_viewer(1, DEFAULT_VWINDOW);
196
197         if( mwindow->session->drag_assets->total )
198                 vwindow->change_source(
199                         mwindow->session->drag_assets->values[0]);
200         else
201         if( mwindow->session->drag_clips->total )
202                 vwindow->change_source(
203                         mwindow->session->drag_clips->values[0]);
204
205         return 1;
206 }
207
208
209 ClipPopupViewWindow::ClipPopupViewWindow(MWindow *mwindow, ClipPopup *popup)
210  : BC_MenuItem(_("View in new window"))
211 {
212         this->mwindow = mwindow;
213         this->popup = popup;
214 }
215
216 ClipPopupViewWindow::~ClipPopupViewWindow()
217 {
218 }
219
220 int ClipPopupViewWindow::handle_event()
221 {
222         for( int i=0; i<mwindow->session->drag_assets->size(); ++i ) {
223                 VWindow *vwindow = mwindow->get_viewer(1);
224                 vwindow->gui->lock_window("ClipPopupView::handle_event 1");
225                 vwindow->change_source(mwindow->session->drag_assets->get(i));
226                 vwindow->gui->unlock_window();
227         }
228         for( int i=0; i<mwindow->session->drag_clips->size(); ++i ) {
229                 VWindow *vwindow = mwindow->get_viewer(1);
230                 vwindow->gui->lock_window("ClipPopupView::handle_event 2");
231                 vwindow->change_source(mwindow->session->drag_clips->get(i));
232                 vwindow->gui->unlock_window();
233         }
234         return 1;
235 }
236
237
238 ClipPopupCopy::ClipPopupCopy(MWindow *mwindow, ClipPopup *popup)
239  : BC_MenuItem(_("Copy"))
240 {
241         this->mwindow = mwindow;
242         this->popup = popup;
243 }
244 ClipPopupCopy::~ClipPopupCopy()
245 {
246 }
247
248 int ClipPopupCopy::handle_event()
249 {
250         MWindowGUI *gui = mwindow->gui;
251         gui->lock_window("ClipPopupCopy::handle_event");
252         if( mwindow->session->drag_clips->total > 0 ) {
253                 EDL *edl = mwindow->session->drag_clips->values[0];
254                 EDL *copy_edl = new EDL; // no parent or assets wont be copied
255                 copy_edl->create_objects();
256                 copy_edl->copy_all(edl);
257                 FileXML file;
258                 double start = 0, end = edl->tracks->total_length();
259                 copy_edl->copy(start, end, 1, &file, "", 1);
260                 copy_edl->remove_user();
261                 const char *file_string = file.string();
262                 long file_length = strlen(file_string);
263                 gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
264                 gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
265         }
266         gui->unlock_window(); 
267         return 1;
268 }
269
270
271 ClipPopupPaste::ClipPopupPaste(MWindow *mwindow, ClipPopup *popup)
272  : BC_MenuItem(_("Paste"))
273 {
274         this->mwindow = mwindow;
275         this->popup = popup;
276 }
277
278 ClipPopupPaste::~ClipPopupPaste()
279 {
280 }
281
282 int ClipPopupPaste::handle_event()
283 {
284         popup->paste_assets();
285         return 1;
286 }
287
288
289 ClipMatchSize::ClipMatchSize(MWindow *mwindow, ClipPopup *popup)
290  : BC_MenuItem(_("Match project size"))
291 {
292         this->mwindow = mwindow;
293         this->popup = popup;
294 }
295
296 int ClipMatchSize::handle_event()
297 {
298         popup->match_size();
299         return 1;
300 }
301
302
303 ClipMatchRate::ClipMatchRate(MWindow *mwindow, ClipPopup *popup)
304  : BC_MenuItem(_("Match frame rate"))
305 {
306         this->mwindow = mwindow;
307         this->popup = popup;
308 }
309
310 int ClipMatchRate::handle_event()
311 {
312         popup->match_rate();
313         return 1;
314 }
315
316
317 ClipMatchAll::ClipMatchAll(MWindow *mwindow, ClipPopup *popup)
318  : BC_MenuItem(_("Match all"))
319 {
320         this->mwindow = mwindow;
321         this->popup = popup;
322 }
323
324 int ClipMatchAll::handle_event()
325 {
326         popup->match_all();
327         return 1;
328 }
329
330
331 ClipPopupDelete::ClipPopupDelete(MWindow *mwindow, ClipPopup *popup)
332  : BC_MenuItem(_("Delete"))
333 {
334         this->mwindow = mwindow;
335         this->popup = popup;
336 }
337
338
339 ClipPopupDelete::~ClipPopupDelete()
340 {
341 }
342
343 int ClipPopupDelete::handle_event()
344 {
345         mwindow->remove_assets_from_project(1, 1,
346                 mwindow->session->drag_assets,
347                 mwindow->session->drag_clips);
348         return 1;
349 }
350
351
352 ClipPasteToFolder::ClipPasteToFolder(MWindow *mwindow)
353  : BC_MenuItem(_("Paste Clip"))
354 {
355         this->mwindow = mwindow;
356 }
357
358 int ClipPasteToFolder::handle_event()
359 {
360         MWindowGUI *gui = mwindow->gui;
361         gui->lock_window("ClipPasteToFolder::handle_event 1");
362         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
363         if( len ) {
364                 char *string = new char[len];
365                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
366                 const char *clip_header = "<EDL VERSION=";
367                 if( !strncmp(clip_header, string, strlen(clip_header)) ) {
368                         FileXML file;
369                         file.read_from_string(string);
370                         EDL *edl = mwindow->edl;
371                         EDL *new_edl = new EDL(mwindow->edl);
372                         new_edl->create_objects();
373                         new_edl->load_xml(&file, LOAD_ALL);
374                         edl->update_assets(new_edl);
375                         mwindow->save_clip(new_edl, _("paste clip: "));
376                 }
377                 else {
378                         char *cp = strchr(string, '\n');
379                         if( cp-string < 32 ) *cp = 0;
380                         else if( len > 32 ) string[32] = 0;
381                         eprintf("paste buffer is not EDL:\n%s", string);
382                 }
383                 delete [] string;
384         }
385         else {
386                 eprintf("paste buffer empty");
387         }
388         gui->unlock_window();
389         return 1;
390 }
391
392
393 ClipListMenu::ClipListMenu(MWindow *mwindow, AWindowGUI *gui)
394  : BC_PopupMenu(0, 0, 0, "", 0)
395 {
396         this->mwindow = mwindow;
397         this->gui = gui;
398 }
399
400 ClipListMenu::~ClipListMenu()
401 {
402 }
403
404 void ClipListMenu::create_objects()
405 {
406         add_item(format = new AWindowListFormat(mwindow, gui));
407         add_item(new AWindowListSort(mwindow, gui));
408         add_item(new ClipPasteToFolder(mwindow));
409         update();
410 }
411
412 void ClipListMenu::update()
413 {
414         format->update();
415 }
416
417
418 ClipPopupNest::ClipPopupNest(MWindow *mwindow, ClipPopup *popup)
419  : BC_MenuItem(_("Nest"))
420 {
421         this->mwindow = mwindow;
422         this->popup = popup;
423 }
424 ClipPopupNest::~ClipPopupNest()
425 {
426 }
427
428 int ClipPopupNest::handle_event()
429 {
430         MWindowGUI *gui = mwindow->gui;
431         gui->lock_window("ClipPopupNest::handle_event 1");
432         if( mwindow->session->drag_clips->total > 0 ) {
433                 EDL *edl = mwindow->edl;
434                 time_t dt;      time(&dt);
435                 struct tm dtm;  localtime_r(&dt, &dtm);
436                 char path[BCSTRLEN];
437                 sprintf(path, _("Nested_%02d%02d%02d-%02d%02d%02d"),
438                         dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
439                         dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
440                 EDL *clip = mwindow->session->drag_clips->values[0];
441                 EDL *nested = edl->new_nested(clip, path);
442                 EDL *new_clip = edl->create_nested_clip(nested);
443                 new_clip->awindow_folder = AW_CLIP_FOLDER;
444                 sprintf(new_clip->local_session->clip_icon,
445                         "clip_%02d%02d%02d-%02d%02d%02d.png",
446                         dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
447                         dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
448                 snprintf(new_clip->local_session->clip_title,
449                         sizeof(new_clip->local_session->clip_title),
450                         _("Nested: %s"), clip->local_session->clip_title);
451                 strcpy(new_clip->local_session->clip_notes,
452                         clip->local_session->clip_notes);
453                 int idx = edl->clips.number_of(clip);
454                 if( idx >= 0 ) {
455                         edl->clips[idx] = new_clip;
456                         clip->remove_user();
457                 }
458                 else
459                         edl->clips.append(new_clip);
460                 mwindow->mainindexes->add_next_asset(0, nested);
461                 mwindow->mainindexes->start_build();
462                 popup->gui->async_update_assets();
463         }
464         gui->unlock_window();
465         return 1;
466 }
467
468
469 ClipPopupUnNest::ClipPopupUnNest(MWindow *mwindow, ClipPopup *popup)
470  : BC_MenuItem(_("UnNest"))
471 {
472         this->mwindow = mwindow;
473         this->popup = popup;
474 }
475 ClipPopupUnNest::~ClipPopupUnNest()
476 {
477 }
478
479 int ClipPopupUnNest::handle_event()
480 {
481         EDL *nested_edl = 0;
482         MWindowGUI *gui = mwindow->gui;
483         gui->lock_window("ClipPopupUnNest::handle_event 1");
484         if( mwindow->session->drag_clips->total > 0 ) {
485                 EDL *clip = mwindow->session->drag_clips->values[0];
486                 Track *track = clip->tracks->first;
487                 Edit *edit = track ? track->edits->first : 0;
488                 nested_edl = edit && !edit->next && !edit->asset ? edit->nested_edl : 0;
489                 while( nested_edl && (track=track->next)!=0 ) {
490                         Edit *edit = track->edits->first;
491                         if( !edit || edit->next || edit->nested_edl != nested_edl )
492                                 nested_edl = 0;
493                 }
494                 if( nested_edl ) {
495                         EDL *edl = mwindow->edl;
496                         EDL *new_clip = new EDL(edl);
497                         new_clip->create_objects();
498                         new_clip->copy_all(nested_edl);
499                         new_clip->awindow_folder = AW_CLIP_FOLDER;
500                         int idx = edl->clips.number_of(clip);
501                         if( idx >= 0 ) {
502                                 edl->clips[idx] = new_clip;
503                                 clip->remove_user();
504                         }
505                         else
506                                 edl->clips.append(new_clip);
507                         popup->gui->async_update_assets();
508                 }
509         }
510         gui->unlock_window();
511         return 1;
512 }
513