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