fix for selected_to_clipboard unpacked copy
[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         popup->gui->unlock_window();
363         mwindow->remove_assets_from_project(1, 1,
364                 mwindow->session->drag_assets,
365                 mwindow->session->drag_clips);
366         popup->gui->lock_window("AssetPopupProjectRemove::handle_event");
367         return 1;
368 }
369
370
371 AssetPopupDiskRemove::AssetPopupDiskRemove(MWindow *mwindow, AssetPopup *popup)
372  : BC_MenuItem(_("Remove from disk"))
373 {
374         this->mwindow = mwindow;
375         this->popup = popup;
376 }
377
378
379 AssetPopupDiskRemove::~AssetPopupDiskRemove()
380 {
381 }
382
383 int AssetPopupDiskRemove::handle_event()
384 {
385         mwindow->awindow->asset_remove->start();
386         return 1;
387 }
388
389
390 AssetListMenu::AssetListMenu(MWindow *mwindow, AWindowGUI *gui)
391  : BC_PopupMenu(0, 0, 0, "", 0)
392 {
393         this->mwindow = mwindow;
394         this->gui = gui;
395 }
396
397 AssetListMenu::~AssetListMenu()
398 {
399         if( !shots_displayed ) {
400                 delete asset_snapshot;
401                 delete asset_grabshot;
402         }
403 }
404
405 void AssetListMenu::create_objects()
406 {
407         add_item(load_file = new AssetPopupLoadFile(mwindow, gui));
408         add_item(format = new AWindowListFormat(mwindow, gui));
409         add_item(select_used = new AssetSelectUsed(mwindow, gui));
410         BC_SubMenu *submenu;
411         select_used->add_submenu(submenu = new BC_SubMenu());
412         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("All"), SELECT_ALL));
413         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("Used"), SELECT_USED));
414         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("Unused"), SELECT_UNUSED));
415         submenu->add_submenuitem(new AssetSelectUsedItem(select_used, _("None"), SELECT_NONE));
416         add_item(new AWindowListSort(mwindow, gui));
417         add_item(new AssetListCopy(mwindow, gui));
418         add_item(new AssetListPaste(mwindow, gui));
419         SnapshotSubMenu *snapshot_submenu;
420         add_item(asset_snapshot = new AssetSnapshot(mwindow, this));
421         asset_snapshot->add_submenu(snapshot_submenu = new SnapshotSubMenu(asset_snapshot));
422         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("png"),  SNAPSHOT_PNG));
423         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("jpeg"), SNAPSHOT_JPEG));
424         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("tiff"), SNAPSHOT_TIFF));
425         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("ppm"),  SNAPSHOT_PPM));
426         GrabshotSubMenu *grabshot_submenu;
427         add_item(asset_grabshot = new AssetGrabshot(mwindow, this));
428         asset_grabshot->add_submenu(grabshot_submenu = new GrabshotSubMenu(asset_grabshot));
429         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("png"),  GRABSHOT_PNG));
430         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("jpeg"), GRABSHOT_JPEG));
431         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("tiff"), GRABSHOT_TIFF));
432         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("ppm"),  GRABSHOT_PPM));
433         update_titles(shots_displayed = 1);
434 }
435
436 AssetPopupLoadFile::AssetPopupLoadFile(MWindow *mwindow, AWindowGUI *gui)
437  : BC_MenuItem(_("Load files..."), "o", 'o')
438 {
439         this->mwindow = mwindow;
440         this->gui = gui;
441 }
442
443 AssetPopupLoadFile::~AssetPopupLoadFile()
444 {
445 }
446
447 int AssetPopupLoadFile::handle_event()
448 {
449         mwindow->gui->mainmenu->load_file->thread->start();
450         return 1;
451 }
452
453 void AssetListMenu::update_titles(int shots)
454 {
455         format->update();
456         if( shots && !shots_displayed ) {
457                 shots_displayed = 1;
458                 add_item(asset_snapshot);
459                 add_item(asset_grabshot);
460         }
461         else if( !shots && shots_displayed ) {
462                 shots_displayed = 0;
463                 remove_item(asset_snapshot);
464                 remove_item(asset_grabshot);
465         }
466 }
467
468 AssetListCopy::AssetListCopy(MWindow *mwindow, AWindowGUI *gui)
469  : BC_MenuItem(_("Copy file list"))
470 {
471         this->mwindow = mwindow;
472         this->gui = gui;
473         copy_dialog = 0;
474 }
475 AssetListCopy::~AssetListCopy()
476 {
477         delete copy_dialog;
478 }
479
480 int AssetListCopy::handle_event()
481 {
482         int len = 0;
483         MWindowGUI *gui = mwindow->gui;
484         gui->lock_window("AssetListCopy::handle_event");
485         mwindow->awindow->gui->collect_assets();
486         int n = mwindow->session->drag_assets->total;
487         for( int i=0; i<n; ++i ) {
488                 Indexable *indexable = mwindow->session->drag_assets->values[i];
489                 const char *path = indexable->path;
490                 if( !*path ) continue;
491                 len += strlen(path) + 1;
492         }
493         char *text = new char[len+1], *cp = text;
494         for( int i=0; i<n; ++i ) {
495                 Indexable *indexable = mwindow->session->drag_assets->values[i];
496                 const char *path = indexable->path;
497                 if( !*path ) continue;
498                 cp += sprintf(cp, "%s\n", path);
499         }
500         *cp = 0;
501         int cur_x, cur_y;
502         gui->get_abs_cursor(cur_x, cur_y, 0);
503         gui->unlock_window(); 
504
505         if( n ) {
506                 if( !copy_dialog )
507                         copy_dialog = new AssetCopyDialog(this);
508                 copy_dialog->start(text, cur_x, cur_y);
509         }
510         else {
511                 eprintf(_("Nothing selected"));
512                 delete [] text;
513         }
514         return 1;
515 }
516
517 AssetCopyDialog::AssetCopyDialog(AssetListCopy *copy)
518  : BC_DialogThread()
519 {
520         this->copy = copy;
521         copy_window = 0;
522 }
523
524 void AssetCopyDialog::start(char *text, int x, int y)
525 {
526         close_window();
527         this->text = text;
528         this->x = x;  this->y = y;
529         BC_DialogThread::start();
530 }
531
532 AssetCopyDialog::~AssetCopyDialog()
533 {
534         close_window();
535 }
536
537 BC_Window* AssetCopyDialog::new_gui()
538 {
539         BC_DisplayInfo display_info;
540
541         copy_window = new AssetCopyWindow(this);
542         copy_window->create_objects();
543         return copy_window;
544 }
545
546 void AssetCopyDialog::handle_done_event(int result)
547 {
548         delete [] text;  text = 0;
549 }
550
551 void AssetCopyDialog::handle_close_event(int result)
552 {
553         copy_window = 0;
554 }
555
556
557 AssetCopyWindow::AssetCopyWindow(AssetCopyDialog *copy_dialog)
558  : BC_Window(_(PROGRAM_NAME ": Copy File List"),
559         copy_dialog->x - 500/2, copy_dialog->y - 200/2,
560         500, 200, 500, 200, 1, 0, 1)
561 {
562         this->copy_dialog = copy_dialog;
563 }
564
565 AssetCopyWindow::~AssetCopyWindow()
566 {
567 }
568
569 void AssetCopyWindow::create_objects()
570 {
571         lock_window("AssetCopyWindow::create_objects");
572         BC_Title *title;
573         int x = 10, y = 10, pad = 5;
574         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
575         y += title->get_h() + pad;
576         int text_w = get_w() - x - 10;
577         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
578         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
579         char *text = copy_dialog->text;
580         int len = strlen(text) + BCTEXTLEN;
581         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
582         file_list->create_objects();
583
584         add_subwindow(new BC_OKButton(this));
585         show_window();
586         unlock_window();
587 }
588
589 int AssetCopyWindow::resize_event(int w, int h)
590 {
591         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
592         int text_w = w - fx - 10;
593         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
594         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
595         file_list->reposition_window(fx, fy, text_w, text_rows);
596         return 0;
597 }
598
599 AssetListPaste::AssetListPaste(MWindow *mwindow, AWindowGUI *gui)
600  : BC_MenuItem(_("Paste file list"))
601 {
602         this->mwindow = mwindow;
603         this->gui = gui;
604         paste_dialog = 0;
605 }
606 AssetListPaste::~AssetListPaste()
607 {
608         delete paste_dialog;
609 }
610
611 int AssetListPaste::handle_event()
612 {
613         if( !paste_dialog )
614                 paste_dialog = new AssetPasteDialog(this);
615         else
616                 paste_dialog->close_window();
617         int cur_x, cur_y;
618         gui->get_abs_cursor(cur_x, cur_y, 0);
619         paste_dialog->start(cur_x, cur_y);
620         return 1;
621 }
622
623 AssetPasteDialog::AssetPasteDialog(AssetListPaste *paste)
624  : BC_DialogThread()
625 {
626         this->paste = paste;
627         paste_window = 0;
628 }
629
630 AssetPasteDialog::~AssetPasteDialog()
631 {
632         close_window();
633 }
634
635 BC_Window* AssetPasteDialog::new_gui()
636 {
637         paste_window = new AssetPasteWindow(this);
638         paste_window->create_objects();
639         return paste_window;
640 }
641
642 void AssetPasteDialog::handle_done_event(int result)
643 {
644         if( result ) return;
645         const char *bp = paste_window->file_list->get_text(), *ep = bp+strlen(bp);
646         ArrayList<char*> path_list;
647         path_list.set_array_delete();
648
649         for( const char *cp=bp; cp<ep && *cp; ) {
650                 const char *dp = strchr(cp, '\n');
651                 if( !dp ) dp = ep;
652                 char path[BCTEXTLEN], *pp = path;
653                 int len = sizeof(path)-1;
654                 while( --len>0 && cp<dp ) *pp++ = *cp++;
655                 if( *cp ) ++cp;
656                 *pp = 0;
657                 if( !strlen(path) ) continue;
658                 path_list.append(cstrdup(path));
659         }
660         if( !path_list.size() ) return;
661
662         MWindow *mwindow = paste->mwindow;
663         mwindow->interrupt_indexes();
664         mwindow->gui->lock_window("AssetPasteDialog::handle_done_event");
665         result = mwindow->load_filenames(&path_list, LOADMODE_RESOURCESONLY, 0);
666         mwindow->gui->unlock_window();
667         path_list.remove_all_objects();
668         mwindow->save_backup();
669         mwindow->restart_brender();
670         mwindow->session->changes_made = 1;
671 }
672
673 void AssetPasteDialog::handle_close_event(int result)
674 {
675         paste_window = 0;
676 }
677
678 void AssetPasteDialog::start(int x, int y)
679 {
680         this->x = x;  this->y = y;
681         BC_DialogThread::start();
682 }
683
684 AssetPasteWindow::AssetPasteWindow(AssetPasteDialog *paste_dialog)
685  : BC_Window(_(PROGRAM_NAME ": Paste File List"),
686         paste_dialog->x - 500/2, paste_dialog->y - 200/2,
687         500, 200, 500, 200, 1, 0, 1)
688 {
689         this->paste_dialog = paste_dialog;
690 }
691
692 AssetPasteWindow::~AssetPasteWindow()
693 {
694 }
695
696 void AssetPasteWindow::create_objects()
697 {
698         lock_window("AssetPasteWindow::create_objects()");
699         BC_Title *title;
700         int x = 10, y = 10, pad = 5;
701         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
702         y += title->get_h() + pad;
703         int text_w = get_w() - x - 10;
704         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
705         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
706         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
707         file_list->create_objects();
708         add_subwindow(new BC_OKButton(this));
709         add_subwindow(new BC_CancelButton(this));
710         show_window();
711         unlock_window();
712 }
713
714 int AssetPasteWindow::resize_event(int w, int h)
715 {
716         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
717         int text_w = w - fx - 10;
718         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
719         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
720         file_list->reposition_window(fx, fy, text_w, text_rows);
721         return 0;
722 }
723
724
725
726 AssetSnapshot::AssetSnapshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
727  : BC_MenuItem(_("Snapshot..."))
728 {
729         this->mwindow = mwindow;
730         this->asset_list_menu = asset_list_menu;
731 }
732
733 AssetSnapshot::~AssetSnapshot()
734 {
735 }
736
737 SnapshotSubMenu::SnapshotSubMenu(AssetSnapshot *asset_snapshot)
738 {
739         this->asset_snapshot = asset_snapshot;
740 }
741
742 SnapshotSubMenu::~SnapshotSubMenu()
743 {
744 }
745
746 SnapshotMenuItem::SnapshotMenuItem(SnapshotSubMenu *submenu, const char *text, int mode)
747  : BC_MenuItem(text)
748 {
749         this->submenu = submenu;
750         this->mode = mode;
751 }
752
753 SnapshotMenuItem::~SnapshotMenuItem()
754 {
755 }
756
757 int SnapshotMenuItem::handle_event()
758 {
759         MWindow *mwindow = submenu->asset_snapshot->mwindow;
760         EDL *edl = mwindow->edl;
761         if( !edl->have_video() ) return 1;
762
763         Preferences *preferences = mwindow->preferences;
764         char filename[BCTEXTLEN];
765         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
766         time_t tt;     time(&tt);
767         struct tm tm;  localtime_r(&tt,&tm);
768         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
769                 preferences->snapshot_path, _("snap"),
770                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
771                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
772         char *asset_path = FileSystem::basepath(filename);
773         Asset *asset = new Asset(asset_path);
774         delete [] asset_path;
775
776         int fw = edl->get_w(), fh = edl->get_h();
777         int fcolor_model = edl->session->color_model;
778
779         switch( mode ) {
780         case SNAPSHOT_PNG:
781                 asset->format = FILE_PNG;
782                 asset->png_use_alpha = 1;
783                 break;
784         case SNAPSHOT_JPEG:
785                 asset->format = FILE_JPEG;
786                 asset->jpeg_quality = 90;
787                 break;
788         case SNAPSHOT_TIFF:
789                 asset->format = FILE_TIFF;
790                 asset->tiff_cmodel = 0;
791                 asset->tiff_compression = 0;
792                 break;
793         case SNAPSHOT_PPM:
794                 asset->format = FILE_PPM;
795                 break;
796         }
797         asset->width = fw;
798         asset->height = fh;
799         asset->audio_data = 0;
800         asset->video_data = 1;
801         asset->video_length = 1;
802         asset->layers = 1;
803
804         File file;
805         int processors = preferences->project_smp + 1;
806         if( processors > 8 ) processors = 8;
807         file.set_processors(processors);
808         int ret = file.open_file(preferences, asset, 0, 1);
809         if( !ret ) {
810                 file.start_video_thread(1, fcolor_model,
811                         processors > 1 ? 2 : 1, 0);
812                 VFrame ***frames = file.get_video_buffer();
813                 VFrame *frame = frames[0][0];
814                 TransportCommand command;
815                 //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME;
816                 command.command = CURRENT_FRAME;
817                 command.get_edl()->copy_all(edl);
818                 command.change_type = CHANGE_ALL;
819                 command.realtime = 0;
820
821                 RenderEngine render_engine(0, preferences, 0, 0);
822                 CICache video_cache(preferences);
823                 render_engine.set_vcache(&video_cache);
824                 render_engine.arm_command(&command);
825
826                 double position = edl->local_session->get_selectionstart(1);
827                 int64_t source_position = (int64_t)(position * edl->get_frame_rate());
828                 ret = !render_engine.vrender ? 1 :
829                         render_engine.vrender->process_buffer(frame, source_position, 0);
830                 if( !ret )
831                         ret = file.write_video_buffer(1);
832                 file.close_file();
833         }
834         if( !ret ) {
835                 asset->folder_no = AW_MEDIA_FOLDER;
836                 mwindow->edl->assets->append(asset);
837                 mwindow->awindow->gui->async_update_assets();
838         }
839         else {
840                 eprintf(_("snapshot render failed"));
841                 asset->remove_user();
842         }
843         return 1;
844 }
845
846
847 AssetGrabshot::AssetGrabshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
848  : BC_MenuItem(_("Grabshot..."))
849 {
850         this->mwindow = mwindow;
851         this->asset_list_menu = asset_list_menu;
852 }
853
854 AssetGrabshot::~AssetGrabshot()
855 {
856 }
857
858 GrabshotSubMenu::GrabshotSubMenu(AssetGrabshot *asset_grabshot)
859 {
860         this->asset_grabshot = asset_grabshot;
861 }
862
863 GrabshotSubMenu::~GrabshotSubMenu()
864 {
865 }
866
867 GrabshotMenuItem::GrabshotMenuItem(GrabshotSubMenu *submenu, const char *text, int mode)
868  : BC_MenuItem(text)
869 {
870         this->submenu = submenu;
871         this->mode = mode;
872         grab_thread = 0;
873 }
874
875 GrabshotMenuItem::~GrabshotMenuItem()
876 {
877         delete grab_thread;
878 }
879
880 int GrabshotMenuItem::handle_event()
881 {
882         if( !grab_thread )
883                 grab_thread = new GrabshotThread(submenu->asset_grabshot->mwindow);
884         if( !grab_thread->running() )
885                 grab_thread->start(this);
886         return 1;
887 }
888
889 GrabshotThread::GrabshotThread(MWindow *mwindow)
890  : Thread(1, 0, 0)
891 {
892         this->mwindow = mwindow;
893         popup = 0;
894         done = -1;
895 }
896 GrabshotThread::~GrabshotThread()
897 {
898         delete popup;
899 }
900
901 void GrabshotThread::start(GrabshotMenuItem *menu_item)
902 {
903         popup = new GrabshotPopup(this, menu_item->mode);
904         popup->lock_window("GrabshotThread::start");
905         for( int i=0; i<4; ++i )
906                 edge[i] = new BC_Popup(mwindow->gui, 0,0, 1,1, ORANGE, 1);
907         mwindow->gui->grab_buttons();
908         mwindow->gui->grab_cursor();
909         popup->grab(mwindow->gui);
910         popup->create_objects();
911         popup->show_window();
912         popup->unlock_window();
913         done = 0;
914         Thread::start();
915 }
916
917 void GrabshotThread::run()
918 {
919         popup->lock_window("GrabshotThread::run 0");
920         while( !done ) {
921                 popup->update();
922                 popup->unlock_window();
923                 enable_cancel();
924                 Timer::delay(200);
925                 disable_cancel();
926                 popup->lock_window("GrabshotThread::run 1");
927         }
928         mwindow->gui->ungrab_cursor();
929         mwindow->gui->ungrab_buttons();
930         popup->ungrab(mwindow->gui);
931         for( int i=0; i<4; ++i ) delete edge[i];
932         popup->unlock_window();
933         delete popup;  popup = 0;
934 }
935
936 GrabshotPopup::GrabshotPopup(GrabshotThread *grab_thread, int mode)
937  : BC_Popup(grab_thread->mwindow->gui, 0,0, 16,16, -1,1)
938 {
939         this->grab_thread = grab_thread;
940         this->mode = mode;
941         dragging = -1;
942         grab_color = ORANGE;
943         x0 = y0 = x1 = y1 = -1;
944         lx0 = ly0 = lx1 = ly1 = -1;
945 }
946 GrabshotPopup::~GrabshotPopup()
947 {
948 }
949
950 int GrabshotPopup::grab_event(XEvent *event)
951 {
952         int cur_drag = dragging;
953         switch( event->type ) {
954         case ButtonPress:
955                 if( cur_drag > 0 ) return 1;
956                 x0 = event->xbutton.x_root;
957                 y0 = event->xbutton.y_root;
958                 if( !cur_drag ) {
959                         draw_selection(-1);
960                         if( event->xbutton.button == RIGHT_BUTTON ) break;
961                         if( x0>=get_x() && x0<get_x()+get_w() &&
962                             y0>=get_y() && y0<get_y()+get_h() ) break;
963                 }
964                 x1 = x0;  y1 = y0;
965                 draw_selection(1);
966                 dragging = 1;
967                 return 1;
968         case ButtonRelease:
969                 dragging = 0;
970         case MotionNotify:
971                 if( cur_drag > 0 ) {
972                         x1 = event->xbutton.x_root;
973                         y1 = event->xbutton.y_root;
974                         draw_selection(0);
975                 }
976                 return 1;
977         default:
978                 return 0;
979         }
980
981         int cx = lx0,     cy = ly0;
982         int cw = lx1-lx0, ch = ly1-ly0;
983         hide_window();
984         sync_display();
985         grab_thread->done = 1;
986
987         MWindow *mwindow = grab_thread->mwindow;
988         Preferences *preferences = mwindow->preferences;
989         char filename[BCTEXTLEN];
990         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
991         time_t tt;     time(&tt);
992         struct tm tm;  localtime_r(&tt,&tm);
993         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
994                 preferences->snapshot_path, _("grab"),
995                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
996                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
997         char *asset_path = FileSystem::basepath(filename);
998         Asset *asset = new Asset(asset_path);
999         delete [] asset_path;
1000         switch( mode ) {
1001         case GRABSHOT_PNG:
1002                 asset->format = FILE_PNG;
1003                 asset->png_use_alpha = 1;
1004                 break;
1005         case GRABSHOT_JPEG:
1006                 asset->format = FILE_JPEG;
1007                 asset->jpeg_quality = 90;
1008                 break;
1009         case GRABSHOT_TIFF:
1010                 asset->format = FILE_TIFF;
1011                 asset->tiff_cmodel = 0;
1012                 asset->tiff_compression = 0;
1013                 break;
1014         case GRABSHOT_PPM:
1015                 asset->format = FILE_PPM;
1016                 break;
1017         }
1018
1019 // no odd dimensions
1020         int rw = get_root_w(0), rh = get_root_h(0);
1021         if( cx < 0 ) { cw += cx;  cx = 0; }
1022         if( cy < 0 ) { ch += cy;  cy = 0; }
1023         if( cx+cw > rw ) cw = rw-cx;
1024         if( cy+ch > rh ) ch = rh-cy;
1025         if( !cw || !ch ) return 1;
1026
1027         VFrame vframe(cw,ch, BC_RGB888);
1028         if( cx+cw < rw ) ++cw;
1029         if( cy+ch < rh ) ++ch;
1030         BC_Capture capture_bitmap(cw,ch, 0);
1031         capture_bitmap.capture_frame(&vframe, cx,cy);
1032
1033         asset->width = vframe.get_w();
1034         asset->height = vframe.get_h();
1035         asset->audio_data = 0;
1036         asset->video_data = 1;
1037         asset->video_length = 1;
1038         asset->layers = 1;
1039
1040         File file;
1041         int fcolor_model = mwindow->edl->session->color_model;
1042         int processors = preferences->project_smp + 1;
1043         if( processors > 8 ) processors = 8;
1044         file.set_processors(processors);
1045         int ret = file.open_file(preferences, asset, 0, 1);
1046         if( !ret ) {
1047                 file.start_video_thread(1, fcolor_model,
1048                         processors > 1 ? 2 : 1, 0);
1049                 VFrame ***frames = file.get_video_buffer();
1050                 VFrame *frame = frames[0][0];
1051                 frame->transfer_from(&vframe);
1052                 ret = file.write_video_buffer(1);
1053                 file.close_file();
1054         }
1055         if( !ret ) {
1056                 asset->folder_no = AW_MEDIA_FOLDER;
1057                 mwindow->edl->assets->append(asset);
1058                 mwindow->awindow->gui->async_update_assets();
1059         }
1060         else {
1061                 eprintf(_("grabshot render failed"));
1062                 asset->remove_user();
1063         }
1064
1065         return 1;
1066 }
1067
1068 void GrabshotPopup::update()
1069 {
1070         set_color(grab_color ^= GREEN);
1071         draw_box(0,0, get_w(),get_h());
1072         flash(1);
1073 }
1074
1075 void GrabshotPopup::draw_selection(int show)
1076 {
1077         if( show < 0 ) {
1078                 for( int i=0; i<4; ++i ) hide_window(0);
1079                 flush();
1080                 return;
1081         }
1082
1083         int nx0 = x0 < x1 ? x0 : x1;
1084         int nx1 = x0 < x1 ? x1 : x0;
1085         int ny0 = y0 < y1 ? y0 : y1;
1086         int ny1 = y0 < y1 ? y1 : y0;
1087         lx0 = nx0;  lx1 = nx1;  ly0 = ny0;  ly1 = ny1;
1088
1089         --nx0;  --ny0;
1090         BC_Popup **edge = grab_thread->edge;
1091         edge[0]->reposition_window(nx0,ny0, nx1-nx0, 1);
1092         edge[1]->reposition_window(nx1,ny0, 1, ny1-ny0);
1093         edge[2]->reposition_window(nx0,ny1, nx1-nx0, 1);
1094         edge[3]->reposition_window(nx0,ny0, 1, ny1-ny0);
1095
1096         if( show > 0 ) {
1097                 for( int i=0; i<4; ++i ) edge[i]->show_window(0);
1098         }
1099         flush();
1100 }
1101