refresh frame fix, dblclk proxy viewer fix, vicon refresh fix for awdw resize, fix...
[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 "filesystem.h"
41 #include "filexml.h"
42 #include "language.h"
43 #include "loadfile.h"
44 #include "localsession.h"
45 #include "mainerror.h"
46 #include "mainindexes.h"
47 #include "mainmenu.h"
48 #include "mainsession.h"
49 #include "mwindow.h"
50 #include "mwindowgui.h"
51 #include "preferences.h"
52 #include "renderengine.h"
53 #include "tracks.h"
54 #include "transportque.h"
55 #include "vframe.h"
56 #include "vrender.h"
57 #include "vwindow.h"
58 #include "vwindowgui.h"
59 #include "zwindow.h"
60
61
62 AssetPopup::AssetPopup(MWindow *mwindow, AWindowGUI *gui)
63  : BC_PopupMenu(0, 0, 0, "", 0)
64 {
65         this->mwindow = mwindow;
66         this->gui = gui;
67 }
68
69 AssetPopup::~AssetPopup()
70 {
71 }
72
73 void AssetPopup::create_objects()
74 {
75         BC_MenuItem *menu_item;
76         BC_SubMenu *submenu;
77         add_item(info = new AssetPopupInfo(mwindow, this));
78         add_item(format = new AWindowListFormat(mwindow, gui));
79         add_item(new AssetPopupSort(mwindow, this));
80         add_item(index = new AssetPopupBuildIndex(mwindow, this));
81         add_item(view = new AssetPopupView(mwindow, this));
82         add_item(view_window = new AssetPopupViewWindow(mwindow, this));
83         add_item(mixer = new AssetPopupMixer(mwindow, this));
84         add_item(new AssetPopupPaste(mwindow, this));
85         add_item(menu_item = new BC_MenuItem(_("Match...")));
86         menu_item->add_submenu(submenu = new BC_SubMenu());
87         submenu->add_submenuitem(new AssetMatchSize(mwindow, this));
88         submenu->add_submenuitem(new AssetMatchRate(mwindow, this));
89         submenu->add_submenuitem(new AssetMatchAll(mwindow, this));
90         add_item(menu_item = new BC_MenuItem(_("Remove...")));
91         menu_item->add_submenu(submenu = new BC_SubMenu());
92         submenu->add_submenuitem(new AssetPopupProjectRemove(mwindow, this));
93         submenu->add_submenuitem(new AssetPopupDiskRemove(mwindow, this));
94 }
95
96 void AssetPopup::paste_assets()
97 {
98 // Collect items into the drag vectors for temporary storage
99         gui->lock_window("AssetPopup::paste_assets");
100         mwindow->gui->lock_window("AssetPopup::paste_assets");
101         mwindow->cwindow->gui->lock_window("AssetPopup::paste_assets");
102
103         gui->collect_assets();
104         mwindow->paste_assets(mwindow->edl->local_session->get_selectionstart(1),
105                 mwindow->edl->tracks->first, 0);   // do not overwrite
106
107         gui->unlock_window();
108         mwindow->gui->unlock_window();
109         mwindow->cwindow->gui->unlock_window();
110 }
111
112 void AssetPopup::match_size()
113 {
114 // Collect items into the drag vectors for temporary storage
115         gui->collect_assets();
116         mwindow->gui->lock_window("AssetPopup::match_size");
117         mwindow->asset_to_size();
118         mwindow->gui->unlock_window();
119 }
120
121 void AssetPopup::match_rate()
122 {
123 // Collect items into the drag vectors for temporary storage
124         gui->collect_assets();
125         mwindow->gui->lock_window("AssetPopup::match_rate");
126         mwindow->asset_to_rate();
127         mwindow->gui->unlock_window();
128 }
129
130 void AssetPopup::match_all()
131 {
132 // Collect items into the drag vectors for temporary storage
133         gui->collect_assets();
134         mwindow->gui->lock_window("AssetPopup::match_rate");
135         mwindow->asset_to_all();
136         mwindow->gui->unlock_window();
137 }
138
139 int AssetPopup::update()
140 {
141         format->update();
142         gui->collect_assets();
143         return 0;
144 }
145
146
147 AssetPopupInfo::AssetPopupInfo(MWindow *mwindow, AssetPopup *popup)
148  : BC_MenuItem(_("Info..."))
149 {
150         this->mwindow = mwindow;
151         this->popup = popup;
152 }
153
154 AssetPopupInfo::~AssetPopupInfo()
155 {
156 }
157
158 int AssetPopupInfo::handle_event()
159 {
160         int cur_x, cur_y;
161         popup->gui->get_abs_cursor(cur_x, cur_y);
162         int n = mwindow->session->drag_assets->size();
163         if( n > 0 ) {
164                 for( int i=0; i<n; ++i ) {
165                         AssetEdit *asset_edit = mwindow->awindow->get_asset_editor();
166                         asset_edit->edit_asset(
167                                 mwindow->session->drag_assets->values[i], cur_x-30*i, cur_y-30*i);
168                 }
169         }
170         else if( mwindow->session->drag_clips->size() ) {
171                 popup->gui->awindow->clip_edit->edit_clip(
172                         mwindow->session->drag_clips->values[0], cur_x, cur_y);
173         }
174         return 1;
175 }
176
177
178 AssetPopupBuildIndex::AssetPopupBuildIndex(MWindow *mwindow, AssetPopup *popup)
179  : BC_MenuItem(_("Rebuild index"))
180 {
181         this->mwindow = mwindow;
182         this->popup = popup;
183 }
184
185 AssetPopupBuildIndex::~AssetPopupBuildIndex()
186 {
187 }
188
189 int AssetPopupBuildIndex::handle_event()
190 {
191 //printf("AssetPopupBuildIndex::handle_event 1\n");
192         mwindow->rebuild_indices();
193         return 1;
194 }
195
196
197 AssetPopupSort::AssetPopupSort(MWindow *mwindow, AssetPopup *popup)
198  : BC_MenuItem(_("Sort"))
199 {
200         this->mwindow = mwindow;
201         this->popup = popup;
202 }
203
204 AssetPopupSort::~AssetPopupSort()
205 {
206 }
207
208 int AssetPopupSort::handle_event()
209 {
210         mwindow->awindow->gui->sort_assets();
211         return 1;
212 }
213
214
215 AssetPopupView::AssetPopupView(MWindow *mwindow, AssetPopup *popup)
216  : BC_MenuItem(_("View"))
217 {
218         this->mwindow = mwindow;
219         this->popup = popup;
220 }
221
222 AssetPopupView::~AssetPopupView()
223 {
224 }
225
226 int AssetPopupView::handle_event()
227 {
228         VWindow *vwindow = mwindow->get_viewer(1, DEFAULT_VWINDOW);
229
230         if( mwindow->session->drag_assets->total )
231                 vwindow->change_source(
232                         mwindow->session->drag_assets->values[0]);
233         else
234         if( mwindow->session->drag_clips->total )
235                 vwindow->change_source(
236                         mwindow->session->drag_clips->values[0]);
237
238         return 1;
239 }
240
241
242 AssetPopupViewWindow::AssetPopupViewWindow(MWindow *mwindow, AssetPopup *popup)
243  : BC_MenuItem(_("View in new window"))
244 {
245         this->mwindow = mwindow;
246         this->popup = popup;
247 }
248
249 AssetPopupViewWindow::~AssetPopupViewWindow()
250 {
251 }
252
253 int AssetPopupViewWindow::handle_event()
254 {
255         for( int i=0; i<mwindow->session->drag_assets->size(); ++i ) {
256                 VWindow *vwindow = mwindow->get_viewer(1);
257                 vwindow->gui->lock_window("AssetPopupView::handle_event 1");
258                 vwindow->change_source(mwindow->session->drag_assets->get(i));
259                 vwindow->gui->unlock_window();
260         }
261         for( int i=0; i<mwindow->session->drag_clips->size(); ++i ) {
262                 VWindow *vwindow = mwindow->get_viewer(1);
263                 vwindow->gui->lock_window("AssetPopupView::handle_event 2");
264                 vwindow->change_source(mwindow->session->drag_clips->get(i));
265                 vwindow->gui->unlock_window();
266         }
267         return 1;
268 }
269
270 AssetPopupMixer::AssetPopupMixer(MWindow *mwindow, AssetPopup *popup)
271  : BC_MenuItem(_("Open Mixers"))
272 {
273         this->mwindow = mwindow;
274         this->popup = popup;
275 }
276
277 AssetPopupMixer::~AssetPopupMixer()
278 {
279 }
280
281 int AssetPopupMixer::handle_event()
282 {
283         mwindow->gui->lock_window("AssetPopupMixer::handle_event");
284         mwindow->create_mixers();
285         mwindow->gui->unlock_window();
286         return 1;
287 }
288
289 AssetPopupPaste::AssetPopupPaste(MWindow *mwindow, AssetPopup *popup)
290  : BC_MenuItem(_("Paste"))
291 {
292         this->mwindow = mwindow;
293         this->popup = popup;
294 }
295
296 AssetPopupPaste::~AssetPopupPaste()
297 {
298 }
299
300 int AssetPopupPaste::handle_event()
301 {
302         popup->paste_assets();
303         return 1;
304 }
305
306
307 AssetMatchSize::AssetMatchSize(MWindow *mwindow, AssetPopup *popup)
308  : BC_MenuItem(_("Match project size"))
309 {
310         this->mwindow = mwindow;
311         this->popup = popup;
312 }
313
314 int AssetMatchSize::handle_event()
315 {
316         popup->match_size();
317         return 1;
318 }
319
320 AssetMatchRate::AssetMatchRate(MWindow *mwindow, AssetPopup *popup)
321  : BC_MenuItem(_("Match frame rate"))
322 {
323         this->mwindow = mwindow;
324         this->popup = popup;
325 }
326
327 int AssetMatchRate::handle_event()
328 {
329         popup->match_rate();
330         return 1;
331 }
332
333 AssetMatchAll::AssetMatchAll(MWindow *mwindow, AssetPopup *popup)
334  : BC_MenuItem(_("Match all"))
335 {
336         this->mwindow = mwindow;
337         this->popup = popup;
338 }
339
340 int AssetMatchAll::handle_event()
341 {
342         popup->match_all();
343         return 1;
344 }
345
346
347 AssetPopupProjectRemove::AssetPopupProjectRemove(MWindow *mwindow, AssetPopup *popup)
348  : BC_MenuItem(_("Remove from project"))
349 {
350         this->mwindow = mwindow;
351         this->popup = popup;
352 }
353
354 AssetPopupProjectRemove::~AssetPopupProjectRemove()
355 {
356 }
357
358 int AssetPopupProjectRemove::handle_event()
359 {
360         mwindow->remove_assets_from_project(1, 1,
361                 mwindow->session->drag_assets,
362                 mwindow->session->drag_clips);
363         return 1;
364 }
365
366
367 AssetPopupDiskRemove::AssetPopupDiskRemove(MWindow *mwindow, AssetPopup *popup)
368  : BC_MenuItem(_("Remove from disk"))
369 {
370         this->mwindow = mwindow;
371         this->popup = popup;
372 }
373
374
375 AssetPopupDiskRemove::~AssetPopupDiskRemove()
376 {
377 }
378
379 int AssetPopupDiskRemove::handle_event()
380 {
381         mwindow->awindow->asset_remove->start();
382         return 1;
383 }
384
385
386 AssetListMenu::AssetListMenu(MWindow *mwindow, AWindowGUI *gui)
387  : BC_PopupMenu(0, 0, 0, "", 0)
388 {
389         this->mwindow = mwindow;
390         this->gui = gui;
391 }
392
393 AssetListMenu::~AssetListMenu()
394 {
395         if( !shots_displayed ) {
396                 delete asset_snapshot;
397                 delete asset_grabshot;
398         }
399 }
400
401 void AssetListMenu::create_objects()
402 {
403         add_item(load_file = new AssetPopupLoadFile(mwindow, gui));
404         add_item(format = new AWindowListFormat(mwindow, gui));
405         add_item(new AWindowListSort(mwindow, gui));
406         add_item(new AssetListCopy(mwindow, gui));
407         add_item(new AssetListPaste(mwindow, gui));
408         SnapshotSubMenu *snapshot_submenu;
409         add_item(asset_snapshot = new AssetSnapshot(mwindow, this));
410         asset_snapshot->add_submenu(snapshot_submenu = new SnapshotSubMenu(asset_snapshot));
411         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("png"),  SNAPSHOT_PNG));
412         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("jpeg"), SNAPSHOT_JPEG));
413         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("tiff"), SNAPSHOT_TIFF));
414         snapshot_submenu->add_submenuitem(new SnapshotMenuItem(snapshot_submenu, _("ppm"),  SNAPSHOT_PPM));
415         GrabshotSubMenu *grabshot_submenu;
416         add_item(asset_grabshot = new AssetGrabshot(mwindow, this));
417         asset_grabshot->add_submenu(grabshot_submenu = new GrabshotSubMenu(asset_grabshot));
418         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("png"),  GRABSHOT_PNG));
419         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("jpeg"), GRABSHOT_JPEG));
420         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("tiff"), GRABSHOT_TIFF));
421         grabshot_submenu->add_submenuitem(new GrabshotMenuItem(grabshot_submenu, _("ppm"),  GRABSHOT_PPM));
422         update_titles(shots_displayed = 1);
423 }
424
425 AssetPopupLoadFile::AssetPopupLoadFile(MWindow *mwindow, AWindowGUI *gui)
426  : BC_MenuItem(_("Load files..."), "o", 'o')
427 {
428         this->mwindow = mwindow;
429         this->gui = gui;
430 }
431
432 AssetPopupLoadFile::~AssetPopupLoadFile()
433 {
434 }
435
436 int AssetPopupLoadFile::handle_event()
437 {
438         mwindow->gui->mainmenu->load_file->thread->start();
439         return 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         lock_window("AssetCopyWindow::create_objects");
561         BC_Title *title;
562         int x = 10, y = 10, pad = 5;
563         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
564         y += title->get_h() + pad;
565         int text_w = get_w() - x - 10;
566         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
567         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
568         char *text = copy_dialog->text;
569         int len = strlen(text) + BCTEXTLEN;
570         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
571         file_list->create_objects();
572
573         add_subwindow(new BC_OKButton(this));
574         show_window();
575         unlock_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         lock_window("AssetPasteWindow::create_objects()");
688         BC_Title *title;
689         int x = 10, y = 10, pad = 5;
690         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
691         y += title->get_h() + pad;
692         int text_w = get_w() - x - 10;
693         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
694         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
695         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
696         file_list->create_objects();
697         add_subwindow(new BC_OKButton(this));
698         add_subwindow(new BC_CancelButton(this));
699         show_window();
700         unlock_window();
701 }
702
703 int AssetPasteWindow::resize_event(int w, int h)
704 {
705         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
706         int text_w = w - fx - 10;
707         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
708         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
709         file_list->reposition_window(fx, fy, text_w, text_rows);
710         return 0;
711 }
712
713
714
715 AssetSnapshot::AssetSnapshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
716  : BC_MenuItem(_("Snapshot..."))
717 {
718         this->mwindow = mwindow;
719         this->asset_list_menu = asset_list_menu;
720 }
721
722 AssetSnapshot::~AssetSnapshot()
723 {
724 }
725
726 SnapshotSubMenu::SnapshotSubMenu(AssetSnapshot *asset_snapshot)
727 {
728         this->asset_snapshot = asset_snapshot;
729 }
730
731 SnapshotSubMenu::~SnapshotSubMenu()
732 {
733 }
734
735 SnapshotMenuItem::SnapshotMenuItem(SnapshotSubMenu *submenu, const char *text, int mode)
736  : BC_MenuItem(text)
737 {
738         this->submenu = submenu;
739         this->mode = mode;
740 }
741
742 SnapshotMenuItem::~SnapshotMenuItem()
743 {
744 }
745
746 int SnapshotMenuItem::handle_event()
747 {
748         MWindow *mwindow = submenu->asset_snapshot->mwindow;
749         EDL *edl = mwindow->edl;
750         if( !edl->have_video() ) return 1;
751
752         Preferences *preferences = mwindow->preferences;
753         char filename[BCTEXTLEN];
754         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
755         time_t tt;     time(&tt);
756         struct tm tm;  localtime_r(&tt,&tm);
757         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
758                 preferences->snapshot_path, _("snap"),
759                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
760                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
761         char *asset_path = FileSystem::basepath(filename);
762         Asset *asset = new Asset(asset_path);
763         delete [] asset_path;
764
765         int fw = edl->get_w(), fh = edl->get_h();
766         int fcolor_model = edl->session->color_model;
767
768         switch( mode ) {
769         case SNAPSHOT_PNG:
770                 asset->format = FILE_PNG;
771                 asset->png_use_alpha = 1;
772                 break;
773         case SNAPSHOT_JPEG:
774                 asset->format = FILE_JPEG;
775                 asset->jpeg_quality = 90;
776                 break;
777         case SNAPSHOT_TIFF:
778                 asset->format = FILE_TIFF;
779                 asset->tiff_cmodel = 0;
780                 asset->tiff_compression = 0;
781                 break;
782         case SNAPSHOT_PPM:
783                 asset->format = FILE_PPM;
784                 break;
785         }
786         asset->width = fw;
787         asset->height = fh;
788         asset->audio_data = 0;
789         asset->video_data = 1;
790         asset->video_length = 1;
791         asset->layers = 1;
792
793         File file;
794         int processors = preferences->project_smp + 1;
795         if( processors > 8 ) processors = 8;
796         file.set_processors(processors);
797         int ret = file.open_file(preferences, asset, 0, 1);
798         if( !ret ) {
799                 file.start_video_thread(1, fcolor_model,
800                         processors > 1 ? 2 : 1, 0);
801                 VFrame ***frames = file.get_video_buffer();
802                 VFrame *frame = frames[0][0];
803                 TransportCommand command;
804                 //command.command = audio_tracks ? NORMAL_FWD : CURRENT_FRAME;
805                 command.command = CURRENT_FRAME;
806                 command.get_edl()->copy_all(edl);
807                 command.change_type = CHANGE_ALL;
808                 command.realtime = 0;
809
810                 RenderEngine render_engine(0, preferences, 0, 0);
811                 CICache video_cache(preferences);
812                 render_engine.set_vcache(&video_cache);
813                 render_engine.arm_command(&command);
814
815                 double position = edl->local_session->get_selectionstart(1);
816                 int64_t source_position = (int64_t)(position * edl->get_frame_rate());
817                 ret = !render_engine.vrender ? 1 :
818                         render_engine.vrender->process_buffer(frame, source_position, 0);
819                 if( !ret )
820                         ret = file.write_video_buffer(1);
821                 file.close_file();
822         }
823         if( !ret ) {
824                 asset->awindow_folder = AW_MEDIA_FOLDER;
825                 mwindow->edl->assets->append(asset);
826                 mwindow->awindow->gui->async_update_assets();
827         }
828         else {
829                 eprintf(_("snapshot render failed"));
830                 asset->remove_user();
831         }
832         return 1;
833 }
834
835
836 AssetGrabshot::AssetGrabshot(MWindow *mwindow, AssetListMenu *asset_list_menu)
837  : BC_MenuItem(_("Grabshot..."))
838 {
839         this->mwindow = mwindow;
840         this->asset_list_menu = asset_list_menu;
841 }
842
843 AssetGrabshot::~AssetGrabshot()
844 {
845 }
846
847 GrabshotSubMenu::GrabshotSubMenu(AssetGrabshot *asset_grabshot)
848 {
849         this->asset_grabshot = asset_grabshot;
850 }
851
852 GrabshotSubMenu::~GrabshotSubMenu()
853 {
854 }
855
856 GrabshotMenuItem::GrabshotMenuItem(GrabshotSubMenu *submenu, const char *text, int mode)
857  : BC_MenuItem(text)
858 {
859         this->submenu = submenu;
860         this->mode = mode;
861         grab_thread = 0;
862 }
863
864 GrabshotMenuItem::~GrabshotMenuItem()
865 {
866         delete grab_thread;
867 }
868
869 int GrabshotMenuItem::handle_event()
870 {
871         if( !grab_thread )
872                 grab_thread = new GrabshotThread(submenu->asset_grabshot->mwindow);
873         if( !grab_thread->running() )
874                 grab_thread->start(this);
875         return 1;
876 }
877
878 GrabshotThread::GrabshotThread(MWindow *mwindow)
879  : Thread(1, 0, 0)
880 {
881         this->mwindow = mwindow;
882         popup = 0;
883         done = -1;
884 }
885 GrabshotThread::~GrabshotThread()
886 {
887         delete popup;
888 }
889
890 void GrabshotThread::start(GrabshotMenuItem *menu_item)
891 {
892         popup = new GrabshotPopup(this, menu_item->mode);
893         popup->lock_window("GrabshotThread::start");
894         for( int i=0; i<4; ++i )
895                 edge[i] = new BC_Popup(mwindow->gui, 0,0, 1,1, ORANGE, 1);
896         mwindow->gui->grab_buttons();
897         mwindow->gui->grab_cursor();
898         popup->grab(mwindow->gui);
899         popup->create_objects();
900         popup->show_window();
901         popup->unlock_window();
902         done = 0;
903         Thread::start();
904 }
905
906 void GrabshotThread::run()
907 {
908         popup->lock_window("GrabshotThread::run 0");
909         while( !done ) {
910                 popup->update();
911                 popup->unlock_window();
912                 enable_cancel();
913                 Timer::delay(200);
914                 disable_cancel();
915                 popup->lock_window("GrabshotThread::run 1");
916         }
917         mwindow->gui->ungrab_cursor();
918         mwindow->gui->ungrab_buttons();
919         popup->ungrab(mwindow->gui);
920         for( int i=0; i<4; ++i ) delete edge[i];
921         popup->unlock_window();
922         delete popup;  popup = 0;
923 }
924
925 GrabshotPopup::GrabshotPopup(GrabshotThread *grab_thread, int mode)
926  : BC_Popup(grab_thread->mwindow->gui, 0,0, 16,16, -1,1)
927 {
928         this->grab_thread = grab_thread;
929         this->mode = mode;
930         dragging = -1;
931         grab_color = ORANGE;
932         x0 = y0 = x1 = y1 = -1;
933         lx0 = ly0 = lx1 = ly1 = -1;
934 }
935 GrabshotPopup::~GrabshotPopup()
936 {
937 }
938
939 int GrabshotPopup::grab_event(XEvent *event)
940 {
941         int cur_drag = dragging;
942         switch( event->type ) {
943         case ButtonPress:
944                 if( cur_drag > 0 ) return 1;
945                 x0 = event->xbutton.x_root;
946                 y0 = event->xbutton.y_root;
947                 if( !cur_drag ) {
948                         draw_selection(-1);
949                         if( event->xbutton.button == RIGHT_BUTTON ) break;
950                         if( x0>=get_x() && x0<get_x()+get_w() &&
951                             y0>=get_y() && y0<get_y()+get_h() ) break;
952                 }
953                 x1 = x0;  y1 = y0;
954                 draw_selection(1);
955                 dragging = 1;
956                 return 1;
957         case ButtonRelease:
958                 dragging = 0;
959         case MotionNotify:
960                 if( cur_drag > 0 ) {
961                         x1 = event->xbutton.x_root;
962                         y1 = event->xbutton.y_root;
963                         draw_selection(0);
964                 }
965                 return 1;
966         default:
967                 return 0;
968         }
969
970         int cx = lx0,     cy = ly0;
971         int cw = lx1-lx0, ch = ly1-ly0;
972         hide_window();
973         sync_display();
974         grab_thread->done = 1;
975
976         MWindow *mwindow = grab_thread->mwindow;
977         Preferences *preferences = mwindow->preferences;
978         char filename[BCTEXTLEN];
979         static const char *exts[] = { "png", "jpg", "tif", "ppm" };
980         time_t tt;     time(&tt);
981         struct tm tm;  localtime_r(&tt,&tm);
982         snprintf(filename,sizeof(filename),"%s/%s_%04d%02d%02d-%02d%02d%02d.%s",
983                 preferences->snapshot_path, _("grab"),
984                 1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday,
985                 tm.tm_hour,tm.tm_min,tm.tm_sec, exts[mode]);
986         char *asset_path = FileSystem::basepath(filename);
987         Asset *asset = new Asset(asset_path);
988         delete [] asset_path;
989         switch( mode ) {
990         case GRABSHOT_PNG:
991                 asset->format = FILE_PNG;
992                 asset->png_use_alpha = 1;
993                 break;
994         case GRABSHOT_JPEG:
995                 asset->format = FILE_JPEG;
996                 asset->jpeg_quality = 90;
997                 break;
998         case GRABSHOT_TIFF:
999                 asset->format = FILE_TIFF;
1000                 asset->tiff_cmodel = 0;
1001                 asset->tiff_compression = 0;
1002                 break;
1003         case GRABSHOT_PPM:
1004                 asset->format = FILE_PPM;
1005                 break;
1006         }
1007
1008 // no odd dimensions
1009         int rw = get_root_w(0), rh = get_root_h(0);
1010         if( cx < 0 ) { cw += cx;  cx = 0; }
1011         if( cy < 0 ) { ch += cy;  cy = 0; }
1012         if( cx+cw > rw ) cw = rw-cx;
1013         if( cy+ch > rh ) ch = rh-cy;
1014         if( !cw || !ch ) return 1;
1015
1016         VFrame vframe(cw,ch, BC_RGB888);
1017         if( cx+cw < rw ) ++cw;
1018         if( cy+ch < rh ) ++ch;
1019         BC_Capture capture_bitmap(cw,ch, 0);
1020         capture_bitmap.capture_frame(&vframe, cx,cy);
1021
1022         asset->width = vframe.get_w();
1023         asset->height = vframe.get_h();
1024         asset->audio_data = 0;
1025         asset->video_data = 1;
1026         asset->video_length = 1;
1027         asset->layers = 1;
1028
1029         File file;
1030         int fcolor_model = mwindow->edl->session->color_model;
1031         int processors = preferences->project_smp + 1;
1032         if( processors > 8 ) processors = 8;
1033         file.set_processors(processors);
1034         int ret = file.open_file(preferences, asset, 0, 1);
1035         if( !ret ) {
1036                 file.start_video_thread(1, fcolor_model,
1037                         processors > 1 ? 2 : 1, 0);
1038                 VFrame ***frames = file.get_video_buffer();
1039                 VFrame *frame = frames[0][0];
1040                 frame->transfer_from(&vframe);
1041                 ret = file.write_video_buffer(1);
1042                 file.close_file();
1043         }
1044         if( !ret ) {
1045                 asset->awindow_folder = AW_MEDIA_FOLDER;
1046                 mwindow->edl->assets->append(asset);
1047                 mwindow->awindow->gui->async_update_assets();
1048         }
1049         else {
1050                 eprintf(_("grabshot render failed"));
1051                 asset->remove_user();
1052         }
1053
1054         return 1;
1055 }
1056
1057 void GrabshotPopup::update()
1058 {
1059         set_color(grab_color ^= GREEN);
1060         draw_box(0,0, get_w(),get_h());
1061         flash(1);
1062 }
1063
1064 void GrabshotPopup::draw_selection(int show)
1065 {
1066         if( show < 0 ) {
1067                 for( int i=0; i<4; ++i ) hide_window(0);
1068                 flush();
1069                 return;
1070         }
1071
1072         int nx0 = x0 < x1 ? x0 : x1;
1073         int nx1 = x0 < x1 ? x1 : x0;
1074         int ny0 = y0 < y1 ? y0 : y1;
1075         int ny1 = y0 < y1 ? y1 : y0;
1076         lx0 = nx0;  lx1 = nx1;  ly0 = ny0;  ly1 = ny1;
1077
1078         --nx0;  --ny0;
1079         BC_Popup **edge = grab_thread->edge;
1080         edge[0]->reposition_window(nx0,ny0, nx1-nx0, 1);
1081         edge[1]->reposition_window(nx1,ny0, 1, ny1-ny0);
1082         edge[2]->reposition_window(nx0,ny1, nx1-nx0, 1);
1083         edge[3]->reposition_window(nx0,ny0, 1, ny1-ny0);
1084
1085         if( show > 0 ) {
1086                 for( int i=0; i<4; ++i ) edge[i]->show_window(0);
1087         }
1088         flush();
1089 }
1090