add unclear btn icons, make both bksp and ctrl-m mute edit selection
[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, 70, 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, 70, 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)
1753  : BC_Button(x, y, mwindow->theme->get_image_set("unclear_button"))
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, 70)
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, t[SUBMASKS];
2522         int clr_w = CWindowMaskClrMask::calculate_w(mwindow);
2523         int clr_x = get_w()-x - clr_w;
2524
2525         lock_window("CWindowMaskGUI::create_objects");
2526         BC_TitleBar *title_bar;
2527         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks on Track")));
2528         y += title_bar->get_h() + margin;
2529         BC_Title *title;
2530         add_subwindow(title = new BC_Title(x,y, _("Track:")));
2531         int x1 = x + 90, ww = clr_x-2*margin - x1;
2532         for( int i=0,n=sizeof(t)/sizeof(t[0]); i<n; ++i ) t[i] = x1+(i*ww)/n;
2533         int del_x = t[5];
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         add_subwindow(mask_solo_track = new CWindowMaskSoloTrack(mwindow, this, del_x, y, 0));
2544         y += mask_on_track->get_h() + margin;
2545         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Masks")));
2546         y += title_bar->get_h() + margin;
2547         add_subwindow(title = new BC_Title(x, y, _("Mask:")));
2548         mask_name = new CWindowMaskName(mwindow, this, x1, y, "");
2549         mask_name->create_objects();
2550         mask_name->set_tooltip(_("Mask name"));
2551         add_subwindow(mask_clr = new CWindowMaskClrMask(mwindow, this, clr_x, y));
2552         add_subwindow(mask_del = new CWindowMaskDelMask(mwindow, this, del_x, y));
2553         y += mask_name->get_h() + 2*margin;
2554         BC_Bar *bar;
2555 //      add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2556 //      y += bar->get_h() + 2*margin;
2557
2558         add_subwindow(title = new BC_Title(x, y, _("Select:")));
2559         int bw = 0, bh = 0;
2560         BC_CheckBox::calculate_extents(this, &bw, &bh);
2561         for( int i=0; i<SUBMASKS; ++i ) {
2562                 int v = i == mwindow->edl->session->cwindow_mask ? 1 : 0;
2563                 mask_buttons[i] = new CWindowMaskButton(mwindow, this, t[i], y, i, v);
2564                 add_subwindow(mask_buttons[i]);
2565         }
2566         add_subwindow(mask_thumbler = new CWindowMaskThumbler(mwindow, this, clr_x, y));
2567         y += bh + margin;
2568         for( int i=0; i<SUBMASKS; ++i ) {
2569                 char text[BCSTRLEN];  sprintf(text, "%d", i);
2570                 int tx = (bw - get_text_width(MEDIUMFONT, text)) / 2;
2571                 mask_blabels[i] = new BC_Title(t[i]+tx, y, text);
2572                 add_subwindow(mask_blabels[i]);
2573         }
2574         y += mask_blabels[0]->get_h() + margin;
2575         add_subwindow(title = new BC_Title(x, y, _("Enable:")));
2576         for( int i=0; i<SUBMASKS; ++i ) {
2577                 mask_enables[i] = new CWindowMaskEnable(mwindow, this, t[i], y, i, 1);
2578                 add_subwindow(mask_enables[i]);
2579         }
2580         add_subwindow(mask_unclr = new CWindowMaskUnclear(mwindow, this, clr_x, y));
2581         y += mask_enables[0]->get_h() + 2*margin;
2582         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Presets shapes")));
2583         y += title_bar->get_h() + margin;
2584         add_subwindow(mask_shape_sqr = new CWindowMaskShape(mwindow, this,
2585                 "mask_prst_sqr_images", MASK_SHAPE_SQUARE, t[0], y, _("Square")));
2586         add_subwindow(mask_shape_crc = new CWindowMaskShape(mwindow, this,
2587                 "mask_prst_crc_images", MASK_SHAPE_CIRCLE, t[1], y, _("Circle")));
2588         add_subwindow(mask_shape_tri = new CWindowMaskShape(mwindow, this,
2589                 "mask_prst_tri_images", MASK_SHAPE_TRIANGLE, t[2], y, _("Triangle")));
2590         add_subwindow(mask_shape_ovl = new CWindowMaskShape(mwindow, this,
2591                 "mask_prst_ovl_images", MASK_SHAPE_OVAL, t[3], y, _("Oval")));
2592         add_subwindow(mask_load_list = new CWindowMaskLoadList(mwindow, this));
2593         add_subwindow(mask_load = new CWindowMaskLoad(mwindow, this, t[5], y, 80));
2594         add_subwindow(mask_save = new CWindowMaskSave(mwindow, this, t[6], y, 80));
2595         add_subwindow(mask_delete = new CWindowMaskDelete(mwindow, this, t[7], y, 80));
2596         y += mask_load->get_h() + 2*margin;
2597         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Position & Scale")));
2598         y += title_bar->get_h() + 2*margin;
2599         add_subwindow(mask_center = new CWindowMaskCenter(mwindow, this, t[0], y, 80));
2600         add_subwindow(mask_normal = new CWindowMaskNormal(mwindow, this, t[1], y, 80));
2601
2602         add_subwindow(mask_scale_x = new CWindowMaskScaleXY(mwindow, this,
2603                 t[5], y, theme->get_image_set("mask_scale_x"), 0, 0, _("xlate/scale x")));
2604         add_subwindow(mask_scale_y = new CWindowMaskScaleXY(mwindow, this,
2605                 t[6], y, theme->get_image_set("mask_scale_y"), 0, 1, _("xlate/scale y")));
2606         add_subwindow(mask_scale_xy = new CWindowMaskScaleXY(mwindow, this,
2607                 t[7], y, theme->get_image_set("mask_scale_xy"), 1, 2, _("xlate/scale xy")));
2608         y += mask_center->get_h() + 2*margin;
2609         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Fade & Feather")));
2610         y += title_bar->get_h() + 2*margin;
2611
2612         add_subwindow(title = new BC_Title(x, y, _("Fade:")));
2613         fade = new CWindowMaskFade(mwindow, this, x1, y);
2614         fade->create_objects();
2615         x2 = x1 + fade->get_w() + 2*margin;
2616         int w2 = clr_x-2*margin - x2;
2617         add_subwindow(fade_slider = new CWindowMaskFadeSlider(mwindow, this, x2, y, w2));
2618         add_subwindow(gang_fader = new CWindowMaskGangFader(mwindow, this, clr_x, y));
2619         y += fade->get_h() + margin;
2620         add_subwindow(title = new BC_Title(x, y, _("Feather:")));
2621         feather = new CWindowMaskFeather(mwindow, this, x1, y);
2622         feather->create_objects();
2623         w2 = clr_x - 2*margin - x2;
2624         feather_slider = new CWindowMaskFeatherSlider(mwindow, this, x2, y, w2, 0);
2625         add_subwindow(feather_slider);
2626         add_subwindow(gang_feather = new CWindowMaskGangFeather(mwindow, this, clr_x, y));
2627         y += feather->get_h() + 2*margin;
2628         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Mask Points")));
2629         y += title_bar->get_h() + margin;
2630
2631         add_subwindow(title = new BC_Title(x, y, _("Point:")));
2632         active_point = new CWindowMaskAffectedPoint(mwindow, this, t[0], y);
2633         active_point->create_objects();
2634 // typ=0, this mask, this point
2635         add_subwindow(mask_pnt_linear = new CWindowMaskSmoothButton(mwindow, this,
2636                 _("linear point"), 0, 0, t[3], y, "mask_pnt_linear_images"));
2637         add_subwindow(mask_pnt_smooth = new CWindowMaskSmoothButton(mwindow, this,
2638                 _("smooth point"), 0, 1, t[4], y, "mask_pnt_smooth_images"));
2639         add_subwindow(del_point = new CWindowMaskDelPoint(mwindow, this, del_x, y));
2640         y += active_point->get_h() + margin;
2641         add_subwindow(title = new BC_Title(x, y, "X:"));
2642         this->x = new CWindowCoord(this, t[0], y, (float)0.0);
2643         this->x->create_objects();
2644 // typ>0, this mask, all points
2645         add_subwindow(mask_crv_linear = new CWindowMaskSmoothButton(mwindow, this,
2646                 _("linear curve"), 1, 0, t[3], y, "mask_crv_linear_images"));
2647         add_subwindow(mask_crv_smooth = new CWindowMaskSmoothButton(mwindow, this,
2648                 _("smooth curve"), 1, 1, t[4], y, "mask_crv_smooth_images"));
2649         add_subwindow(draw_markers = new CWindowMaskDrawMarkers(mwindow, this, del_x, y));
2650         y += this->x->get_h() + margin;
2651         add_subwindow(title = new BC_Title(x, y, "Y:"));
2652         this->y = new CWindowCoord(this, x1, y, (float)0.0);
2653         this->y->create_objects();
2654 // typ<0, all masks, all points
2655         add_subwindow(mask_all_linear = new CWindowMaskSmoothButton(mwindow, this,
2656                 _("linear all"), -1, 0, t[3], y, "mask_all_linear_images"));
2657         add_subwindow(mask_all_smooth = new CWindowMaskSmoothButton(mwindow, this,
2658                 _("smooth all"), -1, 1, t[4], y, "mask_all_smooth_images"));
2659         add_subwindow(draw_boundary = new CWindowMaskDrawBoundary(mwindow, this, del_x, y));
2660         y += this->y->get_h() + 2*margin;
2661         add_subwindow(title_bar = new BC_TitleBar(x, y, get_w()-2*x, 20, 10, _("Pivot Point")));
2662         y += title_bar->get_h() + margin;
2663
2664         add_subwindow(title = new BC_Title(x, y, "X:"));
2665         float cx = mwindow->edl->session->output_w / 2.f;
2666         focus_x = new CWindowCoord(this, x1, y, cx);
2667         focus_x->create_objects();
2668         add_subwindow(focus = new CWindowMaskFocus(mwindow, this, del_x, y));
2669         add_subwindow(gang_focus = new CWindowMaskGangFocus(mwindow, this, clr_x, y));
2670         y += focus_x->get_h() + margin;
2671         add_subwindow(title = new BC_Title(x, y, "Y:"));
2672         float cy = mwindow->edl->session->output_h / 2.f;
2673         focus_y = new CWindowCoord(this, x1, y, cy);
2674         focus_y->create_objects();
2675         y += focus_y->get_h() + 2*margin;
2676         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2677         y += bar->get_h() + margin;
2678         add_subwindow(this->apply_before_plugins = new CWindowMaskBeforePlugins(this, 10, y));
2679         y += this->apply_before_plugins->get_h();
2680         add_subwindow(this->disable_opengl_masking = new CWindowDisableOpenGLMasking(this, 10, y));
2681         add_subwindow(help = new CWindowMaskHelp(mwindow, this, del_x, y));
2682         y += this->disable_opengl_masking->get_h() + 2*margin;
2683         help_y = y;
2684         add_subwindow(bar = new BC_Bar(x, y, get_w()-2*x));
2685         y += bar->get_h() + 2*margin;
2686         add_subwindow(title = new BC_Title(x, y, _(
2687                 "Shift+LMB: move an end point\n"
2688                 "Ctrl+LMB: move a control point\n"
2689                 "Alt+LMB: to drag translate the mask\n"
2690                 "Shift+Key Delete: to delete the point\n"
2691                 "Shift+MMB: Set Pivot Point at pointer\n"
2692                 "Wheel: rotate around Pivot Point\n"
2693                 "Shift+Wheel: scale around Pivot Point\n"
2694                 "Ctrl+Wheel: rotate/scale around pointer")));
2695         help_h = y + title->get_h() + 2*margin;
2696         update();
2697         resize_window(get_w(), help_y);
2698         unlock_window();
2699 }
2700
2701 int CWindowMaskGUI::close_event()
2702 {
2703         done_event();
2704         return CWindowToolGUI::close_event();
2705 }
2706
2707 void CWindowMaskGUI::done_event()
2708 {
2709         if( mwindow->in_destructor ) return;
2710         int &solo_track_id = mwindow->edl->local_session->solo_track_id;
2711         if( solo_track_id >= 0 ) {
2712                 solo_track_id = -1;
2713                 update_preview();
2714         }
2715 }
2716
2717 void CWindowMaskGUI::get_keyframe(Track* &track,
2718                 MaskAutos* &autos, MaskAuto* &keyframe,
2719                 SubMask* &mask, MaskPoint* &point, int create_it)
2720 {
2721         autos = 0;
2722         keyframe = 0;
2723
2724         track = mwindow->cwindow->calculate_mask_track();
2725         if( !track )
2726                 track = mwindow->cwindow->calculate_affected_track();
2727                 
2728         if(track) {
2729                 autos = (MaskAutos*)track->automation->autos[AUTOMATION_MASK];
2730                 keyframe = (MaskAuto*)mwindow->cwindow->calculate_affected_auto(
2731                         autos,
2732                         create_it);
2733         }
2734
2735         mask = !keyframe ? 0 :
2736                 keyframe->get_submask(mwindow->edl->session->cwindow_mask);
2737
2738         point = 0;
2739         if( keyframe ) {
2740                 if( mwindow->cwindow->gui->affected_point < mask->points.total &&
2741                         mwindow->cwindow->gui->affected_point >= 0 ) {
2742                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2743                 }
2744         }
2745 }
2746
2747 void CWindowMaskGUI::update()
2748 {
2749         Track *track;
2750         MaskAutos *autos;
2751         MaskAuto *keyframe;
2752         SubMask *mask;
2753         MaskPoint *point;
2754 //printf("CWindowMaskGUI::update 1\n");
2755         get_keyframe(track, autos, keyframe, mask, point, 0);
2756         mwindow->cwindow->mask_track_id = track ? track->get_id() : -1;
2757         mask_on_track->set_back_color(!track || track->record ?
2758                 get_resources()->text_background :
2759                 get_resources()->text_background_disarmed);
2760         mask_on_track->update_items();
2761         mask_on_track->update(!track ? "" : track->title);
2762         mask_name->update_items(keyframe);
2763         const char *text = "";
2764         int sz = !keyframe ? 0 : keyframe->masks.size();
2765         int k = mwindow->edl->session->cwindow_mask;
2766         if( k >= 0 && k < sz )
2767                 text = keyframe->masks[k]->name;
2768         else
2769                 k = mwindow->edl->session->cwindow_mask = 0;
2770         mask_name->update(text);
2771         update_buttons(keyframe, k);
2772         if( point ) {
2773                 x->update(point->x);
2774                 y->update(point->y);
2775         }
2776         if( track ) {
2777                 double position = mwindow->edl->local_session->get_selectionstart(1);
2778                 int64_t position_i = track->to_units(position, 0);
2779                 feather->update(autos->get_feather(position_i, k, PLAY_FORWARD));
2780                 fade->update(autos->get_fader(position_i, k, PLAY_FORWARD));
2781                 int show_mask = track->masks;
2782                 for( int i=0; i<SUBMASKS; ++i )
2783                         mask_enables[i]->update((show_mask>>i) & 1);
2784         }
2785         if( keyframe ) {
2786                 apply_before_plugins->update(keyframe->apply_before_plugins);
2787                 disable_opengl_masking->update(keyframe->disable_opengl_masking);
2788         }
2789         active_point->update((int64_t)mwindow->cwindow->gui->affected_point);
2790 }
2791
2792 void CWindowMaskGUI::handle_event()
2793 {
2794         Track *track;
2795         MaskAuto *keyframe;
2796         MaskAutos *autos;
2797         SubMask *mask;
2798         MaskPoint *point;
2799         get_keyframe(track, autos, keyframe, mask, point, 0);
2800
2801         mwindow->undo->update_undo_before(_("mask point"), this);
2802
2803         if(point)
2804         {
2805 #ifdef USE_KEYFRAME_SPANNING
2806 // Create temp keyframe
2807                 MaskAuto temp_keyframe(mwindow->edl, autos);
2808                 temp_keyframe.copy_data(keyframe);
2809 // Get affected point in temp keyframe
2810                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
2811                 if(mwindow->cwindow->gui->affected_point < mask->points.total &&
2812                         mwindow->cwindow->gui->affected_point >= 0)
2813                 {
2814                         point = mask->points.values[mwindow->cwindow->gui->affected_point];
2815                 }
2816
2817                 if(point)
2818                 {
2819                         point->x = atof(x->get_text());
2820                         point->y = atof(y->get_text());
2821 // Commit to spanned keyframes
2822                         autos->update_parameter(&temp_keyframe);
2823                 }
2824 #else
2825                 point->x = atof(x->get_text());
2826                 point->y = atof(y->get_text());
2827 #endif
2828         }
2829
2830         update_preview();
2831         mwindow->undo->update_undo_after(_("mask point"), LOAD_AUTOMATION);
2832 }
2833
2834 void CWindowMaskGUI::set_focused(int v, float cx, float cy)
2835 {
2836         CWindowGUI *cgui = mwindow->cwindow->gui;
2837         cgui->unlock_window();
2838         lock_window("CWindowMaskGUI::set_focused");
2839         if( focused != v )
2840                 focus->update(focused = v);
2841         focus_x->update(cx);
2842         focus_y->update(cy);
2843         unlock_window();
2844         cgui->lock_window("CWindowCanvas::set_focused");
2845 }
2846
2847 void CWindowMaskGUI::update_buttons(MaskAuto *keyframe, int k)
2848 {
2849         int text_color = get_resources()->default_text_color;
2850         int high_color = get_resources()->button_highlighted;
2851         for( int i=0; i<SUBMASKS; ++i ) {
2852                 int color = text_color;
2853                 if( keyframe ) {
2854                         SubMask *submask = keyframe->get_submask(i);
2855                         if( submask && submask->points.size() )
2856                                 color = high_color;
2857                 }
2858                 mask_blabels[i]->set_color(color);
2859                 mask_buttons[i]->update(i==k ? 1 : 0);
2860         }
2861 }
2862
2863 // typ=0, this mask, this point
2864 // typ>0, this mask, all points
2865 // typ<0, all masks, all points
2866 // dxy= on? pt[+1]-pt[-1] : dxy=0
2867 int CWindowMaskGUI::smooth_mask(int typ, int on)
2868 {
2869         MaskAutos *autos;
2870         MaskAuto *keyframe;
2871         Track *track;
2872         MaskPoint *point;
2873         SubMask *mask;
2874 #ifdef USE_KEYFRAME_SPANNING
2875         int create_it = 0;
2876 #else
2877         int create_it = 1;
2878 #endif
2879
2880         mwindow->undo->update_undo_before(_("mask smooth"), this);
2881
2882 // Get existing keyframe
2883         get_keyframe(track, autos, keyframe,
2884                         mask, point, create_it);
2885         if( track ) {
2886 #ifdef USE_KEYFRAME_SPANNING
2887                 MaskAuto temp_keyframe(mwindow->edl, autos);
2888                 temp_keyframe.copy_data(keyframe);
2889                 keyframe = &temp_keyframe;
2890 #endif
2891                 int k = mwindow->edl->session->cwindow_mask;
2892                 int n = typ>=0 ? k+1 : keyframe->masks.size();
2893                 for( int j=typ<0? 0 : k; j<n; ++j ) {
2894                         if( !mask_enables[j]->get_value() ) continue;
2895                         SubMask *sub_mask = keyframe->get_submask(j);
2896                         MaskPoints &points = sub_mask->points;
2897                         int psz = points.size();
2898                         if( psz < 3 ) continue;
2899                         int l = mwindow->cwindow->gui->affected_point;
2900                         if( l > psz ) l = psz;
2901                         int m = typ ? psz : l+1;
2902                         for( int i=typ ? 0 : l; i<m; ++i ) {
2903                                 int i0 = i-1, i1 = i+1;
2904                                 if( i0 < 0 ) i0 = psz-1;
2905                                 if( i1 >= psz ) i1 = 0;
2906                                 MaskPoint *p0 = points[i0];
2907                                 MaskPoint *p  = points[i];
2908                                 MaskPoint *p1 = points[i1];
2909                                 float dx = !on ? 0 : p1->x - p0->x;
2910                                 float dy = !on ? 0 : p1->y - p0->y;
2911                                 p->control_x1 = -dx/4;  p->control_y1 = -dy/4;
2912                                 p->control_x2 =  dx/4;  p->control_y2 =  dy/4;
2913                         }
2914                 }
2915 #ifdef USE_KEYFRAME_SPANNING
2916                 autos->update_parameter(keyframe);
2917 #endif
2918                 update_preview();
2919         }
2920
2921         mwindow->undo->update_undo_after(_("mask smooth"), LOAD_AUTOMATION);
2922         return 1;
2923 }
2924
2925 int CWindowMaskGUI::save_mask(const char *nm)
2926 {
2927         int k = mwindow->edl->session->cwindow_mask;
2928         MaskAutos *autos;
2929         MaskAuto *keyframe;
2930         Track *track;
2931         MaskPoint *point;
2932         SubMask *mask;
2933         get_keyframe(track, autos, keyframe, mask, point, 0);
2934         if( !track ) return 0;
2935         SubMask *sub_mask = keyframe->get_submask(k);
2936         ArrayList<SubMask *> masks;
2937         load_masks(masks);
2938         int i = masks.size();
2939         while( --i >= 0 ) {
2940                 if( strcmp(masks[i]->name, nm) ) continue;
2941                 masks.remove_object_number(i);
2942         }
2943         mask = new SubMask(0, -1);
2944         strncpy(mask->name, nm, sizeof(mask->name)-1);
2945         mask->copy_from(*sub_mask, 0);
2946         masks.append(mask);
2947         save_masks(masks);
2948         masks.remove_all_objects();
2949         return 1;
2950 }
2951
2952 int CWindowMaskGUI::del_mask(const char *nm)
2953 {
2954         ArrayList<SubMask *> masks;
2955         load_masks(masks);
2956         int i = masks.size();
2957         while( --i >= 0 ) {
2958                 if( strcmp(masks[i]->name, nm) ) continue;
2959                 masks.remove_object_number(i);
2960         }
2961         save_masks(masks);
2962         masks.remove_all_objects();
2963         return 1;
2964 }
2965
2966 int CWindowMaskGUI::center_mask()
2967 {
2968         int k = mwindow->edl->session->cwindow_mask;
2969         MaskAutos *autos;
2970         MaskAuto *keyframe;
2971         Track *track;
2972         MaskPoint *point;
2973         SubMask *mask;
2974 #ifdef USE_KEYFRAME_SPANNING
2975         int create_it = 0;
2976 #else
2977         int create_it = 1;
2978 #endif
2979         get_keyframe(track, autos, keyframe,
2980                         mask, point, create_it);
2981         if( !track ) return 0;
2982         mwindow->undo->update_undo_before(_("mask center"), this);
2983
2984 // Get existing keyframe
2985 #ifdef USE_KEYFRAME_SPANNING
2986         MaskAuto temp_keyframe(mwindow->edl, autos);
2987         temp_keyframe.copy_data(keyframe);
2988         keyframe = &temp_keyframe;
2989 #endif
2990         SubMask *sub_mask = keyframe->get_submask(k);
2991         MaskPoints &points = sub_mask->points;
2992         int psz = points.size();
2993         if( psz > 0 ) {
2994                 float cx = 0, cy = 0;
2995                 for( int i=0; i<psz; ++i ) {
2996                         MaskPoint *p  = points[i];
2997                         cx += p->x;  cy += p->y;
2998                 }
2999                 cx /= psz;  cy /= psz;
3000                 cx -= mwindow->edl->session->output_w / 2.f;
3001                 cy -= mwindow->edl->session->output_h / 2.f;
3002                 for( int i=0; i<psz; ++i ) {
3003                         MaskPoint *p  = points[i];
3004                         p->x -= cx;  p->y -= cy;
3005                 }
3006         }
3007 #ifdef USE_KEYFRAME_SPANNING
3008         autos->update_parameter(keyframe);
3009 #endif
3010         update_preview();
3011         mwindow->undo->update_undo_after(_("mask center"), LOAD_AUTOMATION);
3012         return 1;
3013 }
3014
3015 int CWindowMaskGUI::normal_mask()
3016 {
3017         int k = mwindow->edl->session->cwindow_mask;
3018         MaskAutos *autos;
3019         MaskAuto *keyframe;
3020         Track *track;
3021         MaskPoint *point;
3022         SubMask *mask;
3023 #ifdef USE_KEYFRAME_SPANNING
3024         int create_it = 0;
3025 #else
3026         int create_it = 1;
3027 #endif
3028 // Get existing keyframe
3029         get_keyframe(track, autos, keyframe,
3030                         mask, point, create_it);
3031         if( !track ) return 0;
3032         mwindow->undo->update_undo_before(_("mask normal"), this);
3033
3034 #ifdef USE_KEYFRAME_SPANNING
3035         MaskAuto temp_keyframe(mwindow->edl, autos);
3036         temp_keyframe.copy_data(keyframe);
3037         keyframe = &temp_keyframe;
3038 #endif
3039         SubMask *sub_mask = keyframe->get_submask(k);
3040         MaskPoints &points = sub_mask->points;
3041         int psz = points.size();
3042         float cx = 0, cy = 0;
3043         double dr = 0;
3044         if( psz > 0 ) {
3045                 for( int i=0; i<psz; ++i ) {
3046                         MaskPoint *p  = points[i];
3047                         cx += p->x;  cy += p->y;
3048                 }
3049                 cx /= psz;  cy /= psz;
3050                 for( int i=0; i<psz; ++i ) {
3051                         MaskPoint *p  = points[i];
3052                         float dx = fabsf(p->x-cx), dy = fabsf(p->y-cy);
3053                         double d = sqrt(dx*dx + dy*dy);
3054                         if( dr < d ) dr = d;
3055                 }
3056         }
3057         if( dr > 0 ) {
3058                 float out_w = mwindow->edl->session->output_w;
3059                 float out_h = mwindow->edl->session->output_h;
3060                 float r = bmax(out_w, out_h);
3061                 float s = r / (4 * dr * sqrt(2.));
3062                 for( int i=0; i<psz; ++i ) {
3063                         MaskPoint *p  = points[i];
3064                         float x = p->x, y = p->y;
3065                         p->x = (x-cx) * s + cx;
3066                         p->y = (y-cy) * s + cy;
3067                         p->control_x1 *= s;  p->control_y1 *= s;
3068                         p->control_x2 *= s;  p->control_y2 *= s;
3069                 }
3070         }
3071 #ifdef USE_KEYFRAME_SPANNING
3072         autos->update_parameter(keyframe);
3073 #endif
3074         update_preview();
3075
3076         mwindow->undo->update_undo_after(_("mask normal"), LOAD_AUTOMATION);
3077         return 1;
3078 }
3079
3080
3081 CWindowMaskLoadList::CWindowMaskLoadList(MWindow *mwindow, CWindowMaskGUI *gui)
3082  : BC_ListBox(-1, -1, 1, 1, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
3083 {
3084         this->mwindow = mwindow;
3085         this->gui = gui;
3086         set_use_button(0);
3087 }
3088
3089 CWindowMaskLoadList::~CWindowMaskLoadList()
3090 {
3091 }
3092
3093
3094 int CWindowMaskLoadList::handle_event()
3095 {
3096         MaskAutos *autos;
3097         MaskAuto *keyframe;
3098         Track *track;
3099         MaskPoint *point;
3100         SubMask *mask;
3101 #ifdef USE_KEYFRAME_SPANNING
3102         int create_it = 0;
3103 #else
3104         int create_it = 1;
3105 #endif
3106
3107         mwindow->undo->update_undo_before(_("mask shape"), this);
3108
3109 // Get existing keyframe
3110         gui->get_keyframe(track, autos, keyframe,
3111                         mask, point, create_it);
3112         CWindowMaskItem *item = (CWindowMaskItem *) get_selection(0, 0);
3113         if( track && item ) {
3114 #ifdef USE_KEYFRAME_SPANNING
3115                 MaskAuto temp_keyframe(mwindow->edl, autos);
3116                 temp_keyframe.copy_data(keyframe);
3117                 keyframe = &temp_keyframe;
3118                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
3119 #endif
3120                 ArrayList<SubMask *> masks;
3121                 gui->load_masks(masks);
3122                 mask->copy_from(*masks[item->id], 0);
3123                 masks.remove_all_objects();
3124 #ifdef USE_KEYFRAME_SPANNING
3125                 autos->update_parameter(keyframe);
3126 #endif
3127                 gui->update();
3128                 gui->update_preview(1);
3129         }
3130         mwindow->undo->update_undo_after(_("mask shape"), LOAD_AUTOMATION);
3131         return 1;
3132 }
3133
3134 void CWindowMaskLoadList::create_objects()
3135 {
3136         shape_items.remove_all_objects();
3137         ArrayList<SubMask *> masks;
3138         gui->load_masks(masks);
3139         for( int i=0; i<masks.size(); ++i )
3140                 shape_items.append(new CWindowMaskItem(masks[i]->name, i));
3141         masks.remove_all_objects();
3142         update(&shape_items, 0, 0, 1);
3143 }
3144
3145 CWindowMaskLoad::CWindowMaskLoad(MWindow *mwindow,
3146         CWindowMaskGUI *gui, int x, int y, int w)
3147  : BC_Button(x, y, mwindow->theme->get_image_set("mask_prst_load_images"))
3148 {
3149         this->mwindow = mwindow;
3150         this->gui = gui;
3151         set_tooltip(_("Load preset"));
3152 }
3153
3154 int CWindowMaskLoad::handle_event()
3155 {
3156         gui->mask_load_list->create_objects();
3157         int px, py;
3158         get_abs_cursor(px, py);
3159         return gui->mask_load_list->activate(px, py, 120,160);
3160 }
3161
3162
3163 CWindowMaskSave::CWindowMaskSave(MWindow *mwindow,
3164         CWindowMaskGUI *gui, int x, int y, int w)
3165  : BC_Button(x, y, mwindow->theme->get_image_set("mask_prst_save_images"))
3166 {
3167         this->mwindow = mwindow;
3168         this->gui = gui;
3169         set_tooltip(_("Save preset"));
3170 }
3171
3172 CWindowMaskSave::~CWindowMaskSave()
3173 {
3174 }
3175
3176 int CWindowMaskSave::handle_event()
3177 {
3178         Track *track;
3179         MaskAutos *autos;
3180         MaskAuto *keyframe;
3181         SubMask *mask;
3182         MaskPoint *point;
3183         gui->get_keyframe(track, autos, keyframe, mask, point, 0);
3184         if( track ) {
3185                 int sx = 0, sy = 0;
3186                 gui->get_abs_cursor(sx, sy);
3187                 if( !gui->preset_dialog )
3188                         gui->preset_dialog = new CWindowMaskPresetDialog(mwindow, gui);
3189                 gui->preset_dialog->start_dialog(sx, sy, keyframe);
3190         }
3191         return 1;
3192 }
3193
3194 CWindowMaskPresetDialog::CWindowMaskPresetDialog(MWindow *mwindow, CWindowMaskGUI *gui)
3195  : BC_DialogThread()
3196 {
3197         this->mwindow = mwindow;
3198         this->gui = gui;
3199         pgui = 0;
3200 }
3201
3202 CWindowMaskPresetDialog::~CWindowMaskPresetDialog()
3203 {
3204         close_window();
3205 }
3206
3207 void CWindowMaskPresetDialog::handle_close_event(int result)
3208 {
3209         pgui = 0;
3210 }
3211
3212 void CWindowMaskPresetDialog::handle_done_event(int result)
3213 {
3214         if( result ) return;
3215         const char *nm = pgui->preset_text->get_text();
3216         if( keyframe )
3217                 gui->save_mask(nm);
3218         else
3219                 gui->del_mask(nm);
3220 }
3221
3222 BC_Window* CWindowMaskPresetDialog::new_gui()
3223 {
3224         pgui = new CWindowMaskPresetGUI(this, sx, sy,
3225                 keyframe ? _(PROGRAM_NAME ": Save Mask") :
3226                            _(PROGRAM_NAME ": Delete Mask"));
3227         pgui->create_objects();
3228         return pgui;
3229 }
3230
3231 void CWindowMaskPresetDialog::start_dialog(int sx, int sy, MaskAuto *keyframe)
3232 {
3233         close_window();
3234         this->sx = sx;  this->sy = sy;
3235         this->keyframe = keyframe;
3236         start();
3237 }
3238
3239 CWindowMaskPresetGUI::CWindowMaskPresetGUI(CWindowMaskPresetDialog *preset_dialog,
3240                         int x, int y, const char *title)
3241  : BC_Window(title, x, y, 320, 100, 320, 100, 0, 0, 1)
3242 {
3243         this->preset_dialog = preset_dialog;
3244 }
3245
3246 void CWindowMaskPresetGUI::create_objects()
3247 {
3248         int x = 10, y = 10, pad = 8;
3249         lock_window("CWindowMaskPresetGUI::create_objects");
3250         BC_Title *title;
3251         add_subwindow(title = new BC_Title(x, y,
3252                 preset_dialog->keyframe ? _("Save mask:") : _("Delete mask:")));
3253         int x1 = x + title->get_w() + pad;
3254         int x2 = get_w() - x - pad - x1 -
3255                 BC_WindowBase::get_resources()->listbox_button[0]->get_w();
3256         CWindowMaskGUI *gui = preset_dialog->gui;
3257         preset_text = new CWindowMaskPresetText(this,
3258                 x1, y, x2, 120, gui->mask_name->get_text());
3259         preset_text->create_objects();
3260         preset_text->set_tooltip(_("Mask name"));
3261         preset_text->update_items();
3262         add_subwindow(new BC_OKButton(this));
3263         add_subwindow(new BC_CancelButton(this));
3264         show_window();
3265         raise_window();
3266         unlock_window();
3267 }
3268
3269 CWindowMaskPresetText::CWindowMaskPresetText(CWindowMaskPresetGUI *pgui,
3270                 int x, int y, int w, int h, const char *text)
3271  : BC_PopupTextBox(pgui, 0, text, x, y, w, h)
3272 {
3273         this->pgui = pgui;
3274 }
3275
3276 int CWindowMaskPresetText::handle_event()
3277 {
3278         int k = get_number();
3279         if( k >= 0 && k<mask_items.size() )
3280                 update(mask_items[k]->get_text());
3281         return 1;
3282 }
3283
3284 void CWindowMaskPresetText::update_items()
3285 {
3286         mask_items.remove_all_objects();
3287         ArrayList<SubMask *> masks;
3288         pgui->preset_dialog->gui->load_masks(masks);
3289         for( int i=0; i<masks.size(); ++i ) {
3290                 char text[BCSTRLEN];  memset(text, 0, sizeof(text));
3291                 strncpy(text, masks[i]->name, sizeof(text)-1);
3292                 mask_items.append(new CWindowMaskItem(text));
3293         }
3294         masks.remove_all_objects();
3295         update_list(&mask_items);
3296 }
3297
3298
3299 CWindowMaskDelete::CWindowMaskDelete(MWindow *mwindow,
3300         CWindowMaskGUI *gui, int x, int y, int w)
3301  : BC_Button(x, y, mwindow->theme->get_image_set("mask_prst_trsh_images"))
3302 {
3303         this->mwindow = mwindow;
3304         this->gui = gui;
3305         set_tooltip(_("delete preset"));
3306 }
3307
3308 int CWindowMaskDelete::handle_event()
3309 {
3310         int sx = 0, sy = 0;
3311         gui->get_abs_cursor(sx, sy);
3312         if( !gui->preset_dialog )
3313                 gui->preset_dialog = new CWindowMaskPresetDialog(mwindow, gui);
3314         gui->preset_dialog->start_dialog(sx, sy, 0);
3315         return 1;
3316 }
3317
3318
3319 CWindowMaskCenter::CWindowMaskCenter(MWindow *mwindow,
3320         CWindowMaskGUI *gui, int x, int y, int w)
3321  : BC_Button(x, y, mwindow->theme->get_image_set("mask_pstn_cen_images"))
3322 {
3323         this->mwindow = mwindow;
3324         this->gui = gui;
3325         set_tooltip(_("center mask"));
3326 }
3327
3328 int CWindowMaskCenter::handle_event()
3329 {
3330         return gui->center_mask();
3331 }
3332
3333
3334 CWindowMaskNormal::CWindowMaskNormal(MWindow *mwindow,
3335         CWindowMaskGUI *gui, int x, int y, int w)
3336  : BC_Button(x, y, mwindow->theme->get_image_set("mask_pstn_nrm_images"))
3337 {
3338         this->mwindow = mwindow;
3339         this->gui = gui;
3340         set_tooltip(_("normalize mask"));
3341 }
3342
3343 int CWindowMaskNormal::handle_event()
3344 {
3345         return gui->normal_mask();
3346 }
3347
3348
3349 CWindowMaskShape::CWindowMaskShape(MWindow *mwindow, CWindowMaskGUI *gui,
3350                 const char *images, int shape, int x, int y, const char *tip)
3351  : BC_Button(x, y, mwindow->theme->get_image_set(images))
3352 {
3353         this->mwindow = mwindow;
3354         this->gui = gui;
3355         this->shape = shape;
3356         set_tooltip(tip);
3357 }
3358
3359 CWindowMaskShape::~CWindowMaskShape()
3360 {
3361 }
3362
3363 void CWindowMaskShape::builtin_shape(int i, SubMask *sub_mask)
3364 {
3365         int out_w = mwindow->edl->session->output_w;
3366         int out_h = mwindow->edl->session->output_h;
3367         float cx = out_w/2.f, cy = out_h/2.f;
3368         float r = bmax(cx, cy) / 4.f;
3369         double c = 4*(sqrt(2.)-1)/3; // bezier aprox circle
3370         float r2 = r / 2.f, rc = r*c, r4 = r / 4.f;
3371         MaskPoint *pt = 0;
3372         MaskPoints &points = sub_mask->points;
3373         points.remove_all_objects();
3374         switch( i ) {
3375         case MASK_SHAPE_SQUARE:
3376                 points.append(pt = new MaskPoint());
3377                 pt->x = cx - r;  pt->y = cy - r;
3378                 points.append(pt = new MaskPoint());
3379                 pt->x = cx + r;  pt->y = cy - r;
3380                 points.append(pt = new MaskPoint());
3381                 pt->x = cx + r;  pt->y = cy + r;
3382                 points.append(pt = new MaskPoint());
3383                 pt->x = cx - r;  pt->y = cy + r;
3384                 break;
3385         case MASK_SHAPE_CIRCLE:
3386                 points.append(pt = new MaskPoint());
3387                 pt->x = cx - r;  pt->y = cy - r;
3388                 pt->control_x1 = -rc;  pt->control_y1 =  rc;
3389                 pt->control_x2 =  rc;  pt->control_y2 = -rc;
3390                 points.append(pt = new MaskPoint());
3391                 pt->x = cx + r;  pt->y = cy - r;
3392                 pt->control_x1 = -rc;  pt->control_y1 = -rc;
3393                 pt->control_x2 =  rc;  pt->control_y2 =  rc;
3394                 points.append(pt = new MaskPoint());
3395                 pt->x = cx + r;  pt->y = cy + r;
3396                 pt->control_x1 =  rc;  pt->control_y1 = -rc;
3397                 pt->control_x2 = -rc;  pt->control_y2 =  rc;
3398                 points.append(pt = new MaskPoint());
3399                 pt->x = cx - r;  pt->y = cy + r;
3400                 pt->control_x1 =  rc;  pt->control_y1 =  rc;
3401                 pt->control_x2 = -rc;  pt->control_y2 = -rc;
3402                 break;
3403         case MASK_SHAPE_TRIANGLE:
3404                 points.append(pt = new MaskPoint());
3405                 pt->x = cx + 0;  pt->y = cy - r*(sqrt(3.)-1.);
3406                 points.append(pt = new MaskPoint());
3407                 pt->x = cx + r;  pt->y = cy + r;
3408                 points.append(pt = new MaskPoint());
3409                 pt->x = cx - r;  pt->y = cy + r;
3410                 break;
3411         case MASK_SHAPE_OVAL:
3412                 points.append(pt = new MaskPoint());
3413                 pt->x = cx - r;  pt->y = cy - r2;
3414                 pt->control_x1 = -r2;  pt->control_y1 =  r4;
3415                 pt->control_x2 =  r2;  pt->control_y2 = -r4;
3416                 points.append(pt = new MaskPoint());
3417                 pt->x = cx + r;  pt->y = cy - r2;
3418                 pt->control_x1 = -r2;  pt->control_y1 = -r4;
3419                 pt->control_x2 =  r2;  pt->control_y2 =  r4;
3420                 points.append(pt = new MaskPoint());
3421                 pt->x = cx + r;  pt->y = cy + r2;
3422                 pt->control_x1 =  r2;  pt->control_y1 = -r4;
3423                 pt->control_x2 = -r2;  pt->control_y2 =  r4;
3424                 points.append(pt = new MaskPoint());
3425                 pt->x = cx - r;  pt->y = cy + r2;
3426                 pt->control_x1 =  r2;  pt->control_y1 =  r4;
3427                 pt->control_x2 = -r2;  pt->control_y2 = -r4;
3428                 break;
3429         }
3430 }
3431
3432 int CWindowMaskShape::handle_event()
3433 {
3434         MaskAutos *autos;
3435         MaskAuto *keyframe;
3436         Track *track;
3437         MaskPoint *point;
3438         SubMask *mask;
3439 #ifdef USE_KEYFRAME_SPANNING
3440         int create_it = 0;
3441 #else
3442         int create_it = 1;
3443 #endif
3444
3445         mwindow->undo->update_undo_before(_("mask shape"), this);
3446
3447 // Get existing keyframe
3448         gui->get_keyframe(track, autos, keyframe,
3449                         mask, point, create_it);
3450         if( track ) {
3451 #ifdef USE_KEYFRAME_SPANNING
3452                 MaskAuto temp_keyframe(mwindow->edl, autos);
3453                 temp_keyframe.copy_data(keyframe);
3454                 keyframe = &temp_keyframe;
3455                 mask = temp_keyframe.get_submask(mwindow->edl->session->cwindow_mask);
3456 #endif
3457                 if( mask ) {
3458                         builtin_shape(shape, mask);
3459 #ifdef USE_KEYFRAME_SPANNING
3460                         autos->update_parameter(keyframe);
3461 #endif
3462                         gui->update();
3463                         gui->update_preview(1);
3464                 }
3465         }
3466         mwindow->undo->update_undo_after(_("mask shape"), LOAD_AUTOMATION);
3467         return 1;
3468 }
3469
3470 void CWindowMaskGUI::load_masks(ArrayList<SubMask *> &masks)
3471 {
3472         char path[BCTEXTLEN];
3473         sprintf(path, "%s/%s", File::get_config_path(), MASKS_FILE);
3474         FileSystem fs;
3475         fs.complete_path(path);
3476         FileXML file;
3477         file.read_from_file(path, 1);
3478
3479         masks.remove_all_objects();
3480         int result;
3481         while( !(result = file.read_tag()) ) {
3482                 if( file.tag.title_is("MASK") ) {
3483                         SubMask *sub_mask = new SubMask(0, -1);
3484                         char name[BCTEXTLEN];  name[0] = 0;
3485                         file.tag.get_property("NAME", name);
3486                         strncpy(sub_mask->name, name, sizeof(sub_mask->name));
3487                         sub_mask->load(&file);
3488                         masks.append(sub_mask);
3489                 }
3490         }
3491 }
3492
3493 void CWindowMaskGUI::save_masks(ArrayList<SubMask *> &masks)
3494 {
3495         FileXML file;
3496         for( int i=0; i<masks.size(); ++i ) {
3497                 SubMask *sub_mask = masks[i];
3498                 sub_mask->copy(&file);
3499         }
3500         file.terminate_string();
3501
3502         char path[BCTEXTLEN];
3503         sprintf(path, "%s/%s", File::get_config_path(), MASKS_FILE);
3504         FileSystem fs;
3505         fs.complete_path(path);
3506         file.write_to_file(path);
3507 }
3508
3509
3510 CWindowRulerGUI::CWindowRulerGUI(MWindow *mwindow, CWindowTool *thread)
3511  : CWindowToolGUI(mwindow, thread, _(PROGRAM_NAME ": Ruler"), 320, 240)
3512 {
3513 }
3514
3515 CWindowRulerGUI::~CWindowRulerGUI()
3516 {
3517 }
3518
3519 void CWindowRulerGUI::create_objects()
3520 {
3521         int x = 10, y = 10, x1 = 100;
3522         BC_Title *title;
3523
3524         lock_window("CWindowRulerGUI::create_objects");
3525         add_subwindow(title = new BC_Title(x, y, _("Current:")));
3526         add_subwindow(current = new BC_TextBox(x1, y, 200, 1, ""));
3527         y += title->get_h() + 5;
3528         add_subwindow(title = new BC_Title(x, y, _("Point 1:")));
3529         add_subwindow(point1 = new BC_TextBox(x1, y, 200, 1, ""));
3530         y += title->get_h() + 5;
3531         add_subwindow(title = new BC_Title(x, y, _("Point 2:")));
3532         add_subwindow(point2 = new BC_TextBox(x1, y, 200, 1, ""));
3533         y += title->get_h() + 5;
3534         add_subwindow(title = new BC_Title(x, y, _("Deltas:")));
3535         add_subwindow(deltas = new BC_TextBox(x1, y, 200, 1, ""));
3536         y += title->get_h() + 5;
3537         add_subwindow(title = new BC_Title(x, y, _("Distance:")));
3538         add_subwindow(distance = new BC_TextBox(x1, y, 200, 1, ""));
3539         y += title->get_h() + 5;
3540         add_subwindow(title = new BC_Title(x, y, _("Angle:")));
3541         add_subwindow(angle = new BC_TextBox(x1, y, 200, 1, ""));
3542         y += title->get_h() + 10;
3543         char string[BCTEXTLEN];
3544         sprintf(string,
3545                  _("Press Ctrl to lock ruler to the\nnearest 45%c%c angle."),
3546                 0xc2, 0xb0); // degrees utf
3547         add_subwindow(title = new BC_Title(x,
3548                 y,
3549                 string));
3550         y += title->get_h() + 10;
3551         sprintf(string, _("Press Alt to translate the ruler."));
3552         add_subwindow(title = new BC_Title(x,
3553                 y,
3554                 string));
3555         update();
3556         unlock_window();
3557 }
3558
3559 void CWindowRulerGUI::update()
3560 {
3561         char string[BCTEXTLEN];
3562         int cx = mwindow->session->cwindow_output_x;
3563         int cy = mwindow->session->cwindow_output_y;
3564         sprintf(string, "%d, %d", cx, cy);
3565         current->update(string);
3566         double x1 = mwindow->edl->session->ruler_x1;
3567         double y1 = mwindow->edl->session->ruler_y1;
3568         sprintf(string, "%.0f, %.0f", x1, y1);
3569         point1->update(string);
3570         double x2 = mwindow->edl->session->ruler_x2;
3571         double y2 = mwindow->edl->session->ruler_y2;
3572         sprintf(string, "%.0f, %.0f", x2, y2);
3573         point2->update(string);
3574         double dx = x2 - x1, dy = y2 - y1;
3575         sprintf(string, "%s%.0f, %s%.0f", (dx>=0? "+":""), dx, (dy>=0? "+":""), dy);
3576         deltas->update(string);
3577         double d = sqrt(dx*dx + dy*dy);
3578         sprintf(string, _("%0.01f pixels"), d);
3579         distance->update(string);
3580         double a = d > 0 ? (atan2(-dy, dx) * 180/M_PI) : 0.;
3581         sprintf(string, "%0.02f %c%c", a, 0xc2, 0xb0);
3582         angle->update(string);
3583 }
3584
3585 void CWindowRulerGUI::handle_event()
3586 {
3587 }
3588
3589
3590
3591