add prof sigusr1 feature, tweak amdgup tooltip, fix colorpicker handle_new_color...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / cwindowtool.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2008-2017 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include <stdio.h>
22 #include <stdint.h>
23
24 #include "automation.h"
25 #include "bccolors.h"
26 #include "bctimer.h"
27 #include "clip.h"
28 #include "condition.h"
29 #include "cpanel.h"
30 #include "cplayback.h"
31 #include "cwindow.h"
32 #include "cwindowgui.h"
33 #include "cwindowtool.h"
34 #include "edl.h"
35 #include "edlsession.h"
36 #include "floatauto.h"
37 #include "floatautos.h"
38 #include "keys.h"
39 #include "language.h"
40 #include "localsession.h"
41 #include "mainsession.h"
42 #include "mainundo.h"
43 #include "maskauto.h"
44 #include "maskautos.h"
45 #include "mutex.h"
46 #include "mwindow.h"
47 #include "mwindowgui.h"
48 #include "theme.h"
49 #include "track.h"
50 #include "tracks.h"
51 #include "trackcanvas.h"
52 #include "transportque.h"
53
54
55 CWindowTool::CWindowTool(MWindow *mwindow, CWindowGUI *gui)
56  : Thread(1, 0, 0)
57 {
58         this->mwindow = mwindow;
59         this->gui = gui;
60         tool_gui = 0;
61         done = 0;
62         current_tool = CWINDOW_NONE;
63         set_synchronous(1);
64         input_lock = new Condition(0, "CWindowTool::input_lock");
65         output_lock = new Condition(1, "CWindowTool::output_lock");
66         tool_gui_lock = new Mutex("CWindowTool::tool_gui_lock");
67 }
68
69 CWindowTool::~CWindowTool()
70 {
71         done = 1;
72         stop_tool();
73         input_lock->unlock();
74         Thread::join();
75         delete input_lock;
76         delete output_lock;
77         delete tool_gui_lock;
78 }
79
80 void CWindowTool::start_tool(int operation)
81 {
82         CWindowToolGUI *new_gui = 0;
83         int result = 0;
84
85 //printf("CWindowTool::start_tool 1\n");
86         if(current_tool != operation)
87         {
88                 int previous_tool = current_tool;
89                 current_tool = operation;
90                 switch(operation)
91                 {
92                         case CWINDOW_EYEDROP:
93                                 new_gui = new CWindowEyedropGUI(mwindow, this);
94                                 break;
95                         case CWINDOW_CROP:
96                                 new_gui = new CWindowCropGUI(mwindow, this);
97                                 break;
98                         case CWINDOW_CAMERA:
99                                 new_gui = new CWindowCameraGUI(mwindow, this);
100                                 break;
101                         case CWINDOW_PROJECTOR:
102                                 new_gui = new CWindowProjectorGUI(mwindow, this);
103                                 break;
104                         case CWINDOW_MASK:
105                                 new_gui = new CWindowMaskGUI(mwindow, this);
106                                 break;
107                         case CWINDOW_RULER:
108                                 new_gui = new CWindowRulerGUI(mwindow, this);
109                                 break;
110                         case CWINDOW_PROTECT:
111                                 mwindow->edl->session->tool_window = 0;
112                                 gui->composite_panel->operation[CWINDOW_TOOL_WINDOW]->update(0);
113                                 // fall thru
114                         default:
115                                 result = 1;
116                                 stop_tool();
117                                 break;
118                 }
119
120 //printf("CWindowTool::start_tool 1\n");
121
122
123                 if(!result)
124                 {
125                         stop_tool();
126 // Wait for previous tool GUI to finish
127                         output_lock->lock("CWindowTool::start_tool");
128                         this->tool_gui = new_gui;
129                         tool_gui->create_objects();
130                         if( previous_tool == CWINDOW_PROTECT || previous_tool == CWINDOW_NONE ) {
131                                 mwindow->edl->session->tool_window = 1;
132                                 gui->composite_panel->operation[CWINDOW_TOOL_WINDOW]->update(1);
133                         }
134                         update_show_window();
135
136 // Signal thread to run next tool GUI
137                         input_lock->unlock();
138                 }
139 //printf("CWindowTool::start_tool 1\n");
140         }
141         else
142         if(tool_gui)
143         {
144                 tool_gui->lock_window("CWindowTool::start_tool");
145                 tool_gui->update();
146                 tool_gui->unlock_window();
147         }
148
149 //printf("CWindowTool::start_tool 2\n");
150
151 }
152
153
154 void CWindowTool::stop_tool()
155 {
156         if(tool_gui)
157         {
158                 tool_gui->lock_window("CWindowTool::stop_tool");
159                 tool_gui->set_done(0);
160                 tool_gui->unlock_window();
161         }
162 }
163
164 void CWindowTool::show_tool()
165 {
166         if(tool_gui && mwindow->edl->session->tool_window)
167         {
168                 tool_gui->lock_window("CWindowTool::show_tool");
169                 tool_gui->show_window();
170                 tool_gui->unlock_window();
171         }
172 }
173
174 void CWindowTool::hide_tool()
175 {
176         if(tool_gui && mwindow->edl->session->tool_window)
177         {
178                 tool_gui->lock_window("CWindowTool::show_tool");
179                 tool_gui->hide_window();
180                 tool_gui->unlock_window();
181         }
182 }
183
184 void CWindowTool::raise_tool()
185 {
186         if(tool_gui && mwindow->edl->session->tool_window)
187         {
188                 tool_gui->lock_window("CWindowTool::show_tool");
189                 tool_gui->raise_window();
190                 tool_gui->unlock_window();
191         }
192 }
193
194
195 void CWindowTool::run()
196 {
197         while(!done)
198         {
199                 input_lock->lock("CWindowTool::run");
200                 if(!done)
201                 {
202                         tool_gui->run_window();
203                         tool_gui_lock->lock("CWindowTool::run");
204                         delete tool_gui;
205                         tool_gui = 0;
206                         tool_gui_lock->unlock();
207                 }
208                 output_lock->unlock();
209         }
210 }
211
212 void CWindowTool::update_show_window()
213 {
214         if(tool_gui)
215         {
216                 tool_gui->lock_window("CWindowTool::update_show_window");
217
218                 if(mwindow->edl->session->tool_window)
219                 {
220                         tool_gui->update();
221                         tool_gui->show_window();
222                 }
223                 else
224                         tool_gui->hide_window();
225                 tool_gui->flush();
226
227                 tool_gui->unlock_window();
228         }
229 }
230
231 void CWindowTool::raise_window()
232 {
233         if(tool_gui)
234         {
235                 gui->unlock_window();
236                 tool_gui->lock_window("CWindowTool::raise_window");
237                 tool_gui->raise_window();
238                 tool_gui->unlock_window();
239                 gui->lock_window("CWindowTool::raise_window");
240         }
241 }
242
243 void CWindowTool::update_values()
244 {
245         tool_gui_lock->lock("CWindowTool::update_values");
246         if(tool_gui)
247         {
248                 tool_gui->lock_window("CWindowTool::update_values");
249                 tool_gui->update();
250                 tool_gui->flush();
251                 tool_gui->unlock_window();
252         }
253         tool_gui_lock->unlock();
254 }
255
256
257
258
259
260
261
262 CWindowToolGUI::CWindowToolGUI(MWindow *mwindow,
263         CWindowTool *thread,
264         const char *title,
265         int w,
266         int h)
267  : BC_Window(title,
268         mwindow->session->ctool_x,
269         mwindow->session->ctool_y,
270         w,
271         h,
272         w,
273         h,
274         0,
275         0,
276         1)
277 {
278         this->mwindow = mwindow;
279         this->thread = thread;
280         current_operation = 0;
281 }
282
283 CWindowToolGUI::~CWindowToolGUI()
284 {
285 }
286
287 int CWindowToolGUI::close_event()
288 {
289         hide_window();
290         flush();
291         mwindow->edl->session->tool_window = 0;
292         unlock_window();
293         thread->gui->lock_window("CWindowToolGUI::close_event");
294         thread->gui->composite_panel->set_operation(mwindow->edl->session->cwindow_operation);
295         thread->gui->flush();
296         thread->gui->unlock_window();
297         lock_window("CWindowToolGUI::close_event");
298         return 1;
299 }
300
301 int CWindowToolGUI::keypress_event()
302 {
303         int result = 0;
304
305         switch( get_keypress() ) {
306         case 'w':
307         case 'W':
308                 return close_event();
309         case KEY_F1:
310         case KEY_F2:
311         case KEY_F3:
312         case KEY_F4:
313         case KEY_F5:
314         case KEY_F6:
315         case KEY_F7:
316         case KEY_F8:
317         case KEY_F9:
318         case KEY_F10:
319         case KEY_F11:
320         case KEY_F12:
321                 resend_event(thread->gui);
322                 result = 1;
323         }
324
325         return result;
326 }
327
328 int CWindowToolGUI::translation_event()
329 {
330         mwindow->session->ctool_x = get_x();
331         mwindow->session->ctool_y = get_y();
332         return 0;
333 }
334
335
336 void CWindowToolGUI::update_preview(int changed_edl)
337 {
338         unlock_window();
339         draw_preview(changed_edl);
340         lock_window("CWindowToolGUI::update_preview");
341 }
342
343 void CWindowToolGUI::draw_preview(int changed_edl)
344 {
345         CWindowGUI *cgui = mwindow->cwindow->gui;
346         cgui->lock_window("CWindowToolGUI::draw_preview");
347         int change_type = !changed_edl ? CHANGE_PARAMS : CHANGE_EDL;
348         cgui->sync_parameters(change_type, 0, 1);
349         cgui->unlock_window();
350 }
351
352
353 CWindowCoord::CWindowCoord(CWindowToolGUI *gui, int x, int y, float value, int log_increment = 0)
354  : BC_TumbleTextBox(gui, (float)value, (float)-65536, (float)65536, x, y, 100, 3)
355 {
356         this->gui = gui;
357         set_log_floatincrement(log_increment);
358 }
359
360 CWindowCoord::CWindowCoord(CWindowToolGUI *gui, int x, int y, int value)
361  : BC_TumbleTextBox(gui, (int64_t)value, (int64_t)-65536, (int64_t)65536, x, y, 100, 3)
362 {
363         this->gui = gui;
364 }
365 int CWindowCoord::handle_event()
366 {
367         gui->event_caller = this;
368         gui->handle_event();
369         return 1;
370 }
371
372
373 CWindowCropOK::CWindowCropOK(MWindow *mwindow, CWindowToolGUI *gui, int x, int y)
374  : BC_GenericButton(x, y, _("Do it"))
375 {
376         this->mwindow = mwindow;
377         this->gui = gui;
378 }
379 int CWindowCropOK::handle_event()
380 {
381         mwindow->crop_video();
382         return 1;
383 }
384
385
386 int CWindowCropOK::keypress_event()
387 {
388         if(get_keypress() == 0xd)
389         {
390                 handle_event();
391                 return 1;
392         }
393         return 0;
394 }
395
396
397
398
399
400
401
402 CWindowCropGUI::CWindowCropGUI(MWindow *mwindow, CWindowTool *thread)
403  : CWindowToolGUI(mwindow,
404         thread,
405         _(PROGRAM_NAME ": Crop"),
406         330,
407         100)
408 {
409 }
410
411
412 CWindowCropGUI::~CWindowCropGUI()
413 {
414 }
415
416 void CWindowCropGUI::create_objects()
417 {
418         int x = 10, y = 10;
419         BC_Title *title;
420
421         lock_window("CWindowCropGUI::create_objects");
422         int column1 = 0;
423         int pad = MAX(BC_TextBox::calculate_h(this, MEDIUMFONT, 1, 1),
424                 BC_Title::calculate_h(this, "X")) + 5;
425         add_subwindow(title = new BC_Title(x, y, "X1:"));
426         column1 = MAX(column1, title->get_w());
427         y += pad;
428         add_subwindow(title = new BC_Title(x, y, _("W:")));
429         column1 = MAX(column1, title->get_w());
430         y += pad;
431         add_subwindow(new CWindowCropOK(mwindow, thread->tool_gui, x, y));
432
433         x += column1 + 5;
434         y = 10;
435         x1 = new CWindowCoord(thread->tool_gui, x, y,
436                 mwindow->edl->session->crop_x1);
437         x1->create_objects();
438         y += pad;
439         width = new CWindowCoord(thread->tool_gui, x, y,
440                 mwindow->edl->session->crop_x2 - mwindow->edl->session->crop_x1);
441         width->create_objects();
442
443
444         x += x1->get_w() + 10;
445         y = 10;
446         int column2 = 0;
447         add_subwindow(title = new BC_Title(x, y, "Y1:"));
448         column2 = MAX(column2, title->get_w());
449         y += pad;
450         add_subwindow(title = new BC_Title(x, y, _("H:")));
451         column2 = MAX(column2, title->get_w());
452         y += pad;
453
454         y = 10;
455         x += column2 + 5;
456         y1 = new CWindowCoord(thread->tool_gui, x, y,
457                 mwindow->edl->session->crop_y1);
458         y1->create_objects();
459         y += pad;
460         height = new CWindowCoord(thread->tool_gui, x, y,
461                 mwindow->edl->session->crop_y2 - mwindow->edl->session->crop_y1);
462         height->create_objects();
463         unlock_window();
464 }
465
466 void CWindowCropGUI::handle_event()
467 {
468         int new_x1, new_y1;
469         new_x1 = atol(x1->get_text());
470         new_y1 = atol(y1->get_text());
471         if(new_x1 != mwindow->edl->session->crop_x1)
472         {
473                 mwindow->edl->session->crop_x2 = new_x1 +
474                         mwindow->edl->session->crop_x2 -
475                         mwindow->edl->session->crop_x1;
476                 mwindow->edl->session->crop_x1 = new_x1;
477         }
478         if(new_y1 != mwindow->edl->session->crop_y1)
479         {
480                 mwindow->edl->session->crop_y2 = new_y1 +
481                         mwindow->edl->session->crop_y2 -
482                         mwindow->edl->session->crop_y1;
483                 mwindow->edl->session->crop_y1 = atol(y1->get_text());
484         }
485         mwindow->edl->session->crop_x2 = atol(width->get_text()) +
486                 mwindow->edl->session->crop_x1;
487         mwindow->edl->session->crop_y2 = atol(height->get_text()) +
488                 mwindow->edl->session->crop_y1;
489         update();
490         mwindow->cwindow->gui->canvas->redraw(1);
491 }
492
493 void CWindowCropGUI::update()
494 {
495         x1->update((int64_t)mwindow->edl->session->crop_x1);
496         y1->update((int64_t)mwindow->edl->session->crop_y1);
497         width->update((int64_t)mwindow->edl->session->crop_x2 -
498                 mwindow->edl->session->crop_x1);
499         height->update((int64_t)mwindow->edl->session->crop_y2 -
500                 mwindow->edl->session->crop_y1);
501 }
502
503
504 CWindowEyedropGUI::CWindowEyedropGUI(MWindow *mwindow, CWindowTool *thread)
505  : CWindowToolGUI(mwindow, thread, _(PROGRAM_NAME ": Color"), 220, 290)
506 {
507 }
508
509 CWindowEyedropGUI::~CWindowEyedropGUI()
510 {
511 }
512
513 void CWindowEyedropGUI::create_objects()
514 {
515         int margin = mwindow->theme->widget_border;
516         int x = 10 + margin;
517         int y = 10 + margin;
518         int x2 = 70, x3 = x2 + 60;
519         lock_window("CWindowEyedropGUI::create_objects");
520         BC_Title *title0, *title1, *title2, *title3, *title4, *title5, *title6, *title7;
521         add_subwindow(title0 = new BC_Title(x, y,_("X,Y:")));
522         y += title0->get_h() + margin;
523         add_subwindow(title7 = new BC_Title(x, y, _("Radius:")));
524         y += BC_TextBox::calculate_h(this, MEDIUMFONT, 1, 1) + margin;
525
526         add_subwindow(title1 = new BC_Title(x, y, _("Red:")));
527         y += title1->get_h() + margin;
528         add_subwindow(title2 = new BC_Title(x, y, _("Green:")));
529         y += title2->get_h() + margin;
530         add_subwindow(title3 = new BC_Title(x, y, _("Blue:")));
531         y += title3->get_h() + margin;
532
533         add_subwindow(title4 = new BC_Title(x, y, "Y:"));
534         y += title4->get_h() + margin;
535         add_subwindow(title5 = new BC_Title(x, y, "U:"));
536         y += title5->get_h() + margin;
537         add_subwindow(title6 = new BC_Title(x, y, "V:"));
538
539         add_subwindow(current = new BC_Title(x2, title0->get_y(), ""));
540
541         radius = new CWindowCoord(this, x2, title7->get_y(),
542                 mwindow->edl->session->eyedrop_radius);
543         radius->create_objects();
544         radius->set_boundaries((int64_t)0, (int64_t)255);
545
546         add_subwindow(red = new BC_Title(x2, title1->get_y(), "0"));
547         add_subwindow(green = new BC_Title(x2, title2->get_y(), "0"));
548         add_subwindow(blue = new BC_Title(x2, title3->get_y(), "0"));
549         add_subwindow(rgb_hex = new BC_Title(x3, red->get_y(), "#000000"));
550
551         add_subwindow(this->y = new BC_Title(x2, title4->get_y(), "0"));
552         add_subwindow(this->u = new BC_Title(x2, title5->get_y(), "0"));
553         add_subwindow(this->v = new BC_Title(x2, title6->get_y(), "0"));
554         add_subwindow(yuv_hex = new BC_Title(x3, this->y->get_y(), "#000000"));
555
556         y = title6->get_y() + this->v->get_h() + 2*margin;
557         add_subwindow(sample = new BC_SubWindow(x, y, 50, 50));
558         y += sample->get_h() + margin;
559         add_subwindow(use_max = new CWindowEyedropCheckBox(mwindow, this, x, y));
560         update();
561         unlock_window();
562 }
563
564 void CWindowEyedropGUI::update()
565 {
566         char string[BCTEXTLEN];
567         sprintf(string, "%d, %d",
568                 thread->gui->eyedrop_x,
569                 thread->gui->eyedrop_y);
570         current->update(string);
571
572         radius->update((int64_t)mwindow->edl->session->eyedrop_radius);
573
574         LocalSession *local_session = mwindow->edl->local_session;
575         int use_max = local_session->use_max;
576         float r = use_max ? local_session->red_max : local_session->red;
577         float g = use_max ? local_session->green_max : local_session->green;
578         float b = use_max ? local_session->blue_max : local_session->blue;
579         this->red->update(r);
580         this->green->update(g);
581         this->blue->update(b);
582
583         int rx = 255*r + 0.5;  bclamp(rx,0,255);
584         int gx = 255*g + 0.5;  bclamp(gx,0,255);
585         int bx = 255*b + 0.5;  bclamp(bx,0,255);
586         char rgb_text[BCSTRLEN];
587         sprintf(rgb_text, "#%02x%02x%02x", rx, gx, bx);
588         rgb_hex->update(rgb_text);
589         
590         float y, u, v;
591         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
592         this->y->update(y);
593         this->u->update(u);  u += 0.5;
594         this->v->update(v);  v += 0.5;
595
596         int yx = 255*y + 0.5;  bclamp(yx,0,255);
597         int ux = 255*u + 0.5;  bclamp(ux,0,255);
598         int vx = 255*v + 0.5;  bclamp(vx,0,255);
599         char yuv_text[BCSTRLEN];
600         sprintf(yuv_text, "#%02x%02x%02x", yx, ux, vx);
601         yuv_hex->update(yuv_text);
602
603         int rgb = (rx << 16) | (gx << 8) | (bx << 0);
604         sample->set_color(rgb);
605         sample->draw_box(0, 0, sample->get_w(), sample->get_h());
606         sample->set_color(BLACK);
607         sample->draw_rectangle(0, 0, sample->get_w(), sample->get_h());
608         sample->flash();
609 }
610
611 void CWindowEyedropGUI::handle_event()
612 {
613         int new_radius = atoi(radius->get_text());
614         if(new_radius != mwindow->edl->session->eyedrop_radius)
615         {
616                 CWindowGUI *gui = mwindow->cwindow->gui;
617                 if(gui->eyedrop_visible)
618                 {
619                         gui->lock_window("CWindowEyedropGUI::handle_event");
620 // hide it
621                         int rerender;
622                         gui->canvas->do_eyedrop(rerender, 0, 1);
623                 }
624
625                 mwindow->edl->session->eyedrop_radius = new_radius;
626
627                 if(gui->eyedrop_visible)
628                 {
629 // draw it
630                         int rerender;
631                         gui->canvas->do_eyedrop(rerender, 0, 1);
632                         gui->unlock_window();
633                 }
634         }
635 }
636
637
638
639 /* Buttons to control Keyframe-Curve-Mode for Projector or Camera */
640
641 // Configuration for all possible Keyframe Curve Mode toggles
642 struct _CVD {
643         FloatAuto::t_mode mode;
644         bool use_camera;
645         const char* icon_id;
646         const char* tooltip;
647 };
648
649 const _CVD Camera_Crv_Smooth =
650         {       FloatAuto::SMOOTH,
651                 true,
652                 "tan_smooth",
653                 N_("\"smooth\" Curve on current Camera Keyframes")
654         };
655 const _CVD Camera_Crv_Linear =
656         {       FloatAuto::LINEAR,
657                 true,
658                 "tan_linear",
659                 N_("\"linear\" Curve on current Camera Keyframes")
660         };
661 const _CVD Projector_Crv_Smooth =
662         {       FloatAuto::SMOOTH,
663                 false,
664                 "tan_smooth",
665                 N_("\"smooth\" Curve on current Projector Keyframes")
666         };
667 const _CVD Projector_Crv_Linear =
668         {       FloatAuto::LINEAR,
669                 false,
670                 "tan_linear",
671                 N_("\"linear\" Curve on current Projector Keyframes")
672         };
673
674 // Implementation Class für Keyframe Curve Mode buttons
675 //
676 // This button reflects the state of the "current" keyframe
677 // (the nearest keyframe on the left) for all three automation
678 // lines together. Clicking on this button (re)sets the curve
679 // mode for the three "current" keyframes simultanously, but
680 // never creates a new keyframe.
681 //
682 class CWindowCurveToggle : public BC_Toggle
683 {
684 public:
685         CWindowCurveToggle(_CVD mode, MWindow *mwindow, CWindowToolGUI *gui, int x, int y);
686         void check_toggle_state(FloatAuto *x, FloatAuto *y, FloatAuto *z);
687         int handle_event();
688 private:
689         _CVD cfg;
690         MWindow *mwindow;
691         CWindowToolGUI *gui;
692 };
693
694
695 CWindowCurveToggle::CWindowCurveToggle(_CVD mode, MWindow *mwindow, CWindowToolGUI *gui, int x, int y)
696  : BC_Toggle(x, y, mwindow->theme->get_image_set(mode.icon_id), false),
697    cfg(mode)
698 {
699         this->gui = gui;
700         this->mwindow = mwindow;
701         set_tooltip(_(cfg.tooltip));
702 }
703
704 void CWindowCurveToggle::check_toggle_state(FloatAuto *x, FloatAuto *y, FloatAuto *z)
705 {
706 // the toggle state is only set to ON if all
707 // three automation lines have the same curve mode.
708 // For mixed states the toggle stays off.
709         set_value( x->curve_mode == this->cfg.mode &&
710                    y->curve_mode == this->cfg.mode &&
711                    z->curve_mode == this->cfg.mode
712                  ,true // redraw to show new state
713                  );
714 }
715
716 int CWindowCurveToggle::handle_event()
717 {
718         Track *track = mwindow->cwindow->calculate_affected_track();
719         if(track) {
720                 FloatAuto *x=0, *y=0, *z=0;
721                 mwindow->cwindow->calculate_affected_autos(track,
722                         &x, &y, &z, cfg.use_camera, 0,0,0); // don't create new keyframe
723                 if( x ) x->change_curve_mode(cfg.mode);
724                 if( y ) y->change_curve_mode(cfg.mode);
725                 if( z ) z->change_curve_mode(cfg.mode);
726
727                 gui->update();
728                 gui->update_preview();
729         }
730
731         return 1;
732 }
733
734
735 CWindowEyedropCheckBox::CWindowEyedropCheckBox(MWindow *mwindow, 
736         CWindowEyedropGUI *gui, int x, int y)
737  : BC_CheckBox(x, y, mwindow->edl->local_session->use_max, _("Use maximum"))
738 {
739         this->mwindow = mwindow;
740         this->gui = gui;
741 }
742
743 int CWindowEyedropCheckBox::handle_event()
744 {
745         mwindow->edl->local_session->use_max = get_value();
746         
747         gui->update();
748         return 1;
749 }
750
751
752 CWindowCameraGUI::CWindowCameraGUI(MWindow *mwindow, CWindowTool *thread)
753  : CWindowToolGUI(mwindow,
754         thread,
755         _(PROGRAM_NAME ": Camera"),
756         170,
757         170)
758 {
759 }
760 CWindowCameraGUI::~CWindowCameraGUI()
761 {
762 }
763
764 void CWindowCameraGUI::create_objects()
765 {
766         int x = 10, y = 10, x1;
767         Track *track = mwindow->cwindow->calculate_affected_track();
768         FloatAuto *x_auto = 0, *y_auto = 0, *z_auto = 0;
769         BC_Title *title;
770         BC_Button *button;
771
772         lock_window("CWindowCameraGUI::create_objects");
773         if( track ) {
774                 mwindow->cwindow->calculate_affected_autos(track,
775                         &x_auto, &y_auto, &z_auto, 1, 0, 0, 0);
776         }
777
778         add_subwindow(title = new BC_Title(x, y, "X:"));
779         x += title->get_w();
780         this->x = new CWindowCoord(this, x, y,
781                 x_auto ? x_auto->get_value() : (float)0);
782         this->x->create_objects();
783
784
785         y += 30;
786         x = 10;
787         add_subwindow(title = new BC_Title(x, y, "Y:"));
788         x += title->get_w();
789         this->y = new CWindowCoord(this, x, y,
790                 y_auto ? y_auto->get_value() : (float)0);
791         this->y->create_objects();
792         y += 30;
793         x = 10;
794         add_subwindow(title = new BC_Title(x, y, "Z:"));
795         x += title->get_w();
796         this->z = new CWindowCoord(this, x, y,
797                 z_auto ? z_auto->get_value() : (float)1);
798         this->z->create_objects();
799         this->z->set_increment(0.01);
800
801         y += 30;
802         x1 = 10;
803         add_subwindow(button = new CWindowCameraLeft(mwindow, this, x1, y));
804         x1 += button->get_w();
805         add_subwindow(button = new CWindowCameraCenter(mwindow, this, x1, y));
806         x1 += button->get_w();
807         add_subwindow(button = new CWindowCameraRight(mwindow, this, x1, y));
808
809         y += button->get_h();
810         x1 = 10;
811         add_subwindow(button = new CWindowCameraTop(mwindow, this, x1, y));
812         x1 += button->get_w();
813         add_subwindow(button = new CWindowCameraMiddle(mwindow, this, x1, y));
814         x1 += button->get_w();
815         add_subwindow(button = new CWindowCameraBottom(mwindow, this, x1, y));
816 // additional Buttons to control the curve mode of the "current" keyframe
817         x1 += button->get_w() + 15;
818         add_subwindow(this->t_smooth = new CWindowCurveToggle(Camera_Crv_Smooth, mwindow, this, x1, y));
819         x1 += button->get_w();
820         add_subwindow(this->t_linear = new CWindowCurveToggle(Camera_Crv_Linear, mwindow, this, x1, y));
821
822 // fill in current auto keyframe values, set toggle states.
823         this->update();
824         unlock_window();
825 }
826
827 void CWindowCameraGUI::handle_event()
828 {
829         FloatAuto *x_auto = 0;
830         FloatAuto *y_auto = 0;
831         FloatAuto *z_auto = 0;
832         Track *track = mwindow->cwindow->calculate_affected_track();
833         if(track)
834         {
835                 mwindow->undo->update_undo_before(_("camera"), this);
836                 if(event_caller == x)
837                 {
838                         x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
839                                 track->automation->autos[AUTOMATION_CAMERA_X],
840                                 1);
841                         if(x_auto)
842                         {
843                                 x_auto->set_value(atof(x->get_text()));
844                                 update();
845                                 update_preview();
846                         }
847                 }
848                 else
849                 if(event_caller == y)
850                 {
851                         y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
852                                 track->automation->autos[AUTOMATION_CAMERA_Y],
853                                 1);
854                         if(y_auto)
855                         {
856                                 y_auto->set_value(atof(y->get_text()));
857                                 update();
858                                 update_preview();
859                         }
860                 }
861                 else
862                 if(event_caller == z)
863                 {
864                         z_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
865                                 track->automation->autos[AUTOMATION_CAMERA_Z],
866                                 1);
867                         if(z_auto)
868                         {
869                                 float zoom = atof(z->get_text());
870                                 if(zoom > 100.) zoom = 100.;
871                                 else
872                                 if(zoom < 0.01) zoom = 0.01;
873         // Doesn't allow user to enter from scratch
874         //              if(zoom != atof(z->get_text()))
875         //                      z->update(zoom);
876
877                                 z_auto->set_value(zoom);
878                                 mwindow->gui->lock_window("CWindowCameraGUI::handle_event");
879                                 mwindow->gui->draw_overlays(1);
880                                 mwindow->gui->unlock_window();
881                                 update();
882                                 update_preview();
883                         }
884                 }
885
886                 mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
887         }
888 }
889
890 void CWindowCameraGUI::update()
891 {
892         FloatAuto *x_auto = 0;
893         FloatAuto *y_auto = 0;
894         FloatAuto *z_auto = 0;
895         Track *track = mwindow->cwindow->calculate_affected_track();
896         if( track ) {
897                 mwindow->cwindow->calculate_affected_autos(track,
898                         &x_auto, &y_auto, &z_auto, 1, 0, 0, 0);
899         }
900
901         if(x_auto)
902                 x->update(x_auto->get_value());
903         if(y_auto)
904                 y->update(y_auto->get_value());
905         if(z_auto) {
906                 float value = z_auto->get_value();
907                 z->update(value);
908                 thread->gui->lock_window("CWindowCameraGUI::update");
909                 thread->gui->composite_panel->cpanel_zoom->update(value);
910                 thread->gui->unlock_window();
911         }
912
913         if( x_auto && y_auto && z_auto )
914         {
915                 t_smooth->check_toggle_state(x_auto, y_auto, z_auto);
916                 t_linear->check_toggle_state(x_auto, y_auto, z_auto);
917         }
918 }
919
920
921
922
923 CWindowCameraLeft::CWindowCameraLeft(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
924  : BC_Button(x, y, mwindow->theme->get_image_set("left_justify"))
925 {
926         this->gui = gui;
927         this->mwindow = mwindow;
928         set_tooltip(_("Left justify"));
929 }
930 int CWindowCameraLeft::handle_event()
931 {
932         FloatAuto *x_auto = 0;
933         FloatAuto *z_auto = 0;
934         Track *track = mwindow->cwindow->calculate_affected_track();
935         if( track ) {
936                 mwindow->cwindow->calculate_affected_autos(track,
937                         &x_auto, 0, &z_auto, 1, 1, 0, 0);
938         }
939
940         if(x_auto && z_auto)
941         {
942                 int w = 0, h = 0;
943                 track->get_source_dimensions(
944                         mwindow->edl->local_session->get_selectionstart(1),
945                         w,
946                         h);
947
948                 if(w && h)
949                 {
950                         mwindow->undo->update_undo_before(_("camera"), 0);
951                         x_auto->set_value(
952                                 (double)track->track_w / z_auto->get_value() / 2 -
953                                 (double)w / 2);
954                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
955                         gui->update();
956                         gui->update_preview();
957                 }
958         }
959
960         return 1;
961 }
962
963
964 CWindowCameraCenter::CWindowCameraCenter(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
965  : BC_Button(x, y, mwindow->theme->get_image_set("center_justify"))
966 {
967         this->gui = gui;
968         this->mwindow = mwindow;
969         set_tooltip(_("Center horizontal"));
970 }
971 int CWindowCameraCenter::handle_event()
972 {
973         FloatAuto *x_auto = 0;
974         Track *track = mwindow->cwindow->calculate_affected_track();
975         if(track)
976                 x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
977                         track->automation->autos[AUTOMATION_CAMERA_X],
978                         1);
979
980         if(x_auto)
981         {
982                 mwindow->undo->update_undo_before(_("camera"), 0);
983                 x_auto->set_value(0);
984                 gui->update();
985                 gui->update_preview();
986                 mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
987         }
988
989         return 1;
990 }
991
992
993 CWindowCameraRight::CWindowCameraRight(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
994  : BC_Button(x, y, mwindow->theme->get_image_set("right_justify"))
995 {
996         this->gui = gui;
997         this->mwindow = mwindow;
998         set_tooltip(_("Right justify"));
999 }
1000 int CWindowCameraRight::handle_event()
1001 {
1002         FloatAuto *x_auto = 0;
1003         FloatAuto *z_auto = 0;
1004         Track *track = mwindow->cwindow->calculate_affected_track();
1005         if( track ) {
1006                 mwindow->cwindow->calculate_affected_autos(track,
1007                         &x_auto, 0, &z_auto, 1, 1, 0, 0);
1008         }
1009
1010         if(x_auto && z_auto)
1011         {
1012                 int w = 0, h = 0;
1013                 track->get_source_dimensions(
1014                         mwindow->edl->local_session->get_selectionstart(1),
1015                         w,
1016                         h);
1017
1018                 if(w && h)
1019                 {
1020                         mwindow->undo->update_undo_before(_("camera"), 0);
1021                         x_auto->set_value( -((double)track->track_w / z_auto->get_value() / 2 -
1022                                 (double)w / 2));
1023                         gui->update();
1024                         gui->update_preview();
1025                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1026                 }
1027         }
1028
1029         return 1;
1030 }
1031
1032
1033 CWindowCameraTop::CWindowCameraTop(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
1034  : BC_Button(x, y, mwindow->theme->get_image_set("top_justify"))
1035 {
1036         this->gui = gui;
1037         this->mwindow = mwindow;
1038         set_tooltip(_("Top justify"));
1039 }
1040 int CWindowCameraTop::handle_event()
1041 {
1042         FloatAuto *y_auto = 0;
1043         FloatAuto *z_auto = 0;
1044         Track *track = mwindow->cwindow->calculate_affected_track();
1045         if( track ) {
1046                 mwindow->cwindow->calculate_affected_autos(track,
1047                         0, &y_auto, &z_auto, 1, 0, 1, 0);
1048         }
1049
1050         if(y_auto && z_auto)
1051         {
1052                 int w = 0, h = 0;
1053                 track->get_source_dimensions(
1054                         mwindow->edl->local_session->get_selectionstart(1),
1055                         w,
1056                         h);
1057
1058                 if(w && h)
1059                 {
1060                         mwindow->undo->update_undo_before(_("camera"), 0);
1061                         y_auto->set_value((double)track->track_h / z_auto->get_value() / 2 -
1062                                 (double)h / 2);
1063                         gui->update();
1064                         gui->update_preview();
1065                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1066                 }
1067         }
1068
1069         return 1;
1070 }
1071
1072
1073 CWindowCameraMiddle::CWindowCameraMiddle(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
1074  : BC_Button(x, y, mwindow->theme->get_image_set("middle_justify"))
1075 {
1076         this->gui = gui;
1077         this->mwindow = mwindow;
1078         set_tooltip(_("Center vertical"));
1079 }
1080 int CWindowCameraMiddle::handle_event()
1081 {
1082         FloatAuto *y_auto = 0;
1083         Track *track = mwindow->cwindow->calculate_affected_track();
1084         if(track)
1085                 y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1086                         track->automation->autos[AUTOMATION_CAMERA_Y], 1);
1087
1088         if(y_auto)
1089         {
1090                 mwindow->undo->update_undo_before(_("camera"), 0);
1091                 y_auto->set_value(0);
1092                 gui->update();
1093                 gui->update_preview();
1094                 mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1095         }
1096
1097         return 1;
1098 }
1099
1100
1101 CWindowCameraBottom::CWindowCameraBottom(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
1102  : BC_Button(x, y, mwindow->theme->get_image_set("bottom_justify"))
1103 {
1104         this->gui = gui;
1105         this->mwindow = mwindow;
1106         set_tooltip(_("Bottom justify"));
1107 }
1108 int CWindowCameraBottom::handle_event()
1109 {
1110         FloatAuto *y_auto = 0;
1111         FloatAuto *z_auto = 0;
1112         Track *track = mwindow->cwindow->calculate_affected_track();
1113         if( track ) {
1114                 mwindow->cwindow->calculate_affected_autos(track,
1115                         0, &y_auto, &z_auto, 1, 0, 1, 0);
1116         }
1117
1118         if(y_auto && z_auto)
1119         {
1120                 int w = 0, h = 0;
1121                 track->get_source_dimensions(
1122                         mwindow->edl->local_session->get_selectionstart(1),
1123                         w,
1124                         h);
1125
1126                 if(w && h)
1127                 {
1128                         mwindow->undo->update_undo_before(_("camera"), 0);
1129                         y_auto->set_value(-((double)track->track_h / z_auto->get_value() / 2 -
1130                                 (double)h / 2));
1131                         gui->update();
1132                         gui->update_preview();
1133                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1134                 }
1135         }
1136
1137         return 1;
1138 }
1139
1140
1141 CWindowProjectorGUI::CWindowProjectorGUI(MWindow *mwindow, CWindowTool *thread)
1142  : CWindowToolGUI(mwindow,
1143         thread,
1144         _(PROGRAM_NAME ": Projector"),
1145         170,
1146         170)
1147 {
1148 }
1149 CWindowProjectorGUI::~CWindowProjectorGUI()
1150 {
1151 }
1152 void CWindowProjectorGUI::create_objects()
1153 {
1154         int x = 10, y = 10, x1;
1155         Track *track = mwindow->cwindow->calculate_affected_track();
1156         FloatAuto *x_auto = 0;
1157         FloatAuto *y_auto = 0;
1158         FloatAuto *z_auto = 0;
1159         BC_Title *title;
1160         BC_Button *button;
1161
1162         lock_window("CWindowProjectorGUI::create_objects");
1163         if( track ) {
1164                 mwindow->cwindow->calculate_affected_autos(track,
1165                         &x_auto, &y_auto, &z_auto, 0, 0, 0, 0);
1166         }
1167
1168         add_subwindow(title = new BC_Title(x, y, "X:"));
1169         x += title->get_w();
1170         this->x = new CWindowCoord(this, x, y,
1171                 x_auto ? x_auto->get_value() : (float)0);
1172         this->x->create_objects();
1173         y += 30;
1174         x = 10;
1175         add_subwindow(title = new BC_Title(x, y, "Y:"));
1176         x += title->get_w();
1177         this->y = new CWindowCoord(this, x, y,
1178                 y_auto ? y_auto->get_value() : (float)0);
1179         this->y->create_objects();
1180         y += 30;
1181         x = 10;
1182         add_subwindow(title = new BC_Title(x, y, "Z:"));
1183         x += title->get_w();
1184         this->z = new CWindowCoord(this, x, y,
1185                 z_auto ? z_auto->get_value() : (float)1);
1186         this->z->create_objects();
1187         this->z->set_increment(0.01);
1188
1189         y += 30;
1190         x1 = 10;
1191         add_subwindow(button = new CWindowProjectorLeft(mwindow, this, x1, y));
1192         x1 += button->get_w();
1193         add_subwindow(button = new CWindowProjectorCenter(mwindow, this, x1, y));
1194         x1 += button->get_w();
1195         add_subwindow(button = new CWindowProjectorRight(mwindow, this, x1, y));
1196
1197         y += button->get_h();
1198         x1 = 10;
1199         add_subwindow(button = new CWindowProjectorTop(mwindow, this, x1, y));
1200         x1 += button->get_w();
1201         add_subwindow(button = new CWindowProjectorMiddle(mwindow, this, x1, y));
1202         x1 += button->get_w();
1203         add_subwindow(button = new CWindowProjectorBottom(mwindow, this, x1, y));
1204
1205 // additional Buttons to control the curve mode of the "current" keyframe
1206         x1 += button->get_w() + 15;
1207         add_subwindow(this->t_smooth = new CWindowCurveToggle(Projector_Crv_Smooth, mwindow, this, x1, y));
1208         x1 += button->get_w();
1209         add_subwindow(this->t_linear = new CWindowCurveToggle(Projector_Crv_Linear, mwindow, this, x1, y));
1210
1211 // fill in current auto keyframe values, set toggle states.
1212         this->update();
1213         unlock_window();
1214 }
1215
1216 void CWindowProjectorGUI::handle_event()
1217 {
1218         FloatAuto *x_auto = 0;
1219         FloatAuto *y_auto = 0;
1220         FloatAuto *z_auto = 0;
1221         Track *track = mwindow->cwindow->calculate_affected_track();
1222
1223         if(track)
1224         {
1225                 mwindow->undo->update_undo_before(_("projector"), this);
1226                 if(event_caller == x)
1227                 {
1228                         x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1229                                 track->automation->autos[AUTOMATION_PROJECTOR_X],
1230                                 1);
1231                         if(x_auto)
1232                         {
1233                                 x_auto->set_value(atof(x->get_text()));
1234                                 update();
1235                                 update_preview();
1236                         }
1237                 }
1238                 else
1239                 if(event_caller == y)
1240                 {
1241                         y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1242                                 track->automation->autos[AUTOMATION_PROJECTOR_Y],
1243                                 1);
1244                         if(y_auto)
1245                         {
1246                                 y_auto->set_value(atof(y->get_text()));
1247                                 update();
1248                                 update_preview();
1249                         }
1250                 }
1251                 else
1252                 if(event_caller == z)
1253                 {
1254                         z_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1255                                 track->automation->autos[AUTOMATION_PROJECTOR_Z],
1256                                 1);
1257                         if(z_auto)
1258                         {
1259                                 float zoom = atof(z->get_text());
1260                                 if(zoom > 100.) zoom = 100.;
1261                                 else if(zoom < 0.01) zoom = 0.01;
1262 //                      if (zoom != atof(z->get_text()))
1263 //                              z->update(zoom);
1264                                 z_auto->set_value(zoom);
1265
1266                                 mwindow->gui->lock_window("CWindowProjectorGUI::handle_event");
1267                                 mwindow->gui->draw_overlays(1);
1268                                 mwindow->gui->unlock_window();
1269
1270                                 update();
1271                                 update_preview();
1272                         }
1273                 }
1274                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1275         }
1276 }
1277
1278 void CWindowProjectorGUI::update()
1279 {
1280         FloatAuto *x_auto = 0;
1281         FloatAuto *y_auto = 0;
1282         FloatAuto *z_auto = 0;
1283         Track *track = mwindow->cwindow->calculate_affected_track();
1284         if( track ) {
1285                 mwindow->cwindow->calculate_affected_autos(track,
1286                         &x_auto, &y_auto, &z_auto, 0, 0, 0, 0);
1287         }
1288
1289         if(x_auto)
1290                 x->update(x_auto->get_value());
1291         if(y_auto)
1292                 y->update(y_auto->get_value());
1293         if(z_auto) {
1294                 float value = z_auto->get_value();
1295                 z->update(value);
1296                 thread->gui->lock_window("CWindowProjectorGUI::update");
1297                 thread->gui->composite_panel->cpanel_zoom->update(value);
1298                 thread->gui->unlock_window();
1299         }
1300
1301         if( x_auto && y_auto && z_auto )
1302         {
1303                 t_smooth->check_toggle_state(x_auto, y_auto, z_auto);
1304                 t_linear->check_toggle_state(x_auto, y_auto, z_auto);
1305         }
1306 }
1307
1308 CWindowProjectorLeft::CWindowProjectorLeft(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1309  : BC_Button(x, y, mwindow->theme->get_image_set("left_justify"))
1310 {
1311         this->gui = gui;
1312         this->mwindow = mwindow;
1313         set_tooltip(_("Left justify"));
1314 }
1315 int CWindowProjectorLeft::handle_event()
1316 {
1317         FloatAuto *x_auto = 0;
1318         FloatAuto *z_auto = 0;
1319         Track *track = mwindow->cwindow->calculate_affected_track();
1320         if( track ) {
1321                 mwindow->cwindow->calculate_affected_autos(track,
1322                         &x_auto, 0, &z_auto, 0, 1, 0, 0);
1323         }
1324         if(x_auto && z_auto)
1325         {
1326                 mwindow->undo->update_undo_before(_("projector"), 0);
1327                 x_auto->set_value( (double)track->track_w * z_auto->get_value() / 2 -
1328                         (double)mwindow->edl->session->output_w / 2 );
1329                 gui->update();
1330                 gui->update_preview();
1331                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1332         }
1333
1334         return 1;
1335 }
1336
1337
1338 CWindowProjectorCenter::CWindowProjectorCenter(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1339  : BC_Button(x, y, mwindow->theme->get_image_set("center_justify"))
1340 {
1341         this->gui = gui;
1342         this->mwindow = mwindow;
1343         set_tooltip(_("Center horizontal"));
1344 }
1345 int CWindowProjectorCenter::handle_event()
1346 {
1347         FloatAuto *x_auto = 0;
1348         Track *track = mwindow->cwindow->calculate_affected_track();
1349         if(track)
1350                 x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1351                         track->automation->autos[AUTOMATION_PROJECTOR_X],
1352                         1);
1353
1354         if(x_auto)
1355         {
1356                 mwindow->undo->update_undo_before(_("projector"), 0);
1357                 x_auto->set_value(0);
1358                 gui->update();
1359                 gui->update_preview();
1360                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1361         }
1362
1363         return 1;
1364 }
1365
1366
1367 CWindowProjectorRight::CWindowProjectorRight(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1368  : BC_Button(x, y, mwindow->theme->get_image_set("right_justify"))
1369 {
1370         this->gui = gui;
1371         this->mwindow = mwindow;
1372         set_tooltip(_("Right justify"));
1373 }
1374 int CWindowProjectorRight::handle_event()
1375 {
1376         FloatAuto *x_auto = 0;
1377         FloatAuto *z_auto = 0;
1378         Track *track = mwindow->cwindow->calculate_affected_track();
1379         if( track ) {
1380                 mwindow->cwindow->calculate_affected_autos(track,
1381                         &x_auto, 0, &z_auto, 0, 1, 0, 0);
1382         }
1383
1384         if(x_auto && z_auto)
1385         {
1386                 mwindow->undo->update_undo_before(_("projector"), 0);
1387                 x_auto->set_value( -((double)track->track_w * z_auto->get_value() / 2 -
1388                         (double)mwindow->edl->session->output_w / 2));
1389                 gui->update();
1390                 gui->update_preview();
1391                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1392         }
1393
1394         return 1;
1395 }
1396
1397
1398 CWindowProjectorTop::CWindowProjectorTop(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1399  : BC_Button(x, y, mwindow->theme->get_image_set("top_justify"))
1400 {
1401         this->gui = gui;
1402         this->mwindow = mwindow;
1403         set_tooltip(_("Top justify"));
1404 }
1405 int CWindowProjectorTop::handle_event()
1406 {
1407         FloatAuto *y_auto = 0;
1408         FloatAuto *z_auto = 0;
1409         Track *track = mwindow->cwindow->calculate_affected_track();
1410         if( track ) {
1411                 mwindow->cwindow->calculate_affected_autos(track,
1412                         0, &y_auto, &z_auto, 0, 0, 1, 0);
1413         }
1414
1415         if(y_auto && z_auto)
1416         {
1417                 mwindow->undo->update_undo_before(_("projector"), 0);
1418                 y_auto->set_value( (double)track->track_h * z_auto->get_value() / 2 -
1419                         (double)mwindow->edl->session->output_h / 2 );
1420                 gui->update();
1421                 gui->update_preview();
1422                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1423         }
1424
1425         return 1;
1426 }
1427
1428
1429 CWindowProjectorMiddle::CWindowProjectorMiddle(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1430  : BC_Button(x, y, mwindow->theme->get_image_set("middle_justify"))
1431 {
1432         this->gui = gui;
1433         this->mwindow = mwindow;
1434         set_tooltip(_("Center vertical"));
1435 }
1436 int CWindowProjectorMiddle::handle_event()
1437 {
1438         FloatAuto *y_auto = 0;
1439         Track *track = mwindow->cwindow->calculate_affected_track();
1440         if(track)
1441                 y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1442                         track->automation->autos[AUTOMATION_PROJECTOR_Y], 1);
1443
1444         if(y_auto)
1445         {
1446                 mwindow->undo->update_undo_before(_("projector"), 0);
1447                 y_auto->set_value(0);
1448                 gui->update();
1449                 gui->update_preview();
1450                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1451         }
1452
1453         return 1;
1454 }
1455
1456
1457 CWindowProjectorBottom::CWindowProjectorBottom(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1458  : BC_Button(x, y, mwindow->theme->get_image_set("bottom_justify"))
1459 {
1460         this->gui = gui;
1461         this->mwindow = mwindow;
1462         set_tooltip(_("Bottom justify"));
1463 }
1464 int CWindowProjectorBottom::handle_event()
1465 {
1466         FloatAuto *y_auto = 0;
1467         FloatAuto *z_auto = 0;
1468         Track *track = mwindow->cwindow->calculate_affected_track();
1469         if( track ) {
1470                 mwindow->cwindow->calculate_affected_autos(track,
1471                         0, &y_auto, &z_auto, 0, 0, 1, 0);
1472         }
1473
1474         if(y_auto && z_auto)
1475         {
1476                 mwindow->undo->update_undo_before(_("projector"), 0);
1477                 y_auto->set_value( -((double)track->track_h * z_auto->get_value() / 2 -
1478                         (double)mwindow->edl->session->output_h / 2));
1479                 gui->update();
1480                 gui->update_preview();
1481                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1482         }
1483
1484         return 1;
1485 }
1486
1487
1488 CWindowMaskOnTrack::CWindowMaskOnTrack(MWindow *mwindow, CWindowMaskGUI *gui,
1489                 int x, int y, int w, const char *text)
1490  : BC_PopupTextBox(gui, 0, text, x, y, w, 120)
1491 {
1492         this->mwindow = mwindow;
1493         this->gui = gui;
1494 }
1495
1496 CWindowMaskOnTrack::~CWindowMaskOnTrack()
1497 {
1498 }
1499
1500 int CWindowMaskOnTrack::handle_event()
1501 {
1502         CWindowMaskItem *track_item = 0;
1503         int k = get_number(), track_id = -1;
1504 //printf("selected %d = %s\n", k, k<0 ? "()" : track_items[k]->get_text());
1505         if( k >= 0 ) {
1506                 track_item = (CWindowMaskItem *)track_items[k];
1507                 Track *track = track_item ? mwindow->edl->tracks->get_track_by_id(track_item->id) : 0;
1508                 if( track && track->record ) track_id = track->get_id();
1509         }
1510         else
1511                 track_id = mwindow->cwindow->mask_track_id;
1512         set_back_color(track_id >= 0 ?
1513                 gui->get_resources()->text_background :
1514                 gui->get_resources()->text_background_disarmed);
1515         if( mwindow->cwindow->mask_track_id != track_id )
1516                 gui->mask_on_track->update(track_item ? track_item->get_text() : "");
1517         mwindow->cwindow->mask_track_id = track_id;
1518         mwindow->edl->local_session->solo_track_id = -1;
1519         gui->mask_solo_track->update(0);
1520         gui->update();
1521         gui->update_preview(1);
1522         return 1;
1523 }
1524
1525 void CWindowMaskOnTrack::update_items()
1526 {
1527         track_items.remove_all_objects();
1528         int high_color = gui->get_resources()->button_highlighted;
1529         for( Track *track=mwindow->edl->tracks->first; track; track=track->next ) {
1530                 if( track->data_type != TRACK_VIDEO ) continue;
1531                 MaskAutos *mask_autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1532                 int color = !track->record ? RED : mask_autos->first ?  high_color : -1;
1533                 MaskAuto *mask_auto = (MaskAuto*)mask_autos->default_auto;
1534                 for( int i=0; color<0 && i<mask_auto->masks.size(); ++i )
1535                         if( mask_auto->masks[i]->points.size() > 0 ) color = high_color;
1536                 track_items.append(new CWindowMaskItem(track->title, track->get_id(), color));
1537         }
1538         update_list(&track_items);
1539 }
1540
1541 CWindowMaskTrackTumbler::CWindowMaskTrackTumbler(MWindow *mwindow, CWindowMaskGUI *gui,
1542                 int x, int y)
1543  : BC_Tumbler(x, y)
1544 {
1545         this->mwindow = mwindow;
1546         this->gui = gui;
1547 }
1548 CWindowMaskTrackTumbler::~CWindowMaskTrackTumbler()
1549 {
1550 }
1551
1552 int CWindowMaskTrackTumbler::handle_up_event()
1553 {
1554         return do_event(1);
1555 }
1556
1557 int CWindowMaskTrackTumbler::handle_down_event()
1558 {
1559         return do_event(-1);
1560 }
1561
1562 int CWindowMaskTrackTumbler::do_event(int dir)
1563 {
1564         CWindowMaskItem *track_item = 0;
1565         CWindowMaskItem **items = (CWindowMaskItem**)&gui->mask_on_track->track_items[0];
1566         int n = gui->mask_on_track->track_items.size();
1567         int id = mwindow->cwindow->mask_track_id;
1568         if( n > 0 ) {   
1569                 int k = n;
1570                 while( --k >= 0 && items[k]->id != id );
1571                 if( k >= 0 ) {
1572                         k += dir;
1573                         bclamp(k, 0, n-1);
1574                         track_item = items[k];
1575                 }
1576                 else
1577                         track_item = items[0];
1578         }
1579         Track *track = track_item ? mwindow->edl->tracks->get_track_by_id(track_item->id) : 0;
1580         int track_id = track_item && track && track->record ? track_item->id : -1;
1581         gui->mask_on_track->set_back_color(track_id >= 0 ?
1582                 gui->get_resources()->text_background :
1583                 gui->get_resources()->text_background_disarmed);
1584         gui->mask_on_track->update(track_item ? track_item->get_text() : "");
1585         mwindow->cwindow->mask_track_id = track_item ? track_item->id : -1;
1586         mwindow->edl->local_session->solo_track_id = -1;
1587         gui->mask_solo_track->update(0);
1588         gui->update();
1589         gui->update_preview(1);
1590         return 1;
1591 }
1592
1593
1594 CWindowMaskName::CWindowMaskName(MWindow *mwindow, CWindowMaskGUI *gui,
1595                 int x, int y, const char *text)
1596  : BC_PopupTextBox(gui, 0, text, x, y, 100, 160)
1597 {
1598         this->mwindow = mwindow;
1599         this->gui = gui;
1600 }
1601
1602 CWindowMaskName::~CWindowMaskName()
1603 {
1604 }
1605
1606 int CWindowMaskName::handle_event()
1607 {
1608         Track *track;
1609         MaskAutos *autos;
1610         MaskAuto *keyframe;
1611         SubMask *mask;
1612         MaskPoint *point;
1613 //printf("CWindowMaskGUI::update 1\n");
1614         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
1615         if( track ) {
1616                 int k = get_number();
1617                 if( k < 0 ) k = mwindow->edl->session->cwindow_mask;
1618                 else mwindow->edl->session->cwindow_mask = k;
1619                 if( k >= 0 && k < mask_items.size() ) {
1620                         mask_items[k]->set_text(get_text());
1621                         update_list(&mask_items);
1622                 }
1623 #ifdef USE_KEYFRAME_SPANNING
1624                 MaskAuto temp_keyframe(mwindow->edl, autos);
1625                 temp_keyframe.copy_data(keyframe);
1626                 SubMask *submask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
1627                 memset(submask->name, 0, sizeof(submask->name));
1628                 strncpy(submask->name, get_text(), sizeof(submask->name)-1);
1629                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->update_parameter(&temp_keyframe);
1630 #else
1631                 for(MaskAuto *current = (MaskAuto*)autos->default_auto; current; ) {
1632                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1633                         memset(submask->name, 0, sizeof(submask->name));
1634                         strncpy(submask->name, get_text(), sizeof(submask->name));
1635                         current = current == (MaskAuto*)autos->default_auto ?
1636                                 (MaskAuto*)autos->first : (MaskAuto*)NEXT;
1637                 }
1638 #endif
1639                 gui->update();
1640                 gui->update_preview();
1641         }
1642         return 1;
1643 }
1644
1645 void CWindowMaskName::update_items(MaskAuto *keyframe)
1646 {
1647         mask_items.remove_all_objects();
1648         int sz = !keyframe ? 0 : keyframe->masks.size();
1649         for( int i=0; i<SUBMASKS; ++i ) {
1650                 char text[BCSTRLEN];
1651                 if( i < sz ) {
1652                         SubMask *sub_mask = keyframe->masks.get(i);
1653                         strncpy(text, sub_mask->name, sizeof(text));
1654                 }
1655                 else
1656                         sprintf(text, "%d", i);
1657                 mask_items.append(new CWindowMaskItem(text));
1658         }
1659         update_list(&mask_items);
1660 }
1661
1662
1663 CWindowMaskButton::CWindowMaskButton(MWindow *mwindow, CWindowMaskGUI *gui,
1664                  int x, int y, int no, int v)
1665  : BC_CheckBox(x, y, v)
1666 {
1667         this->mwindow = mwindow;
1668         this->gui = gui;
1669         this->no = no;
1670 }
1671
1672 CWindowMaskButton::~CWindowMaskButton()
1673 {
1674 }
1675
1676 int CWindowMaskButton::handle_event()
1677 {
1678         mwindow->edl->session->cwindow_mask = no;
1679         gui->mask_name->update(gui->mask_name->mask_items[no]->get_text());
1680         gui->update();
1681         gui->update_preview();
1682         return 1;
1683 }
1684
1685 CWindowMaskThumbler::CWindowMaskThumbler(MWindow *mwindow, CWindowMaskGUI *gui,
1686                 int x, int y)
1687  : BC_Tumbler(x, y)
1688 {
1689         this->mwindow = mwindow;
1690         this->gui = gui;
1691 }
1692
1693 CWindowMaskThumbler::~CWindowMaskThumbler()
1694 {
1695 }
1696
1697 int CWindowMaskThumbler::handle_up_event()
1698 {
1699         return do_event(1);
1700 }
1701
1702 int CWindowMaskThumbler::handle_down_event()
1703 {
1704         return do_event(-1);
1705 }
1706
1707 int CWindowMaskThumbler::do_event(int dir)
1708 {
1709         int k = mwindow->edl->session->cwindow_mask;
1710         if( (k+=dir) >= SUBMASKS ) k = 0;
1711         else if( k < 0 ) k = SUBMASKS-1;
1712         mwindow->edl->session->cwindow_mask = k;
1713         gui->mask_name->update(gui->mask_name->mask_items[k]->get_text());
1714         gui->update();
1715         gui->update_preview();
1716         return 1;
1717 }
1718
1719 CWindowMaskEnable::CWindowMaskEnable(MWindow *mwindow, CWindowMaskGUI *gui,
1720                  int x, int y, int no, int v)
1721  : BC_CheckBox(x, y, v)
1722 {
1723         this->mwindow = mwindow;
1724         this->gui = gui;
1725         this->no = no;
1726 }
1727
1728 CWindowMaskEnable::~CWindowMaskEnable()
1729 {
1730 }
1731
1732 int CWindowMaskEnable::handle_event()
1733 {
1734         Track *track = mwindow->cwindow->calculate_mask_track();
1735         if( track ) {
1736                 mwindow->undo->update_undo_before(_("mask enable"), this);
1737                 int bit = 1 << no;
1738                 if( get_value() )
1739                         track->masks |= bit;
1740                 else
1741                         track->masks &= ~bit;
1742                 gui->update();
1743                 gui->update_preview(1);
1744                 mwindow->undo->update_undo_after(_("mask enable"), LOAD_PATCHES);
1745         }
1746         return 1;
1747 }
1748
1749 CWindowMaskUnclear::CWindowMaskUnclear(MWindow *mwindow,
1750         CWindowMaskGUI *gui, int x, int y, int w)
1751  : BC_GenericButton(x, y, w, _("Enable"))
1752 {
1753         this->mwindow = mwindow;
1754         this->gui = gui;
1755         set_tooltip(_("Show/Hide mask"));
1756 }
1757
1758 int CWindowMaskUnclear::handle_event()
1759 {
1760         Track *track = mwindow->cwindow->calculate_mask_track();
1761         if( track ) {
1762                 mwindow->undo->update_undo_before(_("mask enables"), this);
1763                 int m = (1<<SUBMASKS)-1;
1764                 if( track->masks == m )
1765                         track->masks = 0;
1766                 else
1767                         track->masks = m;
1768                 for( int i=0; i<SUBMASKS; ++i )
1769                         gui->mask_enables[i]->update((track->masks>>i) & 1);
1770                 gui->update_preview(1);
1771                 mwindow->undo->update_undo_after(_("mask enables"), LOAD_PATCHES);
1772         }
1773         return 1;
1774 }
1775
1776 CWindowMaskSoloTrack::CWindowMaskSoloTrack(MWindow *mwindow,
1777         CWindowMaskGUI *gui, int x, int y, int v)
1778  : BC_CheckBox(x, y, v, _("Solo"))
1779 {
1780         this->mwindow = mwindow;
1781         this->gui = gui;
1782         set_tooltip(_("Solo video track"));
1783 }
1784
1785 int CWindowMaskSoloTrack::handle_event()
1786 {
1787         mwindow->edl->local_session->solo_track_id =
1788                 get_value() ? mwindow->cwindow->mask_track_id : -1;
1789         gui->update_preview(1);
1790         return 1;
1791 }
1792
1793 int CWindowMaskSoloTrack::calculate_w(BC_WindowBase *gui)
1794 {
1795         int w = 0, h = 0;
1796         calculate_extents(gui, &w, &h, _("Solo"));
1797         return w;
1798 }
1799
1800 CWindowMaskDelMask::CWindowMaskDelMask(MWindow *mwindow,
1801         CWindowMaskGUI *gui, int x, int y)
1802  : BC_GenericButton(x, y, _("Delete"))
1803 {
1804         this->mwindow = mwindow;
1805         this->gui = gui;
1806         set_tooltip(_("Delete mask"));
1807 }
1808
1809 int CWindowMaskDelMask::handle_event()
1810 {
1811         MaskAutos *autos;
1812         MaskAuto *keyframe;
1813         Track *track;
1814         MaskPoint *point;
1815         SubMask *mask;
1816         int total_points;
1817
1818 // Get existing keyframe
1819         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
1820
1821         if( track ) {
1822                 mwindow->undo->update_undo_before(_("mask delete"), 0);
1823
1824 #ifdef USE_KEYFRAME_SPANNING
1825 // Create temp keyframe
1826                 MaskAuto temp_keyframe(mwindow->edl, autos);
1827                 temp_keyframe.copy_data(keyframe);
1828                 SubMask *submask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
1829                 submask->points.remove_all_objects();
1830                 total_points = 0;
1831 // Commit change to span of keyframes
1832                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->update_parameter(&temp_keyframe);
1833 #else
1834                 for(MaskAuto *current = (MaskAuto*)autos->default_auto; current; ) {
1835                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1836                         submask->points.clear();
1837                         current = current == (MaskAuto*)autos->default_auto ?
1838                                 (MaskAuto*)autos->first : (MaskAuto*)NEXT;
1839                 }
1840                 total_points = 0;
1841 #endif
1842                 if( mwindow->cwindow->gui->affected_point >= total_points )
1843                         mwindow->cwindow->gui->affected_point =
1844                                 total_points > 0 ? total_points-1 : 0;
1845
1846                 gui->update();
1847                 gui->update_preview();
1848                 mwindow->undo->update_undo_after(_("mask delete"), LOAD_AUTOMATION);
1849         }
1850
1851         return 1;
1852 }
1853
1854 CWindowMaskDelPoint::CWindowMaskDelPoint(MWindow *mwindow,
1855         CWindowMaskGUI *gui, int x, int y)
1856  : BC_GenericButton(x, y, _("Delete"))
1857 {
1858         this->mwindow = mwindow;
1859         this->gui = gui;
1860         set_tooltip(_("Delete point"));
1861 }
1862
1863 int CWindowMaskDelPoint::handle_event()
1864 {
1865         MaskAutos *autos;
1866         MaskAuto *keyframe;
1867         Track *track;
1868         MaskPoint *point;
1869         SubMask *mask;
1870         int total_points;
1871
1872 // Get existing keyframe
1873         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
1874         if( track ) {
1875                 mwindow->undo->update_undo_before(_("point delete"), 0);
1876
1877 #ifdef USE_KEYFRAME_SPANNING
1878 // Create temp keyframe
1879                 MaskAuto temp_keyframe(mwindow->edl, autos);
1880                 temp_keyframe.copy_data(keyframe);
1881 // Update parameter
1882                 SubMask *submask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
1883                 int i = mwindow->cwindow->gui->affected_point;
1884                 for( ; i<submask->points.total-1; ++i )
1885                         *submask->points.values[i] = *submask->points.values[i+1];
1886                 if( submask->points.total > 0 ) {
1887                         point = submask->points.values[submask->points.total-1];
1888                         submask->points.remove_object(point);
1889                 }
1890                 total_points = submask->points.total;
1891
1892 // Commit change to span of keyframes
1893                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->update_parameter(&temp_keyframe);
1894 #else
1895                 MaskAuto *current = (MaskAuto*)autos->default_auto;
1896                 while( current ) {
1897                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1898                         int i = mwindow->cwindow->gui->affected_point;
1899                         for( ; i<submask->points.total-1; ++i ) {
1900                                 *submask->points.values[i] = *submask->points.values[i+1];
1901                         if( submask->points.total > 0 ) {
1902                                 point = submask->points.values[submask->points.total-1];
1903                                 submask->points.remove_object(point);
1904                         }
1905                         total_points = submask->points.total;
1906                         current = current == (MaskAuto*)autos->default_auto ?
1907                                 (MaskAuto*)autos->first : (MaskAuto*)NEXT;
1908                 }
1909 #endif
1910                 if( mwindow->cwindow->gui->affected_point >= total_points )
1911                         mwindow->cwindow->gui->affected_point =
1912                                 total_points > 0 ? total_points-1 : 0;
1913
1914                 gui->update();
1915                 gui->update_preview();
1916                 mwindow->undo->update_undo_after(_("mask delete"), LOAD_AUTOMATION);
1917         }
1918
1919         return 1;
1920 }
1921
1922 int CWindowMaskDelPoint::keypress_event()
1923 {
1924         if( get_keypress() == BACKSPACE ||
1925             get_keypress() == DELETE )
1926                 return handle_event();
1927         return 0;
1928 }
1929
1930
1931
1932
1933 CWindowMaskAffectedPoint::CWindowMaskAffectedPoint(MWindow *mwindow,
1934         CWindowMaskGUI *gui, int x, int y)
1935  : BC_TumbleTextBox(gui,
1936                 (int64_t)mwindow->cwindow->gui->affected_point,
1937                 (int64_t)0, INT64_MAX, x, y, 100)
1938 {
1939         this->mwindow = mwindow;
1940         this->gui = gui;
1941 }
1942
1943 CWindowMaskAffectedPoint::~CWindowMaskAffectedPoint()
1944 {
1945 }
1946
1947 int CWindowMaskAffectedPoint::handle_event()
1948 {
1949         int total_points = 0;
1950         int affected_point = atol(get_text());
1951         Track *track = mwindow->cwindow->calculate_mask_track();
1952         if(track) {
1953                 MaskAutos *autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1954                 MaskAuto *keyframe = (MaskAuto*)mwindow->cwindow->calculate_affected_auto(autos, 0);
1955                 if( keyframe ) {
1956                         SubMask *mask = keyframe->get_submask(mwindow->edl->session->cwindow_mask);
1957                         total_points = mask->points.size();
1958                 }
1959         }
1960         int active_point = affected_point;
1961         if( affected_point >= total_points )
1962                 affected_point = total_points - 1;
1963         if( affected_point < 0 )
1964                 affected_point = 0;
1965         if( active_point != affected_point )
1966                 update((int64_t)affected_point);
1967         mwindow->cwindow->gui->affected_point = affected_point;
1968         gui->update();
1969         gui->update_preview();
1970         return 1;
1971 }
1972
1973
1974 CWindowMaskFocus::CWindowMaskFocus(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
1975  : BC_CheckBox(x, y, gui->focused, _("Focus"))
1976 {
1977         this->mwindow = mwindow;
1978         this->gui = gui;
1979         set_tooltip(_("Center for rotate/scale"));
1980 }
1981
1982 CWindowMaskFocus::~CWindowMaskFocus()
1983 {
1984 }
1985
1986 int CWindowMaskFocus::handle_event()
1987 {
1988         gui->focused = get_value();
1989         gui->update();
1990         gui->update_preview();
1991         return 1;
1992 }
1993
1994 CWindowMaskHelp::CWindowMaskHelp(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
1995  : BC_CheckBox(x, y, 0, _("Help"))
1996 {
1997         this->mwindow = mwindow;
1998         this->gui = gui;
1999         set_tooltip(_("Show help text"));
2000 }
2001
2002 CWindowMaskHelp::~CWindowMaskHelp()
2003 {
2004 }
2005
2006 int CWindowMaskHelp::handle_event()
2007 {
2008         gui->helped = get_value();
2009         gui->resize_window(gui->get_w(),
2010                 gui->helped ? gui->help_h : gui->help_y);
2011         gui->update();
2012         return 1;
2013 }
2014
2015 CWindowMaskDrawMarkers::CWindowMaskDrawMarkers(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2016  : BC_CheckBox(x, y, gui->markers, _("Markers"))
2017 {
2018         this->mwindow = mwindow;
2019         this->gui = gui;
2020         set_tooltip("Display points");
2021 }
2022
2023 CWindowMaskDrawMarkers::~CWindowMaskDrawMarkers()
2024 {
2025 }
2026
2027 int CWindowMaskDrawMarkers::handle_event()
2028 {
2029         gui->markers = get_value();
2030         gui->update();
2031         gui->update_preview();
2032         return 1;
2033 }
2034
2035 CWindowMaskDrawBoundary::CWindowMaskDrawBoundary(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2036  : BC_CheckBox(x, y, gui->boundary, _("Boundary"))
2037 {
2038         this->mwindow = mwindow;
2039         this->gui = gui;
2040         set_tooltip("Display mask outline");
2041 }
2042
2043 CWindowMaskDrawBoundary::~CWindowMaskDrawBoundary()
2044 {
2045 }
2046
2047 int CWindowMaskDrawBoundary::handle_event()
2048 {
2049         gui->boundary = get_value();
2050         gui->update();
2051         gui->update_preview();
2052         return 1;
2053 }
2054
2055
2056 CWindowMaskFeather::CWindowMaskFeather(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2057  : BC_TumbleTextBox(gui, 0, -FEATHER_MAX, FEATHER_MAX, x, y, 64, 2)
2058 {
2059         this->mwindow = mwindow;
2060         this->gui = gui;
2061 }
2062 CWindowMaskFeather::~CWindowMaskFeather()
2063 {
2064 }
2065
2066 int CWindowMaskFeather::update(float v)
2067 {
2068         gui->feather_slider->update(v);
2069         return BC_TumbleTextBox::update(v);
2070 }
2071
2072 int CWindowMaskFeather::update_value(float v)
2073 {
2074         MaskAutos *autos;
2075         MaskAuto *keyframe;
2076         Track *track;
2077         MaskPoint *point;
2078         SubMask *mask;
2079 #ifdef USE_KEYFRAME_SPANNING
2080         int create_it = 0;
2081 #else
2082         int create_it = 1;
2083 #endif
2084
2085         mwindow->undo->update_undo_before(_("mask feather"), this);
2086
2087 // Get existing keyframe
2088         gui->get_keyframe(track, autos, keyframe,
2089                         mask, point, create_it);
2090         if( track ) {
2091                 int gang = gui->gang_feather->get_value();
2092 #ifdef USE_KEYFRAME_SPANNING
2093                 MaskAuto temp_keyframe(mwindow->edl, autos);
2094                 temp_keyframe.copy_data(keyframe);
2095                 keyframe = &temp_keyframe;
2096 #endif
2097                 float change = v - mask->feather;
2098                 int k = mwindow->edl->session->cwindow_mask;
2099                 int n = gang ? keyframe->masks.size() : k+1;
2100                 for( int i=gang? 0 : k; i<n; ++i ) {
2101                         SubMask *sub_mask = keyframe->get_submask(i);
2102                         float feather = sub_mask->feather + change;
2103                         bclamp(feather, -FEATHER_MAX, FEATHER_MAX);
2104                         sub_mask->feather = feather;
2105                 }
2106 #ifdef USE_KEYFRAME_SPANNING
2107                 autos->update_parameter(keyframe);
2108 #endif
2109                 gui->update_preview();
2110         }
2111
2112         mwindow->undo->update_undo_after(_("mask feather"), LOAD_AUTOMATION);
2113         return 1;
2114 }
2115
2116 int CWindowMaskFeather::handle_event()
2117 {
2118         float v = atof(get_text());
2119         gui->feather_slider->update(v);
2120         return gui->feather->update_value(v);
2121 }
2122
2123 CWindowMaskFeatherSlider::CWindowMaskFeatherSlider(MWindow *mwindow,
2124                 CWindowMaskGUI *gui, int x, int y, int w, float v)
2125  : BC_FSlider(x, y, 0, w, w, -FEATHER_MAX, FEATHER_MAX, v)
2126 {
2127         this->mwindow = mwindow;
2128         this->gui = gui;
2129         set_precision(0.01);
2130         timer = new Timer();
2131         stick = 0;
2132         last_v = 0;
2133 }
2134
2135 CWindowMaskFeatherSlider::~CWindowMaskFeatherSlider()
2136 {
2137         delete timer;
2138 }
2139
2140 int CWindowMaskFeatherSlider::handle_event()
2141 {
2142         float v = get_value();
2143         if( stick > 0 ) {
2144                 int64_t ms = timer->get_difference();
2145                 if( ms < 250 && --stick > 0 ) {
2146                         if( get_value() == 0 ) return 1;
2147                         update(v = 0);
2148                 }
2149                 else {
2150                         stick = 0;
2151                         last_v = v;
2152                 }
2153         }
2154         else if( (last_v>=0 && v<0) || (last_v<0 && v>=0) ) {
2155                 stick = 16;
2156                 v = 0;
2157         }
2158         else
2159                 last_v = v;
2160         timer->update();
2161         gui->feather->BC_TumbleTextBox::update(v);
2162         return gui->feather->update_value(v);
2163 }
2164
2165 int CWindowMaskFeatherSlider::update(float v)
2166 {
2167         return BC_FSlider::update(v);
2168 }
2169
2170 CWindowMaskFade::CWindowMaskFade(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2171  : BC_TumbleTextBox(gui, 0, -100.f, 100.f, x, y, 64, 2)
2172 {
2173         this->mwindow = mwindow;
2174         this->gui = gui;
2175 }
2176 CWindowMaskFade::~CWindowMaskFade()
2177 {
2178 }
2179
2180 int CWindowMaskFade::update(float v)
2181 {
2182         gui->fade_slider->update(v);
2183         return BC_TumbleTextBox::update(v);
2184 }
2185
2186 int CWindowMaskFade::update_value(float v)
2187 {
2188         MaskAutos *autos;
2189         MaskAuto *keyframe;
2190         Track *track;
2191         MaskPoint *point;
2192         SubMask *mask;
2193 #ifdef USE_KEYFRAME_SPANNING
2194         int create_it = 0;
2195 #else
2196         int create_it = 1;
2197 #endif
2198
2199         mwindow->undo->update_undo_before(_("mask fade"), this);
2200
2201 // Get existing keyframe
2202         gui->get_keyframe(track, autos, keyframe,
2203                         mask, point, create_it);
2204         if( track ) {
2205                 int gang = gui->gang_fader->get_value();
2206 #ifdef USE_KEYFRAME_SPANNING
2207                 MaskAuto temp_keyframe(mwindow->edl, autos);
2208                 temp_keyframe.copy_data(keyframe);
2209                 keyframe = &temp_keyframe;
2210 #endif
2211                 float change = v - mask->fader;
2212                 int k = mwindow->edl->session->cwindow_mask;
2213                 int n = gang ? keyframe->masks.size() : k+1;
2214                 for( int i=gang? 0 : k; i<n; ++i ) {
2215                         SubMask *sub_mask = keyframe->get_submask(i);
2216                         float fader = sub_mask->fader + change;
2217                         bclamp(fader, -100.f, 100.f);
2218                         sub_mask->fader = fader;
2219                 }
2220 #ifdef USE_KEYFRAME_SPANNING
2221                 autos->update_parameter(keyframe);
2222 #endif
2223                 gui->update_preview();
2224         }
2225
2226         mwindow->undo->update_undo_after(_("mask fade"), LOAD_AUTOMATION);
2227         return 1;
2228 }
2229
2230 int CWindowMaskFade::handle_event()
2231 {
2232         float v = atof(get_text());
2233         gui->fade_slider->update(v);
2234         return gui->fade->update_value(v);
2235 }
2236
2237 CWindowMaskFadeSlider::CWindowMaskFadeSlider(MWindow *mwindow, CWindowMaskGUI *gui,
2238                 int x, int y, int w)
2239  : BC_ISlider(x, y, 0, w, w, -200, 200, 0)
2240 {
2241         this->mwindow = mwindow;
2242         this->gui = gui;
2243         timer = new Timer();
2244         stick = 0;
2245         last_v = 0;
2246 }
2247
2248 CWindowMaskFadeSlider::~CWindowMaskFadeSlider()
2249 {
2250         delete timer;
2251 }
2252
2253 int CWindowMaskFadeSlider::handle_event()
2254 {
2255         float v = 100*get_value()/200;
2256         if( stick > 0 ) {
2257                 int64_t ms = timer->get_difference();
2258                 if( ms < 250 && --stick > 0 ) {
2259                         if( get_value() == 0 ) return 1;
2260                         update(v = 0);
2261                 }
2262                 else {
2263                         stick = 0;
2264                         last_v = v;
2265                 }
2266         }
2267         else if( (last_v>=0 && v<0) || (last_v<0 && v>=0) ) {
2268                 stick = 16;
2269                 v = 0;
2270         }
2271         else
2272                 last_v = v;
2273         timer->update();
2274         gui->fade->BC_TumbleTextBox::update(v);
2275         return gui->fade->update_value(v);
2276 }
2277
2278 int CWindowMaskFadeSlider::update(int64_t v)
2279 {
2280         return BC_ISlider::update(200*v/100);
2281 }
2282
2283 CWindowMaskGangFader::CWindowMaskGangFader(MWindow *mwindow,
2284                 CWindowMaskGUI *gui, int x, int y)
2285  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2286 {
2287         this->mwindow = mwindow;
2288         this->gui = gui;
2289         set_tooltip(_("Gang fader"));
2290 }
2291
2292 CWindowMaskGangFader::~CWindowMaskGangFader()
2293 {
2294 }
2295
2296 int CWindowMaskGangFader::handle_event()
2297 {
2298         return 1;
2299 }
2300
2301 CWindowMaskGangFocus::CWindowMaskGangFocus(MWindow *mwindow,
2302                 CWindowMaskGUI *gui, int x, int y)
2303  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2304 {
2305         this->mwindow = mwindow;
2306         this->gui = gui;
2307         set_tooltip(_("Gang rotate/scale/translate"));
2308 }
2309
2310 CWindowMaskGangFocus::~CWindowMaskGangFocus()
2311 {
2312 }
2313
2314 int CWindowMaskGangFocus::handle_event()
2315 {
2316         return 1;
2317 }
2318
2319 CWindowMaskBeforePlugins::CWindowMaskBeforePlugins(CWindowMaskGUI *gui, int x, int y)
2320  : BC_CheckBox(x,
2321         y,
2322         1,
2323         _("Apply mask before plugins"))
2324 {
2325         this->gui = gui;
2326 }
2327
2328 int CWindowMaskBeforePlugins::handle_event()
2329 {
2330         Track *track;
2331         MaskAutos *autos;
2332         MaskAuto *keyframe;
2333         SubMask *mask;
2334         MaskPoint *point;
2335         gui->get_keyframe(track, autos, keyframe, mask, point, 1);
2336
2337         if (keyframe) {
2338                 int v = get_value();
2339 #ifdef USE_KEYFRAME_SPANNING
2340                 MaskAuto temp_keyframe(gui->mwindow->edl, autos);
2341                 temp_keyframe.copy_data(keyframe);
2342                 temp_keyframe.apply_before_plugins = v;
2343                 autos->update_parameter(&temp_keyframe);
2344 #else
2345                 keyframe->apply_before_plugins = v;
2346 #endif
2347                 gui->update_preview();
2348         }
2349         return 1;
2350 }
2351
2352
2353 CWindowDisableOpenGLMasking::CWindowDisableOpenGLMasking(CWindowMaskGUI *gui, int x, int y)
2354  : BC_CheckBox(x, y, 1, _("Disable OpenGL masking"))
2355 {
2356         this->gui = gui;
2357 }
2358
2359 int CWindowDisableOpenGLMasking::handle_event()
2360 {
2361         Track *track;
2362         MaskAutos *autos;
2363         MaskAuto *keyframe;
2364         SubMask *mask;
2365         MaskPoint *point;
2366         gui->get_keyframe(track, autos, keyframe, mask, point, 1);
2367
2368         if( keyframe ) {
2369                 int v = get_value();
2370 #ifdef USE_KEYFRAME_SPANNING
2371                 MaskAuto temp_keyframe(gui->mwindow->edl, autos);
2372                 temp_keyframe.copy_data(keyframe);
2373                 temp_keyframe.disable_opengl_masking = v;
2374                 autos->update_parameter(&temp_keyframe);
2375 #else
2376                 keyframe->disable_opengl_masking = v;
2377 #endif
2378                 gui->update_preview();
2379         }
2380         return 1;
2381 }
2382
2383
2384 CWindowMaskClrMask::CWindowMaskClrMask(MWindow *mwindow,
2385                 CWindowMaskGUI *gui, int x, int y)
2386  : BC_Button(x, y, mwindow->theme->get_image_set("reset_button"))
2387 {
2388         this->mwindow = mwindow;
2389         this->gui = gui;
2390         set_tooltip(_("Delete all masks"));
2391 }
2392
2393 CWindowMaskClrMask::~CWindowMaskClrMask()
2394 {
2395 }
2396
2397 int CWindowMaskClrMask::calculate_w(MWindow *mwindow)
2398 {
2399         VFrame *vfrm = *mwindow->theme->get_image_set("reset_button");
2400         return vfrm->get_w();
2401 }
2402
2403 int CWindowMaskClrMask::handle_event()
2404 {
2405         MaskAutos *autos;
2406         MaskAuto *keyframe;
2407         Track *track;
2408         MaskPoint *point;
2409         SubMask *mask;
2410
2411 // Get existing keyframe
2412         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
2413
2414         if( track ) {
2415                 mwindow->undo->update_undo_before(_("del masks"), 0);
2416                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->clear_all();
2417                 mwindow->undo->update_undo_after(_("del masks"), LOAD_AUTOMATION);
2418         }
2419
2420         gui->update();
2421         gui->update_preview(1);
2422         return 1;
2423 }
2424
2425 CWindowMaskGangFeather::CWindowMaskGangFeather(MWindow *mwindow,
2426                 CWindowMaskGUI *gui, int x, int y)
2427  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2428 {
2429         this->mwindow = mwindow;
2430         this->gui = gui;
2431         set_tooltip(_("Gang feather"));
2432 }
2433
2434 CWindowMaskGangFeather::~CWindowMaskGangFeather()
2435 {
2436 }
2437
2438 int CWindowMaskGangFeather::handle_event()
2439 {
2440         return 1;
2441 }
2442
2443 CWindowMaskGUI::CWindowMaskGUI(MWindow *mwindow, CWindowTool *thread)
2444  : CWindowToolGUI(mwindow, thread,
2445         _(PROGRAM_NAME ": Mask"), 430, 680)
2446 {
2447         this->mwindow = mwindow;
2448         this->thread = thread;
2449         active_point = 0;
2450         fade = 0;
2451         feather = 0;
2452         focused = 0;
2453         markers = 1;
2454         boundary = 1;
2455 }
2456 CWindowMaskGUI::~CWindowMaskGUI()
2457 {
2458         lock_window("CWindowMaskGUI::~CWindowMaskGUI");
2459         done_event();
2460         delete active_point;
2461         delete fade;
2462         delete feather;
2463         unlock_window();
2464 }
2465
2466 void CWindowMaskGUI::create_objects()
2467 {
2468         int x = 10, y = 10, margin = mwindow->theme->widget_border;
2469         int clr_w = CWindowMaskClrMask::calculate_w(mwindow);
2470         int clr_x = get_w()-x - clr_w;
2471         int del_w = CWindowMaskDelMask::calculate_w(this,_("Delete"));
2472         int del_x = clr_x-2*margin - del_w;
2473
2474         lock_window("CWindowMaskGUI::create_objects");
2475         BC_TitleBar *title_bar;
2476         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks on Track")));
2477         y += title_bar->get_h() + margin;
2478         BC_Title *title;
2479         add_subwindow(title = new BC_Title(x,y, _("Track:")));
2480         int x1 = x + 90;
2481         Track *track = mwindow->cwindow->calculate_affected_track();
2482         const char *text = track ? track->title : "";
2483         mwindow->cwindow->mask_track_id = track ? track->get_id() : -1;
2484         mask_on_track = new CWindowMaskOnTrack(mwindow, this, x1, y, 100, text);
2485         mask_on_track->create_objects();
2486         mask_on_track->set_tooltip(_("Video track"));
2487         int x2 = x1 + mask_on_track->get_w();
2488         add_subwindow(mask_track_tumbler = new CWindowMaskTrackTumbler(mwindow, this, x2, y));
2489         mwindow->edl->local_session->solo_track_id = -1;
2490         x2 = del_x + (del_w - CWindowMaskSoloTrack::calculate_w(this)) / 2;
2491         add_subwindow(mask_solo_track = new CWindowMaskSoloTrack(mwindow, this, x2, y, 0));
2492         y += mask_on_track->get_h() + margin;
2493         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks")));
2494         y += title_bar->get_h() + margin;
2495         add_subwindow(title = new BC_Title(x, y, _("Mask:")));
2496         mask_name = new CWindowMaskName(mwindow, this, x1, y, "");
2497         mask_name->create_objects();
2498         mask_name->set_tooltip(_("Mask name"));
2499         add_subwindow(clr_mask = new CWindowMaskClrMask(mwindow, this, clr_x, y));
2500         add_subwindow(del_mask = new CWindowMaskDelMask(mwindow, this, del_x, y));
2501         y += mask_name->get_h() + 2*margin;
2502
2503         add_subwindow(title = new BC_Title(x, y, _("Select:")));
2504         int bw = 0, bh = 0;
2505         BC_CheckBox::calculate_extents(this, &bw, &bh);
2506         int bdx = bw + 2*margin;
2507         x2 = x1;
2508         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2509                 int v = i == mwindow->edl->session->cwindow_mask ? 1 : 0;
2510                 mask_buttons[i] = new CWindowMaskButton(mwindow, this, x2, y, i, v);
2511                 add_subwindow(mask_buttons[i]);
2512         }
2513         x2 += margin;
2514         add_subwindow(mask_thumbler = new CWindowMaskThumbler(mwindow, this, x2, y));
2515         y += bh + margin;
2516         x2 = x1;
2517         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2518                 char text[BCSTRLEN];  sprintf(text, "%d", i);
2519                 int tx = (bw - get_text_width(MEDIUMFONT, text)) / 2;
2520                 mask_blabels[i] = new BC_Title(x2+tx, y, text);
2521                 add_subwindow(mask_blabels[i]);
2522         }
2523         y += mask_blabels[0]->get_h() + margin;
2524         add_subwindow(unclr_mask = new CWindowMaskUnclear(mwindow, this, x, y, x1-x-2*margin));
2525         x2 = x1;
2526         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2527                 mask_enables[i] = new CWindowMaskEnable(mwindow, this, x2, y, i, 1);
2528                 add_subwindow(mask_enables[i]);
2529         }
2530         y += mask_enables[0]->get_h() + 2*margin;
2531         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Fade & Feather")));
2532         y += title_bar->get_h() + margin;
2533
2534         add_subwindow(title = new BC_Title(x, y, _("Fade:")));
2535         fade = new CWindowMaskFade(mwindow, this, x1, y);
2536         fade->create_objects();
2537         x2 = x1 + fade->get_w() + 2*margin;
2538         int w2 = clr_x-2*margin - x2;
2539         add_subwindow(fade_slider = new CWindowMaskFadeSlider(mwindow, this, x2, y, w2));
2540         add_subwindow(gang_fader = new CWindowMaskGangFader(mwindow, this, clr_x, y));
2541         y += fade->get_h() + margin;
2542         add_subwindow(title = new BC_Title(x, y, _("Feather:")));
2543         feather = new CWindowMaskFeather(mwindow, this, x1, y);
2544         feather->create_objects();
2545         w2 = clr_x - 2*margin - x2;
2546         feather_slider = new CWindowMaskFeatherSlider(mwindow, this, x2, y, w2, 0);
2547         add_subwindow(feather_slider);
2548         add_subwindow(gang_feather = new CWindowMaskGangFeather(mwindow, this, clr_x, y));
2549         y += feather->get_h() + 2*margin;
2550         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Mask Points")));
2551         y += title_bar->get_h() + margin;
2552
2553         add_subwindow(title = new BC_Title(x, y, _("Point:")));
2554         active_point = new CWindowMaskAffectedPoint(mwindow, this, x1, y);
2555         active_point->create_objects();
2556         add_subwindow(del_point = new CWindowMaskDelPoint(mwindow, this, del_x, y));
2557         y += active_point->get_h() + margin;
2558         add_subwindow(title = new BC_Title(x, y, "X:"));
2559         this->x = new CWindowCoord(this, x1, y, (float)0.0);
2560         this->x->create_objects();
2561         add_subwindow(draw_markers = new CWindowMaskDrawMarkers(mwindow, this, del_x, y));
2562         y += this->x->get_h() + margin;
2563         add_subwindow(title = new BC_Title(x, y, "Y:"));
2564         this->y = new CWindowCoord(this, x1, y, (float)0.0);
2565         this->y->create_objects();
2566         add_subwindow(draw_boundary = new CWindowMaskDrawBoundary(mwindow, this, del_x, y));
2567         y += this->y->get_h() + 2*margin;
2568         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Pivot Point")));
2569         y += title_bar->get_h() + margin;
2570
2571         add_subwindow(title = new BC_Title(x, y, "X:"));
2572         float cx = mwindow->edl->session->output_w / 2.f;
2573         focus_x = new CWindowCoord(this, x1, y, cx);
2574         focus_x->create_objects();
2575         add_subwindow(focus = new CWindowMaskFocus(mwindow, this, del_x, y));
2576         add_subwindow(gang_focus = new CWindowMaskGangFocus(mwindow, this, clr_x, y));
2577         y += focus_x->get_h() + margin;
2578         add_subwindow(title = new BC_Title(x, y, "Y:"));
2579         float cy = mwindow->edl->session->output_h / 2.f;
2580         focus_y = new CWindowCoord(this, x1, y, cy);
2581         focus_y->create_objects();
2582         y += focus_x->get_h() + 2*margin;
2583         BC_Bar *bar;
2584         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2585         y += bar->get_h() + margin;
2586         add_subwindow(this->apply_before_plugins = new CWindowMaskBeforePlugins(this, 10, y));
2587         y += this->apply_before_plugins->get_h();
2588         add_subwindow(this->disable_opengl_masking = new CWindowDisableOpenGLMasking(this, 10, y));
2589         add_subwindow(help = new CWindowMaskHelp(mwindow, this, del_x, y));
2590         y += this->disable_opengl_masking->get_h() + 2*margin;
2591         help_y = y;
2592         add_subwindow(new BC_Bar(x, y, get_w()-2*x));
2593         y += bar->get_h() + 2*margin;
2594         add_subwindow(title = new BC_Title(x, y, _(
2595                 "Shift+LMB: move an end point\n"
2596                 "Ctrl+LMB: move a control point\n"
2597                 "Alt+LMB: to drag translate the mask\n"
2598                 "Shift+Key Delete to delete the point\n"
2599                 "Shift+MMB: Set Pivot Point at pointer\n"
2600                 "Wheel: rotate around Pivot Point\n"
2601                 "Shift+Wheel: scale around Pivot Point\n"
2602                 "Ctrl Wheel: rotate/scale around pointer")));
2603         help_h = y + title->get_h() + 2*margin;
2604         update();
2605         resize_window(get_w(), help_y);
2606         unlock_window();
2607 }
2608
2609 int CWindowMaskGUI::close_event()
2610 {
2611         done_event();
2612         return CWindowToolGUI::close_event();
2613 }
2614
2615 void CWindowMaskGUI::done_event()
2616 {
2617         if( mwindow->in_destructor ) return;
2618         int &solo_track_id = mwindow->edl->local_session->solo_track_id;
2619         if( solo_track_id >= 0 ) {
2620                 solo_track_id = -1;
2621                 update_preview();
2622         }
2623 }
2624
2625 void CWindowMaskGUI::get_keyframe(Track* &track,
2626                 MaskAutos* &autos, MaskAuto* &keyframe,
2627                 SubMask* &mask, MaskPoint* &point, int create_it)
2628 {
2629         autos = 0;
2630         keyframe = 0;
2631
2632         track = mwindow->cwindow->calculate_mask_track();
2633         if( !track )
2634                 track = mwindow->cwindow->calculate_affected_track();
2635                 
2636         if(track) {
2637                 autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
2638                 keyframe = (MaskAuto*)mwindow->cwindow->calculate_affected_auto(
2639                         autos,
2640                         create_it);
2641         }
2642
2643         mask = !keyframe ? 0 :
2644                 keyframe->get_submask(mwindow->edl->session->cwindow_mask);
2645
2646         point = 0;
2647         if( keyframe ) {
2648                 if( mwindow->cwindow->gui->affected_point < mask->points.total &&
2649                         mwindow->cwindow->gui->affected_point >= 0 ) {
2650                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2651                 }
2652         }
2653 }
2654
2655 void CWindowMaskGUI::update()
2656 {
2657         Track *track;
2658         MaskAutos *autos;
2659         MaskAuto *keyframe;
2660         SubMask *mask;
2661         MaskPoint *point;
2662 //printf("CWindowMaskGUI::update 1\n");
2663         get_keyframe(track, autos, keyframe, mask, point, 0);
2664         mwindow->cwindow->mask_track_id = track ? track->get_id() : -1;
2665         mask_on_track->set_back_color(!track || track->record ?
2666                 get_resources()->text_background :
2667                 get_resources()->text_background_disarmed);
2668         mask_on_track->update_items();
2669         mask_on_track->update(!track ? "" : track->title);
2670         mask_name->update_items(keyframe);
2671         const char *text = "";
2672         int sz = !keyframe ? 0 : keyframe->masks.size();
2673         int k = mwindow->edl->session->cwindow_mask;
2674         if( k >= 0 && k < sz )
2675                 text = keyframe->masks[k]->name;
2676         else
2677                 k = mwindow->edl->session->cwindow_mask = 0;
2678         mask_name->update(text);
2679         update_buttons(keyframe, k);
2680         if( point ) {
2681                 x->update(point->x);
2682                 y->update(point->y);
2683         }
2684         if( track ) {
2685                 double position = mwindow->edl->local_session->get_selectionstart(1);
2686                 int64_t position_i = track->to_units(position, 0);
2687                 feather->update(autos->get_feather(position_i, k, PLAY_FORWARD));
2688                 fade->update(autos->get_fader(position_i, k, PLAY_FORWARD));
2689                 int show_mask = track->masks;
2690                 for( int i=0; i<SUBMASKS; ++i )
2691                         mask_enables[i]->update((show_mask>>i) & 1);
2692         }
2693         if( keyframe ) {
2694                 apply_before_plugins->update(keyframe->apply_before_plugins);
2695                 disable_opengl_masking->update(keyframe->disable_opengl_masking);
2696         }
2697         active_point->update((int64_t)mwindow->cwindow->gui->affected_point);
2698 }
2699
2700 void CWindowMaskGUI::handle_event()
2701 {
2702         Track *track;
2703         MaskAuto *keyframe;
2704         MaskAutos *autos;
2705         SubMask *mask;
2706         MaskPoint *point;
2707         get_keyframe(track, autos, keyframe, mask, point, 0);
2708
2709         mwindow->undo->update_undo_before(_("mask point"), this);
2710
2711         if(point)
2712         {
2713 #ifdef USE_KEYFRAME_SPANNING
2714 // Create temp keyframe
2715                 MaskAuto temp_keyframe(mwindow->edl, autos);
2716                 temp_keyframe.copy_data(keyframe);
2717 // Get affected point in temp keyframe
2718                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
2719                 if(mwindow->cwindow->gui->affected_point < mask->points.total &&
2720                         mwindow->cwindow->gui->affected_point >= 0)
2721                 {
2722                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2723                 }
2724
2725                 if(point)
2726                 {
2727                         point->x = atof(x->get_text());
2728                         point->y = atof(y->get_text());
2729 // Commit to spanned keyframes
2730                         autos->update_parameter(&temp_keyframe);
2731                 }
2732 #else
2733                 point->x = atof(x->get_text());
2734                 point->y = atof(y->get_text());
2735 #endif
2736         }
2737
2738         update_preview();
2739         mwindow->undo->update_undo_after(_("mask point"), LOAD_AUTOMATION);
2740 }
2741
2742 void CWindowMaskGUI::set_focused(int v, float cx, float cy)
2743 {
2744         CWindowGUI *cgui = mwindow->cwindow->gui;
2745         cgui->unlock_window();
2746         lock_window("CWindowMaskGUI::set_focused");
2747         if( focused != v )
2748                 focus->update(focused = v);
2749         focus_x->update(cx);
2750         focus_y->update(cy);
2751         unlock_window();
2752         cgui->lock_window("CWindowCanvas::set_focused");
2753 }
2754
2755 void CWindowMaskGUI::update_buttons(MaskAuto *keyframe, int k)
2756 {
2757         int text_color = get_resources()->default_text_color;
2758         int high_color = get_resources()->button_highlighted;
2759         for( int i=0; i<SUBMASKS; ++i ) {
2760                 int color = text_color;
2761                 if( keyframe ) {
2762                         SubMask *submask = keyframe->get_submask(i);
2763                         if( submask && submask->points.size() )
2764                                 color = high_color;
2765                 }
2766                 mask_blabels[i]->set_color(color);
2767                 mask_buttons[i]->update(i==k ? 1 : 0);
2768         }
2769 }
2770
2771 CWindowRulerGUI::CWindowRulerGUI(MWindow *mwindow, CWindowTool *thread)
2772  : CWindowToolGUI(mwindow, thread, _(PROGRAM_NAME ": Ruler"), 320, 240)
2773 {
2774 }
2775
2776 CWindowRulerGUI::~CWindowRulerGUI()
2777 {
2778 }
2779
2780 void CWindowRulerGUI::create_objects()
2781 {
2782         int x = 10, y = 10, x1 = 100;
2783         BC_Title *title;
2784
2785         lock_window("CWindowRulerGUI::create_objects");
2786         add_subwindow(title = new BC_Title(x, y, _("Current:")));
2787         add_subwindow(current = new BC_TextBox(x1, y, 200, 1, ""));
2788         y += title->get_h() + 5;
2789         add_subwindow(title = new BC_Title(x, y, _("Point 1:")));
2790         add_subwindow(point1 = new BC_TextBox(x1, y, 200, 1, ""));
2791         y += title->get_h() + 5;
2792         add_subwindow(title = new BC_Title(x, y, _("Point 2:")));
2793         add_subwindow(point2 = new BC_TextBox(x1, y, 200, 1, ""));
2794         y += title->get_h() + 5;
2795         add_subwindow(title = new BC_Title(x, y, _("Deltas:")));
2796         add_subwindow(deltas = new BC_TextBox(x1, y, 200, 1, ""));
2797         y += title->get_h() + 5;
2798         add_subwindow(title = new BC_Title(x, y, _("Distance:")));
2799         add_subwindow(distance = new BC_TextBox(x1, y, 200, 1, ""));
2800         y += title->get_h() + 5;
2801         add_subwindow(title = new BC_Title(x, y, _("Angle:")));
2802         add_subwindow(angle = new BC_TextBox(x1, y, 200, 1, ""));
2803         y += title->get_h() + 10;
2804         char string[BCTEXTLEN];
2805         sprintf(string,
2806                  _("Press Ctrl to lock ruler to the\nnearest 45%c%c angle."),
2807                 0xc2, 0xb0); // degrees utf
2808         add_subwindow(title = new BC_Title(x,
2809                 y,
2810                 string));
2811         y += title->get_h() + 10;
2812         sprintf(string, _("Press Alt to translate the ruler."));
2813         add_subwindow(title = new BC_Title(x,
2814                 y,
2815                 string));
2816         update();
2817         unlock_window();
2818 }
2819
2820 void CWindowRulerGUI::update()
2821 {
2822         char string[BCTEXTLEN];
2823         int cx = mwindow->session->cwindow_output_x;
2824         int cy = mwindow->session->cwindow_output_y;
2825         sprintf(string, "%d, %d", cx, cy);
2826         current->update(string);
2827         double x1 = mwindow->edl->session->ruler_x1;
2828         double y1 = mwindow->edl->session->ruler_y1;
2829         sprintf(string, "%.0f, %.0f", x1, y1);
2830         point1->update(string);
2831         double x2 = mwindow->edl->session->ruler_x2;
2832         double y2 = mwindow->edl->session->ruler_y2;
2833         sprintf(string, "%.0f, %.0f", x2, y2);
2834         point2->update(string);
2835         double dx = x2 - x1, dy = y2 - y1;
2836         sprintf(string, "%s%.0f, %s%.0f", (dx>=0? "+":""), dx, (dy>=0? "+":""), dy);
2837         deltas->update(string);
2838         double d = sqrt(dx*dx + dy*dy);
2839         sprintf(string, _("%0.01f pixels"), d);
2840         distance->update(string);
2841         double a = d > 0 ? (atan2(-dy, dx) * 180/M_PI) : 0.;
2842         sprintf(string, "%0.02f %c%c", a, 0xc2, 0xb0);
2843         angle->update(string);
2844 }
2845
2846 void CWindowRulerGUI::handle_event()
2847 {
2848 }
2849
2850
2851
2852