switch move/swap tracks, add mv trk shortcut, update msg
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / transitionpopup.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 "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "edit.h"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "language.h"
28 #include "mwindow.h"
29 #include "mwindowgui.h"
30 #include "plugin.h"
31 #include "transition.h"
32 #include "track.h"
33 #include "tracks.h"
34 #include "transitionpopup.h"
35
36
37 TransitionLengthThread::TransitionLengthThread(MWindow *mwindow)
38  : BC_DialogThread()
39 {
40         this->mwindow = mwindow;
41 }
42
43 TransitionLengthThread::~TransitionLengthThread()
44 {
45         close_window();
46 }
47
48 void TransitionLengthThread::start(Transition *transition, double length)
49 {
50         this->transition = transition;
51         this->orig_length = length;
52         this->new_length = length;
53         BC_DialogThread::start();
54 }
55
56 #define TLW_W xS(300)
57 #define TLW_H yS(100)
58
59 BC_Window* TransitionLengthThread::new_gui()
60 {
61         BC_DisplayInfo display_info;
62         int x = display_info.get_abs_cursor_x() - TLW_W / 2;
63         int y = display_info.get_abs_cursor_y() - TLW_H / 2;
64         TransitionLengthDialog *gui = new TransitionLengthDialog(mwindow, this, x, y);
65         gui->create_objects();
66         return gui;
67 }
68
69 void TransitionLengthThread::handle_close_event(int result)
70 {
71         if( mwindow->in_destructor ) return;
72         if( !result ) {
73                 if( transition )
74                         mwindow->set_transition_length(transition, new_length);
75                 else
76                         mwindow->set_transition_length(new_length);
77         }
78         else
79                 update(orig_length);
80 }
81
82 int TransitionLengthThread::update(double length)
83 {
84         if( !EQUIV(this->new_length, length) ) {
85                 this->new_length = length;
86                 if( transition ) {
87                         transition->length = transition->track->to_units(length, 1);
88                         mwindow->sync_parameters(CHANGE_EDL);
89                         mwindow->gui->lock_window();
90                         mwindow->gui->draw_overlays(1);
91                         mwindow->gui->unlock_window();
92                 }
93         }
94         return 1;
95 }
96
97
98 TransitionUnitsItem::TransitionUnitsItem(TransitionUnitsPopup *popup,
99                 const char *text, int id)
100  : BC_MenuItem(text)
101 {
102         this->popup = popup;
103         this->id = id;
104 }
105
106 TransitionUnitsItem::~TransitionUnitsItem()
107 {
108 }
109
110 int TransitionUnitsItem::handle_event()
111 {
112         TransitionUnitsPopup *units_popup = (TransitionUnitsPopup *)get_popup_menu();
113         TransitionLengthDialog *gui = units_popup->gui;
114         double length = gui->thread->new_length;
115         units_popup->units = id;
116         gui->update_text(length);
117         units_popup->set_text(get_text());
118         return 1;
119 }
120
121 TransitionUnitsPopup::TransitionUnitsPopup(TransitionLengthDialog *gui, int x, int y)
122  : BC_PopupMenu(x, y, xS(120), "", 1)
123 {
124         this->gui = gui;
125         units = TIME_SECONDS;
126 }
127
128 TransitionUnitsPopup::~TransitionUnitsPopup()
129 {
130 }
131
132 void TransitionUnitsPopup::create_objects()
133 {
134         TransitionUnitsItem *units_item;
135         add_item(units_item = new TransitionUnitsItem(this, _("Seconds"), TIME_SECONDS));
136         add_item(new TransitionUnitsItem(this, _("Frames"), TIME_FRAMES));
137         add_item(new TransitionUnitsItem(this, _("Samples"), TIME_SAMPLES));
138         add_item(new TransitionUnitsItem(this, _("H:M:S.xxx"), TIME_HMS));
139         add_item(new TransitionUnitsItem(this, _("H:M:S:frm"), TIME_HMSF));
140         set_text(units_item->get_text());
141 }
142
143
144 TransitionLengthDialog::TransitionLengthDialog(MWindow *mwindow,
145         TransitionLengthThread *thread, int x, int y)
146  : BC_Window(_(PROGRAM_NAME ": Transition length"), x, y,
147                 TLW_W, TLW_H, -1, -1, 0, 0, 1)
148 {
149         this->mwindow = mwindow;
150         this->thread = thread;
151 }
152
153 TransitionLengthDialog::~TransitionLengthDialog()
154 {
155 }
156
157
158 void TransitionLengthDialog::create_objects()
159 {
160         lock_window("TransitionLengthDialog::create_objects");
161         add_subwindow(units_popup = new TransitionUnitsPopup(this, xS(10), yS(10)));
162         units_popup->create_objects();
163         text = new TransitionLengthText(mwindow, this, xS(160), yS(10));
164         text->create_objects();
165         text->set_precision(3);
166         text->set_increment(0.1);
167         add_subwindow(new BC_OKButton(this));
168         add_subwindow(new BC_CancelButton(this));
169         show_window();
170         unlock_window();
171 }
172
173 int TransitionLengthDialog::close_event()
174 {
175         set_done(0);
176         return 1;
177 }
178
179 void TransitionLengthDialog::update_text(double length)
180 {
181         int units = units_popup->units;
182         EDLSession *session = mwindow->edl->session;
183         char string[BCSTRLEN];
184         Units::totext(string, length, units,
185                 session->sample_rate, session->frame_rate,
186                 session->frames_per_foot);
187         text->update(string);
188 }
189
190
191 TransitionLengthText::TransitionLengthText(MWindow *mwindow,
192         TransitionLengthDialog *gui, int x, int y)
193  : BC_TumbleTextBox(gui, (float)gui->thread->new_length,
194                 0.f, 100.f, x, y, xS(100))
195 {
196         this->mwindow = mwindow;
197         this->gui = gui;
198 }
199
200 int TransitionLengthText::handle_event()
201 {
202         const char *text = get_text();
203         int units = gui->units_popup->units;
204         EDLSession *session = gui->mwindow->edl->session;
205         double result = Units::text_to_seconds(text, session->sample_rate,
206                 units, session->frame_rate, session->frames_per_foot);
207         return gui->thread->update(result);
208 }
209
210 int TransitionLengthText::handle_up_down(int dir)
211 {
212         double delta = 0;
213         int units = gui->units_popup->units;
214         EDLSession *session = gui->mwindow->edl->session;
215         switch( units ) {
216         case TIME_SECONDS:
217         case TIME_HMS:
218                 delta = 0.1;
219                 break;
220         case TIME_HMSF:
221         case TIME_FRAMES:
222                 delta = session->frame_rate > 0 ? 1./session->frame_rate : 0;
223                 break;
224         case TIME_SAMPLES:
225                 delta = session->sample_rate > 0 ? 1./session->sample_rate : 0;
226                 break;
227         }
228         double length = gui->thread->new_length + delta * dir;
229         gui->update_text(length);
230         return gui->thread->update(length);
231 }
232
233 int TransitionLengthText::handle_up_event()
234 {
235         return handle_up_down(+1);
236 }
237 int TransitionLengthText::handle_down_event()
238 {
239         return handle_up_down(-1);
240 }
241
242
243 TransitionPopup::TransitionPopup(MWindow *mwindow, MWindowGUI *gui)
244  : BC_PopupMenu(0, 0, 0, "", 0)
245 {
246         this->mwindow = mwindow;
247         this->gui = gui;
248 }
249
250 TransitionPopup::~TransitionPopup()
251 {
252         delete length_thread;
253 //      delete dialog_thread;
254 }
255
256
257 void TransitionPopup::create_objects()
258 {
259         length_thread = new TransitionLengthThread(mwindow);
260 //      add_item(attach = new TransitionPopupAttach(mwindow, this));
261         add_item(show = new TransitionPopupShow(mwindow, this));
262         add_item(on = new TransitionPopupOn(mwindow, this));
263         add_item(length_item = new TransitionPopupLength(mwindow, this));
264         add_item(detach = new TransitionPopupDetach(mwindow, this));
265 }
266
267 int TransitionPopup::update(Transition *transition)
268 {
269         this->transition = transition;
270         show->set_checked(transition->show);
271         on->set_checked(transition->on);
272         char len_text[50];
273         sprintf(len_text, _("Length: %2.2f sec"), transition->track->from_units(transition->length));
274         length_item->set_text(len_text);
275         return 0;
276 }
277
278
279 TransitionPopupAttach::TransitionPopupAttach(MWindow *mwindow, TransitionPopup *popup)
280  : BC_MenuItem(_("Attach..."))
281 {
282         this->mwindow = mwindow;
283         this->popup = popup;
284 }
285
286 TransitionPopupAttach::~TransitionPopupAttach()
287 {
288 }
289
290 int TransitionPopupAttach::handle_event()
291 {
292 //      popup->dialog_thread->start();
293         return 1;
294 }
295
296
297 TransitionPopupDetach::TransitionPopupDetach(MWindow *mwindow, TransitionPopup *popup)
298  : BC_MenuItem(_("Detach"))
299 {
300         this->mwindow = mwindow;
301         this->popup = popup;
302 }
303
304 TransitionPopupDetach::~TransitionPopupDetach()
305 {
306 }
307
308 int TransitionPopupDetach::handle_event()
309 {
310         mwindow->detach_transition(popup->transition);
311         return 1;
312 }
313
314
315 TransitionPopupOn::TransitionPopupOn(MWindow *mwindow, TransitionPopup *popup)
316  : BC_MenuItem(_("On"))
317 {
318         this->mwindow = mwindow;
319         this->popup = popup;
320 }
321
322 TransitionPopupOn::~TransitionPopupOn()
323 {
324 }
325
326 int TransitionPopupOn::handle_event()
327 {
328         popup->transition->on = !get_checked();
329         mwindow->sync_parameters(CHANGE_EDL);
330         return 1;
331 }
332
333
334 TransitionPopupShow::TransitionPopupShow(MWindow *mwindow, TransitionPopup *popup)
335  : BC_MenuItem(_("Show"))
336 {
337         this->mwindow = mwindow;
338         this->popup = popup;
339 }
340
341 TransitionPopupShow::~TransitionPopupShow()
342 {
343 }
344
345 int TransitionPopupShow::handle_event()
346 {
347         mwindow->show_plugin(popup->transition);
348         return 1;
349 }
350
351
352 TransitionPopupLength::TransitionPopupLength(MWindow *mwindow, TransitionPopup *popup)
353  : BC_MenuItem(_("Length"))
354 {
355         this->mwindow = mwindow;
356         this->popup = popup;
357 }
358
359 TransitionPopupLength::~TransitionPopupLength()
360 {
361 }
362
363 int TransitionPopupLength::handle_event()
364 {
365         Transition *transition = popup->transition;
366         double length = transition->edit->track->from_units(transition->length);
367         popup->length_thread->start(popup->transition, length);
368         return 1;
369 }
370