add new boxblur plugin, mods to videoscope, fix segv for menu btns kfrm-tweak/kfrm...
[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(C_("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], snapshot_path[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         File::getenv_path(snapshot_path, preferences->snapshot_path);
900         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
901                 snapshot_path, _("snap"),
902                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
903                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
904         char *asset_path = FileSystem::basepath(filename);
905         Asset *asset = new Asset(asset_path);
906         delete [] asset_path;
907
908         int fw = edl->get_w(), fh = edl->get_h();
909         int fcolor_model = edl->session->color_model;
910
911         switch( mode ) {
912         case SNAPSHOT_PNG:
913                 asset->format = FILE_PNG;
914                 asset->png_use_alpha = 1;
915                 break;
916         case SNAPSHOT_JPEG:
917                 asset->format = FILE_JPEG;
918                 asset->jpeg_quality = 90;
919                 break;
920         case SNAPSHOT_TIFF:
921                 asset->format = FILE_TIFF;
922                 asset->tiff_cmodel = 0;
923                 asset->tiff_compression = 0;
924                 break;
925         case SNAPSHOT_PPM:
926                 asset->format = FILE_PPM;
927                 break;
928         }
929         asset->width = fw;
930         asset->height = fh;
931         asset->audio_data = 0;
932         asset->video_data = 1;
933         asset->video_length = 1;
934         asset->layers = 1;
935
936         File file;
937         int processors = preferences->project_smp + 1;
938         if( processors > 8 ) processors = 8;
939         file.set_processors(processors);
940         int ret = file.open_file(preferences, asset, 0, 1);
941         if( !ret ) {
942                 file.start_video_thread(1, fcolor_model,
943                         processors > 1 ? 2 : 1, 0);
944                 VFrame ***frames = file.get_video_buffer();
945                 VFrame *frame = frames[0][0];
946                 TransportCommand command;
947                 //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME;
948                 command.command = CURRENT_FRAME;
949                 command.get_edl()->copy_all(edl);
950                 command.change_type = CHANGE_ALL;
951                 command.realtime = 0;
952
953                 RenderEngine render_engine(0, preferences, 0, 0);
954                 CICache video_cache(preferences);
955                 render_engine.set_vcache(&video_cache);
956                 render_engine.arm_command(&command);
957
958                 double position = edl->local_session->get_selectionstart(1);
959                 int64_t source_position = (int64_t)(position * edl->get_frame_rate());
960                 ret = !render_engine.vrender ? 1 :
961                         render_engine.vrender->process_buffer(frame, source_position, 0);
962                 if( !ret )
963                         ret = file.write_video_buffer(1);
964                 file.close_file();
965         }
966         if( !ret ) {
967                 asset->folder_no = AW_MEDIA_FOLDER;
968                 mwindow->edl->assets->append(asset);
969                 mwindow->awindow->gui->async_update_assets();
970         }
971         else {
972                 eprintf(_("snapshot render failed"));
973                 asset->remove_user();
974         }
975         return 1;
976 }
977
978
979 AssetGrabshot::AssetGrabshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
980  : BC_MenuItem(_("Grabshot..."))
981 {
982         this->mwindow = mwindow;
983         this->asset_list_menu = asset_list_menu;
984 }
985
986 AssetGrabshot::~AssetGrabshot()
987 {
988 }
989
990 GrabshotSubMenu::GrabshotSubMenu(AssetGrabshot *asset_grabshot)
991 {
992         this->asset_grabshot = asset_grabshot;
993 }
994
995 GrabshotSubMenu::~GrabshotSubMenu()
996 {
997 }
998
999 GrabshotMenuItem::GrabshotMenuItem(GrabshotSubMenu *submenu, const char *text, int mode)
1000  : BC_MenuItem(text)
1001 {
1002         this->submenu = submenu;
1003         this->mode = mode;
1004         grab_thread = 0;
1005 }
1006
1007 GrabshotMenuItem::~GrabshotMenuItem()
1008 {
1009         delete grab_thread;
1010 }
1011
1012 int GrabshotMenuItem::handle_event()
1013 {
1014         if( !grab_thread )
1015                 grab_thread = new GrabshotThread(submenu->asset_grabshot->mwindow);
1016         if( !grab_thread->running() )
1017                 grab_thread->start(this);
1018         return 1;
1019 }
1020
1021 GrabshotThread::GrabshotThread(MWindow *mwindow)
1022  : Thread(1, 0, 0)
1023 {
1024         this->mwindow = mwindow;
1025         popup = 0;
1026         done = -1;
1027 }
1028 GrabshotThread::~GrabshotThread()
1029 {
1030         delete popup;
1031 }
1032
1033 void GrabshotThread::start(GrabshotMenuItem *menu_item)
1034 {
1035         popup = new GrabshotPopup(this, menu_item->mode);
1036         popup->lock_window("GrabshotThread::start");
1037         for( int i=0; i<4; ++i )
1038                 edge[i] = new BC_Popup(mwindow->gui, 0,0, 1,1, ORANGE, 1);
1039         mwindow->gui->grab_buttons();
1040         mwindow->gui->grab_cursor();
1041         popup->grab(mwindow->gui);
1042         popup->create_objects();
1043         popup->show_window();
1044         popup->unlock_window();
1045         done = 0;
1046         Thread::start();
1047 }
1048
1049 void GrabshotThread::run()
1050 {
1051         popup->lock_window("GrabshotThread::run 0");
1052         while( !done ) {
1053                 popup->update();
1054                 popup->unlock_window();
1055                 enable_cancel();
1056                 Timer::delay(200);
1057                 disable_cancel();
1058                 popup->lock_window("GrabshotThread::run 1");
1059         }
1060         mwindow->gui->ungrab_cursor();
1061         mwindow->gui->ungrab_buttons();
1062         popup->ungrab(mwindow->gui);
1063         for( int i=0; i<4; ++i ) delete edge[i];
1064         popup->unlock_window();
1065         delete popup;  popup = 0;
1066 }
1067
1068 GrabshotPopup::GrabshotPopup(GrabshotThread *grab_thread, int mode)
1069  : BC_Popup(grab_thread->mwindow->gui, 0,0, 16,16, -1,1)
1070 {
1071         this->grab_thread = grab_thread;
1072         this->mode = mode;
1073         dragging = -1;
1074         grab_color = ORANGE;
1075         x0 = y0 = x1 = y1 = -1;
1076         lx0 = ly0 = lx1 = ly1 = -1;
1077 }
1078 GrabshotPopup::~GrabshotPopup()
1079 {
1080 }
1081
1082 int GrabshotPopup::grab_event(XEvent *event)
1083 {
1084         int cur_drag = dragging;
1085         switch( event->type ) {
1086         case ButtonPress:
1087                 if( cur_drag > 0 ) return 1;
1088                 x0 = event->xbutton.x_root;
1089                 y0 = event->xbutton.y_root;
1090                 if( !cur_drag ) {
1091                         draw_selection(-1);
1092                         if( event->xbutton.button == RIGHT_BUTTON ) break;
1093                         if( x0>=get_x() && x0<get_x()+get_w() &&
1094                             y0>=get_y() && y0<get_y()+get_h() ) break;
1095                 }
1096                 x1 = x0;  y1 = y0;
1097                 draw_selection(1);
1098                 dragging = 1;
1099                 return 1;
1100         case ButtonRelease:
1101                 dragging = 0;
1102         case MotionNotify:
1103                 if( cur_drag > 0 ) {
1104                         x1 = event->xbutton.x_root;
1105                         y1 = event->xbutton.y_root;
1106                         draw_selection(0);
1107                 }
1108                 return 1;
1109         default:
1110                 return 0;
1111         }
1112
1113         int cx = lx0,     cy = ly0;
1114         int cw = lx1-lx0, ch = ly1-ly0;
1115         hide_window();
1116         sync_display();
1117         grab_thread->done = 1;
1118
1119         MWindow *mwindow = grab_thread->mwindow;
1120         Preferences *preferences = mwindow->preferences;
1121         char filename[BCTEXTLEN], snapshot_path[BCTEXTLEN];
1122         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
1123         time_t tt;     time(&tt);
1124         struct tm tm;  localtime_r(&tt,&tm);
1125         File::getenv_path(snapshot_path, preferences->snapshot_path);
1126         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
1127                 snapshot_path, _("grab"),
1128                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
1129                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
1130         char *asset_path = FileSystem::basepath(filename);
1131         Asset *asset = new Asset(asset_path);
1132         delete [] asset_path;
1133         switch( mode ) {
1134         case GRABSHOT_PNG:
1135                 asset->format = FILE_PNG;
1136                 asset->png_use_alpha = 1;
1137                 break;
1138         case GRABSHOT_JPEG:
1139                 asset->format = FILE_JPEG;
1140                 asset->jpeg_quality = 90;
1141                 break;
1142         case GRABSHOT_TIFF:
1143                 asset->format = FILE_TIFF;
1144                 asset->tiff_cmodel = 0;
1145                 asset->tiff_compression = 0;
1146                 break;
1147         case GRABSHOT_PPM:
1148                 asset->format = FILE_PPM;
1149                 break;
1150         }
1151
1152 // no odd dimensions
1153         int rw = get_root_w(0), rh = get_root_h(0);
1154         if( cx < 0 ) { cw += cx;  cx = 0; }
1155         if( cy < 0 ) { ch += cy;  cy = 0; }
1156         if( cx+cw > rw ) cw = rw-cx;
1157         if( cy+ch > rh ) ch = rh-cy;
1158         if( !cw || !ch ) return 1;
1159
1160         VFrame vframe(cw,ch, BC_RGB888);
1161         if( cx+cw < rw ) ++cw;
1162         if( cy+ch < rh ) ++ch;
1163         BC_Capture capture_bitmap(cw,ch, 0);
1164         capture_bitmap.capture_frame(&vframe, cx,cy);
1165
1166         asset->width = vframe.get_w();
1167         asset->height = vframe.get_h();
1168         asset->audio_data = 0;
1169         asset->video_data = 1;
1170         asset->video_length = 1;
1171         asset->layers = 1;
1172
1173         File file;
1174         int fcolor_model = mwindow->edl->session->color_model;
1175         int processors = preferences->project_smp + 1;
1176         if( processors > 8 ) processors = 8;
1177         file.set_processors(processors);
1178         int ret = file.open_file(preferences, asset, 0, 1);
1179         if( !ret ) {
1180                 file.start_video_thread(1, fcolor_model,
1181                         processors > 1 ? 2 : 1, 0);
1182                 VFrame ***frames = file.get_video_buffer();
1183                 VFrame *frame = frames[0][0];
1184                 frame->transfer_from(&vframe);
1185                 ret = file.write_video_buffer(1);
1186                 file.close_file();
1187         }
1188         if( !ret ) {
1189                 asset->folder_no = AW_MEDIA_FOLDER;
1190                 mwindow->edl->assets->append(asset);
1191                 mwindow->awindow->gui->async_update_assets();
1192         }
1193         else {
1194                 eprintf(_("grabshot render failed"));
1195                 asset->remove_user();
1196         }
1197
1198         return 1;
1199 }
1200
1201 void GrabshotPopup::update()
1202 {
1203         set_color(grab_color ^= GREEN);
1204         draw_box(0,0, get_w(),get_h());
1205         flash(1);
1206 }
1207
1208 void GrabshotPopup::draw_selection(int show)
1209 {
1210         if( show < 0 ) {
1211                 for( int i=0; i<4; ++i ) hide_window(0);
1212                 flush();
1213                 return;
1214         }
1215
1216         int nx0 = x0 < x1 ? x0 : x1;
1217         int nx1 = x0 < x1 ? x1 : x0;
1218         int ny0 = y0 < y1 ? y0 : y1;
1219         int ny1 = y0 < y1 ? y1 : y0;
1220         lx0 = nx0;  lx1 = nx1;  ly0 = ny0;  ly1 = ny1;
1221
1222         --nx0;  --ny0;
1223         BC_Popup **edge = grab_thread->edge;
1224         edge[0]->reposition_window(nx0,ny0, nx1-nx0, 1);
1225         edge[1]->reposition_window(nx1,ny0, 1, ny1-ny0);
1226         edge[2]->reposition_window(nx0,ny1, nx1-nx0, 1);
1227         edge[3]->reposition_window(nx0,ny0, 1, ny1-ny0);
1228
1229         if( show > 0 ) {
1230                 for( int i=0; i<4; ++i ) edge[i]->show_window(0);
1231         }
1232         flush();
1233 }
1234