prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / apatchgui.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 "apatchgui.h"
23 #include "apatchgui.inc"
24 #include "atrack.h"
25 #include "autoconf.h"
26 #include "automation.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "floatauto.h"
30 #include "floatautos.h"
31 #include "language.h"
32 #include "localsession.h"
33 #include "mainundo.h"
34 #include "mwindow.h"
35 #include "mwindowgui.h"
36 #include "panauto.h"
37 #include "panautos.h"
38 #include "patchbay.h"
39 #include "theme.h"
40 #include "trackcanvas.h"
41
42
43
44
45 APatchGUI::APatchGUI(MWindow *mwindow, 
46         PatchBay *patchbay, 
47         ATrack *track, 
48         int x, 
49         int y)
50  : PatchGUI(mwindow, 
51         patchbay, 
52         track, 
53         x, 
54         y)
55 {
56         data_type = TRACK_AUDIO;
57         this->atrack = track;
58         meter = 0;
59         pan = 0;
60         fade = 0;
61 }
62
63 APatchGUI::~APatchGUI()
64 {
65         if(fade) delete fade;
66         if(meter) delete meter;
67         if(pan) delete pan;
68 }
69
70 void APatchGUI::create_objects()
71 {
72         update(x, y);
73 }
74
75 int APatchGUI::reposition(int x, int y)
76 {
77         int y1 = PatchGUI::reposition(x, y);
78
79         if(fade) fade->reposition_window(fade->get_x(), 
80                 y1 + y);
81         y1 += mwindow->theme->fade_h;
82
83         if(meter) meter->reposition_window(meter->get_x(),
84                 y1 + y,
85                 -1,
86                 meter->get_w());
87         y1 += mwindow->theme->meter_h;
88
89         if(pan) pan->reposition_window(pan->get_x(),
90                 y1 + y);
91
92         if(nudge) nudge->reposition_window(nudge->get_x(),
93                 y1 + y);
94
95         y1 += mwindow->theme->pan_h;
96         return y1;
97 }
98
99 int APatchGUI::update(int x, int y)
100 {
101         int h = track->vertical_span(mwindow->theme);
102         int x1 = 0;
103         int y1 = PatchGUI::update(x, y);
104
105         if(fade)
106         {
107                 if(h - y1 < mwindow->theme->fade_h)
108                 {
109                         delete fade;
110                         fade = 0;
111                 }
112                 else
113                 {
114                         FloatAuto *previous = 0, *next = 0;
115                         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
116                         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
117                         unit_position = atrack->to_units(unit_position, 0);
118                         FloatAutos *ptr = (FloatAutos*)atrack->automation->autos[AUTOMATION_FADE];
119                         float value = ptr->get_value(
120                                 (long)unit_position,
121                                 PLAY_FORWARD, 
122                                 previous, 
123                                 next);
124                         fade->update(value);
125                 }
126         }
127         else
128         if(h - y1 >= mwindow->theme->fade_h)
129         {
130                 patchbay->add_subwindow(fade = new AFadePatch(mwindow, 
131                         this, 
132                         x1 + x, 
133                         y1 + y, 
134                         patchbay->get_w() - 10));
135         }
136         y1 += mwindow->theme->fade_h;
137
138         if(meter)
139         {
140                 if(h - y1 < mwindow->theme->meter_h)
141                 {
142                         delete meter;
143                         meter = 0;
144                 }
145         }
146         else
147         if(h - y1 >= mwindow->theme->meter_h)
148         {
149                 patchbay->add_subwindow(meter = new AMeterPatch(mwindow,
150                         this,
151                         x1 + x, 
152                         y1 + y));
153         }
154         y1 += mwindow->theme->meter_h;
155         x1 += 10;
156
157         if(pan)
158         {
159                 if(h - y1 < mwindow->theme->pan_h)
160                 {
161                         delete pan;
162                         pan = 0;
163                         delete nudge;
164                         nudge = 0;
165                 }
166                 else
167                 {
168                         if(pan->get_total_values() != mwindow->edl->session->audio_channels)
169                         {
170                                 pan->change_channels(mwindow->edl->session->audio_channels,
171                                         mwindow->edl->session->achannel_positions);
172                         }
173                         else
174                         {
175                                 int handle_x, handle_y;
176                                 PanAuto *previous = 0, *next = 0;
177                                 double unit_position = mwindow->edl->local_session->get_selectionstart(1);
178                                 unit_position = mwindow->edl->align_to_frame(unit_position, 0);
179                                 unit_position = atrack->to_units(unit_position, 0);
180                                 PanAutos *ptr = (PanAutos*)atrack->automation->autos[AUTOMATION_PAN];
181                                 ptr->get_handle(handle_x,
182                                         handle_y,
183                                         (long)unit_position, 
184                                         PLAY_FORWARD,
185                                         previous,
186                                         next);
187                                 pan->update(handle_x, handle_y);
188                         }
189                         nudge->update();
190                 }
191         }
192         else
193         if(h - y1 >= mwindow->theme->pan_h)
194         {
195                 patchbay->add_subwindow(pan = new APanPatch(mwindow,
196                         this,
197                         x1 + x, 
198                         y1 + y));
199                 x1 += pan->get_w() + 10;
200                 patchbay->add_subwindow(nudge = new NudgePatch(mwindow,
201                         this,
202                         x1 + x,
203                         y1 + y,
204                         patchbay->get_w() - x1 - 10));
205         }
206         y1 += mwindow->theme->pan_h;
207
208         return y1;
209 }
210
211 void APatchGUI::synchronize_fade(float value_change)
212 {
213         if(fade && !change_source) 
214         {
215                 fade->update(fade->get_value() + value_change);
216                 fade->update_edl();
217         }
218 }
219
220
221
222 AFadePatch::AFadePatch(MWindow *mwindow, APatchGUI *patch, int x, int y, int w)
223  : BC_FSlider(x, 
224                         y, 
225                         0, 
226                         w, 
227                         w, 
228                         (float)INFINITYGAIN, 
229                         (float)MAX_AUDIO_FADE, 
230                         get_keyframe(mwindow, patch)->value)
231 {
232         this->mwindow = mwindow;
233         this->patch = patch;
234 }
235
236 float AFadePatch::update_edl()
237 {
238         FloatAuto *current;
239         double position = mwindow->edl->local_session->get_selectionstart(1);
240         Autos *fade_autos = patch->atrack->automation->autos[AUTOMATION_FADE];
241         int need_undo = !fade_autos->auto_exists_for_editing(position);
242
243         mwindow->undo->update_undo_before(_("fade"), need_undo ? 0 : this);
244
245         current = (FloatAuto*)fade_autos->get_auto_for_editing(position);
246
247         float result = get_value() - current->value;
248         current->value = get_value();
249
250         mwindow->undo->update_undo_after(_("fade"), LOAD_AUTOMATION);
251
252         return result;
253 }
254
255
256 int AFadePatch::handle_event()
257 {
258         if(shift_down()) 
259         {
260                 update(0.0);
261                 set_tooltip(get_caption());
262         }
263
264         patch->change_source = 1;
265         float change = update_edl();
266         if(patch->track->gang && patch->track->record) 
267                 patch->patchbay->synchronize_faders(change, TRACK_AUDIO, patch->track);
268         patch->change_source = 0;
269
270         mwindow->sync_parameters(CHANGE_PARAMS);
271
272         if(mwindow->edl->session->auto_conf->autos[AUTOMATION_FADE])
273         {
274                 mwindow->gui->draw_overlays(1);
275         }
276         return 1;
277 }
278
279 FloatAuto* AFadePatch::get_keyframe(MWindow *mwindow, APatchGUI *patch)
280 {
281         Auto *current = 0;
282         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
283         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
284         unit_position = patch->atrack->to_units(unit_position, 0);
285
286         FloatAutos *ptr = (FloatAutos*)patch->atrack->automation->autos[AUTOMATION_FADE];
287         return (FloatAuto*)ptr->get_prev_auto(
288                 (long)unit_position, 
289                 PLAY_FORWARD,
290                 current);
291 }
292
293
294 APanPatch::APanPatch(MWindow *mwindow, APatchGUI *patch, int x, int y)
295  : BC_Pan(x, 
296                 y, 
297                 PAN_RADIUS, 
298                 MAX_PAN, 
299                 mwindow->edl->session->audio_channels, 
300                 mwindow->edl->session->achannel_positions, 
301                 get_keyframe(mwindow, patch)->handle_x, 
302                 get_keyframe(mwindow, patch)->handle_y,
303                 get_keyframe(mwindow, patch)->values)
304 {
305         this->mwindow = mwindow;
306         this->patch = patch;
307         set_tooltip(_("Pan"));
308 }
309
310 int APanPatch::handle_event()
311 {
312         PanAuto *current;
313         double position = mwindow->edl->local_session->get_selectionstart(1);
314         Autos *pan_autos = patch->atrack->automation->autos[AUTOMATION_PAN];
315         int need_undo = !pan_autos->auto_exists_for_editing(position);
316
317         mwindow->undo->update_undo_before(_("pan"), need_undo ? 0 : this);
318         
319         current = (PanAuto*)pan_autos->get_auto_for_editing(position);
320
321         current->handle_x = get_stick_x();
322         current->handle_y = get_stick_y();
323         memcpy(current->values, get_values(), sizeof(float) * mwindow->edl->session->audio_channels);
324
325         mwindow->undo->update_undo_after(_("pan"), LOAD_AUTOMATION);
326
327         mwindow->sync_parameters(CHANGE_PARAMS);
328
329         if(need_undo && mwindow->edl->session->auto_conf->autos[AUTOMATION_PAN])
330         {
331                 mwindow->gui->draw_overlays(1);
332         }
333         return 1;
334 }
335
336 PanAuto* APanPatch::get_keyframe(MWindow *mwindow, APatchGUI *patch)
337 {
338         Auto *current = 0;
339         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
340         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
341         unit_position = patch->atrack->to_units(unit_position, 0);
342
343         PanAutos *ptr = (PanAutos*)patch->atrack->automation->autos[AUTOMATION_PAN];
344         return (PanAuto*)ptr->get_prev_auto(
345                 (long)unit_position, 
346                 PLAY_FORWARD,
347                 current);
348 }
349
350
351
352
353 AMeterPatch::AMeterPatch(MWindow *mwindow, APatchGUI *patch, int x, int y)
354  : BC_Meter(x, 
355                         y, 
356                         METER_HORIZ, 
357                         patch->patchbay->get_w() - 10, 
358                         mwindow->edl->session->min_meter_db, 
359                         mwindow->edl->session->max_meter_db, 
360                         mwindow->edl->session->meter_format, 
361                         0,
362                         -1)
363 {
364         this->mwindow = mwindow;
365         this->patch = patch;
366         set_delays(TRACKING_RATE * 10,
367                         TRACKING_RATE);
368 }
369
370 int AMeterPatch::button_press_event()
371 {
372         if(cursor_inside() && is_event_win() && get_buttonpress() == 1)
373         {
374                 mwindow->reset_meters();
375                 return 1;
376         }
377
378         return 0;
379 }
380
381
382
383
384