r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / vpatchgui.C
blobcc4d5444cf4aea414ef94de92c39a6ef05e011db
1 #include "autoconf.h"
2 #include "automation.h"
3 #include "edl.h"
4 #include "edlsession.h"
5 #include "floatauto.h"
6 #include "floatautos.h"
7 #include "intauto.h"
8 #include "intautos.h"
9 #include "language.h"
10 #include "localsession.h"
11 #include "mainsession.h"
12 #include "mainundo.h"
13 #include "mwindow.h"
14 #include "mwindowgui.h"
15 #include "overlayframe.inc"
16 #include "patchbay.h"
17 #include "theme.h"
18 #include "trackcanvas.h"
19 #include "vpatchgui.h"
20 #include "vtrack.h"
22 #include <string.h>
28 VPatchGUI::VPatchGUI(MWindow *mwindow, PatchBay *patchbay, VTrack *track, int x, int y)
29  : PatchGUI(mwindow, patchbay, track, x, y)
31         data_type = TRACK_VIDEO;
32         this->vtrack = track;
33         mode = 0;
34         fade = 0;
37 VPatchGUI::~VPatchGUI()
39         if(fade) delete fade;
40         if(mode) delete mode;
43 int VPatchGUI::create_objects()
45         return update(x, y);
48 int VPatchGUI::reposition(int x, int y)
50         int x1 = 0;
51         int y1 = PatchGUI::reposition(x, y);
53         if(fade) fade->reposition_window(fade->get_x(), 
54                 y1 + y);
56         y1 += mwindow->theme->fade_h;
58         if(mode) mode->reposition_window(mode->get_x(),
59                 y1 + y);
61         if(nudge) nudge->reposition_window(nudge->get_x(), 
62                 y1 + y);
65         y1 += mwindow->theme->mode_h;
67         return y1;
70 int VPatchGUI::update(int x, int y)
72         int h = track->vertical_span(mwindow->theme);
73         int x1 = 0;
74         int y1 = PatchGUI::update(x, y);
76         if(fade)
77         {
78                 if(h - y1 < mwindow->theme->fade_h)
79                 {
80                         delete fade;
81                         fade = 0;
82                 }
83                 else
84                 {
85                         FloatAuto *previous = 0, *next = 0;
86                         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
87                         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
88                         unit_position = vtrack->to_units(unit_position, 0);
89                         int value = (int)((FloatAutos*)vtrack->automation->autos[AUTOMATION_FADE])->get_value(
90                                 (int64_t)unit_position,
91                                 PLAY_FORWARD, 
92                                 previous, 
93                                 next);
94                         fade->update(value);
95 //                      fade->update((int)fade->get_keyframe(mwindow, this)->value);
96                 }
97         }
98         else
99         if(h - y1 >= mwindow->theme->fade_h)
100         {
101                 patchbay->add_subwindow(fade = new VFadePatch(mwindow, 
102                         this, 
103                         x1 + x, 
104                         y1 + y, 
105                         patchbay->get_w() - 10));
106         }
107         y1 += mwindow->theme->fade_h;
109         if(mode)
110         {
111                 if(h - y1 < mwindow->theme->mode_h)
112                 {
113                         delete mode;
114                         mode = 0;
115                         delete nudge;
116                         nudge = 0;
117                 }
118                 else
119                 {
120                         mode->update(mode->get_keyframe(mwindow, this)->value);
121                         nudge->update();
122                 }
123         }
124         else
125         if(h - y1 >= mwindow->theme->mode_h)
126         {
127                 patchbay->add_subwindow(mode = new VModePatch(mwindow, 
128                         this, 
129                         x1 + x, 
130                         y1 + y));
131                 mode->create_objects();
132                 x1 += mode->get_w() + 10;
133                 patchbay->add_subwindow(nudge = new NudgePatch(mwindow,
134                         this,
135                         x1 + x,
136                         y1 + y,
137                         patchbay->get_w() - x1 - 10));
138         }
144         y1 += mwindow->theme->mode_h;
145         
146         return y1;
151 void VPatchGUI::synchronize_fade(float value_change)
153         if(fade && !change_source) 
154         {
155                 fade->update(Units::to_int64(fade->get_value() + value_change));
156                 fade->update_edl();
157         }
161 VFadePatch::VFadePatch(MWindow *mwindow, VPatchGUI *patch, int x, int y, int w)
162  : BC_ISlider(x, 
163                         y,
164                         0,
165                         w, 
166                         w, 
167                         0, 
168                         MAX_VIDEO_FADE, 
169                         (int64_t)get_keyframe(mwindow, patch)->value)
171         this->mwindow = mwindow;
172         this->patch = patch;
175 float VFadePatch::update_edl()
177         FloatAuto *current;
178         double position = mwindow->edl->local_session->get_selectionstart(1);
179         Autos *fade_autos = patch->vtrack->automation->autos[AUTOMATION_FADE];
180         int need_undo = !fade_autos->auto_exists_for_editing(position);
183         current = (FloatAuto*)fade_autos->get_auto_for_editing(position);
185         float result = get_value() - current->value;
186         current->value = get_value();
188         mwindow->undo->update_undo(_("fade"), LOAD_AUTOMATION, need_undo ? 0 : this);
190         return result;
193 int VFadePatch::handle_event()
195         if(shift_down())
196         {
197                 update(100);
198                 set_tooltip(get_caption());
199         }
201         patch->change_source = 1;
203         float change = update_edl();
205         if(patch->track->gang) 
206                 patch->patchbay->synchronize_faders(change, TRACK_VIDEO, patch->track);
208         patch->change_source = 0;
211         mwindow->gui->unlock_window();
212         mwindow->restart_brender();
213         mwindow->sync_parameters(CHANGE_PARAMS);
214         mwindow->gui->lock_window("VFadePatch::handle_event");
215         if(mwindow->edl->session->auto_conf->autos[AUTOMATION_FADE])
216         {
217                 mwindow->gui->canvas->draw_overlays();
218                 mwindow->gui->canvas->flash();
219         }
220         return 1;
223 FloatAuto* VFadePatch::get_keyframe(MWindow *mwindow, VPatchGUI *patch)
225         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
226         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
227         unit_position = patch->vtrack->to_units(unit_position, 0);
228         Auto *current = 0;
230         return (FloatAuto*)patch->vtrack->automation->autos[AUTOMATION_FADE]->get_prev_auto(
231                 (int64_t)unit_position, 
232                 PLAY_FORWARD,
233                 current);
239 VModePatch::VModePatch(MWindow *mwindow, VPatchGUI *patch, int x, int y)
240  : BC_PopupMenu(x, 
241         y,
242         patch->patchbay->mode_icons[0]->get_w() + 20,
243         "",
244         1,
245         mwindow->theme->get_image_set("mode_popup", 0),
246         10)
248         this->mwindow = mwindow;
249         this->patch = patch;
250         this->mode = get_keyframe(mwindow, patch)->value;
251         set_icon(patch->patchbay->mode_to_icon(this->mode));
252         set_tooltip("Overlay mode");
255 int VModePatch::handle_event()
257 // Set menu items
258 //      for(int i = 0; i < total_items(); i++)
259 //      {
260 //              VModePatchItem *item = (VModePatchItem*)get_item(i);
261 //              if(item->mode == mode) 
262 //                      item->set_checked(1);
263 //              else
264 //                      item->set_checked(0);
265 //      }
266         update(mode);
268 // Set keyframe
269         IntAuto *current;
270         double position = mwindow->edl->local_session->get_selectionstart(1);
271         Autos *mode_autos = patch->vtrack->automation->autos[AUTOMATION_MODE];
272         int need_undo = !mode_autos->auto_exists_for_editing(position);
275         current = (IntAuto*)mode_autos->get_auto_for_editing(position);
276         current->value = mode;
278         mwindow->undo->update_undo(_("mode"), LOAD_AUTOMATION, need_undo ? 0 : this);
280         mwindow->sync_parameters(CHANGE_PARAMS);
282         if(mwindow->edl->session->auto_conf->autos[AUTOMATION_MODE])
283         {
284                 mwindow->gui->canvas->draw_overlays();
285                 mwindow->gui->canvas->flash();
286         }
287         mwindow->session->changes_made = 1;
288         return 1;
291 IntAuto* VModePatch::get_keyframe(MWindow *mwindow, VPatchGUI *patch)
293         Auto *current = 0;
294         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
295         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
296         unit_position = patch->vtrack->to_units(unit_position, 0);
298         return (IntAuto*)patch->vtrack->automation->autos[AUTOMATION_MODE]->get_prev_auto(
299                 (int64_t)unit_position, 
300                 PLAY_FORWARD,
301                 current);
305 int VModePatch::create_objects()
307         add_item(new VModePatchItem(this, mode_to_text(TRANSFER_NORMAL), TRANSFER_NORMAL));
308         add_item(new VModePatchItem(this, mode_to_text(TRANSFER_ADDITION), TRANSFER_ADDITION));
309         add_item(new VModePatchItem(this, mode_to_text(TRANSFER_SUBTRACT), TRANSFER_SUBTRACT));
310         add_item(new VModePatchItem(this, mode_to_text(TRANSFER_MULTIPLY), TRANSFER_MULTIPLY));
311         add_item(new VModePatchItem(this, mode_to_text(TRANSFER_DIVIDE), TRANSFER_DIVIDE));
312         add_item(new VModePatchItem(this, mode_to_text(TRANSFER_REPLACE), TRANSFER_REPLACE));
313         add_item(new VModePatchItem(this, mode_to_text(TRANSFER_MAX), TRANSFER_MAX));
314         return 0;
317 void VModePatch::update(int mode)
319         set_icon(patch->patchbay->mode_to_icon(mode));
320         for(int i = 0; i < total_items(); i++)
321         {
322                 VModePatchItem *item = (VModePatchItem*)get_item(i);
323                 item->set_checked(item->mode == mode);
324         }
328 char* VModePatch::mode_to_text(int mode)
330         switch(mode)
331         {
332                 case TRANSFER_NORMAL:
333                         return _("Normal");
334                         break;
336                 case TRANSFER_REPLACE:
337                         return _("Replace");
338                         break;
340                 case TRANSFER_ADDITION:
341                         return _("Addition");
342                         break;
344                 case TRANSFER_SUBTRACT:
345                         return _("Subtract");
346                         break;
348                 case TRANSFER_MULTIPLY:
349                         return _("Multiply");
350                         break;
352                 case TRANSFER_DIVIDE:
353                         return _("Divide");
354                         break;
356                 case TRANSFER_MAX:
357                         return _("Max");
358                         break;
360                 default:
361                         return _("Normal");
362                         break;
363         }
364         return "";
372 VModePatchItem::VModePatchItem(VModePatch *popup, char *text, int mode)
373  : BC_MenuItem(text)
375         this->popup = popup;
376         this->mode = mode;
377         if(this->mode == popup->mode) set_checked(1);
380 int VModePatchItem::handle_event()
382         popup->mode = mode;
383 //      popup->set_icon(popup->patch->patchbay->mode_to_icon(mode));
384         popup->handle_event();
385         return 1;