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