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