upgrade bld_prep.sh debian libpng, add rectify timeline audio pref, rework maskgui...
[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)-1);
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];  memset(text, 0, sizeof(text));
1653                 if( i < sz ) {
1654                         SubMask *sub_mask = keyframe->masks.get(i);
1655                         strncpy(text, sub_mask->name, sizeof(text)-1);
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 int CWindowMaskFocus::calculate_w(CWindowMaskGUI *gui)
1998 {
1999         int w, h;
2000         calculate_extents(gui, &w, &h, _("Focus"));
2001         return w;
2002 }
2003
2004 CWindowMaskScaleXY::CWindowMaskScaleXY(MWindow *mwindow, CWindowMaskGUI *gui,
2005                 int x, int y, VFrame **data, int v, int id, const char *tip)
2006  : BC_Toggle(x, y, data, v)
2007 {
2008         this->id = id;
2009         this->mwindow = mwindow;
2010         this->gui = gui;
2011         set_tooltip(tip);
2012 }
2013
2014 CWindowMaskScaleXY::~CWindowMaskScaleXY()
2015 {
2016 }
2017
2018 int CWindowMaskScaleXY::handle_event()
2019 {
2020         gui->scale_mode = id;
2021         gui->mask_scale_x->update(id == 0);
2022         gui->mask_scale_y->update(id == 1);
2023         gui->mask_scale_xy->update(id == 2);
2024         return 1;
2025 }
2026
2027 CWindowMaskHelp::CWindowMaskHelp(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2028  : BC_CheckBox(x, y, 0, _("Help"))
2029 {
2030         this->mwindow = mwindow;
2031         this->gui = gui;
2032         set_tooltip(_("Show help text"));
2033 }
2034
2035 CWindowMaskHelp::~CWindowMaskHelp()
2036 {
2037 }
2038
2039 int CWindowMaskHelp::handle_event()
2040 {
2041         gui->helped = get_value();
2042         gui->resize_window(gui->get_w(),
2043                 gui->helped ? gui->help_h : gui->help_y);
2044         gui->update();
2045         return 1;
2046 }
2047
2048 CWindowMaskDrawMarkers::CWindowMaskDrawMarkers(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2049  : BC_CheckBox(x, y, gui->markers, _("Markers"))
2050 {
2051         this->mwindow = mwindow;
2052         this->gui = gui;
2053         set_tooltip("Display points");
2054 }
2055
2056 CWindowMaskDrawMarkers::~CWindowMaskDrawMarkers()
2057 {
2058 }
2059
2060 int CWindowMaskDrawMarkers::handle_event()
2061 {
2062         gui->markers = get_value();
2063         gui->update();
2064         gui->update_preview();
2065         return 1;
2066 }
2067
2068 CWindowMaskDrawBoundary::CWindowMaskDrawBoundary(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2069  : BC_CheckBox(x, y, gui->boundary, _("Boundary"))
2070 {
2071         this->mwindow = mwindow;
2072         this->gui = gui;
2073         set_tooltip("Display mask outline");
2074 }
2075
2076 CWindowMaskDrawBoundary::~CWindowMaskDrawBoundary()
2077 {
2078 }
2079
2080 int CWindowMaskDrawBoundary::handle_event()
2081 {
2082         gui->boundary = get_value();
2083         gui->update();
2084         gui->update_preview();
2085         return 1;
2086 }
2087
2088
2089 CWindowMaskFeather::CWindowMaskFeather(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2090  : BC_TumbleTextBox(gui, 0, -FEATHER_MAX, FEATHER_MAX, x, y, 64, 2)
2091 {
2092         this->mwindow = mwindow;
2093         this->gui = gui;
2094 }
2095 CWindowMaskFeather::~CWindowMaskFeather()
2096 {
2097 }
2098
2099 int CWindowMaskFeather::update(float v)
2100 {
2101         gui->feather_slider->update(v);
2102         return BC_TumbleTextBox::update(v);
2103 }
2104
2105 int CWindowMaskFeather::update_value(float v)
2106 {
2107         MaskAutos *autos;
2108         MaskAuto *keyframe;
2109         Track *track;
2110         MaskPoint *point;
2111         SubMask *mask;
2112 #ifdef USE_KEYFRAME_SPANNING
2113         int create_it = 0;
2114 #else
2115         int create_it = 1;
2116 #endif
2117
2118         mwindow->undo->update_undo_before(_("mask feather"), this);
2119
2120 // Get existing keyframe
2121         gui->get_keyframe(track, autos, keyframe,
2122                         mask, point, create_it);
2123         if( track ) {
2124                 int gang = gui->gang_feather->get_value();
2125 #ifdef USE_KEYFRAME_SPANNING
2126                 MaskAuto temp_keyframe(mwindow->edl, autos);
2127                 temp_keyframe.copy_data(keyframe);
2128                 keyframe = &temp_keyframe;
2129 #endif
2130                 float change = v - mask->feather;
2131                 int k = mwindow->edl->session->cwindow_mask;
2132                 int n = gang ? keyframe->masks.size() : k+1;
2133                 for( int i=gang? 0 : k; i<n; ++i ) {
2134                         if( !gui->mask_enables[i]->get_value() ) continue;
2135                         SubMask *sub_mask = keyframe->get_submask(i);
2136                         float feather = sub_mask->feather + change;
2137                         bclamp(feather, -FEATHER_MAX, FEATHER_MAX);
2138                         sub_mask->feather = feather;
2139                 }
2140 #ifdef USE_KEYFRAME_SPANNING
2141                 autos->update_parameter(keyframe);
2142 #endif
2143                 gui->update_preview();
2144         }
2145
2146         mwindow->undo->update_undo_after(_("mask feather"), LOAD_AUTOMATION);
2147         return 1;
2148 }
2149
2150 int CWindowMaskFeather::handle_event()
2151 {
2152         float v = atof(get_text());
2153         gui->feather_slider->update(v);
2154         return gui->feather->update_value(v);
2155 }
2156
2157 CWindowMaskFeatherSlider::CWindowMaskFeatherSlider(MWindow *mwindow,
2158                 CWindowMaskGUI *gui, int x, int y, int w, float v)
2159  : BC_FSlider(x, y, 0, w, w, -FEATHER_MAX, FEATHER_MAX, v)
2160 {
2161         this->mwindow = mwindow;
2162         this->gui = gui;
2163         set_precision(0.01);
2164         timer = new Timer();
2165         stick = 0;
2166         last_v = 0;
2167 }
2168
2169 CWindowMaskFeatherSlider::~CWindowMaskFeatherSlider()
2170 {
2171         delete timer;
2172 }
2173
2174 int CWindowMaskFeatherSlider::handle_event()
2175 {
2176         float v = get_value();
2177         if( stick > 0 ) {
2178                 int64_t ms = timer->get_difference();
2179                 if( ms < 250 && --stick > 0 ) {
2180                         if( get_value() == 0 ) return 1;
2181                         update(v = 0);
2182                 }
2183                 else {
2184                         stick = 0;
2185                         last_v = v;
2186                 }
2187         }
2188         else if( (last_v>=0 && v<0) || (last_v<0 && v>=0) ) {
2189                 stick = 16;
2190                 v = 0;
2191         }
2192         else
2193                 last_v = v;
2194         timer->update();
2195         gui->feather->BC_TumbleTextBox::update(v);
2196         return gui->feather->update_value(v);
2197 }
2198
2199 int CWindowMaskFeatherSlider::update(float v)
2200 {
2201         return BC_FSlider::update(v);
2202 }
2203
2204 CWindowMaskFade::CWindowMaskFade(MWindow *mwindow, CWindowMaskGUI *gui, int x, int y)
2205  : BC_TumbleTextBox(gui, 0, -100.f, 100.f, x, y, 64, 2)
2206 {
2207         this->mwindow = mwindow;
2208         this->gui = gui;
2209 }
2210 CWindowMaskFade::~CWindowMaskFade()
2211 {
2212 }
2213
2214 int CWindowMaskFade::update(float v)
2215 {
2216         gui->fade_slider->update(v);
2217         return BC_TumbleTextBox::update(v);
2218 }
2219
2220 int CWindowMaskFade::update_value(float v)
2221 {
2222         MaskAutos *autos;
2223         MaskAuto *keyframe;
2224         Track *track;
2225         MaskPoint *point;
2226         SubMask *mask;
2227 #ifdef USE_KEYFRAME_SPANNING
2228         int create_it = 0;
2229 #else
2230         int create_it = 1;
2231 #endif
2232
2233         mwindow->undo->update_undo_before(_("mask fade"), this);
2234
2235 // Get existing keyframe
2236         gui->get_keyframe(track, autos, keyframe,
2237                         mask, point, create_it);
2238         if( track ) {
2239                 int gang = gui->gang_fader->get_value();
2240 #ifdef USE_KEYFRAME_SPANNING
2241                 MaskAuto temp_keyframe(mwindow->edl, autos);
2242                 temp_keyframe.copy_data(keyframe);
2243                 keyframe = &temp_keyframe;
2244 #endif
2245                 float change = v - mask->fader;
2246                 int k = mwindow->edl->session->cwindow_mask;
2247                 int n = gang ? keyframe->masks.size() : k+1;
2248                 for( int i=gang? 0 : k; i<n; ++i ) {
2249                         if( !gui->mask_enables[i]->get_value() ) continue;
2250                         SubMask *sub_mask = keyframe->get_submask(i);
2251                         float fader = sub_mask->fader + change;
2252                         bclamp(fader, -100.f, 100.f);
2253                         sub_mask->fader = fader;
2254                 }
2255 #ifdef USE_KEYFRAME_SPANNING
2256                 autos->update_parameter(keyframe);
2257 #endif
2258                 gui->update_preview();
2259         }
2260
2261         mwindow->undo->update_undo_after(_("mask fade"), LOAD_AUTOMATION);
2262         return 1;
2263 }
2264
2265 int CWindowMaskFade::handle_event()
2266 {
2267         float v = atof(get_text());
2268         gui->fade_slider->update(v);
2269         return gui->fade->update_value(v);
2270 }
2271
2272 CWindowMaskFadeSlider::CWindowMaskFadeSlider(MWindow *mwindow, CWindowMaskGUI *gui,
2273                 int x, int y, int w)
2274  : BC_ISlider(x, y, 0, w, w, -200, 200, 0)
2275 {
2276         this->mwindow = mwindow;
2277         this->gui = gui;
2278         timer = new Timer();
2279         stick = 0;
2280         last_v = 0;
2281 }
2282
2283 CWindowMaskFadeSlider::~CWindowMaskFadeSlider()
2284 {
2285         delete timer;
2286 }
2287
2288 int CWindowMaskFadeSlider::handle_event()
2289 {
2290         float v = 100*get_value()/200;
2291         if( stick > 0 ) {
2292                 int64_t ms = timer->get_difference();
2293                 if( ms < 250 && --stick > 0 ) {
2294                         if( get_value() == 0 ) return 1;
2295                         update(v = 0);
2296                 }
2297                 else {
2298                         stick = 0;
2299                         last_v = v;
2300                 }
2301         }
2302         else if( (last_v>=0 && v<0) || (last_v<0 && v>=0) ) {
2303                 stick = 16;
2304                 v = 0;
2305         }
2306         else
2307                 last_v = v;
2308         timer->update();
2309         gui->fade->BC_TumbleTextBox::update(v);
2310         return gui->fade->update_value(v);
2311 }
2312
2313 int CWindowMaskFadeSlider::update(int64_t v)
2314 {
2315         return BC_ISlider::update(200*v/100);
2316 }
2317
2318 CWindowMaskGangFader::CWindowMaskGangFader(MWindow *mwindow,
2319                 CWindowMaskGUI *gui, int x, int y)
2320  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2321 {
2322         this->mwindow = mwindow;
2323         this->gui = gui;
2324         set_tooltip(_("Gang fader"));
2325 }
2326
2327 CWindowMaskGangFader::~CWindowMaskGangFader()
2328 {
2329 }
2330
2331 int CWindowMaskGangFader::handle_event()
2332 {
2333         return 1;
2334 }
2335
2336 CWindowMaskGangFocus::CWindowMaskGangFocus(MWindow *mwindow,
2337                 CWindowMaskGUI *gui, int x, int y)
2338  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2339 {
2340         this->mwindow = mwindow;
2341         this->gui = gui;
2342         set_tooltip(_("Gang rotate/scale/translate"));
2343 }
2344
2345 CWindowMaskGangFocus::~CWindowMaskGangFocus()
2346 {
2347 }
2348
2349 int CWindowMaskGangFocus::handle_event()
2350 {
2351         return 1;
2352 }
2353
2354
2355 CWindowMaskSmoothButton::CWindowMaskSmoothButton(MWindow *mwindow, CWindowMaskGUI *gui,
2356                 const char *tip, int type, int on, int x, int y, const char *images)
2357  : BC_Button(x, y, mwindow->theme->get_image_set(images))
2358 {
2359         this->mwindow = mwindow;
2360         this->gui = gui;
2361         this->type = type;
2362         this->on = on;
2363         set_tooltip(tip);
2364 }
2365
2366 int CWindowMaskSmoothButton::handle_event()
2367 {
2368         return gui->smooth_mask(type, on);
2369 }
2370
2371 CWindowMaskBeforePlugins::CWindowMaskBeforePlugins(CWindowMaskGUI *gui, int x, int y)
2372  : BC_CheckBox(x, y, 1, _("Apply mask before plugins"))
2373 {
2374         this->gui = gui;
2375 }
2376
2377 int CWindowMaskBeforePlugins::handle_event()
2378 {
2379         Track *track;
2380         MaskAutos *autos;
2381         MaskAuto *keyframe;
2382         SubMask *mask;
2383         MaskPoint *point;
2384         gui->get_keyframe(track, autos, keyframe, mask, point, 1);
2385
2386         if (keyframe) {
2387                 int v = get_value();
2388 #ifdef USE_KEYFRAME_SPANNING
2389                 MaskAuto temp_keyframe(gui->mwindow->edl, autos);
2390                 temp_keyframe.copy_data(keyframe);
2391                 temp_keyframe.apply_before_plugins = v;
2392                 autos->update_parameter(&temp_keyframe);
2393 #else
2394                 keyframe->apply_before_plugins = v;
2395 #endif
2396                 gui->update_preview();
2397         }
2398         return 1;
2399 }
2400
2401
2402 CWindowDisableOpenGLMasking::CWindowDisableOpenGLMasking(CWindowMaskGUI *gui, int x, int y)
2403  : BC_CheckBox(x, y, 1, _("Disable OpenGL masking"))
2404 {
2405         this->gui = gui;
2406 }
2407
2408 int CWindowDisableOpenGLMasking::handle_event()
2409 {
2410         Track *track;
2411         MaskAutos *autos;
2412         MaskAuto *keyframe;
2413         SubMask *mask;
2414         MaskPoint *point;
2415         gui->get_keyframe(track, autos, keyframe, mask, point, 1);
2416
2417         if( keyframe ) {
2418                 int v = get_value();
2419 #ifdef USE_KEYFRAME_SPANNING
2420                 MaskAuto temp_keyframe(gui->mwindow->edl, autos);
2421                 temp_keyframe.copy_data(keyframe);
2422                 temp_keyframe.disable_opengl_masking = v;
2423                 autos->update_parameter(&temp_keyframe);
2424 #else
2425                 keyframe->disable_opengl_masking = v;
2426 #endif
2427                 gui->update_preview();
2428         }
2429         return 1;
2430 }
2431
2432
2433 CWindowMaskClrMask::CWindowMaskClrMask(MWindow *mwindow,
2434                 CWindowMaskGUI *gui, int x, int y)
2435  : BC_Button(x, y, mwindow->theme->get_image_set("reset_button"))
2436 {
2437         this->mwindow = mwindow;
2438         this->gui = gui;
2439         set_tooltip(_("Delete all masks"));
2440 }
2441
2442 CWindowMaskClrMask::~CWindowMaskClrMask()
2443 {
2444 }
2445
2446 int CWindowMaskClrMask::calculate_w(MWindow *mwindow)
2447 {
2448         VFrame *vfrm = *mwindow->theme->get_image_set("reset_button");
2449         return vfrm->get_w();
2450 }
2451
2452 int CWindowMaskClrMask::handle_event()
2453 {
2454         MaskAutos *autos;
2455         MaskAuto *keyframe;
2456         Track *track;
2457         MaskPoint *point;
2458         SubMask *mask;
2459
2460 // Get existing keyframe
2461         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
2462
2463         if( track ) {
2464                 mwindow->undo->update_undo_before(_("del masks"), 0);
2465                 ((MaskAutos*)track->automation->autos[AUTOMATION_MASK])->clear_all();
2466                 mwindow->undo->update_undo_after(_("del masks"), LOAD_AUTOMATION);
2467         }
2468
2469         gui->update();
2470         gui->update_preview(1);
2471         return 1;
2472 }
2473
2474 CWindowMaskGangFeather::CWindowMaskGangFeather(MWindow *mwindow,
2475                 CWindowMaskGUI *gui, int x, int y)
2476  : BC_Toggle(x, y, mwindow->theme->get_image_set("gangpatch_data"), 0)
2477 {
2478         this->mwindow = mwindow;
2479         this->gui = gui;
2480         set_tooltip(_("Gang feather"));
2481 }
2482
2483 CWindowMaskGangFeather::~CWindowMaskGangFeather()
2484 {
2485 }
2486
2487 int CWindowMaskGangFeather::handle_event()
2488 {
2489         return 1;
2490 }
2491
2492 CWindowMaskGUI::CWindowMaskGUI(MWindow *mwindow, CWindowTool *thread)
2493  : CWindowToolGUI(mwindow, thread,
2494         _(PROGRAM_NAME ": Mask"), 440, 700)
2495 {
2496         this->mwindow = mwindow;
2497         this->thread = thread;
2498         active_point = 0;
2499         fade = 0;
2500         feather = 0;
2501         focused = 0;
2502         scale_mode = 2;
2503         markers = 1;
2504         boundary = 1;
2505         preset_dialog = 0;
2506 }
2507 CWindowMaskGUI::~CWindowMaskGUI()
2508 {
2509         lock_window("CWindowMaskGUI::~CWindowMaskGUI");
2510         done_event();
2511         delete active_point;
2512         delete fade;
2513         delete feather;
2514         unlock_window();
2515         delete preset_dialog;
2516 }
2517
2518 void CWindowMaskGUI::create_objects()
2519 {
2520         Theme *theme = mwindow->theme;
2521         int x = 10, y = 10, margin = theme->widget_border;
2522         int clr_w = CWindowMaskClrMask::calculate_w(mwindow);
2523         int clr_x = get_w()-x - clr_w;
2524         int del_w = CWindowMaskDelMask::calculate_w(this,_("Delete"));
2525         int del_x = clr_x-2*margin - del_w;
2526
2527         lock_window("CWindowMaskGUI::create_objects");
2528         BC_TitleBar *title_bar;
2529         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks on Track")));
2530         y += title_bar->get_h() + margin;
2531         BC_Title *title;
2532         add_subwindow(title = new BC_Title(x,y, _("Track:")));
2533         int x1 = x + 90;
2534         Track *track = mwindow->cwindow->calculate_affected_track();
2535         const char *text = track ? track->title : "";
2536         mwindow->cwindow->mask_track_id = track ? track->get_id() : -1;
2537         mask_on_track = new CWindowMaskOnTrack(mwindow, this, x1, y, 100, text);
2538         mask_on_track->create_objects();
2539         mask_on_track->set_tooltip(_("Video track"));
2540         int x2 = x1 + mask_on_track->get_w();
2541         add_subwindow(mask_track_tumbler = new CWindowMaskTrackTumbler(mwindow, this, x2, y));
2542         mwindow->edl->local_session->solo_track_id = -1;
2543         x2 = del_x + (del_w - CWindowMaskSoloTrack::calculate_w(this)) / 2;
2544         add_subwindow(mask_solo_track = new CWindowMaskSoloTrack(mwindow, this, x2, y, 0));
2545         y += mask_on_track->get_h() + margin;
2546         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks")));
2547         y += title_bar->get_h() + margin;
2548         add_subwindow(title = new BC_Title(x, y, _("Mask:")));
2549         mask_name = new CWindowMaskName(mwindow, this, x1, y, "");
2550         mask_name->create_objects();
2551         mask_name->set_tooltip(_("Mask name"));
2552         add_subwindow(mask_clr = new CWindowMaskClrMask(mwindow, this, clr_x, y));
2553         add_subwindow(mask_del = new CWindowMaskDelMask(mwindow, this, del_x, y));
2554         y += mask_name->get_h() + 2*margin;
2555         BC_Bar *bar;
2556 //      add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2557 //      y += bar->get_h() + 2*margin;
2558
2559         add_subwindow(title = new BC_Title(x, y, _("Select:")));
2560         int bw = 0, bh = 0;
2561         BC_CheckBox::calculate_extents(this, &bw, &bh);
2562         int bdx = bw + 2*margin;
2563         x2 = x1;
2564         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2565                 int v = i == mwindow->edl->session->cwindow_mask ? 1 : 0;
2566                 mask_buttons[i] = new CWindowMaskButton(mwindow, this, x2, y, i, v);
2567                 add_subwindow(mask_buttons[i]);
2568         }
2569         x2 += margin;
2570         add_subwindow(mask_thumbler = new CWindowMaskThumbler(mwindow, this, x2, y));
2571         y += bh + margin;
2572         x2 = x1;
2573         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2574                 char text[BCSTRLEN];  sprintf(text, "%d", i);
2575                 int tx = (bw - get_text_width(MEDIUMFONT, text)) / 2;
2576                 mask_blabels[i] = new BC_Title(x2+tx, y, text);
2577                 add_subwindow(mask_blabels[i]);
2578         }
2579         y += mask_blabels[0]->get_h() + margin;
2580         add_subwindow(mask_unclr = new CWindowMaskUnclear(mwindow, this, x, y, x1-x-2*margin));
2581         x2 = x1;
2582         for( int i=0; i<SUBMASKS; x2+=bdx, ++i ) {
2583                 mask_enables[i] = new CWindowMaskEnable(mwindow, this, x2, y, i, 1);
2584                 add_subwindow(mask_enables[i]);
2585         }
2586         y += mask_enables[0]->get_h() + 2*margin;
2587         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Presets shapes")));
2588         y += title_bar->get_h() + margin;
2589         int x3 = get_w()/5+1, x4 = (5*get_w())/8 + 1;
2590         add_subwindow(mask_shape_sqr = new CWindowMaskShape(mwindow, this,
2591                 "mask_prst_sqr_images", MASK_SHAPE_SQUARE, x2=x3, y, _("Square")));
2592         x2 += mask_shape_sqr->get_w() + 2*margin;
2593         add_subwindow(mask_shape_crc = new CWindowMaskShape(mwindow, this,
2594                 "mask_prst_crc_images", MASK_SHAPE_CIRCLE, x2, y, _("Circle")));
2595         x2 += mask_shape_crc->get_w() + 2*margin;
2596         add_subwindow(mask_shape_tri = new CWindowMaskShape(mwindow, this,
2597                 "mask_prst_tri_images", MASK_SHAPE_TRIANGLE, x2, y, _("Triangle")));
2598         x2 += mask_shape_tri->get_w() + 2*margin;
2599         add_subwindow(mask_shape_ovl = new CWindowMaskShape(mwindow, this,
2600                 "mask_prst_ovl_images", MASK_SHAPE_OVAL, x2, y, _("Oval")));
2601         x2 += mask_shape_ovl->get_w() + 2*margin;
2602         add_subwindow(mask_load_list = new CWindowMaskLoadList(mwindow, this));
2603         add_subwindow(mask_load = new CWindowMaskLoad(mwindow, this, x2=x4, y, 80));
2604         x2 += mask_load->get_w() + 2*margin;
2605         add_subwindow(mask_save = new CWindowMaskSave(mwindow, this, x2, y, 80));
2606         x2 += mask_save->get_w() + 2*margin;
2607         add_subwindow(mask_delete = new CWindowMaskDelete(mwindow, this, x2, y, 80));
2608         y += mask_load->get_h() + 2*margin;
2609         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Position & Scale")));
2610         y += title_bar->get_h() + 2*margin;
2611         add_subwindow(mask_center = new CWindowMaskCenter(mwindow, this, x2=x3, y, 80));
2612         x2 += mask_center->get_w() + 2*margin;
2613         add_subwindow(mask_normal = new CWindowMaskNormal(mwindow, this, x2, y, 80));
2614
2615         add_subwindow(mask_scale_x = new CWindowMaskScaleXY(mwindow, this,
2616                 x2=x4, y, theme->get_image_set("mask_scale_x"), 0, 0, _("scale x")));
2617         x2 += mask_scale_x->get_w() + 2*margin;
2618         add_subwindow(mask_scale_y = new CWindowMaskScaleXY(mwindow, this,
2619                 x2, y, theme->get_image_set("mask_scale_y"), 0, 1, _("scale y")));
2620         x2 += mask_scale_y->get_w() + 2*margin;
2621         add_subwindow(mask_scale_xy = new CWindowMaskScaleXY(mwindow, this,
2622                 x2, y, theme->get_image_set("mask_scale_xy"), 1, 2, _("scale xy")));
2623         y += mask_center->get_h() + 2*margin;
2624         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Fade & Feather")));
2625         y += title_bar->get_h() + 2*margin;
2626
2627         add_subwindow(title = new BC_Title(x, y, _("Fade:")));
2628         fade = new CWindowMaskFade(mwindow, this, x1, y);
2629         fade->create_objects();
2630         x2 = x1 + fade->get_w() + 2*margin;
2631         int w2 = clr_x-2*margin - x2;
2632         add_subwindow(fade_slider = new CWindowMaskFadeSlider(mwindow, this, x2, y, w2));
2633         add_subwindow(gang_fader = new CWindowMaskGangFader(mwindow, this, clr_x, y));
2634         y += fade->get_h() + margin;
2635         add_subwindow(title = new BC_Title(x, y, _("Feather:")));
2636         feather = new CWindowMaskFeather(mwindow, this, x1, y);
2637         feather->create_objects();
2638         w2 = clr_x - 2*margin - x2;
2639         feather_slider = new CWindowMaskFeatherSlider(mwindow, this, x2, y, w2, 0);
2640         add_subwindow(feather_slider);
2641         add_subwindow(gang_feather = new CWindowMaskGangFeather(mwindow, this, clr_x, y));
2642         y += feather->get_h() + 2*margin;
2643         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Mask Points")));
2644         y += title_bar->get_h() + margin;
2645
2646         x1 = x + 60;
2647         add_subwindow(title = new BC_Title(x, y, _("Point:")));
2648         active_point = new CWindowMaskAffectedPoint(mwindow, this, x1, y);
2649         active_point->create_objects();
2650         x3  = x1 + active_point->get_w() + 4*margin;
2651 // typ=0, this mask, this point
2652         add_subwindow(mask_pnt_linear = new CWindowMaskSmoothButton(mwindow, this,
2653                 _("linear point"), 0, 0, x3, y, "mask_pnt_linear_images"));
2654         x4  = x3 + mask_pnt_linear->get_w() + 2*margin;
2655         add_subwindow(mask_pnt_smooth = new CWindowMaskSmoothButton(mwindow, this,
2656                 _("smooth point"), 0, 1, x4, y, "mask_pnt_smooth_images"));
2657         add_subwindow(del_point = new CWindowMaskDelPoint(mwindow, this, del_x, y));
2658         y += active_point->get_h() + margin;
2659         add_subwindow(title = new BC_Title(x, y, "X:"));
2660         this->x = new CWindowCoord(this, x1, y, (float)0.0);
2661         this->x->create_objects();
2662 // typ>0, this mask, all points
2663         add_subwindow(mask_crv_linear = new CWindowMaskSmoothButton(mwindow, this,
2664                 _("linear curve"), 1, 0, x3, y, "mask_crv_linear_images"));
2665         add_subwindow(mask_crv_smooth = new CWindowMaskSmoothButton(mwindow, this,
2666                 _("smooth curve"), 1, 1, x4, y, "mask_crv_smooth_images"));
2667         add_subwindow(draw_markers = new CWindowMaskDrawMarkers(mwindow, this, del_x, y));
2668         y += this->x->get_h() + margin;
2669         add_subwindow(title = new BC_Title(x, y, "Y:"));
2670         this->y = new CWindowCoord(this, x1, y, (float)0.0);
2671         this->y->create_objects();
2672 // typ<0, all masks, all points
2673         add_subwindow(mask_all_linear = new CWindowMaskSmoothButton(mwindow, this,
2674                 _("linear all"), -1, 0, x3, y, "mask_all_linear_images"));
2675         add_subwindow(mask_all_smooth = new CWindowMaskSmoothButton(mwindow, this,
2676                 _("smooth all"), -1, 1, x4, y, "mask_all_smooth_images"));
2677         add_subwindow(draw_boundary = new CWindowMaskDrawBoundary(mwindow, this, del_x, y));
2678         y += this->y->get_h() + 2*margin;
2679         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Pivot Point")));
2680         y += title_bar->get_h() + margin;
2681
2682         add_subwindow(title = new BC_Title(x, y, "X:"));
2683         float cx = mwindow->edl->session->output_w / 2.f;
2684         focus_x = new CWindowCoord(this, x1, y, cx);
2685         focus_x->create_objects();
2686         x2 = clr_x - 2*margin - CWindowMaskFocus::calculate_w(this);
2687         add_subwindow(focus = new CWindowMaskFocus(mwindow, this, x2, y));
2688         add_subwindow(gang_focus = new CWindowMaskGangFocus(mwindow, this, clr_x, y));
2689         y += focus_x->get_h() + margin;
2690         add_subwindow(title = new BC_Title(x, y, "Y:"));
2691         float cy = mwindow->edl->session->output_h / 2.f;
2692         focus_y = new CWindowCoord(this, x1, y, cy);
2693         focus_y->create_objects();
2694         y += focus_y->get_h() + 2*margin;
2695         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2696         y += bar->get_h() + margin;
2697         add_subwindow(this->apply_before_plugins = new CWindowMaskBeforePlugins(this, 10, y));
2698         y += this->apply_before_plugins->get_h();
2699         add_subwindow(this->disable_opengl_masking = new CWindowDisableOpenGLMasking(this, 10, y));
2700         add_subwindow(help = new CWindowMaskHelp(mwindow, this, del_x, y));
2701         y += this->disable_opengl_masking->get_h() + 2*margin;
2702         help_y = y;
2703         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2704         y += bar->get_h() + 2*margin;
2705         add_subwindow(title = new BC_Title(x, y, _(
2706                 "Shift+LMB: move an end point\n"
2707                 "Ctrl+LMB: move a control point\n"
2708                 "Alt+LMB: to drag translate the mask\n"
2709                 "Shift+Key Delete: to delete the point\n"
2710                 "Shift+MMB: Set Pivot Point at pointer\n"
2711                 "Wheel: rotate around Pivot Point\n"
2712                 "Shift+Wheel: scale around Pivot Point\n"
2713                 "Ctrl+Wheel: rotate/scale around pointer")));
2714         help_h = y + title->get_h() + 2*margin;
2715         update();
2716         resize_window(get_w(), help_y);
2717         unlock_window();
2718 }
2719
2720 int CWindowMaskGUI::close_event()
2721 {
2722         done_event();
2723         return CWindowToolGUI::close_event();
2724 }
2725
2726 void CWindowMaskGUI::done_event()
2727 {
2728         if( mwindow->in_destructor ) return;
2729         int &solo_track_id = mwindow->edl->local_session->solo_track_id;
2730         if( solo_track_id >= 0 ) {
2731                 solo_track_id = -1;
2732                 update_preview();
2733         }
2734 }
2735
2736 void CWindowMaskGUI::get_keyframe(Track* &track,
2737                 MaskAutos* &autos, MaskAuto* &keyframe,
2738                 SubMask* &mask, MaskPoint* &point, int create_it)
2739 {
2740         autos = 0;
2741         keyframe = 0;
2742
2743         track = mwindow->cwindow->calculate_mask_track();
2744         if( !track )
2745                 track = mwindow->cwindow->calculate_affected_track();
2746                 
2747         if(track) {
2748                 autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
2749                 keyframe = (MaskAuto*)mwindow->cwindow->calculate_affected_auto(
2750                         autos,
2751                         create_it);
2752         }
2753
2754         mask = !keyframe ? 0 :
2755                 keyframe->get_submask(mwindow->edl->session->cwindow_mask);
2756
2757         point = 0;
2758         if( keyframe ) {
2759                 if( mwindow->cwindow->gui->affected_point < mask->points.total &&
2760                         mwindow->cwindow->gui->affected_point >= 0 ) {
2761                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2762                 }
2763         }
2764 }
2765
2766 void CWindowMaskGUI::update()
2767 {
2768         Track *track;
2769         MaskAutos *autos;
2770         MaskAuto *keyframe;
2771         SubMask *mask;
2772         MaskPoint *point;
2773 //printf("CWindowMaskGUI::update 1\n");
2774         get_keyframe(track, autos, keyframe, mask, point, 0);
2775         mwindow->cwindow->mask_track_id = track ? track->get_id() : -1;
2776         mask_on_track->set_back_color(!track || track->record ?
2777                 get_resources()->text_background :
2778                 get_resources()->text_background_disarmed);
2779         mask_on_track->update_items();
2780         mask_on_track->update(!track ? "" : track->title);
2781         mask_name->update_items(keyframe);
2782         const char *text = "";
2783         int sz = !keyframe ? 0 : keyframe->masks.size();
2784         int k = mwindow->edl->session->cwindow_mask;
2785         if( k >= 0 && k < sz )
2786                 text = keyframe->masks[k]->name;
2787         else
2788                 k = mwindow->edl->session->cwindow_mask = 0;
2789         mask_name->update(text);
2790         update_buttons(keyframe, k);
2791         if( point ) {
2792                 x->update(point->x);
2793                 y->update(point->y);
2794         }
2795         if( track ) {
2796                 double position = mwindow->edl->local_session->get_selectionstart(1);
2797                 int64_t position_i = track->to_units(position, 0);
2798                 feather->update(autos->get_feather(position_i, k, PLAY_FORWARD));
2799                 fade->update(autos->get_fader(position_i, k, PLAY_FORWARD));
2800                 int show_mask = track->masks;
2801                 for( int i=0; i<SUBMASKS; ++i )
2802                         mask_enables[i]->update((show_mask>>i) & 1);
2803         }
2804         if( keyframe ) {
2805                 apply_before_plugins->update(keyframe->apply_before_plugins);
2806                 disable_opengl_masking->update(keyframe->disable_opengl_masking);
2807         }
2808         active_point->update((int64_t)mwindow->cwindow->gui->affected_point);
2809 }
2810
2811 void CWindowMaskGUI::handle_event()
2812 {
2813         Track *track;
2814         MaskAuto *keyframe;
2815         MaskAutos *autos;
2816         SubMask *mask;
2817         MaskPoint *point;
2818         get_keyframe(track, autos, keyframe, mask, point, 0);
2819
2820         mwindow->undo->update_undo_before(_("mask point"), this);
2821
2822         if(point)
2823         {
2824 #ifdef USE_KEYFRAME_SPANNING
2825 // Create temp keyframe
2826                 MaskAuto temp_keyframe(mwindow->edl, autos);
2827                 temp_keyframe.copy_data(keyframe);
2828 // Get affected point in temp keyframe
2829                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
2830                 if(mwindow->cwindow->gui->affected_point < mask->points.total &&
2831                         mwindow->cwindow->gui->affected_point >= 0)
2832                 {
2833                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2834                 }
2835
2836                 if(point)
2837                 {
2838                         point->x = atof(x->get_text());
2839                         point->y = atof(y->get_text());
2840 // Commit to spanned keyframes
2841                         autos->update_parameter(&temp_keyframe);
2842                 }
2843 #else
2844                 point->x = atof(x->get_text());
2845                 point->y = atof(y->get_text());
2846 #endif
2847         }
2848
2849         update_preview();
2850         mwindow->undo->update_undo_after(_("mask point"), LOAD_AUTOMATION);
2851 }
2852
2853 void CWindowMaskGUI::set_focused(int v, float cx, float cy)
2854 {
2855         CWindowGUI *cgui = mwindow->cwindow->gui;
2856         cgui->unlock_window();
2857         lock_window("CWindowMaskGUI::set_focused");
2858         if( focused != v )
2859                 focus->update(focused = v);
2860         focus_x->update(cx);
2861         focus_y->update(cy);
2862         unlock_window();
2863         cgui->lock_window("CWindowCanvas::set_focused");
2864 }
2865
2866 void CWindowMaskGUI::update_buttons(MaskAuto *keyframe, int k)
2867 {
2868         int text_color = get_resources()->default_text_color;
2869         int high_color = get_resources()->button_highlighted;
2870         for( int i=0; i<SUBMASKS; ++i ) {
2871                 int color = text_color;
2872                 if( keyframe ) {
2873                         SubMask *submask = keyframe->get_submask(i);
2874                         if( submask && submask->points.size() )
2875                                 color = high_color;
2876                 }
2877                 mask_blabels[i]->set_color(color);
2878                 mask_buttons[i]->update(i==k ? 1 : 0);
2879         }
2880 }
2881
2882 // typ=0, this mask, this point
2883 // typ>0, this mask, all points
2884 // typ<0, all masks, all points
2885 // dxy= on? pt[+1]-pt[-1] : dxy=0
2886 int CWindowMaskGUI::smooth_mask(int typ, int on)
2887 {
2888         MaskAutos *autos;
2889         MaskAuto *keyframe;
2890         Track *track;
2891         MaskPoint *point;
2892         SubMask *mask;
2893 #ifdef USE_KEYFRAME_SPANNING
2894         int create_it = 0;
2895 #else
2896         int create_it = 1;
2897 #endif
2898
2899         mwindow->undo->update_undo_before(_("mask smooth"), this);
2900
2901 // Get existing keyframe
2902         get_keyframe(track, autos, keyframe,
2903                         mask, point, create_it);
2904         if( track ) {
2905 #ifdef USE_KEYFRAME_SPANNING
2906                 MaskAuto temp_keyframe(mwindow->edl, autos);
2907                 temp_keyframe.copy_data(keyframe);
2908                 keyframe = &temp_keyframe;
2909 #endif
2910                 int k = mwindow->edl->session->cwindow_mask;
2911                 int n = typ>=0 ? k+1 : keyframe->masks.size();
2912                 for( int j=typ<0? 0 : k; j<n; ++j ) {
2913                         if( !mask_enables[j]->get_value() ) continue;
2914                         SubMask *sub_mask = keyframe->get_submask(j);
2915                         MaskPoints &points = sub_mask->points;
2916                         int psz = points.size();
2917                         if( psz < 3 ) continue;
2918                         int l = mwindow->cwindow->gui->affected_point;
2919                         if( l > psz ) l = psz;
2920                         int m = typ ? psz : l+1;
2921                         for( int i=typ ? 0 : l; i<m; ++i ) {
2922                                 int i0 = i-1, i1 = i+1;
2923                                 if( i0 < 0 ) i0 = psz-1;
2924                                 if( i1 >= psz ) i1 = 0;
2925                                 MaskPoint *p0 = points[i0];
2926                                 MaskPoint *p  = points[i];
2927                                 MaskPoint *p1 = points[i1];
2928                                 float dx = !on ? 0 : p1->x - p0->x;
2929                                 float dy = !on ? 0 : p1->y - p0->y;
2930                                 p->control_x1 = -dx/4;  p->control_y1 = -dy/4;
2931                                 p->control_x2 =  dx/4;  p->control_y2 =  dy/4;
2932                         }
2933                 }
2934 #ifdef USE_KEYFRAME_SPANNING
2935                 autos->update_parameter(keyframe);
2936 #endif
2937                 update_preview();
2938         }
2939
2940         mwindow->undo->update_undo_after(_("mask smooth"), LOAD_AUTOMATION);
2941         return 1;
2942 }
2943
2944 int CWindowMaskGUI::save_mask(const char *nm)
2945 {
2946         int k = mwindow->edl->session->cwindow_mask;
2947         MaskAutos *autos;
2948         MaskAuto *keyframe;
2949         Track *track;
2950         MaskPoint *point;
2951         SubMask *mask;
2952         get_keyframe(track, autos, keyframe, mask, point, 0);
2953         if( !track ) return 0;
2954         SubMask *sub_mask = keyframe->get_submask(k);
2955         ArrayList<SubMask *> masks;
2956         load_masks(masks);
2957         int i = masks.size();
2958         while( --i >= 0 ) {
2959                 if( strcmp(masks[i]->name, nm) ) continue;
2960                 masks.remove_object_number(i);
2961         }
2962         mask = new SubMask(0, -1);
2963         strncpy(mask->name, nm, sizeof(mask->name)-1);
2964         mask->copy_from(*sub_mask, 0);
2965         masks.append(mask);
2966         save_masks(masks);
2967         masks.remove_all_objects();
2968         return 1;
2969 }
2970
2971 int CWindowMaskGUI::del_mask(const char *nm)
2972 {
2973         ArrayList<SubMask *> masks;
2974         load_masks(masks);
2975         int i = masks.size();
2976         while( --i >= 0 ) {
2977                 if( strcmp(masks[i]->name, nm) ) continue;
2978                 masks.remove_object_number(i);
2979         }
2980         save_masks(masks);
2981         masks.remove_all_objects();
2982         return 1;
2983 }
2984
2985 int CWindowMaskGUI::center_mask()
2986 {
2987         int k = mwindow->edl->session->cwindow_mask;
2988         MaskAutos *autos;
2989         MaskAuto *keyframe;
2990         Track *track;
2991         MaskPoint *point;
2992         SubMask *mask;
2993 #ifdef USE_KEYFRAME_SPANNING
2994         int create_it = 0;
2995 #else
2996         int create_it = 1;
2997 #endif
2998         get_keyframe(track, autos, keyframe,
2999                         mask, point, create_it);
3000         if( !track ) return 0;
3001         mwindow->undo->update_undo_before(_("mask center"), this);
3002
3003 // Get existing keyframe
3004 #ifdef USE_KEYFRAME_SPANNING
3005         MaskAuto temp_keyframe(mwindow->edl, autos);
3006         temp_keyframe.copy_data(keyframe);
3007         keyframe = &temp_keyframe;
3008 #endif
3009         SubMask *sub_mask = keyframe->get_submask(k);
3010         MaskPoints &points = sub_mask->points;
3011         int psz = points.size();
3012         if( psz > 0 ) {
3013                 float cx = 0, cy = 0;
3014                 for( int i=0; i<psz; ++i ) {
3015                         MaskPoint *p  = points[i];
3016                         cx += p->x;  cy += p->y;
3017                 }
3018                 cx /= psz;  cy /= psz;
3019                 cx -= mwindow->edl->session->output_w / 2.f;
3020                 cy -= mwindow->edl->session->output_h / 2.f;
3021                 for( int i=0; i<psz; ++i ) {
3022                         MaskPoint *p  = points[i];
3023                         p->x -= cx;  p->y -= cy;
3024                 }
3025         }
3026 #ifdef USE_KEYFRAME_SPANNING
3027         autos->update_parameter(keyframe);
3028 #endif
3029         update_preview();
3030         mwindow->undo->update_undo_after(_("mask center"), LOAD_AUTOMATION);
3031         return 1;
3032 }
3033
3034 int CWindowMaskGUI::normal_mask()
3035 {
3036         int k = mwindow->edl->session->cwindow_mask;
3037         MaskAutos *autos;
3038         MaskAuto *keyframe;
3039         Track *track;
3040         MaskPoint *point;
3041         SubMask *mask;
3042 #ifdef USE_KEYFRAME_SPANNING
3043         int create_it = 0;
3044 #else
3045         int create_it = 1;
3046 #endif
3047 // Get existing keyframe
3048         get_keyframe(track, autos, keyframe,
3049                         mask, point, create_it);
3050         if( !track ) return 0;
3051         mwindow->undo->update_undo_before(_("mask normal"), this);
3052
3053 #ifdef USE_KEYFRAME_SPANNING
3054         MaskAuto temp_keyframe(mwindow->edl, autos);
3055         temp_keyframe.copy_data(keyframe);
3056         keyframe = &temp_keyframe;
3057 #endif
3058         SubMask *sub_mask = keyframe->get_submask(k);
3059         MaskPoints &points = sub_mask->points;
3060         int psz = points.size();
3061         float cx = 0, cy = 0;
3062         double dr = 0;
3063         if( psz > 0 ) {
3064                 for( int i=0; i<psz; ++i ) {
3065                         MaskPoint *p  = points[i];
3066                         cx += p->x;  cy += p->y;
3067                 }
3068                 cx /= psz;  cy /= psz;
3069                 for( int i=0; i<psz; ++i ) {
3070                         MaskPoint *p  = points[i];
3071                         float dx = fabsf(p->x-cx), dy = fabsf(p->y-cy);
3072                         double d = sqrt(dx*dx + dy*dy);
3073                         if( dr < d ) dr = d;
3074                 }
3075         }
3076         if( dr > 0 ) {
3077                 float out_w = mwindow->edl->session->output_w;
3078                 float out_h = mwindow->edl->session->output_h;
3079                 float r = bmax(out_w, out_h);
3080                 float s = r / (4 * dr * sqrt(2.));
3081                 for( int i=0; i<psz; ++i ) {
3082                         MaskPoint *p  = points[i];
3083                         float x = p->x, y = p->y;
3084                         p->x = (x-cx) * s + cx;
3085                         p->y = (y-cy) * s + cy;
3086                         p->control_x1 *= s;  p->control_y1 *= s;
3087                         p->control_x2 *= s;  p->control_y2 *= s;
3088                 }
3089         }
3090 #ifdef USE_KEYFRAME_SPANNING
3091         autos->update_parameter(keyframe);
3092 #endif
3093         update_preview();
3094
3095         mwindow->undo->update_undo_after(_("mask normal"), LOAD_AUTOMATION);
3096         return 1;
3097 }
3098
3099
3100 CWindowMaskLoadList::CWindowMaskLoadList(MWindow *mwindow, CWindowMaskGUI *gui)
3101  : BC_ListBox(-1, -1, 1, 1, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
3102 {
3103         this->mwindow = mwindow;
3104         this->gui = gui;
3105         set_use_button(0);
3106 }
3107
3108 CWindowMaskLoadList::~CWindowMaskLoadList()
3109 {
3110 }
3111
3112
3113 int CWindowMaskLoadList::handle_event()
3114 {
3115         MaskAutos *autos;
3116         MaskAuto *keyframe;
3117         Track *track;
3118         MaskPoint *point;
3119         SubMask *mask;
3120 #ifdef USE_KEYFRAME_SPANNING
3121         int create_it = 0;
3122 #else
3123         int create_it = 1;
3124 #endif
3125
3126         mwindow->undo->update_undo_before(_("mask shape"), this);
3127
3128 // Get existing keyframe
3129         gui->get_keyframe(track, autos, keyframe,
3130                         mask, point, create_it);
3131         CWindowMaskItem *item = (CWindowMaskItem *) get_selection(0, 0);
3132         if( track && item ) {
3133 #ifdef USE_KEYFRAME_SPANNING
3134                 MaskAuto temp_keyframe(mwindow->edl, autos);
3135                 temp_keyframe.copy_data(keyframe);
3136                 keyframe = &temp_keyframe;
3137                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
3138 #endif
3139                 ArrayList<SubMask *> masks;
3140                 gui->load_masks(masks);
3141                 mask->copy_from(*masks[item->id], 0);
3142                 masks.remove_all_objects();
3143 #ifdef USE_KEYFRAME_SPANNING
3144                 autos->update_parameter(keyframe);
3145 #endif
3146                 gui->update();
3147                 gui->update_preview(1);
3148         }
3149         mwindow->undo->update_undo_after(_("mask shape"), LOAD_AUTOMATION);
3150         return 1;
3151 }
3152
3153 void CWindowMaskLoadList::create_objects()
3154 {
3155         shape_items.remove_all_objects();
3156         ArrayList<SubMask *> masks;
3157         gui->load_masks(masks);
3158         for( int i=0; i<masks.size(); ++i )
3159                 shape_items.append(new CWindowMaskItem(masks[i]->name, i));
3160         masks.remove_all_objects();
3161         update(&shape_items, 0, 0, 1);
3162 }
3163
3164 CWindowMaskLoad::CWindowMaskLoad(MWindow *mwindow,
3165         CWindowMaskGUI *gui, int x, int y, int w)
3166  : BC_Button(x, y, mwindow->theme->get_image_set("mask_prst_load_images"))
3167 {
3168         this->mwindow = mwindow;
3169         this->gui = gui;
3170         set_tooltip(_("Load preset"));
3171 }
3172
3173 int CWindowMaskLoad::handle_event()
3174 {
3175         gui->mask_load_list->create_objects();
3176         int px, py;
3177         get_abs_cursor(px, py);
3178         return gui->mask_load_list->activate(px, py, 120,160);
3179 }
3180
3181
3182 CWindowMaskSave::CWindowMaskSave(MWindow *mwindow,
3183         CWindowMaskGUI *gui, int x, int y, int w)
3184  : BC_Button(x, y, mwindow->theme->get_image_set("mask_prst_save_images"))
3185 {
3186         this->mwindow = mwindow;
3187         this->gui = gui;
3188         set_tooltip(_("Save preset"));
3189 }
3190
3191 CWindowMaskSave::~CWindowMaskSave()
3192 {
3193 }
3194
3195 int CWindowMaskSave::handle_event()
3196 {
3197         Track *track;
3198         MaskAutos *autos;
3199         MaskAuto *keyframe;
3200         SubMask *mask;
3201         MaskPoint *point;
3202         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
3203         if( track ) {
3204                 int sx = 0, sy = 0;
3205                 gui->get_abs_cursor(sx, sy);
3206                 if( !gui->preset_dialog )
3207                         gui->preset_dialog = new CWindowMaskPresetDialog(mwindow, gui);
3208                 gui->preset_dialog->start_dialog(sx, sy, keyframe);
3209         }
3210         return 1;
3211 }
3212
3213 CWindowMaskPresetDialog::CWindowMaskPresetDialog(MWindow *mwindow, CWindowMaskGUI *gui)
3214  : BC_DialogThread()
3215 {
3216         this->mwindow = mwindow;
3217         this->gui = gui;
3218         pgui = 0;
3219 }
3220
3221 CWindowMaskPresetDialog::~CWindowMaskPresetDialog()
3222 {
3223         close_window();
3224 }
3225
3226 void CWindowMaskPresetDialog::handle_close_event(int result)
3227 {
3228         pgui = 0;
3229 }
3230
3231 void CWindowMaskPresetDialog::handle_done_event(int result)
3232 {
3233         if( result ) return;
3234         const char *nm = pgui->preset_text->get_text();
3235         if( keyframe )
3236                 gui->save_mask(nm);
3237         else
3238                 gui->del_mask(nm);
3239 }
3240
3241 BC_Window* CWindowMaskPresetDialog::new_gui()
3242 {
3243         pgui = new CWindowMaskPresetGUI(this, sx, sy,
3244                 keyframe ? _(PROGRAM_NAME ": Save Mask") :
3245                            _(PROGRAM_NAME ": Delete Mask"));
3246         pgui->create_objects();
3247         return pgui;
3248 }
3249
3250 void CWindowMaskPresetDialog::start_dialog(int sx, int sy, MaskAuto *keyframe)
3251 {
3252         close_window();
3253         this->sx = sx;  this->sy = sy;
3254         this->keyframe = keyframe;
3255         start();
3256 }
3257
3258 CWindowMaskPresetGUI::CWindowMaskPresetGUI(CWindowMaskPresetDialog *preset_dialog,
3259                         int x, int y, const char *title)
3260  : BC_Window(title, x, y, 320, 100, 320, 100, 0, 0, 1)
3261 {
3262         this->preset_dialog = preset_dialog;
3263 }
3264
3265 void CWindowMaskPresetGUI::create_objects()
3266 {
3267         int x = 10, y = 10, pad = 8;
3268         lock_window("CWindowMaskPresetGUI::create_objects");
3269         BC_Title *title;
3270         add_subwindow(title = new BC_Title(x, y,
3271                 preset_dialog->keyframe ? _("Save mask:") : _("Delete mask:")));
3272         int x1 = x + title->get_w() + pad;
3273         int x2 = get_w() - x - pad - x1 -
3274                 BC_WindowBase::get_resources()->listbox_button[0]->get_w();
3275         CWindowMaskGUI *gui = preset_dialog->gui;
3276         preset_text = new CWindowMaskPresetText(this,
3277                 x1, y, x2, 120, gui->mask_name->get_text());
3278         preset_text->create_objects();
3279         preset_text->set_tooltip(_("Mask name"));
3280         preset_text->update_items();
3281         add_subwindow(new BC_OKButton(this));
3282         add_subwindow(new BC_CancelButton(this));
3283         show_window();
3284         raise_window();
3285         unlock_window();
3286 }
3287
3288 CWindowMaskPresetText::CWindowMaskPresetText(CWindowMaskPresetGUI *pgui,
3289                 int x, int y, int w, int h, const char *text)
3290  : BC_PopupTextBox(pgui, 0, text, x, y, w, h)
3291 {
3292         this->pgui = pgui;
3293 }
3294
3295 int CWindowMaskPresetText::handle_event()
3296 {
3297         int k = get_number();
3298         if( k >= 0 && k<mask_items.size() )
3299                 update(mask_items[k]->get_text());
3300         return 1;
3301 }
3302
3303 void CWindowMaskPresetText::update_items()
3304 {
3305         mask_items.remove_all_objects();
3306         ArrayList<SubMask *> masks;
3307         pgui->preset_dialog->gui->load_masks(masks);
3308         for( int i=0; i<masks.size(); ++i ) {
3309                 char text[BCSTRLEN];  memset(text, 0, sizeof(text));
3310                 strncpy(text, masks[i]->name, sizeof(text)-1);
3311                 mask_items.append(new CWindowMaskItem(text));
3312         }
3313         masks.remove_all_objects();
3314         update_list(&mask_items);
3315 }
3316
3317
3318 CWindowMaskDelete::CWindowMaskDelete(MWindow *mwindow,
3319         CWindowMaskGUI *gui, int x, int y, int w)
3320  : BC_Button(x, y, mwindow->theme->get_image_set("mask_prst_trsh_images"))
3321 {
3322         this->mwindow = mwindow;
3323         this->gui = gui;
3324         set_tooltip(_("delete preset"));
3325 }
3326
3327 int CWindowMaskDelete::handle_event()
3328 {
3329         int sx = 0, sy = 0;
3330         gui->get_abs_cursor(sx, sy);
3331         if( !gui->preset_dialog )
3332                 gui->preset_dialog = new CWindowMaskPresetDialog(mwindow, gui);
3333         gui->preset_dialog->start_dialog(sx, sy, 0);
3334         return 1;
3335 }
3336
3337
3338 CWindowMaskCenter::CWindowMaskCenter(MWindow *mwindow,
3339         CWindowMaskGUI *gui, int x, int y, int w)
3340  : BC_Button(x, y, mwindow->theme->get_image_set("mask_pstn_cen_images"))
3341 {
3342         this->mwindow = mwindow;
3343         this->gui = gui;
3344         set_tooltip(_("center mask"));
3345 }
3346
3347 int CWindowMaskCenter::handle_event()
3348 {
3349         return gui->center_mask();
3350 }
3351
3352
3353 CWindowMaskNormal::CWindowMaskNormal(MWindow *mwindow,
3354         CWindowMaskGUI *gui, int x, int y, int w)
3355  : BC_Button(x, y, mwindow->theme->get_image_set("mask_pstn_nrm_images"))
3356 {
3357         this->mwindow = mwindow;
3358         this->gui = gui;
3359         set_tooltip(_("normalize mask"));
3360 }
3361
3362 int CWindowMaskNormal::handle_event()
3363 {
3364         return gui->normal_mask();
3365 }
3366
3367
3368 CWindowMaskShape::CWindowMaskShape(MWindow *mwindow, CWindowMaskGUI *gui,
3369                 const char *images, int shape, int x, int y, const char *tip)
3370  : BC_Button(x, y, mwindow->theme->get_image_set(images))
3371 {
3372         this->mwindow = mwindow;
3373         this->gui = gui;
3374         this->shape = shape;
3375         set_tooltip(tip);
3376 }
3377
3378 CWindowMaskShape::~CWindowMaskShape()
3379 {
3380 }
3381
3382 void CWindowMaskShape::builtin_shape(int i, SubMask *sub_mask)
3383 {
3384         int out_w = mwindow->edl->session->output_w;
3385         int out_h = mwindow->edl->session->output_h;
3386         float cx = out_w/2.f, cy = out_h/2.f;
3387         float r = bmax(cx, cy) / 4.f;
3388         double c = 4*(sqrt(2.)-1)/3; // bezier aprox circle
3389         float r2 = r / 2.f, rc = r*c, r4 = r / 4.f;
3390         MaskPoint *pt = 0;
3391         MaskPoints &points = sub_mask->points;
3392         points.remove_all_objects();
3393         switch( i ) {
3394         case MASK_SHAPE_SQUARE:
3395                 points.append(pt = new MaskPoint());
3396                 pt->x = cx - r;  pt->y = cy - r;
3397                 points.append(pt = new MaskPoint());
3398                 pt->x = cx + r;  pt->y = cy - r;
3399                 points.append(pt = new MaskPoint());
3400                 pt->x = cx + r;  pt->y = cy + r;
3401                 points.append(pt = new MaskPoint());
3402                 pt->x = cx - r;  pt->y = cy + r;
3403                 break;
3404         case MASK_SHAPE_CIRCLE:
3405                 points.append(pt = new MaskPoint());
3406                 pt->x = cx - r;  pt->y = cy - r;
3407                 pt->control_x1 = -rc;  pt->control_y1 =  rc;
3408                 pt->control_x2 =  rc;  pt->control_y2 = -rc;
3409                 points.append(pt = new MaskPoint());
3410                 pt->x = cx + r;  pt->y = cy - r;
3411                 pt->control_x1 = -rc;  pt->control_y1 = -rc;
3412                 pt->control_x2 =  rc;  pt->control_y2 =  rc;
3413                 points.append(pt = new MaskPoint());
3414                 pt->x = cx + r;  pt->y = cy + r;
3415                 pt->control_x1 =  rc;  pt->control_y1 = -rc;
3416                 pt->control_x2 = -rc;  pt->control_y2 =  rc;
3417                 points.append(pt = new MaskPoint());
3418                 pt->x = cx - r;  pt->y = cy + r;
3419                 pt->control_x1 =  rc;  pt->control_y1 =  rc;
3420                 pt->control_x2 = -rc;  pt->control_y2 = -rc;
3421                 break;
3422         case MASK_SHAPE_TRIANGLE:
3423                 points.append(pt = new MaskPoint());
3424                 pt->x = cx + 0;  pt->y = cy - r*(sqrt(3.)-1.);
3425                 points.append(pt = new MaskPoint());
3426                 pt->x = cx + r;  pt->y = cy + r;
3427                 points.append(pt = new MaskPoint());
3428                 pt->x = cx - r;  pt->y = cy + r;
3429                 break;
3430         case MASK_SHAPE_OVAL:
3431                 points.append(pt = new MaskPoint());
3432                 pt->x = cx - r;  pt->y = cy - r2;
3433                 pt->control_x1 = -r2;  pt->control_y1 =  r4;
3434                 pt->control_x2 =  r2;  pt->control_y2 = -r4;
3435                 points.append(pt = new MaskPoint());
3436                 pt->x = cx + r;  pt->y = cy - r2;
3437                 pt->control_x1 = -r2;  pt->control_y1 = -r4;
3438                 pt->control_x2 =  r2;  pt->control_y2 =  r4;
3439                 points.append(pt = new MaskPoint());
3440                 pt->x = cx + r;  pt->y = cy + r2;
3441                 pt->control_x1 =  r2;  pt->control_y1 = -r4;
3442                 pt->control_x2 = -r2;  pt->control_y2 =  r4;
3443                 points.append(pt = new MaskPoint());
3444                 pt->x = cx - r;  pt->y = cy + r2;
3445                 pt->control_x1 =  r2;  pt->control_y1 =  r4;
3446                 pt->control_x2 = -r2;  pt->control_y2 = -r4;
3447                 break;
3448         }
3449 }
3450
3451 int CWindowMaskShape::handle_event()
3452 {
3453         MaskAutos *autos;
3454         MaskAuto *keyframe;
3455         Track *track;
3456         MaskPoint *point;
3457         SubMask *mask;
3458 #ifdef USE_KEYFRAME_SPANNING
3459         int create_it = 0;
3460 #else
3461         int create_it = 1;
3462 #endif
3463
3464         mwindow->undo->update_undo_before(_("mask shape"), this);
3465
3466 // Get existing keyframe
3467         gui->get_keyframe(track, autos, keyframe,
3468                         mask, point, create_it);
3469         if( track ) {
3470 #ifdef USE_KEYFRAME_SPANNING
3471                 MaskAuto temp_keyframe(mwindow->edl, autos);
3472                 temp_keyframe.copy_data(keyframe);
3473                 keyframe = &temp_keyframe;
3474                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
3475 #endif
3476                 if( mask ) {
3477                         builtin_shape(shape, mask);
3478 #ifdef USE_KEYFRAME_SPANNING
3479                         autos->update_parameter(keyframe);
3480 #endif
3481                         gui->update();
3482                         gui->update_preview(1);
3483                 }
3484         }
3485         mwindow->undo->update_undo_after(_("mask shape"), LOAD_AUTOMATION);
3486         return 1;
3487 }
3488
3489 void CWindowMaskGUI::load_masks(ArrayList<SubMask *> &masks)
3490 {
3491         char path[BCTEXTLEN];
3492         sprintf(path, "%s/%s", File::get_config_path(), MASKS_FILE);
3493         FileSystem fs;
3494         fs.complete_path(path);
3495         FileXML file;
3496         file.read_from_file(path, 1);
3497
3498         masks.remove_all_objects();
3499         int result;
3500         while( !(result = file.read_tag()) ) {
3501                 if( file.tag.title_is("MASK") ) {
3502                         SubMask *sub_mask = new SubMask(0, -1);
3503                         char name[BCTEXTLEN];  name[0] = 0;
3504                         file.tag.get_property("NAME", name);
3505                         strncpy(sub_mask->name, name, sizeof(sub_mask->name));
3506                         sub_mask->load(&file);
3507                         masks.append(sub_mask);
3508                 }
3509         }
3510 }
3511
3512 void CWindowMaskGUI::save_masks(ArrayList<SubMask *> &masks)
3513 {
3514         FileXML file;
3515         for( int i=0; i<masks.size(); ++i ) {
3516                 SubMask *sub_mask = masks[i];
3517                 sub_mask->copy(&file);
3518         }
3519         file.terminate_string();
3520
3521         char path[BCTEXTLEN];
3522         sprintf(path, "%s/%s", File::get_config_path(), MASKS_FILE);
3523         FileSystem fs;
3524         fs.complete_path(path);
3525         file.write_to_file(path);
3526 }
3527
3528
3529 CWindowRulerGUI::CWindowRulerGUI(MWindow *mwindow, CWindowTool *thread)
3530  : CWindowToolGUI(mwindow, thread, _(PROGRAM_NAME ": Ruler"), 320, 240)
3531 {
3532 }
3533
3534 CWindowRulerGUI::~CWindowRulerGUI()
3535 {
3536 }
3537
3538 void CWindowRulerGUI::create_objects()
3539 {
3540         int x = 10, y = 10, x1 = 100;
3541         BC_Title *title;
3542
3543         lock_window("CWindowRulerGUI::create_objects");
3544         add_subwindow(title = new BC_Title(x, y, _("Current:")));
3545         add_subwindow(current = new BC_TextBox(x1, y, 200, 1, ""));
3546         y += title->get_h() + 5;
3547         add_subwindow(title = new BC_Title(x, y, _("Point 1:")));
3548         add_subwindow(point1 = new BC_TextBox(x1, y, 200, 1, ""));
3549         y += title->get_h() + 5;
3550         add_subwindow(title = new BC_Title(x, y, _("Point 2:")));
3551         add_subwindow(point2 = new BC_TextBox(x1, y, 200, 1, ""));
3552         y += title->get_h() + 5;
3553         add_subwindow(title = new BC_Title(x, y, _("Deltas:")));
3554         add_subwindow(deltas = new BC_TextBox(x1, y, 200, 1, ""));
3555         y += title->get_h() + 5;
3556         add_subwindow(title = new BC_Title(x, y, _("Distance:")));
3557         add_subwindow(distance = new BC_TextBox(x1, y, 200, 1, ""));
3558         y += title->get_h() + 5;
3559         add_subwindow(title = new BC_Title(x, y, _("Angle:")));
3560         add_subwindow(angle = new BC_TextBox(x1, y, 200, 1, ""));
3561         y += title->get_h() + 10;
3562         char string[BCTEXTLEN];
3563         sprintf(string,
3564                  _("Press Ctrl to lock ruler to the\nnearest 45%c%c angle."),
3565                 0xc2, 0xb0); // degrees utf
3566         add_subwindow(title = new BC_Title(x,
3567                 y,
3568                 string));
3569         y += title->get_h() + 10;
3570         sprintf(string, _("Press Alt to translate the ruler."));
3571         add_subwindow(title = new BC_Title(x,
3572                 y,
3573                 string));
3574         update();
3575         unlock_window();
3576 }
3577
3578 void CWindowRulerGUI::update()
3579 {
3580         char string[BCTEXTLEN];
3581         int cx = mwindow->session->cwindow_output_x;
3582         int cy = mwindow->session->cwindow_output_y;
3583         sprintf(string, "%d, %d", cx, cy);
3584         current->update(string);
3585         double x1 = mwindow->edl->session->ruler_x1;
3586         double y1 = mwindow->edl->session->ruler_y1;
3587         sprintf(string, "%.0f, %.0f", x1, y1);
3588         point1->update(string);
3589         double x2 = mwindow->edl->session->ruler_x2;
3590         double y2 = mwindow->edl->session->ruler_y2;
3591         sprintf(string, "%.0f, %.0f", x2, y2);
3592         point2->update(string);
3593         double dx = x2 - x1, dy = y2 - y1;
3594         sprintf(string, "%s%.0f, %s%.0f", (dx>=0? "+":""), dx, (dy>=0? "+":""), dy);
3595         deltas->update(string);
3596         double d = sqrt(dx*dx + dy*dy);
3597         sprintf(string, _("%0.01f pixels"), d);
3598         distance->update(string);
3599         double a = d > 0 ? (atan2(-dy, dx) * 180/M_PI) : 0.;
3600         sprintf(string, "%0.02f %c%c", a, 0xc2, 0xb0);
3601         angle->update(string);
3602 }
3603
3604 void CWindowRulerGUI::handle_event()
3605 {
3606 }
3607
3608
3609
3610