1095238feef887aa8b5f68ed09e8df320577986e
[goodguy/history.git] / cinelerra-5.1 / 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 "gwindowgui.h"
29 #include "intauto.h"
30 #include "intautos.h"
31 #include "language.h"
32 #include "localsession.h"
33 #include "mainsession.h"
34 #include "mainundo.h"
35 #include "mwindow.h"
36 #include "mwindowgui.h"
37 #include "overlayframe.inc"
38 #include "patchbay.h"
39 #include "theme.h"
40 #include "trackcanvas.h"
41 #include "vpatchgui.h"
42 #include "vtrack.h"
43 #include "vwindow.h"
44
45 #include <string.h>
46
47
48 VPatchGUI::VPatchGUI(MWindow *mwindow, PatchBay *patchbay, VTrack *track, int x, int y)
49  : PatchGUI(mwindow, patchbay, track, x, y)
50 {
51         data_type = TRACK_VIDEO;
52         this->vtrack = track;
53         mode = 0;
54         fade = 0;
55 }
56
57 VPatchGUI::~VPatchGUI()
58 {
59         if( fade ) delete fade;
60         if( mode ) delete mode;
61 }
62
63 void VPatchGUI::create_objects()
64 {
65         update(x, y);
66 }
67
68 int VPatchGUI::reposition(int x, int y)
69 {
70         //int x1 = 0;
71         int y1 = PatchGUI::reposition(x, y);
72
73         if( fade )
74                 fade->reposition_window(fade->get_x(), y1+y);
75         y1 += mwindow->theme->fade_h;
76         if( mix )
77                 mix->reposition_window(mix->get_x(), y1+y);
78         if( mode )
79                 mode->reposition_window(mode->get_x(), y1+y);
80         if( nudge )
81                 nudge->reposition_window(nudge->get_x(), y1+y);
82         y1 += mwindow->theme->mode_h;
83         return y1;
84 }
85
86 int VPatchGUI::update(int x, int y)
87 {
88         int h = track->vertical_span(mwindow->theme);
89         int x1 = 0;
90         int y1 = PatchGUI::update(x, y);
91
92         int y2 = y1 + mwindow->theme->fade_h;
93         if( fade ) {
94                 if( h < y2 ) {
95                         delete fade;  fade = 0;
96                 }
97                 else {
98                         fade->update(fade->get_w(), mwindow->get_float_auto(this, AUTOMATION_FADE)->get_value(),
99                                      mwindow->edl->local_session->automation_mins[AUTOGROUPTYPE_VIDEO_FADE],
100                                      mwindow->edl->local_session->automation_maxs[AUTOGROUPTYPE_VIDEO_FADE]);
101                 }
102         }
103         else if( h >= y2 ) {
104                 patchbay->add_subwindow(fade = new VFadePatch(mwindow, this, x1+x, y1+y,
105                         patchbay->get_w() - 10));
106         }
107         y1 = y2;
108
109         y2 = y1 + mwindow->theme->mode_h;
110         if( mode ) {
111                 if( h < y2 ) {
112                         delete mix;    mix = 0;
113                         delete mode;   mode = 0;
114                         delete nudge;  nudge = 0;
115                 }
116                 else {
117                         if( mwindow->session->selected_zwindow >= 0 ) {
118                                 int v = mwindow->mixer_track_active(track);
119                                 mix->update(v);
120                         }
121                         mode->update(mwindow->get_int_auto(this, AUTOMATION_MODE)->value);
122                         nudge->update();
123                 }
124         }
125         else if( h >= y2 ) {
126                 patchbay->add_subwindow(mix = new VMixPatch(mwindow, this, x1+x, y1+y+5));
127                 x1 += mix->get_w();
128                 patchbay->add_subwindow(mode = new VModePatch(mwindow, this, x1+x, y1+y));
129                 mode->create_objects();
130                 x1 += mode->get_w();
131                 patchbay->add_subwindow(nudge = new NudgePatch(mwindow, this, x1+x, y1+y,
132                         patchbay->get_w() - x1-x - 10));
133         }
134         y1 = y2;
135
136         return y1;
137 }
138
139
140 void VPatchGUI::synchronize_fade(float value_change)
141 {
142         if( fade && !change_source ) {
143                 fade->update(Units::to_int64(fade->get_value() + value_change));
144                 fade->update_edl();
145         }
146 }
147
148
149 VFadePatch::VFadePatch(MWindow *mwindow, VPatchGUI *patch, int x, int y, int w)
150  : BC_ISlider(x, y, 0, w, w,
151                 mwindow->edl->local_session->automation_mins[AUTOGROUPTYPE_VIDEO_FADE],
152                 mwindow->edl->local_session->automation_maxs[AUTOGROUPTYPE_VIDEO_FADE],
153                 (int64_t)mwindow->get_float_auto(patch,AUTOMATION_FADE)->get_value())
154 {
155         this->mwindow = mwindow;
156         this->patch = patch;
157 }
158
159 float VFadePatch::update_edl()
160 {
161         FloatAuto *current;
162         double position = mwindow->edl->local_session->get_selectionstart(1);
163         Autos *fade_autos = patch->vtrack->automation->autos[AUTOMATION_FADE];
164         int need_undo = !fade_autos->auto_exists_for_editing(position);
165
166         mwindow->undo->update_undo_before(_("fade"), need_undo ? 0 : this);
167
168         current = (FloatAuto*)fade_autos->get_auto_for_editing(position);
169
170         float result = get_value() - current->get_value();
171         current->set_value(get_value());
172
173         mwindow->undo->update_undo_after(_("fade"), LOAD_AUTOMATION);
174
175         return result;
176 }
177
178 int VFadePatch::handle_event()
179 {
180         if( shift_down() ) {
181                 update(100);
182                 set_tooltip(get_caption());
183         }
184
185         patch->change_source = 1;
186
187         float change = update_edl();
188
189         if( patch->track->gang && patch->track->record )
190                 patch->patchbay->synchronize_faders(change, TRACK_VIDEO, patch->track);
191
192         patch->change_source = 0;
193
194
195         mwindow->gui->unlock_window();
196         mwindow->restart_brender();
197         mwindow->sync_parameters(CHANGE_PARAMS);
198         mwindow->gui->lock_window("VFadePatch::handle_event");
199         if( mwindow->edl->session->auto_conf->autos[AUTOMATION_FADE] ) {
200                 mwindow->gui->draw_overlays(1);
201         }
202         return 1;
203 }
204
205
206 VKeyFadePatch::VKeyFadePatch(MWindow *mwindow, VPatchGUI *patch, int x, int y)
207  : BC_SubWindow(x,y, patch->patchbay->get_w(),20,
208         GWindowGUI::auto_colors[AUTOMATION_FADE])
209 {
210         this->mwindow = mwindow;
211         this->patch = patch;
212 }
213
214 void VKeyFadePatch::create_objects()
215 {
216         vkey_fade_value = new VKeyFadeValue(this);
217         add_subwindow(vkey_fade_value);
218         vkey_fade_value->activate();
219         show_window();
220 }
221
222 VKeyFadeValue::VKeyFadeValue(VKeyFadePatch *vkey_fade_patch)
223  : VFadePatch(vkey_fade_patch->mwindow, vkey_fade_patch->patch,
224         0,0, vkey_fade_patch->get_w())
225 {
226         this->vkey_fade_patch = vkey_fade_patch;
227 }
228
229 int VKeyFadeValue::button_release_event()
230 {
231         VFadePatch::button_release_event();
232         return 0;
233 }
234
235 int VKeyFadeValue::handle_event()
236 {
237         VPatchGUI *patch = vkey_fade_patch->patch;
238         int ret = VFadePatch::handle_event();
239         VFadePatch *fade = patch->fade;
240         if( fade )
241                 fade->update(get_value());
242         return ret;
243 }
244
245
246 VModePatch::VModePatch(MWindow *mwindow, VPatchGUI *patch, int x, int y)
247  : BC_PopupMenu(x, y, patch->patchbay->mode_icons[0]->get_w() + 20,
248         "", 1, mwindow->theme->get_image_set("mode_popup", 0), 10)
249 {
250         this->mwindow = mwindow;
251         this->patch = patch;
252         this->mode = mwindow->get_int_auto(patch, AUTOMATION_MODE)->value;
253         set_icon(patch->patchbay->mode_to_icon(this->mode));
254         set_tooltip(_("Overlay mode"));
255 }
256
257 VModePatch::VModePatch(MWindow *mwindow, VPatchGUI *patch)
258  : BC_PopupMenu(0, 0, 0, "", 0)
259 {
260         this->mwindow = mwindow;
261         this->patch = patch;
262         this->mode = mwindow->get_int_auto(patch, AUTOMATION_MODE)->value;
263 }
264
265
266 int VModePatch::handle_event()
267 {
268 // Set menu items
269 //      for( int i = 0; i < total_items(); i++ )
270 //      {
271 //              VModePatchItem *item = (VModePatchItem*)get_item(i);
272 //              if( item->mode == mode )
273 //                      item->set_checked(1);
274 //              else
275 //                      item->set_checked(0);
276 //      }
277         update(mode);
278
279 // Set keyframe
280         IntAuto *current;
281         double position = mwindow->edl->local_session->get_selectionstart(1);
282         Autos *mode_autos = patch->vtrack->automation->autos[AUTOMATION_MODE];
283         int need_undo = !mode_autos->auto_exists_for_editing(position);
284
285         mwindow->undo->update_undo_before(_("mode"), need_undo ? 0 : this);
286
287         current = (IntAuto*)mode_autos->get_auto_for_editing(position);
288         current->value = mode;
289
290         mwindow->undo->update_undo_after(_("mode"), LOAD_AUTOMATION);
291
292         mwindow->sync_parameters(CHANGE_PARAMS);
293
294         if( mwindow->edl->session->auto_conf->autos[AUTOMATION_MODE] ) {
295                 mwindow->gui->draw_overlays(1);
296         }
297         mwindow->session->changes_made = 1;
298         return 1;
299 }
300
301 void VModePatch::create_objects()
302 {
303         VModePatchItem *mode_item;
304         VModePatchSubMenu *submenu;
305         add_item(mode_item = new VModePatchItem(this, mode_to_text(TRANSFER_NORMAL),  TRANSFER_NORMAL));
306         add_item(mode_item = new VModePatchItem(this, _("Arithmetic..."), -1));
307         mode_item->add_submenu(submenu = new VModePatchSubMenu(mode_item));
308         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_ADDITION), TRANSFER_ADDITION));
309         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SUBTRACT), TRANSFER_SUBTRACT));
310         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DIVIDE),   TRANSFER_DIVIDE));
311         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_MULTIPLY), TRANSFER_MULTIPLY));
312         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_REPLACE),  TRANSFER_REPLACE));
313         add_item(mode_item = new VModePatchItem(this, _("PorterDuff..."), -1));
314         mode_item->add_submenu(submenu = new VModePatchSubMenu(mode_item));
315         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DST),      TRANSFER_DST));
316         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DST_ATOP), TRANSFER_DST_ATOP));
317         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DST_IN),   TRANSFER_DST_IN));
318         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DST_OUT),  TRANSFER_DST_OUT));
319         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DST_OVER), TRANSFER_DST_OVER));
320         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SRC),      TRANSFER_SRC));
321         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SRC_ATOP), TRANSFER_SRC_ATOP));
322         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SRC_IN),   TRANSFER_SRC_IN));
323         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SRC_OUT),  TRANSFER_SRC_OUT));
324         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SRC_OVER), TRANSFER_SRC_OVER));
325         add_item(mode_item = new VModePatchItem(this, _("Logical..."), -1));
326         mode_item->add_submenu(submenu = new VModePatchSubMenu(mode_item));
327         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_MIN),      TRANSFER_MIN));
328         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_MAX),      TRANSFER_MAX));
329         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DARKEN),   TRANSFER_DARKEN));
330         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_LIGHTEN),  TRANSFER_LIGHTEN));
331         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_AND),      TRANSFER_AND));
332         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_OR),       TRANSFER_OR));
333         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_XOR),      TRANSFER_XOR));
334         add_item(mode_item = new VModePatchItem(this, _("Graphic Art..."), -1));
335         mode_item->add_submenu(submenu = new VModePatchSubMenu(mode_item));
336         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_OVERLAY),  TRANSFER_OVERLAY));
337         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SCREEN),   TRANSFER_SCREEN));
338         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_BURN),     TRANSFER_BURN));
339         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DODGE),    TRANSFER_DODGE));
340         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_DIFFERENCE),TRANSFER_DIFFERENCE));
341         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_HARDLIGHT),TRANSFER_HARDLIGHT));
342         submenu->add_submenuitem(new VModeSubMenuItem(submenu, mode_to_text(TRANSFER_SOFTLIGHT),TRANSFER_SOFTLIGHT));
343 }
344
345 void VModePatch::update(int mode)
346 {
347         set_icon(patch->patchbay->mode_to_icon(mode));
348         for( int i = 0; i < total_items(); i++ ) {
349                 VModePatchItem *item = (VModePatchItem*)get_item(i);
350                 item->set_checked(item->mode == mode);
351                 VModePatchSubMenu *submenu = (VModePatchSubMenu *)item->get_submenu();
352                 if( !submenu ) continue;
353                 int n = submenu->total_items();
354                 for( int j=0; j<n; ++j ) {
355                         VModePatchItem *subitem = (VModePatchItem*)submenu->get_item(j);
356                         subitem->set_checked(subitem->mode == mode);
357                 }
358         }
359 }
360
361
362 const char* VModePatch::mode_to_text(int mode)
363 {
364         switch( mode ) {
365         case TRANSFER_NORMAL:           return _("Normal");
366         case TRANSFER_ADDITION:         return _("Addition");
367         case TRANSFER_SUBTRACT:         return _("Subtract");
368         case TRANSFER_MULTIPLY:         return _("Multiply");
369         case TRANSFER_DIVIDE:           return _("Divide");
370         case TRANSFER_REPLACE:          return _("Replace");
371         case TRANSFER_MAX:              return _("Max");
372         case TRANSFER_MIN:              return _("Min");
373         case TRANSFER_DARKEN:           return _("Darken");
374         case TRANSFER_LIGHTEN:          return _("Lighten");
375         case TRANSFER_DST:              return _("Dst");
376         case TRANSFER_DST_ATOP:         return _("DstAtop");
377         case TRANSFER_DST_IN:           return _("DstIn");
378         case TRANSFER_DST_OUT:          return _("DstOut");
379         case TRANSFER_DST_OVER:         return _("DstOver");
380         case TRANSFER_SRC:              return _("Src");
381         case TRANSFER_SRC_ATOP:         return _("SrcAtop");
382         case TRANSFER_SRC_IN:           return _("SrcIn");
383         case TRANSFER_SRC_OUT:          return _("SrcOut");
384         case TRANSFER_SRC_OVER:         return _("SrcOver");
385         case TRANSFER_AND:              return _("AND");
386         case TRANSFER_OR:               return _("OR");
387         case TRANSFER_XOR:              return _("XOR");
388         case TRANSFER_OVERLAY:          return _("Overlay");
389         case TRANSFER_SCREEN:           return _("Screen");
390         case TRANSFER_BURN:             return _("Burn");
391         case TRANSFER_DODGE:            return _("Dodge");
392         case TRANSFER_HARDLIGHT:        return _("Hardlight");
393         case TRANSFER_SOFTLIGHT:        return _("Softlight");
394         case TRANSFER_DIFFERENCE:       return _("Difference");
395         }
396         return _("Normal");
397 }
398
399
400
401 VModePatchItem::VModePatchItem(VModePatch *popup, const char *text, int mode)
402  : BC_MenuItem(text)
403 {
404         this->popup = popup;
405         this->mode = mode;
406         if( this->mode == popup->mode ) set_checked(1);
407 }
408
409 int VModePatchItem::handle_event()
410 {
411         if( mode >= 0 ) {
412                 popup->mode = mode;
413 //              popup->set_icon(popup->patch->patchbay->mode_to_icon(mode));
414                 popup->handle_event();
415         }
416         return 1;
417 }
418
419 VModePatchSubMenu::VModePatchSubMenu(VModePatchItem *mode_item)
420 {
421         this->mode_item = mode_item;
422 }
423 VModePatchSubMenu::~VModePatchSubMenu()
424 {
425 }
426
427 VModeSubMenuItem::VModeSubMenuItem(VModePatchSubMenu *submenu, const char *text, int mode)
428  : BC_MenuItem(text)
429 {
430         this->submenu = submenu;
431         this->mode = mode;
432         VModePatch *popup = submenu->mode_item->popup;
433         if( this->mode == popup->mode ) set_checked(1);
434 }
435 VModeSubMenuItem::~VModeSubMenuItem()
436 {
437 }
438
439 int VModeSubMenuItem::handle_event()
440 {
441         VModePatch *popup = submenu->mode_item->popup;
442         popup->mode = mode;
443 //      popup->set_icon(popup->patch->patchbay->mode_to_icon(mode));
444         popup->handle_event();
445         return 1;
446 }
447
448
449 VKeyModePatch::VKeyModePatch(MWindow *mwindow, VPatchGUI *patch)
450  : VModePatch(mwindow, patch)
451 {
452 }
453
454 int VKeyModePatch::button_release_event()
455 {
456         VModePatch::button_release_event();
457         return 0;
458 }
459
460 int VKeyModePatch::handle_event()
461 {
462         int ret = VModePatch::handle_event();
463         VModePatch *mode = patch->mode;
464         if( mode )
465                 mode->update(this->mode);
466         return ret;
467 }
468
469
470 VMixPatch::VMixPatch(MWindow *mwindow, VPatchGUI *patch, int x, int y)
471  : MixPatch(mwindow, patch, x, y)
472 {
473         set_tooltip(_("Mixer"));
474 }
475
476 VMixPatch::~VMixPatch()
477 {
478 }
479