glx ply3d fixes, async_gui updates for edits, dflt vs auto kfrm fix, mask mode 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 "awindow.h"
27 #include "awindowgui.h"
28 #include "bcdisplayinfo.h"
29 #include "bcsignals.h"
30 #include "clipedit.h"
31 #include "cstrdup.h"
32 #include "cwindow.h"
33 #include "cwindowgui.h"
34 #include "edl.h"
35 #include "edlsession.h"
36 #include "filexml.h"
37 #include "language.h"
38 #include "localsession.h"
39 #include "mainerror.h"
40 #include "mainindexes.h"
41 #include "mainsession.h"
42 #include "mwindow.h"
43 #include "mwindowgui.h"
44 #include "tracks.h"
45 #include "vwindow.h"
46 #include "vwindowgui.h"
47 #include "zwindow.h"
48
49
50 AssetPopup::AssetPopup(MWindow *mwindow, AWindowGUI *gui)
51  : BC_PopupMenu(0, 0, 0, "", 0)
52 {
53         this->mwindow = mwindow;
54         this->gui = gui;
55 }
56
57 AssetPopup::~AssetPopup()
58 {
59 }
60
61 void AssetPopup::create_objects()
62 {
63         BC_MenuItem *menu_item;
64         BC_SubMenu *submenu;
65         add_item(info = new AssetPopupInfo(mwindow, this));
66         add_item(format = new AWindowListFormat(mwindow, gui));
67         add_item(new AssetPopupSort(mwindow, this));
68         add_item(index = new AssetPopupBuildIndex(mwindow, this));
69         add_item(view = new AssetPopupView(mwindow, this));
70         add_item(view_window = new AssetPopupViewWindow(mwindow, this));
71         add_item(mixer = new AssetPopupMixer(mwindow, this));
72         add_item(new AssetPopupPaste(mwindow, this));
73         add_item(menu_item = new BC_MenuItem(_("Match...")));
74         menu_item->add_submenu(submenu = new BC_SubMenu());
75         submenu->add_submenuitem(new AssetMatchSize(mwindow, this));
76         submenu->add_submenuitem(new AssetMatchRate(mwindow, this));
77         submenu->add_submenuitem(new AssetMatchAll(mwindow, this));
78         add_item(menu_item = new BC_MenuItem(_("Remove...")));
79         menu_item->add_submenu(submenu = new BC_SubMenu());
80         submenu->add_submenuitem(new AssetPopupProjectRemove(mwindow, this));
81         submenu->add_submenuitem(new AssetPopupDiskRemove(mwindow, this));
82 }
83
84 void AssetPopup::paste_assets()
85 {
86 // Collect items into the drag vectors for temporary storage
87         gui->lock_window("AssetPopup::paste_assets");
88         mwindow->gui->lock_window("AssetPopup::paste_assets");
89         mwindow->cwindow->gui->lock_window("AssetPopup::paste_assets");
90
91         gui->collect_assets();
92         mwindow->paste_assets(mwindow->edl->local_session->get_selectionstart(1),
93                 mwindow->edl->tracks->first,
94                 0);   // do not overwrite
95
96         gui->unlock_window();
97         mwindow->gui->unlock_window();
98         mwindow->cwindow->gui->unlock_window();
99 }
100
101 void AssetPopup::match_size()
102 {
103 // Collect items into the drag vectors for temporary storage
104         gui->collect_assets();
105         mwindow->gui->lock_window("AssetPopup::match_size");
106         mwindow->asset_to_size();
107         mwindow->gui->unlock_window();
108 }
109
110 void AssetPopup::match_rate()
111 {
112 // Collect items into the drag vectors for temporary storage
113         gui->collect_assets();
114         mwindow->gui->lock_window("AssetPopup::match_rate");
115         mwindow->asset_to_rate();
116         mwindow->gui->unlock_window();
117 }
118
119 void AssetPopup::match_all()
120 {
121 // Collect items into the drag vectors for temporary storage
122         gui->collect_assets();
123         mwindow->gui->lock_window("AssetPopup::match_rate");
124         mwindow->asset_to_all();
125         mwindow->gui->unlock_window();
126 }
127
128 int AssetPopup::update()
129 {
130         format->update();
131         gui->collect_assets();
132         return 0;
133 }
134
135
136 AssetPopupInfo::AssetPopupInfo(MWindow *mwindow, AssetPopup *popup)
137  : BC_MenuItem(_("Info..."))
138 {
139         this->mwindow = mwindow;
140         this->popup = popup;
141 }
142
143 AssetPopupInfo::~AssetPopupInfo()
144 {
145 }
146
147 int AssetPopupInfo::handle_event()
148 {
149         int cur_x, cur_y;
150         popup->gui->get_abs_cursor(cur_x, cur_y);
151         if( mwindow->session->drag_assets->size() ) {
152                 AssetEdit *asset_edit = mwindow->awindow->get_asset_editor();
153                 asset_edit->edit_asset(
154                         mwindow->session->drag_assets->values[0], cur_x, cur_y);
155         }
156         else if( mwindow->session->drag_clips->size() ) {
157                 popup->gui->awindow->clip_edit->edit_clip(
158                         mwindow->session->drag_clips->values[0], cur_x, cur_y);
159         }
160         return 1;
161 }
162
163
164 AssetPopupBuildIndex::AssetPopupBuildIndex(MWindow *mwindow, AssetPopup *popup)
165  : BC_MenuItem(_("Rebuild index"))
166 {
167         this->mwindow = mwindow;
168         this->popup = popup;
169 }
170
171 AssetPopupBuildIndex::~AssetPopupBuildIndex()
172 {
173 }
174
175 int AssetPopupBuildIndex::handle_event()
176 {
177 //printf("AssetPopupBuildIndex::handle_event 1\n");
178         mwindow->rebuild_indices();
179         return 1;
180 }
181
182
183 AssetPopupSort::AssetPopupSort(MWindow *mwindow, AssetPopup *popup)
184  : BC_MenuItem(_("Sort items"))
185 {
186         this->mwindow = mwindow;
187         this->popup = popup;
188 }
189
190 AssetPopupSort::~AssetPopupSort()
191 {
192 }
193
194 int AssetPopupSort::handle_event()
195 {
196         mwindow->awindow->gui->sort_assets();
197         return 1;
198 }
199
200
201 AssetPopupView::AssetPopupView(MWindow *mwindow, AssetPopup *popup)
202  : BC_MenuItem(_("View"))
203 {
204         this->mwindow = mwindow;
205         this->popup = popup;
206 }
207
208 AssetPopupView::~AssetPopupView()
209 {
210 }
211
212 int AssetPopupView::handle_event()
213 {
214         VWindow *vwindow = mwindow->get_viewer(1, DEFAULT_VWINDOW);
215         vwindow->gui->lock_window("AssetPopupView::handle_event");
216
217         if( mwindow->session->drag_assets->total )
218                 vwindow->change_source(
219                         mwindow->session->drag_assets->values[0]);
220         else
221         if( mwindow->session->drag_clips->total )
222                 vwindow->change_source(
223                         mwindow->session->drag_clips->values[0]);
224
225         vwindow->gui->unlock_window();
226         return 1;
227 }
228
229
230 AssetPopupViewWindow::AssetPopupViewWindow(MWindow *mwindow, AssetPopup *popup)
231  : BC_MenuItem(_("View in new window"))
232 {
233         this->mwindow = mwindow;
234         this->popup = popup;
235 }
236
237 AssetPopupViewWindow::~AssetPopupViewWindow()
238 {
239 }
240
241 int AssetPopupViewWindow::handle_event()
242 {
243 // Find window with nothing
244         VWindow *vwindow = mwindow->get_viewer(1);
245
246 // TODO: create new vwindow or change current vwindow
247         vwindow->gui->lock_window("AssetPopupView::handle_event");
248
249         if( mwindow->session->drag_assets->total )
250                 vwindow->change_source(
251                         mwindow->session->drag_assets->values[0]);
252         else
253         if( mwindow->session->drag_clips->total )
254                 vwindow->change_source(
255                         mwindow->session->drag_clips->values[0]);
256
257         vwindow->gui->unlock_window();
258         return 1;
259 }
260
261 AssetPopupMixer::AssetPopupMixer(MWindow *mwindow, AssetPopup *popup)
262  : BC_MenuItem(_("Open Mixers"))
263 {
264         this->mwindow = mwindow;
265         this->popup = popup;
266 }
267
268 AssetPopupMixer::~AssetPopupMixer()
269 {
270 }
271
272 int AssetPopupMixer::handle_event()
273 {
274         mwindow->select_zwindow(0);
275         for( int i=0; i<mwindow->session->drag_assets->total; ++i ) {
276                 Indexable *indexable = mwindow->session->drag_assets->values[i];
277                 ArrayList<Indexable*> new_assets;
278                 new_assets.append(indexable);
279                 Track *track = mwindow->edl->tracks->last;
280                 mwindow->load_assets(&new_assets, -1, LOADMODE_NEW_TRACKS, 0, 0, 0, 0, 0, 0);
281                 track = !track ? mwindow->edl->tracks->first : track->next;
282                 Mixer *mixer = 0;
283                 ZWindow *zwindow = mwindow->get_mixer(mixer);
284                 while( track ) {
285                         track->play = track->record = 0;
286                         if( track->data_type == TRACK_VIDEO ) {
287                                 sprintf(track->title, _("Mixer %d"), zwindow->idx);
288                         }
289                         mixer->mixer_ids.append(track->get_mixer_id());
290                         track = track->next;
291                 }
292                 char *path = indexable->path;
293                 char *tp = strrchr(path, '/');
294                 if( !tp ) tp = path; else ++tp;
295                 zwindow->set_title(tp);
296                 zwindow->start();
297         }
298         mwindow->queue_mixers(mwindow->edl,CURRENT_FRAME,0,0,1,0);
299         mwindow->resync_guis();
300         return 1;
301 }
302
303 AssetPopupPaste::AssetPopupPaste(MWindow *mwindow, AssetPopup *popup)
304  : BC_MenuItem(_("Paste"))
305 {
306         this->mwindow = mwindow;
307         this->popup = popup;
308 }
309
310 AssetPopupPaste::~AssetPopupPaste()
311 {
312 }
313
314 int AssetPopupPaste::handle_event()
315 {
316         popup->paste_assets();
317         return 1;
318 }
319
320
321 AssetMatchSize::AssetMatchSize(MWindow *mwindow, AssetPopup *popup)
322  : BC_MenuItem(_("Match project size"))
323 {
324         this->mwindow = mwindow;
325         this->popup = popup;
326 }
327
328 int AssetMatchSize::handle_event()
329 {
330         popup->match_size();
331         return 1;
332 }
333
334 AssetMatchRate::AssetMatchRate(MWindow *mwindow, AssetPopup *popup)
335  : BC_MenuItem(_("Match frame rate"))
336 {
337         this->mwindow = mwindow;
338         this->popup = popup;
339 }
340
341 int AssetMatchRate::handle_event()
342 {
343         popup->match_rate();
344         return 1;
345 }
346
347 AssetMatchAll::AssetMatchAll(MWindow *mwindow, AssetPopup *popup)
348  : BC_MenuItem(_("Match all"))
349 {
350         this->mwindow = mwindow;
351         this->popup = popup;
352 }
353
354 int AssetMatchAll::handle_event()
355 {
356         popup->match_all();
357         return 1;
358 }
359
360
361 AssetPopupProjectRemove::AssetPopupProjectRemove(MWindow *mwindow, AssetPopup *popup)
362  : BC_MenuItem(_("Remove from project"))
363 {
364         this->mwindow = mwindow;
365         this->popup = popup;
366 }
367
368 AssetPopupProjectRemove::~AssetPopupProjectRemove()
369 {
370 }
371
372 int AssetPopupProjectRemove::handle_event()
373 {
374         mwindow->remove_assets_from_project(1, 1,
375                 mwindow->session->drag_assets,
376                 mwindow->session->drag_clips);
377         return 1;
378 }
379
380
381 AssetPopupDiskRemove::AssetPopupDiskRemove(MWindow *mwindow, AssetPopup *popup)
382  : BC_MenuItem(_("Remove from disk"))
383 {
384         this->mwindow = mwindow;
385         this->popup = popup;
386 }
387
388
389 AssetPopupDiskRemove::~AssetPopupDiskRemove()
390 {
391 }
392
393 int AssetPopupDiskRemove::handle_event()
394 {
395         mwindow->awindow->asset_remove->start();
396         return 1;
397 }
398
399
400 AssetListMenu::AssetListMenu(MWindow *mwindow, AWindowGUI *gui)
401  : BC_PopupMenu(0, 0, 0, "", 0)
402 {
403         this->mwindow = mwindow;
404         this->gui = gui;
405 }
406
407 AssetListMenu::~AssetListMenu()
408 {
409 }
410
411 void AssetListMenu::create_objects()
412 {
413         add_item(format = new AWindowListFormat(mwindow, gui));
414         add_item(new AWindowListSort(mwindow, gui));
415         add_item(new AssetListCopy(mwindow, gui));
416         add_item(new AssetListPaste(mwindow, gui));
417         update_titles();
418 }
419
420 void AssetListMenu::update_titles()
421 {
422         format->update();
423 }
424
425 AssetListCopy::AssetListCopy(MWindow *mwindow, AWindowGUI *gui)
426  : BC_MenuItem(_("Copy file list"))
427 {
428         this->mwindow = mwindow;
429         this->gui = gui;
430         copy_dialog = 0;
431 }
432 AssetListCopy::~AssetListCopy()
433 {
434         delete copy_dialog;
435 }
436
437 int AssetListCopy::handle_event()
438 {
439         int len = 0;
440         MWindowGUI *gui = mwindow->gui;
441         gui->lock_window("AssetListCopy::handle_event");
442         mwindow->awindow->gui->collect_assets();
443         int n = mwindow->session->drag_assets->total;
444         for( int i=0; i<n; ++i ) {
445                 Indexable *indexable = mwindow->session->drag_assets->values[i];
446                 const char *path = indexable->path;
447                 if( !*path ) continue;
448                 len += strlen(path) + 1;
449         }
450         char *text = new char[len+1], *cp = text;
451         for( int i=0; i<n; ++i ) {
452                 Indexable *indexable = mwindow->session->drag_assets->values[i];
453                 const char *path = indexable->path;
454                 if( !*path ) continue;
455                 cp += sprintf(cp, "%s\n", path);
456         }
457         *cp = 0;
458         int cur_x, cur_y;
459         gui->get_abs_cursor(cur_x, cur_y, 0);
460         gui->unlock_window(); 
461
462         if( n ) {
463                 if( !copy_dialog )
464                         copy_dialog = new AssetCopyDialog(this);
465                 copy_dialog->start(text, cur_x, cur_y);
466         }
467         else {
468                 eprintf(_("Nothing selected"));
469                 delete [] text;
470         }
471         return 1;
472 }
473
474 AssetCopyDialog::AssetCopyDialog(AssetListCopy *copy)
475  : BC_DialogThread()
476 {
477         this->copy = copy;
478         copy_window = 0;
479 }
480
481 void AssetCopyDialog::start(char *text, int x, int y)
482 {
483         close_window();
484         this->text = text;
485         this->x = x;  this->y = y;
486         BC_DialogThread::start();
487 }
488
489 AssetCopyDialog::~AssetCopyDialog()
490 {
491         close_window();
492 }
493
494 BC_Window* AssetCopyDialog::new_gui()
495 {
496         BC_DisplayInfo display_info;
497
498         copy_window = new AssetCopyWindow(this);
499         copy_window->create_objects();
500         return copy_window;
501 }
502
503 void AssetCopyDialog::handle_done_event(int result)
504 {
505         delete [] text;  text = 0;
506 }
507
508 void AssetCopyDialog::handle_close_event(int result)
509 {
510         copy_window = 0;
511 }
512
513
514 AssetCopyWindow::AssetCopyWindow(AssetCopyDialog *copy_dialog)
515  : BC_Window(_(PROGRAM_NAME ": Copy File List"),
516         copy_dialog->x - 500/2, copy_dialog->y - 200/2,
517         500, 200, 500, 200, 1, 0, 1)
518 {
519         this->copy_dialog = copy_dialog;
520 }
521
522 AssetCopyWindow::~AssetCopyWindow()
523 {
524 }
525
526 void AssetCopyWindow::create_objects()
527 {
528         BC_Title *title;
529         int x = 10, y = 10, pad = 5;
530         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
531         y += title->get_h() + pad;
532         int text_w = get_w() - x - 10;
533         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
534         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
535         char *text = copy_dialog->text;
536         int len = strlen(text) + BCTEXTLEN;
537         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
538         file_list->create_objects();
539
540         add_subwindow(new BC_OKButton(this));
541         show_window();
542 }
543
544 int AssetCopyWindow::resize_event(int w, int h)
545 {
546         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
547         int text_w = w - fx - 10;
548         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
549         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
550         file_list->reposition_window(fx, fy, text_w, text_rows);
551         return 0;
552 }
553
554 AssetListPaste::AssetListPaste(MWindow *mwindow, AWindowGUI *gui)
555  : BC_MenuItem(_("Paste file list"))
556 {
557         this->mwindow = mwindow;
558         this->gui = gui;
559         paste_dialog = 0;
560 }
561 AssetListPaste::~AssetListPaste()
562 {
563         delete paste_dialog;
564 }
565
566 int AssetListPaste::handle_event()
567 {
568         if( !paste_dialog )
569                 paste_dialog = new AssetPasteDialog(this);
570         else
571                 paste_dialog->close_window();
572         int cur_x, cur_y;
573         gui->get_abs_cursor(cur_x, cur_y, 0);
574         paste_dialog->start(cur_x, cur_y);
575         return 1;
576 }
577
578 AssetPasteDialog::AssetPasteDialog(AssetListPaste *paste)
579  : BC_DialogThread()
580 {
581         this->paste = paste;
582         paste_window = 0;
583 }
584
585 AssetPasteDialog::~AssetPasteDialog()
586 {
587         close_window();
588 }
589
590 BC_Window* AssetPasteDialog::new_gui()
591 {
592         paste_window = new AssetPasteWindow(this);
593         paste_window->create_objects();
594         return paste_window;
595 }
596
597 void AssetPasteDialog::handle_done_event(int result)
598 {
599         if( result ) return;
600         const char *bp = paste_window->file_list->get_text(), *ep = bp+strlen(bp);
601         ArrayList<char*> path_list;
602         path_list.set_array_delete();
603
604         for( const char *cp=bp; cp<ep && *cp; ) {
605                 const char *dp = strchr(cp, '\n');
606                 if( !dp ) dp = ep;
607                 char path[BCTEXTLEN], *pp = path;
608                 int len = sizeof(path)-1;
609                 while( --len>0 && cp<dp ) *pp++ = *cp++;
610                 if( *cp ) ++cp;
611                 *pp = 0;
612                 if( !strlen(path) ) continue;
613                 path_list.append(cstrdup(path));
614         }
615         if( !path_list.size() ) return;
616
617         MWindow *mwindow = paste->mwindow;
618         mwindow->interrupt_indexes();
619         mwindow->gui->lock_window("AssetPasteDialog::handle_done_event");
620         result = mwindow->load_filenames(&path_list, LOADMODE_RESOURCESONLY, 0);
621         mwindow->gui->unlock_window();
622         path_list.remove_all_objects();
623         mwindow->save_backup();
624         mwindow->restart_brender();
625         mwindow->session->changes_made = 1;
626 }
627
628 void AssetPasteDialog::handle_close_event(int result)
629 {
630         paste_window = 0;
631 }
632
633 void AssetPasteDialog::start(int x, int y)
634 {
635         this->x = x;  this->y = y;
636         BC_DialogThread::start();
637 }
638
639 AssetPasteWindow::AssetPasteWindow(AssetPasteDialog *paste_dialog)
640  : BC_Window(_(PROGRAM_NAME ": Paste File List"),
641         paste_dialog->x - 500/2, paste_dialog->y - 200/2,
642         500, 200, 500, 200, 1, 0, 1)
643 {
644         this->paste_dialog = paste_dialog;
645 }
646
647 AssetPasteWindow::~AssetPasteWindow()
648 {
649 }
650
651 void AssetPasteWindow::create_objects()
652 {
653         BC_Title *title;
654         int x = 10, y = 10, pad = 5;
655         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
656         y += title->get_h() + pad;
657         int text_w = get_w() - x - 10;
658         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
659         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
660         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
661         file_list->create_objects();
662         add_subwindow(new BC_OKButton(this));
663         add_subwindow(new BC_CancelButton(this));
664         show_window();
665 }
666
667 int AssetPasteWindow::resize_event(int w, int h)
668 {
669         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
670         int text_w = w - fx - 10;
671         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
672         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
673         file_list->reposition_window(fx, fy, text_w, text_rows);
674         return 0;
675 }
676