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