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