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