cbc57606b3fac5139769e18178882e55b6ac51e8
[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_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_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         color_picker->color = 0;
445         color_picker->update_gui(0, 0);
446         return 1;
447 }
448
449 EditTitleColorPicker::EditTitleColorPicker(EditPopup *popup, int color)
450  : ColorPicker(1, _("Bar Color"))
451 {
452         this->popup = popup;
453         this->color = color;
454 }
455 EditTitleColorPicker::~EditTitleColorPicker()
456 {
457 }
458 void EditTitleColorPicker::create_objects(ColorWindow *gui)
459 {
460         int y = gui->get_h() - BC_CancelButton::calculate_h() + 10;
461         int x = gui->get_w() - BC_CancelButton::calculate_w() - 10;
462         x -= BC_GenericButton::calculate_w(gui, _("default")) + 15;
463         gui->add_subwindow(new EditTitleColorDefault(this, x, y));
464 }
465
466 int EditTitleColorPicker::handle_new_color(int color, int alpha)
467 {
468         this->color = color | (~alpha << 24);
469         return 1;
470 }
471
472 void EditTitleColorPicker::handle_done_event(int result)
473 {
474         if( !result ) {
475                 EDL *edl = popup->mwindow->edl;
476                 int count = 0;
477                 for( Track *track=edl->tracks->first; track; track=track->next ) {
478                         if( !track->record ) continue;
479                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
480                                 if( !edit->is_selected ) continue;
481                                 edit->color = color;
482                                 ++count;
483                         }
484                 }
485                 if( count )
486                         edl->tracks->clear_selected_edits();
487                 else
488                         popup->edit->color = color;
489         }
490         MWindowGUI *mwindow_gui = popup->mwindow->gui;
491         mwindow_gui->lock_window("GWindowColorUpdate::run");
492         mwindow_gui->draw_trackmovement();
493         mwindow_gui->unlock_window();
494 }
495
496
497 EditPopupShow::EditPopupShow(MWindow *mwindow, EditPopup *popup)
498  : BC_MenuItem(_("Show edit"))
499 {
500         this->mwindow = mwindow;
501         this->popup = popup;
502         dialog_thread = new EditShowDialogThread(this);
503 }
504
505 EditPopupShow::~EditPopupShow()
506 {
507         delete dialog_thread;
508 }
509
510 int EditPopupShow::handle_event()
511 {
512         if( popup->edit ) {
513                 dialog_thread->close_window();
514                 int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
515                 int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
516                 dialog_thread->start(wx, wy);
517         }
518         return 1;
519 }
520
521 void EditShowDialogThread::start(int wx, int wy)
522 {
523         this->wx = wx;  this->wy = wy;
524         BC_DialogThread::start();
525 }
526
527 EditShowDialogThread::EditShowDialogThread(EditPopupShow *edit_show)
528 {
529         this->edit_show = edit_show;
530         window = 0;
531 }
532 EditShowDialogThread::~EditShowDialogThread()
533 {
534         close_window();
535 }
536
537 BC_Window* EditShowDialogThread::new_gui()
538 {
539         MWindow *mwindow = edit_show->mwindow;
540         EditPopup *popup = edit_show->popup;
541         window = new EditPopupShowWindow(mwindow, popup, wx, wy);
542         window->create_objects();
543         return window;
544 }
545
546 void EditShowDialogThread::handle_close_event(int result)
547 {
548         window = 0;
549 }
550
551 EditPopupShowWindow::EditPopupShowWindow(MWindow *mwindow,
552                 EditPopup *popup, int wx, int wy)
553  : BC_Window(_(PROGRAM_NAME ": Show edit"), wx, wy,
554         300, 220, 300, 220, 0, 0, 1)
555 {
556         this->mwindow = mwindow;
557         this->popup = popup;
558 }
559
560 EditPopupShowWindow::~EditPopupShowWindow()
561 {
562 }
563
564 void EditPopupShowWindow::create_objects()
565 {
566         lock_window("EditPopupShowWindow::create_objects");
567         int x = 10, y = 10;
568         BC_Title *title;
569         char text[BCTEXTLEN];
570         Edit *edit = popup->edit;
571         Track *track = edit->track;
572         sprintf(text, _("Track %d:"), mwindow->edl->tracks->number_of(track)+1);
573         add_subwindow(title = new BC_Title(x, y, text));
574         int x1 = x + title->get_w() + 10;
575         int tw = get_w() - x1 - 20;
576         truncate_text(text, track->title, tw);
577         add_subwindow(new BC_Title(x1, y, text));
578         y += title->get_h() + 5;
579         sprintf(text, _("Edit %d:"), track->edits->number_of(edit)+1);
580         add_subwindow(title = new BC_Title(x, y, text));
581         char edit_title[BCTEXTLEN];
582         edit->get_title(edit_title);
583         truncate_text(text, edit_title, tw);
584         add_subwindow(new BC_Title(x1, y, text));
585         y += title->get_h() + 5;
586
587         EDLSession *session = mwindow->edl->session;
588         int time_format = session->time_format;
589         int sample_rate = session->sample_rate;
590         double frame_rate = session->frame_rate;
591         double frames_per_foot = session->frames_per_foot;
592
593         double startsource = track->from_units(edit->startsource);
594         double startproject = track->from_units(edit->startproject);
595         double length = track->from_units(edit->length);
596
597         char text_startsource[BCSTRLEN];
598         char text_startproject[BCSTRLEN];
599         char text_length[BCSTRLEN];
600         sprintf(text, _("StartSource: %s\nStartProject: %s\nLength: %s\n"),
601                 Units::totext(text_startsource, startsource,
602                         time_format, sample_rate, frame_rate, frames_per_foot),
603                 Units::totext(text_startproject, startproject,
604                         time_format, sample_rate, frame_rate, frames_per_foot),
605                 Units::totext(text_length, length,
606                         time_format, sample_rate, frame_rate, frames_per_foot));
607         show_text = new EditPopupShowText(this, mwindow, x+15, y+10, text);
608         add_subwindow(show_text);
609         add_tool(new BC_OKButton(this));
610
611         show_window();
612         flush();
613         unlock_window();
614 }
615
616
617 EditPopupShowText::EditPopupShowText(EditPopupShowWindow *window,
618         MWindow *mwindow, int x, int y, const char *text)
619  : BC_TextBox(x, y, 250, 4, text)
620 {
621         this->window = window;
622         this->mwindow = mwindow;
623 }
624
625 EditPopupShowText::~EditPopupShowText()
626 {
627 }
628