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