add edit length update when open_edl changes media length, replace stack_warn with...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / editpopup.C
index 25931b5d42a8821443de286a97931fc9880b3278..da9b06ba4cb84d5b60c08acf376461e166ff1e8e 100644 (file)
@@ -29,6 +29,7 @@
 #include "edl.h"
 #include "edlsession.h"
 #include "file.h"
+#include "filexml.h"
 #include "keys.h"
 #include "language.h"
 #include "localsession.h"
@@ -49,6 +50,13 @@ EditPopup::EditPopup(MWindow *mwindow, MWindowGUI *gui)
 {
        this->mwindow = mwindow;
        this->gui = gui;
+
+       track = 0;
+       edit = 0;
+       plugin = 0;
+       pluginset = 0;
+       position = 0;
+       open_edl = 0;
 }
 
 EditPopup::~EditPopup()
@@ -57,452 +65,307 @@ EditPopup::~EditPopup()
 
 void EditPopup::create_objects()
 {
-       add_item(new EditAttachEffect(mwindow, this));
-       add_item(new EditMoveTrackUp(mwindow, this));
-       add_item(new EditMoveTrackDown(mwindow, this));
-       add_item(new EditPopupDeleteTrack(mwindow, this));
-       add_item(new EditPopupAddTrack(mwindow, this));
-       add_item(new EditPopupFindAsset(mwindow, this));
-       add_item(new EditPopupTitle(mwindow, this));
-       add_item(new EditPopupShow(mwindow, this));
-       resize_option = 0;
-       matchsize_option = 0;
-}
-
-int EditPopup::update(Track *track, Edit *edit)
+       add_item(open_edl = new EditPopupOpenEDL(mwindow, this));
+       add_item(new EditPopupClearSelect(mwindow, this));
+       add_item(new EditPopupCopy(mwindow, this));
+       add_item(new EditPopupCut(mwindow, this));
+       add_item(new EditPopupMute(mwindow, this));
+       add_item(new EditPopupCopyPack(mwindow, this));
+       add_item(new EditPopupCutPack(mwindow, this));
+       add_item(new EditPopupMutePack(mwindow, this));
+       add_item(new EditPopupPaste(mwindow, this));
+       add_item(new EditPopupOverwrite(mwindow, this));
+       add_item(new BC_MenuItem("-"));
+       add_item(new EditPopupOverwritePlugins(mwindow, this));
+       add_item(new EditCollectEffects(mwindow, this));
+       add_item(new EditPasteEffects(mwindow, this));
+       add_item(new EditPopupTimecode(mwindow, this));
+}
+
+int EditPopup::activate_menu(Track *track, Edit *edit,
+               PluginSet *pluginset, Plugin *plugin, double position)
 {
-       this->edit = edit;
        this->track = track;
-
-       if(track->data_type == TRACK_VIDEO && !resize_option)
-       {
-               add_item(resize_option = new EditPopupResize(mwindow, this));
-               add_item(matchsize_option = new EditPopupMatchSize(mwindow, this));
-       }
-       else
-       if(track->data_type == TRACK_AUDIO && resize_option)
-       {
-               del_item(resize_option);     resize_option = 0;
-               del_item(matchsize_option);  matchsize_option = 0;
-       }
-       return 0;
+       this->edit = edit;
+       this->pluginset = pluginset;
+       this->plugin = plugin;
+       this->position = position;
+       int enable = !edit ? 0 :
+               edit->nested_edl ? 1 :
+               !edit->asset ? 0 :
+               edit->asset->format == FILE_REF ? 1 : 0;
+       open_edl->set_enabled(enable);
+       return BC_PopupMenu::activate_menu();
 }
 
-
-EditAttachEffect::EditAttachEffect(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Attach effect..."))
+EditPopupOpenEDL::EditPopupOpenEDL(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Open EDL"))
 {
        this->mwindow = mwindow;
        this->popup = popup;
-       dialog_thread = new PluginDialogThread(mwindow);
-}
-
-EditAttachEffect::~EditAttachEffect()
-{
-       delete dialog_thread;
+       set_ctrl(1);
+       set_shift(1);
 }
 
-int EditAttachEffect::handle_event()
+int EditPopupOpenEDL::handle_event()
 {
-       dialog_thread->start_window(popup->track,
-               0, _(PROGRAM_NAME ": Attach Effect"),
-               0, popup->track->data_type);
+       Edit *edit = popup->edit;
+       if( !edit ) return 1;
+       EDL *edl = 0;
+       Indexable *idxbl = 0;
+       if( edit->asset && edit->asset->format == FILE_REF ) {
+               FileXML xml_file;
+               const char *filename = edit->asset->path;
+               if( xml_file.read_from_file(filename, 1) ) {
+                       eprintf(_("Error: unable to open:\n  %s"), filename);
+                       return 1;
+               }
+               edl = new EDL;
+               edl->create_objects();
+               if( edl->load_xml(&xml_file, LOAD_ALL) ) {
+                       eprintf(_("Error: unable to load:\n  %s"), filename);
+                       edl->remove_user();
+                       return 1;
+               }
+               idxbl = edit->asset;
+       }
+       else if( edit->nested_edl ) {
+               edl = edit->nested_edl;
+               edl->add_user();
+               idxbl = edl;
+       }
+       else {
+               char edit_title[BCTEXTLEN];
+               edit->get_title(edit_title);
+               eprintf(_("Edit is not EDL: %s"), edit_title);
+               return 1;
+       }
+       mwindow->stack_push(edl, idxbl);
        return 1;
 }
 
-
-EditMoveTrackUp::EditMoveTrackUp(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Move up"))
+EditPopupClearSelect::EditPopupClearSelect(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Clear Select"),_("Ctrl-Shift-A"),'A')
 {
        this->mwindow = mwindow;
        this->popup = popup;
+       set_ctrl(1);
+       set_shift(1);
 }
-EditMoveTrackUp::~EditMoveTrackUp()
-{
-}
-int EditMoveTrackUp::handle_event()
+
+int EditPopupClearSelect::handle_event()
 {
-       mwindow->move_track_up(popup->track);
+       mwindow->clear_select();
        return 1;
 }
 
-
-
-EditMoveTrackDown::EditMoveTrackDown(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Move down"))
+EditPopupCopy::EditPopupCopy(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Copy"),_("Ctrl-c"),'c')
 {
        this->mwindow = mwindow;
        this->popup = popup;
+       set_ctrl(1);
 }
-EditMoveTrackDown::~EditMoveTrackDown()
-{
-}
-int EditMoveTrackDown::handle_event()
+
+int EditPopupCopy::handle_event()
 {
-       mwindow->move_track_down(popup->track);
+       mwindow->selected_edits_to_clipboard(0);
        return 1;
 }
 
-
-EditPopupResize::EditPopupResize(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Resize track..."))
+EditPopupCopyPack::EditPopupCopyPack(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Copy pack"),_("Ctrl-Shift-C"),'C')
 {
        this->mwindow = mwindow;
        this->popup = popup;
-       dialog_thread = new ResizeTrackThread(mwindow);
-}
-EditPopupResize::~EditPopupResize()
-{
-       delete dialog_thread;
+       set_ctrl(1);
+       set_shift(1);
 }
 
-int EditPopupResize::handle_event()
+int EditPopupCopyPack::handle_event()
 {
-       dialog_thread->start_window(popup->track);
+       mwindow->selected_edits_to_clipboard(1);
        return 1;
 }
 
-
-EditPopupMatchSize::EditPopupMatchSize(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Match output size"))
+EditPopupCut::EditPopupCut(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Cut"),_("Ctrl-x"),'x')
 {
        this->mwindow = mwindow;
        this->popup = popup;
-}
-EditPopupMatchSize::~EditPopupMatchSize()
-{
+       set_ctrl(1);
 }
 
-int EditPopupMatchSize::handle_event()
+int EditPopupCut::handle_event()
 {
-       mwindow->match_output_size(popup->track);
+       mwindow->cut_selected_edits(1, 0);
        return 1;
 }
 
-
-EditPopupDeleteTrack::EditPopupDeleteTrack(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Delete track"))
+EditPopupCutPack::EditPopupCutPack(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Cut pack"),_("Ctrl-z"),'z')
 {
        this->mwindow = mwindow;
        this->popup = popup;
+       set_ctrl(1);
 }
-int EditPopupDeleteTrack::handle_event()
+
+int EditPopupCutPack::handle_event()
 {
-       mwindow->delete_track(popup->track);
+       mwindow->cut_selected_edits(1, 1);
        return 1;
 }
 
-
-EditPopupAddTrack::EditPopupAddTrack(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Add track"))
+EditPopupMute::EditPopupMute(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(C_("Mute"),_("Ctrl-m"),'m')
 {
        this->mwindow = mwindow;
        this->popup = popup;
+       set_ctrl(1);
 }
 
-int EditPopupAddTrack::handle_event()
+int EditPopupMute::handle_event()
 {
-       switch( popup->track->data_type ) {
-       case TRACK_AUDIO:
-               mwindow->add_audio_track_entry(1, popup->track);
-               break;
-       case TRACK_VIDEO:
-               mwindow->add_video_track_entry(popup->track);
-               break;
-       case TRACK_SUBTITLE:
-               mwindow->add_subttl_track_entry(popup->track);
-               break;
-       }
+       mwindow->cut_selected_edits(0, 0);
        return 1;
 }
 
-EditPopupFindAsset::EditPopupFindAsset(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Find in Resources"))
+EditPopupMutePack::EditPopupMutePack(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Mute pack"),_("Ctrl-Shift-M"),'M')
 {
        this->mwindow = mwindow;
        this->popup = popup;
+       set_ctrl(1);
+       set_shift(1);
 }
 
-int EditPopupFindAsset::handle_event()
+int EditPopupMutePack::handle_event()
 {
-       Edit *edit = popup->edit;
-       if( edit ) {
-               Indexable *idxbl = (Indexable *)edit->asset;
-               if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
-               if( idxbl ) {
-                       AWindowGUI *agui = mwindow->awindow->gui;
-                       agui->lock_window("EditPopupFindAsset::handle_event");
-                       AssetPicon *picon = 0;
-                       for( int i=0, n=agui->assets.size(); i<n; ++i ) {
-                               AssetPicon *ap = (AssetPicon *)agui->assets[i];
-                               int found = ap->indexable && ( idxbl == ap->indexable ||
-                                       !strcmp(idxbl->path, ap->indexable->path) );
-                               if( found && !picon ) picon = ap;
-                               ap->set_selected(found);
-                       }
-                       if( picon ) {
-                               int selected_folder = picon->indexable->folder_no;
-                               mwindow->edl->session->awindow_folder = selected_folder;
-                               for( int i=0,n=agui->folders.size(); i<n; ++i ) {
-                                       AssetPicon *folder_item = (AssetPicon *)agui->folders[i];
-                                       int selected = folder_item->foldernum == selected_folder ? 1 : 0;
-                                       folder_item->set_selected(selected);
-                               }
-                       }
-                       agui->unlock_window();
-                       agui->async_update_assets();
-               }
-       }
+       mwindow->cut_selected_edits(0, 1);
        return 1;
 }
 
-
-EditPopupTitle::EditPopupTitle(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("User title..."))
+EditPopupPaste::EditPopupPaste(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Paste"),_("Ctrl-v"),'v')
 {
        this->mwindow = mwindow;
        this->popup = popup;
-       dialog_thread = new EditTitleDialogThread(this);
-}
-
-EditPopupTitle::~EditPopupTitle()
-{
-       delete dialog_thread;
+       set_ctrl(1);
 }
 
-int EditPopupTitle::handle_event()
+int EditPopupPaste::handle_event()
 {
-       if( popup->edit ) {
-               dialog_thread->close_window();
-               int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
-               int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
-               dialog_thread->start(wx, wy);
+       mwindow->paste(popup->position, popup->track, 0, 0);
+       mwindow->edl->tracks->clear_selected_edits();
+       popup->gui->draw_overlays(1);
+       if( mwindow->session->current_operation == DROP_TARGETING ) {
+               mwindow->session->current_operation = NO_OPERATION;
+               popup->gui->update_cursor();
        }
        return 1;
 }
 
-void EditTitleDialogThread::start(int wx, int wy)
-{
-       this->wx = wx;  this->wy = wy;
-       BC_DialogThread::start();
-}
-
-EditTitleDialogThread::EditTitleDialogThread(EditPopupTitle *edit_title)
-{
-       this->edit_title = edit_title;
-       window = 0;
-}
-EditTitleDialogThread::~EditTitleDialogThread()
-{
-       close_window();
-}
-
-BC_Window* EditTitleDialogThread::new_gui()
-{
-       MWindow *mwindow = edit_title->mwindow;
-       EditPopup *popup = edit_title->popup;
-       window = new EditPopupTitleWindow(mwindow, popup, wx, wy);
-       window->create_objects();
-       return window;
-}
-
-void EditTitleDialogThread::handle_close_event(int result)
-{
-       window = 0;
-}
-
-void EditTitleDialogThread::handle_done_event(int result)
-{
-       if( result ) return;
-       MWindow *mwindow = edit_title->mwindow;
-       EditPopup *popup = edit_title->popup;
-       strcpy(popup->edit->user_title, window->title_text->get_text());
-       mwindow->gui->lock_window("EditTitleDialogThread::handle_done_event");
-       mwindow->gui->draw_canvas(1, 0);
-       mwindow->gui->flash_canvas(1);
-       mwindow->gui->unlock_window();
-}
-
-EditPopupTitleWindow::EditPopupTitleWindow(MWindow *mwindow,
-               EditPopup *popup, int wx, int wy)
- : BC_Window(_(PROGRAM_NAME ": Set edit title"), wx, wy,
-       300, 130, 300, 130, 0, 0, 1)
+EditPopupOverwrite::EditPopupOverwrite(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Overwrite"),_("Ctrl-b"),'b')
 {
        this->mwindow = mwindow;
        this->popup = popup;
-       strcpy(new_text, !popup->edit ? "" : popup->edit->user_title);
-}
-
-EditPopupTitleWindow::~EditPopupTitleWindow()
-{
-}
-
-void EditPopupTitleWindow::create_objects()
-{
-       lock_window("EditPopupTitleWindow::create_objects");
-       int x = 10, y = 10;
-       add_subwindow(new BC_Title(x, y, _("User title:")));
-       title_text = new EditPopupTitleText(this, mwindow, x+15, y+20, new_text);
-       add_subwindow(title_text);
-       add_tool(new BC_OKButton(this));
-       add_tool(new BC_CancelButton(this));
-
-
-       show_window();
-       flush();
-       unlock_window();
-}
-
-
-EditPopupTitleText::EditPopupTitleText(EditPopupTitleWindow *window,
-       MWindow *mwindow, int x, int y, const char *text)
- : BC_TextBox(x, y, 250, 1, text)
-{
-       this->window = window;
-       this->mwindow = mwindow;
+       set_ctrl(1);
 }
 
-EditPopupTitleText::~EditPopupTitleText()
+int EditPopupOverwrite::handle_event()
 {
-}
-
-int EditPopupTitleText::handle_event()
-{
-       if( get_keypress() == RETURN )
-               window->set_done(0);
+       mwindow->paste(popup->position, popup->track, 0, -1);
+       mwindow->edl->tracks->clear_selected_edits();
+       popup->gui->draw_overlays(1);
+       if( mwindow->session->current_operation == DROP_TARGETING ) {
+               mwindow->session->current_operation = NO_OPERATION;
+               popup->gui->update_cursor();
+       }
        return 1;
 }
 
-
-EditPopupShow::EditPopupShow(MWindow *mwindow, EditPopup *popup)
- : BC_MenuItem(_("Show edit"))
+EditPopupOverwritePlugins::EditPopupOverwritePlugins(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Overwrite Plugins"),_("Ctrl-Shift-P"),'P')
 {
        this->mwindow = mwindow;
        this->popup = popup;
-       dialog_thread = new EditShowDialogThread(this);
+       set_ctrl(1);
+       set_shift(1);
 }
 
-EditPopupShow::~EditPopupShow()
+int EditPopupOverwritePlugins::handle_event()
 {
-       delete dialog_thread;
-}
-
-int EditPopupShow::handle_event()
-{
-       if( popup->edit ) {
-               dialog_thread->close_window();
-               int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
-               int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
-               dialog_thread->start(wx, wy);
+       mwindow->paste_clipboard(popup->track, popup->position, 1, 0,
+                       mwindow->edl->session->labels_follow_edits,
+                       mwindow->edl->session->autos_follow_edits,
+                       mwindow->edl->session->plugins_follow_edits);
+       mwindow->edl->tracks->clear_selected_edits();
+       if( mwindow->session->current_operation == DROP_TARGETING ) {
+               mwindow->session->current_operation = NO_OPERATION;
+               popup->gui->update_cursor();
        }
        return 1;
 }
 
-void EditShowDialogThread::start(int wx, int wy)
-{
-       this->wx = wx;  this->wy = wy;
-       BC_DialogThread::start();
-}
 
-EditShowDialogThread::EditShowDialogThread(EditPopupShow *edit_show)
+EditCollectEffects::EditCollectEffects(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Collect Effects"))
 {
-       this->edit_show = edit_show;
-       window = 0;
-}
-EditShowDialogThread::~EditShowDialogThread()
-{
-       close_window();
-}
-
-BC_Window* EditShowDialogThread::new_gui()
-{
-       MWindow *mwindow = edit_show->mwindow;
-       EditPopup *popup = edit_show->popup;
-       window = new EditPopupShowWindow(mwindow, popup, wx, wy);
-       window->create_objects();
-       return window;
+       this->mwindow = mwindow;
+       this->popup = popup;
+       set_ctrl(1);
 }
 
-void EditShowDialogThread::handle_close_event(int result)
+int EditCollectEffects::handle_event()
 {
-       window = 0;
+       if( mwindow->session->current_operation == NO_OPERATION )
+               mwindow->collect_effects();
+       return 1;
 }
 
-EditPopupShowWindow::EditPopupShowWindow(MWindow *mwindow,
-               EditPopup *popup, int wx, int wy)
- : BC_Window(_(PROGRAM_NAME ": Show edit"), wx, wy,
-       300, 220, 300, 220, 0, 0, 1)
+EditPasteEffects::EditPasteEffects(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Paste Effects"))
 {
        this->mwindow = mwindow;
        this->popup = popup;
+       set_ctrl(1);
+       set_shift(1);
 }
 
-EditPopupShowWindow::~EditPopupShowWindow()
+int EditPasteEffects::handle_event()
 {
+       if( mwindow->session->current_operation == NO_OPERATION )
+               mwindow->paste_effects();
+       return 1;
 }
 
-void EditPopupShowWindow::create_objects()
+EditPopupTimecode::EditPopupTimecode(MWindow *mwindow, EditPopup *popup)
+ : BC_MenuItem(_("Timecode"),_("Ctrl-!"),'!')
 {
-       lock_window("EditPopupShowWindow::create_objects");
-       int x = 10, y = 10;
-       BC_Title *title;
-       char text[BCTEXTLEN];
-       Track *track = popup->track;
-       Edit *edit = popup->edit;
-       sprintf(text, _("Track %d:"), mwindow->edl->tracks->number_of(track)+1);
-       add_subwindow(title = new BC_Title(x, y, text));
-       int x1 = x + title->get_w() + 10;
-       int tw = get_w() - x1 - 20;
-       truncate_text(text, track->title, tw);
-       add_subwindow(new BC_Title(x1, y, text));
-       y += title->get_h() + 5;
-       sprintf(text, _("Edit %d:"), track->edits->number_of(edit)+1);
-       add_subwindow(title = new BC_Title(x, y, text));
-       char edit_title[BCTEXTLEN];
-       edit->get_title(edit_title);
-       truncate_text(text, edit_title, tw);
-       add_subwindow(new BC_Title(x1, y, text));
-       y += title->get_h() + 5;
-
-       EDLSession *session = mwindow->edl->session;
-       int time_format = session->time_format;
-       int sample_rate = session->sample_rate;
-       double frame_rate = session->frame_rate;
-       double frames_per_foot = session->frames_per_foot;
-
-       double startsource = track->from_units(edit->startsource);
-       double startproject = track->from_units(edit->startproject);
-       double length = track->from_units(edit->length);
-
-       char text_startsource[BCSTRLEN];
-       char text_startproject[BCSTRLEN];
-       char text_length[BCSTRLEN];
-       sprintf(text, _("StartSource: %s\nStartProject: %s\nLength: %s\n"),
-               Units::totext(text_startsource, startsource,
-                       time_format, sample_rate, frame_rate, frames_per_foot),
-               Units::totext(text_startproject, startproject,
-                       time_format, sample_rate, frame_rate, frames_per_foot),
-               Units::totext(text_length, length,
-                       time_format, sample_rate, frame_rate, frames_per_foot));
-       show_text = new EditPopupShowText(this, mwindow, x+15, y+10, text);
-       add_subwindow(show_text);
-       add_tool(new BC_OKButton(this));
-
-       show_window();
-       flush();
-       unlock_window();
-}
-
-
-EditPopupShowText::EditPopupShowText(EditPopupShowWindow *window,
-       MWindow *mwindow, int x, int y, const char *text)
- : BC_TextBox(x, y, 250, 4, text)
-{
-       this->window = window;
        this->mwindow = mwindow;
+       this->popup = popup;
+       set_ctrl(1);
 }
 
-EditPopupShowText::~EditPopupShowText()
+int EditPopupTimecode::handle_event()
 {
+       if( mwindow->session->current_operation != NO_OPERATION ) return 1;
+       Edit *edit = popup->edit;
+       if( !edit || !edit->asset ) return 1;
+       Asset *asset = edit->asset;
+       double timecode = asset->timecode != -2 ? asset->timecode :
+               FFMPEG::get_timecode(asset->path,
+                       edit->track->data_type, edit->channel,
+                       mwindow->edl->session->frame_rate);
+       asset->timecode = timecode;
+       if( timecode >= 0 ) {
+               int64_t pos = edit->startproject + edit->startsource;
+               double position = edit->track->from_units(pos);
+               mwindow->set_timecode_offset(timecode - position);
+       }
+       else
+               mwindow->set_timecode_offset(0);
+       return 1;
 }