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