clean up bclistbox, listbox shift drag fix, plugin info
[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(cur_x, cur_y);
149         if( mwindow->session->drag_assets->size() ) {
150                 AssetEdit *asset_edit = mwindow->awindow->get_asset_editor();
151                 asset_edit->edit_asset(
152                         mwindow->session->drag_assets->values[0], cur_x, cur_y);
153         }
154         else if( mwindow->session->drag_clips->size() ) {
155                 popup->gui->awindow->clip_edit->edit_clip(
156                         mwindow->session->drag_clips->values[0], cur_x, cur_y);
157         }
158         return 1;
159 }
160
161
162 AssetPopupBuildIndex::AssetPopupBuildIndex(MWindow *mwindow, AssetPopup *popup)
163  : BC_MenuItem(_("Rebuild index"))
164 {
165         this->mwindow = mwindow;
166         this->popup = popup;
167 }
168
169 AssetPopupBuildIndex::~AssetPopupBuildIndex()
170 {
171 }
172
173 int AssetPopupBuildIndex::handle_event()
174 {
175 //printf("AssetPopupBuildIndex::handle_event 1\n");
176         mwindow->rebuild_indices();
177         return 1;
178 }
179
180
181 AssetPopupSort::AssetPopupSort(MWindow *mwindow, AssetPopup *popup)
182  : BC_MenuItem(_("Sort items"))
183 {
184         this->mwindow = mwindow;
185         this->popup = popup;
186 }
187
188 AssetPopupSort::~AssetPopupSort()
189 {
190 }
191
192 int AssetPopupSort::handle_event()
193 {
194         mwindow->awindow->gui->sort_assets();
195         return 1;
196 }
197
198
199 AssetPopupView::AssetPopupView(MWindow *mwindow, AssetPopup *popup)
200  : BC_MenuItem(_("View"))
201 {
202         this->mwindow = mwindow;
203         this->popup = popup;
204 }
205
206 AssetPopupView::~AssetPopupView()
207 {
208 }
209
210 int AssetPopupView::handle_event()
211 {
212         VWindow *vwindow = mwindow->get_viewer(1, DEFAULT_VWINDOW);
213         vwindow->gui->lock_window("AssetPopupView::handle_event");
214
215         if( mwindow->session->drag_assets->total )
216                 vwindow->change_source(
217                         mwindow->session->drag_assets->values[0]);
218         else
219         if( mwindow->session->drag_clips->total )
220                 vwindow->change_source(
221                         mwindow->session->drag_clips->values[0]);
222
223         vwindow->gui->unlock_window();
224         return 1;
225 }
226
227
228 AssetPopupViewWindow::AssetPopupViewWindow(MWindow *mwindow, AssetPopup *popup)
229  : BC_MenuItem(_("View in new window"))
230 {
231         this->mwindow = mwindow;
232         this->popup = popup;
233 }
234
235 AssetPopupViewWindow::~AssetPopupViewWindow()
236 {
237 }
238
239 int AssetPopupViewWindow::handle_event()
240 {
241 // Find window with nothing
242         VWindow *vwindow = mwindow->get_viewer(1);
243
244 // TODO: create new vwindow or change current vwindow
245         vwindow->gui->lock_window("AssetPopupView::handle_event");
246
247         if( mwindow->session->drag_assets->total )
248                 vwindow->change_source(
249                         mwindow->session->drag_assets->values[0]);
250         else
251         if( mwindow->session->drag_clips->total )
252                 vwindow->change_source(
253                         mwindow->session->drag_clips->values[0]);
254
255         vwindow->gui->unlock_window();
256         return 1;
257 }
258
259
260 AssetPopupPaste::AssetPopupPaste(MWindow *mwindow, AssetPopup *popup)
261  : BC_MenuItem(_("Paste"))
262 {
263         this->mwindow = mwindow;
264         this->popup = popup;
265 }
266
267 AssetPopupPaste::~AssetPopupPaste()
268 {
269 }
270
271 int AssetPopupPaste::handle_event()
272 {
273         popup->paste_assets();
274         return 1;
275 }
276
277
278 AssetMatchSize::AssetMatchSize(MWindow *mwindow, AssetPopup *popup)
279  : BC_MenuItem(_("Match project size"))
280 {
281         this->mwindow = mwindow;
282         this->popup = popup;
283 }
284
285 int AssetMatchSize::handle_event()
286 {
287         popup->match_size();
288         return 1;
289 }
290
291 AssetMatchRate::AssetMatchRate(MWindow *mwindow, AssetPopup *popup)
292  : BC_MenuItem(_("Match frame rate"))
293 {
294         this->mwindow = mwindow;
295         this->popup = popup;
296 }
297
298 int AssetMatchRate::handle_event()
299 {
300         popup->match_rate();
301         return 1;
302 }
303
304 AssetMatchAll::AssetMatchAll(MWindow *mwindow, AssetPopup *popup)
305  : BC_MenuItem(_("Match all"))
306 {
307         this->mwindow = mwindow;
308         this->popup = popup;
309 }
310
311 int AssetMatchAll::handle_event()
312 {
313         popup->match_all();
314         return 1;
315 }
316
317
318 AssetPopupProjectRemove::AssetPopupProjectRemove(MWindow *mwindow, AssetPopup *popup)
319  : BC_MenuItem(_("Remove from project"))
320 {
321         this->mwindow = mwindow;
322         this->popup = popup;
323 }
324
325 AssetPopupProjectRemove::~AssetPopupProjectRemove()
326 {
327 }
328
329 int AssetPopupProjectRemove::handle_event()
330 {
331         mwindow->remove_assets_from_project(1, 1,
332                 mwindow->session->drag_assets,
333                 mwindow->session->drag_clips);
334         return 1;
335 }
336
337
338 AssetPopupDiskRemove::AssetPopupDiskRemove(MWindow *mwindow, AssetPopup *popup)
339  : BC_MenuItem(_("Remove from disk"))
340 {
341         this->mwindow = mwindow;
342         this->popup = popup;
343 }
344
345
346 AssetPopupDiskRemove::~AssetPopupDiskRemove()
347 {
348 }
349
350 int AssetPopupDiskRemove::handle_event()
351 {
352         mwindow->awindow->asset_remove->start();
353         return 1;
354 }
355
356
357 AssetListMenu::AssetListMenu(MWindow *mwindow, AWindowGUI *gui)
358  : BC_PopupMenu(0, 0, 0, "", 0)
359 {
360         this->mwindow = mwindow;
361         this->gui = gui;
362 }
363
364 AssetListMenu::~AssetListMenu()
365 {
366 }
367
368 void AssetListMenu::create_objects()
369 {
370         add_item(format = new AWindowListFormat(mwindow, gui));
371         add_item(new AWindowListSort(mwindow, gui));
372         add_item(new AssetListCopy(mwindow, gui));
373         add_item(new AssetListPaste(mwindow, gui));
374         update_titles();
375 }
376
377 void AssetListMenu::update_titles()
378 {
379         format->update();
380 }
381
382 AssetListCopy::AssetListCopy(MWindow *mwindow, AWindowGUI *gui)
383  : BC_MenuItem(_("Copy file list"))
384 {
385         this->mwindow = mwindow;
386         this->gui = gui;
387         copy_dialog = 0;
388 }
389 AssetListCopy::~AssetListCopy()
390 {
391         delete copy_dialog;
392 }
393
394 int AssetListCopy::handle_event()
395 {
396         int len = 0;
397         MWindowGUI *gui = mwindow->gui;
398         gui->lock_window("AssetListCopy::handle_event");
399         mwindow->awindow->gui->collect_assets();
400         int n = mwindow->session->drag_assets->total;
401         for( int i=0; i<n; ++i ) {
402                 Indexable *indexable = mwindow->session->drag_assets->values[i];
403                 const char *path = indexable->path;
404                 if( !*path ) continue;
405                 len += strlen(path) + 1;
406         }
407         char *text = new char[len+1], *cp = text;
408         for( int i=0; i<n; ++i ) {
409                 Indexable *indexable = mwindow->session->drag_assets->values[i];
410                 const char *path = indexable->path;
411                 if( !*path ) continue;
412                 cp += sprintf(cp, "%s\n", path);
413         }
414         *cp = 0;
415         int cur_x, cur_y;
416         gui->get_abs_cursor(cur_x, cur_y, 0);
417         gui->unlock_window(); 
418
419         if( n ) {
420                 if( !copy_dialog )
421                         copy_dialog = new AssetCopyDialog(this);
422                 copy_dialog->start(text, cur_x, cur_y);
423         }
424         else {
425                 eprintf(_("Nothing selected"));
426                 delete [] text;
427         }
428         return 1;
429 }
430
431 AssetCopyDialog::AssetCopyDialog(AssetListCopy *copy)
432  : BC_DialogThread()
433 {
434         this->copy = copy;
435         copy_window = 0;
436 }
437
438 void AssetCopyDialog::start(char *text, int x, int y)
439 {
440         close_window();
441         this->text = text;
442         this->x = x;  this->y = y;
443         BC_DialogThread::start();
444 }
445
446 AssetCopyDialog::~AssetCopyDialog()
447 {
448         close_window();
449 }
450
451 BC_Window* AssetCopyDialog::new_gui()
452 {
453         BC_DisplayInfo display_info;
454
455         copy_window = new AssetCopyWindow(this);
456         copy_window->create_objects();
457         return copy_window;
458 }
459
460 void AssetCopyDialog::handle_done_event(int result)
461 {
462         delete [] text;  text = 0;
463 }
464
465 void AssetCopyDialog::handle_close_event(int result)
466 {
467         copy_window = 0;
468 }
469
470
471 AssetCopyWindow::AssetCopyWindow(AssetCopyDialog *copy_dialog)
472  : BC_Window(_(PROGRAM_NAME ": Copy File List"),
473         copy_dialog->x - 500/2, copy_dialog->y - 200/2,
474         500, 200, 500, 200, 1, 0, 1)
475 {
476         this->copy_dialog = copy_dialog;
477 }
478
479 AssetCopyWindow::~AssetCopyWindow()
480 {
481 }
482
483 void AssetCopyWindow::create_objects()
484 {
485         BC_Title *title;
486         int x = 10, y = 10, pad = 5;
487         add_subwindow(title = new BC_Title(x, y, _("List of asset paths:")));
488         y += title->get_h() + pad;
489         int text_w = get_w() - x - 10;
490         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
491         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
492         char *text = copy_dialog->text;
493         int len = strlen(text) + BCTEXTLEN;
494         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, text, len);
495         file_list->create_objects();
496
497         add_subwindow(new BC_OKButton(this));
498         show_window();
499 }
500
501 int AssetCopyWindow::resize_event(int w, int h)
502 {
503         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
504         int text_w = w - fx - 10;
505         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
506         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
507         file_list->reposition_window(fx, fy, text_w, text_rows);
508         return 0;
509 }
510
511 AssetListPaste::AssetListPaste(MWindow *mwindow, AWindowGUI *gui)
512  : BC_MenuItem(_("Paste file list"))
513 {
514         this->mwindow = mwindow;
515         this->gui = gui;
516         paste_dialog = 0;
517 }
518 AssetListPaste::~AssetListPaste()
519 {
520         delete paste_dialog;
521 }
522
523 int AssetListPaste::handle_event()
524 {
525         if( !paste_dialog )
526                 paste_dialog = new AssetPasteDialog(this);
527         else
528                 paste_dialog->close_window();
529         int cur_x, cur_y;
530         gui->get_abs_cursor(cur_x, cur_y, 0);
531         paste_dialog->start(cur_x, cur_y);
532         return 1;
533 }
534
535 AssetPasteDialog::AssetPasteDialog(AssetListPaste *paste)
536  : BC_DialogThread()
537 {
538         this->paste = paste;
539         paste_window = 0;
540 }
541
542 AssetPasteDialog::~AssetPasteDialog()
543 {
544         close_window();
545 }
546
547 BC_Window* AssetPasteDialog::new_gui()
548 {
549         paste_window = new AssetPasteWindow(this);
550         paste_window->create_objects();
551         return paste_window;
552 }
553
554 void AssetPasteDialog::handle_done_event(int result)
555 {
556         if( result ) return;
557         const char *bp = paste_window->file_list->get_text(), *ep = bp+strlen(bp);
558         ArrayList<char*> path_list;
559         path_list.set_array_delete();
560
561         for( const char *cp=bp; cp<ep && *cp; ) {
562                 const char *dp = strchr(cp, '\n');
563                 if( !dp ) dp = ep;
564                 char path[BCTEXTLEN], *pp = path;
565                 int len = sizeof(path)-1;
566                 while( --len>0 && cp<dp ) *pp++ = *cp++;
567                 if( *cp ) ++cp;
568                 *pp = 0;
569                 if( !strlen(path) ) continue;
570                 path_list.append(cstrdup(path));
571         }
572         if( !path_list.size() ) return;
573
574         MWindow *mwindow = paste->mwindow;
575         mwindow->interrupt_indexes();
576         mwindow->gui->lock_window("AssetPasteDialog::handle_done_event");
577         result = mwindow->load_filenames(&path_list, LOADMODE_RESOURCESONLY, 0);
578         mwindow->gui->unlock_window();
579         path_list.remove_all_objects();
580         mwindow->save_backup();
581         mwindow->restart_brender();
582         mwindow->session->changes_made = 1;
583 }
584
585 void AssetPasteDialog::handle_close_event(int result)
586 {
587         paste_window = 0;
588 }
589
590 void AssetPasteDialog::start(int x, int y)
591 {
592         this->x = x;  this->y = y;
593         BC_DialogThread::start();
594 }
595
596 AssetPasteWindow::AssetPasteWindow(AssetPasteDialog *paste_dialog)
597  : BC_Window(_(PROGRAM_NAME ": Paste File List"),
598         paste_dialog->x - 500/2, paste_dialog->y - 200/2,
599         500, 200, 500, 200, 1, 0, 1)
600 {
601         this->paste_dialog = paste_dialog;
602 }
603
604 AssetPasteWindow::~AssetPasteWindow()
605 {
606 }
607
608 void AssetPasteWindow::create_objects()
609 {
610         BC_Title *title;
611         int x = 10, y = 10, pad = 5;
612         add_subwindow(title = new BC_Title(x, y, _("Enter list of asset paths:")));
613         y += title->get_h() + pad;
614         int text_w = get_w() - x - 10;
615         int text_h = get_h() - y - BC_OKButton::calculate_h() - pad;
616         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
617         file_list = new BC_ScrollTextBox(this, x, y, text_w, text_rows, (char*)0, 65536);
618         file_list->create_objects();
619         add_subwindow(new BC_OKButton(this));
620         add_subwindow(new BC_CancelButton(this));
621         show_window();
622 }
623
624 int AssetPasteWindow::resize_event(int w, int h)
625 {
626         int fx = file_list->get_x(), fy = file_list->get_y(), pad = 5;
627         int text_w = w - fx - 10;
628         int text_h = h - fy - BC_OKButton::calculate_h() - pad;
629         int text_rows = BC_TextBox::pixels_to_rows(this, MEDIUMFONT, text_h);
630         file_list->reposition_window(fx, fy, text_w, text_rows);
631         return 0;
632 }
633