add audio to proxy, and minor bug fixes
[goodguy/history.git] / cinelerra-5.1 / cinelerra / assetpopup.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "assetedit.h"
24 #include "assetpopup.h"
25 #include "assetremove.h"
26 #include "assets.h"
27 #include "awindow.h"
28 #include "awindowgui.h"
29 #include "bccapture.h"
30 #include "bcdisplayinfo.h"
31 #include "bcsignals.h"
32 #include "cache.h"
33 #include "clipedit.h"
34 #include "cstrdup.h"
35 #include "cwindow.h"
36 #include "cwindowgui.h"
37 #include "edl.h"
38 #include "edlsession.h"
39 #include "file.h"
40 #include "filexml.h"
41 #include "language.h"
42 #include "localsession.h"
43 #include "mainerror.h"
44 #include "mainindexes.h"
45 #include "mainsession.h"
46 #include "mwindow.h"
47 #include "mwindowgui.h"
48 #include "preferences.h"
49 #include "renderengine.h"
50 #include "tracks.h"
51 #include "transportque.h"
52 #include "vframe.h"
53 #include "vrender.h"
54 #include "vwindow.h"
55 #include "vwindowgui.h"
56 #include "zwindow.h"
57
58
59 AssetPopup::AssetPopup(MWindow *mwindow, AWindowGUI *gui)
60  : BC_PopupMenu(0, 0, 0, "", 0)
61 {
62         this->mwindow = mwindow;
63         this->gui = gui;
64 }
65
66 AssetPopup::~AssetPopup()
67 {
68 }
69
70 void AssetPopup::create_objects()
71 {
72         BC_MenuItem *menu_item;
73         BC_SubMenu *submenu;
74         add_item(info = new AssetPopupInfo(mwindow, this));
75         add_item(format = new AWindowListFormat(mwindow, gui));
76         add_item(menu_item = new BC_MenuItem(_("Sort...")));
77         menu_item->add_submenu(submenu = new BC_SubMenu());
78         submenu->add_submenuitem(new AssetPopupSortNames(mwindow, this));
79         submenu->add_submenuitem(new AssetPopupSortTimes(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         mwindow->session->clear_drag_proxy();
108
109         gui->unlock_window();
110         mwindow->gui->unlock_window();
111         mwindow->cwindow->gui->unlock_window();
112 }
113
114 void AssetPopup::match_size()
115 {
116 // Collect items into the drag vectors for temporary storage
117         gui->collect_assets();
118         mwindow->gui->lock_window("AssetPopup::match_size");
119         mwindow->asset_to_size();
120         mwindow->gui->unlock_window();
121 }
122
123 void AssetPopup::match_rate()
124 {
125 // Collect items into the drag vectors for temporary storage
126         gui->collect_assets();
127         mwindow->gui->lock_window("AssetPopup::match_rate");
128         mwindow->asset_to_rate();
129         mwindow->gui->unlock_window();
130 }
131
132 void AssetPopup::match_all()
133 {
134 // Collect items into the drag vectors for temporary storage
135         gui->collect_assets();
136         mwindow->gui->lock_window("AssetPopup::match_rate");
137         mwindow->asset_to_all();
138         mwindow->gui->unlock_window();
139 }
140
141 int AssetPopup::update()
142 {
143         format->update();
144         int proxy = mwindow->edl->session->awindow_folder == AW_PROXY_FOLDER ? 1 : 0;
145         gui->collect_assets(proxy);
146         return 0;
147 }
148
149
150 AssetPopupInfo::AssetPopupInfo(MWindow *mwindow, AssetPopup *popup)
151  : BC_MenuItem(_("Info..."))
152 {
153         this->mwindow = mwindow;
154         this->popup = popup;
155 }
156
157 AssetPopupInfo::~AssetPopupInfo()
158 {
159 }
160
161 int AssetPopupInfo::handle_event()
162 {
163         int cur_x, cur_y;
164         popup->gui->get_abs_cursor(cur_x, cur_y);
165         int n = mwindow->session->drag_assets->size();
166         if( n > 0 ) {
167                 for( int i=0; i<n; ++i ) {
168                         AssetEdit *asset_edit = mwindow->awindow->get_asset_editor();
169                         asset_edit->edit_asset(
170                                 mwindow->session->drag_assets->values[i], cur_x-30*i, cur_y-30*i);
171                 }
172         }
173         else if( mwindow->session->drag_clips->size() ) {
174                 popup->gui->awindow->clip_edit->edit_clip(
175                         mwindow->session->drag_clips->values[0], cur_x, cur_y);
176         }
177         return 1;
178 }
179
180
181 AssetPopupBuildIndex::AssetPopupBuildIndex(MWindow *mwindow, AssetPopup *popup)
182  : BC_MenuItem(_("Rebuild index"))
183 {
184         this->mwindow = mwindow;
185         this->popup = popup;
186 }
187
188 AssetPopupBuildIndex::~AssetPopupBuildIndex()
189 {
190 }
191
192 int AssetPopupBuildIndex::handle_event()
193 {
194 //printf("AssetPopupBuildIndex::handle_event 1\n");
195         mwindow->rebuild_indices();
196         return 1;
197 }
198
199
200 AssetPopupSortNames::AssetPopupSortNames(MWindow *mwindow, AssetPopup *popup)
201  : BC_MenuItem(_("Sort names"))
202 {
203         this->mwindow = mwindow;
204         this->popup = popup;
205 }
206
207 AssetPopupSortNames::~AssetPopupSortNames()
208 {
209 }
210
211 int AssetPopupSortNames::handle_event()
212 {
213         mwindow->awindow->gui->sort_assets(0);
214         return 1;
215 }
216
217 AssetPopupSortTimes::AssetPopupSortTimes(MWindow *mwindow, AssetPopup *popup)
218  : BC_MenuItem(_("Sort times"))
219 {
220         this->mwindow = mwindow;
221         this->popup = popup;
222 }
223
224 AssetPopupSortTimes::~AssetPopupSortTimes()
225 {
226 }
227
228 int AssetPopupSortTimes::handle_event()
229 {
230         mwindow->awindow->gui->sort_assets(1);
231         return 1;
232 }
233
234
235 AssetPopupView::AssetPopupView(MWindow *mwindow, AssetPopup *popup)
236  : BC_MenuItem(_("View"))
237 {
238         this->mwindow = mwindow;
239         this->popup = popup;
240 }
241
242 AssetPopupView::~AssetPopupView()
243 {
244 }
245
246 int AssetPopupView::handle_event()
247 {
248         VWindow *vwindow = mwindow->get_viewer(1, DEFAULT_VWINDOW);
249
250         if( mwindow->session->drag_assets->total )
251                 vwindow->change_source(
252                         mwindow->session->drag_assets->values[0]);
253         else
254         if( mwindow->session->drag_clips->total )
255                 vwindow->change_source(
256                         mwindow->session->drag_clips->values[0]);
257
258         return 1;
259 }
260
261
262 AssetPopupViewWindow::AssetPopupViewWindow(MWindow *mwindow, AssetPopup *popup)
263  : BC_MenuItem(_("View in new window"))
264 {
265         this->mwindow = mwindow;
266         this->popup = popup;
267 }
268
269 AssetPopupViewWindow::~AssetPopupViewWindow()
270 {
271 }
272
273 int AssetPopupViewWindow::handle_event()
274 {
275         for( int i=0; i<mwindow->session->drag_assets->size(); ++i ) {
276                 VWindow *vwindow = mwindow->get_viewer(1);
277                 vwindow->gui->lock_window("AssetPopupView::handle_event 1");
278                 vwindow->change_source(mwindow->session->drag_assets->get(i));
279                 vwindow->gui->unlock_window();
280         }
281         for( int i=0; i<mwindow->session->drag_clips->size(); ++i ) {
282                 VWindow *vwindow = mwindow->get_viewer(1);
283                 vwindow->gui->lock_window("AssetPopupView::handle_event 2");
284                 vwindow->change_source(mwindow->session->drag_clips->get(i));
285                 vwindow->gui->unlock_window();
286         }
287         return 1;
288 }
289
290 AssetPopupMixer::AssetPopupMixer(MWindow *mwindow, AssetPopup *popup)
291  : BC_MenuItem(_("Open Mixers"))
292 {
293         this->mwindow = mwindow;
294         this->popup = popup;
295 }
296
297 AssetPopupMixer::~AssetPopupMixer()
298 {
299 }
300
301 int AssetPopupMixer::handle_event()
302 {
303         mwindow->gui->lock_window("AssetPopupMixer::handle_event");
304         mwindow->create_mixers();
305         mwindow->gui->unlock_window();
306         return 1;
307 }
308
309 AssetPopupPaste::AssetPopupPaste(MWindow *mwindow, AssetPopup *popup)
310  : BC_MenuItem(_("Paste"))
311 {
312         this->mwindow = mwindow;
313         this->popup = popup;
314 }
315
316 AssetPopupPaste::~AssetPopupPaste()
317 {
318 }
319
320 int AssetPopupPaste::handle_event()
321 {
322         popup->paste_assets();
323         return 1;
324 }
325
326
327 AssetMatchSize::AssetMatchSize(MWindow *mwindow, AssetPopup *popup)
328  : BC_MenuItem(_("Match project size"))
329 {
330         this->mwindow = mwindow;
331         this->popup = popup;
332 }
333
334 int AssetMatchSize::handle_event()
335 {
336         popup->match_size();
337         return 1;
338 }
339
340 AssetMatchRate::AssetMatchRate(MWindow *mwindow, AssetPopup *popup)
341  : BC_MenuItem(_("Match frame rate"))
342 {
343         this->mwindow = mwindow;
344         this->popup = popup;
345 }
346
347 int AssetMatchRate::handle_event()
348 {
349         popup->match_rate();
350         return 1;
351 }
352
353 AssetMatchAll::AssetMatchAll(MWindow *mwindow, AssetPopup *popup)
354  : BC_MenuItem(_("Match all"))
355 {
356         this->mwindow = mwindow;
357         this->popup = popup;
358 }
359
360 int AssetMatchAll::handle_event()
361 {
362         popup->match_all();
363         return 1;
364 }
365
366
367 AssetPopupProjectRemove::AssetPopupProjectRemove(MWindow *mwindow, AssetPopup *popup)
368  : BC_MenuItem(_("Remove from project"))
369 {
370         this->mwindow = mwindow;
371         this->popup = popup;
372 }
373
374 AssetPopupProjectRemove::~AssetPopupProjectRemove()
375 {
376 }
377
378 int AssetPopupProjectRemove::handle_event()
379 {
380         mwindow->remove_assets_from_project(1, 1,
381                 mwindow->session->drag_assets,
382                 mwindow->session->drag_clips);
383         return 1;
384 }
385
386
387 AssetPopupDiskRemove::AssetPopupDiskRemove(MWindow *mwindow, AssetPopup *popup)
388  : BC_MenuItem(_("Remove from disk"))
389 {
390         this->mwindow = mwindow;
391         this->popup = popup;
392 }
393
394
395 AssetPopupDiskRemove::~AssetPopupDiskRemove()
396 {
397 }
398
399 int AssetPopupDiskRemove::handle_event()
400 {
401         mwindow->awindow->asset_remove->start();
402         return 1;
403 }
404
405
406 AssetListMenu::AssetListMenu(MWindow *mwindow, AWindowGUI *gui)
407  : BC_PopupMenu(0, 0, 0, "", 0)
408 {
409         this->mwindow = mwindow;
410         this->gui = gui;
411 }
412
413 AssetListMenu::~AssetListMenu()
414 {
415         if( !shots_displayed ) {
416                 delete asset_snapshot;
417                 delete asset_grabshot;
418         }
419 }
420
421 void AssetListMenu::create_objects()
422 {
423         add_item(format = new AWindowListFormat(mwindow, gui));
424         add_item(new AWindowListSort(mwindow, gui));
425         add_item(new AssetListCopy(mwindow, gui));
426         add_item(new AssetListPaste(mwindow, gui));
427         SnapshotSubMenu *snapshot_submenu;
428         add_item(asset_snapshot = new AssetSnapshot(mwindow, this));
429         asset_snapshot->add_submenu(snapshot_submenu = new SnapshotSubMenu(asset_snapshot));
430         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("png"),  SNAPSHOT_PNG));
431         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("jpeg"), SNAPSHOT_JPEG));
432         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("tiff"), SNAPSHOT_TIFF));
433         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("ppm"),  SNAPSHOT_PPM));
434         GrabshotSubMenu *grabshot_submenu;
435         add_item(asset_grabshot = new AssetGrabshot(mwindow, this));
436         asset_grabshot->add_submenu(grabshot_submenu = new GrabshotSubMenu(asset_grabshot));
437         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("png"),  GRABSHOT_PNG));
438         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("jpeg"), GRABSHOT_JPEG));
439         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("tiff"), GRABSHOT_TIFF));
440         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("ppm"),  GRABSHOT_PPM));
441         update_titles(shots_displayed = 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         BC_Title *title;
563         int x = 10, y = 10, pad = 5;
564         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
565         y += title->get_h() + pad;
566         int text_w = get_w() - x - 10;
567         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
568         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
569         char *text = copy_dialog->text;
570         int len = strlen(text) + BCTEXTLEN;
571         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
572         file_list->create_objects();
573
574         add_subwindow(new BC_OKButton(this));
575         show_window();
576 }
577
578 int AssetCopyWindow::resize_event(int w, int h)
579 {
580         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
581         int text_w = w - fx - 10;
582         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
583         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
584         file_list->reposition_window(fx, fy, text_w, text_rows);
585         return 0;
586 }
587
588 AssetListPaste::AssetListPaste(MWindow *mwindow, AWindowGUI *gui)
589  : BC_MenuItem(_("Paste file list"))
590 {
591         this->mwindow = mwindow;
592         this->gui = gui;
593         paste_dialog = 0;
594 }
595 AssetListPaste::~AssetListPaste()
596 {
597         delete paste_dialog;
598 }
599
600 int AssetListPaste::handle_event()
601 {
602         if( !paste_dialog )
603                 paste_dialog = new AssetPasteDialog(this);
604         else
605                 paste_dialog->close_window();
606         int cur_x, cur_y;
607         gui->get_abs_cursor(cur_x, cur_y, 0);
608         paste_dialog->start(cur_x, cur_y);
609         return 1;
610 }
611
612 AssetPasteDialog::AssetPasteDialog(AssetListPaste *paste)
613  : BC_DialogThread()
614 {
615         this->paste = paste;
616         paste_window = 0;
617 }
618
619 AssetPasteDialog::~AssetPasteDialog()
620 {
621         close_window();
622 }
623
624 BC_Window* AssetPasteDialog::new_gui()
625 {
626         paste_window = new AssetPasteWindow(this);
627         paste_window->create_objects();
628         return paste_window;
629 }
630
631 void AssetPasteDialog::handle_done_event(int result)
632 {
633         if( result ) return;
634         const char *bp = paste_window->file_list->get_text(), *ep = bp+strlen(bp);
635         ArrayList<char*> path_list;
636         path_list.set_array_delete();
637
638         for( const char *cp=bp; cp<ep && *cp; ) {
639                 const char *dp = strchr(cp, '\n');
640                 if( !dp ) dp = ep;
641                 char path[BCTEXTLEN], *pp = path;
642                 int len = sizeof(path)-1;
643                 while( --len>0 && cp<dp ) *pp++ = *cp++;
644                 if( *cp ) ++cp;
645                 *pp = 0;
646                 if( !strlen(path) ) continue;
647                 path_list.append(cstrdup(path));
648         }
649         if( !path_list.size() ) return;
650
651         MWindow *mwindow = paste->mwindow;
652         mwindow->interrupt_indexes();
653         mwindow->gui->lock_window("AssetPasteDialog::handle_done_event");
654         result = mwindow->load_filenames(&path_list, LOADMODE_RESOURCESONLY, 0);
655         mwindow->gui->unlock_window();
656         path_list.remove_all_objects();
657         mwindow->save_backup();
658         mwindow->restart_brender();
659         mwindow->session->changes_made = 1;
660 }
661
662 void AssetPasteDialog::handle_close_event(int result)
663 {
664         paste_window = 0;
665 }
666
667 void AssetPasteDialog::start(int x, int y)
668 {
669         this->x = x;  this->y = y;
670         BC_DialogThread::start();
671 }
672
673 AssetPasteWindow::AssetPasteWindow(AssetPasteDialog *paste_dialog)
674  : BC_Window(_(PROGRAM_NAME ": Paste File List"),
675         paste_dialog->x - 500/2, paste_dialog->y - 200/2,
676         500, 200, 500, 200, 1, 0, 1)
677 {
678         this->paste_dialog = paste_dialog;
679 }
680
681 AssetPasteWindow::~AssetPasteWindow()
682 {
683 }
684
685 void AssetPasteWindow::create_objects()
686 {
687         BC_Title *title;
688         int x = 10, y = 10, pad = 5;
689         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
690         y += title->get_h() + pad;
691         int text_w = get_w() - x - 10;
692         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
693         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
694         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
695         file_list->create_objects();
696         add_subwindow(new BC_OKButton(this));
697         add_subwindow(new BC_CancelButton(this));
698         show_window();
699 }
700
701 int AssetPasteWindow::resize_event(int w, int h)
702 {
703         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
704         int text_w = w - fx - 10;
705         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
706         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
707         file_list->reposition_window(fx, fy, text_w, text_rows);
708         return 0;
709 }
710
711
712
713 AssetSnapshot::AssetSnapshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
714  : BC_MenuItem(_("Snapshot..."))
715 {
716         this->mwindow = mwindow;
717         this->asset_list_menu = asset_list_menu;
718 }
719
720 AssetSnapshot::~AssetSnapshot()
721 {
722 }
723
724 SnapshotSubMenu::SnapshotSubMenu(AssetSnapshot *asset_snapshot)
725 {
726         this->asset_snapshot = asset_snapshot;
727 }
728
729 SnapshotSubMenu::~SnapshotSubMenu()
730 {
731 }
732
733 SnapshotMenuItem::SnapshotMenuItem(SnapshotSubMenu *submenu, const char *text, int mode)
734  : BC_MenuItem(text)
735 {
736         this->submenu = submenu;
737         this->mode = mode;
738 }
739
740 SnapshotMenuItem::~SnapshotMenuItem()
741 {
742 }
743
744 int SnapshotMenuItem::handle_event()
745 {
746         MWindow *mwindow = submenu->asset_snapshot->mwindow;
747         EDL *edl = mwindow->edl;
748         if( !edl->have_video() ) return 1;
749
750         Preferences *preferences = mwindow->preferences;
751         char filename[BCTEXTLEN];
752         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
753         time_t tt;     time(&tt);
754         struct tm tm;  localtime_r(&tt,&tm);
755         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
756                 preferences->snapshot_path, _("snap"),
757                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
758                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
759         int fw = edl->get_w(), fh = edl->get_h();
760         int fcolor_model = edl->session->color_model;
761
762         Asset *asset = new Asset(filename);
763         switch( mode ) {
764         case SNAPSHOT_PNG:
765                 asset->format = FILE_PNG;
766                 asset->png_use_alpha = 1;
767                 break;
768         case SNAPSHOT_JPEG:
769                 asset->format = FILE_JPEG;
770                 asset->jpeg_quality = 90;
771                 break;
772         case SNAPSHOT_TIFF:
773                 asset->format = FILE_TIFF;
774                 asset->tiff_cmodel = 0;
775                 asset->tiff_compression = 0;
776                 break;
777         case SNAPSHOT_PPM:
778                 asset->format = FILE_PPM;
779                 break;
780         }
781         asset->width = fw;
782         asset->height = fh;
783         asset->audio_data = 0;
784         asset->video_data = 1;
785         asset->video_length = 1;
786         asset->layers = 1;
787
788         File file;
789         int processors = preferences->project_smp + 1;
790         if( processors > 8 ) processors = 8;
791         file.set_processors(processors);
792         int ret = file.open_file(preferences, asset, 0, 1);
793         if( !ret ) {
794                 file.start_video_thread(1, fcolor_model,
795                         processors > 1 ? 2 : 1, 0);
796                 VFrame ***frames = file.get_video_buffer();
797                 VFrame *frame = frames[0][0];
798                 TransportCommand command;
799                 //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME;
800                 command.command = CURRENT_FRAME;
801                 command.get_edl()->copy_all(edl);
802                 command.change_type = CHANGE_ALL;
803                 command.realtime = 0;
804
805                 RenderEngine render_engine(0, preferences, 0, 0);
806                 CICache video_cache(preferences);
807                 render_engine.set_vcache(&video_cache);
808                 render_engine.arm_command(&command);
809
810                 double position = edl->local_session->get_selectionstart(1);
811                 int64_t source_position = (int64_t)(position * edl->get_frame_rate());
812                 ret = !render_engine.vrender ? 1 :
813                         render_engine.vrender->process_buffer(frame, source_position, 0);
814                 if( !ret )
815                         ret = file.write_video_buffer(1);
816                 file.close_file();
817         }
818         if( !ret ) {
819                 asset->awindow_folder = AW_MEDIA_FOLDER;
820                 mwindow->edl->assets->append(asset);
821                 mwindow->awindow->gui->async_update_assets();
822         }
823         else {
824                 eprintf(_("snapshot render failed"));
825                 asset->remove_user();
826         }
827         return 1;
828 }
829
830
831 AssetGrabshot::AssetGrabshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
832  : BC_MenuItem(_("Grabshot..."))
833 {
834         this->mwindow = mwindow;
835         this->asset_list_menu = asset_list_menu;
836 }
837
838 AssetGrabshot::~AssetGrabshot()
839 {
840 }
841
842 GrabshotSubMenu::GrabshotSubMenu(AssetGrabshot *asset_grabshot)
843 {
844         this->asset_grabshot = asset_grabshot;
845 }
846
847 GrabshotSubMenu::~GrabshotSubMenu()
848 {
849 }
850
851 GrabshotMenuItem::GrabshotMenuItem(GrabshotSubMenu *submenu, const char *text, int mode)
852  : BC_MenuItem(text)
853 {
854         this->submenu = submenu;
855         this->mode = mode;
856         grab_thread = 0;
857 }
858
859 GrabshotMenuItem::~GrabshotMenuItem()
860 {
861         delete grab_thread;
862 }
863
864 int GrabshotMenuItem::handle_event()
865 {
866         if( !grab_thread )
867                 grab_thread = new GrabshotThread(submenu->asset_grabshot->mwindow);
868         if( !grab_thread->running() )
869                 grab_thread->start(this);
870         return 1;
871 }
872
873 GrabshotThread::GrabshotThread(MWindow *mwindow)
874  : Thread(1, 0, 0)
875 {
876         this->mwindow = mwindow;
877         popup = 0;
878         done = -1;
879 }
880 GrabshotThread::~GrabshotThread()
881 {
882         delete popup;
883 }
884
885 void GrabshotThread::start(GrabshotMenuItem *menu_item)
886 {
887         popup = new GrabshotPopup(this, menu_item->mode);
888         popup->lock_window("GrabshotThread::start");
889         for( int i=0; i<4; ++i )
890                 edge[i] = new BC_Popup(mwindow->gui, 0,0, 1,1, ORANGE, 1);
891         mwindow->gui->grab_buttons();
892         mwindow->gui->grab_cursor();
893         popup->grab(mwindow->gui);
894         popup->create_objects();
895         popup->show_window();
896         popup->unlock_window();
897         done = 0;
898         Thread::start();
899 }
900
901 void GrabshotThread::run()
902 {
903         popup->lock_window("GrabshotThread::run 0");
904         while( !done ) {
905                 popup->update();
906                 popup->unlock_window();
907                 enable_cancel();
908                 Timer::delay(200);
909                 disable_cancel();
910                 popup->lock_window("GrabshotThread::run 1");
911         }
912         mwindow->gui->ungrab_cursor();
913         mwindow->gui->ungrab_buttons();
914         popup->ungrab(mwindow->gui);
915         for( int i=0; i<4; ++i ) delete edge[i];
916         popup->unlock_window();
917         delete popup;  popup = 0;
918 }
919
920 GrabshotPopup::GrabshotPopup(GrabshotThread *grab_thread, int mode)
921  : BC_Popup(grab_thread->mwindow->gui, 0,0, 16,16, -1,1)
922 {
923         this->grab_thread = grab_thread;
924         this->mode = mode;
925         dragging = -1;
926         grab_color = ORANGE;
927         x0 = y0 = x1 = y1 = -1;
928         lx0 = ly0 = lx1 = ly1 = -1;
929 }
930 GrabshotPopup::~GrabshotPopup()
931 {
932 }
933
934 int GrabshotPopup::grab_event(XEvent *event)
935 {
936         int cur_drag = dragging;
937         switch( event->type ) {
938         case ButtonPress:
939                 if( cur_drag > 0 ) return 1;
940                 x0 = event->xbutton.x_root;
941                 y0 = event->xbutton.y_root;
942                 if( !cur_drag ) {
943                         draw_selection(-1);
944                         if( event->xbutton.button == RIGHT_BUTTON ) break;
945                         if( x0>=get_x() && x0<get_x()+get_w() &&
946                             y0>=get_y() && y0<get_y()+get_h() ) break;
947                 }
948                 x1 = x0;  y1 = y0;
949                 draw_selection(1);
950                 dragging = 1;
951                 return 1;
952         case ButtonRelease:
953                 dragging = 0;
954         case MotionNotify:
955                 if( cur_drag > 0 ) {
956                         x1 = event->xbutton.x_root;
957                         y1 = event->xbutton.y_root;
958                         draw_selection(0);
959                 }
960                 return 1;
961         default:
962                 return 0;
963         }
964
965         int cx = lx0,     cy = ly0;
966         int cw = lx1-lx0, ch = ly1-ly0;
967         hide_window();
968         sync_display();
969         grab_thread->done = 1;
970
971         MWindow *mwindow = grab_thread->mwindow;
972         Preferences *preferences = mwindow->preferences;
973         char filename[BCTEXTLEN];
974         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
975         time_t tt;     time(&tt);
976         struct tm tm;  localtime_r(&tt,&tm);
977         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
978                 preferences->snapshot_path, _("grab"),
979                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
980                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
981
982         Asset *asset = new Asset(filename);
983         switch( mode ) {
984         case GRABSHOT_PNG:
985                 asset->format = FILE_PNG;
986                 asset->png_use_alpha = 1;
987                 break;
988         case GRABSHOT_JPEG:
989                 asset->format = FILE_JPEG;
990                 asset->jpeg_quality = 90;
991                 break;
992         case GRABSHOT_TIFF:
993                 asset->format = FILE_TIFF;
994                 asset->tiff_cmodel = 0;
995                 asset->tiff_compression = 0;
996                 break;
997         case GRABSHOT_PPM:
998                 asset->format = FILE_PPM;
999                 break;
1000         }
1001
1002 // no odd dimensions
1003         int rw = get_root_w(0), rh = get_root_h(0);
1004         if( cx < 0 ) { cw += cx;  cx = 0; }
1005         if( cy < 0 ) { ch += cy;  cy = 0; }
1006         if( cx+cw > rw ) cw = rw-cx;
1007         if( cy+ch > rh ) ch = rh-cy;
1008         if( !cw || !ch ) return 1;
1009
1010         VFrame vframe(cw,ch, BC_RGB888);
1011         if( cx+cw < rw ) ++cw;
1012         if( cy+ch < rh ) ++ch;
1013         BC_Capture capture_bitmap(cw,ch, 0);
1014         capture_bitmap.capture_frame(&vframe, cx,cy);
1015
1016         asset->width = vframe.get_w();
1017         asset->height = vframe.get_h();
1018         asset->audio_data = 0;
1019         asset->video_data = 1;
1020         asset->video_length = 1;
1021         asset->layers = 1;
1022
1023         File file;
1024         int fcolor_model = mwindow->edl->session->color_model;
1025         int processors = preferences->project_smp + 1;
1026         if( processors > 8 ) processors = 8;
1027         file.set_processors(processors);
1028         int ret = file.open_file(preferences, asset, 0, 1);
1029         if( !ret ) {
1030                 file.start_video_thread(1, fcolor_model,
1031                         processors > 1 ? 2 : 1, 0);
1032                 VFrame ***frames = file.get_video_buffer();
1033                 VFrame *frame = frames[0][0];
1034                 frame->transfer_from(&vframe);
1035                 ret = file.write_video_buffer(1);
1036                 file.close_file();
1037         }
1038         if( !ret ) {
1039                 asset->awindow_folder = AW_MEDIA_FOLDER;
1040                 mwindow->edl->assets->append(asset);
1041                 mwindow->awindow->gui->async_update_assets();
1042         }
1043         else {
1044                 eprintf(_("grabshot render failed"));
1045                 asset->remove_user();
1046         }
1047
1048         return 1;
1049 }
1050
1051 void GrabshotPopup::update()
1052 {
1053         set_color(grab_color ^= GREEN);
1054         draw_box(0,0, get_w(),get_h());
1055         flash(1);
1056 }
1057
1058 void GrabshotPopup::draw_selection(int show)
1059 {
1060         if( show < 0 ) {
1061                 for( int i=0; i<4; ++i ) hide_window(0);
1062                 flush();
1063                 return;
1064         }
1065
1066         int nx0 = x0 < x1 ? x0 : x1;
1067         int nx1 = x0 < x1 ? x1 : x0;
1068         int ny0 = y0 < y1 ? y0 : y1;
1069         int ny1 = y0 < y1 ? y1 : y0;
1070         lx0 = nx0;  lx1 = nx1;  ly0 = ny0;  ly1 = ny1;
1071
1072         --nx0;  --ny0;
1073         BC_Popup **edge = grab_thread->edge;
1074         edge[0]->reposition_window(nx0,ny0, nx1-nx0, 1);
1075         edge[1]->reposition_window(nx1,ny0, 1, ny1-ny0);
1076         edge[2]->reposition_window(nx0,ny1, nx1-nx0, 1);
1077         edge[3]->reposition_window(nx0,ny0, 1, ny1-ny0);
1078
1079         if( show > 0 ) {
1080                 for( int i=0; i<4; ++i ) edge[i]->show_window(0);
1081         }
1082         flush();
1083 }
1084