copy/paste behavior tweaks
[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 "track.h"
42 #include "tracks.h"
43 #include "trackcanvas.h"
44
45 #include <string.h>
46
47 EditPopup::EditPopup(MWindow *mwindow, MWindowGUI *gui)
48  : BC_PopupMenu(0, 0, 0, "", 0)
49 {
50         this->mwindow = mwindow;
51         this->gui = gui;
52
53         track = 0;
54         edit = 0;
55         plugin = 0;
56         pluginset = 0;
57         position = 0;
58 }
59
60 EditPopup::~EditPopup()
61 {
62 }
63
64 void EditPopup::create_objects()
65 {
66         add_item(new EditPopupClearSelect(mwindow, this));
67         add_item(new EditPopupCopy(mwindow, this));
68         add_item(new EditPopupCut(mwindow, this));
69         add_item(new EditPopupMute(mwindow, this));
70         add_item(new EditPopupCopyPack(mwindow, this));
71         add_item(new EditPopupCutPack(mwindow, this));
72         add_item(new EditPopupMutePack(mwindow, this));
73         add_item(new EditPopupPaste(mwindow, this));
74         add_item(new EditPopupOverwrite(mwindow, this));
75         add_item(new EditPopupFindAsset(mwindow, this));
76         add_item(new EditPopupTitle(mwindow, this));
77         add_item(new EditPopupShow(mwindow, this));
78 }
79
80 int EditPopup::activate_menu(Track *track, Edit *edit,
81                 PluginSet *pluginset, Plugin *plugin, double position)
82 {
83         this->track = track;
84         this->edit = edit;
85         this->pluginset = pluginset;
86         this->plugin = plugin;
87         this->position = position;
88         return BC_PopupMenu::activate_menu();
89 }
90
91 EditPopupClearSelect::EditPopupClearSelect(MWindow *mwindow, EditPopup *popup)
92  : BC_MenuItem(_("Clear Select"),_("Ctrl-Shift-A"),'A')
93 {
94         this->mwindow = mwindow;
95         this->popup = popup;
96         set_ctrl(1);
97         set_shift(1);
98 }
99
100 int EditPopupClearSelect::handle_event()
101 {
102         mwindow->edl->tracks->clear_selected_edits();
103         popup->gui->draw_overlays(1);
104         return 1;
105 }
106
107 EditPopupCopy::EditPopupCopy(MWindow *mwindow, EditPopup *popup)
108  : BC_MenuItem(_("Copy"),_("Ctrl-c"),'c')
109 {
110         this->mwindow = mwindow;
111         this->popup = popup;
112         set_ctrl(1);
113 }
114
115 int EditPopupCopy::handle_event()
116 {
117         mwindow->selected_to_clipboard(0);
118         return 1;
119 }
120
121 EditPopupCopyPack::EditPopupCopyPack(MWindow *mwindow, EditPopup *popup)
122  : BC_MenuItem(_("Copy pack"),_("Ctrl-Shift-C"),'C')
123 {
124         this->mwindow = mwindow;
125         this->popup = popup;
126         set_ctrl(1);
127         set_shift(1);
128 }
129
130 int EditPopupCopyPack::handle_event()
131 {
132         mwindow->selected_to_clipboard(1);
133         return 1;
134 }
135
136 EditPopupCut::EditPopupCut(MWindow *mwindow, EditPopup *popup)
137  : BC_MenuItem(_("Cut"),_("Ctrl-x"),'x')
138 {
139         this->mwindow = mwindow;
140         this->popup = popup;
141         set_ctrl(1);
142 }
143
144 int EditPopupCut::handle_event()
145 {
146         mwindow->cut_selected_edits(1, 0);
147         return 1;
148 }
149
150 EditPopupCutPack::EditPopupCutPack(MWindow *mwindow, EditPopup *popup)
151  : BC_MenuItem(_("Cut pack"),_("Ctrl-z"),'z')
152 {
153         this->mwindow = mwindow;
154         this->popup = popup;
155         set_ctrl(1);
156 }
157
158 int EditPopupCutPack::handle_event()
159 {
160         mwindow->cut_selected_edits(1, 1);
161         return 1;
162 }
163
164 EditPopupMute::EditPopupMute(MWindow *mwindow, EditPopup *popup)
165  : BC_MenuItem(_("Mute"),_("Ctrl-m"),'m')
166 {
167         this->mwindow = mwindow;
168         this->popup = popup;
169         set_ctrl(1);
170 }
171
172 int EditPopupMute::handle_event()
173 {
174         mwindow->cut_selected_edits(0, 0);
175         return 1;
176 }
177
178 EditPopupMutePack::EditPopupMutePack(MWindow *mwindow, EditPopup *popup)
179  : BC_MenuItem(_("Mute pack"),_("Ctrl-Shift-M"),'M')
180 {
181         this->mwindow = mwindow;
182         this->popup = popup;
183         set_ctrl(1);
184         set_shift(1);
185 }
186
187 int EditPopupMutePack::handle_event()
188 {
189         mwindow->cut_selected_edits(0, 1);
190         return 1;
191 }
192
193 EditPopupPaste::EditPopupPaste(MWindow *mwindow, EditPopup *popup)
194  : BC_MenuItem(_("Paste"),_("Ctrl-v"),'v')
195 {
196         this->mwindow = mwindow;
197         this->popup = popup;
198         set_ctrl(1);
199 }
200
201 int EditPopupPaste::handle_event()
202 {
203         mwindow->paste(popup->position, popup->track, 0, 0);
204         mwindow->edl->tracks->clear_selected_edits();
205         popup->gui->draw_overlays(1);
206         if( mwindow->session->current_operation == DROP_TARGETING ) {
207                 mwindow->session->current_operation = NO_OPERATION;
208                 popup->gui->update_cursor();
209         }
210         return 1;
211 }
212
213 EditPopupOverwrite::EditPopupOverwrite(MWindow *mwindow, EditPopup *popup)
214  : BC_MenuItem(_("Overwrite"),_("Ctrl-b"),'b')
215 {
216         this->mwindow = mwindow;
217         this->popup = popup;
218         set_ctrl(1);
219 }
220
221 int EditPopupOverwrite::handle_event()
222 {
223         mwindow->paste(popup->position, popup->track, 0, -1);
224         mwindow->edl->tracks->clear_selected_edits();
225         popup->gui->draw_overlays(1);
226         if( mwindow->session->current_operation == DROP_TARGETING ) {
227                 mwindow->session->current_operation = NO_OPERATION;
228                 popup->gui->update_cursor();
229         }
230         return 1;
231 }
232
233 EditPopupFindAsset::EditPopupFindAsset(MWindow *mwindow, EditPopup *popup)
234  : BC_MenuItem(_("Find in Resources"))
235 {
236         this->mwindow = mwindow;
237         this->popup = popup;
238 }
239
240 int EditPopupFindAsset::handle_event()
241 {
242         Edit *edit = popup->edit;
243         if( edit ) {
244                 Indexable *idxbl = (Indexable *)edit->asset;
245                 if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
246                 if( idxbl ) {
247                         AWindowGUI *agui = mwindow->awindow->gui;
248                         agui->lock_window("EditPopupFindAsset::handle_event");
249                         AssetPicon *picon = 0;
250                         for( int i=0, n=agui->assets.size(); i<n; ++i ) {
251                                 AssetPicon *ap = (AssetPicon *)agui->assets[i];
252                                 int found = ap->indexable && ( idxbl == ap->indexable ||
253                                         !strcmp(idxbl->path, ap->indexable->path) );
254                                 if( found && !picon ) picon = ap;
255                                 ap->set_selected(found);
256                         }
257                         if( picon ) {
258                                 int selected_folder = picon->indexable->folder_no;
259                                 mwindow->edl->session->awindow_folder = selected_folder;
260                                 for( int i=0,n=agui->folders.size(); i<n; ++i ) {
261                                         AssetPicon *folder_item = (AssetPicon *)agui->folders[i];
262                                         int selected = folder_item->foldernum == selected_folder ? 1 : 0;
263                                         folder_item->set_selected(selected);
264                                 }
265                         }
266                         agui->unlock_window();
267                         agui->async_update_assets();
268                 }
269         }
270         return 1;
271 }
272
273
274 EditPopupTitle::EditPopupTitle(MWindow *mwindow, EditPopup *popup)
275  : BC_MenuItem(_("User title..."))
276 {
277         this->mwindow = mwindow;
278         this->popup = popup;
279         dialog_thread = new EditTitleDialogThread(this);
280 }
281
282 EditPopupTitle::~EditPopupTitle()
283 {
284         delete dialog_thread;
285 }
286
287 int EditPopupTitle::handle_event()
288 {
289         if( popup->edit ) {
290                 dialog_thread->close_window();
291                 int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
292                 int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
293                 dialog_thread->start(wx, wy);
294         }
295         return 1;
296 }
297
298 void EditTitleDialogThread::start(int wx, int wy)
299 {
300         this->wx = wx;  this->wy = wy;
301         BC_DialogThread::start();
302 }
303
304 EditTitleDialogThread::EditTitleDialogThread(EditPopupTitle *edit_title)
305 {
306         this->edit_title = edit_title;
307         window = 0;
308 }
309 EditTitleDialogThread::~EditTitleDialogThread()
310 {
311         close_window();
312 }
313
314 BC_Window* EditTitleDialogThread::new_gui()
315 {
316         MWindow *mwindow = edit_title->mwindow;
317         EditPopup *popup = edit_title->popup;
318         window = new EditPopupTitleWindow(mwindow, popup, wx, wy);
319         window->create_objects();
320         return window;
321 }
322
323 void EditTitleDialogThread::handle_close_event(int result)
324 {
325         window = 0;
326 }
327
328 void EditTitleDialogThread::handle_done_event(int result)
329 {
330         if( result ) return;
331         MWindow *mwindow = edit_title->mwindow;
332         EditPopup *popup = edit_title->popup;
333         strcpy(popup->edit->user_title, window->title_text->get_text());
334         mwindow->gui->lock_window("EditTitleDialogThread::handle_done_event");
335         mwindow->gui->draw_canvas(1, 0);
336         mwindow->gui->flash_canvas(1);
337         mwindow->gui->unlock_window();
338 }
339
340 EditPopupTitleWindow::EditPopupTitleWindow(MWindow *mwindow,
341                 EditPopup *popup, int wx, int wy)
342  : BC_Window(_(PROGRAM_NAME ": Set edit title"), wx, wy,
343         300, 130, 300, 130, 0, 0, 1)
344 {
345         this->mwindow = mwindow;
346         this->popup = popup;
347         strcpy(new_text, !popup->edit ? "" : popup->edit->user_title);
348 }
349
350 EditPopupTitleWindow::~EditPopupTitleWindow()
351 {
352 }
353
354 void EditPopupTitleWindow::create_objects()
355 {
356         lock_window("EditPopupTitleWindow::create_objects");
357         int x = 10, y = 10;
358         add_subwindow(new BC_Title(x, y, _("User title:")));
359         title_text = new EditPopupTitleText(this, mwindow, x+15, y+20, new_text);
360         add_subwindow(title_text);
361         add_tool(new BC_OKButton(this));
362         add_tool(new BC_CancelButton(this));
363
364
365         show_window();
366         flush();
367         unlock_window();
368 }
369
370
371 EditPopupTitleText::EditPopupTitleText(EditPopupTitleWindow *window,
372         MWindow *mwindow, int x, int y, const char *text)
373  : BC_TextBox(x, y, 250, 1, text)
374 {
375         this->window = window;
376         this->mwindow = mwindow;
377 }
378
379 EditPopupTitleText::~EditPopupTitleText()
380 {
381 }
382
383 int EditPopupTitleText::handle_event()
384 {
385         if( get_keypress() == RETURN )
386                 window->set_done(0);
387         return 1;
388 }
389
390
391 EditPopupShow::EditPopupShow(MWindow *mwindow, EditPopup *popup)
392  : BC_MenuItem(_("Show edit"))
393 {
394         this->mwindow = mwindow;
395         this->popup = popup;
396         dialog_thread = new EditShowDialogThread(this);
397 }
398
399 EditPopupShow::~EditPopupShow()
400 {
401         delete dialog_thread;
402 }
403
404 int EditPopupShow::handle_event()
405 {
406         if( popup->edit ) {
407                 dialog_thread->close_window();
408                 int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
409                 int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
410                 dialog_thread->start(wx, wy);
411         }
412         return 1;
413 }
414
415 void EditShowDialogThread::start(int wx, int wy)
416 {
417         this->wx = wx;  this->wy = wy;
418         BC_DialogThread::start();
419 }
420
421 EditShowDialogThread::EditShowDialogThread(EditPopupShow *edit_show)
422 {
423         this->edit_show = edit_show;
424         window = 0;
425 }
426 EditShowDialogThread::~EditShowDialogThread()
427 {
428         close_window();
429 }
430
431 BC_Window* EditShowDialogThread::new_gui()
432 {
433         MWindow *mwindow = edit_show->mwindow;
434         EditPopup *popup = edit_show->popup;
435         window = new EditPopupShowWindow(mwindow, popup, wx, wy);
436         window->create_objects();
437         return window;
438 }
439
440 void EditShowDialogThread::handle_close_event(int result)
441 {
442         window = 0;
443 }
444
445 EditPopupShowWindow::EditPopupShowWindow(MWindow *mwindow,
446                 EditPopup *popup, int wx, int wy)
447  : BC_Window(_(PROGRAM_NAME ": Show edit"), wx, wy,
448         300, 220, 300, 220, 0, 0, 1)
449 {
450         this->mwindow = mwindow;
451         this->popup = popup;
452 }
453
454 EditPopupShowWindow::~EditPopupShowWindow()
455 {
456 }
457
458 void EditPopupShowWindow::create_objects()
459 {
460         lock_window("EditPopupShowWindow::create_objects");
461         int x = 10, y = 10;
462         BC_Title *title;
463         char text[BCTEXTLEN];
464         Edit *edit = popup->edit;
465         Track *track = edit->track;
466         sprintf(text, _("Track %d:"), mwindow->edl->tracks->number_of(track)+1);
467         add_subwindow(title = new BC_Title(x, y, text));
468         int x1 = x + title->get_w() + 10;
469         int tw = get_w() - x1 - 20;
470         truncate_text(text, track->title, tw);
471         add_subwindow(new BC_Title(x1, y, text));
472         y += title->get_h() + 5;
473         sprintf(text, _("Edit %d:"), track->edits->number_of(edit)+1);
474         add_subwindow(title = new BC_Title(x, y, text));
475         char edit_title[BCTEXTLEN];
476         edit->get_title(edit_title);
477         truncate_text(text, edit_title, tw);
478         add_subwindow(new BC_Title(x1, y, text));
479         y += title->get_h() + 5;
480
481         EDLSession *session = mwindow->edl->session;
482         int time_format = session->time_format;
483         int sample_rate = session->sample_rate;
484         double frame_rate = session->frame_rate;
485         double frames_per_foot = session->frames_per_foot;
486
487         double startsource = track->from_units(edit->startsource);
488         double startproject = track->from_units(edit->startproject);
489         double length = track->from_units(edit->length);
490
491         char text_startsource[BCSTRLEN];
492         char text_startproject[BCSTRLEN];
493         char text_length[BCSTRLEN];
494         sprintf(text, _("StartSource: %s\nStartProject: %s\nLength: %s\n"),
495                 Units::totext(text_startsource, startsource,
496                         time_format, sample_rate, frame_rate, frames_per_foot),
497                 Units::totext(text_startproject, startproject,
498                         time_format, sample_rate, frame_rate, frames_per_foot),
499                 Units::totext(text_length, length,
500                         time_format, sample_rate, frame_rate, frames_per_foot));
501         show_text = new EditPopupShowText(this, mwindow, x+15, y+10, text);
502         add_subwindow(show_text);
503         add_tool(new BC_OKButton(this));
504
505         show_window();
506         flush();
507         unlock_window();
508 }
509
510
511 EditPopupShowText::EditPopupShowText(EditPopupShowWindow *window,
512         MWindow *mwindow, int x, int y, const char *text)
513  : BC_TextBox(x, y, 250, 4, text)
514 {
515         this->window = window;
516         this->mwindow = mwindow;
517 }
518
519 EditPopupShowText::~EditPopupShowText()
520 {
521 }
522