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