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