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