a364947a94269793fc6db9c92221f263e97cc167
[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 // Find window with nothing
223         VWindow *vwindow = mwindow->get_viewer(1);
224
225 // TODO: create new vwindow or change current vwindow
226         vwindow->gui->lock_window("ClipPopupView::handle_event");
227
228         if( mwindow->session->drag_assets->total )
229                 vwindow->change_source(
230                         mwindow->session->drag_assets->values[0]);
231         else
232         if( mwindow->session->drag_clips->total )
233                 vwindow->change_source(
234                         mwindow->session->drag_clips->values[0]);
235
236         vwindow->gui->unlock_window();
237         return 1;
238 }
239
240
241 ClipPopupCopy::ClipPopupCopy(MWindow *mwindow, ClipPopup *popup)
242  : BC_MenuItem(_("Copy"))
243 {
244         this->mwindow = mwindow;
245         this->popup = popup;
246 }
247 ClipPopupCopy::~ClipPopupCopy()
248 {
249 }
250
251 int ClipPopupCopy::handle_event()
252 {
253         MWindowGUI *gui = mwindow->gui;
254         gui->lock_window("ClipPopupCopy::handle_event");
255         if( mwindow->session->drag_clips->total > 0 ) {
256                 FileXML file;
257                 EDL *edl = mwindow->session->drag_clips->values[0];
258                 double start = 0, end = edl->tracks->total_length();
259                 edl->copy(start, end, 1, &file, "", 1);
260                 const char *file_string = file.string();
261                 long file_length = strlen(file_string);
262                 gui->to_clipboard(file_string, file_length, SECONDARY_SELECTION);
263                 gui->to_clipboard(file_string, file_length, BC_PRIMARY_SELECTION);
264         }
265         gui->unlock_window(); 
266         return 1;
267 }
268
269
270 ClipPopupPaste::ClipPopupPaste(MWindow *mwindow, ClipPopup *popup)
271  : BC_MenuItem(_("Paste"))
272 {
273         this->mwindow = mwindow;
274         this->popup = popup;
275 }
276
277 ClipPopupPaste::~ClipPopupPaste()
278 {
279 }
280
281 int ClipPopupPaste::handle_event()
282 {
283         popup->paste_assets();
284         return 1;
285 }
286
287
288 ClipMatchSize::ClipMatchSize(MWindow *mwindow, ClipPopup *popup)
289  : BC_MenuItem(_("Match project size"))
290 {
291         this->mwindow = mwindow;
292         this->popup = popup;
293 }
294
295 int ClipMatchSize::handle_event()
296 {
297         popup->match_size();
298         return 1;
299 }
300
301
302 ClipMatchRate::ClipMatchRate(MWindow *mwindow, ClipPopup *popup)
303  : BC_MenuItem(_("Match frame rate"))
304 {
305         this->mwindow = mwindow;
306         this->popup = popup;
307 }
308
309 int ClipMatchRate::handle_event()
310 {
311         popup->match_rate();
312         return 1;
313 }
314
315
316 ClipMatchAll::ClipMatchAll(MWindow *mwindow, ClipPopup *popup)
317  : BC_MenuItem(_("Match all"))
318 {
319         this->mwindow = mwindow;
320         this->popup = popup;
321 }
322
323 int ClipMatchAll::handle_event()
324 {
325         popup->match_all();
326         return 1;
327 }
328
329
330 ClipPopupDelete::ClipPopupDelete(MWindow *mwindow, ClipPopup *popup)
331  : BC_MenuItem(_("Delete"))
332 {
333         this->mwindow = mwindow;
334         this->popup = popup;
335 }
336
337
338 ClipPopupDelete::~ClipPopupDelete()
339 {
340 }
341
342 int ClipPopupDelete::handle_event()
343 {
344         mwindow->remove_assets_from_project(1, 1,
345                 mwindow->session->drag_assets,
346                 mwindow->session->drag_clips);
347         return 1;
348 }
349
350
351 ClipPasteToFolder::ClipPasteToFolder(MWindow *mwindow)
352  : BC_MenuItem(_("Paste Clip"))
353 {
354         this->mwindow = mwindow;
355 }
356
357 int ClipPasteToFolder::handle_event()
358 {
359         MWindowGUI *gui = mwindow->gui;
360         gui->lock_window("ClipPasteToFolder::handle_event 1");
361         int64_t len = gui->clipboard_len(BC_PRIMARY_SELECTION);
362         if( len ) {
363                 char *string = new char[len];
364                 gui->from_clipboard(string, len, BC_PRIMARY_SELECTION);
365                 const char *clip_header = "<EDL VERSION=";
366                 if( !strncmp(clip_header, string, strlen(clip_header)) ) {
367                         FileXML file;
368                         file.read_from_string(string);
369                         EDL *edl = mwindow->edl;
370                         EDL *new_edl = new EDL(mwindow->edl);
371                         new_edl->create_objects();
372                         new_edl->load_xml(&file, LOAD_ALL);
373                         edl->update_assets(new_edl);
374                         mwindow->save_clip(new_edl, _("paste clip: "));
375                 }
376                 else {
377                         char *cp = strchr(string, '\n');
378                         if( cp-string < 32 ) *cp = 0;
379                         else if( len > 32 ) string[32] = 0;
380                         eprintf("paste buffer is not EDL:\n%s", string);
381                 }
382                 delete [] string;
383         }
384         else {
385                 eprintf("paste buffer empty");
386         }
387         gui->unlock_window();
388         return 1;
389 }
390
391
392 ClipListMenu::ClipListMenu(MWindow *mwindow, AWindowGUI *gui)
393  : BC_PopupMenu(0, 0, 0, "", 0)
394 {
395         this->mwindow = mwindow;
396         this->gui = gui;
397 }
398
399 ClipListMenu::~ClipListMenu()
400 {
401 }
402
403 void ClipListMenu::create_objects()
404 {
405         add_item(format = new AWindowListFormat(mwindow, gui));
406         add_item(new AWindowListSort(mwindow, gui));
407         add_item(new ClipPasteToFolder(mwindow));
408         update();
409 }
410
411 void ClipListMenu::update()
412 {
413         format->update();
414 }
415
416
417 ClipPopupNest::ClipPopupNest(MWindow *mwindow, ClipPopup *popup)
418  : BC_MenuItem(_("Nest"))
419 {
420         this->mwindow = mwindow;
421         this->popup = popup;
422 }
423 ClipPopupNest::~ClipPopupNest()
424 {
425 }
426
427 int ClipPopupNest::handle_event()
428 {
429         MWindowGUI *gui = mwindow->gui;
430         gui->lock_window("ClipPopupNest::handle_event 1");
431         if( mwindow->session->drag_clips->total > 0 ) {
432                 EDL *edl = mwindow->edl;
433                 EDL *clip = mwindow->session->drag_clips->values[0];
434                 EDL *clip_edl = new EDL;  // no parent for nested clip
435                 clip_edl->create_objects();
436                 clip_edl->copy_all(clip);
437                 EDL *new_clip = new EDL(edl);
438                 new_clip->create_objects();
439                 new_clip->awindow_folder = AW_CLIP_FOLDER;
440                 snprintf(new_clip->local_session->clip_title,
441                         sizeof(new_clip->local_session->clip_title),
442                         _("Nested: %s"), clip->local_session->clip_title);
443                 strcpy(new_clip->local_session->clip_notes,
444                         clip->local_session->clip_notes);
445                 time_t dt;      time(&dt);
446                 struct tm dtm;  localtime_r(&dt, &dtm);
447                 char path[BCSTRLEN];
448                 sprintf(path, _("Nested_%02d%02d%02d-%02d%02d%02d"),
449                         dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
450                         dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
451                 clip_edl->set_path(path);
452                 sprintf(new_clip->local_session->clip_icon,
453                         "clip_%02d%02d%02d-%02d%02d%02d.png",
454                         dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
455                         dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
456                 new_clip->set_path(path);
457                 new_clip->to_nested(clip_edl);
458                 int idx = edl->clips.number_of(clip);
459                 if( idx >= 0 ) {
460                         edl->clips[idx] = new_clip;
461                         clip->remove_user();
462                 }
463                 else
464                         edl->clips.append(new_clip);
465                 mwindow->mainindexes->add_next_asset(0, clip_edl);
466                 mwindow->mainindexes->start_build();
467                 clip_edl->remove_user();
468                 popup->gui->async_update_assets();
469         }
470         gui->unlock_window();
471         return 1;
472 }
473
474
475 ClipPopupUnNest::ClipPopupUnNest(MWindow *mwindow, ClipPopup *popup)
476  : BC_MenuItem(_("UnNest"))
477 {
478         this->mwindow = mwindow;
479         this->popup = popup;
480 }
481 ClipPopupUnNest::~ClipPopupUnNest()
482 {
483 }
484
485 int ClipPopupUnNest::handle_event()
486 {
487         EDL *nested_edl = 0;
488         MWindowGUI *gui = mwindow->gui;
489         gui->lock_window("ClipPopupUnNest::handle_event 1");
490         if( mwindow->session->drag_clips->total > 0 ) {
491                 EDL *clip = mwindow->session->drag_clips->values[0];
492                 Track *track = clip->tracks->first;
493                 Edit *edit = track ? track->edits->first : 0;
494                 nested_edl = edit && !edit->next && !edit->asset ? edit->nested_edl : 0;
495                 while( nested_edl && (track=track->next)!=0 ) {
496                         Edit *edit = track->edits->first;
497                         if( !edit || edit->next || edit->nested_edl != nested_edl )
498                                 nested_edl = 0;
499                 }
500                 if( nested_edl ) {
501                         EDL *edl = mwindow->edl;
502                         EDL *new_clip = new EDL(edl);
503                         new_clip->create_objects();
504                         new_clip->copy_all(nested_edl);
505                         new_clip->awindow_folder = AW_CLIP_FOLDER;
506                         int idx = edl->clips.number_of(clip);
507                         if( idx >= 0 ) {
508                                 edl->clips[idx] = new_clip;
509                                 clip->remove_user();
510                         }
511                         else
512                                 edl->clips.append(new_clip);
513                         popup->gui->async_update_assets();
514                 }
515         }
516         gui->unlock_window();
517         return 1;
518 }
519