initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / vpatchgui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 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 "autoconf.h"
23 #include "automation.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "floatauto.h"
27 #include "floatautos.h"
28 #include "intauto.h"
29 #include "intautos.h"
30 #include "language.h"
31 #include "localsession.h"
32 #include "mainsession.h"
33 #include "mainundo.h"
34 #include "mwindow.h"
35 #include "mwindowgui.h"
36 #include "overlayframe.inc"
37 #include "patchbay.h"
38 #include "theme.h"
39 #include "trackcanvas.h"
40 #include "vpatchgui.h"
41 #include "vtrack.h"
42
43 #include <string.h>
44
45
46
47
48
49 VPatchGUI::VPatchGUI(MWindow *mwindow, PatchBay *patchbay, VTrack *track, int x, int y)
50  : PatchGUI(mwindow, patchbay, track, x, y)
51 {
52         data_type = TRACK_VIDEO;
53         this->vtrack = track;
54         mode = 0;
55         fade = 0;
56 }
57
58 VPatchGUI::~VPatchGUI()
59 {
60         if(fade) delete fade;
61         if(mode) delete mode;
62 }
63
64 void VPatchGUI::create_objects()
65 {
66         update(x, y);
67 }
68
69 int VPatchGUI::reposition(int x, int y)
70 {
71         //int x1 = 0;
72         int y1 = PatchGUI::reposition(x, y);
73
74         if(fade) fade->reposition_window(fade->get_x(), 
75                 y1 + y);
76
77         y1 += mwindow->theme->fade_h;
78
79         if(mode) mode->reposition_window(mode->get_x(),
80                 y1 + y);
81
82         if(nudge) nudge->reposition_window(nudge->get_x(), 
83                 y1 + y);
84
85
86         y1 += mwindow->theme->mode_h;
87
88         return y1;
89 }
90
91 int VPatchGUI::update(int x, int y)
92 {
93         int h = track->vertical_span(mwindow->theme);
94         int x1 = 0;
95         int y1 = PatchGUI::update(x, y);
96
97         if(fade)
98         {
99                 if(h - y1 < mwindow->theme->fade_h)
100                 {
101                         delete fade;
102                         fade = 0;
103                 }
104                 else
105                 {
106                         FloatAuto *previous = 0, *next = 0;
107                         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
108                         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
109                         unit_position = vtrack->to_units(unit_position, 0);
110                         int value = (int)((FloatAutos*)vtrack->automation->autos[AUTOMATION_FADE])->get_value(
111                                 (int64_t)unit_position,
112                                 PLAY_FORWARD, 
113                                 previous, 
114                                 next);
115                         fade->update(value);
116 //                      fade->update((int)fade->get_keyframe(mwindow, this)->value);
117                 }
118         }
119         else
120         if(h - y1 >= mwindow->theme->fade_h)
121         {
122                 patchbay->add_subwindow(fade = new VFadePatch(mwindow, 
123                         this, 
124                         x1 + x, 
125                         y1 + y, 
126                         patchbay->get_w() - 10));
127         }
128         y1 += mwindow->theme->fade_h;
129
130         if(mode)
131         {
132                 if(h - y1 < mwindow->theme->mode_h)
133                 {
134                         delete mode;
135                         mode = 0;
136                         delete nudge;
137                         nudge = 0;
138                 }
139                 else
140                 {
141                         mode->update(mode->get_keyframe(mwindow, this)->value);
142                         nudge->update();
143                 }
144         }
145         else
146         if(h - y1 >= mwindow->theme->mode_h)
147         {
148                 patchbay->add_subwindow(mode = new VModePatch(mwindow, 
149                         this, 
150                         x1 + x, 
151                         y1 + y));
152                 mode->create_objects();
153                 x1 += mode->get_w() + 10;
154                 patchbay->add_subwindow(nudge = new NudgePatch(mwindow,
155                         this,
156                         x1 + x,
157                         y1 + y,
158                         patchbay->get_w() - x1 - 10));
159         }
160
161
162
163
164
165         y1 += mwindow->theme->mode_h;
166         
167         return y1;
168 }
169
170
171
172 void VPatchGUI::synchronize_fade(float value_change)
173 {
174         if(fade && !change_source) 
175         {
176                 fade->update(Units::to_int64(fade->get_value() + value_change));
177                 fade->update_edl();
178         }
179 }
180
181
182 VFadePatch::VFadePatch(MWindow *mwindow, VPatchGUI *patch, int x, int y, int w)
183  : BC_ISlider(x, 
184                         y,
185                         0,
186                         w, 
187                         w, 
188                         0, 
189                         MAX_VIDEO_FADE, 
190                         (int64_t)get_keyframe(mwindow, patch)->value)
191 {
192         this->mwindow = mwindow;
193         this->patch = patch;
194 }
195
196 float VFadePatch::update_edl()
197 {
198         FloatAuto *current;
199         double position = mwindow->edl->local_session->get_selectionstart(1);
200         Autos *fade_autos = patch->vtrack->automation->autos[AUTOMATION_FADE];
201         int need_undo = !fade_autos->auto_exists_for_editing(position);
202
203         mwindow->undo->update_undo_before(_("fade"), need_undo ? 0 : this);
204
205         current = (FloatAuto*)fade_autos->get_auto_for_editing(position);
206
207         float result = get_value() - current->value;
208         current->value = get_value();
209
210         mwindow->undo->update_undo_after(_("fade"), LOAD_AUTOMATION);
211
212         return result;
213 }
214
215 int VFadePatch::handle_event()
216 {
217         if(shift_down())
218         {
219                 update(100);
220                 set_tooltip(get_caption());
221         }
222
223         patch->change_source = 1;
224
225         float change = update_edl();
226
227         if(patch->track->gang && patch->track->record) 
228                 patch->patchbay->synchronize_faders(change, TRACK_VIDEO, patch->track);
229
230         patch->change_source = 0;
231
232
233         mwindow->gui->unlock_window();
234         mwindow->restart_brender();
235         mwindow->sync_parameters(CHANGE_PARAMS);
236         mwindow->gui->lock_window("VFadePatch::handle_event");
237         if(mwindow->edl->session->auto_conf->autos[AUTOMATION_FADE])
238         {
239                 mwindow->gui->draw_overlays(1);
240         }
241         return 1;
242 }
243
244 FloatAuto* VFadePatch::get_keyframe(MWindow *mwindow, VPatchGUI *patch)
245 {
246         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
247         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
248         unit_position = patch->vtrack->to_units(unit_position, 0);
249         Auto *current = 0;
250
251         return (FloatAuto*)patch->vtrack->automation->autos[AUTOMATION_FADE]->get_prev_auto(
252                 (int64_t)unit_position, 
253                 PLAY_FORWARD,
254                 current);
255 }
256
257
258
259
260 VModePatch::VModePatch(MWindow *mwindow, VPatchGUI *patch, int x, int y)
261  : BC_PopupMenu(x, 
262         y,
263         patch->patchbay->mode_icons[0]->get_w() + 20,
264         "",
265         1,
266         mwindow->theme->get_image_set("mode_popup", 0),
267         10)
268 {
269         this->mwindow = mwindow;
270         this->patch = patch;
271         this->mode = get_keyframe(mwindow, patch)->value;
272         set_icon(patch->patchbay->mode_to_icon(this->mode));
273         set_tooltip("Overlay mode");
274 }
275
276 int VModePatch::handle_event()
277 {
278 // Set menu items
279 //      for(int i = 0; i < total_items(); i++)
280 //      {
281 //              VModePatchItem *item = (VModePatchItem*)get_item(i);
282 //              if(item->mode == mode) 
283 //                      item->set_checked(1);
284 //              else
285 //                      item->set_checked(0);
286 //      }
287         update(mode);
288
289 // Set keyframe
290         IntAuto *current;
291         double position = mwindow->edl->local_session->get_selectionstart(1);
292         Autos *mode_autos = patch->vtrack->automation->autos[AUTOMATION_MODE];
293         int need_undo = !mode_autos->auto_exists_for_editing(position);
294
295         mwindow->undo->update_undo_before(_("mode"), need_undo ? 0 : this);
296
297         current = (IntAuto*)mode_autos->get_auto_for_editing(position);
298         current->value = mode;
299
300         mwindow->undo->update_undo_after(_("mode"), LOAD_AUTOMATION);
301
302         mwindow->sync_parameters(CHANGE_PARAMS);
303
304         if(mwindow->edl->session->auto_conf->autos[AUTOMATION_MODE])
305         {
306                 mwindow->gui->draw_overlays(1);
307         }
308         mwindow->session->changes_made = 1;
309         return 1;
310 }
311
312 IntAuto* VModePatch::get_keyframe(MWindow *mwindow, VPatchGUI *patch)
313 {
314         Auto *current = 0;
315         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
316         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
317         unit_position = patch->vtrack->to_units(unit_position, 0);
318
319         return (IntAuto*)patch->vtrack->automation->autos[AUTOMATION_MODE]->get_prev_auto(
320                 (int64_t)unit_position, 
321                 PLAY_FORWARD,
322                 current);
323 }
324
325
326 void VModePatch::create_objects()
327 {
328         for( int mode=0; mode<TRANSFER_TYPES; ++mode )
329                 add_item(new VModePatchItem(this, mode_to_text(mode), mode));
330 }
331
332 void VModePatch::update(int mode)
333 {
334         set_icon(patch->patchbay->mode_to_icon(mode));
335         for(int i = 0; i < total_items(); i++)
336         {
337                 VModePatchItem *item = (VModePatchItem*)get_item(i);
338                 item->set_checked(item->mode == mode);
339         }
340 }
341
342
343 const char* VModePatch::mode_to_text(int mode)
344 {
345         switch(mode) {
346                 case TRANSFER_NORMAL:           return _("Normal");
347                 case TRANSFER_ADDITION:         return _("Addition");
348                 case TRANSFER_SUBTRACT:         return _("Subtract");
349                 case TRANSFER_MULTIPLY:         return _("Multiply");
350                 case TRANSFER_DIVIDE:           return _("Divide");
351                 case TRANSFER_REPLACE:          return _("Replace");
352                 case TRANSFER_MAX:              return _("Max");
353                 case TRANSFER_MIN:              return _("Min");
354                 case TRANSFER_AVERAGE:          return _("Average");
355                 case TRANSFER_DARKEN:           return _("Darken");
356                 case TRANSFER_LIGHTEN:          return _("Lighten");
357                 case TRANSFER_DST:              return _("Dst");
358                 case TRANSFER_DST_ATOP:         return _("DstAtop");
359                 case TRANSFER_DST_IN:           return _("DstIn");
360                 case TRANSFER_DST_OUT:          return _("DstOut");
361                 case TRANSFER_DST_OVER:         return _("DstOver");
362                 case TRANSFER_SRC:              return _("Src");
363                 case TRANSFER_SRC_ATOP:         return _("SrcAtop");
364                 case TRANSFER_SRC_IN:           return _("SrcIn");
365                 case TRANSFER_SRC_OUT:          return _("SrcOut");
366                 case TRANSFER_SRC_OVER:         return _("SrcOver");
367                 case TRANSFER_OR:               return _("Or");
368                 case TRANSFER_XOR:              return _("Xor");
369         }
370         return _("Normal");
371 }
372
373
374
375 VModePatchItem::VModePatchItem(VModePatch *popup, const char *text, int mode)
376  : BC_MenuItem(text)
377 {
378         this->popup = popup;
379         this->mode = mode;
380         if(this->mode == popup->mode) set_checked(1);
381 }
382
383 int VModePatchItem::handle_event()
384 {
385         popup->mode = mode;
386 //      popup->set_icon(popup->patch->patchbay->mode_to_icon(mode));
387         popup->handle_event();
388         return 1;
389 }