colorpicker rework, cin.desktop+shortcuts+license updates, edit titlebar colors+auto...
[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         strcpy(popup->edit->user_title, window->title_text->get_text());
336         mwindow->gui->lock_window("EditUserTitleDialogThread::handle_done_event");
337         mwindow->gui->draw_canvas(1, 0);
338         mwindow->gui->flash_canvas(1);
339         mwindow->gui->unlock_window();
340 }
341
342 EditPopupUserTitleWindow::EditPopupUserTitleWindow(MWindow *mwindow,
343                 EditPopup *popup, int wx, int wy)
344  : BC_Window(_(PROGRAM_NAME ": Set edit title"), wx, wy,
345         300, 130, 300, 130, 0, 0, 1)
346 {
347         this->mwindow = mwindow;
348         this->popup = popup;
349         strcpy(new_text, !popup->edit ? "" : popup->edit->user_title);
350 }
351
352 EditPopupUserTitleWindow::~EditPopupUserTitleWindow()
353 {
354 }
355
356 void EditPopupUserTitleWindow::create_objects()
357 {
358         lock_window("EditPopupUserTitleWindow::create_objects");
359         int x = 10, y = 10, x1;
360         BC_Title *title = new BC_Title(x1=x, y, _("User title:"));
361         add_subwindow(title);  x1 += title->get_w() + 10;
362         title_text = new EditPopupUserTitleText(this, mwindow, x1, y, new_text);
363         add_subwindow(title_text);
364
365         add_tool(new BC_OKButton(this));
366         add_tool(new BC_CancelButton(this));
367
368
369         show_window();
370         flush();
371         unlock_window();
372 }
373
374
375 EditPopupUserTitleText::EditPopupUserTitleText(EditPopupUserTitleWindow *window,
376         MWindow *mwindow, int x, int y, const char *text)
377  : BC_TextBox(x, y, window->get_w()-x-15, 1, text)
378 {
379         this->window = window;
380         this->mwindow = mwindow;
381 }
382
383 EditPopupUserTitleText::~EditPopupUserTitleText()
384 {
385 }
386
387 int EditPopupUserTitleText::handle_event()
388 {
389         if( get_keypress() == RETURN )
390                 window->set_done(0);
391         return 1;
392 }
393
394
395 EditPopupTitleColor::EditPopupTitleColor(MWindow *mwindow, EditPopup *popup)
396  : BC_MenuItem(_("Bar Color..."))
397 {
398         this->mwindow = mwindow;
399         this->popup = popup;
400         color_picker = 0;
401 }
402 EditPopupTitleColor::~EditPopupTitleColor()
403 {
404         delete color_picker;
405 }
406
407 int EditPopupTitleColor::handle_event()
408 {
409         if( popup->edit ) {
410                 int color = popup->mwindow->get_title_color(popup->edit);
411                 if( color < 0 ) color = popup->mwindow->theme->get_color_title_bg();
412                 delete color_picker;
413                 color_picker = new EditTitleColorPicker(popup);
414                 color_picker->start_window(color, -1, 1);
415         }
416         return 1;
417 }
418
419 EditTitleColorDefault::EditTitleColorDefault(
420         EditTitleColorPicker *color_picker, int x, int y)
421  : BC_GenericButton(x, y, _("default"))
422 {
423         this->color_picker = color_picker;
424 }
425
426 int EditTitleColorDefault::handle_event()
427 {
428         int color = color_picker->popup->mwindow->theme->get_color_title_bg();
429         color_picker->update_gui(color, -1);
430         return 1;
431 }
432
433 EditTitleColorPicker::EditTitleColorPicker(EditPopup *popup)
434  : ColorPicker(0, _("Bar Color"))
435 {
436         this->popup = popup;
437         color = -1;
438 }
439 EditTitleColorPicker::~EditTitleColorPicker()
440 {
441 }
442 void EditTitleColorPicker::create_objects(ColorWindow *gui)
443 {
444         int y = gui->get_h() - BC_CancelButton::calculate_h() - 50;
445         int x = gui->get_w() - BC_GenericButton::calculate_w(gui, _("default")) - 15;
446         gui->add_subwindow(new EditTitleColorDefault(this, x, y));
447 }
448
449 int EditTitleColorPicker::handle_new_color(int color, int alpha)
450 {
451         this->color = color;
452         return 1;
453 }
454
455 void EditTitleColorPicker::handle_done_event(int result)
456 {
457         if( !result ) {
458                 EDL *edl = popup->mwindow->edl;
459                 int count = 0;
460                 for( Track *track=edl->tracks->first; track; track=track->next ) {
461                         if( !track->record ) continue;
462                         for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
463                                 if( !edit->is_selected ) continue;
464                                 edit->color = color;
465                                 ++count;
466                         }
467                 }
468                 if( count )
469                         edl->tracks->clear_selected_edits();
470                 else
471                         popup->edit->color = color;
472         }
473         MWindowGUI *mwindow_gui = popup->mwindow->gui;
474         mwindow_gui->lock_window("GWindowColorUpdate::run");
475         mwindow_gui->draw_trackmovement();
476         mwindow_gui->unlock_window();
477 }
478
479
480 EditPopupShow::EditPopupShow(MWindow *mwindow, EditPopup *popup)
481  : BC_MenuItem(_("Show edit"))
482 {
483         this->mwindow = mwindow;
484         this->popup = popup;
485         dialog_thread = new EditShowDialogThread(this);
486 }
487
488 EditPopupShow::~EditPopupShow()
489 {
490         delete dialog_thread;
491 }
492
493 int EditPopupShow::handle_event()
494 {
495         if( popup->edit ) {
496                 dialog_thread->close_window();
497                 int wx = mwindow->gui->get_abs_cursor_x(0) - 400 / 2;
498                 int wy = mwindow->gui->get_abs_cursor_y(0) - 500 / 2;
499                 dialog_thread->start(wx, wy);
500         }
501         return 1;
502 }
503
504 void EditShowDialogThread::start(int wx, int wy)
505 {
506         this->wx = wx;  this->wy = wy;
507         BC_DialogThread::start();
508 }
509
510 EditShowDialogThread::EditShowDialogThread(EditPopupShow *edit_show)
511 {
512         this->edit_show = edit_show;
513         window = 0;
514 }
515 EditShowDialogThread::~EditShowDialogThread()
516 {
517         close_window();
518 }
519
520 BC_Window* EditShowDialogThread::new_gui()
521 {
522         MWindow *mwindow = edit_show->mwindow;
523         EditPopup *popup = edit_show->popup;
524         window = new EditPopupShowWindow(mwindow, popup, wx, wy);
525         window->create_objects();
526         return window;
527 }
528
529 void EditShowDialogThread::handle_close_event(int result)
530 {
531         window = 0;
532 }
533
534 EditPopupShowWindow::EditPopupShowWindow(MWindow *mwindow,
535                 EditPopup *popup, int wx, int wy)
536  : BC_Window(_(PROGRAM_NAME ": Show edit"), wx, wy,
537         300, 220, 300, 220, 0, 0, 1)
538 {
539         this->mwindow = mwindow;
540         this->popup = popup;
541 }
542
543 EditPopupShowWindow::~EditPopupShowWindow()
544 {
545 }
546
547 void EditPopupShowWindow::create_objects()
548 {
549         lock_window("EditPopupShowWindow::create_objects");
550         int x = 10, y = 10;
551         BC_Title *title;
552         char text[BCTEXTLEN];
553         Edit *edit = popup->edit;
554         Track *track = edit->track;
555         sprintf(text, _("Track %d:"), mwindow->edl->tracks->number_of(track)+1);
556         add_subwindow(title = new BC_Title(x, y, text));
557         int x1 = x + title->get_w() + 10;
558         int tw = get_w() - x1 - 20;
559         truncate_text(text, track->title, tw);
560         add_subwindow(new BC_Title(x1, y, text));
561         y += title->get_h() + 5;
562         sprintf(text, _("Edit %d:"), track->edits->number_of(edit)+1);
563         add_subwindow(title = new BC_Title(x, y, text));
564         char edit_title[BCTEXTLEN];
565         edit->get_title(edit_title);
566         truncate_text(text, edit_title, tw);
567         add_subwindow(new BC_Title(x1, y, text));
568         y += title->get_h() + 5;
569
570         EDLSession *session = mwindow->edl->session;
571         int time_format = session->time_format;
572         int sample_rate = session->sample_rate;
573         double frame_rate = session->frame_rate;
574         double frames_per_foot = session->frames_per_foot;
575
576         double startsource = track->from_units(edit->startsource);
577         double startproject = track->from_units(edit->startproject);
578         double length = track->from_units(edit->length);
579
580         char text_startsource[BCSTRLEN];
581         char text_startproject[BCSTRLEN];
582         char text_length[BCSTRLEN];
583         sprintf(text, _("StartSource: %s\nStartProject: %s\nLength: %s\n"),
584                 Units::totext(text_startsource, startsource,
585                         time_format, sample_rate, frame_rate, frames_per_foot),
586                 Units::totext(text_startproject, startproject,
587                         time_format, sample_rate, frame_rate, frames_per_foot),
588                 Units::totext(text_length, length,
589                         time_format, sample_rate, frame_rate, frames_per_foot));
590         show_text = new EditPopupShowText(this, mwindow, x+15, y+10, text);
591         add_subwindow(show_text);
592         add_tool(new BC_OKButton(this));
593
594         show_window();
595         flush();
596         unlock_window();
597 }
598
599
600 EditPopupShowText::EditPopupShowText(EditPopupShowWindow *window,
601         MWindow *mwindow, int x, int y, const char *text)
602  : BC_TextBox(x, y, 250, 4, text)
603 {
604         this->window = window;
605         this->mwindow = mwindow;
606 }
607
608 EditPopupShowText::~EditPopupShowText()
609 {
610 }
611