asset drag/drop to viewers, bluebanana bug, listbox fontlist highlight
[goodguy/history.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 "language.h"
31 #include "localsession.h"
32 #include "mainmenu.h"
33 #include "mainsession.h"
34 #include "mwindow.h"
35 #include "mwindowgui.h"
36 #include "theme.h"
37 #include "tracks.h"
38 #include "trackcanvas.h"
39 #include "zoombar.h"
40
41 #include <math.h>
42
43
44
45 GWindowGUI::GWindowGUI(MWindow *mwindow, int w, int h)
46  : BC_Window(_(PROGRAM_NAME ": Overlays"),
47         mwindow->session->gwindow_x, mwindow->session->gwindow_y,
48         w, h, w, h, 0, 0, 1)
49 {
50         this->mwindow = mwindow;
51         color_thread = 0;
52 }
53
54 GWindowGUI::~GWindowGUI()
55 {
56         delete color_thread;
57 }
58
59 void GWindowGUI::start_color_thread(GWindowColorButton *color_button)
60 {
61         unlock_window();
62         delete color_thread;
63         color_thread = new GWindowColorThread(color_button);
64         int color = auto_colors[color_button->auto_toggle->info->ref];
65         color_thread->start(color);
66         lock_window("GWindowGUI::start_color_thread");
67 }
68
69 const char *GWindowGUI::other_text[NON_AUTOMATION_TOTAL] =
70 {
71         N_("Assets"),
72         N_("Titles"),
73         N_("Transitions"),
74         N_("Plugin Autos")
75 };
76
77 const char *GWindowGUI::auto_text[AUTOMATION_TOTAL] =
78 {
79         N_("Mute"),
80         N_("Camera X"),
81         N_("Camera Y"),
82         N_("Camera Z"),
83         N_("Projector X"),
84         N_("Projector Y"),
85         N_("Projector Z"),
86         N_("Fade"),
87         N_("Pan"),
88         N_("Mode"),
89         N_("Mask"),
90         N_("Speed")
91 };
92
93 int GWindowGUI::auto_colors[AUTOMATION_TOTAL] =
94 {
95         PINK,
96         RED,
97         GREEN,
98         BLUE,
99         LTPINK,
100         LTGREEN,
101         LTBLUE,
102         LTPURPLE,
103         0,
104         0,
105         0,
106         ORANGE,
107 };
108
109 void GWindowGUI::load_defaults()
110 {
111         BC_Hash *defaults = mwindow->defaults;
112         auto_colors[AUTOMATION_MUTE] = defaults->get("AUTO_COLOR_MUTE", auto_colors[AUTOMATION_MUTE]);
113         auto_colors[AUTOMATION_CAMERA_X] = defaults->get("AUTO_COLOR_CAMERA_X", auto_colors[AUTOMATION_CAMERA_X]);
114         auto_colors[AUTOMATION_CAMERA_Y] = defaults->get("AUTO_COLOR_CAMERA_Y", auto_colors[AUTOMATION_CAMERA_Y]);
115         auto_colors[AUTOMATION_CAMERA_Z] = defaults->get("AUTO_COLOR_CAMERA_Z", auto_colors[AUTOMATION_CAMERA_Z]);
116         auto_colors[AUTOMATION_PROJECTOR_X] = defaults->get("AUTO_COLOR_PROJECTOR_X", auto_colors[AUTOMATION_PROJECTOR_X]);
117         auto_colors[AUTOMATION_PROJECTOR_Y] = defaults->get("AUTO_COLOR_PROJECTOR_Y", auto_colors[AUTOMATION_PROJECTOR_Y]);
118         auto_colors[AUTOMATION_PROJECTOR_Z] = defaults->get("AUTO_COLOR_PROJECTOR_Z", auto_colors[AUTOMATION_PROJECTOR_Z]);
119         auto_colors[AUTOMATION_FADE] = defaults->get("AUTO_COLOR_FADE", auto_colors[AUTOMATION_FADE]);
120         auto_colors[AUTOMATION_SPEED] = defaults->get("AUTO_COLOR_SPEED", auto_colors[AUTOMATION_SPEED]);
121 }
122
123 void GWindowGUI::save_defaults()
124 {
125         BC_Hash *defaults = mwindow->defaults;
126         defaults->update("AUTO_COLOR_MUTE", auto_colors[AUTOMATION_MUTE]);
127         defaults->update("AUTO_COLOR_CAMERA_X", auto_colors[AUTOMATION_CAMERA_X]);
128         defaults->update("AUTO_COLOR_CAMERA_Y", auto_colors[AUTOMATION_CAMERA_Y]);
129         defaults->update("AUTO_COLOR_CAMERA_Z", auto_colors[AUTOMATION_CAMERA_Z]);
130         defaults->update("AUTO_COLOR_PROJECTOR_X", auto_colors[AUTOMATION_PROJECTOR_X]);
131         defaults->update("AUTO_COLOR_PROJECTOR_Y", auto_colors[AUTOMATION_PROJECTOR_Y]);
132         defaults->update("AUTO_COLOR_PROJECTOR_Z", auto_colors[AUTOMATION_PROJECTOR_Z]);
133         defaults->update("AUTO_COLOR_FADE", auto_colors[AUTOMATION_FADE]);
134         defaults->update("AUTO_COLOR_SPEED", auto_colors[AUTOMATION_SPEED]);
135 }
136
137 static toggleinfo toggle_order[] =
138 {
139         {0, NON_AUTOMATION_ASSETS},
140         {0, NON_AUTOMATION_TITLES},
141         {0, NON_AUTOMATION_TRANSITIONS},
142         {0, NON_AUTOMATION_PLUGIN_AUTOS},
143         {0, -1}, // bar
144         {1, AUTOMATION_FADE},
145         {1, AUTOMATION_MUTE},
146         {1, AUTOMATION_SPEED},
147         {1, AUTOMATION_CAMERA_X},
148         {1, AUTOMATION_CAMERA_Y},
149         {1, AUTOMATION_CAMERA_Z},
150         {1, AUTOMATION_PROJECTOR_X},
151         {1, AUTOMATION_PROJECTOR_Y},
152         {1, AUTOMATION_PROJECTOR_Z},
153         {0, -1}, // bar
154         {1, AUTOMATION_MODE},
155         {1, AUTOMATION_PAN},
156         {1, AUTOMATION_MASK},
157 };
158
159 void GWindowGUI::calculate_extents(BC_WindowBase *gui, int *w, int *h)
160 {
161         int temp1, temp2, temp3, temp4, temp5, temp6, temp7;
162         int current_w, current_h;
163         *w = 10;
164         *h = 10;
165
166         for( int i=0; i<(int)(sizeof(toggle_order)/sizeof(toggle_order[0])); ++i ) {
167                 toggleinfo *tp = &toggle_order[i];
168                 int isauto = tp->isauto, ref = tp->ref;
169                 if( ref < 0 ) {
170                         *h += get_resources()->bar_data->get_h() + 5;
171                         continue;
172                 }
173                 BC_Toggle::calculate_extents(gui,
174                         BC_WindowBase::get_resources()->checkbox_images,
175                         0, &temp1, &current_w, &current_h,
176                         &temp2, &temp3, &temp4, &temp5, &temp6, &temp7,
177                         _(isauto ?  auto_text[ref] : other_text[ref]),
178                         MEDIUMFONT);
179                 current_w += current_h;
180                 *w = MAX(current_w, *w);
181                 *h += current_h + 5;
182         }
183
184         *h += 10;
185         *w += 20;
186 }
187
188 GWindowColorButton::GWindowColorButton(GWindowToggle *auto_toggle, int x, int y, int w)
189  : BC_Button(x, y, w, vframes)
190 {
191         this->auto_toggle = auto_toggle;
192         this->color = 0;
193         for( int i=0; i<3; ++i )
194                 vframes[i] = new VFrame(w, w, BC_RGBA8888, -1);
195 }
196
197 GWindowColorButton::~GWindowColorButton()
198 {
199         for( int i=0; i<3; ++i )
200                 delete vframes[i];
201 }
202
203 void GWindowColorButton::set_color(int color)
204 {
205         this->color = color;
206         int r = (color>>16) & 0xff;
207         int g = (color>>8) & 0xff;
208         int b = (color>>0) & 0xff;
209         for( int i=0; i<3; ++i ) {
210                 VFrame *vframe = vframes[i];
211                 int ww = vframe->get_w(), hh = vframe->get_h();
212                 int cx = (ww+1)/2, cy = hh/2;
213                 double cc = (cx*cx + cy*cy) / 4.;
214                 uint8_t *bp = vframe->get_data(), *dp = bp;
215                 uint8_t *ep = dp + vframe->get_data_size();
216                 int rr = r, gg = g, bb = b;
217                 int bpl = vframe->get_bytes_per_line();
218                 switch( i ) {
219                 case BUTTON_UP:
220                         break;
221                 case BUTTON_UPHI:
222                         if( (rr+=48) > 0xff ) rr = 0xff;
223                         if( (gg+=48) > 0xff ) gg = 0xff;
224                         if( (bb+=48) > 0xff ) bb = 0xff;
225                         break;
226                 case BUTTON_DOWNHI:
227                         if( (rr-=48) < 0x00 ) rr = 0x00;
228                         if( (gg-=48) < 0x00 ) gg = 0x00;
229                         if( (bb-=48) < 0x00 ) bb = 0x00;
230                         break;
231                 }
232                 while( dp < ep ) {
233                         int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
234                         int dy = cy - yy, dx = cx - xx;
235                         double s = dx*dx + dy*dy - cc;
236                         double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
237                         int aa = ss * 0xff;
238                         *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
239                 }
240         }
241         set_images(vframes);
242 }
243
244 void GWindowColorButton::update_gui(int color)
245 {
246         set_color(color);
247         draw_face();
248 }
249
250 GWindowColorThread::GWindowColorThread(GWindowColorButton *color_button)
251  : ColorThread(0, color_button->auto_toggle->caption)
252 {
253         this->color = 0;
254         this->color_button = color_button;
255         color_update = new GWindowColorUpdate(this);
256 }
257
258 GWindowColorThread::~GWindowColorThread()
259 {
260         delete color_update;
261 }
262
263 void GWindowColorThread::start(int color)
264 {
265         start_window(color, 0, 1);
266         color_update->start();
267 }
268
269 void GWindowColorThread::handle_done_event(int result)
270 {
271         color_update->stop();
272         GWindowGUI *gui = color_button->auto_toggle->gui;
273         int ref = color_button->auto_toggle->info->ref;
274         gui->lock_window("GWindowColorThread::handle_done_event");
275         if( !result ) {
276                 GWindowGUI::auto_colors[ref] = color;
277                 color_button->auto_toggle->update_gui(color);
278                 gui->save_defaults();
279         }
280         else {
281                 color = GWindowGUI::auto_colors[ref];
282                 color_button->update_gui(color);
283         }
284         gui->unlock_window();
285         MWindowGUI *mwindow_gui = color_button->auto_toggle->gui->mwindow->gui;
286         mwindow_gui->lock_window("GWindowColorUpdate::run");
287         mwindow_gui->draw_overlays(1);
288         mwindow_gui->unlock_window();
289 }
290
291 int GWindowColorThread::handle_new_color(int color, int alpha)
292 {
293         this->color = color;
294         color_update->update_lock->unlock();
295         return 1;
296 }
297
298 void GWindowColorThread::update_gui()
299 {
300         color_button->update_gui(color);
301 }
302
303 GWindowColorUpdate::GWindowColorUpdate(GWindowColorThread *color_thread)
304  : Thread(1, 0, 0)
305 {
306         this->color_thread = color_thread;
307         this->update_lock = new Condition(0,"GWindowColorUpdate::update_lock");
308         done = 1;
309 }
310
311 GWindowColorUpdate::~GWindowColorUpdate()
312 {
313         stop();
314         delete update_lock;
315 }
316
317 void GWindowColorUpdate::start()
318 {
319         if( done ) {
320                 done = 0;
321                 Thread::start();
322         }
323 }
324
325 void GWindowColorUpdate::stop()
326 {
327         if( !done ) {
328                 done = 1;
329                 update_lock->unlock();
330                 join();
331         }
332 }
333
334 void GWindowColorUpdate::run()
335 {
336         while( !done ) {
337                 update_lock->lock("GWindowColorUpdate::run");
338                 if( done ) break;
339                 color_thread->update_gui();
340         }
341 }
342
343
344 int GWindowColorButton::handle_event()
345 {
346         GWindowGUI *gui = auto_toggle->gui;
347         gui->start_color_thread(this);
348         return 1;
349 }
350
351 void GWindowGUI::create_objects()
352 {
353         int x = 10, y = 10;
354         lock_window("GWindowGUI::create_objects 1");
355
356         for( int i=0; i<(int)(sizeof(toggle_order)/sizeof(toggle_order[0])); ++i ) {
357                 toggleinfo *tp = &toggle_order[i];
358                 int ref = tp->ref;
359                 if( ref < 0 ) {
360                         BC_Bar *bar = new BC_Bar(x,y,get_w()-x-10);
361                         add_tool(bar);
362                         toggles[i] = 0;
363                         y += bar->get_h() + 5;
364                         continue;
365                 }
366                 VFrame *vframe = 0;
367                 switch( ref ) {
368                 case AUTOMATION_MODE: vframe = mwindow->theme->modekeyframe_data;  break;
369                 case AUTOMATION_PAN:  vframe = mwindow->theme->pankeyframe_data;   break;
370                 case AUTOMATION_MASK: vframe = mwindow->theme->maskkeyframe_data;  break;
371                 }
372                 const char *label = _(tp->isauto ? auto_text[tp->ref] : other_text[tp->ref]);
373                 int color = !tp->isauto ? -1 : auto_colors[tp->ref];
374                 GWindowToggle *toggle = new GWindowToggle(mwindow, this, x, y, label, color, tp);
375                 add_tool(toggles[i] = toggle);
376                 if( vframe )
377                         draw_vframe(vframe, get_w()-vframe->get_w()-10, y);
378                 else if( tp->isauto ) {
379                         int wh = toggle->get_h() - 4;
380                         GWindowColorButton *color_button =
381                                 new GWindowColorButton(toggle, get_w()-wh-10, y+2, wh);
382                         add_tool(color_button);
383                         color_button->set_color(color);
384                         color_button->draw_face();
385                 }
386                 y += toggles[i]->get_h() + 5;
387         }
388         unlock_window();
389 }
390
391 void GWindowGUI::update_mwindow()
392 {
393         unlock_window();
394         mwindow->gui->mainmenu->update_toggles(1);
395         lock_window("GWindowGUI::update_mwindow");
396 }
397
398 void GWindowGUI::update_toggles(int use_lock)
399 {
400         if(use_lock) lock_window("GWindowGUI::update_toggles");
401
402         for( int i=0; i<(int)(sizeof(toggle_order)/sizeof(toggle_order[0])); ++i ) {
403                 if( toggles[i] ) toggles[i]->update();
404         }
405
406         if(use_lock) unlock_window();
407 }
408
409 int GWindowGUI::translation_event()
410 {
411         mwindow->session->gwindow_x = get_x();
412         mwindow->session->gwindow_y = get_y();
413         return 0;
414 }
415
416 int GWindowGUI::close_event()
417 {
418         hide_window();
419         mwindow->session->show_gwindow = 0;
420         unlock_window();
421
422         mwindow->gui->lock_window("GWindowGUI::close_event");
423         mwindow->gui->mainmenu->show_gwindow->set_checked(0);
424         mwindow->gui->unlock_window();
425
426         lock_window("GWindowGUI::close_event");
427         mwindow->save_defaults();
428         return 1;
429 }
430
431 int GWindowGUI::keypress_event()
432 {
433         switch(get_keypress()) {
434         case 'w':
435         case 'W':
436         case '0':
437                 if( ctrl_down() ) {
438                         close_event();
439                         return 1;
440                 }
441                 break;
442         }
443         return 0;
444 }
445
446
447 GWindowToggle::GWindowToggle(MWindow *mwindow, GWindowGUI *gui, int x, int y,
448         const char *text, int color, toggleinfo *info)
449  : BC_CheckBox(x, y, *get_main_value(mwindow, info), text, MEDIUMFONT, color)
450 {
451         this->mwindow = mwindow;
452         this->gui = gui;
453         this->info = info;
454         this->color = color;
455         this->color_button = 0;
456 }
457
458 GWindowToggle::~GWindowToggle()
459 {
460         delete color_button;
461 }
462
463 int GWindowToggle::handle_event()
464 {
465         int value = get_value();
466         *get_main_value(mwindow, info) = value;
467         gui->update_mwindow();
468
469
470 // Update stuff in MWindow
471         unlock_window();
472         mwindow->gui->lock_window("GWindowToggle::handle_event");
473         if( info->isauto ) {
474                 int autogroup_type = -1;
475                 switch( info->ref ) {
476                 case AUTOMATION_FADE:
477                         autogroup_type = mwindow->edl->tracks->recordable_video_tracks() ?
478                                 AUTOGROUPTYPE_VIDEO_FADE : AUTOGROUPTYPE_AUDIO_FADE ;
479                         break;
480                 case AUTOMATION_SPEED:
481                         autogroup_type = AUTOGROUPTYPE_SPEED;
482                         break;
483                 case AUTOMATION_CAMERA_X:
484                 case AUTOMATION_PROJECTOR_X:
485                         autogroup_type = AUTOGROUPTYPE_X;
486                         break;
487                 case AUTOMATION_CAMERA_Y:
488                 case AUTOMATION_PROJECTOR_Y:
489                         autogroup_type = AUTOGROUPTYPE_Y;
490                         break;
491                 case AUTOMATION_CAMERA_Z:
492                 case AUTOMATION_PROJECTOR_Z:
493                         autogroup_type = AUTOGROUPTYPE_ZOOM;
494                         break;
495                 }
496                 if( value && autogroup_type >= 0 ) {
497                         mwindow->edl->local_session->zoombar_showautotype = autogroup_type;
498                         mwindow->gui->zoombar->update_autozoom();
499                 }
500                 mwindow->gui->draw_overlays(1);
501         }
502         else {
503                 switch( info->ref ) {
504                 case NON_AUTOMATION_ASSETS:
505                 case NON_AUTOMATION_TITLES:
506                         mwindow->gui->update(1, 1, 0, 0, 1, 0, 0);
507                         break;
508
509                 case NON_AUTOMATION_TRANSITIONS:
510                 case NON_AUTOMATION_PLUGIN_AUTOS:
511                         mwindow->gui->draw_overlays(1);
512                         break;
513                 }
514         }
515
516         mwindow->gui->unlock_window();
517         lock_window("GWindowToggle::handle_event");
518
519         return 1;
520 }
521
522 int* GWindowToggle::get_main_value(MWindow *mwindow, toggleinfo *info)
523 {
524         if( info->isauto )
525                 return &mwindow->edl->session->auto_conf->autos[info->ref];
526
527         switch( info->ref ) {
528         case NON_AUTOMATION_ASSETS: return &mwindow->edl->session->show_assets;
529         case NON_AUTOMATION_TITLES: return &mwindow->edl->session->show_titles;
530         case NON_AUTOMATION_TRANSITIONS: return &mwindow->edl->session->auto_conf->transitions;
531         case NON_AUTOMATION_PLUGIN_AUTOS: return &mwindow->edl->session->auto_conf->plugins;
532         }
533         return 0;
534 }
535
536 void GWindowToggle::update()
537 {
538         int *vp = get_main_value(mwindow, info);
539         if( !vp ) return;
540         set_value(*vp);
541 }
542
543 void GWindowToggle::update_gui(int color)
544 {
545         BC_Toggle::color = color;
546         draw_face(1,0);
547 }
548