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