51e3f886a609b0e7c3f97db366045c37a26c1b90
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / assetpopup.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 "asset.h"
23 #include "assetedit.h"
24 #include "assetpopup.h"
25 #include "assetremove.h"
26 #include "assets.h"
27 #include "awindow.h"
28 #include "awindowgui.h"
29 #include "bccapture.h"
30 #include "bcdisplayinfo.h"
31 #include "bcsignals.h"
32 #include "cache.h"
33 #include "clipedit.h"
34 #include "cstrdup.h"
35 #include "cwindow.h"
36 #include "cwindowgui.h"
37 #include "edl.h"
38 #include "edlsession.h"
39 #include "file.h"
40 #include "filesystem.h"
41 #include "filexml.h"
42 #include "language.h"
43 #include "loadfile.h"
44 #include "localsession.h"
45 #include "mainerror.h"
46 #include "mainindexes.h"
47 #include "mainmenu.h"
48 #include "mainsession.h"
49 #include "mwindow.h"
50 #include "mwindowgui.h"
51 #include "preferences.h"
52 #include "renderengine.h"
53 #include "tracks.h"
54 #include "transportque.h"
55 #include "vframe.h"
56 #include "vrender.h"
57 #include "vwindow.h"
58 #include "vwindowgui.h"
59 #include "zwindow.h"
60
61
62 AssetPopup::AssetPopup(MWindow *mwindow, AWindowGUI *gui)
63  : BC_PopupMenu(0, 0, 0, "", 0)
64 {
65         this->mwindow = mwindow;
66         this->gui = gui;
67 }
68
69 AssetPopup::~AssetPopup()
70 {
71 }
72
73 void AssetPopup::create_objects()
74 {
75         BC_MenuItem *menu_item;
76         BC_SubMenu *submenu;
77         add_item(info = new AssetPopupInfo(mwindow, this));
78         add_item(format = new AWindowListFormat(mwindow, gui));
79         add_item(open_edl = new AssetPopupOpenEDL(mwindow, this));
80         add_item(close_edl = new AssetPopupCloseEDL(mwindow, gui));
81         add_item(to_clip = new AssetPopupToClip(mwindow, this));
82         add_item(sort = new AssetPopupSort(mwindow, this));
83         add_item(index = new AssetPopupBuildIndex(mwindow, this));
84         add_item(view = new AssetPopupView(mwindow, this));
85         add_item(view_window = new AssetPopupViewWindow(mwindow, this));
86         add_item(open_mixer = new AssetPopupOpenMixer(mwindow, this));
87         add_item(insert_mixer = new AssetPopupInsertMixer(mwindow, this));
88         add_item(paste = new AssetPopupPaste(mwindow, this));
89         add_item(menu_item = new BC_MenuItem(_("Match...")));
90         menu_item->add_submenu(submenu = new BC_SubMenu());
91         submenu->add_submenuitem(new AssetMatchSize(mwindow, this));
92         submenu->add_submenuitem(new AssetMatchRate(mwindow, this));
93         submenu->add_submenuitem(new AssetMatchAll(mwindow, this));
94         add_item(menu_item = new BC_MenuItem(_("Remove...")));
95         menu_item->add_submenu(submenu = new BC_SubMenu());
96         submenu->add_submenuitem(new AssetPopupProjectRemove(mwindow, this));
97         submenu->add_submenuitem(new AssetPopupDiskRemove(mwindow, this));
98 }
99
100 void AssetPopup::paste_assets()
101 {
102 // Collect items into the drag vectors for temporary storage
103         gui->lock_window("AssetPopup::paste_assets");
104         mwindow->gui->lock_window("AssetPopup::paste_assets");
105         mwindow->cwindow->gui->lock_window("AssetPopup::paste_assets");
106
107         int proxy = mwindow->edl->session->awindow_folder == AW_PROXY_FOLDER ? 1 : 0;
108         gui->collect_assets(proxy);
109         mwindow->paste_assets(mwindow->edl->local_session->get_selectionstart(1),
110                 mwindow->edl->tracks->first, 0);   // do not overwrite
111
112         gui->unlock_window();
113         mwindow->gui->unlock_window();
114         mwindow->cwindow->gui->unlock_window();
115 }
116
117 void AssetPopup::match_size()
118 {
119 // Collect items into the drag vectors for temporary storage
120         gui->collect_assets();
121         mwindow->gui->lock_window("AssetPopup::match_size");
122         mwindow->asset_to_size();
123         mwindow->gui->unlock_window();
124 }
125
126 void AssetPopup::match_rate()
127 {
128 // Collect items into the drag vectors for temporary storage
129         gui->collect_assets();
130         mwindow->gui->lock_window("AssetPopup::match_rate");
131         mwindow->asset_to_rate();
132         mwindow->gui->unlock_window();
133 }
134
135 void AssetPopup::match_all()
136 {
137 // Collect items into the drag vectors for temporary storage
138         gui->collect_assets();
139         mwindow->gui->lock_window("AssetPopup::match_rate");
140         mwindow->asset_to_all();
141         mwindow->gui->unlock_window();
142 }
143
144 int AssetPopup::update()
145 {
146         format->update();
147         int proxy = mwindow->edl->session->awindow_folder == AW_PROXY_FOLDER ? 1 : 0;
148         gui->collect_assets(proxy);
149         int enable_open = 0;
150         int assets_total = mwindow->session->drag_assets->size();
151         Indexable *idxbl = !assets_total ? 0 :
152                 mwindow->session->drag_assets->get(0);
153         if( idxbl ) {
154                 if( idxbl->is_asset ) {
155                         Asset *asset = (Asset *)idxbl;
156                         if( asset->format == FILE_REF )
157                                 enable_open = 1;
158                 }
159                 else
160                         enable_open = 1;
161         }
162         open_edl->set_enabled(enable_open);
163         int enable_close = mwindow->stack.size() > 0 ? 1 : 0;
164         close_edl->set_enabled(enable_close);
165         int enable_clip = 0;
166         for( int i=0; !enable_clip && i<assets_total; ++i ) {
167                 Indexable *idxbl = mwindow->session->drag_assets->get(i);
168                 if( !idxbl->is_asset ) enable_clip = 1;
169         }
170         to_clip->set_enabled(enable_clip);
171         return 0;
172 }
173
174
175 AssetPopupInfo::AssetPopupInfo(MWindow *mwindow, AssetPopup *popup)
176  : BC_MenuItem(_("Info..."))
177 {
178         this->mwindow = mwindow;
179         this->popup = popup;
180 }
181
182 AssetPopupInfo::~AssetPopupInfo()
183 {
184 }
185
186 int AssetPopupInfo::handle_event()
187 {
188         int cur_x, cur_y;
189         popup->gui->get_abs_cursor(cur_x, cur_y);
190         int n = mwindow->session->drag_assets->size();
191         if( n > 0 ) {
192                 int xs30 = xS(30), ys30 = yS(30);
193                 for( int i=0; i<n; ++i ) {
194                         AssetEdit *asset_edit = mwindow->awindow->get_asset_editor();
195                         asset_edit->edit_asset(
196                                 mwindow->session->drag_assets->values[i], cur_x-xs30*i, cur_y-ys30*i);
197                 }
198         }
199         else if( mwindow->session->drag_clips->size() ) {
200                 popup->gui->awindow->clip_edit->edit_clip(
201                         mwindow->session->drag_clips->values[0], cur_x, cur_y);
202         }
203         return 1;
204 }
205
206
207 AssetPopupOpenEDL::AssetPopupOpenEDL(MWindow *mwindow, AssetPopup *popup)
208  : BC_MenuItem(_("Open EDL"))
209 {
210         this->mwindow = mwindow;
211         this->popup = popup;
212 }
213 AssetPopupOpenEDL::~AssetPopupOpenEDL()
214 {
215 }
216
217 int AssetPopupOpenEDL::handle_event()
218 {
219         int assets_total = mwindow->session->drag_assets->size();
220         if( !assets_total ) return 1;
221         popup->unlock_window();
222         EDL *edl = 0;
223         Indexable *idxbl = mwindow->session->drag_assets->get(0);
224         if( idxbl->is_asset ) {
225                 Asset *asset = (Asset *)idxbl;
226                 if( asset->format == FILE_REF ) {
227                         FileXML xml_file;
228                         const char *filename = asset->path;
229                         if( xml_file.read_from_file(filename, 1) ) {
230                                 eprintf(_("Error: unable to open:\n  %s"), filename);
231                                 return 1;
232                         }
233                         edl = new EDL;
234                         edl->create_objects();
235                         if( edl->load_xml(&xml_file, LOAD_ALL) ) {
236                                 eprintf(_("Error: unable to load:\n  %s"), filename);
237                                 edl->remove_user();
238                                 return 1;
239                         }
240                 }
241         }
242         else {
243                 edl = (EDL *)idxbl;
244         }
245         if( edl )
246                 mwindow->stack_push(edl, idxbl);
247         else
248                 eprintf(_("media is not EDL:\n%s"), idxbl->path);
249         popup->lock_window("AssetPopupOpenEDL::handle_event");
250         return 1;
251 }
252
253 AssetPopupCloseEDL::AssetPopupCloseEDL(MWindow *mwindow, AWindowGUI *gui)
254  : BC_MenuItem(_("Close EDL"))
255 {
256         this->mwindow = mwindow;
257         this->gui = gui;
258 }
259 AssetPopupCloseEDL::~AssetPopupCloseEDL()
260 {
261 }
262
263 int AssetPopupCloseEDL::handle_event()
264 {
265         gui->unlock_window();
266         mwindow->gui->lock_window("AssetPopupCloseEDL::handle_event");
267         mwindow->stack_pop();
268         mwindow->gui->unlock_window();
269         gui->lock_window("AssetPopupCloseEDL::handle_event");
270         return 1;
271 }
272
273
274 AssetPopupToClip::AssetPopupToClip(MWindow *mwindow, AssetPopup *popup)
275  : BC_MenuItem(_("EDL to clip"))
276 {
277         this->mwindow = mwindow;
278         this->popup = popup;
279 }
280
281 AssetPopupToClip::~AssetPopupToClip()
282 {
283 }
284
285 int AssetPopupToClip::handle_event()
286 {
287         mwindow->media_to_clip();
288         return 1;
289 }
290
291
292 AssetPopupBuildIndex::AssetPopupBuildIndex(MWindow *mwindow, AssetPopup *popup)
293  : BC_MenuItem(_("Rebuild index"))
294 {
295         this->mwindow = mwindow;
296         this->popup = popup;
297 }
298
299 AssetPopupBuildIndex::~AssetPopupBuildIndex()
300 {
301 }
302
303 int AssetPopupBuildIndex::handle_event()
304 {
305 //printf("AssetPopupBuildIndex::handle_event 1\n");
306         mwindow->rebuild_indices();
307         return 1;
308 }
309
310
311 AssetPopupSort::AssetPopupSort(MWindow *mwindow, AssetPopup *popup)
312  : BC_MenuItem(_("Sort"))
313 {
314         this->mwindow = mwindow;
315         this->popup = popup;
316 }
317
318 AssetPopupSort::~AssetPopupSort()
319 {
320 }
321
322 int AssetPopupSort::handle_event()
323 {
324         mwindow->awindow->gui->sort_assets();
325         return 1;
326 }
327
328
329 AssetPopupView::AssetPopupView(MWindow *mwindow, AssetPopup *popup)
330  : BC_MenuItem(_("View"))
331 {
332         this->mwindow = mwindow;
333         this->popup = popup;
334 }
335
336 AssetPopupView::~AssetPopupView()
337 {
338 }
339
340 int AssetPopupView::handle_event()
341 {
342         VWindow *vwindow = mwindow->get_viewer(1, DEFAULT_VWINDOW);
343
344         if( mwindow->session->drag_assets->total )
345                 vwindow->change_source(
346                         mwindow->session->drag_assets->values[0]);
347         else
348         if( mwindow->session->drag_clips->total )
349                 vwindow->change_source(
350                         mwindow->session->drag_clips->values[0]);
351
352         return 1;
353 }
354
355
356 AssetPopupViewWindow::AssetPopupViewWindow(MWindow *mwindow, AssetPopup *popup)
357  : BC_MenuItem(_("View in new window"))
358 {
359         this->mwindow = mwindow;
360         this->popup = popup;
361 }
362
363 AssetPopupViewWindow::~AssetPopupViewWindow()
364 {
365 }
366
367 int AssetPopupViewWindow::handle_event()
368 {
369         for( int i=0; i<mwindow->session->drag_assets->size(); ++i ) {
370                 VWindow *vwindow = mwindow->get_viewer(1);
371                 vwindow->gui->lock_window("AssetPopupView::handle_event 1");
372                 vwindow->change_source(mwindow->session->drag_assets->get(i));
373                 vwindow->gui->unlock_window();
374         }
375         for( int i=0; i<mwindow->session->drag_clips->size(); ++i ) {
376                 VWindow *vwindow = mwindow->get_viewer(1);
377                 vwindow->gui->lock_window("AssetPopupView::handle_event 2");
378                 vwindow->change_source(mwindow->session->drag_clips->get(i));
379                 vwindow->gui->unlock_window();
380         }
381         return 1;
382 }
383
384 AssetPopupOpenMixer::AssetPopupOpenMixer(MWindow *mwindow, AssetPopup *popup)
385  : BC_MenuItem(_("Open Mixers"))
386 {
387         this->mwindow = mwindow;
388         this->popup = popup;
389 }
390
391 AssetPopupOpenMixer::~AssetPopupOpenMixer()
392 {
393 }
394
395 int AssetPopupOpenMixer::handle_event()
396 {
397         mwindow->gui->lock_window("AssetPopupOpenMixer::handle_event");
398         mwindow->create_mixers();
399         mwindow->gui->unlock_window();
400         return 1;
401 }
402
403 AssetPopupInsertMixer::AssetPopupInsertMixer(MWindow *mwindow, AssetPopup *popup)
404  : BC_MenuItem(_("Insert Mixers"))
405 {
406         this->mwindow = mwindow;
407         this->popup = popup;
408 }
409
410 AssetPopupInsertMixer::~AssetPopupInsertMixer()
411 {
412 }
413
414 int AssetPopupInsertMixer::handle_event()
415 {
416         mwindow->gui->lock_window("AssetPopupInsertMixer::handle_event");
417         mwindow->create_mixers(-1);
418         mwindow->gui->unlock_window();
419         return 1;
420 }
421
422 AssetPopupPaste::AssetPopupPaste(MWindow *mwindow, AssetPopup *popup)
423  : BC_MenuItem(_("Paste"))
424 {
425         this->mwindow = mwindow;
426         this->popup = popup;
427 }
428
429 AssetPopupPaste::~AssetPopupPaste()
430 {
431 }
432
433 int AssetPopupPaste::handle_event()
434 {
435         popup->paste_assets();
436         return 1;
437 }
438
439
440 AssetMatchSize::AssetMatchSize(MWindow *mwindow, AssetPopup *popup)
441  : BC_MenuItem(_("Match project size"))
442 {
443         this->mwindow = mwindow;
444         this->popup = popup;
445 }
446
447 int AssetMatchSize::handle_event()
448 {
449         popup->match_size();
450         return 1;
451 }
452
453 AssetMatchRate::AssetMatchRate(MWindow *mwindow, AssetPopup *popup)
454  : BC_MenuItem(_("Match frame rate"))
455 {
456         this->mwindow = mwindow;
457         this->popup = popup;
458 }
459
460 int AssetMatchRate::handle_event()
461 {
462         popup->match_rate();
463         return 1;
464 }
465
466 AssetMatchAll::AssetMatchAll(MWindow *mwindow, AssetPopup *popup)
467  : BC_MenuItem(_("Match all"))
468 {
469         this->mwindow = mwindow;
470         this->popup = popup;
471 }
472
473 int AssetMatchAll::handle_event()
474 {
475         popup->match_all();
476         return 1;
477 }
478
479
480 AssetPopupProjectRemove::AssetPopupProjectRemove(MWindow *mwindow, AssetPopup *popup)
481  : BC_MenuItem(_("Remove from project"))
482 {
483         this->mwindow = mwindow;
484         this->popup = popup;
485 }
486
487 AssetPopupProjectRemove::~AssetPopupProjectRemove()
488 {
489 }
490
491 int AssetPopupProjectRemove::handle_event()
492 {
493         popup->gui->unlock_window();
494         mwindow->remove_assets_from_project(1, 1, 1,
495                 mwindow->session->drag_assets, 0);
496         popup->gui->lock_window("AssetPopupProjectRemove::handle_event");
497         return 1;
498 }
499
500
501 AssetPopupDiskRemove::AssetPopupDiskRemove(MWindow *mwindow, AssetPopup *popup)
502  : BC_MenuItem(_("Remove from disk"))
503 {
504         this->mwindow = mwindow;
505         this->popup = popup;
506 }
507
508
509 AssetPopupDiskRemove::~AssetPopupDiskRemove()
510 {
511 }
512
513 int AssetPopupDiskRemove::handle_event()
514 {
515         mwindow->awindow->asset_remove->start();
516         return 1;
517 }
518
519
520 AssetListMenu::AssetListMenu(MWindow *mwindow, AWindowGUI *gui)
521  : BC_PopupMenu(0, 0, 0, "", 0)
522 {
523         this->mwindow = mwindow;
524         this->gui = gui;
525 }
526
527 AssetListMenu::~AssetListMenu()
528 {
529 }
530
531 void AssetListMenu::create_objects()
532 {
533         add_item(load_file = new AssetPopupLoadFile(mwindow, gui));
534         add_item(format = new AWindowListFormat(mwindow, gui));
535         add_item(close_edl = new AssetPopupCloseEDL(mwindow, gui));
536         add_item(select_used = new AssetSelectUsed(mwindow, gui));
537         BC_SubMenu *submenu;
538         select_used->add_submenu(submenu = new BC_SubMenu());
539         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("All"), SELECT_ALL));
540         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("Used"), SELECT_USED));
541         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("Unused"), SELECT_UNUSED));
542         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("None"), SELECT_NONE));
543         add_item(new AWindowListSort(mwindow, gui));
544         add_item(new AssetListCopy(mwindow, gui));
545         add_item(new AssetListPaste(mwindow, gui));
546         SnapshotSubMenu *snapshot_submenu;
547         add_item(asset_snapshot = new AssetSnapshot(mwindow, this));
548         asset_snapshot->add_submenu(snapshot_submenu = new SnapshotSubMenu(asset_snapshot));
549         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("png"),  SNAPSHOT_PNG));
550         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("jpeg"), SNAPSHOT_JPEG));
551         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("tiff"), SNAPSHOT_TIFF));
552         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("ppm"),  SNAPSHOT_PPM));
553         GrabshotSubMenu *grabshot_submenu;
554         add_item(asset_grabshot = new AssetGrabshot(mwindow, this));
555         asset_grabshot->add_submenu(grabshot_submenu = new GrabshotSubMenu(asset_grabshot));
556         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("png"),  GRABSHOT_PNG));
557         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("jpeg"), GRABSHOT_JPEG));
558         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("tiff"), GRABSHOT_TIFF));
559         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("ppm"),  GRABSHOT_PPM));
560         update_titles(1);
561 }
562
563 AssetPopupLoadFile::AssetPopupLoadFile(MWindow *mwindow, AWindowGUI *gui)
564  : BC_MenuItem(_("Load files..."), "o", 'o')
565 {
566         this->mwindow = mwindow;
567         this->gui = gui;
568 }
569
570 AssetPopupLoadFile::~AssetPopupLoadFile()
571 {
572 }
573
574 int AssetPopupLoadFile::handle_event()
575 {
576         mwindow->gui->mainmenu->load_file->thread->start();
577         return 1;
578 }
579
580 void AssetListMenu::update_titles(int shots)
581 {
582         format->update();
583         int enable_close = mwindow->stack.size() > 0 ? 1 : 0;
584         close_edl->set_enabled(enable_close);
585         asset_snapshot->set_enabled(shots);
586         asset_grabshot->set_enabled(shots);
587 }
588
589 AssetListCopy::AssetListCopy(MWindow *mwindow, AWindowGUI *gui)
590  : BC_MenuItem(_("Copy file list"))
591 {
592         this->mwindow = mwindow;
593         this->gui = gui;
594         copy_dialog = 0;
595 }
596 AssetListCopy::~AssetListCopy()
597 {
598         delete copy_dialog;
599 }
600
601 int AssetListCopy::handle_event()
602 {
603         int len = 0;
604         MWindowGUI *gui = mwindow->gui;
605         gui->lock_window("AssetListCopy::handle_event");
606         mwindow->awindow->gui->collect_assets();
607         int n = mwindow->session->drag_assets->total;
608         for( int i=0; i<n; ++i ) {
609                 Indexable *indexable = mwindow->session->drag_assets->values[i];
610                 const char *path = indexable->path;
611                 if( !*path ) continue;
612                 len += strlen(path) + 1;
613         }
614         char *text = new char[len+1], *cp = text;
615         for( int i=0; i<n; ++i ) {
616                 Indexable *indexable = mwindow->session->drag_assets->values[i];
617                 const char *path = indexable->path;
618                 if( !*path ) continue;
619                 cp += sprintf(cp, "%s\n", path);
620         }
621         *cp = 0;
622         int cur_x, cur_y;
623         gui->get_abs_cursor(cur_x, cur_y, 0);
624         gui->unlock_window(); 
625
626         if( n ) {
627                 if( !copy_dialog )
628                         copy_dialog = new AssetCopyDialog(this);
629                 copy_dialog->start(text, cur_x, cur_y);
630         }
631         else {
632                 eprintf(_("Nothing selected"));
633                 delete [] text;
634         }
635         return 1;
636 }
637
638 AssetCopyDialog::AssetCopyDialog(AssetListCopy *copy)
639  : BC_DialogThread()
640 {
641         this->copy = copy;
642         copy_window = 0;
643 }
644
645 void AssetCopyDialog::start(char *text, int x, int y)
646 {
647         close_window();
648         this->text = text;
649         this->x = x;  this->y = y;
650         BC_DialogThread::start();
651 }
652
653 AssetCopyDialog::~AssetCopyDialog()
654 {
655         close_window();
656 }
657
658 BC_Window* AssetCopyDialog::new_gui()
659 {
660         BC_DisplayInfo display_info;
661
662         copy_window = new AssetCopyWindow(this);
663         copy_window->create_objects();
664         return copy_window;
665 }
666
667 void AssetCopyDialog::handle_done_event(int result)
668 {
669         delete [] text;  text = 0;
670 }
671
672 void AssetCopyDialog::handle_close_event(int result)
673 {
674         copy_window = 0;
675 }
676
677 #define ACW_W xS(500)
678 #define ACW_H yS(200)
679
680 AssetCopyWindow::AssetCopyWindow(AssetCopyDialog *copy_dialog)
681  : BC_Window(_(PROGRAM_NAME ": Copy File List"),
682         copy_dialog->x - ACW_W/2, copy_dialog->y - ACW_H/2,
683         ACW_W, ACW_H, ACW_W, ACW_H, 1, 0, 1)
684 {
685         this->copy_dialog = copy_dialog;
686 }
687
688 AssetCopyWindow::~AssetCopyWindow()
689 {
690 }
691
692 void AssetCopyWindow::create_objects()
693 {
694         lock_window("AssetCopyWindow::create_objects");
695         BC_Title *title;
696         int xs10 = xS(10);
697         int ys5 = yS(5), ys10 = yS(10);
698         int x = xs10, y = ys10;
699         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
700         y += title->get_h() + ys5;
701         int text_w = get_w() - x - 10;
702         int text_h = get_h() - y - BC_OKButton::calculate_h() - ys5;
703         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
704         char *text = copy_dialog->text;
705         int len = strlen(text) + BCTEXTLEN;
706         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
707         file_list->create_objects();
708
709         add_subwindow(new BC_OKButton(this));
710         show_window();
711         unlock_window();
712 }
713
714 int AssetCopyWindow::resize_event(int w, int h)
715 {
716         int fx = file_list->get_x(), fy = file_list->get_y();
717         int text_w = w - fx - xS(10);
718         int text_h = h - fy - BC_OKButton::calculate_h() - yS(5);
719         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
720         file_list->reposition_window(fx, fy, text_w, text_rows);
721         return 0;
722 }
723
724 AssetListPaste::AssetListPaste(MWindow *mwindow, AWindowGUI *gui)
725  : BC_MenuItem(_("Paste file list"))
726 {
727         this->mwindow = mwindow;
728         this->gui = gui;
729         paste_dialog = 0;
730 }
731 AssetListPaste::~AssetListPaste()
732 {
733         delete paste_dialog;
734 }
735
736 int AssetListPaste::handle_event()
737 {
738         if( !paste_dialog )
739                 paste_dialog = new AssetPasteDialog(this);
740         else
741                 paste_dialog->close_window();
742         int cur_x, cur_y;
743         gui->get_abs_cursor(cur_x, cur_y, 0);
744         paste_dialog->start(cur_x, cur_y);
745         return 1;
746 }
747
748 AssetPasteDialog::AssetPasteDialog(AssetListPaste *paste)
749  : BC_DialogThread()
750 {
751         this->paste = paste;
752         paste_window = 0;
753 }
754
755 AssetPasteDialog::~AssetPasteDialog()
756 {
757         close_window();
758 }
759
760 BC_Window* AssetPasteDialog::new_gui()
761 {
762         paste_window = new AssetPasteWindow(this);
763         paste_window->create_objects();
764         return paste_window;
765 }
766
767 void AssetPasteDialog::handle_done_event(int result)
768 {
769         if( result ) return;
770         const char *bp = paste_window->file_list->get_text(), *ep = bp+strlen(bp);
771         ArrayList<char*> path_list;
772         path_list.set_array_delete();
773
774         for( const char *cp=bp; cp<ep && *cp; ) {
775                 const char *dp = strchr(cp, '\n');
776                 if( !dp ) dp = ep;
777                 char path[BCTEXTLEN], *pp = path;
778                 int len = sizeof(path)-1;
779                 while( --len>0 && cp<dp ) *pp++ = *cp++;
780                 if( *cp ) ++cp;
781                 *pp = 0;
782                 if( !strlen(path) ) continue;
783                 path_list.append(cstrdup(path));
784         }
785         if( !path_list.size() ) return;
786
787         MWindow *mwindow = paste->mwindow;
788         mwindow->interrupt_indexes();
789         mwindow->gui->lock_window("AssetPasteDialog::handle_done_event");
790         result = mwindow->load_filenames(&path_list,
791                 LOADMODE_RESOURCESONLY, LOADMODE_EDL_CLIP, 0);
792         mwindow->gui->unlock_window();
793         path_list.remove_all_objects();
794         mwindow->save_backup();
795         mwindow->restart_brender();
796         mwindow->session->changes_made = 1;
797 }
798
799 void AssetPasteDialog::handle_close_event(int result)
800 {
801         paste_window = 0;
802 }
803
804 void AssetPasteDialog::start(int x, int y)
805 {
806         this->x = x;  this->y = y;
807         BC_DialogThread::start();
808 }
809
810 #define APW_W xS(500)
811 #define APW_H yS(200)
812
813 AssetPasteWindow::AssetPasteWindow(AssetPasteDialog *paste_dialog)
814  : BC_Window(_(PROGRAM_NAME ": Paste File List"),
815         paste_dialog->x - APW_W/2, paste_dialog->y - APW_H/2,
816         APW_W, APW_H, APW_W, APW_H, 1, 0, 1)
817 {
818         this->paste_dialog = paste_dialog;
819 }
820
821 AssetPasteWindow::~AssetPasteWindow()
822 {
823 }
824
825 void AssetPasteWindow::create_objects()
826 {
827         lock_window("AssetPasteWindow::create_objects()");
828         BC_Title *title;
829         int xs10 = xS(10);
830         int ys5 = yS(5), ys10 = yS(10);
831         int x = xs10, y = ys10;
832         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
833         y += title->get_h() + ys5;
834         int text_w = get_w() - x - xs10;
835         int text_h = get_h() - y - BC_OKButton::calculate_h() - ys5;
836         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
837         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
838         file_list->create_objects();
839         add_subwindow(new BC_OKButton(this));
840         add_subwindow(new BC_CancelButton(this));
841         show_window();
842         unlock_window();
843 }
844
845 int AssetPasteWindow::resize_event(int w, int h)
846 {
847         int fx = file_list->get_x(), fy = file_list->get_y();
848         int text_w = w - fx - xS(10);
849         int text_h = h - fy - BC_OKButton::calculate_h() - yS(5);
850         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
851         file_list->reposition_window(fx, fy, text_w, text_rows);
852         return 0;
853 }
854
855
856
857 AssetSnapshot::AssetSnapshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
858  : BC_MenuItem(_("Snapshot..."))
859 {
860         this->mwindow = mwindow;
861         this->asset_list_menu = asset_list_menu;
862 }
863
864 AssetSnapshot::~AssetSnapshot()
865 {
866 }
867
868 SnapshotSubMenu::SnapshotSubMenu(AssetSnapshot *asset_snapshot)
869 {
870         this->asset_snapshot = asset_snapshot;
871 }
872
873 SnapshotSubMenu::~SnapshotSubMenu()
874 {
875 }
876
877 SnapshotMenuItem::SnapshotMenuItem(SnapshotSubMenu *submenu, const char *text, int mode)
878  : BC_MenuItem(text)
879 {
880         this->submenu = submenu;
881         this->mode = mode;
882 }
883
884 SnapshotMenuItem::~SnapshotMenuItem()
885 {
886 }
887
888 int SnapshotMenuItem::handle_event()
889 {
890         MWindow *mwindow = submenu->asset_snapshot->mwindow;
891         EDL *edl = mwindow->edl;
892         if( !edl->have_video() ) return 1;
893
894         Preferences *preferences = mwindow->preferences;
895         char filename[BCTEXTLEN];
896         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
897         time_t tt;     time(&tt);
898         struct tm tm;  localtime_r(&tt,&tm);
899         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
900                 preferences->snapshot_path, _("snap"),
901                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
902                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
903         char *asset_path = FileSystem::basepath(filename);
904         Asset *asset = new Asset(asset_path);
905         delete [] asset_path;
906
907         int fw = edl->get_w(), fh = edl->get_h();
908         int fcolor_model = edl->session->color_model;
909
910         switch( mode ) {
911         case SNAPSHOT_PNG:
912                 asset->format = FILE_PNG;
913                 asset->png_use_alpha = 1;
914                 break;
915         case SNAPSHOT_JPEG:
916                 asset->format = FILE_JPEG;
917                 asset->jpeg_quality = 90;
918                 break;
919         case SNAPSHOT_TIFF:
920                 asset->format = FILE_TIFF;
921                 asset->tiff_cmodel = 0;
922                 asset->tiff_compression = 0;
923                 break;
924         case SNAPSHOT_PPM:
925                 asset->format = FILE_PPM;
926                 break;
927         }
928         asset->width = fw;
929         asset->height = fh;
930         asset->audio_data = 0;
931         asset->video_data = 1;
932         asset->video_length = 1;
933         asset->layers = 1;
934
935         File file;
936         int processors = preferences->project_smp + 1;
937         if( processors > 8 ) processors = 8;
938         file.set_processors(processors);
939         int ret = file.open_file(preferences, asset, 0, 1);
940         if( !ret ) {
941                 file.start_video_thread(1, fcolor_model,
942                         processors > 1 ? 2 : 1, 0);
943                 VFrame ***frames = file.get_video_buffer();
944                 VFrame *frame = frames[0][0];
945                 TransportCommand command;
946                 //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME;
947                 command.command = CURRENT_FRAME;
948                 command.get_edl()->copy_all(edl);
949                 command.change_type = CHANGE_ALL;
950                 command.realtime = 0;
951
952                 RenderEngine render_engine(0, preferences, 0, 0);
953                 CICache video_cache(preferences);
954                 render_engine.set_vcache(&video_cache);
955                 render_engine.arm_command(&command);
956
957                 double position = edl->local_session->get_selectionstart(1);
958                 int64_t source_position = (int64_t)(position * edl->get_frame_rate());
959                 ret = !render_engine.vrender ? 1 :
960                         render_engine.vrender->process_buffer(frame, source_position, 0);
961                 if( !ret )
962                         ret = file.write_video_buffer(1);
963                 file.close_file();
964         }
965         if( !ret ) {
966                 asset->folder_no = AW_MEDIA_FOLDER;
967                 mwindow->edl->assets->append(asset);
968                 mwindow->awindow->gui->async_update_assets();
969         }
970         else {
971                 eprintf(_("snapshot render failed"));
972                 asset->remove_user();
973         }
974         return 1;
975 }
976
977
978 AssetGrabshot::AssetGrabshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
979  : BC_MenuItem(_("Grabshot..."))
980 {
981         this->mwindow = mwindow;
982         this->asset_list_menu = asset_list_menu;
983 }
984
985 AssetGrabshot::~AssetGrabshot()
986 {
987 }
988
989 GrabshotSubMenu::GrabshotSubMenu(AssetGrabshot *asset_grabshot)
990 {
991         this->asset_grabshot = asset_grabshot;
992 }
993
994 GrabshotSubMenu::~GrabshotSubMenu()
995 {
996 }
997
998 GrabshotMenuItem::GrabshotMenuItem(GrabshotSubMenu *submenu, const char *text, int mode)
999  : BC_MenuItem(text)
1000 {
1001         this->submenu = submenu;
1002         this->mode = mode;
1003         grab_thread = 0;
1004 }
1005
1006 GrabshotMenuItem::~GrabshotMenuItem()
1007 {
1008         delete grab_thread;
1009 }
1010
1011 int GrabshotMenuItem::handle_event()
1012 {
1013         if( !grab_thread )
1014                 grab_thread = new GrabshotThread(submenu->asset_grabshot->mwindow);
1015         if( !grab_thread->running() )
1016                 grab_thread->start(this);
1017         return 1;
1018 }
1019
1020 GrabshotThread::GrabshotThread(MWindow *mwindow)
1021  : Thread(1, 0, 0)
1022 {
1023         this->mwindow = mwindow;
1024         popup = 0;
1025         done = -1;
1026 }
1027 GrabshotThread::~GrabshotThread()
1028 {
1029         delete popup;
1030 }
1031
1032 void GrabshotThread::start(GrabshotMenuItem *menu_item)
1033 {
1034         popup = new GrabshotPopup(this, menu_item->mode);
1035         popup->lock_window("GrabshotThread::start");
1036         for( int i=0; i<4; ++i )
1037                 edge[i] = new BC_Popup(mwindow->gui, 0,0, 1,1, ORANGE, 1);
1038         mwindow->gui->grab_buttons();
1039         mwindow->gui->grab_cursor();
1040         popup->grab(mwindow->gui);
1041         popup->create_objects();
1042         popup->show_window();
1043         popup->unlock_window();
1044         done = 0;
1045         Thread::start();
1046 }
1047
1048 void GrabshotThread::run()
1049 {
1050         popup->lock_window("GrabshotThread::run 0");
1051         while( !done ) {
1052                 popup->update();
1053                 popup->unlock_window();
1054                 enable_cancel();
1055                 Timer::delay(200);
1056                 disable_cancel();
1057                 popup->lock_window("GrabshotThread::run 1");
1058         }
1059         mwindow->gui->ungrab_cursor();
1060         mwindow->gui->ungrab_buttons();
1061         popup->ungrab(mwindow->gui);
1062         for( int i=0; i<4; ++i ) delete edge[i];
1063         popup->unlock_window();
1064         delete popup;  popup = 0;
1065 }
1066
1067 GrabshotPopup::GrabshotPopup(GrabshotThread *grab_thread, int mode)
1068  : BC_Popup(grab_thread->mwindow->gui, 0,0, 16,16, -1,1)
1069 {
1070         this->grab_thread = grab_thread;
1071         this->mode = mode;
1072         dragging = -1;
1073         grab_color = ORANGE;
1074         x0 = y0 = x1 = y1 = -1;
1075         lx0 = ly0 = lx1 = ly1 = -1;
1076 }
1077 GrabshotPopup::~GrabshotPopup()
1078 {
1079 }
1080
1081 int GrabshotPopup::grab_event(XEvent *event)
1082 {
1083         int cur_drag = dragging;
1084         switch( event->type ) {
1085         case ButtonPress:
1086                 if( cur_drag > 0 ) return 1;
1087                 x0 = event->xbutton.x_root;
1088                 y0 = event->xbutton.y_root;
1089                 if( !cur_drag ) {
1090                         draw_selection(-1);
1091                         if( event->xbutton.button == RIGHT_BUTTON ) break;
1092                         if( x0>=get_x() && x0<get_x()+get_w() &&
1093                             y0>=get_y() && y0<get_y()+get_h() ) break;
1094                 }
1095                 x1 = x0;  y1 = y0;
1096                 draw_selection(1);
1097                 dragging = 1;
1098                 return 1;
1099         case ButtonRelease:
1100                 dragging = 0;
1101         case MotionNotify:
1102                 if( cur_drag > 0 ) {
1103                         x1 = event->xbutton.x_root;
1104                         y1 = event->xbutton.y_root;
1105                         draw_selection(0);
1106                 }
1107                 return 1;
1108         default:
1109                 return 0;
1110         }
1111
1112         int cx = lx0,     cy = ly0;
1113         int cw = lx1-lx0, ch = ly1-ly0;
1114         hide_window();
1115         sync_display();
1116         grab_thread->done = 1;
1117
1118         MWindow *mwindow = grab_thread->mwindow;
1119         Preferences *preferences = mwindow->preferences;
1120         char filename[BCTEXTLEN];
1121         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
1122         time_t tt;     time(&tt);
1123         struct tm tm;  localtime_r(&tt,&tm);
1124         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
1125                 preferences->snapshot_path, _("grab"),
1126                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
1127                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
1128         char *asset_path = FileSystem::basepath(filename);
1129         Asset *asset = new Asset(asset_path);
1130         delete [] asset_path;
1131         switch( mode ) {
1132         case GRABSHOT_PNG:
1133                 asset->format = FILE_PNG;
1134                 asset->png_use_alpha = 1;
1135                 break;
1136         case GRABSHOT_JPEG:
1137                 asset->format = FILE_JPEG;
1138                 asset->jpeg_quality = 90;
1139                 break;
1140         case GRABSHOT_TIFF:
1141                 asset->format = FILE_TIFF;
1142                 asset->tiff_cmodel = 0;
1143                 asset->tiff_compression = 0;
1144                 break;
1145         case GRABSHOT_PPM:
1146                 asset->format = FILE_PPM;
1147                 break;
1148         }
1149
1150 // no odd dimensions
1151         int rw = get_root_w(0), rh = get_root_h(0);
1152         if( cx < 0 ) { cw += cx;  cx = 0; }
1153         if( cy < 0 ) { ch += cy;  cy = 0; }
1154         if( cx+cw > rw ) cw = rw-cx;
1155         if( cy+ch > rh ) ch = rh-cy;
1156         if( !cw || !ch ) return 1;
1157
1158         VFrame vframe(cw,ch, BC_RGB888);
1159         if( cx+cw < rw ) ++cw;
1160         if( cy+ch < rh ) ++ch;
1161         BC_Capture capture_bitmap(cw,ch, 0);
1162         capture_bitmap.capture_frame(&vframe, cx,cy);
1163
1164         asset->width = vframe.get_w();
1165         asset->height = vframe.get_h();
1166         asset->audio_data = 0;
1167         asset->video_data = 1;
1168         asset->video_length = 1;
1169         asset->layers = 1;
1170
1171         File file;
1172         int fcolor_model = mwindow->edl->session->color_model;
1173         int processors = preferences->project_smp + 1;
1174         if( processors > 8 ) processors = 8;
1175         file.set_processors(processors);
1176         int ret = file.open_file(preferences, asset, 0, 1);
1177         if( !ret ) {
1178                 file.start_video_thread(1, fcolor_model,
1179                         processors > 1 ? 2 : 1, 0);
1180                 VFrame ***frames = file.get_video_buffer();
1181                 VFrame *frame = frames[0][0];
1182                 frame->transfer_from(&vframe);
1183                 ret = file.write_video_buffer(1);
1184                 file.close_file();
1185         }
1186         if( !ret ) {
1187                 asset->folder_no = AW_MEDIA_FOLDER;
1188                 mwindow->edl->assets->append(asset);
1189                 mwindow->awindow->gui->async_update_assets();
1190         }
1191         else {
1192                 eprintf(_("grabshot render failed"));
1193                 asset->remove_user();
1194         }
1195
1196         return 1;
1197 }
1198
1199 void GrabshotPopup::update()
1200 {
1201         set_color(grab_color ^= GREEN);
1202         draw_box(0,0, get_w(),get_h());
1203         flash(1);
1204 }
1205
1206 void GrabshotPopup::draw_selection(int show)
1207 {
1208         if( show < 0 ) {
1209                 for( int i=0; i<4; ++i ) hide_window(0);
1210                 flush();
1211                 return;
1212         }
1213
1214         int nx0 = x0 < x1 ? x0 : x1;
1215         int nx1 = x0 < x1 ? x1 : x0;
1216         int ny0 = y0 < y1 ? y0 : y1;
1217         int ny1 = y0 < y1 ? y1 : y0;
1218         lx0 = nx0;  lx1 = nx1;  ly0 = ny0;  ly1 = ny1;
1219
1220         --nx0;  --ny0;
1221         BC_Popup **edge = grab_thread->edge;
1222         edge[0]->reposition_window(nx0,ny0, nx1-nx0, 1);
1223         edge[1]->reposition_window(nx1,ny0, 1, ny1-ny0);
1224         edge[2]->reposition_window(nx0,ny1, nx1-nx0, 1);
1225         edge[3]->reposition_window(nx0,ny0, 1, ny1-ny0);
1226
1227         if( show > 0 ) {
1228                 for( int i=0; i<4; ++i ) edge[i]->show_window(0);
1229         }
1230         flush();
1231 }
1232