add preset mask shapes, add ctr/nrml btns, new igor icons, enables apply to mask...
[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 "file.h"
37 #include "filexml.h"
38 #include "floatauto.h"
39 #include "floatautos.h"
40 #include "keys.h"
41 #include "language.h"
42 #include "localsession.h"
43 #include "mainsession.h"
44 #include "mainundo.h"
45 #include "maskauto.h"
46 #include "maskautos.h"
47 #include "mutex.h"
48 #include "mwindow.h"
49 #include "mwindowgui.h"
50 #include "theme.h"
51 #include "track.h"
52 #include "tracks.h"
53 #include "trackcanvas.h"
54 #include "transportque.h"
55
56
57 CWindowTool::CWindowTool(MWindow *mwindow, CWindowGUI *gui)
58  : Thread(1, 0, 0)
59 {
60         this->mwindow = mwindow;
61         this->gui = gui;
62         tool_gui = 0;
63         done = 0;
64         current_tool = CWINDOW_NONE;
65         set_synchronous(1);
66         input_lock = new Condition(0, "CWindowTool::input_lock");
67         output_lock = new Condition(1, "CWindowTool::output_lock");
68         tool_gui_lock = new Mutex("CWindowTool::tool_gui_lock");
69 }
70
71 CWindowTool::~CWindowTool()
72 {
73         done = 1;
74         stop_tool();
75         input_lock->unlock();
76         Thread::join();
77         delete input_lock;
78         delete output_lock;
79         delete tool_gui_lock;
80 }
81
82 void CWindowTool::start_tool(int operation)
83 {
84         CWindowToolGUI *new_gui = 0;
85         int result = 0;
86
87 //printf("CWindowTool::start_tool 1\n");
88         if(current_tool != operation)
89         {
90                 int previous_tool = current_tool;
91                 current_tool = operation;
92                 switch(operation)
93                 {
94                         case CWINDOW_EYEDROP:
95                                 new_gui = new CWindowEyedropGUI(mwindow, this);
96                                 break;
97                         case CWINDOW_CROP:
98                                 new_gui = new CWindowCropGUI(mwindow, this);
99                                 break;
100                         case CWINDOW_CAMERA:
101                                 new_gui = new CWindowCameraGUI(mwindow, this);
102                                 break;
103                         case CWINDOW_PROJECTOR:
104                                 new_gui = new CWindowProjectorGUI(mwindow, this);
105                                 break;
106                         case CWINDOW_MASK:
107                                 new_gui = new CWindowMaskGUI(mwindow, this);
108                                 break;
109                         case CWINDOW_RULER:
110                                 new_gui = new CWindowRulerGUI(mwindow, this);
111                                 break;
112                         case CWINDOW_PROTECT:
113                                 mwindow->edl->session->tool_window = 0;
114                                 gui->composite_panel->operation[CWINDOW_TOOL_WINDOW]->update(0);
115                                 // fall thru
116                         default:
117                                 result = 1;
118                                 stop_tool();
119                                 break;
120                 }
121
122 //printf("CWindowTool::start_tool 1\n");
123
124
125                 if(!result)
126                 {
127                         stop_tool();
128 // Wait for previous tool GUI to finish
129                         output_lock->lock("CWindowTool::start_tool");
130                         this->tool_gui = new_gui;
131                         tool_gui->create_objects();
132                         if( previous_tool == CWINDOW_PROTECT || previous_tool == CWINDOW_NONE ) {
133                                 mwindow->edl->session->tool_window = 1;
134                                 gui->composite_panel->operation[CWINDOW_TOOL_WINDOW]->update(1);
135                         }
136                         update_show_window();
137
138 // Signal thread to run next tool GUI
139                         input_lock->unlock();
140                 }
141 //printf("CWindowTool::start_tool 1\n");
142         }
143         else
144         if(tool_gui)
145         {
146                 tool_gui->lock_window("CWindowTool::start_tool");
147                 tool_gui->update();
148                 tool_gui->unlock_window();
149         }
150
151 //printf("CWindowTool::start_tool 2\n");
152
153 }
154
155
156 void CWindowTool::stop_tool()
157 {
158         if(tool_gui)
159         {
160                 tool_gui->lock_window("CWindowTool::stop_tool");
161                 tool_gui->set_done(0);
162                 tool_gui->unlock_window();
163         }
164 }
165
166 void CWindowTool::show_tool()
167 {
168         if(tool_gui && mwindow->edl->session->tool_window)
169         {
170                 tool_gui->lock_window("CWindowTool::show_tool");
171                 tool_gui->show_window();
172                 tool_gui->unlock_window();
173         }
174 }
175
176 void CWindowTool::hide_tool()
177 {
178         if(tool_gui && mwindow->edl->session->tool_window)
179         {
180                 tool_gui->lock_window("CWindowTool::show_tool");
181                 tool_gui->hide_window();
182                 tool_gui->unlock_window();
183         }
184 }
185
186 void CWindowTool::raise_tool()
187 {
188         if(tool_gui && mwindow->edl->session->tool_window)
189         {
190                 tool_gui->lock_window("CWindowTool::show_tool");
191                 tool_gui->raise_window();
192                 tool_gui->unlock_window();
193         }
194 }
195
196
197 void CWindowTool::run()
198 {
199         while(!done)
200         {
201                 input_lock->lock("CWindowTool::run");
202                 if(!done)
203                 {
204                         tool_gui->run_window();
205                         tool_gui_lock->lock("CWindowTool::run");
206                         delete tool_gui;
207                         tool_gui = 0;
208                         tool_gui_lock->unlock();
209                 }
210                 output_lock->unlock();
211         }
212 }
213
214 void CWindowTool::update_show_window()
215 {
216         if(tool_gui)
217         {
218                 tool_gui->lock_window("CWindowTool::update_show_window");
219
220                 if(mwindow->edl->session->tool_window)
221                 {
222                         tool_gui->update();
223                         tool_gui->show_window();
224                 }
225                 else
226                         tool_gui->hide_window();
227                 tool_gui->flush();
228
229                 tool_gui->unlock_window();
230         }
231 }
232
233 void CWindowTool::raise_window()
234 {
235         if(tool_gui)
236         {
237                 gui->unlock_window();
238                 tool_gui->lock_window("CWindowTool::raise_window");
239                 tool_gui->raise_window();
240                 tool_gui->unlock_window();
241                 gui->lock_window("CWindowTool::raise_window");
242         }
243 }
244
245 void CWindowTool::update_values()
246 {
247         tool_gui_lock->lock("CWindowTool::update_values");
248         if(tool_gui)
249         {
250                 tool_gui->lock_window("CWindowTool::update_values");
251                 tool_gui->update();
252                 tool_gui->flush();
253                 tool_gui->unlock_window();
254         }
255         tool_gui_lock->unlock();
256 }
257
258
259
260
261
262
263
264 CWindowToolGUI::CWindowToolGUI(MWindow *mwindow,
265         CWindowTool *thread,
266         const char *title,
267         int w,
268         int h)
269  : BC_Window(title,
270         mwindow->session->ctool_x,
271         mwindow->session->ctool_y,
272         w,
273         h,
274         w,
275         h,
276         0,
277         0,
278         1)
279 {
280         this->mwindow = mwindow;
281         this->thread = thread;
282         current_operation = 0;
283 }
284
285 CWindowToolGUI::~CWindowToolGUI()
286 {
287 }
288
289 int CWindowToolGUI::close_event()
290 {
291         hide_window();
292         flush();
293         mwindow->edl->session->tool_window = 0;
294         unlock_window();
295         thread->gui->lock_window("CWindowToolGUI::close_event");
296         thread->gui->composite_panel->set_operation(mwindow->edl->session->cwindow_operation);
297         thread->gui->flush();
298         thread->gui->unlock_window();
299         lock_window("CWindowToolGUI::close_event");
300         return 1;
301 }
302
303 int CWindowToolGUI::keypress_event()
304 {
305         int result = 0;
306
307         switch( get_keypress() ) {
308         case 'w':
309         case 'W':
310                 return close_event();
311         case KEY_F1:
312         case KEY_F2:
313         case KEY_F3:
314         case KEY_F4:
315         case KEY_F5:
316         case KEY_F6:
317         case KEY_F7:
318         case KEY_F8:
319         case KEY_F9:
320         case KEY_F10:
321         case KEY_F11:
322         case KEY_F12:
323                 resend_event(thread->gui);
324                 result = 1;
325         }
326
327         return result;
328 }
329
330 int CWindowToolGUI::translation_event()
331 {
332         mwindow->session->ctool_x = get_x();
333         mwindow->session->ctool_y = get_y();
334         return 0;
335 }
336
337
338 void CWindowToolGUI::update_preview(int changed_edl)
339 {
340         unlock_window();
341         draw_preview(changed_edl);
342         lock_window("CWindowToolGUI::update_preview");
343 }
344
345 void CWindowToolGUI::draw_preview(int changed_edl)
346 {
347         CWindowGUI *cgui = mwindow->cwindow->gui;
348         cgui->lock_window("CWindowToolGUI::draw_preview");
349         int change_type = !changed_edl ? CHANGE_PARAMS : CHANGE_EDL;
350         cgui->sync_parameters(change_type, 0, 1);
351         cgui->unlock_window();
352 }
353
354
355 CWindowCoord::CWindowCoord(CWindowToolGUI *gui, int x, int y, float value, int log_increment = 0)
356  : BC_TumbleTextBox(gui, (float)value, (float)-65536, (float)65536, x, y, 100, 3)
357 {
358         this->gui = gui;
359         set_log_floatincrement(log_increment);
360 }
361
362 CWindowCoord::CWindowCoord(CWindowToolGUI *gui, int x, int y, int value)
363  : BC_TumbleTextBox(gui, (int64_t)value, (int64_t)-65536, (int64_t)65536, x, y, 100, 3)
364 {
365         this->gui = gui;
366 }
367 int CWindowCoord::handle_event()
368 {
369         gui->event_caller = this;
370         gui->handle_event();
371         return 1;
372 }
373
374
375 CWindowCropOK::CWindowCropOK(MWindow *mwindow, CWindowToolGUI *gui, int x, int y)
376  : BC_GenericButton(x, y, _("Do it"))
377 {
378         this->mwindow = mwindow;
379         this->gui = gui;
380 }
381 int CWindowCropOK::handle_event()
382 {
383         mwindow->crop_video();
384         return 1;
385 }
386
387
388 int CWindowCropOK::keypress_event()
389 {
390         if(get_keypress() == 0xd)
391         {
392                 handle_event();
393                 return 1;
394         }
395         return 0;
396 }
397
398
399
400
401
402
403
404 CWindowCropGUI::CWindowCropGUI(MWindow *mwindow, CWindowTool *thread)
405  : CWindowToolGUI(mwindow,
406         thread,
407         _(PROGRAM_NAME ": Crop"),
408         330,
409         100)
410 {
411 }
412
413
414 CWindowCropGUI::~CWindowCropGUI()
415 {
416 }
417
418 void CWindowCropGUI::create_objects()
419 {
420         int x = 10, y = 10;
421         BC_Title *title;
422
423         lock_window("CWindowCropGUI::create_objects");
424         int column1 = 0;
425         int pad = MAX(BC_TextBox::calculate_h(this, MEDIUMFONT, 1, 1),
426                 BC_Title::calculate_h(this, "X")) + 5;
427         add_subwindow(title = new BC_Title(x, y, "X1:"));
428         column1 = MAX(column1, title->get_w());
429         y += pad;
430         add_subwindow(title = new BC_Title(x, y, _("W:")));
431         column1 = MAX(column1, title->get_w());
432         y += pad;
433         add_subwindow(new CWindowCropOK(mwindow, thread->tool_gui, x, y));
434
435         x += column1 + 5;
436         y = 10;
437         x1 = new CWindowCoord(thread->tool_gui, x, y,
438                 mwindow->edl->session->crop_x1);
439         x1->create_objects();
440         y += pad;
441         width = new CWindowCoord(thread->tool_gui, x, y,
442                 mwindow->edl->session->crop_x2 - mwindow->edl->session->crop_x1);
443         width->create_objects();
444
445
446         x += x1->get_w() + 10;
447         y = 10;
448         int column2 = 0;
449         add_subwindow(title = new BC_Title(x, y, "Y1:"));
450         column2 = MAX(column2, title->get_w());
451         y += pad;
452         add_subwindow(title = new BC_Title(x, y, _("H:")));
453         column2 = MAX(column2, title->get_w());
454         y += pad;
455
456         y = 10;
457         x += column2 + 5;
458         y1 = new CWindowCoord(thread->tool_gui, x, y,
459                 mwindow->edl->session->crop_y1);
460         y1->create_objects();
461         y += pad;
462         height = new CWindowCoord(thread->tool_gui, x, y,
463                 mwindow->edl->session->crop_y2 - mwindow->edl->session->crop_y1);
464         height->create_objects();
465         unlock_window();
466 }
467
468 void CWindowCropGUI::handle_event()
469 {
470         int new_x1, new_y1;
471         new_x1 = atol(x1->get_text());
472         new_y1 = atol(y1->get_text());
473         if(new_x1 != mwindow->edl->session->crop_x1)
474         {
475                 mwindow->edl->session->crop_x2 = new_x1 +
476                         mwindow->edl->session->crop_x2 -
477                         mwindow->edl->session->crop_x1;
478                 mwindow->edl->session->crop_x1 = new_x1;
479         }
480         if(new_y1 != mwindow->edl->session->crop_y1)
481         {
482                 mwindow->edl->session->crop_y2 = new_y1 +
483                         mwindow->edl->session->crop_y2 -
484                         mwindow->edl->session->crop_y1;
485                 mwindow->edl->session->crop_y1 = atol(y1->get_text());
486         }
487         mwindow->edl->session->crop_x2 = atol(width->get_text()) +
488                 mwindow->edl->session->crop_x1;
489         mwindow->edl->session->crop_y2 = atol(height->get_text()) +
490                 mwindow->edl->session->crop_y1;
491         update();
492         mwindow->cwindow->gui->canvas->redraw(1);
493 }
494
495 void CWindowCropGUI::update()
496 {
497         x1->update((int64_t)mwindow->edl->session->crop_x1);
498         y1->update((int64_t)mwindow->edl->session->crop_y1);
499         width->update((int64_t)mwindow->edl->session->crop_x2 -
500                 mwindow->edl->session->crop_x1);
501         height->update((int64_t)mwindow->edl->session->crop_y2 -
502                 mwindow->edl->session->crop_y1);
503 }
504
505
506 CWindowEyedropGUI::CWindowEyedropGUI(MWindow *mwindow, CWindowTool *thread)
507  : CWindowToolGUI(mwindow, thread, _(PROGRAM_NAME ": Color"), 220, 290)
508 {
509 }
510
511 CWindowEyedropGUI::~CWindowEyedropGUI()
512 {
513 }
514
515 void CWindowEyedropGUI::create_objects()
516 {
517         int margin = mwindow->theme->widget_border;
518         int x = 10 + margin;
519         int y = 10 + margin;
520         int x2 = 70, x3 = x2 + 60;
521         lock_window("CWindowEyedropGUI::create_objects");
522         BC_Title *title0, *title1, *title2, *title3, *title4, *title5, *title6, *title7;
523         add_subwindow(title0 = new BC_Title(x, y,_("X,Y:")));
524         y += title0->get_h() + margin;
525         add_subwindow(title7 = new BC_Title(x, y, _("Radius:")));
526         y += BC_TextBox::calculate_h(this, MEDIUMFONT, 1, 1) + margin;
527
528         add_subwindow(title1 = new BC_Title(x, y, _("Red:")));
529         y += title1->get_h() + margin;
530         add_subwindow(title2 = new BC_Title(x, y, _("Green:")));
531         y += title2->get_h() + margin;
532         add_subwindow(title3 = new BC_Title(x, y, _("Blue:")));
533         y += title3->get_h() + margin;
534
535         add_subwindow(title4 = new BC_Title(x, y, "Y:"));
536         y += title4->get_h() + margin;
537         add_subwindow(title5 = new BC_Title(x, y, "U:"));
538         y += title5->get_h() + margin;
539         add_subwindow(title6 = new BC_Title(x, y, "V:"));
540
541         add_subwindow(current = new BC_Title(x2, title0->get_y(), ""));
542
543         radius = new CWindowCoord(this, x2, title7->get_y(),
544                 mwindow->edl->session->eyedrop_radius);
545         radius->create_objects();
546         radius->set_boundaries((int64_t)0, (int64_t)255);
547
548         add_subwindow(red = new BC_Title(x2, title1->get_y(), "0"));
549         add_subwindow(green = new BC_Title(x2, title2->get_y(), "0"));
550         add_subwindow(blue = new BC_Title(x2, title3->get_y(), "0"));
551         add_subwindow(rgb_hex = new BC_Title(x3, red->get_y(), "#000000"));
552
553         add_subwindow(this->y = new BC_Title(x2, title4->get_y(), "0"));
554         add_subwindow(this->u = new BC_Title(x2, title5->get_y(), "0"));
555         add_subwindow(this->v = new BC_Title(x2, title6->get_y(), "0"));
556         add_subwindow(yuv_hex = new BC_Title(x3, this->y->get_y(), "#000000"));
557
558         y = title6->get_y() + this->v->get_h() + 2*margin;
559         add_subwindow(sample = new BC_SubWindow(x, y, 50, 50));
560         y += sample->get_h() + margin;
561         add_subwindow(use_max = new CWindowEyedropCheckBox(mwindow, this, x, y));
562         update();
563         unlock_window();
564 }
565
566 void CWindowEyedropGUI::update()
567 {
568         char string[BCTEXTLEN];
569         sprintf(string, "%d, %d",
570                 thread->gui->eyedrop_x,
571                 thread->gui->eyedrop_y);
572         current->update(string);
573
574         radius->update((int64_t)mwindow->edl->session->eyedrop_radius);
575
576         LocalSession *local_session = mwindow->edl->local_session;
577         int use_max = local_session->use_max;
578         float r = use_max ? local_session->red_max : local_session->red;
579         float g = use_max ? local_session->green_max : local_session->green;
580         float b = use_max ? local_session->blue_max : local_session->blue;
581         this->red->update(r);
582         this->green->update(g);
583         this->blue->update(b);
584
585         int rx = 255*r + 0.5;  bclamp(rx,0,255);
586         int gx = 255*g + 0.5;  bclamp(gx,0,255);
587         int bx = 255*b + 0.5;  bclamp(bx,0,255);
588         char rgb_text[BCSTRLEN];
589         sprintf(rgb_text, "#%02x%02x%02x", rx, gx, bx);
590         rgb_hex->update(rgb_text);
591         
592         float y, u, v;
593         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
594         this->y->update(y);
595         this->u->update(u);  u += 0.5;
596         this->v->update(v);  v += 0.5;
597
598         int yx = 255*y + 0.5;  bclamp(yx,0,255);
599         int ux = 255*u + 0.5;  bclamp(ux,0,255);
600         int vx = 255*v + 0.5;  bclamp(vx,0,255);
601         char yuv_text[BCSTRLEN];
602         sprintf(yuv_text, "#%02x%02x%02x", yx, ux, vx);
603         yuv_hex->update(yuv_text);
604
605         int rgb = (rx << 16) | (gx << 8) | (bx << 0);
606         sample->set_color(rgb);
607         sample->draw_box(0, 0, sample->get_w(), sample->get_h());
608         sample->set_color(BLACK);
609         sample->draw_rectangle(0, 0, sample->get_w(), sample->get_h());
610         sample->flash();
611 }
612
613 void CWindowEyedropGUI::handle_event()
614 {
615         int new_radius = atoi(radius->get_text());
616         if(new_radius != mwindow->edl->session->eyedrop_radius)
617         {
618                 CWindowGUI *gui = mwindow->cwindow->gui;
619                 if(gui->eyedrop_visible)
620                 {
621                         gui->lock_window("CWindowEyedropGUI::handle_event");
622 // hide it
623                         int rerender;
624                         gui->canvas->do_eyedrop(rerender, 0, 1);
625                 }
626
627                 mwindow->edl->session->eyedrop_radius = new_radius;
628
629                 if(gui->eyedrop_visible)
630                 {
631 // draw it
632                         int rerender;
633                         gui->canvas->do_eyedrop(rerender, 0, 1);
634                         gui->unlock_window();
635                 }
636         }
637 }
638
639
640
641 /* Buttons to control Keyframe-Curve-Mode for Projector or Camera */
642
643 // Configuration for all possible Keyframe Curve Mode toggles
644 struct _CVD {
645         FloatAuto::t_mode mode;
646         bool use_camera;
647         const char* icon_id;
648         const char* tooltip;
649 };
650
651 const _CVD Camera_Crv_Smooth =
652         {       FloatAuto::SMOOTH,
653                 true,
654                 "tan_smooth",
655                 N_("\"smooth\" Curve on current Camera Keyframes")
656         };
657 const _CVD Camera_Crv_Linear =
658         {       FloatAuto::LINEAR,
659                 true,
660                 "tan_linear",
661                 N_("\"linear\" Curve on current Camera Keyframes")
662         };
663 const _CVD Projector_Crv_Smooth =
664         {       FloatAuto::SMOOTH,
665                 false,
666                 "tan_smooth",
667                 N_("\"smooth\" Curve on current Projector Keyframes")
668         };
669 const _CVD Projector_Crv_Linear =
670         {       FloatAuto::LINEAR,
671                 false,
672                 "tan_linear",
673                 N_("\"linear\" Curve on current Projector Keyframes")
674         };
675
676 // Implementation Class für Keyframe Curve Mode buttons
677 //
678 // This button reflects the state of the "current" keyframe
679 // (the nearest keyframe on the left) for all three automation
680 // lines together. Clicking on this button (re)sets the curve
681 // mode for the three "current" keyframes simultanously, but
682 // never creates a new keyframe.
683 //
684 class CWindowCurveToggle : public BC_Toggle
685 {
686 public:
687         CWindowCurveToggle(_CVD mode, MWindow *mwindow, CWindowToolGUI *gui, int x, int y);
688         void check_toggle_state(FloatAuto *x, FloatAuto *y, FloatAuto *z);
689         int handle_event();
690 private:
691         _CVD cfg;
692         MWindow *mwindow;
693         CWindowToolGUI *gui;
694 };
695
696
697 CWindowCurveToggle::CWindowCurveToggle(_CVD mode, MWindow *mwindow, CWindowToolGUI *gui, int x, int y)
698  : BC_Toggle(x, y, mwindow->theme->get_image_set(mode.icon_id), false),
699    cfg(mode)
700 {
701         this->gui = gui;
702         this->mwindow = mwindow;
703         set_tooltip(_(cfg.tooltip));
704 }
705
706 void CWindowCurveToggle::check_toggle_state(FloatAuto *x, FloatAuto *y, FloatAuto *z)
707 {
708 // the toggle state is only set to ON if all
709 // three automation lines have the same curve mode.
710 // For mixed states the toggle stays off.
711         set_value( x->curve_mode == this->cfg.mode &&
712                    y->curve_mode == this->cfg.mode &&
713                    z->curve_mode == this->cfg.mode
714                    ,true // redraw to show new state
715                 );
716 }
717
718 int CWindowCurveToggle::handle_event()
719 {
720         Track *track = mwindow->cwindow->calculate_affected_track();
721         if(track) {
722                 FloatAuto *x=0, *y=0, *z=0;
723                 mwindow->cwindow->calculate_affected_autos(track,
724                         &x, &y, &z, cfg.use_camera, 0,0,0); // don't create new keyframe
725                 if( x ) x->change_curve_mode(cfg.mode);
726                 if( y ) y->change_curve_mode(cfg.mode);
727                 if( z ) z->change_curve_mode(cfg.mode);
728
729                 gui->update();
730                 gui->update_preview();
731         }
732
733         return 1;
734 }
735
736
737 CWindowEyedropCheckBox::CWindowEyedropCheckBox(MWindow *mwindow, 
738         CWindowEyedropGUI *gui, int x, int y)
739  : BC_CheckBox(x, y, mwindow->edl->local_session->use_max, _("Use maximum"))
740 {
741         this->mwindow = mwindow;
742         this->gui = gui;
743 }
744
745 int CWindowEyedropCheckBox::handle_event()
746 {
747         mwindow->edl->local_session->use_max = get_value();
748         
749         gui->update();
750         return 1;
751 }
752
753
754 CWindowCameraGUI::CWindowCameraGUI(MWindow *mwindow, CWindowTool *thread)
755  : CWindowToolGUI(mwindow,
756         thread,
757         _(PROGRAM_NAME ": Camera"),
758         170,
759         170)
760 {
761 }
762 CWindowCameraGUI::~CWindowCameraGUI()
763 {
764 }
765
766 void CWindowCameraGUI::create_objects()
767 {
768         int x = 10, y = 10, x1;
769         Track *track = mwindow->cwindow->calculate_affected_track();
770         FloatAuto *x_auto = 0, *y_auto = 0, *z_auto = 0;
771         BC_Title *title;
772         BC_Button *button;
773
774         lock_window("CWindowCameraGUI::create_objects");
775         if( track ) {
776                 mwindow->cwindow->calculate_affected_autos(track,
777                         &x_auto, &y_auto, &z_auto, 1, 0, 0, 0);
778         }
779
780         add_subwindow(title = new BC_Title(x, y, "X:"));
781         x += title->get_w();
782         this->x = new CWindowCoord(this, x, y,
783                 x_auto ? x_auto->get_value() : (float)0);
784         this->x->create_objects();
785
786
787         y += 30;
788         x = 10;
789         add_subwindow(title = new BC_Title(x, y, "Y:"));
790         x += title->get_w();
791         this->y = new CWindowCoord(this, x, y,
792                 y_auto ? y_auto->get_value() : (float)0);
793         this->y->create_objects();
794         y += 30;
795         x = 10;
796         add_subwindow(title = new BC_Title(x, y, "Z:"));
797         x += title->get_w();
798         this->z = new CWindowCoord(this, x, y,
799                 z_auto ? z_auto->get_value() : (float)1);
800         this->z->create_objects();
801         this->z->set_increment(0.01);
802
803         y += 30;
804         x1 = 10;
805         add_subwindow(button = new CWindowCameraLeft(mwindow, this, x1, y));
806         x1 += button->get_w();
807         add_subwindow(button = new CWindowCameraCenter(mwindow, this, x1, y));
808         x1 += button->get_w();
809         add_subwindow(button = new CWindowCameraRight(mwindow, this, x1, y));
810
811         y += button->get_h();
812         x1 = 10;
813         add_subwindow(button = new CWindowCameraTop(mwindow, this, x1, y));
814         x1 += button->get_w();
815         add_subwindow(button = new CWindowCameraMiddle(mwindow, this, x1, y));
816         x1 += button->get_w();
817         add_subwindow(button = new CWindowCameraBottom(mwindow, this, x1, y));
818 // additional Buttons to control the curve mode of the "current" keyframe
819         x1 += button->get_w() + 15;
820         add_subwindow(this->t_smooth = new CWindowCurveToggle(Camera_Crv_Smooth, mwindow, this, x1, y));
821         x1 += button->get_w();
822         add_subwindow(this->t_linear = new CWindowCurveToggle(Camera_Crv_Linear, mwindow, this, x1, y));
823
824 // fill in current auto keyframe values, set toggle states.
825         this->update();
826         unlock_window();
827 }
828
829 void CWindowCameraGUI::handle_event()
830 {
831         FloatAuto *x_auto = 0;
832         FloatAuto *y_auto = 0;
833         FloatAuto *z_auto = 0;
834         Track *track = mwindow->cwindow->calculate_affected_track();
835         if(track)
836         {
837                 mwindow->undo->update_undo_before(_("camera"), this);
838                 if(event_caller == x)
839                 {
840                         x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
841                                 track->automation->autos[AUTOMATION_CAMERA_X],
842                                 1);
843                         if(x_auto)
844                         {
845                                 x_auto->set_value(atof(x->get_text()));
846                                 update();
847                                 update_preview();
848                         }
849                 }
850                 else
851                 if(event_caller == y)
852                 {
853                         y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
854                                 track->automation->autos[AUTOMATION_CAMERA_Y],
855                                 1);
856                         if(y_auto)
857                         {
858                                 y_auto->set_value(atof(y->get_text()));
859                                 update();
860                                 update_preview();
861                         }
862                 }
863                 else
864                 if(event_caller == z)
865                 {
866                         z_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
867                                 track->automation->autos[AUTOMATION_CAMERA_Z],
868                                 1);
869                         if(z_auto)
870                         {
871                                 float zoom = atof(z->get_text());
872                                 if(zoom > 100.) zoom = 100.;
873                                 else
874                                 if(zoom < 0.01) zoom = 0.01;
875         // Doesn't allow user to enter from scratch
876         //              if(zoom != atof(z->get_text()))
877         //                      z->update(zoom);
878
879                                 z_auto->set_value(zoom);
880                                 mwindow->gui->lock_window("CWindowCameraGUI::handle_event");
881                                 mwindow->gui->draw_overlays(1);
882                                 mwindow->gui->unlock_window();
883                                 update();
884                                 update_preview();
885                         }
886                 }
887
888                 mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
889         }
890 }
891
892 void CWindowCameraGUI::update()
893 {
894         FloatAuto *x_auto = 0;
895         FloatAuto *y_auto = 0;
896         FloatAuto *z_auto = 0;
897         Track *track = mwindow->cwindow->calculate_affected_track();
898         if( track ) {
899                 mwindow->cwindow->calculate_affected_autos(track,
900                         &x_auto, &y_auto, &z_auto, 1, 0, 0, 0);
901         }
902
903         if(x_auto)
904                 x->update(x_auto->get_value());
905         if(y_auto)
906                 y->update(y_auto->get_value());
907         if(z_auto) {
908                 float value = z_auto->get_value();
909                 z->update(value);
910                 thread->gui->lock_window("CWindowCameraGUI::update");
911                 thread->gui->composite_panel->cpanel_zoom->update(value);
912                 thread->gui->unlock_window();
913         }
914
915         if( x_auto && y_auto && z_auto )
916         {
917                 t_smooth->check_toggle_state(x_auto, y_auto, z_auto);
918                 t_linear->check_toggle_state(x_auto, y_auto, z_auto);
919         }
920 }
921
922
923
924
925 CWindowCameraLeft::CWindowCameraLeft(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
926  : BC_Button(x, y, mwindow->theme->get_image_set("left_justify"))
927 {
928         this->gui = gui;
929         this->mwindow = mwindow;
930         set_tooltip(_("Left justify"));
931 }
932 int CWindowCameraLeft::handle_event()
933 {
934         FloatAuto *x_auto = 0;
935         FloatAuto *z_auto = 0;
936         Track *track = mwindow->cwindow->calculate_affected_track();
937         if( track ) {
938                 mwindow->cwindow->calculate_affected_autos(track,
939                         &x_auto, 0, &z_auto, 1, 1, 0, 0);
940         }
941
942         if(x_auto && z_auto)
943         {
944                 int w = 0, h = 0;
945                 track->get_source_dimensions(
946                         mwindow->edl->local_session->get_selectionstart(1),
947                         w,
948                         h);
949
950                 if(w && h)
951                 {
952                         mwindow->undo->update_undo_before(_("camera"), 0);
953                         x_auto->set_value(
954                                 (double)track->track_w / z_auto->get_value() / 2 -
955                                 (double)w / 2);
956                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
957                         gui->update();
958                         gui->update_preview();
959                 }
960         }
961
962         return 1;
963 }
964
965
966 CWindowCameraCenter::CWindowCameraCenter(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
967  : BC_Button(x, y, mwindow->theme->get_image_set("center_justify"))
968 {
969         this->gui = gui;
970         this->mwindow = mwindow;
971         set_tooltip(_("Center horizontal"));
972 }
973 int CWindowCameraCenter::handle_event()
974 {
975         FloatAuto *x_auto = 0;
976         Track *track = mwindow->cwindow->calculate_affected_track();
977         if(track)
978                 x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
979                         track->automation->autos[AUTOMATION_CAMERA_X],
980                         1);
981
982         if(x_auto)
983         {
984                 mwindow->undo->update_undo_before(_("camera"), 0);
985                 x_auto->set_value(0);
986                 gui->update();
987                 gui->update_preview();
988                 mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
989         }
990
991         return 1;
992 }
993
994
995 CWindowCameraRight::CWindowCameraRight(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
996  : BC_Button(x, y, mwindow->theme->get_image_set("right_justify"))
997 {
998         this->gui = gui;
999         this->mwindow = mwindow;
1000         set_tooltip(_("Right justify"));
1001 }
1002 int CWindowCameraRight::handle_event()
1003 {
1004         FloatAuto *x_auto = 0;
1005         FloatAuto *z_auto = 0;
1006         Track *track = mwindow->cwindow->calculate_affected_track();
1007         if( track ) {
1008                 mwindow->cwindow->calculate_affected_autos(track,
1009                         &x_auto, 0, &z_auto, 1, 1, 0, 0);
1010         }
1011
1012         if(x_auto && z_auto)
1013         {
1014                 int w = 0, h = 0;
1015                 track->get_source_dimensions(
1016                         mwindow->edl->local_session->get_selectionstart(1),
1017                         w,
1018                         h);
1019
1020                 if(w && h)
1021                 {
1022                         mwindow->undo->update_undo_before(_("camera"), 0);
1023                         x_auto->set_value( -((double)track->track_w / z_auto->get_value() / 2 -
1024                                 (double)w / 2));
1025                         gui->update();
1026                         gui->update_preview();
1027                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1028                 }
1029         }
1030
1031         return 1;
1032 }
1033
1034
1035 CWindowCameraTop::CWindowCameraTop(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
1036  : BC_Button(x, y, mwindow->theme->get_image_set("top_justify"))
1037 {
1038         this->gui = gui;
1039         this->mwindow = mwindow;
1040         set_tooltip(_("Top justify"));
1041 }
1042 int CWindowCameraTop::handle_event()
1043 {
1044         FloatAuto *y_auto = 0;
1045         FloatAuto *z_auto = 0;
1046         Track *track = mwindow->cwindow->calculate_affected_track();
1047         if( track ) {
1048                 mwindow->cwindow->calculate_affected_autos(track,
1049                         0, &y_auto, &z_auto, 1, 0, 1, 0);
1050         }
1051
1052         if(y_auto && z_auto)
1053         {
1054                 int w = 0, h = 0;
1055                 track->get_source_dimensions(
1056                         mwindow->edl->local_session->get_selectionstart(1),
1057                         w,
1058                         h);
1059
1060                 if(w && h)
1061                 {
1062                         mwindow->undo->update_undo_before(_("camera"), 0);
1063                         y_auto->set_value((double)track->track_h / z_auto->get_value() / 2 -
1064                                 (double)h / 2);
1065                         gui->update();
1066                         gui->update_preview();
1067                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1068                 }
1069         }
1070
1071         return 1;
1072 }
1073
1074
1075 CWindowCameraMiddle::CWindowCameraMiddle(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
1076  : BC_Button(x, y, mwindow->theme->get_image_set("middle_justify"))
1077 {
1078         this->gui = gui;
1079         this->mwindow = mwindow;
1080         set_tooltip(_("Center vertical"));
1081 }
1082 int CWindowCameraMiddle::handle_event()
1083 {
1084         FloatAuto *y_auto = 0;
1085         Track *track = mwindow->cwindow->calculate_affected_track();
1086         if(track)
1087                 y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1088                         track->automation->autos[AUTOMATION_CAMERA_Y], 1);
1089
1090         if(y_auto)
1091         {
1092                 mwindow->undo->update_undo_before(_("camera"), 0);
1093                 y_auto->set_value(0);
1094                 gui->update();
1095                 gui->update_preview();
1096                 mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1097         }
1098
1099         return 1;
1100 }
1101
1102
1103 CWindowCameraBottom::CWindowCameraBottom(MWindow *mwindow, CWindowCameraGUI *gui, int x, int y)
1104  : BC_Button(x, y, mwindow->theme->get_image_set("bottom_justify"))
1105 {
1106         this->gui = gui;
1107         this->mwindow = mwindow;
1108         set_tooltip(_("Bottom justify"));
1109 }
1110 int CWindowCameraBottom::handle_event()
1111 {
1112         FloatAuto *y_auto = 0;
1113         FloatAuto *z_auto = 0;
1114         Track *track = mwindow->cwindow->calculate_affected_track();
1115         if( track ) {
1116                 mwindow->cwindow->calculate_affected_autos(track,
1117                         0, &y_auto, &z_auto, 1, 0, 1, 0);
1118         }
1119
1120         if(y_auto && z_auto)
1121         {
1122                 int w = 0, h = 0;
1123                 track->get_source_dimensions(
1124                         mwindow->edl->local_session->get_selectionstart(1),
1125                         w,
1126                         h);
1127
1128                 if(w && h)
1129                 {
1130                         mwindow->undo->update_undo_before(_("camera"), 0);
1131                         y_auto->set_value(-((double)track->track_h / z_auto->get_value() / 2 -
1132                                 (double)h / 2));
1133                         gui->update();
1134                         gui->update_preview();
1135                         mwindow->undo->update_undo_after(_("camera"), LOAD_ALL);
1136                 }
1137         }
1138
1139         return 1;
1140 }
1141
1142
1143 CWindowProjectorGUI::CWindowProjectorGUI(MWindow *mwindow, CWindowTool *thread)
1144  : CWindowToolGUI(mwindow,
1145         thread,
1146         _(PROGRAM_NAME ": Projector"),
1147         170,
1148         170)
1149 {
1150 }
1151 CWindowProjectorGUI::~CWindowProjectorGUI()
1152 {
1153 }
1154 void CWindowProjectorGUI::create_objects()
1155 {
1156         int x = 10, y = 10, x1;
1157         Track *track = mwindow->cwindow->calculate_affected_track();
1158         FloatAuto *x_auto = 0;
1159         FloatAuto *y_auto = 0;
1160         FloatAuto *z_auto = 0;
1161         BC_Title *title;
1162         BC_Button *button;
1163
1164         lock_window("CWindowProjectorGUI::create_objects");
1165         if( track ) {
1166                 mwindow->cwindow->calculate_affected_autos(track,
1167                         &x_auto, &y_auto, &z_auto, 0, 0, 0, 0);
1168         }
1169
1170         add_subwindow(title = new BC_Title(x, y, "X:"));
1171         x += title->get_w();
1172         this->x = new CWindowCoord(this, x, y,
1173                 x_auto ? x_auto->get_value() : (float)0);
1174         this->x->create_objects();
1175         y += 30;
1176         x = 10;
1177         add_subwindow(title = new BC_Title(x, y, "Y:"));
1178         x += title->get_w();
1179         this->y = new CWindowCoord(this, x, y,
1180                 y_auto ? y_auto->get_value() : (float)0);
1181         this->y->create_objects();
1182         y += 30;
1183         x = 10;
1184         add_subwindow(title = new BC_Title(x, y, "Z:"));
1185         x += title->get_w();
1186         this->z = new CWindowCoord(this, x, y,
1187                 z_auto ? z_auto->get_value() : (float)1);
1188         this->z->create_objects();
1189         this->z->set_increment(0.01);
1190
1191         y += 30;
1192         x1 = 10;
1193         add_subwindow(button = new CWindowProjectorLeft(mwindow, this, x1, y));
1194         x1 += button->get_w();
1195         add_subwindow(button = new CWindowProjectorCenter(mwindow, this, x1, y));
1196         x1 += button->get_w();
1197         add_subwindow(button = new CWindowProjectorRight(mwindow, this, x1, y));
1198
1199         y += button->get_h();
1200         x1 = 10;
1201         add_subwindow(button = new CWindowProjectorTop(mwindow, this, x1, y));
1202         x1 += button->get_w();
1203         add_subwindow(button = new CWindowProjectorMiddle(mwindow, this, x1, y));
1204         x1 += button->get_w();
1205         add_subwindow(button = new CWindowProjectorBottom(mwindow, this, x1, y));
1206
1207 // additional Buttons to control the curve mode of the "current" keyframe
1208         x1 += button->get_w() + 15;
1209         add_subwindow(this->t_smooth = new CWindowCurveToggle(Projector_Crv_Smooth, mwindow, this, x1, y));
1210         x1 += button->get_w();
1211         add_subwindow(this->t_linear = new CWindowCurveToggle(Projector_Crv_Linear, mwindow, this, x1, y));
1212
1213 // fill in current auto keyframe values, set toggle states.
1214         this->update();
1215         unlock_window();
1216 }
1217
1218 void CWindowProjectorGUI::handle_event()
1219 {
1220         FloatAuto *x_auto = 0;
1221         FloatAuto *y_auto = 0;
1222         FloatAuto *z_auto = 0;
1223         Track *track = mwindow->cwindow->calculate_affected_track();
1224
1225         if(track)
1226         {
1227                 mwindow->undo->update_undo_before(_("projector"), this);
1228                 if(event_caller == x)
1229                 {
1230                         x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1231                                 track->automation->autos[AUTOMATION_PROJECTOR_X],
1232                                 1);
1233                         if(x_auto)
1234                         {
1235                                 x_auto->set_value(atof(x->get_text()));
1236                                 update();
1237                                 update_preview();
1238                         }
1239                 }
1240                 else
1241                 if(event_caller == y)
1242                 {
1243                         y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1244                                 track->automation->autos[AUTOMATION_PROJECTOR_Y],
1245                                 1);
1246                         if(y_auto)
1247                         {
1248                                 y_auto->set_value(atof(y->get_text()));
1249                                 update();
1250                                 update_preview();
1251                         }
1252                 }
1253                 else
1254                 if(event_caller == z)
1255                 {
1256                         z_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1257                                 track->automation->autos[AUTOMATION_PROJECTOR_Z],
1258                                 1);
1259                         if(z_auto)
1260                         {
1261                                 float zoom = atof(z->get_text());
1262                                 if(zoom > 100.) zoom = 100.;
1263                                 else if(zoom < 0.01) zoom = 0.01;
1264 //                      if (zoom != atof(z->get_text()))
1265 //                              z->update(zoom);
1266                                 z_auto->set_value(zoom);
1267
1268                                 mwindow->gui->lock_window("CWindowProjectorGUI::handle_event");
1269                                 mwindow->gui->draw_overlays(1);
1270                                 mwindow->gui->unlock_window();
1271
1272                                 update();
1273                                 update_preview();
1274                         }
1275                 }
1276                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1277         }
1278 }
1279
1280 void CWindowProjectorGUI::update()
1281 {
1282         FloatAuto *x_auto = 0;
1283         FloatAuto *y_auto = 0;
1284         FloatAuto *z_auto = 0;
1285         Track *track = mwindow->cwindow->calculate_affected_track();
1286         if( track ) {
1287                 mwindow->cwindow->calculate_affected_autos(track,
1288                         &x_auto, &y_auto, &z_auto, 0, 0, 0, 0);
1289         }
1290
1291         if(x_auto)
1292                 x->update(x_auto->get_value());
1293         if(y_auto)
1294                 y->update(y_auto->get_value());
1295         if(z_auto) {
1296                 float value = z_auto->get_value();
1297                 z->update(value);
1298                 thread->gui->lock_window("CWindowProjectorGUI::update");
1299                 thread->gui->composite_panel->cpanel_zoom->update(value);
1300                 thread->gui->unlock_window();
1301         }
1302
1303         if( x_auto && y_auto && z_auto )
1304         {
1305                 t_smooth->check_toggle_state(x_auto, y_auto, z_auto);
1306                 t_linear->check_toggle_state(x_auto, y_auto, z_auto);
1307         }
1308 }
1309
1310 CWindowProjectorLeft::CWindowProjectorLeft(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1311  : BC_Button(x, y, mwindow->theme->get_image_set("left_justify"))
1312 {
1313         this->gui = gui;
1314         this->mwindow = mwindow;
1315         set_tooltip(_("Left justify"));
1316 }
1317 int CWindowProjectorLeft::handle_event()
1318 {
1319         FloatAuto *x_auto = 0;
1320         FloatAuto *z_auto = 0;
1321         Track *track = mwindow->cwindow->calculate_affected_track();
1322         if( track ) {
1323                 mwindow->cwindow->calculate_affected_autos(track,
1324                         &x_auto, 0, &z_auto, 0, 1, 0, 0);
1325         }
1326         if(x_auto && z_auto)
1327         {
1328                 mwindow->undo->update_undo_before(_("projector"), 0);
1329                 x_auto->set_value( (double)track->track_w * z_auto->get_value() / 2 -
1330                         (double)mwindow->edl->session->output_w / 2 );
1331                 gui->update();
1332                 gui->update_preview();
1333                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1334         }
1335
1336         return 1;
1337 }
1338
1339
1340 CWindowProjectorCenter::CWindowProjectorCenter(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1341  : BC_Button(x, y, mwindow->theme->get_image_set("center_justify"))
1342 {
1343         this->gui = gui;
1344         this->mwindow = mwindow;
1345         set_tooltip(_("Center horizontal"));
1346 }
1347 int CWindowProjectorCenter::handle_event()
1348 {
1349         FloatAuto *x_auto = 0;
1350         Track *track = mwindow->cwindow->calculate_affected_track();
1351         if(track)
1352                 x_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1353                         track->automation->autos[AUTOMATION_PROJECTOR_X],
1354                         1);
1355
1356         if(x_auto)
1357         {
1358                 mwindow->undo->update_undo_before(_("projector"), 0);
1359                 x_auto->set_value(0);
1360                 gui->update();
1361                 gui->update_preview();
1362                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1363         }
1364
1365         return 1;
1366 }
1367
1368
1369 CWindowProjectorRight::CWindowProjectorRight(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1370  : BC_Button(x, y, mwindow->theme->get_image_set("right_justify"))
1371 {
1372         this->gui = gui;
1373         this->mwindow = mwindow;
1374         set_tooltip(_("Right justify"));
1375 }
1376 int CWindowProjectorRight::handle_event()
1377 {
1378         FloatAuto *x_auto = 0;
1379         FloatAuto *z_auto = 0;
1380         Track *track = mwindow->cwindow->calculate_affected_track();
1381         if( track ) {
1382                 mwindow->cwindow->calculate_affected_autos(track,
1383                         &x_auto, 0, &z_auto, 0, 1, 0, 0);
1384         }
1385
1386         if(x_auto && z_auto)
1387         {
1388                 mwindow->undo->update_undo_before(_("projector"), 0);
1389                 x_auto->set_value( -((double)track->track_w * z_auto->get_value() / 2 -
1390                         (double)mwindow->edl->session->output_w / 2));
1391                 gui->update();
1392                 gui->update_preview();
1393                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1394         }
1395
1396         return 1;
1397 }
1398
1399
1400 CWindowProjectorTop::CWindowProjectorTop(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1401  : BC_Button(x, y, mwindow->theme->get_image_set("top_justify"))
1402 {
1403         this->gui = gui;
1404         this->mwindow = mwindow;
1405         set_tooltip(_("Top justify"));
1406 }
1407 int CWindowProjectorTop::handle_event()
1408 {
1409         FloatAuto *y_auto = 0;
1410         FloatAuto *z_auto = 0;
1411         Track *track = mwindow->cwindow->calculate_affected_track();
1412         if( track ) {
1413                 mwindow->cwindow->calculate_affected_autos(track,
1414                         0, &y_auto, &z_auto, 0, 0, 1, 0);
1415         }
1416
1417         if(y_auto && z_auto)
1418         {
1419                 mwindow->undo->update_undo_before(_("projector"), 0);
1420                 y_auto->set_value( (double)track->track_h * z_auto->get_value() / 2 -
1421                         (double)mwindow->edl->session->output_h / 2 );
1422                 gui->update();
1423                 gui->update_preview();
1424                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1425         }
1426
1427         return 1;
1428 }
1429
1430
1431 CWindowProjectorMiddle::CWindowProjectorMiddle(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1432  : BC_Button(x, y, mwindow->theme->get_image_set("middle_justify"))
1433 {
1434         this->gui = gui;
1435         this->mwindow = mwindow;
1436         set_tooltip(_("Center vertical"));
1437 }
1438 int CWindowProjectorMiddle::handle_event()
1439 {
1440         FloatAuto *y_auto = 0;
1441         Track *track = mwindow->cwindow->calculate_affected_track();
1442         if(track)
1443                 y_auto = (FloatAuto*)mwindow->cwindow->calculate_affected_auto(
1444                         track->automation->autos[AUTOMATION_PROJECTOR_Y], 1);
1445
1446         if(y_auto)
1447         {
1448                 mwindow->undo->update_undo_before(_("projector"), 0);
1449                 y_auto->set_value(0);
1450                 gui->update();
1451                 gui->update_preview();
1452                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1453         }
1454
1455         return 1;
1456 }
1457
1458
1459 CWindowProjectorBottom::CWindowProjectorBottom(MWindow *mwindow, CWindowProjectorGUI *gui, int x, int y)
1460  : BC_Button(x, y, mwindow->theme->get_image_set("bottom_justify"))
1461 {
1462         this->gui = gui;
1463         this->mwindow = mwindow;
1464         set_tooltip(_("Bottom justify"));
1465 }
1466 int CWindowProjectorBottom::handle_event()
1467 {
1468         FloatAuto *y_auto = 0;
1469         FloatAuto *z_auto = 0;
1470         Track *track = mwindow->cwindow->calculate_affected_track();
1471         if( track ) {
1472                 mwindow->cwindow->calculate_affected_autos(track,
1473                         0, &y_auto, &z_auto, 0, 0, 1, 0);
1474         }
1475
1476         if(y_auto && z_auto)
1477         {
1478                 mwindow->undo->update_undo_before(_("projector"), 0);
1479                 y_auto->set_value( -((double)track->track_h * z_auto->get_value() / 2 -
1480                         (double)mwindow->edl->session->output_h / 2));
1481                 gui->update();
1482                 gui->update_preview();
1483                 mwindow->undo->update_undo_after(_("projector"), LOAD_ALL);
1484         }
1485
1486         return 1;
1487 }
1488
1489
1490 CWindowMaskOnTrack::CWindowMaskOnTrack(MWindow *mwindow, CWindowMaskGUI *gui,
1491                 int x, int y, int w, const char *text)
1492  : BC_PopupTextBox(gui, 0, text, x, y, w, 120)
1493 {
1494         this->mwindow = mwindow;
1495         this->gui = gui;
1496 }
1497
1498 CWindowMaskOnTrack::~CWindowMaskOnTrack()
1499 {
1500 }
1501
1502 int CWindowMaskOnTrack::handle_event()
1503 {
1504         CWindowMaskItem *track_item = 0;
1505         int k = get_number(), track_id = -1;
1506 //printf("selected %d = %s\n", k, k<0 ? "()" : track_items[k]->get_text());
1507         if( k >= 0 ) {
1508                 track_item = (CWindowMaskItem *)track_items[k];
1509                 Track *track = track_item ? mwindow->edl->tracks->get_track_by_id(track_item->id) : 0;
1510                 if( track && track->record ) track_id = track->get_id();
1511         }
1512         else
1513                 track_id = mwindow->cwindow->mask_track_id;
1514         set_back_color(track_id >= 0 ?
1515                 gui->get_resources()->text_background :
1516                 gui->get_resources()->text_background_disarmed);
1517         if( mwindow->cwindow->mask_track_id != track_id )
1518                 gui->mask_on_track->update(track_item ? track_item->get_text() : "");
1519         mwindow->cwindow->mask_track_id = track_id;
1520         mwindow->edl->local_session->solo_track_id = -1;
1521         gui->mask_solo_track->update(0);
1522         gui->update();
1523         gui->update_preview(1);
1524         return 1;
1525 }
1526
1527 void CWindowMaskOnTrack::update_items()
1528 {
1529         track_items.remove_all_objects();
1530         int high_color = gui->get_resources()->button_highlighted;
1531         for( Track *track=mwindow->edl->tracks->first; track; track=track->next ) {
1532                 if( track->data_type != TRACK_VIDEO ) continue;
1533                 MaskAutos *mask_autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1534                 int color = !track->record ? RED : mask_autos->first ?  high_color : -1;
1535                 MaskAuto *mask_auto = (MaskAuto*)mask_autos->default_auto;
1536                 for( int i=0; color<0 && i<mask_auto->masks.size(); ++i )
1537                         if( mask_auto->masks[i]->points.size() > 0 ) color = high_color;
1538                 track_items.append(new CWindowMaskItem(track->title, track->get_id(), color));
1539         }
1540         update_list(&track_items);
1541 }
1542
1543 CWindowMaskTrackTumbler::CWindowMaskTrackTumbler(MWindow *mwindow, CWindowMaskGUI *gui,
1544                 int x, int y)
1545  : BC_Tumbler(x, y)
1546 {
1547         this->mwindow = mwindow;
1548         this->gui = gui;
1549 }
1550 CWindowMaskTrackTumbler::~CWindowMaskTrackTumbler()
1551 {
1552 }
1553
1554 int CWindowMaskTrackTumbler::handle_up_event()
1555 {
1556         return do_event(1);
1557 }
1558
1559 int CWindowMaskTrackTumbler::handle_down_event()
1560 {
1561         return do_event(-1);
1562 }
1563
1564 int CWindowMaskTrackTumbler::do_event(int dir)
1565 {
1566         CWindowMaskItem *track_item = 0;
1567         CWindowMaskItem **items = (CWindowMaskItem**)&gui->mask_on_track->track_items[0];
1568         int n = gui->mask_on_track->track_items.size();
1569         int id = mwindow->cwindow->mask_track_id;
1570         if( n > 0 ) {   
1571                 int k = n;
1572                 while( --k >= 0 && items[k]->id != id );
1573                 if( k >= 0 ) {
1574                         k += dir;
1575                         bclamp(k, 0, n-1);
1576                         track_item = items[k];
1577                 }
1578                 else
1579                         track_item = items[0];
1580         }
1581         Track *track = track_item ? mwindow->edl->tracks->get_track_by_id(track_item->id) : 0;
1582         int track_id = track_item && track && track->record ? track_item->id : -1;
1583         gui->mask_on_track->set_back_color(track_id >= 0 ?
1584                 gui->get_resources()->text_background :
1585                 gui->get_resources()->text_background_disarmed);
1586         gui->mask_on_track->update(track_item ? track_item->get_text() : "");
1587         mwindow->cwindow->mask_track_id = track_item ? track_item->id : -1;
1588         mwindow->edl->local_session->solo_track_id = -1;
1589         gui->mask_solo_track->update(0);
1590         gui->update();
1591         gui->update_preview(1);
1592         return 1;
1593 }
1594
1595
1596 CWindowMaskName::CWindowMaskName(MWindow *mwindow, CWindowMaskGUI *gui,
1597                 int x, int y, const char *text)
1598  : BC_PopupTextBox(gui, 0, text, x, y, 100, 160)
1599 {
1600         this->mwindow = mwindow;
1601         this->gui = gui;
1602 }
1603
1604 CWindowMaskName::~CWindowMaskName()
1605 {
1606 }
1607
1608 int CWindowMaskName::handle_event()
1609 {
1610         Track *track;
1611         MaskAutos *autos;
1612         MaskAuto *keyframe;
1613         SubMask *mask;
1614         MaskPoint *point;
1615 //printf("CWindowMaskGUI::update 1\n");
1616         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
1617         if( track ) {
1618                 int k = get_number();
1619                 if( k < 0 ) k = mwindow->edl->session->cwindow_mask;
1620                 else mwindow->edl->session->cwindow_mask = k;
1621                 if( k >= 0 && k < mask_items.size() ) {
1622                         mask_items[k]->set_text(get_text());
1623                         update_list(&mask_items);
1624                 }
1625 #ifdef USE_KEYFRAME_SPANNING
1626                 MaskAuto temp_keyframe(mwindow->edl, autos);
1627                 temp_keyframe.copy_data(keyframe);
1628                 SubMask *submask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
1629                 memset(submask->name, 0, sizeof(submask->name));
1630                 strncpy(submask->name, get_text(), sizeof(submask->name)-1);
1631                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->update_parameter(&temp_keyframe);
1632 #else
1633                 for(MaskAuto *current = (MaskAuto*)autos->default_auto; current; ) {
1634                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1635                         memset(submask->name, 0, sizeof(submask->name));
1636                         strncpy(submask->name, get_text(), sizeof(submask->name));
1637                         current = current == (MaskAuto*)autos->default_auto ?
1638                                 (MaskAuto*)autos->first : (MaskAuto*)NEXT;
1639                 }
1640 #endif
1641                 gui->update();
1642                 gui->update_preview();
1643         }
1644         return 1;
1645 }
1646
1647 void CWindowMaskName::update_items(MaskAuto *keyframe)
1648 {
1649         mask_items.remove_all_objects();
1650         int sz = !keyframe ? 0 : keyframe->masks.size();
1651         for( int i=0; i<SUBMASKS; ++i ) {
1652                 char text[BCSTRLEN];
1653                 if( i < sz ) {
1654                         SubMask *sub_mask = keyframe->masks.get(i);
1655                         strncpy(text, sub_mask->name, sizeof(text));
1656                 }
1657                 else
1658                         sprintf(text, "%d", i);
1659                 mask_items.append(new CWindowMaskItem(text));
1660         }
1661         update_list(&mask_items);
1662 }
1663
1664
1665 CWindowMaskButton::CWindowMaskButton(MWindow *mwindow, CWindowMaskGUI *gui,
1666                  int x, int y, int no, int v)
1667  : BC_CheckBox(x, y, v)
1668 {
1669         this->mwindow = mwindow;
1670         this->gui = gui;
1671         this->no = no;
1672 }
1673
1674 CWindowMaskButton::~CWindowMaskButton()
1675 {
1676 }
1677
1678 int CWindowMaskButton::handle_event()
1679 {
1680         mwindow->edl->session->cwindow_mask = no;
1681         gui->mask_name->update(gui->mask_name->mask_items[no]->get_text());
1682         gui->update();
1683         gui->update_preview();
1684         return 1;
1685 }
1686
1687 CWindowMaskThumbler::CWindowMaskThumbler(MWindow *mwindow, CWindowMaskGUI *gui,
1688                 int x, int y)
1689  : BC_Tumbler(x, y)
1690 {
1691         this->mwindow = mwindow;
1692         this->gui = gui;
1693 }
1694
1695 CWindowMaskThumbler::~CWindowMaskThumbler()
1696 {
1697 }
1698
1699 int CWindowMaskThumbler::handle_up_event()
1700 {
1701         return do_event(1);
1702 }
1703
1704 int CWindowMaskThumbler::handle_down_event()
1705 {
1706         return do_event(-1);
1707 }
1708
1709 int CWindowMaskThumbler::do_event(int dir)
1710 {
1711         int k = mwindow->edl->session->cwindow_mask;
1712         if( (k+=dir) >= SUBMASKS ) k = 0;
1713         else if( k < 0 ) k = SUBMASKS-1;
1714         mwindow->edl->session->cwindow_mask = k;
1715         gui->mask_name->update(gui->mask_name->mask_items[k]->get_text());
1716         gui->update();
1717         gui->update_preview();
1718         return 1;
1719 }
1720
1721 CWindowMaskEnable::CWindowMaskEnable(MWindow *mwindow, CWindowMaskGUI *gui,
1722                  int x, int y, int no, int v)
1723  : BC_CheckBox(x, y, v)
1724 {
1725         this->mwindow = mwindow;
1726         this->gui = gui;
1727         this->no = no;
1728 }
1729
1730 CWindowMaskEnable::~CWindowMaskEnable()
1731 {
1732 }
1733
1734 int CWindowMaskEnable::handle_event()
1735 {
1736         Track *track = mwindow->cwindow->calculate_mask_track();
1737         if( track ) {
1738                 mwindow->undo->update_undo_before(_("mask enable"), this);
1739                 int bit = 1 << no;
1740                 if( get_value() )
1741                         track->masks |= bit;
1742                 else
1743                         track->masks &= ~bit;
1744                 gui->update();
1745                 gui->update_preview(1);
1746                 mwindow->undo->update_undo_after(_("mask enable"), LOAD_PATCHES);
1747         }
1748         return 1;
1749 }
1750
1751 CWindowMaskUnclear::CWindowMaskUnclear(MWindow *mwindow,
1752         CWindowMaskGUI *gui, int x, int y, int w)
1753  : BC_GenericButton(x, y, w, _("Enable"))
1754 {
1755         this->mwindow = mwindow;
1756         this->gui = gui;
1757         set_tooltip(_("Show/Hide mask"));
1758 }
1759
1760 int CWindowMaskUnclear::handle_event()
1761 {
1762         Track *track = mwindow->cwindow->calculate_mask_track();
1763         if( track ) {
1764                 mwindow->undo->update_undo_before(_("mask enables"), this);
1765                 int m = (1<<SUBMASKS)-1;
1766                 if( track->masks == m )
1767                         track->masks = 0;
1768                 else
1769                         track->masks = m;
1770                 for( int i=0; i<SUBMASKS; ++i )
1771                         gui->mask_enables[i]->update((track->masks>>i) & 1);
1772                 gui->update_preview(1);
1773                 mwindow->undo->update_undo_after(_("mask enables"), LOAD_PATCHES);
1774         }
1775         return 1;
1776 }
1777
1778 CWindowMaskSoloTrack::CWindowMaskSoloTrack(MWindow *mwindow,
1779         CWindowMaskGUI *gui, int x, int y, int v)
1780  : BC_CheckBox(x, y, v, _("Solo"))
1781 {
1782         this->mwindow = mwindow;
1783         this->gui = gui;
1784         set_tooltip(_("Solo video track"));
1785 }
1786
1787 int CWindowMaskSoloTrack::handle_event()
1788 {
1789         mwindow->edl->local_session->solo_track_id =
1790                 get_value() ? mwindow->cwindow->mask_track_id : -1;
1791         gui->update_preview(1);
1792         return 1;
1793 }
1794
1795 int CWindowMaskSoloTrack::calculate_w(BC_WindowBase *gui)
1796 {
1797         int w = 0, h = 0;
1798         calculate_extents(gui, &w, &h, _("Solo"));
1799         return w;
1800 }
1801
1802 CWindowMaskDelMask::CWindowMaskDelMask(MWindow *mwindow,
1803         CWindowMaskGUI *gui, int x, int y)
1804  : BC_GenericButton(x, y, _("Delete"))
1805 {
1806         this->mwindow = mwindow;
1807         this->gui = gui;
1808         set_tooltip(_("Delete mask"));
1809 }
1810
1811 int CWindowMaskDelMask::handle_event()
1812 {
1813         MaskAutos *autos;
1814         MaskAuto *keyframe;
1815         Track *track;
1816         MaskPoint *point;
1817         SubMask *mask;
1818         int total_points;
1819
1820 // Get existing keyframe
1821         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
1822
1823         if( track ) {
1824                 mwindow->undo->update_undo_before(_("mask delete"), 0);
1825
1826 #ifdef USE_KEYFRAME_SPANNING
1827 // Create temp keyframe
1828                 MaskAuto temp_keyframe(mwindow->edl, autos);
1829                 temp_keyframe.copy_data(keyframe);
1830                 SubMask *submask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
1831                 submask->points.remove_all_objects();
1832                 total_points = 0;
1833 // Commit change to span of keyframes
1834                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->update_parameter(&temp_keyframe);
1835 #else
1836                 for(MaskAuto *current = (MaskAuto*)autos->default_auto; current; ) {
1837                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1838                         submask->points.remove_all_objects();
1839                         current = current == (MaskAuto*)autos->default_auto ?
1840                                 (MaskAuto*)autos->first : (MaskAuto*)NEXT;
1841                 }
1842                 total_points = 0;
1843 #endif
1844                 if( mwindow->cwindow->gui->affected_point >= total_points )
1845                         mwindow->cwindow->gui->affected_point =
1846                                 total_points > 0 ? total_points-1 : 0;
1847
1848                 gui->update();
1849                 gui->update_preview();
1850                 mwindow->undo->update_undo_after(_("mask delete"), LOAD_AUTOMATION);
1851         }
1852
1853         return 1;
1854 }
1855
1856 CWindowMaskDelPoint::CWindowMaskDelPoint(MWindow *mwindow,
1857         CWindowMaskGUI *gui, int x, int y)
1858  : BC_GenericButton(x, y, _("Delete"))
1859 {
1860         this->mwindow = mwindow;
1861         this->gui = gui;
1862         set_tooltip(_("Delete point"));
1863 }
1864
1865 int CWindowMaskDelPoint::handle_event()
1866 {
1867         MaskAutos *autos;
1868         MaskAuto *keyframe;
1869         Track *track;
1870         MaskPoint *point;
1871         SubMask *mask;
1872         int total_points;
1873
1874 // Get existing keyframe
1875         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
1876         if( track ) {
1877                 mwindow->undo->update_undo_before(_("point delete"), 0);
1878
1879 #ifdef USE_KEYFRAME_SPANNING
1880 // Create temp keyframe
1881                 MaskAuto temp_keyframe(mwindow->edl, autos);
1882                 temp_keyframe.copy_data(keyframe);
1883 // Update parameter
1884                 SubMask *submask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
1885                 int i = mwindow->cwindow->gui->affected_point;
1886                 for( ; i<submask->points.total-1; ++i )
1887                         *submask->points.values[i] = *submask->points.values[i+1];
1888                 if( submask->points.total > 0 ) {
1889                         point = submask->points.values[submask->points.total-1];
1890                         submask->points.remove_object(point);
1891                 }
1892                 total_points = submask->points.total;
1893
1894 // Commit change to span of keyframes
1895                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->update_parameter(&temp_keyframe);
1896 #else
1897                 total_points = 0;
1898                 MaskAuto *current = (MaskAuto*)autos->default_auto;
1899                 while( current ) {
1900                         SubMask *submask = current->get_submask(mwindow->edl->session->cwindow_mask);
1901                         int i = mwindow->cwindow->gui->affected_point;
1902                         for( ; i<submask->points.total-1; ++i )
1903                                 *submask->points.values[i] = *submask->points.values[i+1];
1904                         if( submask->points.total > 0 ) {
1905                                 point = submask->points.values[submask->points.total-1];
1906                                 submask->points.remove_object(point);
1907                         }
1908                         total_points = submask->points.total;
1909                         current = current == (MaskAuto*)autos->default_auto ?
1910                                 (MaskAuto*)autos->first : (MaskAuto*)NEXT;
1911                 }
1912 #endif
1913                 if( mwindow->cwindow->gui->affected_point >= total_points )
1914                         mwindow->cwindow->gui->affected_point =
1915                                 total_points > 0 ? total_points-1 : 0;
1916
1917                 gui->update();
1918                 gui->update_preview();
1919                 mwindow->undo->update_undo_after(_("point delete"), LOAD_AUTOMATION);
1920         }
1921
1922         return 1;
1923 }
1924
1925 int CWindowMaskDelPoint::keypress_event()
1926 {
1927         if( get_keypress() == BACKSPACE ||
1928             get_keypress() == DELETE )
1929                 return handle_event();
1930         return 0;
1931 }
1932
1933
1934
1935
1936 CWindowMaskAffectedPoint::CWindowMaskAffectedPoint(MWindow *mwindow,
1937         CWindowMaskGUI *gui, int x, int y)
1938  : BC_TumbleTextBox(gui,
1939                 (int64_t)mwindow->cwindow->gui->affected_point,
1940                 (int64_t)0, INT64_MAX, x, y, 100)
1941 {
1942         this->mwindow = mwindow;
1943         this->gui = gui;
1944 }
1945
1946 CWindowMaskAffectedPoint::~CWindowMaskAffectedPoint()
1947 {
1948 }
1949
1950 int CWindowMaskAffectedPoint::handle_event()
1951 {
1952         int total_points = 0;
1953         int affected_point = atol(get_text());
1954         Track *track = mwindow->cwindow->calculate_mask_track();
1955         if(track) {
1956                 MaskAutos *autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
1957                 MaskAuto *keyframe = (MaskAuto*)mwindow->cwindow->calculate_affected_auto(autos, 0);
1958                 if( keyframe ) {
1959                         SubMask *mask = keyframe->get_submask(mwindow->edl->session->cwindow_mask);
1960                         total_points = mask->points.size();
1961                 }
1962         }
1963         int active_point = affected_point;
1964         if( affected_point >= total_points )
1965                 affected_point = total_points - 1;
1966         if( affected_point < 0 )
1967                 affected_point = 0;
1968         if( active_point != affected_point )
1969                 update((int64_t)affected_point);
1970         mwindow->cwindow->gui->affected_point = affected_point;
1971         gui->update();
1972         gui->update_preview();
1973         return 1;
1974 }
1975
1976
1977 CWindowMaskFocus::CWindowMaskFocus(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
1978  : BC_CheckBox(x, y, gui->focused, _("Focus"))
1979 {
1980         this->mwindow = mwindow;
1981         this->gui = gui;
1982         set_tooltip(_("Center for rotate/scale"));
1983 }
1984
1985 CWindowMaskFocus::~CWindowMaskFocus()
1986 {
1987 }
1988
1989 int CWindowMaskFocus::handle_event()
1990 {
1991         gui->focused = get_value();
1992         gui->update();
1993         gui->update_preview();
1994         return 1;
1995 }
1996
1997 CWindowMaskHelp::CWindowMaskHelp(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
1998  : BC_CheckBox(x, y, 0, _("Help"))
1999 {
2000         this->mwindow = mwindow;
2001         this->gui = gui;
2002         set_tooltip(_("Show help text"));
2003 }
2004
2005 CWindowMaskHelp::~CWindowMaskHelp()
2006 {
2007 }
2008
2009 int CWindowMaskHelp::handle_event()
2010 {
2011         gui->helped = get_value();
2012         gui->resize_window(gui->get_w(),
2013                 gui->helped ? gui->help_h : gui->help_y);
2014         gui->update();
2015         return 1;
2016 }
2017
2018 CWindowMaskDrawMarkers::CWindowMaskDrawMarkers(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2019  : BC_CheckBox(x, y, gui->markers, _("Markers"))
2020 {
2021         this->mwindow = mwindow;
2022         this->gui = gui;
2023         set_tooltip("Display points");
2024 }
2025
2026 CWindowMaskDrawMarkers::~CWindowMaskDrawMarkers()
2027 {
2028 }
2029
2030 int CWindowMaskDrawMarkers::handle_event()
2031 {
2032         gui->markers = get_value();
2033         gui->update();
2034         gui->update_preview();
2035         return 1;
2036 }
2037
2038 CWindowMaskDrawBoundary::CWindowMaskDrawBoundary(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2039  : BC_CheckBox(x, y, gui->boundary, _("Boundary"))
2040 {
2041         this->mwindow = mwindow;
2042         this->gui = gui;
2043         set_tooltip("Display mask outline");
2044 }
2045
2046 CWindowMaskDrawBoundary::~CWindowMaskDrawBoundary()
2047 {
2048 }
2049
2050 int CWindowMaskDrawBoundary::handle_event()
2051 {
2052         gui->boundary = get_value();
2053         gui->update();
2054         gui->update_preview();
2055         return 1;
2056 }
2057
2058
2059 CWindowMaskFeather::CWindowMaskFeather(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2060  : BC_TumbleTextBox(gui, 0, -FEATHER_MAX, FEATHER_MAX, x, y, 64, 2)
2061 {
2062         this->mwindow = mwindow;
2063         this->gui = gui;
2064 }
2065 CWindowMaskFeather::~CWindowMaskFeather()
2066 {
2067 }
2068
2069 int CWindowMaskFeather::update(float v)
2070 {
2071         gui->feather_slider->update(v);
2072         return BC_TumbleTextBox::update(v);
2073 }
2074
2075 int CWindowMaskFeather::update_value(float v)
2076 {
2077         MaskAutos *autos;
2078         MaskAuto *keyframe;
2079         Track *track;
2080         MaskPoint *point;
2081         SubMask *mask;
2082 #ifdef USE_KEYFRAME_SPANNING
2083         int create_it = 0;
2084 #else
2085         int create_it = 1;
2086 #endif
2087
2088         mwindow->undo->update_undo_before(_("mask feather"), this);
2089
2090 // Get existing keyframe
2091         gui->get_keyframe(track, autos, keyframe,
2092                         mask, point, create_it);
2093         if( track ) {
2094                 int gang = gui->gang_feather->get_value();
2095 #ifdef USE_KEYFRAME_SPANNING
2096                 MaskAuto temp_keyframe(mwindow->edl, autos);
2097                 temp_keyframe.copy_data(keyframe);
2098                 keyframe = &temp_keyframe;
2099 #endif
2100                 float change = v - mask->feather;
2101                 int k = mwindow->edl->session->cwindow_mask;
2102                 int n = gang ? keyframe->masks.size() : k+1;
2103                 for( int i=gang? 0 : k; i<n; ++i ) {
2104                         if( !gui->mask_enables[i]->get_value() ) continue;
2105                         SubMask *sub_mask = keyframe->get_submask(i);
2106                         float feather = sub_mask->feather + change;
2107                         bclamp(feather, -FEATHER_MAX, FEATHER_MAX);
2108                         sub_mask->feather = feather;
2109                 }
2110 #ifdef USE_KEYFRAME_SPANNING
2111                 autos->update_parameter(keyframe);
2112 #endif
2113                 gui->update_preview();
2114         }
2115
2116         mwindow->undo->update_undo_after(_("mask feather"), LOAD_AUTOMATION);
2117         return 1;
2118 }
2119
2120 int CWindowMaskFeather::handle_event()
2121 {
2122         float v = atof(get_text());
2123         gui->feather_slider->update(v);
2124         return gui->feather->update_value(v);
2125 }
2126
2127 CWindowMaskFeatherSlider::CWindowMaskFeatherSlider(MWindow *mwindow,
2128                 CWindowMaskGUI *gui, int x, int y, int w, float v)
2129  : BC_FSlider(x, y, 0, w, w, -FEATHER_MAX, FEATHER_MAX, v)
2130 {
2131         this->mwindow = mwindow;
2132         this->gui = gui;
2133         set_precision(0.01);
2134         timer = new Timer();
2135         stick = 0;
2136         last_v = 0;
2137 }
2138
2139 CWindowMaskFeatherSlider::~CWindowMaskFeatherSlider()
2140 {
2141         delete timer;
2142 }
2143
2144 int CWindowMaskFeatherSlider::handle_event()
2145 {
2146         float v = get_value();
2147         if( stick > 0 ) {
2148                 int64_t ms = timer->get_difference();
2149                 if( ms < 250 && --stick > 0 ) {
2150                         if( get_value() == 0 ) return 1;
2151                         update(v = 0);
2152                 }
2153                 else {
2154                         stick = 0;
2155                         last_v = v;
2156                 }
2157         }
2158         else if( (last_v>=0 && v<0) || (last_v<0 && v>=0) ) {
2159                 stick = 16;
2160                 v = 0;
2161         }
2162         else
2163                 last_v = v;
2164         timer->update();
2165         gui->feather->BC_TumbleTextBox::update(v);
2166         return gui->feather->update_value(v);
2167 }
2168
2169 int CWindowMaskFeatherSlider::update(float v)
2170 {
2171         return BC_FSlider::update(v);
2172 }
2173
2174 CWindowMaskFade::CWindowMaskFade(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2175  : BC_TumbleTextBox(gui, 0, -100.f, 100.f, x, y, 64, 2)
2176 {
2177         this->mwindow = mwindow;
2178         this->gui = gui;
2179 }
2180 CWindowMaskFade::~CWindowMaskFade()
2181 {
2182 }
2183
2184 int CWindowMaskFade::update(float v)
2185 {
2186         gui->fade_slider->update(v);
2187         return BC_TumbleTextBox::update(v);
2188 }
2189
2190 int CWindowMaskFade::update_value(float v)
2191 {
2192         MaskAutos *autos;
2193         MaskAuto *keyframe;
2194         Track *track;
2195         MaskPoint *point;
2196         SubMask *mask;
2197 #ifdef USE_KEYFRAME_SPANNING
2198         int create_it = 0;
2199 #else
2200         int create_it = 1;
2201 #endif
2202
2203         mwindow->undo->update_undo_before(_("mask fade"), this);
2204
2205 // Get existing keyframe
2206         gui->get_keyframe(track, autos, keyframe,
2207                         mask, point, create_it);
2208         if( track ) {
2209                 int gang = gui->gang_fader->get_value();
2210 #ifdef USE_KEYFRAME_SPANNING
2211                 MaskAuto temp_keyframe(mwindow->edl, autos);
2212                 temp_keyframe.copy_data(keyframe);
2213                 keyframe = &temp_keyframe;
2214 #endif
2215                 float change = v - mask->fader;
2216                 int k = mwindow->edl->session->cwindow_mask;
2217                 int n = gang ? keyframe->masks.size() : k+1;
2218                 for( int i=gang? 0 : k; i<n; ++i ) {
2219                         if( !gui->mask_enables[i]->get_value() ) continue;
2220                         SubMask *sub_mask = keyframe->get_submask(i);
2221                         float fader = sub_mask->fader + change;
2222                         bclamp(fader, -100.f, 100.f);
2223                         sub_mask->fader = fader;
2224                 }
2225 #ifdef USE_KEYFRAME_SPANNING
2226                 autos->update_parameter(keyframe);
2227 #endif
2228                 gui->update_preview();
2229         }
2230
2231         mwindow->undo->update_undo_after(_("mask fade"), LOAD_AUTOMATION);
2232         return 1;
2233 }
2234
2235 int CWindowMaskFade::handle_event()
2236 {
2237         float v = atof(get_text());
2238         gui->fade_slider->update(v);
2239         return gui->fade->update_value(v);
2240 }
2241
2242 CWindowMaskFadeSlider::CWindowMaskFadeSlider(MWindow *mwindow, CWindowMaskGUI *gui,
2243                 int x, int y, int w)
2244  : BC_ISlider(x, y, 0, w, w, -200, 200, 0)
2245 {
2246         this->mwindow = mwindow;
2247         this->gui = gui;
2248         timer = new Timer();
2249         stick = 0;
2250         last_v = 0;
2251 }
2252
2253 CWindowMaskFadeSlider::~CWindowMaskFadeSlider()
2254 {
2255         delete timer;
2256 }
2257
2258 int CWindowMaskFadeSlider::handle_event()
2259 {
2260         float v = 100*get_value()/200;
2261         if( stick > 0 ) {
2262                 int64_t ms = timer->get_difference();
2263                 if( ms < 250 && --stick > 0 ) {
2264                         if( get_value() == 0 ) return 1;
2265                         update(v = 0);
2266                 }
2267                 else {
2268                         stick = 0;
2269                         last_v = v;
2270                 }
2271         }
2272         else if( (last_v>=0 && v<0) || (last_v<0 && v>=0) ) {
2273                 stick = 16;
2274                 v = 0;
2275         }
2276         else
2277                 last_v = v;
2278         timer->update();
2279         gui->fade->BC_TumbleTextBox::update(v);
2280         return gui->fade->update_value(v);
2281 }
2282
2283 int CWindowMaskFadeSlider::update(int64_t v)
2284 {
2285         return BC_ISlider::update(200*v/100);
2286 }
2287
2288 CWindowMaskGangFader::CWindowMaskGangFader(MWindow *mwindow,
2289                 CWindowMaskGUI *gui, int x, int y)
2290  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2291 {
2292         this->mwindow = mwindow;
2293         this->gui = gui;
2294         set_tooltip(_("Gang fader"));
2295 }
2296
2297 CWindowMaskGangFader::~CWindowMaskGangFader()
2298 {
2299 }
2300
2301 int CWindowMaskGangFader::handle_event()
2302 {
2303         return 1;
2304 }
2305
2306 CWindowMaskGangFocus::CWindowMaskGangFocus(MWindow *mwindow,
2307                 CWindowMaskGUI *gui, int x, int y)
2308  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2309 {
2310         this->mwindow = mwindow;
2311         this->gui = gui;
2312         set_tooltip(_("Gang rotate/scale/translate"));
2313 }
2314
2315 CWindowMaskGangFocus::~CWindowMaskGangFocus()
2316 {
2317 }
2318
2319 int CWindowMaskGangFocus::handle_event()
2320 {
2321         return 1;
2322 }
2323
2324
2325 CWindowMaskSmoothButton::CWindowMaskSmoothButton(MWindow *mwindow, CWindowMaskGUI *gui,
2326                 const char *tip, int type, int on, int x, int y, const char *images)
2327  : BC_Button(x, y, mwindow->theme->get_image_set(images))
2328 {
2329         this->mwindow = mwindow;
2330         this->gui = gui;
2331         this->type = type;
2332         this->on = on;
2333         set_tooltip(tip);
2334 }
2335
2336 int CWindowMaskSmoothButton::handle_event()
2337 {
2338         return gui->smooth_mask(type, on);
2339 }
2340
2341 CWindowMaskBeforePlugins::CWindowMaskBeforePlugins(CWindowMaskGUI *gui, int x, int y)
2342  : BC_CheckBox(x, y, 1, _("Apply mask before plugins"))
2343 {
2344         this->gui = gui;
2345 }
2346
2347 int CWindowMaskBeforePlugins::handle_event()
2348 {
2349         Track *track;
2350         MaskAutos *autos;
2351         MaskAuto *keyframe;
2352         SubMask *mask;
2353         MaskPoint *point;
2354         gui->get_keyframe(track, autos, keyframe, mask, point, 1);
2355
2356         if (keyframe) {
2357                 int v = get_value();
2358 #ifdef USE_KEYFRAME_SPANNING
2359                 MaskAuto temp_keyframe(gui->mwindow->edl, autos);
2360                 temp_keyframe.copy_data(keyframe);
2361                 temp_keyframe.apply_before_plugins = v;
2362                 autos->update_parameter(&temp_keyframe);
2363 #else
2364                 keyframe->apply_before_plugins = v;
2365 #endif
2366                 gui->update_preview();
2367         }
2368         return 1;
2369 }
2370
2371
2372 CWindowDisableOpenGLMasking::CWindowDisableOpenGLMasking(CWindowMaskGUI *gui, int x, int y)
2373  : BC_CheckBox(x, y, 1, _("Disable OpenGL masking"))
2374 {
2375         this->gui = gui;
2376 }
2377
2378 int CWindowDisableOpenGLMasking::handle_event()
2379 {
2380         Track *track;
2381         MaskAutos *autos;
2382         MaskAuto *keyframe;
2383         SubMask *mask;
2384         MaskPoint *point;
2385         gui->get_keyframe(track, autos, keyframe, mask, point, 1);
2386
2387         if( keyframe ) {
2388                 int v = get_value();
2389 #ifdef USE_KEYFRAME_SPANNING
2390                 MaskAuto temp_keyframe(gui->mwindow->edl, autos);
2391                 temp_keyframe.copy_data(keyframe);
2392                 temp_keyframe.disable_opengl_masking = v;
2393                 autos->update_parameter(&temp_keyframe);
2394 #else
2395                 keyframe->disable_opengl_masking = v;
2396 #endif
2397                 gui->update_preview();
2398         }
2399         return 1;
2400 }
2401
2402
2403 CWindowMaskClrMask::CWindowMaskClrMask(MWindow *mwindow,
2404                 CWindowMaskGUI *gui, int x, int y)
2405  : BC_Button(x, y, mwindow->theme->get_image_set("reset_button"))
2406 {
2407         this->mwindow = mwindow;
2408         this->gui = gui;
2409         set_tooltip(_("Delete all masks"));
2410 }
2411
2412 CWindowMaskClrMask::~CWindowMaskClrMask()
2413 {
2414 }
2415
2416 int CWindowMaskClrMask::calculate_w(MWindow *mwindow)
2417 {
2418         VFrame *vfrm = *mwindow->theme->get_image_set("reset_button");
2419         return vfrm->get_w();
2420 }
2421
2422 int CWindowMaskClrMask::handle_event()
2423 {
2424         MaskAutos *autos;
2425         MaskAuto *keyframe;
2426         Track *track;
2427         MaskPoint *point;
2428         SubMask *mask;
2429
2430 // Get existing keyframe
2431         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
2432
2433         if( track ) {
2434                 mwindow->undo->update_undo_before(_("del masks"), 0);
2435                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->clear_all();
2436                 mwindow->undo->update_undo_after(_("del masks"), LOAD_AUTOMATION);
2437         }
2438
2439         gui->update();
2440         gui->update_preview(1);
2441         return 1;
2442 }
2443
2444 CWindowMaskGangFeather::CWindowMaskGangFeather(MWindow *mwindow,
2445                 CWindowMaskGUI *gui, int x, int y)
2446  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2447 {
2448         this->mwindow = mwindow;
2449         this->gui = gui;
2450         set_tooltip(_("Gang feather"));
2451 }
2452
2453 CWindowMaskGangFeather::~CWindowMaskGangFeather()
2454 {
2455 }
2456
2457 int CWindowMaskGangFeather::handle_event()
2458 {
2459         return 1;
2460 }
2461
2462 CWindowMaskGUI::CWindowMaskGUI(MWindow *mwindow, CWindowTool *thread)
2463  : CWindowToolGUI(mwindow, thread,
2464         _(PROGRAM_NAME ": Mask"), 430, 700)
2465 {
2466         this->mwindow = mwindow;
2467         this->thread = thread;
2468         active_point = 0;
2469         fade = 0;
2470         feather = 0;
2471         focused = 0;
2472         markers = 1;
2473         boundary = 1;
2474         preset_dialog = 0;
2475 }
2476 CWindowMaskGUI::~CWindowMaskGUI()
2477 {
2478         lock_window("CWindowMaskGUI::~CWindowMaskGUI");
2479         done_event();
2480         delete active_point;
2481         delete fade;
2482         delete feather;
2483         unlock_window();
2484         delete preset_dialog;
2485 }
2486
2487 void CWindowMaskGUI::create_objects()
2488 {
2489         int x = 10, y = 10, margin = mwindow->theme->widget_border;
2490         int clr_w = CWindowMaskClrMask::calculate_w(mwindow);
2491         int clr_x = get_w()-x - clr_w;
2492         int del_w = CWindowMaskDelMask::calculate_w(this,_("Delete"));
2493         int del_x = clr_x-2*margin - del_w;
2494
2495         lock_window("CWindowMaskGUI::create_objects");
2496         BC_TitleBar *title_bar;
2497         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks on Track")));
2498         y += title_bar->get_h() + margin;
2499         BC_Title *title;
2500         add_subwindow(title = new BC_Title(x,y, _("Track:")));
2501         int x1 = x + 90;
2502         Track *track = mwindow->cwindow->calculate_affected_track();
2503         const char *text = track ? track->title : "";
2504         mwindow->cwindow->mask_track_id = track ? track->get_id() : -1;
2505         mask_on_track = new CWindowMaskOnTrack(mwindow, this, x1, y, 100, text);
2506         mask_on_track->create_objects();
2507         mask_on_track->set_tooltip(_("Video track"));
2508         int x2 = x1 + mask_on_track->get_w();
2509         add_subwindow(mask_track_tumbler = new CWindowMaskTrackTumbler(mwindow, this, x2, y));
2510         mwindow->edl->local_session->solo_track_id = -1;
2511         x2 = del_x + (del_w - CWindowMaskSoloTrack::calculate_w(this)) / 2;
2512         add_subwindow(mask_solo_track = new CWindowMaskSoloTrack(mwindow, this, x2, y, 0));
2513         y += mask_on_track->get_h() + margin;
2514         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks")));
2515         y += title_bar->get_h() + margin;
2516         add_subwindow(title = new BC_Title(x, y, _("Mask:")));
2517         mask_name = new CWindowMaskName(mwindow, this, x1, y, "");
2518         mask_name->create_objects();
2519         mask_name->set_tooltip(_("Mask name"));
2520         add_subwindow(mask_clr = new CWindowMaskClrMask(mwindow, this, clr_x, y));
2521         add_subwindow(mask_del = new CWindowMaskDelMask(mwindow, this, del_x, y));
2522         y += mask_name->get_h() + 2*margin;
2523         add_subwindow(title = new BC_Title(x, y, _("Presets:")));
2524         add_subwindow(mask_shape = new CWindowMaskShape(mwindow, this));
2525         mask_shape->create_objects();
2526         add_subwindow(mask_load = new CWindowMaskLoad(mwindow, this, x2=x1, y, 80));
2527         x2 += mask_load->get_w() + 2*margin;
2528         add_subwindow(mask_save = new CWindowMaskSave(mwindow, this, x2, y, 80));
2529         x2 += mask_save->get_w() + 2*margin;
2530         add_subwindow(mask_delete = new CWindowMaskDelete(mwindow, this, x2, y, 80));
2531         y += mask_load->get_h() + 2*margin;
2532         BC_Bar *bar;
2533         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2534         y += bar->get_h() + 2*margin;
2535
2536         add_subwindow(title = new BC_Title(x, y, _("Select:")));
2537         int bw = 0, bh = 0;
2538         BC_CheckBox::calculate_extents(this, &bw, &bh);
2539         int bdx = bw + 2*margin;
2540         x2 = x1;
2541         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2542                 int v = i == mwindow->edl->session->cwindow_mask ? 1 : 0;
2543                 mask_buttons[i] = new CWindowMaskButton(mwindow, this, x2, y, i, v);
2544                 add_subwindow(mask_buttons[i]);
2545         }
2546         x2 += margin;
2547         add_subwindow(mask_thumbler = new CWindowMaskThumbler(mwindow, this, x2, y));
2548         y += bh + margin;
2549         x2 = x1;
2550         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2551                 char text[BCSTRLEN];  sprintf(text, "%d", i);
2552                 int tx = (bw - get_text_width(MEDIUMFONT, text)) / 2;
2553                 mask_blabels[i] = new BC_Title(x2+tx, y, text);
2554                 add_subwindow(mask_blabels[i]);
2555         }
2556         y += mask_blabels[0]->get_h() + margin;
2557         add_subwindow(mask_unclr = new CWindowMaskUnclear(mwindow, this, x, y, x1-x-2*margin));
2558         x2 = x1;
2559         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2560                 mask_enables[i] = new CWindowMaskEnable(mwindow, this, x2, y, i, 1);
2561                 add_subwindow(mask_enables[i]);
2562         }
2563         y += mask_enables[0]->get_h() + 2*margin;
2564         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Fade & Feather")));
2565         y += title_bar->get_h() + margin;
2566
2567         add_subwindow(title = new BC_Title(x, y, _("Fade:")));
2568         fade = new CWindowMaskFade(mwindow, this, x1, y);
2569         fade->create_objects();
2570         x2 = x1 + fade->get_w() + 2*margin;
2571         int w2 = clr_x-2*margin - x2;
2572         add_subwindow(fade_slider = new CWindowMaskFadeSlider(mwindow, this, x2, y, w2));
2573         add_subwindow(gang_fader = new CWindowMaskGangFader(mwindow, this, clr_x, y));
2574         y += fade->get_h() + margin;
2575         add_subwindow(title = new BC_Title(x, y, _("Feather:")));
2576         feather = new CWindowMaskFeather(mwindow, this, x1, y);
2577         feather->create_objects();
2578         w2 = clr_x - 2*margin - x2;
2579         feather_slider = new CWindowMaskFeatherSlider(mwindow, this, x2, y, w2, 0);
2580         add_subwindow(feather_slider);
2581         add_subwindow(gang_feather = new CWindowMaskGangFeather(mwindow, this, clr_x, y));
2582         y += feather->get_h() + 2*margin;
2583         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Mask Points")));
2584         y += title_bar->get_h() + margin;
2585
2586         x1 = x + 60;
2587         add_subwindow(title = new BC_Title(x, y, _("Point:")));
2588         active_point = new CWindowMaskAffectedPoint(mwindow, this, x1, y);
2589         active_point->create_objects();
2590         int x3  = x1 + active_point->get_w() + 4*margin;
2591 // typ=0, this mask, this point
2592         add_subwindow(mask_pnt_linear = new CWindowMaskSmoothButton(mwindow, this,
2593                 _("linear point"), 0, 0, x3, y, "mask_pnt_linear_images"));
2594         int x4  = x3 + mask_pnt_linear->get_w() + 2*margin;
2595         add_subwindow(mask_pnt_smooth = new CWindowMaskSmoothButton(mwindow, this,
2596                 _("smooth point"), 0, 1, x4, y, "mask_pnt_smooth_images"));
2597         add_subwindow(del_point = new CWindowMaskDelPoint(mwindow, this, del_x, y));
2598         y += active_point->get_h() + margin;
2599         add_subwindow(title = new BC_Title(x, y, "X:"));
2600         this->x = new CWindowCoord(this, x1, y, (float)0.0);
2601         this->x->create_objects();
2602 // typ>0, this mask, all points
2603         add_subwindow(mask_crv_linear = new CWindowMaskSmoothButton(mwindow, this,
2604                 _("linear curve"), 1, 0, x3, y, "mask_crv_linear_images"));
2605         add_subwindow(mask_crv_smooth = new CWindowMaskSmoothButton(mwindow, this,
2606                 _("smooth curve"), 1, 1, x4, y, "mask_crv_smooth_images"));
2607         add_subwindow(draw_markers = new CWindowMaskDrawMarkers(mwindow, this, del_x, y));
2608         y += this->x->get_h() + margin;
2609         add_subwindow(title = new BC_Title(x, y, "Y:"));
2610         this->y = new CWindowCoord(this, x1, y, (float)0.0);
2611         this->y->create_objects();
2612 // typ<0, all masks, all points
2613         add_subwindow(mask_all_linear = new CWindowMaskSmoothButton(mwindow, this,
2614                 _("linear all"), -1, 0, x3, y, "mask_all_linear_images"));
2615         add_subwindow(mask_all_smooth = new CWindowMaskSmoothButton(mwindow, this,
2616                 _("smooth all"), -1, 1, x4, y, "mask_all_smooth_images"));
2617         add_subwindow(draw_boundary = new CWindowMaskDrawBoundary(mwindow, this, del_x, y));
2618         y += this->y->get_h() + 2*margin;
2619         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Pivot Point")));
2620         y += title_bar->get_h() + margin;
2621
2622         add_subwindow(title = new BC_Title(x, y, "X:"));
2623         float cx = mwindow->edl->session->output_w / 2.f;
2624         focus_x = new CWindowCoord(this, x1, y, cx);
2625         focus_x->create_objects();
2626         add_subwindow(focus = new CWindowMaskFocus(mwindow, this, del_x, y));
2627         add_subwindow(gang_focus = new CWindowMaskGangFocus(mwindow, this, clr_x, y));
2628         y += focus_x->get_h() + margin;
2629         add_subwindow(title = new BC_Title(x, y, "Y:"));
2630         float cy = mwindow->edl->session->output_h / 2.f;
2631         focus_y = new CWindowCoord(this, x1, y, cy);
2632         focus_y->create_objects();
2633         add_subwindow(mask_center = new CWindowMaskCenter(mwindow, this, x2=x4, y, 80));
2634         x2 += mask_center->get_w() + 2*margin;
2635         add_subwindow(mask_normal = new CWindowMaskNormal(mwindow, this, x2, y, 80));
2636         y += focus_x->get_h() + 2*margin;
2637         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2638         y += bar->get_h() + margin;
2639         add_subwindow(this->apply_before_plugins = new CWindowMaskBeforePlugins(this, 10, y));
2640         y += this->apply_before_plugins->get_h();
2641         add_subwindow(this->disable_opengl_masking = new CWindowDisableOpenGLMasking(this, 10, y));
2642         add_subwindow(help = new CWindowMaskHelp(mwindow, this, del_x, y));
2643         y += this->disable_opengl_masking->get_h() + 2*margin;
2644         help_y = y;
2645         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2646         y += bar->get_h() + 2*margin;
2647         add_subwindow(title = new BC_Title(x, y, _(
2648                 "Shift+LMB: move an end point\n"
2649                 "Ctrl+LMB: move a control point\n"
2650                 "Alt+LMB: to drag translate the mask\n"
2651                 "Shift+Key Delete: to delete the point\n"
2652                 "Shift+MMB: Set Pivot Point at pointer\n"
2653                 "Wheel: rotate around Pivot Point\n"
2654                 "Shift+Wheel: scale around Pivot Point\n"
2655                 "Ctrl+Wheel: rotate/scale around pointer")));
2656         help_h = y + title->get_h() + 2*margin;
2657         update();
2658         resize_window(get_w(), help_y);
2659         unlock_window();
2660 }
2661
2662 int CWindowMaskGUI::close_event()
2663 {
2664         done_event();
2665         return CWindowToolGUI::close_event();
2666 }
2667
2668 void CWindowMaskGUI::done_event()
2669 {
2670         if( mwindow->in_destructor ) return;
2671         int &solo_track_id = mwindow->edl->local_session->solo_track_id;
2672         if( solo_track_id >= 0 ) {
2673                 solo_track_id = -1;
2674                 update_preview();
2675         }
2676 }
2677
2678 void CWindowMaskGUI::get_keyframe(Track* &track,
2679                 MaskAutos* &autos, MaskAuto* &keyframe,
2680                 SubMask* &mask, MaskPoint* &point, int create_it)
2681 {
2682         autos = 0;
2683         keyframe = 0;
2684
2685         track = mwindow->cwindow->calculate_mask_track();
2686         if( !track )
2687                 track = mwindow->cwindow->calculate_affected_track();
2688                 
2689         if(track) {
2690                 autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
2691                 keyframe = (MaskAuto*)mwindow->cwindow->calculate_affected_auto(
2692                         autos,
2693                         create_it);
2694         }
2695
2696         mask = !keyframe ? 0 :
2697                 keyframe->get_submask(mwindow->edl->session->cwindow_mask);
2698
2699         point = 0;
2700         if( keyframe ) {
2701                 if( mwindow->cwindow->gui->affected_point < mask->points.total &&
2702                         mwindow->cwindow->gui->affected_point >= 0 ) {
2703                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2704                 }
2705         }
2706 }
2707
2708 void CWindowMaskGUI::update()
2709 {
2710         Track *track;
2711         MaskAutos *autos;
2712         MaskAuto *keyframe;
2713         SubMask *mask;
2714         MaskPoint *point;
2715 //printf("CWindowMaskGUI::update 1\n");
2716         get_keyframe(track, autos, keyframe, mask, point, 0);
2717         mwindow->cwindow->mask_track_id = track ? track->get_id() : -1;
2718         mask_on_track->set_back_color(!track || track->record ?
2719                 get_resources()->text_background :
2720                 get_resources()->text_background_disarmed);
2721         mask_on_track->update_items();
2722         mask_on_track->update(!track ? "" : track->title);
2723         mask_name->update_items(keyframe);
2724         const char *text = "";
2725         int sz = !keyframe ? 0 : keyframe->masks.size();
2726         int k = mwindow->edl->session->cwindow_mask;
2727         if( k >= 0 && k < sz )
2728                 text = keyframe->masks[k]->name;
2729         else
2730                 k = mwindow->edl->session->cwindow_mask = 0;
2731         mask_name->update(text);
2732         update_buttons(keyframe, k);
2733         if( point ) {
2734                 x->update(point->x);
2735                 y->update(point->y);
2736         }
2737         if( track ) {
2738                 double position = mwindow->edl->local_session->get_selectionstart(1);
2739                 int64_t position_i = track->to_units(position, 0);
2740                 feather->update(autos->get_feather(position_i, k, PLAY_FORWARD));
2741                 fade->update(autos->get_fader(position_i, k, PLAY_FORWARD));
2742                 int show_mask = track->masks;
2743                 for( int i=0; i<SUBMASKS; ++i )
2744                         mask_enables[i]->update((show_mask>>i) & 1);
2745         }
2746         if( keyframe ) {
2747                 apply_before_plugins->update(keyframe->apply_before_plugins);
2748                 disable_opengl_masking->update(keyframe->disable_opengl_masking);
2749         }
2750         active_point->update((int64_t)mwindow->cwindow->gui->affected_point);
2751 }
2752
2753 void CWindowMaskGUI::handle_event()
2754 {
2755         Track *track;
2756         MaskAuto *keyframe;
2757         MaskAutos *autos;
2758         SubMask *mask;
2759         MaskPoint *point;
2760         get_keyframe(track, autos, keyframe, mask, point, 0);
2761
2762         mwindow->undo->update_undo_before(_("mask point"), this);
2763
2764         if(point)
2765         {
2766 #ifdef USE_KEYFRAME_SPANNING
2767 // Create temp keyframe
2768                 MaskAuto temp_keyframe(mwindow->edl, autos);
2769                 temp_keyframe.copy_data(keyframe);
2770 // Get affected point in temp keyframe
2771                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
2772                 if(mwindow->cwindow->gui->affected_point < mask->points.total &&
2773                         mwindow->cwindow->gui->affected_point >= 0)
2774                 {
2775                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2776                 }
2777
2778                 if(point)
2779                 {
2780                         point->x = atof(x->get_text());
2781                         point->y = atof(y->get_text());
2782 // Commit to spanned keyframes
2783                         autos->update_parameter(&temp_keyframe);
2784                 }
2785 #else
2786                 point->x = atof(x->get_text());
2787                 point->y = atof(y->get_text());
2788 #endif
2789         }
2790
2791         update_preview();
2792         mwindow->undo->update_undo_after(_("mask point"), LOAD_AUTOMATION);
2793 }
2794
2795 void CWindowMaskGUI::set_focused(int v, float cx, float cy)
2796 {
2797         CWindowGUI *cgui = mwindow->cwindow->gui;
2798         cgui->unlock_window();
2799         lock_window("CWindowMaskGUI::set_focused");
2800         if( focused != v )
2801                 focus->update(focused = v);
2802         focus_x->update(cx);
2803         focus_y->update(cy);
2804         unlock_window();
2805         cgui->lock_window("CWindowCanvas::set_focused");
2806 }
2807
2808 void CWindowMaskGUI::update_buttons(MaskAuto *keyframe, int k)
2809 {
2810         int text_color = get_resources()->default_text_color;
2811         int high_color = get_resources()->button_highlighted;
2812         for( int i=0; i<SUBMASKS; ++i ) {
2813                 int color = text_color;
2814                 if( keyframe ) {
2815                         SubMask *submask = keyframe->get_submask(i);
2816                         if( submask && submask->points.size() )
2817                                 color = high_color;
2818                 }
2819                 mask_blabels[i]->set_color(color);
2820                 mask_buttons[i]->update(i==k ? 1 : 0);
2821         }
2822 }
2823
2824 // typ=0, this mask, this point
2825 // typ>0, this mask, all points
2826 // typ<0, all masks, all points
2827 // dxy= on? pt[+1]-pt[-1] : dxy=0
2828 int CWindowMaskGUI::smooth_mask(int typ, int on)
2829 {
2830         MaskAutos *autos;
2831         MaskAuto *keyframe;
2832         Track *track;
2833         MaskPoint *point;
2834         SubMask *mask;
2835 #ifdef USE_KEYFRAME_SPANNING
2836         int create_it = 0;
2837 #else
2838         int create_it = 1;
2839 #endif
2840
2841         mwindow->undo->update_undo_before(_("mask smooth"), this);
2842
2843 // Get existing keyframe
2844         get_keyframe(track, autos, keyframe,
2845                         mask, point, create_it);
2846         if( track ) {
2847 #ifdef USE_KEYFRAME_SPANNING
2848                 MaskAuto temp_keyframe(mwindow->edl, autos);
2849                 temp_keyframe.copy_data(keyframe);
2850                 keyframe = &temp_keyframe;
2851 #endif
2852                 int k = mwindow->edl->session->cwindow_mask;
2853                 int n = typ>=0 ? k+1 : keyframe->masks.size();
2854                 for( int j=typ<0? 0 : k; j<n; ++j ) {
2855                         if( !mask_enables[j]->get_value() ) continue;
2856                         SubMask *sub_mask = keyframe->get_submask(j);
2857                         ArrayList<MaskPoint*> &points = sub_mask->points;
2858                         int psz = points.size();
2859                         if( psz < 3 ) continue;
2860                         int l = mwindow->cwindow->gui->affected_point;
2861                         if( l > psz ) l = psz;
2862                         int m = typ ? psz : l+1;
2863                         for( int i=typ ? 0 : l; i<m; ++i ) {
2864                                 int i0 = i-1, i1 = i+1;
2865                                 if( i0 < 0 ) i0 = psz-1;
2866                                 if( i1 >= psz ) i1 = 0;
2867                                 MaskPoint *p0 = points[i0];
2868                                 MaskPoint *p  = points[i];
2869                                 MaskPoint *p1 = points[i1];
2870                                 float dx = !on ? 0 : p1->x - p0->x;
2871                                 float dy = !on ? 0 : p1->y - p0->y;
2872                                 p->control_x1 = -dx/4;  p->control_y1 = -dy/4;
2873                                 p->control_x2 =  dx/4;  p->control_y2 =  dy/4;
2874                         }
2875                 }
2876 #ifdef USE_KEYFRAME_SPANNING
2877                 autos->update_parameter(keyframe);
2878 #endif
2879                 update_preview();
2880         }
2881
2882         mwindow->undo->update_undo_after(_("mask smooth"), LOAD_AUTOMATION);
2883         return 1;
2884 }
2885
2886 int CWindowMaskGUI::save_mask(const char *nm)
2887 {
2888         int k = mwindow->edl->session->cwindow_mask;
2889         MaskAutos *autos;
2890         MaskAuto *keyframe;
2891         Track *track;
2892         MaskPoint *point;
2893         SubMask *mask;
2894         get_keyframe(track, autos, keyframe, mask, point, 0);
2895         if( !track ) return 0;
2896         SubMask *sub_mask = keyframe->get_submask(k);
2897         ArrayList<SubMask *> masks;
2898         load_masks(masks);
2899         int i = masks.size();
2900         while( --i >= 0 ) {
2901                 if( strcmp(masks[i]->name, nm) ) continue;
2902                 masks.remove_object_number(i);
2903         }
2904         mask = new SubMask(0, -1);
2905         strncpy(mask->name, nm, sizeof(mask->name)-1);
2906         mask->copy_from(*sub_mask, 0);
2907         masks.append(mask);
2908         save_masks(masks);
2909         masks.remove_all_objects();
2910         return 1;
2911 }
2912
2913 int CWindowMaskGUI::del_mask(const char *nm)
2914 {
2915         ArrayList<SubMask *> masks;
2916         load_masks(masks);
2917         int i = masks.size();
2918         while( --i >= 0 ) {
2919                 if( strcmp(masks[i]->name, nm) ) continue;
2920                 masks.remove_object_number(i);
2921         }
2922         save_masks(masks);
2923         masks.remove_all_objects();
2924         return 1;
2925 }
2926
2927 int CWindowMaskGUI::center_mask()
2928 {
2929         int k = mwindow->edl->session->cwindow_mask;
2930         MaskAutos *autos;
2931         MaskAuto *keyframe;
2932         Track *track;
2933         MaskPoint *point;
2934         SubMask *mask;
2935 #ifdef USE_KEYFRAME_SPANNING
2936         int create_it = 0;
2937 #else
2938         int create_it = 1;
2939 #endif
2940         get_keyframe(track, autos, keyframe,
2941                         mask, point, create_it);
2942         if( !track ) return 0;
2943         mwindow->undo->update_undo_before(_("mask center"), this);
2944
2945 // Get existing keyframe
2946 #ifdef USE_KEYFRAME_SPANNING
2947         MaskAuto temp_keyframe(mwindow->edl, autos);
2948         temp_keyframe.copy_data(keyframe);
2949         keyframe = &temp_keyframe;
2950 #endif
2951         SubMask *sub_mask = keyframe->get_submask(k);
2952         ArrayList<MaskPoint*> &points = sub_mask->points;
2953         int psz = points.size();
2954         if( psz > 0 ) {
2955                 float cx = 0, cy = 0;
2956                 for( int i=0; i<psz; ++i ) {
2957                         MaskPoint *p  = points[i];
2958                         cx += p->x;  cy += p->y;
2959                 }
2960                 cx /= psz;  cy /= psz;
2961                 cx -= mwindow->edl->session->output_w / 2.f;
2962                 cy -= mwindow->edl->session->output_h / 2.f;
2963                 for( int i=0; i<psz; ++i ) {
2964                         MaskPoint *p  = points[i];
2965                         p->x -= cx;  p->y -= cy;
2966                 }
2967         }
2968 #ifdef USE_KEYFRAME_SPANNING
2969         autos->update_parameter(keyframe);
2970 #endif
2971         update_preview();
2972         mwindow->undo->update_undo_after(_("mask center"), LOAD_AUTOMATION);
2973         return 1;
2974 }
2975
2976 int CWindowMaskGUI::normal_mask()
2977 {
2978         int k = mwindow->edl->session->cwindow_mask;
2979         MaskAutos *autos;
2980         MaskAuto *keyframe;
2981         Track *track;
2982         MaskPoint *point;
2983         SubMask *mask;
2984 #ifdef USE_KEYFRAME_SPANNING
2985         int create_it = 0;
2986 #else
2987         int create_it = 1;
2988 #endif
2989 // Get existing keyframe
2990         get_keyframe(track, autos, keyframe,
2991                         mask, point, create_it);
2992         if( !track ) return 0;
2993         mwindow->undo->update_undo_before(_("mask normal"), this);
2994
2995 #ifdef USE_KEYFRAME_SPANNING
2996         MaskAuto temp_keyframe(mwindow->edl, autos);
2997         temp_keyframe.copy_data(keyframe);
2998         keyframe = &temp_keyframe;
2999 #endif
3000         SubMask *sub_mask = keyframe->get_submask(k);
3001         ArrayList<MaskPoint*> &points = sub_mask->points;
3002         int psz = points.size();
3003         float cx = 0, cy = 0;
3004         double dr = 0;
3005         if( psz > 0 ) {
3006                 for( int i=0; i<psz; ++i ) {
3007                         MaskPoint *p  = points[i];
3008                         cx += p->x;  cy += p->y;
3009                 }
3010                 cx /= psz;  cy /= psz;
3011                 for( int i=0; i<psz; ++i ) {
3012                         MaskPoint *p  = points[i];
3013                         float dx = fabsf(p->x-cx), dy = fabsf(p->y-cy);
3014                         double d = sqrt(dx*dx + dy*dy);
3015                         if( dr < d ) dr = d;
3016                 }
3017         }
3018         if( dr > 0 ) {
3019                 float out_w = mwindow->edl->session->output_w;
3020                 float out_h = mwindow->edl->session->output_h;
3021                 float r = bmax(out_w, out_h);
3022                 float s = r / (4 * dr * sqrt(2.));
3023                 for( int i=0; i<psz; ++i ) {
3024                         MaskPoint *p  = points[i];
3025                         float x = p->x, y = p->y;
3026                         p->x = (x-cx) * s + cx;
3027                         p->y = (y-cy) * s + cy;
3028                         p->control_x1 *= s;  p->control_y1 *= s;
3029                         p->control_x2 *= s;  p->control_y2 *= s;
3030                 }
3031         }
3032 #ifdef USE_KEYFRAME_SPANNING
3033         autos->update_parameter(keyframe);
3034 #endif
3035         update_preview();
3036
3037         mwindow->undo->update_undo_after(_("mask normal"), LOAD_AUTOMATION);
3038         return 1;
3039 }
3040
3041
3042 CWindowMaskLoad::CWindowMaskLoad(MWindow *mwindow,
3043         CWindowMaskGUI *gui, int x, int y, int w)
3044  : BC_GenericButton(x, y, w, _("Load"))
3045 {
3046         this->mwindow = mwindow;
3047         this->gui = gui;
3048         set_tooltip(_("Load preset"));
3049 }
3050
3051 int CWindowMaskLoad::handle_event()
3052 {
3053         gui->mask_shape->create_objects();
3054         int px, py;
3055         get_abs_cursor(px, py);
3056         return gui->mask_shape->activate(px, py, 120,160);
3057 }
3058
3059
3060 CWindowMaskSave::CWindowMaskSave(MWindow *mwindow,
3061         CWindowMaskGUI *gui, int x, int y, int w)
3062  : BC_GenericButton(x, y, w, _("Save"))
3063 {
3064         this->mwindow = mwindow;
3065         this->gui = gui;
3066         set_tooltip(_("Save preset"));
3067 }
3068
3069 CWindowMaskSave::~CWindowMaskSave()
3070 {
3071 }
3072
3073 int CWindowMaskSave::handle_event()
3074 {
3075         Track *track;
3076         MaskAutos *autos;
3077         MaskAuto *keyframe;
3078         SubMask *mask;
3079         MaskPoint *point;
3080         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
3081         if( track ) {
3082                 int sx = 0, sy = 0;
3083                 gui->get_abs_cursor(sx, sy);
3084                 if( !gui->preset_dialog )
3085                         gui->preset_dialog = new CWindowMaskPresetDialog(mwindow, gui);
3086                 gui->preset_dialog->start_dialog(sx, sy, keyframe);
3087         }
3088         return 1;
3089 }
3090
3091 CWindowMaskPresetDialog::CWindowMaskPresetDialog(MWindow *mwindow, CWindowMaskGUI *gui)
3092  : BC_DialogThread()
3093 {
3094         this->mwindow = mwindow;
3095         this->gui = gui;
3096         pgui = 0;
3097 }
3098
3099 CWindowMaskPresetDialog::~CWindowMaskPresetDialog()
3100 {
3101         close_window();
3102 }
3103
3104 void CWindowMaskPresetDialog::handle_close_event(int result)
3105 {
3106         pgui = 0;
3107 }
3108
3109 void CWindowMaskPresetDialog::handle_done_event(int result)
3110 {
3111         if( result ) return;
3112         const char *nm = pgui->preset_text->get_text();
3113         if( keyframe )
3114                 gui->save_mask(nm);
3115         else
3116                 gui->del_mask(nm);
3117 }
3118
3119 BC_Window* CWindowMaskPresetDialog::new_gui()
3120 {
3121         pgui = new CWindowMaskPresetGUI(this, sx, sy,
3122                 keyframe ? _(PROGRAM_NAME ": Save Mask") :
3123                            _(PROGRAM_NAME ": Delete Mask"));
3124         pgui->create_objects();
3125         return pgui;
3126 }
3127
3128 void CWindowMaskPresetDialog::start_dialog(int sx, int sy, MaskAuto *keyframe)
3129 {
3130         close_window();
3131         this->sx = sx;  this->sy = sy;
3132         this->keyframe = keyframe;
3133         start();
3134 }
3135
3136 CWindowMaskPresetGUI::CWindowMaskPresetGUI(CWindowMaskPresetDialog *preset_dialog,
3137                         int x, int y, const char *title)
3138  : BC_Window(title, x, y, 320, 100, 320, 100, 0, 0, 1)
3139 {
3140         this->preset_dialog = preset_dialog;
3141 }
3142
3143 void CWindowMaskPresetGUI::create_objects()
3144 {
3145         int x = 10, y = 10, pad = 8;
3146         lock_window("CWindowMaskPresetGUI::create_objects");
3147         BC_Title *title;
3148         add_subwindow(title = new BC_Title(x, y,
3149                 preset_dialog->keyframe ? _("Save mask:") : _("Delete mask:")));
3150         int x1 = x + title->get_w() + pad;
3151         int x2 = get_w() - x - pad - x1 -
3152                 BC_WindowBase::get_resources()->listbox_button[0]->get_w();
3153         CWindowMaskGUI *gui = preset_dialog->gui;
3154         preset_text = new CWindowMaskPresetText(this,
3155                 x1, y, x2, 120, gui->mask_name->get_text());
3156         preset_text->create_objects();
3157         preset_text->set_tooltip(_("Mask name"));
3158         preset_text->update_items();
3159         add_subwindow(new BC_OKButton(this));
3160         add_subwindow(new BC_CancelButton(this));
3161         show_window();
3162         raise_window();
3163         unlock_window();
3164 }
3165
3166 CWindowMaskPresetText::CWindowMaskPresetText(CWindowMaskPresetGUI *pgui,
3167                 int x, int y, int w, int h, const char *text)
3168  : BC_PopupTextBox(pgui, 0, text, x, y, w, h)
3169 {
3170         this->pgui = pgui;
3171 }
3172
3173 int CWindowMaskPresetText::handle_event()
3174 {
3175         int k = get_number();
3176         if( k >= 0 && k<mask_items.size() )
3177                 update(mask_items[k]->get_text());
3178         return 1;
3179 }
3180
3181 void CWindowMaskPresetText::update_items()
3182 {
3183         mask_items.remove_all_objects();
3184         ArrayList<SubMask *> masks;
3185         pgui->preset_dialog->gui->load_masks(masks);
3186         for( int i=0; i<masks.size(); ++i ) {
3187                 char text[BCSTRLEN];  memset(text, 0, sizeof(text));
3188                 strncpy(text, masks[i]->name, sizeof(text-1));
3189                 mask_items.append(new CWindowMaskItem(text));
3190         }
3191         masks.remove_all_objects();
3192         update_list(&mask_items);
3193 }
3194
3195
3196 CWindowMaskDelete::CWindowMaskDelete(MWindow *mwindow,
3197         CWindowMaskGUI *gui, int x, int y, int w)
3198  : BC_GenericButton(x, y, w, _("Del"))
3199 {
3200         this->mwindow = mwindow;
3201         this->gui = gui;
3202         set_tooltip(_("delete preset"));
3203 }
3204
3205 int CWindowMaskDelete::handle_event()
3206 {
3207         int sx = 0, sy = 0;
3208         gui->get_abs_cursor(sx, sy);
3209         if( !gui->preset_dialog )
3210                 gui->preset_dialog = new CWindowMaskPresetDialog(mwindow, gui);
3211         gui->preset_dialog->start_dialog(sx, sy, 0);
3212         return 1;
3213 }
3214
3215
3216 CWindowMaskCenter::CWindowMaskCenter(MWindow *mwindow,
3217         CWindowMaskGUI *gui, int x, int y, int w)
3218  : BC_GenericButton(x, y, w, _("Center"))
3219 {
3220         this->mwindow = mwindow;
3221         this->gui = gui;
3222         set_tooltip(_("center mask"));
3223 }
3224
3225 int CWindowMaskCenter::handle_event()
3226 {
3227         return gui->center_mask();
3228 }
3229
3230
3231 CWindowMaskNormal::CWindowMaskNormal(MWindow *mwindow,
3232         CWindowMaskGUI *gui, int x, int y, int w)
3233  : BC_GenericButton(x, y, w, _("Normal"))
3234 {
3235         this->mwindow = mwindow;
3236         this->gui = gui;
3237         set_tooltip(_("normalize mask"));
3238 }
3239
3240 int CWindowMaskNormal::handle_event()
3241 {
3242         return gui->normal_mask();
3243 }
3244
3245
3246 CWindowMaskShape::CWindowMaskShape(MWindow *mwindow, CWindowMaskGUI *gui)
3247  : BC_ListBox(-1, -1, 1, 1, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
3248 {
3249         this->mwindow = mwindow;
3250         this->gui = gui;
3251         set_use_button(0);
3252 }
3253
3254 CWindowMaskShape::~CWindowMaskShape()
3255 {
3256 }
3257
3258 void CWindowMaskShape::builtin_shape(int i, SubMask *sub_mask)
3259 {
3260         int out_w = mwindow->edl->session->output_w;
3261         int out_h = mwindow->edl->session->output_h;
3262         float cx = out_w/2.f, cy = out_h/2.f;
3263         float r = bmax(cx, cy) / 4.f;
3264         double c = 4*(sqrt(2.)-1)/3; // bezier aprox circle
3265         float r2 = r / 2.f, rc = r*c, r4 = r / 4.f;
3266         MaskPoint *pt = 0;
3267         ArrayList<MaskPoint*> &points = sub_mask->points;
3268         points.remove_all_objects();
3269         switch( i ) {
3270         case 0: // square
3271                 points.append(pt = new MaskPoint());
3272                 pt->x = cx - r;  pt->y = cy - r;
3273                 points.append(pt = new MaskPoint());
3274                 pt->x = cx + r;  pt->y = cy - r;
3275                 points.append(pt = new MaskPoint());
3276                 pt->x = cx + r;  pt->y = cy + r;
3277                 points.append(pt = new MaskPoint());
3278                 pt->x = cx - r;  pt->y = cy + r;
3279                 break;
3280         case 1: // circle
3281                 points.append(pt = new MaskPoint());
3282                 pt->x = cx - r;  pt->y = cy - r;
3283                 pt->control_x1 = -rc;  pt->control_y1 =  rc;
3284                 pt->control_x2 =  rc;  pt->control_y2 = -rc;
3285                 points.append(pt = new MaskPoint());
3286                 pt->x = cx + r;  pt->y = cy - r;
3287                 pt->control_x1 = -rc;  pt->control_y1 = -rc;
3288                 pt->control_x2 =  rc;  pt->control_y2 =  rc;
3289                 points.append(pt = new MaskPoint());
3290                 pt->x = cx + r;  pt->y = cy + r;
3291                 pt->control_x1 =  rc;  pt->control_y1 = -rc;
3292                 pt->control_x2 = -rc;  pt->control_y2 =  rc;
3293                 points.append(pt = new MaskPoint());
3294                 pt->x = cx - r;  pt->y = cy + r;
3295                 pt->control_x1 =  rc;  pt->control_y1 =  rc;
3296                 pt->control_x2 = -rc;  pt->control_y2 = -rc;
3297                 break;
3298         case 2: // triangle
3299                 points.append(pt = new MaskPoint());
3300                 pt->x = cx + 0;  pt->y = cy - r*(sqrt(3.)-1.);
3301                 points.append(pt = new MaskPoint());
3302                 pt->x = cx + r;  pt->y = cy + r;
3303                 points.append(pt = new MaskPoint());
3304                 pt->x = cx - r;  pt->y = cy + r;
3305                 break;
3306         case 3: // oval
3307                 points.append(pt = new MaskPoint());
3308                 pt->x = cx - r;  pt->y = cy - r2;
3309                 pt->control_x1 = -r2;  pt->control_y1 =  r4;
3310                 pt->control_x2 =  r2;  pt->control_y2 = -r4;
3311                 points.append(pt = new MaskPoint());
3312                 pt->x = cx + r;  pt->y = cy - r2;
3313                 pt->control_x1 = -r2;  pt->control_y1 = -r4;
3314                 pt->control_x2 =  r2;  pt->control_y2 =  r4;
3315                 points.append(pt = new MaskPoint());
3316                 pt->x = cx + r;  pt->y = cy + r2;
3317                 pt->control_x1 =  r2;  pt->control_y1 = -r4;
3318                 pt->control_x2 = -r2;  pt->control_y2 =  r4;
3319                 points.append(pt = new MaskPoint());
3320                 pt->x = cx - r;  pt->y = cy + r2;
3321                 pt->control_x1 =  r2;  pt->control_y1 =  r4;
3322                 pt->control_x2 = -r2;  pt->control_y2 = -r4;
3323                 break;
3324         }
3325 }
3326
3327 void CWindowMaskShape::load_shape(int i, SubMask *sub_mask)
3328 {
3329         ArrayList<SubMask *> masks;
3330         gui->load_masks(masks);
3331         sub_mask->copy_from(*masks[i], 0);
3332         masks.remove_all_objects();
3333 }
3334
3335 int CWindowMaskShape::handle_event()
3336 {
3337         MaskAutos *autos;
3338         MaskAuto *keyframe;
3339         Track *track;
3340         MaskPoint *point;
3341         SubMask *mask;
3342 #ifdef USE_KEYFRAME_SPANNING
3343         int create_it = 0;
3344 #else
3345         int create_it = 1;
3346 #endif
3347
3348         mwindow->undo->update_undo_before(_("mask shape"), this);
3349
3350 // Get existing keyframe
3351         gui->get_keyframe(track, autos, keyframe,
3352                         mask, point, create_it);
3353         int k = get_selection_number(0, 0);
3354         if( track && k >= 0 ) {
3355 #ifdef USE_KEYFRAME_SPANNING
3356                 MaskAuto temp_keyframe(mwindow->edl, autos);
3357                 temp_keyframe.copy_data(keyframe);
3358                 keyframe = &temp_keyframe;
3359                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
3360 #endif
3361                 if( mask ) {
3362                         if( k < 4 )
3363                                 builtin_shape(k, mask);
3364                         else
3365                                 load_shape(k-4, mask);
3366 #ifdef USE_KEYFRAME_SPANNING
3367                         autos->update_parameter(keyframe);
3368 #endif
3369                         gui->update();
3370                         gui->update_preview(1);
3371                 }
3372         }
3373         mwindow->undo->update_undo_after(_("mask shape"), LOAD_AUTOMATION);
3374         return 1;
3375 }
3376
3377 void CWindowMaskShape::create_objects()
3378 {
3379         shape_items.remove_all_objects();
3380         shape_items.append(new BC_ListBoxItem(_("square")));
3381         shape_items.append(new BC_ListBoxItem(_("circle")));
3382         shape_items.append(new BC_ListBoxItem(_("triangle")));
3383         shape_items.append(new BC_ListBoxItem(_("oval")));
3384         ArrayList<SubMask *> masks;
3385         gui->load_masks(masks);
3386         for( int i=0; i<masks.size(); ++i )
3387                 shape_items.append(new BC_ListBoxItem(masks[i]->name));
3388         masks.remove_all_objects();
3389         update(&shape_items, 0, 0, 1);
3390 }
3391
3392 void CWindowMaskGUI::load_masks(ArrayList<SubMask *> &masks)
3393 {
3394         char path[BCTEXTLEN];
3395         sprintf(path, "%s/%s", File::get_config_path(), MASKS_FILE);
3396         FileSystem fs;
3397         fs.complete_path(path);
3398         FileXML file;
3399         file.read_from_file(path, 1);
3400
3401         masks.remove_all_objects();
3402         int result;
3403         while( !(result = file.read_tag()) ) {
3404                 if( file.tag.title_is("MASK") ) {
3405                         SubMask *sub_mask = new SubMask(0, -1);
3406                         char name[BCTEXTLEN];  name[0] = 0;
3407                         file.tag.get_property("NAME", name);
3408                         strncpy(sub_mask->name, name, sizeof(sub_mask->name));
3409                         sub_mask->load(&file);
3410                         masks.append(sub_mask);
3411                 }
3412         }
3413 }
3414
3415 void CWindowMaskGUI::save_masks(ArrayList<SubMask *> &masks)
3416 {
3417         FileXML file;
3418         for( int i=0; i<masks.size(); ++i ) {
3419                 SubMask *sub_mask = masks[i];
3420                 sub_mask->copy(&file);
3421         }
3422         file.terminate_string();
3423
3424         char path[BCTEXTLEN];
3425         sprintf(path, "%s/%s", File::get_config_path(), MASKS_FILE);
3426         FileSystem fs;
3427         fs.complete_path(path);
3428         file.write_to_file(path);
3429 }
3430
3431
3432 CWindowRulerGUI::CWindowRulerGUI(MWindow *mwindow, CWindowTool *thread)
3433  : CWindowToolGUI(mwindow, thread, _(PROGRAM_NAME ": Ruler"), 320, 240)
3434 {
3435 }
3436
3437 CWindowRulerGUI::~CWindowRulerGUI()
3438 {
3439 }
3440
3441 void CWindowRulerGUI::create_objects()
3442 {
3443         int x = 10, y = 10, x1 = 100;
3444         BC_Title *title;
3445
3446         lock_window("CWindowRulerGUI::create_objects");
3447         add_subwindow(title = new BC_Title(x, y, _("Current:")));
3448         add_subwindow(current = new BC_TextBox(x1, y, 200, 1, ""));
3449         y += title->get_h() + 5;
3450         add_subwindow(title = new BC_Title(x, y, _("Point 1:")));
3451         add_subwindow(point1 = new BC_TextBox(x1, y, 200, 1, ""));
3452         y += title->get_h() + 5;
3453         add_subwindow(title = new BC_Title(x, y, _("Point 2:")));
3454         add_subwindow(point2 = new BC_TextBox(x1, y, 200, 1, ""));
3455         y += title->get_h() + 5;
3456         add_subwindow(title = new BC_Title(x, y, _("Deltas:")));
3457         add_subwindow(deltas = new BC_TextBox(x1, y, 200, 1, ""));
3458         y += title->get_h() + 5;
3459         add_subwindow(title = new BC_Title(x, y, _("Distance:")));
3460         add_subwindow(distance = new BC_TextBox(x1, y, 200, 1, ""));
3461         y += title->get_h() + 5;
3462         add_subwindow(title = new BC_Title(x, y, _("Angle:")));
3463         add_subwindow(angle = new BC_TextBox(x1, y, 200, 1, ""));
3464         y += title->get_h() + 10;
3465         char string[BCTEXTLEN];
3466         sprintf(string,
3467                  _("Press Ctrl to lock ruler to the\nnearest 45%c%c angle."),
3468                 0xc2, 0xb0); // degrees utf
3469         add_subwindow(title = new BC_Title(x,
3470                 y,
3471                 string));
3472         y += title->get_h() + 10;
3473         sprintf(string, _("Press Alt to translate the ruler."));
3474         add_subwindow(title = new BC_Title(x,
3475                 y,
3476                 string));
3477         update();
3478         unlock_window();
3479 }
3480
3481 void CWindowRulerGUI::update()
3482 {
3483         char string[BCTEXTLEN];
3484         int cx = mwindow->session->cwindow_output_x;
3485         int cy = mwindow->session->cwindow_output_y;
3486         sprintf(string, "%d, %d", cx, cy);
3487         current->update(string);
3488         double x1 = mwindow->edl->session->ruler_x1;
3489         double y1 = mwindow->edl->session->ruler_y1;
3490         sprintf(string, "%.0f, %.0f", x1, y1);
3491         point1->update(string);
3492         double x2 = mwindow->edl->session->ruler_x2;
3493         double y2 = mwindow->edl->session->ruler_y2;
3494         sprintf(string, "%.0f, %.0f", x2, y2);
3495         point2->update(string);
3496         double dx = x2 - x1, dy = y2 - y1;
3497         sprintf(string, "%s%.0f, %s%.0f", (dx>=0? "+":""), dx, (dy>=0? "+":""), dy);
3498         deltas->update(string);
3499         double d = sqrt(dx*dx + dy*dy);
3500         sprintf(string, _("%0.01f pixels"), d);
3501         distance->update(string);
3502         double a = d > 0 ? (atan2(-dy, dx) * 180/M_PI) : 0.;
3503         sprintf(string, "%0.02f %c%c", a, 0xc2, 0xb0);
3504         angle->update(string);
3505 }
3506
3507 void CWindowRulerGUI::handle_event()
3508 {
3509 }
3510
3511
3512
3513