label edit deadlock, build openexr cfg option, code cleanup
[goodguy/history.git] / cinelerra-5.1 / cinelerra / assetpopup.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "assetedit.h"
24 #include "assetpopup.h"
25 #include "assetremove.h"
26 #include "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
48
49 AssetPopup::AssetPopup(MWindow *mwindow, AWindowGUI *gui)
50  : BC_PopupMenu(0, 0, 0, "", 0)
51 {
52         this->mwindow = mwindow;
53         this->gui = gui;
54 }
55
56 AssetPopup::~AssetPopup()
57 {
58 }
59
60 void AssetPopup::create_objects()
61 {
62         BC_MenuItem *menu_item;
63         BC_SubMenu *submenu;
64         add_item(info = new AssetPopupInfo(mwindow, this));
65         add_item(format = new AWindowListFormat(mwindow, gui));
66         add_item(new AssetPopupSort(mwindow, this));
67         add_item(index = new AssetPopupBuildIndex(mwindow, this));
68         add_item(view = new AssetPopupView(mwindow, this));
69         add_item(view_window = new AssetPopupViewWindow(mwindow, this));
70         add_item(new AssetPopupPaste(mwindow, this));
71         add_item(menu_item = new BC_MenuItem(_("Match...")));
72         menu_item->add_submenu(submenu = new BC_SubMenu());
73         submenu->add_submenuitem(new AssetMatchSize(mwindow, this));
74         submenu->add_submenuitem(new AssetMatchRate(mwindow, this));
75         submenu->add_submenuitem(new AssetMatchAll(mwindow, this));
76         add_item(menu_item = new BC_MenuItem(_("Remove...")));
77         menu_item->add_submenu(submenu = new BC_SubMenu());
78         submenu->add_submenuitem(new AssetPopupProjectRemove(mwindow, this));
79         submenu->add_submenuitem(new AssetPopupDiskRemove(mwindow, this));
80 }
81
82 void AssetPopup::paste_assets()
83 {
84 // Collect items into the drag vectors for temporary storage
85         gui->lock_window("AssetPopup::paste_assets");
86         mwindow->gui->lock_window("AssetPopup::paste_assets");
87         mwindow->cwindow->gui->lock_window("AssetPopup::paste_assets");
88
89         gui->collect_assets();
90         mwindow->paste_assets(mwindow->edl->local_session->get_selectionstart(1),
91                 mwindow->edl->tracks->first,
92                 0);   // do not overwrite
93
94         gui->unlock_window();
95         mwindow->gui->unlock_window();
96         mwindow->cwindow->gui->unlock_window();
97 }
98
99 void AssetPopup::match_size()
100 {
101 // Collect items into the drag vectors for temporary storage
102         gui->collect_assets();
103         mwindow->gui->lock_window("AssetPopup::match_size");
104         mwindow->asset_to_size();
105         mwindow->gui->unlock_window();
106 }
107
108 void AssetPopup::match_rate()
109 {
110 // Collect items into the drag vectors for temporary storage
111         gui->collect_assets();
112         mwindow->gui->lock_window("AssetPopup::match_rate");
113         mwindow->asset_to_rate();
114         mwindow->gui->unlock_window();
115 }
116
117 void AssetPopup::match_all()
118 {
119 // Collect items into the drag vectors for temporary storage
120         gui->collect_assets();
121         mwindow->gui->lock_window("AssetPopup::match_rate");
122         mwindow->asset_to_all();
123         mwindow->gui->unlock_window();
124 }
125
126 int AssetPopup::update()
127 {
128         format->update();
129         gui->collect_assets();
130         return 0;
131 }
132
133
134 AssetPopupInfo::AssetPopupInfo(MWindow *mwindow, AssetPopup *popup)
135  : BC_MenuItem(_("Info..."))
136 {
137         this->mwindow = mwindow;
138         this->popup = popup;
139 }
140
141 AssetPopupInfo::~AssetPopupInfo()
142 {
143 }
144
145 int AssetPopupInfo::handle_event()
146 {
147         int cur_x, cur_y;
148         popup->gui->get_abs_cursor_xy(cur_x, cur_y, 0);
149         
150         if( mwindow->session->drag_assets->total ) {
151                 mwindow->awindow->asset_edit->edit_asset(
152                         mwindow->session->drag_assets->values[0], cur_x, cur_y);
153         }
154         else
155         if( mwindow->session->drag_clips->total ) {
156                 popup->gui->awindow->clip_edit->edit_clip(
157                         mwindow->session->drag_clips->values[0], cur_x, cur_y);
158         }
159         return 1;
160 }
161
162
163 AssetPopupBuildIndex::AssetPopupBuildIndex(MWindow *mwindow, AssetPopup *popup)
164  : BC_MenuItem(_("Rebuild index"))
165 {
166         this->mwindow = mwindow;
167         this->popup = popup;
168 }
169
170 AssetPopupBuildIndex::~AssetPopupBuildIndex()
171 {
172 }
173
174 int AssetPopupBuildIndex::handle_event()
175 {
176 //printf("AssetPopupBuildIndex::handle_event 1\n");
177         mwindow->rebuild_indices();
178         return 1;
179 }
180
181
182 AssetPopupSort::AssetPopupSort(MWindow *mwindow, AssetPopup *popup)
183  : BC_MenuItem(_("Sort items"))
184 {
185         this->mwindow = mwindow;
186         this->popup = popup;
187 }
188
189 AssetPopupSort::~AssetPopupSort()
190 {
191 }
192
193 int AssetPopupSort::handle_event()
194 {
195         mwindow->awindow->gui->sort_assets();
196         return 1;
197 }
198
199
200 AssetPopupView::AssetPopupView(MWindow *mwindow, AssetPopup *popup)
201  : BC_MenuItem(_("View"))
202 {
203         this->mwindow = mwindow;
204         this->popup = popup;
205 }
206
207 AssetPopupView::~AssetPopupView()
208 {
209 }
210
211 int AssetPopupView::handle_event()
212 {
213         VWindow *vwindow = mwindow->get_viewer(1, DEFAULT_VWINDOW);
214         vwindow->gui->lock_window("AssetPopupView::handle_event");
215
216         if( mwindow->session->drag_assets->total )
217                 vwindow->change_source(
218                         mwindow->session->drag_assets->values[0]);
219         else
220         if( mwindow->session->drag_clips->total )
221                 vwindow->change_source(
222                         mwindow->session->drag_clips->values[0]);
223
224         vwindow->gui->unlock_window();
225         return 1;
226 }
227
228
229 AssetPopupViewWindow::AssetPopupViewWindow(MWindow *mwindow, AssetPopup *popup)
230  : BC_MenuItem(_("View in new window"))
231 {
232         this->mwindow = mwindow;
233         this->popup = popup;
234 }
235
236 AssetPopupViewWindow::~AssetPopupViewWindow()
237 {
238 }
239
240 int AssetPopupViewWindow::handle_event()
241 {
242 // Find window with nothing
243         VWindow *vwindow = mwindow->get_viewer(1);
244
245 // TODO: create new vwindow or change current vwindow
246         vwindow->gui->lock_window("AssetPopupView::handle_event");
247
248         if( mwindow->session->drag_assets->total )
249                 vwindow->change_source(
250                         mwindow->session->drag_assets->values[0]);
251         else
252         if( mwindow->session->drag_clips->total )
253                 vwindow->change_source(
254                         mwindow->session->drag_clips->values[0]);
255
256         vwindow->gui->unlock_window();
257         return 1;
258 }
259
260
261 AssetPopupPaste::AssetPopupPaste(MWindow *mwindow, AssetPopup *popup)
262  : BC_MenuItem(_("Paste"))
263 {
264         this->mwindow = mwindow;
265         this->popup = popup;
266 }
267
268 AssetPopupPaste::~AssetPopupPaste()
269 {
270 }
271
272 int AssetPopupPaste::handle_event()
273 {
274         popup->paste_assets();
275         return 1;
276 }
277
278
279 AssetMatchSize::AssetMatchSize(MWindow *mwindow, AssetPopup *popup)
280  : BC_MenuItem(_("Match project size"))
281 {
282         this->mwindow = mwindow;
283         this->popup = popup;
284 }
285
286 int AssetMatchSize::handle_event()
287 {
288         popup->match_size();
289         return 1;
290 }
291
292 AssetMatchRate::AssetMatchRate(MWindow *mwindow, AssetPopup *popup)
293  : BC_MenuItem(_("Match frame rate"))
294 {
295         this->mwindow = mwindow;
296         this->popup = popup;
297 }
298
299 int AssetMatchRate::handle_event()
300 {
301         popup->match_rate();
302         return 1;
303 }
304
305 AssetMatchAll::AssetMatchAll(MWindow *mwindow, AssetPopup *popup)
306  : BC_MenuItem(_("Match all"))
307 {
308         this->mwindow = mwindow;
309         this->popup = popup;
310 }
311
312 int AssetMatchAll::handle_event()
313 {
314         popup->match_all();
315         return 1;
316 }
317
318
319 AssetPopupProjectRemove::AssetPopupProjectRemove(MWindow *mwindow, AssetPopup *popup)
320  : BC_MenuItem(_("Remove from project"))
321 {
322         this->mwindow = mwindow;
323         this->popup = popup;
324 }
325
326 AssetPopupProjectRemove::~AssetPopupProjectRemove()
327 {
328 }
329
330 int AssetPopupProjectRemove::handle_event()
331 {
332         mwindow->remove_assets_from_project(1, 1,
333                 mwindow->session->drag_assets,
334                 mwindow->session->drag_clips);
335         return 1;
336 }
337
338
339 AssetPopupDiskRemove::AssetPopupDiskRemove(MWindow *mwindow, AssetPopup *popup)
340  : BC_MenuItem(_("Remove from disk"))
341 {
342         this->mwindow = mwindow;
343         this->popup = popup;
344 }
345
346
347 AssetPopupDiskRemove::~AssetPopupDiskRemove()
348 {
349 }
350
351 int AssetPopupDiskRemove::handle_event()
352 {
353         mwindow->awindow->asset_remove->start();
354         return 1;
355 }
356
357
358 AssetListMenu::AssetListMenu(MWindow *mwindow, AWindowGUI *gui)
359  : BC_PopupMenu(0, 0, 0, "", 0)
360 {
361         this->mwindow = mwindow;
362         this->gui = gui;
363 }
364
365 AssetListMenu::~AssetListMenu()
366 {
367 }
368
369 void AssetListMenu::create_objects()
370 {
371         add_item(format = new AWindowListFormat(mwindow, gui));
372         add_item(new AWindowListSort(mwindow, gui));
373         add_item(new AssetListCopy(mwindow, gui));
374         add_item(new AssetListPaste(mwindow, gui));
375         update_titles();
376 }
377
378 void AssetListMenu::update_titles()
379 {
380         format->update();
381 }
382
383 AssetListCopy::AssetListCopy(MWindow *mwindow, AWindowGUI *gui)
384  : BC_MenuItem(_("Copy file list"))
385 {
386         this->mwindow = mwindow;
387         this->gui = gui;
388         copy_dialog = 0;
389 }
390 AssetListCopy::~AssetListCopy()
391 {
392         delete copy_dialog;
393 }
394
395 int AssetListCopy::handle_event()
396 {
397         int len = 0;
398         MWindowGUI *gui = mwindow->gui;
399         gui->lock_window("AssetListCopy::handle_event");
400         mwindow->awindow->gui->collect_assets();
401         int n = mwindow->session->drag_assets->total;
402         for( int i=0; i<n; ++i ) {
403                 Indexable *indexable = mwindow->session->drag_assets->values[i];
404                 const char *path = indexable->path;
405                 if( !*path ) continue;
406                 len += strlen(path) + 1;
407         }
408         char *text = new char[len+1], *cp = text;
409         for( int i=0; i<n; ++i ) {
410                 Indexable *indexable = mwindow->session->drag_assets->values[i];
411                 const char *path = indexable->path;
412                 if( !*path ) continue;
413                 cp += sprintf(cp, "%s\n", path);
414         }
415         *cp = 0;
416         int cur_x, cur_y;
417         gui->get_abs_cursor_xy(cur_x, cur_y, 0);
418         gui->unlock_window(); 
419
420         if( n ) {
421                 if( !copy_dialog )
422                         copy_dialog = new AssetCopyDialog(this);
423                 copy_dialog->start(text, cur_x, cur_y);
424         }
425         else {
426                 eprintf(_("Nothing selected"));
427                 delete [] text;
428         }
429         return 1;
430 }
431
432 AssetCopyDialog::AssetCopyDialog(AssetListCopy *copy)
433  : BC_DialogThread()
434 {
435         this->copy = copy;
436         copy_window = 0;
437 }
438
439 void AssetCopyDialog::start(char *text, int x, int y)
440 {
441         close_window();
442         this->text = text;
443         this->x = x;  this->y = y;
444         BC_DialogThread::start();
445 }
446
447 AssetCopyDialog::~AssetCopyDialog()
448 {
449         close_window();
450 }
451
452 BC_Window* AssetCopyDialog::new_gui()
453 {
454         BC_DisplayInfo display_info;
455
456         copy_window = new AssetCopyWindow(this);
457         copy_window->create_objects();
458         return copy_window;
459 }
460
461 void AssetCopyDialog::handle_done_event(int result)
462 {
463         delete [] text;  text = 0;
464 }
465
466 void AssetCopyDialog::handle_close_event(int result)
467 {
468         copy_window = 0;
469 }
470
471
472 AssetCopyWindow::AssetCopyWindow(AssetCopyDialog *copy_dialog)
473  : BC_Window(_(PROGRAM_NAME ": Copy File List"),
474         copy_dialog->x - 500/2, copy_dialog->y - 200/2,
475         500, 200, 500, 200, 0, 0, 1)
476 {
477         this->copy_dialog = copy_dialog;
478 }
479
480 AssetCopyWindow::~AssetCopyWindow()
481 {
482 }
483
484 void AssetCopyWindow::create_objects()
485 {
486         BC_Title *title;
487         int x = 10, y = 10, pad = 5;
488         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
489         y += title->get_h() + pad;
490         int text_w = get_w() - x - 10;
491         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
492         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
493         char *text = copy_dialog->text;
494         int len = strlen(text) + BCTEXTLEN;
495         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
496         file_list->create_objects();
497
498         add_subwindow(new BC_OKButton(this));
499         show_window();
500 }
501
502
503 AssetListPaste::AssetListPaste(MWindow *mwindow, AWindowGUI *gui)
504  : BC_MenuItem(_("Paste file list"))
505 {
506         this->mwindow = mwindow;
507         this->gui = gui;
508         paste_dialog = 0;
509 }
510 AssetListPaste::~AssetListPaste()
511 {
512         delete paste_dialog;
513 }
514
515 int AssetListPaste::handle_event()
516 {
517         if( !paste_dialog )
518                 paste_dialog->close_window();
519         else
520                 paste_dialog = new AssetPasteDialog(this);
521         int cur_x, cur_y;
522         gui->get_abs_cursor_xy(cur_x, cur_y, 0);
523         paste_dialog->start(cur_x, cur_y);
524         return 1;
525 }
526
527 AssetPasteDialog::AssetPasteDialog(AssetListPaste *paste)
528  : BC_DialogThread()
529 {
530         this->paste = paste;
531         paste_window = 0;
532 }
533
534 AssetPasteDialog::~AssetPasteDialog()
535 {
536         close_window();
537 }
538
539 BC_Window* AssetPasteDialog::new_gui()
540 {
541         paste_window = new AssetPasteWindow(this);
542         paste_window->create_objects();
543         return paste_window;
544 }
545
546 void AssetPasteDialog::handle_done_event(int result)
547 {
548         if( result ) return;
549         const char *bp = paste_window->file_list->get_text(), *ep = bp+strlen(bp);
550         ArrayList<char*> path_list;
551         path_list.set_array_delete();
552
553         for( const char *cp=bp; cp<ep && *cp; ) {
554                 const char *dp = strchr(cp, '\n');
555                 if( !dp ) dp = ep;
556                 char path[BCTEXTLEN], *pp = path;
557                 int len = sizeof(path)-1;
558                 while( --len>0 && cp<dp ) *pp++ = *cp++;
559                 if( *cp ) ++cp;
560                 *pp = 0;
561                 if( !strlen(path) ) continue;
562                 path_list.append(cstrdup(path));
563         }
564         if( !path_list.size() ) return;
565
566         MWindow *mwindow = paste->mwindow;
567         mwindow->interrupt_indexes();
568         mwindow->gui->lock_window("AssetPasteDialog::handle_done_event");
569         result = mwindow->load_filenames(&path_list, LOADMODE_RESOURCESONLY, 0);
570         mwindow->gui->unlock_window();
571         path_list.remove_all_objects();
572         mwindow->save_backup();
573         mwindow->restart_brender();
574         mwindow->session->changes_made = 1;
575 }
576
577 void AssetPasteDialog::handle_close_event(int result)
578 {
579         paste_window = 0;
580 }
581
582 void AssetPasteDialog::start(int x, int y)
583 {
584         this->x = x;  this->y = y;
585         BC_DialogThread::start();
586 }
587
588 AssetPasteWindow::AssetPasteWindow(AssetPasteDialog *paste_dialog)
589  : BC_Window(_(PROGRAM_NAME ": Paste File List"),
590         paste_dialog->x - 500/2, paste_dialog->y - 200/2,
591         500, 200, 500, 200, 0, 0, 1)
592 {
593         this->paste_dialog = paste_dialog;
594 }
595
596 AssetPasteWindow::~AssetPasteWindow()
597 {
598 }
599
600 void AssetPasteWindow::create_objects()
601 {
602         BC_Title *title;
603         int x = 10, y = 10, pad = 5;
604         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
605         y += title->get_h() + pad;
606         int text_w = get_w() - x - 10;
607         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
608         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
609         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
610         file_list->create_objects();
611         add_subwindow(new BC_OKButton(this));
612         add_subwindow(new BC_CancelButton(this));
613         show_window();
614 }
615