09a09cb83c288f812b2e0fed3dcefa8adea46b25
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / gwindowgui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "autoconf.h"
23 #include "bchash.h"
24 #include "bcsignals.h"
25 #include "clip.h"
26 #include "condition.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "gwindowgui.h"
30 #include "keys.h"
31 #include "language.h"
32 #include "localsession.h"
33 #include "mainmenu.h"
34 #include "mainsession.h"
35 #include "mwindow.h"
36 #include "mwindowgui.h"
37 #include "theme.h"
38 #include "tracks.h"
39 #include "trackcanvas.h"
40 #include "zoombar.h"
41
42 #include <math.h>
43
44
45
46 GWindowGUI::GWindowGUI(MWindow *mwindow, int w, int h)
47  : BC_Window(_(PROGRAM_NAME ": Overlays"),
48         mwindow->session->gwindow_x, mwindow->session->gwindow_y,
49         w, h, w, h, 0, 0, 1)
50 {
51         this->mwindow = mwindow;
52         camera_xyz = 0;
53         projector_xyz = 0;
54 }
55
56 GWindowGUI::~GWindowGUI()
57 {
58 }
59
60 const char *GWindowGUI::non_auto_text[NON_AUTOMATION_TOTAL] =
61 {
62         N_("Assets"),
63         N_("Titles"),
64         N_("Transitions"),
65         N_("Plugin Keyframes"),
66         N_("Hard Edges"),
67 };
68
69 const char *GWindowGUI::auto_text[AUTOMATION_TOTAL] =
70 {
71         N_("Mute"),
72         N_("Camera X"),
73         N_("Camera Y"),
74         N_("Camera Z"),
75         N_("Projector X"),
76         N_("Projector Y"),
77         N_("Projector Z"),
78         N_("Fade"),
79         N_("Pan"),
80         N_("Mode"),
81         N_("Mask"),
82         N_("Speed")
83 };
84
85 int GWindowGUI::auto_colors[AUTOMATION_TOTAL] =
86 {
87         PINK,
88         RED,
89         GREEN,
90         BLUE,
91         LTPINK,
92         LTGREEN,
93         LTBLUE,
94         LTPURPLE,
95         0,
96         0,
97         0,
98         ORANGE,
99 };
100
101 void GWindowGUI::load_defaults()
102 {
103         BC_Hash *defaults = mwindow->defaults;
104         auto_colors[AUTOMATION_MUTE] = defaults->get("AUTO_COLOR_MUTE", auto_colors[AUTOMATION_MUTE]);
105         auto_colors[AUTOMATION_CAMERA_X] = defaults->get("AUTO_COLOR_CAMERA_X", auto_colors[AUTOMATION_CAMERA_X]);
106         auto_colors[AUTOMATION_CAMERA_Y] = defaults->get("AUTO_COLOR_CAMERA_Y", auto_colors[AUTOMATION_CAMERA_Y]);
107         auto_colors[AUTOMATION_CAMERA_Z] = defaults->get("AUTO_COLOR_CAMERA_Z", auto_colors[AUTOMATION_CAMERA_Z]);
108         auto_colors[AUTOMATION_PROJECTOR_X] = defaults->get("AUTO_COLOR_PROJECTOR_X", auto_colors[AUTOMATION_PROJECTOR_X]);
109         auto_colors[AUTOMATION_PROJECTOR_Y] = defaults->get("AUTO_COLOR_PROJECTOR_Y", auto_colors[AUTOMATION_PROJECTOR_Y]);
110         auto_colors[AUTOMATION_PROJECTOR_Z] = defaults->get("AUTO_COLOR_PROJECTOR_Z", auto_colors[AUTOMATION_PROJECTOR_Z]);
111         auto_colors[AUTOMATION_FADE] = defaults->get("AUTO_COLOR_FADE", auto_colors[AUTOMATION_FADE]);
112         auto_colors[AUTOMATION_SPEED] = defaults->get("AUTO_COLOR_SPEED", auto_colors[AUTOMATION_SPEED]);
113 }
114
115 void GWindowGUI::save_defaults()
116 {
117         BC_Hash *defaults = mwindow->defaults;
118         defaults->update("AUTO_COLOR_MUTE", auto_colors[AUTOMATION_MUTE]);
119         defaults->update("AUTO_COLOR_CAMERA_X", auto_colors[AUTOMATION_CAMERA_X]);
120         defaults->update("AUTO_COLOR_CAMERA_Y", auto_colors[AUTOMATION_CAMERA_Y]);
121         defaults->update("AUTO_COLOR_CAMERA_Z", auto_colors[AUTOMATION_CAMERA_Z]);
122         defaults->update("AUTO_COLOR_PROJECTOR_X", auto_colors[AUTOMATION_PROJECTOR_X]);
123         defaults->update("AUTO_COLOR_PROJECTOR_Y", auto_colors[AUTOMATION_PROJECTOR_Y]);
124         defaults->update("AUTO_COLOR_PROJECTOR_Z", auto_colors[AUTOMATION_PROJECTOR_Z]);
125         defaults->update("AUTO_COLOR_FADE", auto_colors[AUTOMATION_FADE]);
126         defaults->update("AUTO_COLOR_SPEED", auto_colors[AUTOMATION_SPEED]);
127 }
128
129 static toggleinfo toggle_order[] =
130 {
131         {0, NON_AUTOMATION_ASSETS},
132         {0, NON_AUTOMATION_TITLES},
133         {0, NON_AUTOMATION_TRANSITIONS},
134         {0, NON_AUTOMATION_PLUGIN_AUTOS},
135         {0, -1}, // bar
136         {1, AUTOMATION_FADE},
137         {1, AUTOMATION_MUTE},
138         {1, AUTOMATION_SPEED},
139         {1, AUTOMATION_MODE},
140         {1, AUTOMATION_PAN},
141         {1, AUTOMATION_MASK},
142         {-1, NON_AUTOMATION_HARD_EDGES},
143         {0, -1}, // bar
144         {1, AUTOMATION_CAMERA_X},
145         {1, AUTOMATION_CAMERA_Y},
146         {1, AUTOMATION_CAMERA_Z},
147         {-1, NONAUTOTOGGLES_CAMERA_XYZ},
148         {0, -1}, // bar
149         {1, AUTOMATION_PROJECTOR_X},
150         {1, AUTOMATION_PROJECTOR_Y},
151         {1, AUTOMATION_PROJECTOR_Z},
152         {-1, NONAUTOTOGGLES_PROJECTOR_XYZ},
153 };
154
155 const char *GWindowGUI::toggle_text(toggleinfo *tp)
156 {
157         if( tp->isauto > 0 ) return _(auto_text[tp->ref]);
158         if( !tp->isauto ) return _(non_auto_text[tp->ref]);
159         switch( tp->ref ) {
160         case NONAUTOTOGGLES_CAMERA_XYZ:
161         case NONAUTOTOGGLES_PROJECTOR_XYZ:
162                 return _("XYZ");
163         case NON_AUTOMATION_HARD_EDGES:
164                 return _("Hard Edges");
165         }
166         return "()";
167 }
168
169 void GWindowGUI::calculate_extents(BC_WindowBase *gui, int *w, int *h)
170 {
171         int temp1, temp2, temp3, temp4, temp5, temp6, temp7;
172         int current_w, current_h;
173         *w = 10;
174         *h = 10;
175
176         for( int i=0; i<(int)(sizeof(toggle_order)/sizeof(toggle_order[0])); ++i ) {
177                 toggleinfo *tp = &toggle_order[i];
178                 int ref = tp->ref;
179                 if( ref < 0 ) {
180                         *h += get_resources()->bar_data->get_h() + 5;
181                         continue;
182                 }
183                 BC_Toggle::calculate_extents(gui,
184                         BC_WindowBase::get_resources()->checkbox_images,
185                         0, &temp1, &current_w, &current_h,
186                         &temp2, &temp3, &temp4, &temp5, &temp6, &temp7,
187                         toggle_text(tp), MEDIUMFONT);
188                 current_w += current_h;
189                 *w = MAX(current_w, *w);
190                 *h += current_h + 5;
191         }
192
193         *h += 10;
194         *w += 20;
195 }
196
197 GWindowColorButton::GWindowColorButton(GWindowToggle *auto_toggle,
198                 int x, int y, int w, int color)
199  : ColorCircleButton(auto_toggle->caption, x, y, w, w, color, -1, 1)
200 {
201         this->auto_toggle = auto_toggle;
202         this->color = color;
203 }
204
205 GWindowColorButton::~GWindowColorButton()
206 {
207 }
208
209 int GWindowColorButton::handle_new_color(int color, int alpha)
210 {
211         this->color = color;
212         return 1;
213 }
214
215 void GWindowColorButton::handle_done_event(int result)
216 {
217         ColorCircleButton::handle_done_event(result);
218         int ref = auto_toggle->info->ref;
219         GWindowGUI *gui = auto_toggle->gui;
220         gui->lock_window("GWindowColorThread::handle_done_event");
221         if( !result ) {
222                 GWindowGUI::auto_colors[ref] = color;
223                 auto_toggle->update_gui(color);
224                 gui->save_defaults();
225         }
226         else {
227                 color = GWindowGUI::auto_colors[ref];
228                 update_gui(color);
229         }
230         gui->unlock_window();
231         MWindowGUI *mwindow_gui = gui->mwindow->gui;
232         mwindow_gui->lock_window("GWindowColorUpdate::run");
233         mwindow_gui->draw_overlays(1);
234         mwindow_gui->unlock_window();
235 }
236
237
238 void GWindowGUI::create_objects()
239 {
240         int x = 10, y = 10;
241         lock_window("GWindowGUI::create_objects");
242
243         for( int i=0; i<(int)(sizeof(toggle_order)/sizeof(toggle_order[0])); ++i ) {
244                 toggleinfo *tp = &toggle_order[i];
245                 int ref = tp->ref;
246                 if( ref < 0 ) {
247                         BC_Bar *bar = new BC_Bar(x,y,get_w()-x-10);
248                         add_tool(bar);
249                         toggles[i] = 0;
250                         y += bar->get_h() + 5;
251                         continue;
252                 }
253                 const char *label = toggle_text(tp);
254                 int color = tp->isauto > 0 ? auto_colors[tp->ref] : WHITE;
255                 GWindowToggle *toggle = new GWindowToggle(this, x, y, label, color, tp);
256                 add_tool(toggles[i] = toggle);
257                 if( tp->isauto > 0 ) {
258                         VFrame *vframe = 0;
259                         switch( ref ) {
260                         case AUTOMATION_MODE: vframe = mwindow->theme->modekeyframe_data;  break;
261                         case AUTOMATION_PAN:  vframe = mwindow->theme->pankeyframe_data;   break;
262                         case AUTOMATION_MASK: vframe = mwindow->theme->maskkeyframe_data;  break;
263                         }
264                         if( !vframe ) {
265                                 int wh = toggle->get_h() - 4;
266                                 GWindowColorButton *color_button =
267                                         new GWindowColorButton(toggle, get_w()-wh-10, y+2, wh, color);
268                                 add_tool(color_button);
269                                 color_button->create_objects();
270                         }
271                         else
272                                 draw_vframe(vframe, get_w()-vframe->get_w()-10, y);
273                 }
274                 else {
275                         const char *accel = 0;
276                         switch( ref ) {
277                         case NONAUTOTOGGLES_CAMERA_XYZ:
278                                 camera_xyz = toggle;
279                                 accel = _("Shift-F1");
280                                 break;
281                         case NONAUTOTOGGLES_PROJECTOR_XYZ:
282                                 projector_xyz = toggle;
283                                 accel = _("Shift-F2");
284                                 break;
285                         case NON_AUTOMATION_HARD_EDGES:
286                                 VFrame *vframe = mwindow->theme->hardedge_data;
287                                 draw_vframe(vframe, get_w()-vframe->get_w()-10, y);
288                                 hard_edges = toggle;
289                                 break;
290                         }
291                          if( accel ) {
292                                 int x1 = get_w() - BC_Title::calculate_w(this, accel) - 10;
293                                 add_subwindow(new BC_Title(x1, y, accel));
294                         }
295                 }
296                 y += toggles[i]->get_h() + 5;
297         }
298         update_toggles(0);
299         unlock_window();
300 }
301
302 void GWindowGUI::update_mwindow(int toggles, int overlays)
303 {
304         unlock_window();
305         mwindow->gui->lock_window("GWindowGUI::update_mwindow");
306         if( toggles )
307                 mwindow->gui->mainmenu->update_toggles(0);
308         if( overlays )
309                 mwindow->gui->draw_overlays(1);
310         mwindow->gui->unlock_window();
311         lock_window("GWindowGUI::update_mwindow");
312 }
313
314 void GWindowGUI::update_toggles(int use_lock)
315 {
316         if(use_lock) {
317                 lock_window("GWindowGUI::update_toggles");
318                 set_cool(0);
319         }
320
321         for( int i=0; i<(int)(sizeof(toggle_order)/sizeof(toggle_order[0])); ++i ) {
322                 if( toggles[i] ) toggles[i]->update();
323         }
324
325         camera_xyz->set_value(check_xyz(AUTOMATION_CAMERA_X) > 0 ? 1 : 0);
326         projector_xyz->set_value(check_xyz(AUTOMATION_PROJECTOR_X) > 0 ? 1 : 0);
327
328         if(use_lock) unlock_window();
329 }
330
331 void GWindowGUI::toggle_camera_xyz()
332 {
333         int v = camera_xyz->get_value() ? 0 : 1;
334         camera_xyz->set_value(v);
335         xyz_check(AUTOMATION_CAMERA_X, v);
336         update_toggles(0);
337         update_mwindow(1, 1);
338 }
339
340 void GWindowGUI::toggle_projector_xyz()
341 {
342         int v = projector_xyz->get_value() ? 0 : 1;
343         projector_xyz->set_value(v);
344         xyz_check(AUTOMATION_PROJECTOR_X, v);
345         update_toggles(0);
346         update_mwindow(1, 1);
347 }
348
349 int GWindowGUI::translation_event()
350 {
351         mwindow->session->gwindow_x = get_x();
352         mwindow->session->gwindow_y = get_y();
353         return 0;
354 }
355
356 int GWindowGUI::close_event()
357 {
358         hide_window();
359         mwindow->session->show_gwindow = 0;
360         unlock_window();
361
362         mwindow->gui->lock_window("GWindowGUI::close_event");
363         mwindow->gui->mainmenu->show_gwindow->set_checked(0);
364         mwindow->gui->unlock_window();
365
366         lock_window("GWindowGUI::close_event");
367         mwindow->save_defaults();
368         return 1;
369 }
370
371 int GWindowGUI::keypress_event()
372 {
373         if( ctrl_down() && shift_down() ) {
374                 switch(get_keypress()) {
375                 case KEY_F1:
376                 case KEY_F2:
377                 case KEY_F3:
378                 case KEY_F4:
379                         if( ctrl_down() && shift_down() ) {
380                                 resend_event(mwindow->gui);
381                                 return 1;
382                         }
383                 }
384         }
385         else if( !ctrl_down() && shift_down() ) {
386                 switch(get_keypress()) {
387                 case KEY_F1:
388                         toggle_camera_xyz();
389                         return 1;
390                 case KEY_F2:
391                         toggle_projector_xyz();
392                         return 1;
393                 }
394         }
395         switch(get_keypress()) {
396         case 'w':
397         case 'W':
398         case '0':
399                 if( ctrl_down() ) {
400                         close_event();
401                         return 1;
402                 }
403                 break;
404         }
405         return 0;
406 }
407
408 int GWindowGUI::check_xyz(int group)
409 {
410 // returns 1=all set, -1=all clear, 0=mixed
411         int *autos = mwindow->edl->session->auto_conf->autos;
412         int v = autos[group], ret = v ? 1 : -1;
413         if( autos[group+1] != v || autos[group+2] != v ) ret = 0;
414         return ret;
415 }
416 void GWindowGUI::xyz_check(int group, int v)
417 {
418         int *autos = mwindow->edl->session->auto_conf->autos;
419         autos[group+0] = v;
420         autos[group+1] = v;
421         autos[group+2] = v;
422 }
423
424 int* GWindowGUI::get_main_value(toggleinfo *info)
425 {
426         if( info->isauto > 0 )
427                 return &mwindow->edl->session->auto_conf->autos[info->ref];
428         switch( info->ref ) {
429         case NON_AUTOMATION_ASSETS: return &mwindow->edl->session->show_assets;
430         case NON_AUTOMATION_TITLES: return &mwindow->edl->session->show_titles;
431         case NON_AUTOMATION_TRANSITIONS: return &mwindow->edl->session->auto_conf->transitions;
432         case NON_AUTOMATION_PLUGIN_AUTOS: return &mwindow->edl->session->auto_conf->plugins;
433         case NON_AUTOMATION_HARD_EDGES: return &mwindow->edl->session->auto_conf->hard_edges;
434         }
435         return 0;
436 }
437
438
439 GWindowToggle::GWindowToggle(GWindowGUI *gui, int x, int y,
440         const char *text, int color, toggleinfo *info)
441  : BC_CheckBox(x, y, 0, text, MEDIUMFONT, color)
442 {
443         this->gui = gui;
444         this->info = info;
445         this->color = color;
446         this->color_button = 0;
447         hot = hot_value = 0;
448 }
449
450 GWindowToggle::~GWindowToggle()
451 {
452         delete color_button;
453 }
454
455 int GWindowToggle::handle_event()
456 {
457         int value = get_value();
458         if( shift_down() ) {
459                 if( !hot ) {
460                         gui->set_hot(this);
461                         value = 1;
462                 }
463                 else {
464                         gui->set_cool(1);
465                         value = hot_value;
466                 }
467         }
468         else
469                 gui->set_cool(0);
470         if( info->isauto >= 0 ) {
471                 *gui->get_main_value(info) = value;
472                 switch( info->ref ) {
473                 case AUTOMATION_CAMERA_X:
474                 case AUTOMATION_CAMERA_Y:
475                 case AUTOMATION_CAMERA_Z: {
476                         int v = gui->check_xyz(AUTOMATION_CAMERA_X);
477                         gui->camera_xyz->set_value(v > 0 ? 1 : 0);
478                         break; }
479                 case AUTOMATION_PROJECTOR_X:
480                 case AUTOMATION_PROJECTOR_Y:
481                 case AUTOMATION_PROJECTOR_Z: {
482                         int v = gui->check_xyz(AUTOMATION_PROJECTOR_X);
483                         gui->projector_xyz->set_value(v > 0 ? 1 : 0);
484                         break; }
485                 }
486         }
487         else {
488                 int group = -1;
489                 switch( info->ref ) {
490                 case NONAUTOTOGGLES_CAMERA_XYZ:     group = AUTOMATION_CAMERA_X;     break;
491                 case NONAUTOTOGGLES_PROJECTOR_XYZ:  group = AUTOMATION_PROJECTOR_X;  break;
492                 case NON_AUTOMATION_HARD_EDGES: *gui->get_main_value(info) = value;  break;
493                 }
494                 if( group >= 0 ) {
495                         gui->xyz_check(group, value);
496                         gui->update_toggles(0);
497                 }
498         }
499         gui->update_mwindow(1, 0);
500
501 // Update stuff in MWindow
502         unlock_window();
503         MWindow *mwindow = gui->mwindow;
504         mwindow->gui->lock_window("GWindowToggle::handle_event");
505
506         mwindow->gui->update(1, NORMAL_DRAW, 0, 0, 1, 0, 0);
507         mwindow->gui->draw_overlays(1);
508
509         if( value && info->isauto > 0 ) {
510                 int autogroup_type = -1;
511                 switch( info->ref ) {
512                 case AUTOMATION_FADE:
513                         autogroup_type = mwindow->edl->tracks->recordable_video_tracks() ?
514                                 AUTOGROUPTYPE_VIDEO_FADE : AUTOGROUPTYPE_AUDIO_FADE ;
515                         break;
516                 case AUTOMATION_SPEED:
517                         autogroup_type = AUTOGROUPTYPE_SPEED;
518                         break;
519                 case AUTOMATION_CAMERA_X:
520                 case AUTOMATION_PROJECTOR_X:
521                         autogroup_type = AUTOGROUPTYPE_X;
522                         break;
523                 case AUTOMATION_CAMERA_Y:
524                 case AUTOMATION_PROJECTOR_Y:
525                         autogroup_type = AUTOGROUPTYPE_Y;
526                         break;
527                 case AUTOMATION_CAMERA_Z:
528                 case AUTOMATION_PROJECTOR_Z:
529                         autogroup_type = AUTOGROUPTYPE_ZOOM;
530                         break;
531                 }
532                 if( autogroup_type >= 0 ) {
533                         mwindow->edl->local_session->zoombar_showautotype = autogroup_type;
534                         mwindow->gui->zoombar->update_autozoom();
535                 }
536         }
537
538         mwindow->gui->unlock_window();
539         lock_window("GWindowToggle::handle_event");
540
541         return 1;
542 }
543
544 void GWindowToggle::update()
545 {
546         int *vp = gui->get_main_value(info);
547         if( vp ) set_value(*vp);
548 }
549
550 void GWindowToggle::update_gui(int color)
551 {
552         BC_Toggle::color = color;
553         draw_face(1,0);
554 }
555
556 int GWindowToggle::draw_face(int flash, int flush)
557 {
558         int ret = BC_Toggle::draw_face(flash, flush);
559         if( hot ) {
560                 set_color(color);
561                 set_opaque();
562                 draw_rectangle(text_x-1, text_y-1, text_w+1, text_h+1);
563                 if( flash ) this->flash(0);
564                 if( flush ) this->flush();
565         }
566         return ret;
567 }
568
569 void GWindowGUI::set_cool(int reset, int all)
570 {
571         for( int i=0; i<(int)(sizeof(toggles)/sizeof(toggles[0])); ++i ) {
572                 GWindowToggle* toggle = toggles[i];
573                 if( !toggle ) continue;
574                 int *vp = get_main_value(toggle->info);
575                 if( !vp ) continue;
576                 if( toggle->hot ) {
577                         toggle->hot = 0;
578                         toggle->draw_face(1, 0);
579                 }
580                 if( reset > 0 )
581                         *vp = toggle->hot_value;
582                 else {
583                         toggle->hot_value = *vp;
584                         if( reset < 0 ) {
585                                 if ( all || toggle->info->isauto > 0 )
586                                         *vp = 0;
587                         }
588                 }
589         }
590         if( reset )
591                 update_toggles(0);
592 }
593
594 void GWindowGUI::set_hot(GWindowToggle *toggle)
595 {
596         int *vp = get_main_value(toggle->info);
597         if( !vp ) return;
598         set_cool(-1, !toggle->info->isauto ? 1 : 0);
599         toggle->hot = 1;
600         toggle->set_value(*vp = 1);
601 }
602