prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / preferencesthread.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2011 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 "aboutprefs.h"
23 #include "asset.h"
24 #include "audiodevice.inc"
25 #include "bcsignals.h"
26 #include "cache.h"
27 #include "cplayback.h"
28 #include "cwindow.h"
29 #include "cwindowgui.h"
30 #include "bchash.h"
31 #include "edl.h"
32 #include "edlsession.h"
33 #include "filesystem.h"
34 #include "fonts.h"
35 #include "interfaceprefs.h"
36 #include "keys.h"
37 #include "language.h"
38 #include "levelwindow.h"
39 #include "levelwindowgui.h"
40 #include "mainerror.h"
41 #include "mbuttons.h"
42 #include "meterpanel.h"
43 #include "mutex.h"
44 #include "mwindow.h"
45 #include "mwindowgui.h"
46 #include "patchbay.h"
47 #include "performanceprefs.h"
48 #include "playbackengine.h"
49 #include "playbackprefs.h"
50 #include "preferences.h"
51 #include "recordprefs.h"
52 #include "shbtnprefs.h"
53 #include "theme.h"
54 #include "trackcanvas.h"
55 #include "transportque.h"
56 #include "vwindow.h"
57 #include "vwindowgui.h"
58
59 #include <string.h>
60
61
62
63 #define WIDTH 750
64 #define HEIGHT 670
65
66
67 PreferencesMenuitem::PreferencesMenuitem(MWindow *mwindow)
68  : BC_MenuItem(_("Preferences..."), _("Shift+P"), 'P')
69 {
70         this->mwindow = mwindow; 
71
72         set_shift(1);
73         thread = new PreferencesThread(mwindow);
74 }
75
76 PreferencesMenuitem::~PreferencesMenuitem()
77 {
78         delete thread;
79 }
80
81
82 int PreferencesMenuitem::handle_event() 
83 {
84         mwindow->gui->unlock_window();
85         thread->start();
86         mwindow->gui->lock_window("PreferencesMenuitem::handle_event");
87         return 1;
88 }
89
90
91
92
93 PreferencesThread::PreferencesThread(MWindow *mwindow)
94  : BC_DialogThread()
95 {
96         this->mwindow = mwindow;
97         window = 0;
98         thread_running = 0;
99 }
100
101 PreferencesThread::~PreferencesThread()
102 {
103         close_window();
104 }
105
106 BC_Window* PreferencesThread::new_gui()
107 {
108
109         preferences = new Preferences;
110         edl = new EDL;
111         edl->create_objects();
112         current_dialog = mwindow->defaults->get("DEFAULTPREF", 0);
113         preferences->copy_from(mwindow->preferences);
114         edl->copy_session(mwindow->edl);
115         redraw_indexes = 0;
116         redraw_meters = 0;
117         redraw_times = 0;
118         redraw_overlays = 0;
119         close_assets = 0;
120         reload_plugins = 0;
121         //int need_new_indexes = 0;
122         rerender = 0;
123
124         mwindow->gui->lock_window("NewThread::new_gui");
125         int scr_x = mwindow->gui->get_screen_x(0, -1);
126         int scr_w = mwindow->gui->get_screen_w(0, -1);
127         int scr_h = mwindow->gui->get_screen_h(0, -1);
128
129         int x = scr_x + scr_w / 2 - WIDTH / 2;
130         int y = scr_h / 2 - HEIGHT / 2;
131
132         window = new PreferencesWindow(mwindow, this, x, y);
133         window->create_objects();
134         mwindow->gui->unlock_window();
135
136         thread_running = 1;
137         return window;
138 }
139
140 void PreferencesThread::handle_close_event(int result)
141 {
142         thread_running = 0;
143         if(!result)
144         {
145                 apply_settings();
146                 mwindow->save_defaults();
147         }
148
149         window = 0;
150         delete preferences;
151         edl->Garbage::remove_user();
152         preferences = 0;
153         edl = 0;
154
155         mwindow->defaults->update("DEFAULTPREF", current_dialog);
156         if( mwindow->restart() )
157                 mwindow->gui->set_done(0);
158 }
159
160
161
162 int PreferencesThread::update_framerate()
163 {
164         if(thread_running)
165         {
166                 lock_gui("PreferencesThread::update_framerate");
167                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
168                 if(window) window->update_framerate();
169                 unlock_gui();
170         }
171         return 0;
172 }
173
174
175 void PreferencesThread::update_rates()
176 {
177         if(thread_running)
178         {
179                 lock_gui("PreferencesThread::update_framerate");
180                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
181                 if(window) window->update_rates();
182                 unlock_gui();
183         }
184 }
185
186 int PreferencesThread::apply_settings()
187 {
188 // Compare sessions                                                                                     
189
190         PlaybackConfig *this_playback_config = edl->session->playback_config;
191         AudioOutConfig *this_aconfig = this_playback_config->aconfig;
192         VideoOutConfig *this_vconfig = this_playback_config->vconfig;
193         PlaybackConfig *playback_config = mwindow->edl->session->playback_config;
194         AudioOutConfig *aconfig = playback_config->aconfig;
195         VideoOutConfig *vconfig = playback_config->vconfig;
196
197         rerender = 
198                 edl->session->need_rerender(mwindow->edl->session) ||
199                 (preferences->force_uniprocessor != preferences->force_uniprocessor) ||
200                 this_playback_config->active_config != playback_config->active_config ||
201                 (*this_aconfig != *aconfig) || (*this_vconfig != *vconfig) ||
202                 !preferences->brender_asset->equivalent(*mwindow->preferences->brender_asset, 0, 1);
203
204         if( strcmp(preferences->theme, mwindow->preferences->theme) != 0 )
205                 mwindow->restart_status = -1; // reload, need new bcresources
206
207         mwindow->edl->copy_session(edl, 1);
208         mwindow->preferences->copy_from(preferences);
209         mwindow->init_brender();
210
211         BC_Signals::set_catch_segv(mwindow->preferences->trap_sigsegv);
212         BC_Signals::set_catch_intr(mwindow->preferences->trap_sigintr);
213
214         mwindow->reset_android_remote();
215         mwindow->gui->ffmpeg_toggle->update(mwindow->preferences->ffmpeg_early_probe);
216         mwindow->gui->mainshbtns->load(mwindow->preferences);
217
218 //edl->session->recording_format->dump();
219 //mwindow->edl->session->recording_format->dump();
220
221         if(((mwindow->edl->session->output_w % 4) || 
222                 (mwindow->edl->session->output_h % 4)) && 
223                 mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
224         {
225                 MainError::show_error(
226                         _("This project's dimensions are not multiples of 4 so\n"
227                         "it can't be rendered by OpenGL."));
228         }
229
230
231         if(redraw_meters)
232         {
233                 mwindow->cwindow->gui->lock_window("PreferencesThread::apply_settings");
234                 mwindow->cwindow->gui->meters->change_format(edl->session->meter_format,
235                         edl->session->min_meter_db,
236                         edl->session->max_meter_db);
237                 mwindow->cwindow->gui->unlock_window();
238
239
240
241                 for(int i = 0; i < mwindow->vwindows.size(); i++)
242                 {
243                         VWindow *vwindow = mwindow->vwindows.get(i);
244                         vwindow->gui->lock_window("PreferencesThread::apply_settings");
245                         vwindow->gui->meters->change_format(edl->session->meter_format,
246                                 edl->session->min_meter_db,
247                                 edl->session->max_meter_db);
248                         vwindow->gui->unlock_window();
249
250                 }
251
252
253                 mwindow->gui->lock_window("PreferencesThread::apply_settings 1");
254                 mwindow->gui->set_meter_format(edl->session->meter_format,
255                         edl->session->min_meter_db,
256                         edl->session->max_meter_db);
257                 mwindow->gui->unlock_window();
258
259
260
261                 mwindow->lwindow->gui->lock_window("PreferencesThread::apply_settings");
262                 mwindow->lwindow->gui->panel->change_format(edl->session->meter_format,
263                         edl->session->min_meter_db,
264                         edl->session->max_meter_db);
265                 mwindow->lwindow->gui->unlock_window();
266         }
267
268         if(redraw_overlays)
269         {
270                 mwindow->gui->lock_window("PreferencesThread::apply_settings 2");
271                 mwindow->gui->draw_overlays(1);
272                 mwindow->gui->unlock_window();
273         }
274
275         if(redraw_times)
276         {
277                 mwindow->gui->lock_window("PreferencesThread::apply_settings 3");
278                 mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
279                 mwindow->gui->redraw_time_dependancies();
280                 mwindow->gui->unlock_window();
281         }
282
283         if(rerender)
284         {
285 //printf("PreferencesThread::apply_settings 1\n");
286 // This doesn't stop and restart, only reloads the assets before
287 // the next play command.
288                 mwindow->cwindow->playback_engine->que->send_command(CURRENT_FRAME,
289                         CHANGE_ALL,
290                         mwindow->edl,
291                         1);
292 //printf("PreferencesThread::apply_settings 10\n");
293         }
294
295         if(redraw_times || redraw_overlays)
296         {
297                 mwindow->gui->lock_window("PreferencesThread::apply_settings 4");
298                 mwindow->gui->flush();
299                 mwindow->gui->unlock_window();
300         }
301
302         return 0;
303 }
304
305 const char* PreferencesThread::category_to_text(int category)
306 {
307         PlaybackConfig *playback_config = edl->session->playback_config;
308         switch(category)
309         {
310                 case PLAYBACK_A:
311                         return playback_config->active_config==0 ?
312                                  _("*Playback A") : _("Playback A");
313                 case PLAYBACK_B:
314                         return playback_config->active_config==1 ?
315                                  _("*Playback B") : _("Playback B");
316                 case RECORD:
317                         return _("Recording");
318                 case PERFORMANCE:
319                         return _("Performance");
320                 case INTERFACE:
321                         return _("Interface");
322                 case ABOUT:
323                         return _("About");
324         }
325         return "";
326 }
327
328 int PreferencesThread::text_to_category(const char *category)
329 {
330 SET_TRACE
331         int min_result = -1, result, result_num = 0;
332         for(int i = 0; i < CATEGORIES; i++)
333         {
334                 result = labs(strcmp(category_to_text(i), category));
335                 if(result < min_result || min_result < 0) 
336                 {
337                         min_result = result;
338                         result_num = i;
339                 }
340         }
341 SET_TRACE
342         return result_num;
343 }
344
345
346
347
348
349
350
351
352 PreferencesWindow::PreferencesWindow(MWindow *mwindow, 
353         PreferencesThread *thread,
354         int x,
355         int y)
356  : BC_Window(_(PROGRAM_NAME ": Preferences"), 
357         x,
358         y,
359         WIDTH, 
360         HEIGHT,
361         (int)BC_INFINITY,
362         (int)BC_INFINITY,
363         0,
364         0,
365         1)
366 {
367         this->mwindow = mwindow;
368         this->thread = thread;
369         dialog = 0;
370         category = 0;
371 }
372
373 PreferencesWindow::~PreferencesWindow()
374 {
375         lock_window("PreferencesWindow::~PreferencesWindow");
376         delete category;
377
378
379         if(dialog) delete dialog;
380
381
382         for(int i = 0; i < categories.total; i++)
383                 delete categories.values[i];
384         unlock_window();
385 }
386
387 void PreferencesWindow::create_objects()
388 {
389         BC_Button *button;
390
391
392
393         lock_window("PreferencesWindow::create_objects");
394
395         mwindow->theme->draw_preferences_bg(this);
396         flash();
397
398         int x = mwindow->theme->preferencescategory_x;
399         int y = mwindow->theme->preferencescategory_y;
400         for(int i = 0; i < CATEGORIES; i++)
401         {
402                 add_subwindow(category_button[i] = new PreferencesButton(mwindow,
403                         thread,
404                         x,
405                         y,
406                         i,
407                         thread->category_to_text(i),
408                         (i == thread->current_dialog) ?
409                                 mwindow->theme->get_image_set("category_button_checked") : 
410                                 mwindow->theme->get_image_set("category_button")));
411                 x += category_button[i]->get_w() -
412                         mwindow->theme->preferences_category_overlap;
413         }
414
415
416 //      for(int i = 0; i < CATEGORIES; i++)
417 //              categories.append(new BC_ListBoxItem(thread->category_to_text(i)));
418 //      category = new PreferencesCategory(mwindow, 
419 //              thread, 
420 //              mwindow->theme->preferencescategory_x, 
421 //              mwindow->theme->preferencescategory_y);
422 //      category->create_objects();
423
424
425         add_subwindow(button = new PreferencesOK(mwindow, thread));
426         add_subwindow(new PreferencesApply(mwindow, thread));
427         add_subwindow(new PreferencesCancel(mwindow, thread));
428
429         set_current_dialog(thread->current_dialog);
430
431         show_window();
432         unlock_window();
433 }
434
435 int PreferencesWindow::update_framerate()
436 {
437         lock_window("PreferencesWindow::update_framerate");
438         if(thread->current_dialog < PreferencesThread::RECORD)
439         {
440                 dialog->draw_framerate(1);
441 //              flash();
442         }
443         unlock_window();
444         return 0;
445 }
446
447
448 void PreferencesWindow::update_rates()
449 {
450         lock_window("PreferencesWindow::update_rates");
451         if(thread->current_dialog == PreferencesThread::PERFORMANCE)
452         {
453                 dialog->update_rates();
454         }
455         unlock_window();
456 }
457
458
459 int PreferencesWindow::set_current_dialog(int number)
460 {
461         if(dialog) delete dialog;
462         dialog = 0;
463         thread->current_dialog = number;
464
465 //PRINT_TRACE
466         PreferencesDialog *dialog2 = dialog;
467         dialog = 0;
468 //PRINT_TRACE
469
470 // Redraw category buttons
471         for(int i = 0; i < CATEGORIES; i++)
472         {
473                 if(i == number)
474                 {
475                         category_button[i]->set_images(
476                                 mwindow->theme->get_image_set("category_button_checked"));
477                 }
478                 else
479                 {
480                         category_button[i]->set_images(
481                                 mwindow->theme->get_image_set("category_button"));
482                 }
483                 category_button[i]->draw_face(0);
484
485 // Copy face to background for next button's overlap.
486 // Still can't do state changes right.
487         }
488
489
490 //PRINT_TRACE
491         PlaybackConfig *playback_config = thread->edl->session->playback_config;
492         switch(number)
493         {
494                 case PreferencesThread::PLAYBACK_A:
495                 case PreferencesThread::PLAYBACK_B:
496                         playback_config->load_defaults(mwindow->defaults,
497                                 number == PreferencesThread::PLAYBACK_A ? 0 : 1);
498                         add_subwindow(dialog = new PlaybackPrefs(mwindow, this, number));
499                         break;
500         
501                 case PreferencesThread::RECORD:
502                         add_subwindow(dialog = new RecordPrefs(mwindow, this));
503                         break;
504         
505                 case PreferencesThread::PERFORMANCE:
506                         add_subwindow(dialog = new PerformancePrefs(mwindow, this));
507                         break;
508         
509                 case PreferencesThread::INTERFACE:
510                         add_subwindow(dialog = new InterfacePrefs(mwindow, this));
511                         break;
512         
513                 case PreferencesThread::ABOUT:
514                         add_subwindow(dialog = new AboutPrefs(mwindow, this));
515                         break;
516         }
517
518 //PRINT_TRACE
519         if(dialog)
520         {
521                 dialog->draw_top_background(this, 0, 0, dialog->get_w(), dialog->get_h());
522 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
523                 dialog->create_objects();
524 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
525                 dialog->show_window(0);
526         }
527
528         if(dialog2) 
529         {
530                 dialog2->hide_window(0);
531                 delete dialog2;
532         }
533
534         return 0;
535 }
536
537
538
539
540
541
542
543
544
545
546
547 PreferencesButton::PreferencesButton(MWindow *mwindow, 
548         PreferencesThread *thread, 
549         int x, 
550         int y,
551         int category,
552         const char *text,
553         VFrame **images)
554  : BC_GenericButton(x, y, text, images)
555 {
556         this->mwindow = mwindow;
557         this->thread = thread;
558         this->category = category;
559 }
560
561 int PreferencesButton::handle_event()
562 {
563         thread->window->set_current_dialog(category);
564         return 1;
565 }
566
567
568
569
570
571
572
573
574
575 PreferencesDialog::PreferencesDialog(MWindow *mwindow, 
576         PreferencesWindow *pwindow)
577  : BC_SubWindow(10, 
578         40, 
579         pwindow->get_w() - 20, 
580         pwindow->get_h() - BC_GenericButton::calculate_h() - 10 - 40)
581 {
582         this->pwindow = pwindow;
583         this->mwindow = mwindow;
584         preferences = pwindow->thread->preferences;
585 }
586
587 PreferencesDialog::~PreferencesDialog()
588 {
589 }
590
591 // ============================== category window
592
593
594
595
596 PreferencesApply::PreferencesApply(MWindow *mwindow, PreferencesThread *thread)
597  : BC_GenericButton(thread->window->get_w() / 2 - BC_GenericButton::calculate_w(thread->window, _("Apply")) / 2, 
598         thread->window->get_h() - BC_GenericButton::calculate_h() - 10, 
599         _("Apply"))
600 {
601         this->mwindow = mwindow;
602         this->thread = thread;
603 }
604
605 int PreferencesApply::handle_event()
606 {
607         thread->apply_settings();
608         return 1;
609 }
610
611
612
613
614 PreferencesOK::PreferencesOK(MWindow *mwindow, PreferencesThread *thread)
615  : BC_GenericButton(10, 
616         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
617         _("OK"))
618 {
619         this->mwindow = mwindow;
620         this->thread = thread;
621 }
622
623 int PreferencesOK::keypress_event()
624 {
625         if(get_keypress() == RETURN)
626         {
627                 thread->window->set_done(0);
628                 return 1;
629         }
630         return 0;
631 }
632 int PreferencesOK::handle_event()
633 {
634         thread->window->set_done(0);
635         return 1;
636 }
637
638
639
640 PreferencesCancel::PreferencesCancel(MWindow *mwindow, PreferencesThread *thread)
641  : BC_GenericButton(thread->window->get_w() - BC_GenericButton::calculate_w(thread->window, _("Cancel")) - 10,
642         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
643         _("Cancel"))
644 {
645         this->mwindow = mwindow;
646         this->thread = thread;
647 }
648 int PreferencesCancel::keypress_event()
649 {
650         if(get_keypress() == ESC)
651         {
652                 thread->window->set_done(1);
653                 return 1;
654         }
655         return 0;
656 }
657
658 int PreferencesCancel::handle_event()
659 {
660         thread->window->set_done(1);
661         return 1;
662 }
663
664
665
666
667
668
669
670
671
672
673 PreferencesCategory::PreferencesCategory(MWindow *mwindow, PreferencesThread *thread, int x, int y)
674  : BC_PopupTextBox(thread->window, 
675                 &thread->window->categories,
676                 thread->category_to_text(thread->current_dialog),
677                 x, 
678                 y, 
679                 200,
680                 150)
681 {
682         this->mwindow = mwindow;
683         this->thread = thread;
684 }
685
686 PreferencesCategory::~PreferencesCategory()
687 {
688 }
689
690 int PreferencesCategory::handle_event()
691 {
692 SET_TRACE
693         thread->window->set_current_dialog(thread->text_to_category(get_text()));
694 SET_TRACE
695         return 1;
696 }