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