p/s proxy icon, rework window locks, segv in close_mixers + exportedl, ffmpeg default...
[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         lock_window("AssetCopyWindow::create_objects");
584         BC_Title *title;
585         int x = 10, y = 10, pad = 5;
586         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
587         y += title->get_h() + pad;
588         int text_w = get_w() - x - 10;
589         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
590         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
591         char *text = copy_dialog->text;
592         int len = strlen(text) + BCTEXTLEN;
593         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
594         file_list->create_objects();
595
596         add_subwindow(new BC_OKButton(this));
597         show_window();
598         unlock_window();
599 }
600
601 int AssetCopyWindow::resize_event(int w, int h)
602 {
603         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
604         int text_w = w - fx - 10;
605         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
606         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
607         file_list->reposition_window(fx, fy, text_w, text_rows);
608         return 0;
609 }
610
611 AssetListPaste::AssetListPaste(MWindow *mwindow, AWindowGUI *gui)
612  : BC_MenuItem(_("Paste file list"))
613 {
614         this->mwindow = mwindow;
615         this->gui = gui;
616         paste_dialog = 0;
617 }
618 AssetListPaste::~AssetListPaste()
619 {
620         delete paste_dialog;
621 }
622
623 int AssetListPaste::handle_event()
624 {
625         if( !paste_dialog )
626                 paste_dialog = new AssetPasteDialog(this);
627         else
628                 paste_dialog->close_window();
629         int cur_x, cur_y;
630         gui->get_abs_cursor(cur_x, cur_y, 0);
631         paste_dialog->start(cur_x, cur_y);
632         return 1;
633 }
634
635 AssetPasteDialog::AssetPasteDialog(AssetListPaste *paste)
636  : BC_DialogThread()
637 {
638         this->paste = paste;
639         paste_window = 0;
640 }
641
642 AssetPasteDialog::~AssetPasteDialog()
643 {
644         close_window();
645 }
646
647 BC_Window* AssetPasteDialog::new_gui()
648 {
649         paste_window = new AssetPasteWindow(this);
650         paste_window->create_objects();
651         return paste_window;
652 }
653
654 void AssetPasteDialog::handle_done_event(int result)
655 {
656         if( result ) return;
657         const char *bp = paste_window->file_list->get_text(), *ep = bp+strlen(bp);
658         ArrayList<char*> path_list;
659         path_list.set_array_delete();
660
661         for( const char *cp=bp; cp<ep && *cp; ) {
662                 const char *dp = strchr(cp, '\n');
663                 if( !dp ) dp = ep;
664                 char path[BCTEXTLEN], *pp = path;
665                 int len = sizeof(path)-1;
666                 while( --len>0 && cp<dp ) *pp++ = *cp++;
667                 if( *cp ) ++cp;
668                 *pp = 0;
669                 if( !strlen(path) ) continue;
670                 path_list.append(cstrdup(path));
671         }
672         if( !path_list.size() ) return;
673
674         MWindow *mwindow = paste->mwindow;
675         mwindow->interrupt_indexes();
676         mwindow->gui->lock_window("AssetPasteDialog::handle_done_event");
677         result = mwindow->load_filenames(&path_list, LOADMODE_RESOURCESONLY, 0);
678         mwindow->gui->unlock_window();
679         path_list.remove_all_objects();
680         mwindow->save_backup();
681         mwindow->restart_brender();
682         mwindow->session->changes_made = 1;
683 }
684
685 void AssetPasteDialog::handle_close_event(int result)
686 {
687         paste_window = 0;
688 }
689
690 void AssetPasteDialog::start(int x, int y)
691 {
692         this->x = x;  this->y = y;
693         BC_DialogThread::start();
694 }
695
696 AssetPasteWindow::AssetPasteWindow(AssetPasteDialog *paste_dialog)
697  : BC_Window(_(PROGRAM_NAME ": Paste File List"),
698         paste_dialog->x - 500/2, paste_dialog->y - 200/2,
699         500, 200, 500, 200, 1, 0, 1)
700 {
701         this->paste_dialog = paste_dialog;
702 }
703
704 AssetPasteWindow::~AssetPasteWindow()
705 {
706 }
707
708 void AssetPasteWindow::create_objects()
709 {
710         lock_window("AssetPasteWindow::create_objects()");
711         BC_Title *title;
712         int x = 10, y = 10, pad = 5;
713         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
714         y += title->get_h() + pad;
715         int text_w = get_w() - x - 10;
716         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
717         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
718         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
719         file_list->create_objects();
720         add_subwindow(new BC_OKButton(this));
721         add_subwindow(new BC_CancelButton(this));
722         show_window();
723         unlock_window();
724 }
725
726 int AssetPasteWindow::resize_event(int w, int h)
727 {
728         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
729         int text_w = w - fx - 10;
730         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
731         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
732         file_list->reposition_window(fx, fy, text_w, text_rows);
733         return 0;
734 }
735
736
737
738 AssetSnapshot::AssetSnapshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
739  : BC_MenuItem(_("Snapshot..."))
740 {
741         this->mwindow = mwindow;
742         this->asset_list_menu = asset_list_menu;
743 }
744
745 AssetSnapshot::~AssetSnapshot()
746 {
747 }
748
749 SnapshotSubMenu::SnapshotSubMenu(AssetSnapshot *asset_snapshot)
750 {
751         this->asset_snapshot = asset_snapshot;
752 }
753
754 SnapshotSubMenu::~SnapshotSubMenu()
755 {
756 }
757
758 SnapshotMenuItem::SnapshotMenuItem(SnapshotSubMenu *submenu, const char *text, int mode)
759  : BC_MenuItem(text)
760 {
761         this->submenu = submenu;
762         this->mode = mode;
763 }
764
765 SnapshotMenuItem::~SnapshotMenuItem()
766 {
767 }
768
769 int SnapshotMenuItem::handle_event()
770 {
771         MWindow *mwindow = submenu->asset_snapshot->mwindow;
772         EDL *edl = mwindow->edl;
773         if( !edl->have_video() ) return 1;
774
775         Preferences *preferences = mwindow->preferences;
776         char filename[BCTEXTLEN];
777         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
778         time_t tt;     time(&tt);
779         struct tm tm;  localtime_r(&tt,&tm);
780         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
781                 preferences->snapshot_path, _("snap"),
782                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
783                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
784         char *asset_path = FileSystem::basepath(filename);
785         Asset *asset = new Asset(asset_path);
786         delete [] asset_path;
787
788         int fw = edl->get_w(), fh = edl->get_h();
789         int fcolor_model = edl->session->color_model;
790
791         switch( mode ) {
792         case SNAPSHOT_PNG:
793                 asset->format = FILE_PNG;
794                 asset->png_use_alpha = 1;
795                 break;
796         case SNAPSHOT_JPEG:
797                 asset->format = FILE_JPEG;
798                 asset->jpeg_quality = 90;
799                 break;
800         case SNAPSHOT_TIFF:
801                 asset->format = FILE_TIFF;
802                 asset->tiff_cmodel = 0;
803                 asset->tiff_compression = 0;
804                 break;
805         case SNAPSHOT_PPM:
806                 asset->format = FILE_PPM;
807                 break;
808         }
809         asset->width = fw;
810         asset->height = fh;
811         asset->audio_data = 0;
812         asset->video_data = 1;
813         asset->video_length = 1;
814         asset->layers = 1;
815
816         File file;
817         int processors = preferences->project_smp + 1;
818         if( processors > 8 ) processors = 8;
819         file.set_processors(processors);
820         int ret = file.open_file(preferences, asset, 0, 1);
821         if( !ret ) {
822                 file.start_video_thread(1, fcolor_model,
823                         processors > 1 ? 2 : 1, 0);
824                 VFrame ***frames = file.get_video_buffer();
825                 VFrame *frame = frames[0][0];
826                 TransportCommand command;
827                 //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME;
828                 command.command = CURRENT_FRAME;
829                 command.get_edl()->copy_all(edl);
830                 command.change_type = CHANGE_ALL;
831                 command.realtime = 0;
832
833                 RenderEngine render_engine(0, preferences, 0, 0);
834                 CICache video_cache(preferences);
835                 render_engine.set_vcache(&video_cache);
836                 render_engine.arm_command(&command);
837
838                 double position = edl->local_session->get_selectionstart(1);
839                 int64_t source_position = (int64_t)(position * edl->get_frame_rate());
840                 ret = !render_engine.vrender ? 1 :
841                         render_engine.vrender->process_buffer(frame, source_position, 0);
842                 if( !ret )
843                         ret = file.write_video_buffer(1);
844                 file.close_file();
845         }
846         if( !ret ) {
847                 asset->awindow_folder = AW_MEDIA_FOLDER;
848                 mwindow->edl->assets->append(asset);
849                 mwindow->awindow->gui->async_update_assets();
850         }
851         else {
852                 eprintf(_("snapshot render failed"));
853                 asset->remove_user();
854         }
855         return 1;
856 }
857
858
859 AssetGrabshot::AssetGrabshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
860  : BC_MenuItem(_("Grabshot..."))
861 {
862         this->mwindow = mwindow;
863         this->asset_list_menu = asset_list_menu;
864 }
865
866 AssetGrabshot::~AssetGrabshot()
867 {
868 }
869
870 GrabshotSubMenu::GrabshotSubMenu(AssetGrabshot *asset_grabshot)
871 {
872         this->asset_grabshot = asset_grabshot;
873 }
874
875 GrabshotSubMenu::~GrabshotSubMenu()
876 {
877 }
878
879 GrabshotMenuItem::GrabshotMenuItem(GrabshotSubMenu *submenu, const char *text, int mode)
880  : BC_MenuItem(text)
881 {
882         this->submenu = submenu;
883         this->mode = mode;
884         grab_thread = 0;
885 }
886
887 GrabshotMenuItem::~GrabshotMenuItem()
888 {
889         delete grab_thread;
890 }
891
892 int GrabshotMenuItem::handle_event()
893 {
894         if( !grab_thread )
895                 grab_thread = new GrabshotThread(submenu->asset_grabshot->mwindow);
896         if( !grab_thread->running() )
897                 grab_thread->start(this);
898         return 1;
899 }
900
901 GrabshotThread::GrabshotThread(MWindow *mwindow)
902  : Thread(1, 0, 0)
903 {
904         this->mwindow = mwindow;
905         popup = 0;
906         done = -1;
907 }
908 GrabshotThread::~GrabshotThread()
909 {
910         delete popup;
911 }
912
913 void GrabshotThread::start(GrabshotMenuItem *menu_item)
914 {
915         popup = new GrabshotPopup(this, menu_item->mode);
916         popup->lock_window("GrabshotThread::start");
917         for( int i=0; i<4; ++i )
918                 edge[i] = new BC_Popup(mwindow->gui, 0,0, 1,1, ORANGE, 1);
919         mwindow->gui->grab_buttons();
920         mwindow->gui->grab_cursor();
921         popup->grab(mwindow->gui);
922         popup->create_objects();
923         popup->show_window();
924         popup->unlock_window();
925         done = 0;
926         Thread::start();
927 }
928
929 void GrabshotThread::run()
930 {
931         popup->lock_window("GrabshotThread::run 0");
932         while( !done ) {
933                 popup->update();
934                 popup->unlock_window();
935                 enable_cancel();
936                 Timer::delay(200);
937                 disable_cancel();
938                 popup->lock_window("GrabshotThread::run 1");
939         }
940         mwindow->gui->ungrab_cursor();
941         mwindow->gui->ungrab_buttons();
942         popup->ungrab(mwindow->gui);
943         for( int i=0; i<4; ++i ) delete edge[i];
944         popup->unlock_window();
945         delete popup;  popup = 0;
946 }
947
948 GrabshotPopup::GrabshotPopup(GrabshotThread *grab_thread, int mode)
949  : BC_Popup(grab_thread->mwindow->gui, 0,0, 16,16, -1,1)
950 {
951         this->grab_thread = grab_thread;
952         this->mode = mode;
953         dragging = -1;
954         grab_color = ORANGE;
955         x0 = y0 = x1 = y1 = -1;
956         lx0 = ly0 = lx1 = ly1 = -1;
957 }
958 GrabshotPopup::~GrabshotPopup()
959 {
960 }
961
962 int GrabshotPopup::grab_event(XEvent *event)
963 {
964         int cur_drag = dragging;
965         switch( event->type ) {
966         case ButtonPress:
967                 if( cur_drag > 0 ) return 1;
968                 x0 = event->xbutton.x_root;
969                 y0 = event->xbutton.y_root;
970                 if( !cur_drag ) {
971                         draw_selection(-1);
972                         if( event->xbutton.button == RIGHT_BUTTON ) break;
973                         if( x0>=get_x() && x0<get_x()+get_w() &&
974                             y0>=get_y() && y0<get_y()+get_h() ) break;
975                 }
976                 x1 = x0;  y1 = y0;
977                 draw_selection(1);
978                 dragging = 1;
979                 return 1;
980         case ButtonRelease:
981                 dragging = 0;
982         case MotionNotify:
983                 if( cur_drag > 0 ) {
984                         x1 = event->xbutton.x_root;
985                         y1 = event->xbutton.y_root;
986                         draw_selection(0);
987                 }
988                 return 1;
989         default:
990                 return 0;
991         }
992
993         int cx = lx0,     cy = ly0;
994         int cw = lx1-lx0, ch = ly1-ly0;
995         hide_window();
996         sync_display();
997         grab_thread->done = 1;
998
999         MWindow *mwindow = grab_thread->mwindow;
1000         Preferences *preferences = mwindow->preferences;
1001         char filename[BCTEXTLEN];
1002         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
1003         time_t tt;     time(&tt);
1004         struct tm tm;  localtime_r(&tt,&tm);
1005         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
1006                 preferences->snapshot_path, _("grab"),
1007                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
1008                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
1009         char *asset_path = FileSystem::basepath(filename);
1010         Asset *asset = new Asset(asset_path);
1011         delete [] asset_path;
1012         switch( mode ) {
1013         case GRABSHOT_PNG:
1014                 asset->format = FILE_PNG;
1015                 asset->png_use_alpha = 1;
1016                 break;
1017         case GRABSHOT_JPEG:
1018                 asset->format = FILE_JPEG;
1019                 asset->jpeg_quality = 90;
1020                 break;
1021         case GRABSHOT_TIFF:
1022                 asset->format = FILE_TIFF;
1023                 asset->tiff_cmodel = 0;
1024                 asset->tiff_compression = 0;
1025                 break;
1026         case GRABSHOT_PPM:
1027                 asset->format = FILE_PPM;
1028                 break;
1029         }
1030
1031 // no odd dimensions
1032         int rw = get_root_w(0), rh = get_root_h(0);
1033         if( cx < 0 ) { cw += cx;  cx = 0; }
1034         if( cy < 0 ) { ch += cy;  cy = 0; }
1035         if( cx+cw > rw ) cw = rw-cx;
1036         if( cy+ch > rh ) ch = rh-cy;
1037         if( !cw || !ch ) return 1;
1038
1039         VFrame vframe(cw,ch, BC_RGB888);
1040         if( cx+cw < rw ) ++cw;
1041         if( cy+ch < rh ) ++ch;
1042         BC_Capture capture_bitmap(cw,ch, 0);
1043         capture_bitmap.capture_frame(&vframe, cx,cy);
1044
1045         asset->width = vframe.get_w();
1046         asset->height = vframe.get_h();
1047         asset->audio_data = 0;
1048         asset->video_data = 1;
1049         asset->video_length = 1;
1050         asset->layers = 1;
1051
1052         File file;
1053         int fcolor_model = mwindow->edl->session->color_model;
1054         int processors = preferences->project_smp + 1;
1055         if( processors > 8 ) processors = 8;
1056         file.set_processors(processors);
1057         int ret = file.open_file(preferences, asset, 0, 1);
1058         if( !ret ) {
1059                 file.start_video_thread(1, fcolor_model,
1060                         processors > 1 ? 2 : 1, 0);
1061                 VFrame ***frames = file.get_video_buffer();
1062                 VFrame *frame = frames[0][0];
1063                 frame->transfer_from(&vframe);
1064                 ret = file.write_video_buffer(1);
1065                 file.close_file();
1066         }
1067         if( !ret ) {
1068                 asset->awindow_folder = AW_MEDIA_FOLDER;
1069                 mwindow->edl->assets->append(asset);
1070                 mwindow->awindow->gui->async_update_assets();
1071         }
1072         else {
1073                 eprintf(_("grabshot render failed"));
1074                 asset->remove_user();
1075         }
1076
1077         return 1;
1078 }
1079
1080 void GrabshotPopup::update()
1081 {
1082         set_color(grab_color ^= GREEN);
1083         draw_box(0,0, get_w(),get_h());
1084         flash(1);
1085 }
1086
1087 void GrabshotPopup::draw_selection(int show)
1088 {
1089         if( show < 0 ) {
1090                 for( int i=0; i<4; ++i ) hide_window(0);
1091                 flush();
1092                 return;
1093         }
1094
1095         int nx0 = x0 < x1 ? x0 : x1;
1096         int nx1 = x0 < x1 ? x1 : x0;
1097         int ny0 = y0 < y1 ? y0 : y1;
1098         int ny1 = y0 < y1 ? y1 : y0;
1099         lx0 = nx0;  lx1 = nx1;  ly0 = ny0;  ly1 = ny1;
1100
1101         --nx0;  --ny0;
1102         BC_Popup **edge = grab_thread->edge;
1103         edge[0]->reposition_window(nx0,ny0, nx1-nx0, 1);
1104         edge[1]->reposition_window(nx1,ny0, 1, ny1-ny0);
1105         edge[2]->reposition_window(nx0,ny1, nx1-nx0, 1);
1106         edge[3]->reposition_window(nx0,ny0, 1, ny1-ny0);
1107
1108         if( show > 0 ) {
1109                 for( int i=0; i<4; ++i ) edge[i]->show_window(0);
1110         }
1111         flush();
1112 }
1113