b12c5d07f6bf6e753177bb78070a1fd9d2bec451
[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-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-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-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         if( mwindow->session->current_operation == DROP_TARGETING ) {
205                 mwindow->session->current_operation = NO_OPERATION;
206                 popup->gui->update_cursor();
207         }
208         return 1;
209 }
210
211 EditPopupOverwrite::EditPopupOverwrite(MWindow *mwindow, EditPopup *popup)
212  : BC_MenuItem(_("Overwrite"),_("Ctrl-b"),'b')
213 {
214         this->mwindow = mwindow;
215         this->popup = popup;
216         set_ctrl(1);
217 }
218
219 int EditPopupOverwrite::handle_event()
220 {
221         mwindow->paste(popup->position, popup->track, 0, -1);
222         if( mwindow->session->current_operation == DROP_TARGETING ) {
223                 mwindow->session->current_operation = NO_OPERATION;
224                 popup->gui->update_cursor();
225         }
226         return 1;
227 }
228
229 EditPopupFindAsset::EditPopupFindAsset(MWindow *mwindow, EditPopup *popup)
230  : BC_MenuItem(_("Find in Resources"))
231 {
232         this->mwindow = mwindow;
233         this->popup = popup;
234 }
235
236 int EditPopupFindAsset::handle_event()
237 {
238         Edit *edit = popup->edit;
239         if( edit ) {
240                 Indexable *idxbl = (Indexable *)edit->asset;
241                 if( !idxbl ) idxbl = (Indexable *)edit->nested_edl;
242                 if( idxbl ) {
243                         AWindowGUI *agui = mwindow->awindow->gui;
244                         agui->lock_window("EditPopupFindAsset::handle_event");
245                         AssetPicon *picon = 0;
246                         for( int i=0, n=agui->assets.size(); i<n; ++i ) {
247                                 AssetPicon *ap = (AssetPicon *)agui->assets[i];
248                                 int found = ap->indexable && ( idxbl == ap->indexable ||
249                                         !strcmp(idxbl->path, ap->indexable->path) );
250                                 if( found && !picon ) picon = ap;
251                                 ap->set_selected(found);
252                         }
253                         if( picon ) {
254                                 int selected_folder = picon->indexable->folder_no;
255                                 mwindow->edl->session->awindow_folder = selected_folder;
256                                 for( int i=0,n=agui->folders.size(); i<n; ++i ) {
257                                         AssetPicon *folder_item = (AssetPicon *)agui->folders[i];
258                                         int selected = folder_item->foldernum == selected_folder ? 1 : 0;
259                                         folder_item->set_selected(selected);
260                                 }
261                         }
262                         agui->unlock_window();
263                         agui->async_update_assets();
264                 }
265         }
266         return 1;
267 }
268
269
270 EditPopupTitle::EditPopupTitle(MWindow *mwindow, EditPopup *popup)
271  : BC_MenuItem(_("User title..."))
272 {
273         this->mwindow = mwindow;
274         this->popup = popup;
275         dialog_thread = new EditTitleDialogThread(this);
276 }
277
278 EditPopupTitle::~EditPopupTitle()
279 {
280         delete dialog_thread;
281 }
282
283 int EditPopupTitle::handle_event()
284 {
285         if( popup->edit ) {
286                 dialog_thread->close_window();
287                 int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
288                 int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
289                 dialog_thread->start(wx, wy);
290         }
291         return 1;
292 }
293
294 void EditTitleDialogThread::start(int wx, int wy)
295 {
296         this->wx = wx;  this->wy = wy;
297         BC_DialogThread::start();
298 }
299
300 EditTitleDialogThread::EditTitleDialogThread(EditPopupTitle *edit_title)
301 {
302         this->edit_title = edit_title;
303         window = 0;
304 }
305 EditTitleDialogThread::~EditTitleDialogThread()
306 {
307         close_window();
308 }
309
310 BC_Window* EditTitleDialogThread::new_gui()
311 {
312         MWindow *mwindow = edit_title->mwindow;
313         EditPopup *popup = edit_title->popup;
314         window = new EditPopupTitleWindow(mwindow, popup, wx, wy);
315         window->create_objects();
316         return window;
317 }
318
319 void EditTitleDialogThread::handle_close_event(int result)
320 {
321         window = 0;
322 }
323
324 void EditTitleDialogThread::handle_done_event(int result)
325 {
326         if( result ) return;
327         MWindow *mwindow = edit_title->mwindow;
328         EditPopup *popup = edit_title->popup;
329         strcpy(popup->edit->user_title, window->title_text->get_text());
330         mwindow->gui->lock_window("EditTitleDialogThread::handle_done_event");
331         mwindow->gui->draw_canvas(1, 0);
332         mwindow->gui->flash_canvas(1);
333         mwindow->gui->unlock_window();
334 }
335
336 EditPopupTitleWindow::EditPopupTitleWindow(MWindow *mwindow,
337                 EditPopup *popup, int wx, int wy)
338  : BC_Window(_(PROGRAM_NAME ": Set edit title"), wx, wy,
339         300, 130, 300, 130, 0, 0, 1)
340 {
341         this->mwindow = mwindow;
342         this->popup = popup;
343         strcpy(new_text, !popup->edit ? "" : popup->edit->user_title);
344 }
345
346 EditPopupTitleWindow::~EditPopupTitleWindow()
347 {
348 }
349
350 void EditPopupTitleWindow::create_objects()
351 {
352         lock_window("EditPopupTitleWindow::create_objects");
353         int x = 10, y = 10;
354         add_subwindow(new BC_Title(x, y, _("User title:")));
355         title_text = new EditPopupTitleText(this, mwindow, x+15, y+20, new_text);
356         add_subwindow(title_text);
357         add_tool(new BC_OKButton(this));
358         add_tool(new BC_CancelButton(this));
359
360
361         show_window();
362         flush();
363         unlock_window();
364 }
365
366
367 EditPopupTitleText::EditPopupTitleText(EditPopupTitleWindow *window,
368         MWindow *mwindow, int x, int y, const char *text)
369  : BC_TextBox(x, y, 250, 1, text)
370 {
371         this->window = window;
372         this->mwindow = mwindow;
373 }
374
375 EditPopupTitleText::~EditPopupTitleText()
376 {
377 }
378
379 int EditPopupTitleText::handle_event()
380 {
381         if( get_keypress() == RETURN )
382                 window->set_done(0);
383         return 1;
384 }
385
386
387 EditPopupShow::EditPopupShow(MWindow *mwindow, EditPopup *popup)
388  : BC_MenuItem(_("Show edit"))
389 {
390         this->mwindow = mwindow;
391         this->popup = popup;
392         dialog_thread = new EditShowDialogThread(this);
393 }
394
395 EditPopupShow::~EditPopupShow()
396 {
397         delete dialog_thread;
398 }
399
400 int EditPopupShow::handle_event()
401 {
402         if( popup->edit ) {
403                 dialog_thread->close_window();
404                 int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
405                 int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
406                 dialog_thread->start(wx, wy);
407         }
408         return 1;
409 }
410
411 void EditShowDialogThread::start(int wx, int wy)
412 {
413         this->wx = wx;  this->wy = wy;
414         BC_DialogThread::start();
415 }
416
417 EditShowDialogThread::EditShowDialogThread(EditPopupShow *edit_show)
418 {
419         this->edit_show = edit_show;
420         window = 0;
421 }
422 EditShowDialogThread::~EditShowDialogThread()
423 {
424         close_window();
425 }
426
427 BC_Window* EditShowDialogThread::new_gui()
428 {
429         MWindow *mwindow = edit_show->mwindow;
430         EditPopup *popup = edit_show->popup;
431         window = new EditPopupShowWindow(mwindow, popup, wx, wy);
432         window->create_objects();
433         return window;
434 }
435
436 void EditShowDialogThread::handle_close_event(int result)
437 {
438         window = 0;
439 }
440
441 EditPopupShowWindow::EditPopupShowWindow(MWindow *mwindow,
442                 EditPopup *popup, int wx, int wy)
443  : BC_Window(_(PROGRAM_NAME ": Show edit"), wx, wy,
444         300, 220, 300, 220, 0, 0, 1)
445 {
446         this->mwindow = mwindow;
447         this->popup = popup;
448 }
449
450 EditPopupShowWindow::~EditPopupShowWindow()
451 {
452 }
453
454 void EditPopupShowWindow::create_objects()
455 {
456         lock_window("EditPopupShowWindow::create_objects");
457         int x = 10, y = 10;
458         BC_Title *title;
459         char text[BCTEXTLEN];
460         Edit *edit = popup->edit;
461         Track *track = edit->track;
462         sprintf(text, _("Track %d:"), mwindow->edl->tracks->number_of(track)+1);
463         add_subwindow(title = new BC_Title(x, y, text));
464         int x1 = x + title->get_w() + 10;
465         int tw = get_w() - x1 - 20;
466         truncate_text(text, track->title, tw);
467         add_subwindow(new BC_Title(x1, y, text));
468         y += title->get_h() + 5;
469         sprintf(text, _("Edit %d:"), track->edits->number_of(edit)+1);
470         add_subwindow(title = new BC_Title(x, y, text));
471         char edit_title[BCTEXTLEN];
472         edit->get_title(edit_title);
473         truncate_text(text, edit_title, tw);
474         add_subwindow(new BC_Title(x1, y, text));
475         y += title->get_h() + 5;
476
477         EDLSession *session = mwindow->edl->session;
478         int time_format = session->time_format;
479         int sample_rate = session->sample_rate;
480         double frame_rate = session->frame_rate;
481         double frames_per_foot = session->frames_per_foot;
482
483         double startsource = track->from_units(edit->startsource);
484         double startproject = track->from_units(edit->startproject);
485         double length = track->from_units(edit->length);
486
487         char text_startsource[BCSTRLEN];
488         char text_startproject[BCSTRLEN];
489         char text_length[BCSTRLEN];
490         sprintf(text, _("StartSource: %s\nStartProject: %s\nLength: %s\n"),
491                 Units::totext(text_startsource, startsource,
492                         time_format, sample_rate, frame_rate, frames_per_foot),
493                 Units::totext(text_startproject, startproject,
494                         time_format, sample_rate, frame_rate, frames_per_foot),
495                 Units::totext(text_length, length,
496                         time_format, sample_rate, frame_rate, frames_per_foot));
497         show_text = new EditPopupShowText(this, mwindow, x+15, y+10, text);
498         add_subwindow(show_text);
499         add_tool(new BC_OKButton(this));
500
501         show_window();
502         flush();
503         unlock_window();
504 }
505
506
507 EditPopupShowText::EditPopupShowText(EditPopupShowWindow *window,
508         MWindow *mwindow, int x, int y, const char *text)
509  : BC_TextBox(x, y, 250, 4, text)
510 {
511         this->window = window;
512         this->mwindow = mwindow;
513 }
514
515 EditPopupShowText::~EditPopupShowText()
516 {
517 }
518