r602: Fix baver's code... don't insert timecode when show_tc is not set
[cinelerra_cv/mob.git] / cinelerra / patchbay.C
blobadb3ff2cc492e468bfa8742ee1018f3720051fa8
1 #include "apatchgui.h"
2 #include "automation.h"
3 #include "floatauto.h"
4 #include "floatautos.h"
5 #include "clip.h"
6 #include "edl.h"
7 #include "edlsession.h"
8 #include "filexml.h"
9 #include "intauto.h"
10 #include "intautos.h"
11 #include "language.h"
12 #include "localsession.h"
13 #include "mainundo.h"
14 #include "mwindow.h"
15 #include "mwindowgui.h"
16 #include "patchbay.h"
17 #include "patchgui.h"
18 #include "mainsession.h"
19 #include "theme.h"
20 #include "track.h"
21 #include "trackcanvas.h"
22 #include "tracks.h"
23 #include "vpatchgui.h"
30 NudgePopup::NudgePopup(MWindow *mwindow, PatchBay *patchbay)
31  : BC_PopupMenu(0, 
32                 0, 
33                 0, 
34                 "", 
35                 0)
37         this->mwindow = mwindow;
38         this->patchbay = patchbay;
41 NudgePopup::~NudgePopup()
46 void NudgePopup::create_objects()
48         add_item(seconds_item = new NudgePopupSeconds(this));
49         add_item(native_item = new NudgePopupNative(this));
52 void NudgePopup::activate_menu(PatchGUI *gui)
54 // Set checked status
55         seconds_item->set_checked(mwindow->edl->session->nudge_seconds ? 1 : 0);
56         native_item->set_checked(mwindow->edl->session->nudge_seconds ? 0 : 1);
58 // Set native units to track format
59         native_item->set_text(gui->track->data_type == TRACK_AUDIO ? 
60                 (char*)"Samples" : 
61                 (char*)"Frames");
63 // Show it
64         BC_PopupMenu::activate_menu();
69 NudgePopupSeconds::NudgePopupSeconds(NudgePopup *popup)
70  : BC_MenuItem("Seconds")
72         this->popup = popup;
75 int NudgePopupSeconds::handle_event()
77         popup->mwindow->edl->session->nudge_seconds = 1;
78         popup->patchbay->update();
79         return 1;
86 NudgePopupNative::NudgePopupNative(NudgePopup *popup)
87  : BC_MenuItem("")
89         this->popup = popup;
92 int NudgePopupNative::handle_event()
94         popup->mwindow->edl->session->nudge_seconds = 0;
95         popup->patchbay->update();
96         return 1;
107 PatchBay::PatchBay(MWindow *mwindow, MWindowGUI *gui)
108  : BC_SubWindow(mwindow->theme->patchbay_x,
109         mwindow->theme->patchbay_y,
110         mwindow->theme->patchbay_w,
111         mwindow->theme->patchbay_h)
113         this->mwindow = mwindow;
114         this->gui = gui;
115         button_down = 0;
116         reconfigure_trigger = 0;
117         drag_operation = Tracks::NONE;
120 PatchBay::~PatchBay() 
125 int PatchBay::delete_all_patches()
127     patches.remove_all_objects();
128     return 0;
131 int PatchBay::create_objects()
133         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
134         flash();
136 // Create icons for mode types
137         mode_icons[TRANSFER_NORMAL] = new BC_Pixmap(this, 
138                 mwindow->theme->get_image("mode_normal"),
139                 PIXMAP_ALPHA);
140         mode_icons[TRANSFER_ADDITION] = new BC_Pixmap(this, 
141                 mwindow->theme->get_image("mode_add"),
142                 PIXMAP_ALPHA);
143         mode_icons[TRANSFER_SUBTRACT] = new BC_Pixmap(this, 
144                 mwindow->theme->get_image("mode_subtract"),
145                 PIXMAP_ALPHA);
146         mode_icons[TRANSFER_MULTIPLY] = new BC_Pixmap(this, 
147                 mwindow->theme->get_image("mode_multiply"),
148                 PIXMAP_ALPHA);
149         mode_icons[TRANSFER_DIVIDE] = new BC_Pixmap(this, 
150                 mwindow->theme->get_image("mode_divide"),
151                 PIXMAP_ALPHA);
152         mode_icons[TRANSFER_REPLACE] = new BC_Pixmap(this, 
153                 mwindow->theme->get_image("mode_replace"),
154                 PIXMAP_ALPHA);
155         
156         add_subwindow(nudge_popup = new NudgePopup(mwindow, this));
157         nudge_popup->create_objects();
159         return 0;
162 BC_Pixmap* PatchBay::mode_to_icon(int mode)
164         return mode_icons[mode];
167 int PatchBay::icon_to_mode(BC_Pixmap *icon)
169         for(int i = 0; i < TRANSFER_TYPES; i++)
170                 if(icon == mode_icons[i]) return i;
171         return TRANSFER_NORMAL;
174 void PatchBay::resize_event()
176         reposition_window(mwindow->theme->patchbay_x,
177                 mwindow->theme->patchbay_y,
178                 mwindow->theme->patchbay_w,
179                 mwindow->theme->patchbay_h);
180         draw_top_background(get_parent(), 0, 0, get_w(), get_h());
181         update();
182         flash();
185 int PatchBay::button_press_event()
187         int result = 0;
188 // Too much junk to support the wheel
189         return result;
193 Track *PatchBay::is_over_track()     // called from mwindow
195         int cursor_x = get_relative_cursor_x();
196         int cursor_y = get_relative_cursor_y();
197         Track *over_track = 0;
199         if(get_cursor_over_window() &&
200                 cursor_x >= 0 && 
201                 cursor_y >= 0 && 
202                 cursor_x < get_w() && 
203                 cursor_y < get_h())
204         {
205 // Get track we're inside of
206                 for(Track *track = mwindow->edl->tracks->first;
207                         track;
208                         track = track->next)
209                 {
210                         int y = track->y_pixel;
211                         int h = track->vertical_span(mwindow->theme);
212                         if(cursor_y >= y && cursor_y < y + h)
213                         {       
214                                 over_track = track;
215                         }
216                 }
217         }                               
218         return (over_track);
222 int PatchBay::cursor_motion_event()
224         int cursor_x = get_relative_cursor_x();
225         int cursor_y = get_relative_cursor_y();
226         int update_gui = 0;
228         if(drag_operation != Tracks::NONE)
229         {
230                 if(cursor_y >= 0 &&
231                         cursor_y < get_h())
232                 {
233 // Get track we're inside of
234                         for(Track *track = mwindow->edl->tracks->first;
235                                 track;
236                                 track = track->next)
237                         {
238                                 int y = track->y_pixel;
239                                 int h = track->vertical_span(mwindow->theme);
240                                 if(cursor_y >= y && cursor_y < y + h)
241                                 {
242                                         switch(drag_operation)
243                                         {
244                                                 case Tracks::PLAY:
245                                                         if(track->play != new_status)
246                                                         {
247                                                                 track->play = new_status;
248                                                                 mwindow->gui->unlock_window();
249                                                                 mwindow->restart_brender();
250                                                                 mwindow->sync_parameters(CHANGE_EDL);
251                                                                 mwindow->gui->lock_window();
252                                                                 update_gui = 1;
253                                                         }
254                                                         break;
255                                                 case Tracks::RECORD:
256                                                         if(track->record != new_status)
257                                                         {
258                                                                 track->record = new_status;
259                                                                 update_gui = 1;
260                                                         }
261                                                         break;
262                                                 case Tracks::GANG:
263                                                         if(track->gang != new_status)
264                                                         {
265                                                                 track->gang = new_status;
266                                                                 update_gui = 1;
267                                                         }
268                                                         break;
269                                                 case Tracks::DRAW:
270                                                         if(track->draw != new_status)
271                                                         {
272                                                                 track->draw = new_status;
273                                                                 update_gui = 1;
274                                                         }
275                                                         break;
276                                                 case Tracks::EXPAND:
277                                                         if(track->expand_view != new_status)
278                                                         {
279                                                                 track->expand_view = new_status;
280                                                                 mwindow->trackmovement(mwindow->edl->local_session->track_start);
281                                                                 update_gui = 0;
282                                                         }
283                                                         break;
284                                                 case Tracks::MUTE:
285                                                 {
286                                                         IntAuto *current = 0;
287                                                         Auto *keyframe = 0;
288                                                         double position = mwindow->edl->local_session->selectionstart;
289                                                         Autos *mute_autos = track->automation->mute_autos;
291                                                         current = (IntAuto*)mute_autos->get_prev_auto(PLAY_FORWARD, 
292                                                                 keyframe);
294                                                         if(current->value != new_status)
295                                                         {
296                                                                 mwindow->undo->update_undo_before(_("keyframe"), LOAD_AUTOMATION);
298                                                                 current = (IntAuto*)mute_autos->get_auto_for_editing(position);
300                                                                 current->value = new_status;
302                                                                 mwindow->undo->update_undo_after();
304                                                                 mwindow->gui->unlock_window();
305                                                                 mwindow->restart_brender();
306                                                                 mwindow->sync_parameters(CHANGE_PARAMS);
307                                                                 mwindow->gui->lock_window();
309                                                                 if(mwindow->edl->session->auto_conf->mute)
310                                                                 {
311                                                                         mwindow->gui->canvas->draw_overlays();
312                                                                         mwindow->gui->canvas->flash();
313                                                                 }
314                                                                 update_gui = 1;
315                                                         }
316                                                         break;
317                                                 }
318                                         }
319                                 }
320                         }
321                 }
322         }
324         if(update_gui)
325         {
326                 update();
327         }
328         return 0;
331 void PatchBay::change_meter_format(int mode, int min, int max)
333         for(int i = 0; i < patches.total; i++)
334         {
335                 PatchGUI *patchgui = patches.values[i];
336                 if(patchgui->data_type == TRACK_AUDIO)
337                 {
338                         APatchGUI *apatchgui = (APatchGUI*)patchgui;
339                         if(apatchgui->meter)
340                         {
341                                 apatchgui->meter->change_format(mode, min, max);
342                         }
343                 }
344         }
347 void PatchBay::update_meters(ArrayList<double> *module_levels)
349         for(int level_number = 0, patch_number = 0;
350                 patch_number < patches.total && level_number < module_levels->total;
351                 patch_number++)
352         {
353                 APatchGUI *patchgui = (APatchGUI*)patches.values[patch_number];
355                 if(patchgui->data_type == TRACK_AUDIO)
356                 {
357                         if(patchgui->meter)
358                         {
359                                 double level = module_levels->values[level_number];
360                                 patchgui->meter->update(level, level > 1);
361                         }
363                         level_number++;
364                 }
365         }
368 void PatchBay::reset_meters()
370         for(int patch_number = 0;
371                 patch_number < patches.total;
372                 patch_number++)
373         {
374                 APatchGUI *patchgui = (APatchGUI*)patches.values[patch_number];
375                 if(patchgui->data_type == TRACK_AUDIO && patchgui->meter)
376                 {
377                         patchgui->meter->reset_over();
378                 }
379         }
382 void PatchBay::stop_meters()
384         for(int patch_number = 0;
385                 patch_number < patches.total;
386                 patch_number++)
387         {
388                 APatchGUI *patchgui = (APatchGUI*)patches.values[patch_number];
389                 if(patchgui->data_type == TRACK_AUDIO && patchgui->meter)
390                 {
391                         patchgui->meter->reset();
392                 }
393         }
397 #define PATCH_X 3
399 int PatchBay::update()
401         int patch_count = 0;
403 // Every patch has a GUI regardless of whether or not it is visible.
404 // Make sure GUI's are allocated for every patch and deleted for non-existant
405 // patches.
406         for(Track *current = mwindow->edl->tracks->first;
407                 current;
408                 current = NEXT, patch_count++)
409         {
410                 PatchGUI *patchgui;
411                 int y = current->y_pixel;
413                 if(patches.total > patch_count)
414                 {
415                         if(patches.values[patch_count]->track_id != current->get_id())
416                         {
417                                 delete patches.values[patch_count];
419                                 switch(current->data_type)
420                                 {
421                                         case TRACK_AUDIO:
422                                                 patchgui = patches.values[patch_count] = new APatchGUI(mwindow, this, (ATrack*)current, PATCH_X, y);
423                                                 break;
424                                         case TRACK_VIDEO:
425                                                 patchgui = patches.values[patch_count] = new VPatchGUI(mwindow, this, (VTrack*)current, PATCH_X, y);
426                                                 break;
427                                 }
428                                 patchgui->create_objects();
429                         }
430                         else
431                         {
432                                 patches.values[patch_count]->update(PATCH_X, y);
433                         }
434                 }
435                 else
436                 {
437                         switch(current->data_type)
438                         {
439                                 case TRACK_AUDIO:
440                                         patchgui = new APatchGUI(mwindow, this, (ATrack*)current, PATCH_X, y);
441                                         break;
442                                 case TRACK_VIDEO:
443                                         patchgui = new VPatchGUI(mwindow, this, (VTrack*)current, PATCH_X, y);
444                                         break;
445                         }
446                         patches.append(patchgui);
447                         patchgui->create_objects();
448                 }
449         }
451         while(patches.total > patch_count)
452         {
453                 delete patches.values[patches.total - 1];
454                 patches.remove_number(patches.total - 1);
455         }
457         return 0;
460 void PatchBay::synchronize_faders(float change, int data_type, Track *skip)
462         for(Track *current = mwindow->edl->tracks->first;
463                 current;
464                 current = NEXT)
465         {
466                 if(current->data_type == data_type &&
467                         current->gang && 
468                         current->record && 
469                         current != skip)
470                 {
471                         FloatAutos *fade_autos = current->automation->fade_autos;
472                         double position = mwindow->edl->local_session->selectionstart;
473                         int update_undo = !fade_autos->auto_exists_for_editing(position);
475                         if(update_undo)
476                                 mwindow->undo->update_undo_before(_("fade"), LOAD_AUTOMATION);
478                         FloatAuto *keyframe = (FloatAuto*)fade_autos->get_auto_for_editing(position);
480                         keyframe->value += change;
481                         if(data_type == TRACK_AUDIO)
482                                 CLAMP(keyframe->value, INFINITYGAIN, MAX_AUDIO_FADE);
483                         else
484                                 CLAMP(keyframe->value, 0, MAX_VIDEO_FADE);
485                         if(update_undo)
486                                 mwindow->undo->update_undo_after();
489                         for(int i = 0; i < patches.total; i++)
490                         {
491                                 if(patches.values[i]->track == current)
492                                         patches.values[i]->update(patches.values[i]->x,
493                                                 patches.values[i]->y);
494                         }
495                 }
496         }
501 int PatchBay::resize_event(int top, int bottom)
503         reposition_window(mwindow->theme->patchbay_x,
504                 mwindow->theme->patchbay_y,
505                 mwindow->theme->patchbay_w,
506                 mwindow->theme->patchbay_h);
507         return 0;