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