add missing GPL information in guicast program files
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / trackpopup.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "assets.h"
24 #include "awindow.h"
25 #include "awindowgui.h"
26 #include "edit.h"
27 #include "edits.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "file.h"
31 #include "keys.h"
32 #include "language.h"
33 #include "localsession.h"
34 #include "mainerror.h"
35 #include "mainsession.h"
36 #include "mwindow.h"
37 #include "mwindowgui.h"
38 #include "plugindialog.h"
39 #include "resizetrackthread.h"
40 #include "theme.h"
41 #include "track.h"
42 #include "tracks.h"
43 #include "trackpopup.h"
44 #include "trackcanvas.h"
45
46 #include <string.h>
47
48 TrackPopup::TrackPopup(MWindow *mwindow, MWindowGUI *gui)
49  : BC_PopupMenu(0, 0, 0, "", 0)
50 {
51         this->mwindow = mwindow;
52         this->gui = gui;
53         track = 0;
54         edit = 0;
55         pluginset = 0;
56         plugin = 0;
57         position = 0;
58 }
59
60 TrackPopup::~TrackPopup()
61 {
62 }
63
64 void TrackPopup::create_objects()
65 {
66         add_item(new TrackAttachEffect(mwindow, this));
67         add_item(new TrackMoveUp(mwindow, this));
68         add_item(new TrackMoveDown(mwindow, this));
69         add_item(new TrackRollUp(mwindow, this));
70         add_item(new TrackRollDown(mwindow, this));
71         add_item(new TrackPopupDeleteTrack(mwindow, this));
72         add_item(new TrackPopupAddTrack(mwindow, this));
73         add_item(new TrackPopupFindAsset(mwindow, this));
74         add_item(new TrackPopupShow(mwindow, this));
75         add_item(new TrackPopupUserTitle(mwindow, this));
76         add_item(new TrackPopupTitleColor(mwindow, this));
77         resize_option = 0;
78         matchsize_option = 0;
79 }
80
81 int TrackPopup::activate_menu(Track *track, Edit *edit,
82                 PluginSet *pluginset, Plugin *plugin, double position)
83 {
84         this->track = track;
85         this->edit = edit;
86         this->pluginset = pluginset;
87         this->plugin = plugin;
88         this->position = position;
89
90         if( track->data_type == TRACK_VIDEO && !resize_option ) {
91                 add_item(resize_option = new TrackPopupResize(mwindow, this));
92                 add_item(matchsize_option = new TrackPopupMatchSize(mwindow, this));
93         }
94         else if( track->data_type == TRACK_AUDIO && resize_option ) {
95                 del_item(resize_option);     resize_option = 0;
96                 del_item(matchsize_option);  matchsize_option = 0;
97         }
98
99         return BC_PopupMenu::activate_menu();
100 }
101
102
103 TrackAttachEffect::TrackAttachEffect(MWindow *mwindow, TrackPopup *popup)
104  : BC_MenuItem(_("Attach effect..."))
105 {
106         this->mwindow = mwindow;
107         this->popup = popup;
108         dialog_thread = new PluginDialogThread(mwindow);
109 }
110
111 TrackAttachEffect::~TrackAttachEffect()
112 {
113         delete dialog_thread;
114 }
115
116 int TrackAttachEffect::handle_event()
117 {
118         dialog_thread->start_window(popup->track,
119                 0, _(PROGRAM_NAME ": Attach Effect"),
120                 0, popup->track->data_type);
121         return 1;
122 }
123
124
125 TrackMoveUp::TrackMoveUp(MWindow *mwindow, TrackPopup *popup)
126  : BC_MenuItem(_("Move up"))
127 {
128         this->mwindow = mwindow;
129         this->popup = popup;
130 }
131 TrackMoveUp::~TrackMoveUp()
132 {
133 }
134 int TrackMoveUp::handle_event()
135 {
136         mwindow->move_track_up(popup->track);
137         return 1;
138 }
139
140 TrackMoveDown::TrackMoveDown(MWindow *mwindow, TrackPopup *popup)
141  : BC_MenuItem(_("Move down"))
142 {
143         this->mwindow = mwindow;
144         this->popup = popup;
145 }
146 TrackMoveDown::~TrackMoveDown()
147 {
148 }
149 int TrackMoveDown::handle_event()
150 {
151         mwindow->move_track_down(popup->track);
152         return 1;
153 }
154
155
156 TrackRollUp::TrackRollUp(MWindow *mwindow, TrackPopup *popup)
157  : BC_MenuItem(_("Roll up"))
158 {
159         this->mwindow = mwindow;
160         this->popup = popup;
161 }
162 TrackRollUp::~TrackRollUp()
163 {
164 }
165 int TrackRollUp::handle_event()
166 {
167         mwindow->roll_track_up(popup->track);
168         return 1;
169 }
170
171 TrackRollDown::TrackRollDown(MWindow *mwindow, TrackPopup *popup)
172  : BC_MenuItem(_("Roll down"))
173 {
174         this->mwindow = mwindow;
175         this->popup = popup;
176 }
177 TrackRollDown::~TrackRollDown()
178 {
179 }
180 int TrackRollDown::handle_event()
181 {
182         mwindow->roll_track_down(popup->track);
183         return 1;
184 }
185
186
187 TrackPopupResize::TrackPopupResize(MWindow *mwindow, TrackPopup *popup)
188  : BC_MenuItem(_("Resize track..."))
189 {
190         this->mwindow = mwindow;
191         this->popup = popup;
192         dialog_thread = new ResizeTrackThread(mwindow);
193 }
194 TrackPopupResize::~TrackPopupResize()
195 {
196         delete dialog_thread;
197 }
198
199 int TrackPopupResize::handle_event()
200 {
201         dialog_thread->start_window(popup->track);
202         return 1;
203 }
204
205
206 TrackPopupMatchSize::TrackPopupMatchSize(MWindow *mwindow, TrackPopup *popup)
207  : BC_MenuItem(_("Match output size"))
208 {
209         this->mwindow = mwindow;
210         this->popup = popup;
211 }
212 TrackPopupMatchSize::~TrackPopupMatchSize()
213 {
214 }
215
216 int TrackPopupMatchSize::handle_event()
217 {
218         mwindow->match_output_size(popup->track);
219         return 1;
220 }
221
222
223 TrackPopupDeleteTrack::TrackPopupDeleteTrack(MWindow *mwindow, TrackPopup *popup)
224  : BC_MenuItem(_("Delete track"))
225 {
226         this->mwindow = mwindow;
227         this->popup = popup;
228 }
229 int TrackPopupDeleteTrack::handle_event()
230 {
231         mwindow->delete_track(popup->track);
232         return 1;
233 }
234
235
236 TrackPopupAddTrack::TrackPopupAddTrack(MWindow *mwindow, TrackPopup *popup)
237  : BC_MenuItem(_("Add track"))
238 {
239         this->mwindow = mwindow;
240         this->popup = popup;
241 }
242
243 int TrackPopupAddTrack::handle_event()
244 {
245         switch( popup->track->data_type ) {
246         case TRACK_AUDIO:
247                 mwindow->add_audio_track_entry(1, popup->track);
248                 break;
249         case TRACK_VIDEO:
250                 mwindow->add_video_track_entry(1, popup->track);
251                 break;
252         case TRACK_SUBTITLE:
253                 mwindow->add_subttl_track_entry(1, popup->track);
254                 break;
255         }
256         return 1;
257 }
258
259 TrackPopupFindAsset::TrackPopupFindAsset(MWindow *mwindow, TrackPopup *popup)
260  : BC_MenuItem(_("Find in Resources"))
261 {
262         this->mwindow = mwindow;
263         this->popup = popup;
264 }
265
266 int TrackPopupFindAsset::handle_event()
267 {
268         Edit *edit = popup->edit;
269         if( edit ) {
270                 Indexable *idxbl = (Indexable *)edit->asset;
271                 if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
272                 if( idxbl ) {
273                         AWindowGUI *agui = mwindow->awindow->gui;
274                         agui->lock_window("TrackPopupFindAsset::handle_event");
275                         AssetPicon *picon = 0;
276                         for( int i=0, n=agui->assets.size(); i<n; ++i ) {
277                                 AssetPicon *ap = (AssetPicon *)agui->assets[i];
278                                 int found = ap->indexable && ( idxbl == ap->indexable ||
279                                         !strcmp(idxbl->path, ap->indexable->path) );
280                                 if( found && !picon ) picon = ap;
281                                 ap->set_selected(found);
282                         }
283                         if( picon ) {
284                                 int selected_folder = picon->indexable->folder_no;
285                                 mwindow->edl->session->awindow_folder = selected_folder;
286                                 for( int i=0,n=agui->folders.size(); i<n; ++i ) {
287                                         AssetPicon *folder_item = (AssetPicon *)agui->folders[i];
288                                         int selected = folder_item->foldernum == selected_folder ? 1 : 0;
289                                         folder_item->set_selected(selected);
290                                 }
291                         }
292                         agui->unlock_window();
293                         agui->async_update_assets();
294                 }
295         }
296         return 1;
297 }
298
299
300 TrackPopupUserTitle::TrackPopupUserTitle(MWindow *mwindow, TrackPopup *popup)
301  : BC_MenuItem(_("User title..."))
302 {
303         this->mwindow = mwindow;
304         this->popup = popup;
305         dialog_thread = new TrackUserTitleDialogThread(this);
306 }
307
308 TrackPopupUserTitle::~TrackPopupUserTitle()
309 {
310         delete dialog_thread;
311 }
312
313 #define TTW_W xS(300)
314 #define TTW_H yS(130)
315
316 int TrackPopupUserTitle::handle_event()
317 {
318         if( popup->edit ) {
319                 dialog_thread->close_window();
320                 int wx = mwindow->gui->get_abs_cursor_x(0) - TTW_W / 2;
321                 int wy = mwindow->gui->get_abs_cursor_y(0) - TTW_H / 2;
322                 dialog_thread->start(wx, wy);
323         }
324         return 1;
325 }
326
327 void TrackUserTitleDialogThread::start(int wx, int wy)
328 {
329         this->wx = wx;  this->wy = wy;
330         BC_DialogThread::start();
331 }
332
333 TrackUserTitleDialogThread::TrackUserTitleDialogThread(TrackPopupUserTitle *edit_title)
334 {
335         this->edit_title = edit_title;
336         window = 0;
337 }
338 TrackUserTitleDialogThread::~TrackUserTitleDialogThread()
339 {
340         close_window();
341 }
342
343 BC_Window* TrackUserTitleDialogThread::new_gui()
344 {
345         MWindow *mwindow = edit_title->mwindow;
346         TrackPopup *popup = edit_title->popup;
347         window = new TrackPopupUserTitleWindow(mwindow, popup, wx, wy);
348         window->create_objects();
349         return window;
350 }
351
352 void TrackUserTitleDialogThread::handle_close_event(int result)
353 {
354         window = 0;
355 }
356
357 void TrackUserTitleDialogThread::handle_done_event(int result)
358 {
359         if( result ) return;
360         MWindow *mwindow = edit_title->mwindow;
361         TrackPopup *popup = edit_title->popup;
362         EDL *edl = mwindow->edl;
363         const char *text = window->title_text->get_text();
364         int count = 0;
365         for( Track *track=edl->tracks->first; track; track=track->next ) {
366                 if( !track->is_armed() ) continue;
367                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
368                         if( !edit->is_selected ) continue;
369                         strcpy(edit->user_title, text);
370                         ++count;
371                 }
372         }
373         if( count )
374                 edl->tracks->clear_selected_edits();
375         else if( popup->edit ) {
376                 strcpy(popup->edit->user_title, text);
377         }
378         mwindow->gui->lock_window("TrackUserTitleDialogThread::handle_done_event");
379         mwindow->gui->draw_canvas(1, 0);
380         mwindow->gui->flash_canvas(1);
381         mwindow->gui->unlock_window();
382 }
383
384 TrackPopupUserTitleWindow::TrackPopupUserTitleWindow(MWindow *mwindow,
385                 TrackPopup *popup, int wx, int wy)
386  : BC_Window(_(PROGRAM_NAME ": Set edit title"), wx, wy,
387         TTW_W, TTW_H, TTW_W, TTW_H, 0, 0, 1)
388 {
389         this->mwindow = mwindow;
390         this->popup = popup;
391         strcpy(new_text, !popup->edit ? "" : popup->edit->user_title);
392 // *** CONTEXT_HELP ***
393         context_help_set_keyword("Track Popup Menu");
394 }
395
396 TrackPopupUserTitleWindow::~TrackPopupUserTitleWindow()
397 {
398 }
399
400 void TrackPopupUserTitleWindow::create_objects()
401 {
402         lock_window("TrackPopupUserTitleWindow::create_objects");
403         int x = xS(10), y = yS(10), x1;
404         BC_Title *title = new BC_Title(x1=x, y, _("User title:"));
405         add_subwindow(title);  x1 += title->get_w() + xS(10);
406         title_text = new TrackPopupUserTitleText(this, mwindow, x1, y, new_text);
407         add_subwindow(title_text);
408
409         add_tool(new BC_OKButton(this));
410         add_tool(new BC_CancelButton(this));
411
412
413         show_window();
414         flush();
415         unlock_window();
416 }
417
418
419 TrackPopupUserTitleText::TrackPopupUserTitleText(TrackPopupUserTitleWindow *window,
420         MWindow *mwindow, int x, int y, const char *text)
421  : BC_TextBox(x, y, window->get_w()-x-xS(15), 1, text)
422 {
423         this->window = window;
424         this->mwindow = mwindow;
425 }
426
427 TrackPopupUserTitleText::~TrackPopupUserTitleText()
428 {
429 }
430
431 int TrackPopupUserTitleText::handle_event()
432 {
433         if( get_keypress() == RETURN )
434                 window->set_done(0);
435         return 1;
436 }
437
438
439 TrackPopupTitleColor::TrackPopupTitleColor(MWindow *mwindow, TrackPopup *popup)
440  : BC_MenuItem(_("Bar Color..."))
441 {
442         this->mwindow = mwindow;
443         this->popup = popup;
444         color_picker = 0;
445 }
446 TrackPopupTitleColor::~TrackPopupTitleColor()
447 {
448         delete color_picker;
449 }
450
451 int TrackPopupTitleColor::handle_event()
452 {
453         if( popup->edit ) {
454                 int color = popup->mwindow->get_title_color(popup->edit);
455                 if( !color ) color = popup->mwindow->theme->get_color_title_bg();
456                 delete color_picker;
457                 color_picker = new TrackTitleColorPicker(popup, color);
458                 int alpha = (~color>>24) & 0xff;
459                 color_picker->start_window(color & 0xffffff, alpha, 1);
460         }
461         return 1;
462 }
463
464 TrackTitleColorDefault::TrackTitleColorDefault(
465         TrackTitleColorPicker *color_picker, int x, int y)
466  : BC_GenericButton(x, y, _("default"))
467 {
468         this->color_picker = color_picker;
469 }
470
471 int TrackTitleColorDefault::handle_event()
472 {
473         const unsigned color = 0, alpha = 0xff;
474         color_picker->color = color | (~alpha << 24);
475         color_picker->update_gui(color, alpha);
476         return 1;
477 }
478
479 TrackTitleColorPicker::TrackTitleColorPicker(TrackPopup *popup, int color)
480  : ColorPicker(1, _("Bar Color"))
481 {
482         this->popup = popup;
483         this->color = color;
484 }
485 TrackTitleColorPicker::~TrackTitleColorPicker()
486 {
487 }
488 void TrackTitleColorPicker::create_objects(ColorWindow *gui)
489 {
490         gui->lock_window("TrackTitleColorPicker::create_objects");
491         int y = gui->get_h() - BC_CancelButton::calculate_h() + yS(10);
492         int x = gui->get_w() - BC_CancelButton::calculate_w() - xS(10);
493         x -= BC_GenericButton::calculate_w(gui, _("default")) + xS(15);
494         gui->add_subwindow(new TrackTitleColorDefault(this, x, y));
495         gui->unlock_window();
496 }
497
498 int TrackTitleColorPicker::handle_new_color(int color, int alpha)
499 {
500         this->color = color | (~alpha << 24);
501         return 1;
502 }
503
504 void TrackTitleColorPicker::handle_done_event(int result)
505 {
506         if( !result ) {
507                 EDL *edl = popup->mwindow->edl;
508                 int count = 0;
509                 for( Track *track=edl->tracks->first; track; track=track->next ) {
510                         if( !track->is_armed() ) continue;
511                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
512                                 if( !edit->is_selected ) continue;
513                                 edit->color = color;
514                                 ++count;
515                         }
516                 }
517                 if( count )
518                         edl->tracks->clear_selected_edits();
519                 else
520                         popup->edit->color = color;
521         }
522         MWindowGUI *mwindow_gui = popup->mwindow->gui;
523         mwindow_gui->lock_window("GWindowColorUpdate::run");
524         mwindow_gui->draw_trackmovement();
525         mwindow_gui->unlock_window();
526 }
527
528
529 #define TPW_W xS(300)
530 #define TPW_H yS(220)
531
532 TrackPopupShow::TrackPopupShow(MWindow *mwindow, TrackPopup *popup)
533  : BC_MenuItem(_("Show edit"))
534 {
535         this->mwindow = mwindow;
536         this->popup = popup;
537         dialog_thread = new TrackShowDialogThread(this);
538 }
539
540 TrackPopupShow::~TrackPopupShow()
541 {
542         delete dialog_thread;
543 }
544
545 int TrackPopupShow::handle_event()
546 {
547         if( popup->edit ) {
548                 dialog_thread->close_window();
549                 int wx = mwindow->gui->get_abs_cursor_x(0) - TPW_W / 2;
550                 int wy = mwindow->gui->get_abs_cursor_y(0) - TPW_H / 2;
551                 dialog_thread->start(wx, wy);
552         }
553         return 1;
554 }
555
556 void TrackShowDialogThread::start(int wx, int wy)
557 {
558         this->wx = wx;  this->wy = wy;
559         BC_DialogThread::start();
560 }
561
562 TrackShowDialogThread::TrackShowDialogThread(TrackPopupShow *edit_show)
563 {
564         this->edit_show = edit_show;
565         window = 0;
566 }
567 TrackShowDialogThread::~TrackShowDialogThread()
568 {
569         close_window();
570 }
571
572 BC_Window* TrackShowDialogThread::new_gui()
573 {
574         MWindow *mwindow = edit_show->mwindow;
575         TrackPopup *popup = edit_show->popup;
576         window = new TrackPopupShowWindow(mwindow, popup, wx, wy);
577         window->create_objects();
578         return window;
579 }
580
581 void TrackShowDialogThread::handle_close_event(int result)
582 {
583         window = 0;
584 }
585
586 TrackPopupShowWindow::TrackPopupShowWindow(MWindow *mwindow,
587                 TrackPopup *popup, int wx, int wy)
588  : BC_Window(_(PROGRAM_NAME ": Show edit"), wx, wy,
589         TPW_W, TPW_H, TPW_W, TPW_H, 0, 0, 1)
590 {
591         this->mwindow = mwindow;
592         this->popup = popup;
593 // *** CONTEXT_HELP ***
594         context_help_set_keyword("Track Popup Menu");
595 }
596
597 TrackPopupShowWindow::~TrackPopupShowWindow()
598 {
599 }
600
601 void TrackPopupShowWindow::create_objects()
602 {
603         lock_window("TrackPopupShowWindow::create_objects");
604         int x = xS(10), y = yS(10);
605         BC_Title *title;
606         char text[BCTEXTLEN];
607         Edit *edit = popup->edit;
608         Track *track = edit->track;
609         sprintf(text, _("Track %d:"), mwindow->edl->tracks->number_of(track)+1);
610         add_subwindow(title = new BC_Title(x, y, text));
611         int x1 = x + title->get_w() + xS(10);
612         int tw = get_w() - x1 - xS(20);
613         char *track_title = get_truncated_text(MEDIUMFONT, track->title, tw);
614         add_subwindow(new BC_Title(x1, y, track_title));
615         delete [] track_title;
616         y += title->get_h() + 5;
617         sprintf(text, _("Edit %d:"), track->edits->number_of(edit)+1);
618         add_subwindow(title = new BC_Title(x, y, text));
619         edit->get_title(text);
620         char *edit_title = get_truncated_text(MEDIUMFONT, text, tw);
621         add_subwindow(new BC_Title(x1, y, edit_title));
622         delete [] edit_title;
623         y += title->get_h() + 5;
624
625         EDLSession *session = mwindow->edl->session;
626         int time_format = session->time_format;
627         int sample_rate = session->sample_rate;
628         double frame_rate = session->frame_rate;
629         double frames_per_foot = session->frames_per_foot;
630
631         double startsource = track->from_units(edit->startsource);
632         double startproject = track->from_units(edit->startproject);
633         double length = track->from_units(edit->length);
634
635         char text_startsource[BCSTRLEN];
636         char text_startproject[BCSTRLEN];
637         char text_length[BCSTRLEN];
638         sprintf(text, _("StartSource: %s\nStartProject: %s\nLength: %s\n"),
639                 Units::totext(text_startsource, startsource,
640                         time_format, sample_rate, frame_rate, frames_per_foot),
641                 Units::totext(text_startproject, startproject,
642                         time_format, sample_rate, frame_rate, frames_per_foot),
643                 Units::totext(text_length, length,
644                         time_format, sample_rate, frame_rate, frames_per_foot));
645         show_text = new TrackPopupShowText(this, mwindow, x+xS(15), y+yS(10), text);
646         add_subwindow(show_text);
647         add_tool(new BC_OKButton(this));
648
649         show_window();
650         flush();
651         unlock_window();
652 }
653
654
655 TrackPopupShowText::TrackPopupShowText(TrackPopupShowWindow *window,
656         MWindow *mwindow, int x, int y, const char *text)
657  : BC_TextBox(x, y, xS(250), 4, text)
658 {
659         this->window = window;
660         this->mwindow = mwindow;
661 }
662
663 TrackPopupShowText::~TrackPopupShowText()
664 {
665 }
666