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