add layout_scale preference, scaling cleanup, rework init bc_resources, init tip_info...
[goodguy/cinelerra.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 xS(860)
68 #define HEIGHT yS(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_dialog("PreferencesThread::update_framerate");
178                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
179                 if(window) window->update_framerate();
180                 unlock_dialog();
181         }
182         return 0;
183 }
184
185
186 void PreferencesThread::update_rates()
187 {
188         if(thread_running)
189         {
190                 lock_dialog("PreferencesThread::update_framerate");
191                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
192                 if(window) window->update_rates();
193                 unlock_dialog();
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->autocolor_assets != mwindow->preferences->autocolor_assets )
216                 redraw_indexes = 1;
217
218         if( preferences->yuv_color_space != mwindow->preferences->yuv_color_space ||
219             preferences->yuv_color_range != mwindow->preferences->yuv_color_range ) {
220                 YUV::yuv.yuv_set_colors(
221                         preferences->yuv_color_space,
222                         preferences->yuv_color_range);
223                 rerender = 1;
224         }
225
226         if( preferences->highlight_inverse != mwindow->preferences->highlight_inverse ) {
227                 mwindow->gui->lock_window("PreferencesThread::apply_settings 0");
228                 mwindow->gui->hide_cursor(0);
229                 mwindow->gui->unlock_window();
230                 redraw_overlays = 1;
231         }
232         PreferencesWindow *window = (PreferencesWindow*)get_gui();
233         if( window ) window->unlock_window();
234         mwindow->stop_brender();
235         if( window ) window->lock_window("PreferencesThread::apply_settings 5");
236
237         if( strcmp(preferences->theme, mwindow->preferences->theme) ||
238             strcmp(preferences->plugin_icons, mwindow->preferences->plugin_icons) ||
239             preferences->awindow_picon_h != mwindow->preferences->awindow_picon_h ||
240             preferences->layout_scale != mwindow->preferences->layout_scale ||
241             preferences->vicon_size != mwindow->preferences->vicon_size ||
242             preferences->vicon_color_mode != mwindow->preferences->vicon_color_mode )
243                 mwindow->restart_status = -1;  // reconstruct/restart program
244         if( strcmp(preferences->lv2_path, mwindow->preferences->lv2_path) != 0 )
245                 reload_plugins = 1;
246         if( reload_plugins ) {
247                 MWindow::remove_plugin_index();
248                 File::setenv_path("LV2_PATH", preferences->lv2_path, 1);
249                 mwindow->restart_status = -1;
250         }
251
252         mwindow->edl->copy_session(edl, 1);
253         mwindow->preferences->copy_from(preferences);
254
255         BC_Signals::set_catch_segv(mwindow->preferences->trap_sigsegv);
256         BC_Signals::set_catch_intr(mwindow->preferences->trap_sigintr);
257         BC_WindowBase::get_resources()->popupmenu_btnup = mwindow->preferences->popupmenu_btnup;
258         BC_WindowBase::get_resources()->grab_input_focus = mwindow->preferences->grab_input_focus;
259         BC_WindowBase::get_resources()->textbox_focus_policy = mwindow->preferences->textbox_focus_policy;
260         if( mwindow->preferences->trap_sigsegv || mwindow->preferences->trap_sigintr ) {
261                 BC_Trace::enable_locks();
262         }
263         else {
264                 BC_Trace::disable_locks();
265         }
266
267         mwindow->reset_android_remote();
268         int ffmpeg_early_probe = mwindow->preferences->get_file_probe_armed("FFMPEG_Early");
269         mwindow->gui->ffmpeg_toggle->update(ffmpeg_early_probe);
270         mwindow->gui->ffmpeg_toggle->set_tooltip(ffmpeg_early_probe ?
271                 FFMPEG_EARLY_TIP : FFMPEG_LATE_TIP);
272         mwindow->gui->mainshbtns->load(mwindow->preferences);
273         mwindow->init_brender();
274
275 //edl->session->recording_format->dump();
276 //mwindow->edl->session->recording_format->dump();
277 #ifdef GLx4
278         if(((mwindow->edl->session->output_w % 4) ||
279                 (mwindow->edl->session->output_h % 4)) &&
280                 mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
281         {
282                 MainError::show_error(
283                         _("This project's dimensions are not multiples of 4 so\n"
284                         "it can't be rendered by OpenGL."));
285         }
286 #endif
287
288         if(redraw_meters)
289         {
290                 mwindow->cwindow->gui->lock_window("PreferencesThread::apply_settings");
291                 mwindow->cwindow->gui->meters->change_format(edl->session->meter_format,
292                         edl->session->min_meter_db,
293                         edl->session->max_meter_db);
294                 mwindow->cwindow->gui->unlock_window();
295
296
297
298                 for(int i = 0; i < mwindow->vwindows.size(); i++) {
299                         VWindow *vwindow = mwindow->vwindows.get(i);
300                         if( !vwindow->is_running() ) continue;
301                         vwindow->gui->lock_window("PreferencesThread::apply_settings");
302                         vwindow->gui->meters->change_format(edl->session->meter_format,
303                                 edl->session->min_meter_db,
304                                 edl->session->max_meter_db);
305                         vwindow->gui->unlock_window();
306
307                 }
308
309
310                 mwindow->gui->lock_window("PreferencesThread::apply_settings 1");
311                 mwindow->gui->set_meter_format(edl->session->meter_format,
312                         edl->session->min_meter_db,
313                         edl->session->max_meter_db);
314                 mwindow->gui->unlock_window();
315
316
317
318                 mwindow->lwindow->gui->lock_window("PreferencesThread::apply_settings");
319                 mwindow->lwindow->gui->panel->change_format(edl->session->meter_format,
320                         edl->session->min_meter_db,
321                         edl->session->max_meter_db);
322                 mwindow->lwindow->gui->unlock_window();
323         }
324
325         if(redraw_overlays)
326         {
327                 mwindow->gui->lock_window("PreferencesThread::apply_settings 2");
328                 mwindow->gui->show_cursor(0);
329                 mwindow->gui->draw_overlays(1);
330                 mwindow->gui->unlock_window();
331         }
332
333         if(redraw_times)
334         {
335                 mwindow->gui->lock_window("PreferencesThread::apply_settings 3");
336                 mwindow->gui->update(0, NO_DRAW, 1, 0, 0, 1, 0);
337                 mwindow->gui->redraw_time_dependancies();
338                 mwindow->gui->unlock_window();
339         }
340
341         if(rerender)
342         {
343 //printf("PreferencesThread::apply_settings 1\n");
344 // This doesn't stop and restart, only reloads the assets before
345 // the next play command.
346                 mwindow->cwindow->refresh_frame(CHANGE_ALL, mwindow->edl);
347 //printf("PreferencesThread::apply_settings 10\n");
348         }
349
350         if(redraw_indexes)
351         {
352                 mwindow->gui->lock_window("PreferencesThread::apply_settings 4");
353                 mwindow->gui->draw_trackmovement();
354                 mwindow->gui->unlock_window();
355         }
356
357         if(redraw_times || redraw_overlays || redraw_indexes)
358         {
359                 mwindow->gui->lock_window("PreferencesThread::apply_settings 5");
360                 mwindow->gui->flush();
361                 mwindow->gui->unlock_window();
362         }
363
364         return 0;
365 }
366
367 const char* PreferencesThread::category_to_text(int category)
368 {
369         PlaybackConfig *playback_config = edl->session->playback_config;
370         switch(category)
371         {
372                 case PLAYBACK_A:
373                         return playback_config->active_config==0 ?
374                                  _("*Playback A") : _("Playback A");
375                 case PLAYBACK_B:
376                         return playback_config->active_config==1 ?
377                                  _("*Playback B") : _("Playback B");
378                 case RECORD:
379                         return _("Recording");
380                 case PERFORMANCE:
381                         return _("Performance");
382                 case INTERFACE:
383                         return _("Interface");
384                 case APPEARANCE:
385                         return _("Appearance");
386                 case ABOUT:
387                         return _("About");
388         }
389         return "";
390 }
391
392 int PreferencesThread::text_to_category(const char *category)
393 {
394 SET_TRACE
395         int min_result = -1, result, result_num = 0;
396         for(int i = 0; i < CATEGORIES; i++)
397         {
398                 result = labs(strcmp(category_to_text(i), category));
399                 if(result < min_result || min_result < 0)
400                 {
401                         min_result = result;
402                         result_num = i;
403                 }
404         }
405 SET_TRACE
406         return result_num;
407 }
408
409
410 PreferencesWindow::PreferencesWindow(MWindow *mwindow,
411         PreferencesThread *thread, int x, int y, int w, int h)
412  : BC_Window(_(PROGRAM_NAME ": Preferences"), x,y, w,h,w,h, 1)
413 {
414         this->mwindow = mwindow;
415         this->thread = thread;
416         dialog = 0;
417         category = 0;
418 }
419
420 PreferencesWindow::~PreferencesWindow()
421 {
422         lock_window("PreferencesWindow::~PreferencesWindow");
423         delete category;
424
425
426         if(dialog) delete dialog;
427
428
429         for(int i = 0; i < categories.total; i++)
430                 delete categories.values[i];
431         unlock_window();
432 }
433
434 void PreferencesWindow::create_objects()
435 {
436         BC_Button *button;
437
438         lock_window("PreferencesWindow::create_objects");
439         set_icon(mwindow->theme->get_image("mwindow_icon"));
440         mwindow->theme->draw_preferences_bg(this);
441         flash();
442
443         int x = mwindow->theme->preferencescategory_x;
444         int y = mwindow->theme->preferencescategory_y;
445         for(int i = 0; i < CATEGORIES; i++)
446         {
447                 add_subwindow(category_button[i] = new PreferencesButton(mwindow,
448                         thread, x, y, i, thread->category_to_text(i),
449                         (i == thread->current_dialog) ?
450                                 mwindow->theme->get_image_set("category_button_checked") :
451                                 mwindow->theme->get_image_set("category_button")));
452                 x += category_button[i]->get_w() -
453                         mwindow->theme->preferences_category_overlap;
454         }
455
456
457 //      for(int i = 0; i < CATEGORIES; i++)
458 //              categories.append(new BC_ListBoxItem(thread->category_to_text(i)));
459 //      category = new PreferencesCategory(mwindow,
460 //              thread,
461 //              mwindow->theme->preferencescategory_x,
462 //              mwindow->theme->preferencescategory_y);
463 //      category->create_objects();
464
465
466         add_subwindow(button = new PreferencesOK(mwindow, thread));
467         add_subwindow(new PreferencesApply(mwindow, thread));
468         add_subwindow(new PreferencesCancel(mwindow, thread));
469
470         set_current_dialog(thread->current_dialog);
471
472         show_window();
473         unlock_window();
474 }
475
476 int PreferencesWindow::update_framerate()
477 {
478         lock_window("PreferencesWindow::update_framerate");
479         if(thread->current_dialog < PreferencesThread::RECORD)
480         {
481                 dialog->draw_framerate(1);
482 //              flash();
483         }
484         unlock_window();
485         return 0;
486 }
487
488
489 void PreferencesWindow::update_rates()
490 {
491         lock_window("PreferencesWindow::update_rates");
492         if(thread->current_dialog == PreferencesThread::PERFORMANCE)
493         {
494                 dialog->update_rates();
495         }
496         unlock_window();
497 }
498
499
500 int PreferencesWindow::set_current_dialog(int number)
501 {
502         if(dialog) delete dialog;
503         dialog = 0;
504         thread->current_dialog = number;
505
506 //PRINT_TRACE
507         PreferencesDialog *dialog2 = dialog;
508         dialog = 0;
509 //PRINT_TRACE
510
511 // Redraw category buttons
512         for(int i = 0; i < CATEGORIES; i++)
513         {
514                 if(i == number)
515                 {
516                         category_button[i]->set_images(
517                                 mwindow->theme->get_image_set("category_button_checked"));
518                 }
519                 else
520                 {
521                         category_button[i]->set_images(
522                                 mwindow->theme->get_image_set("category_button"));
523                 }
524                 category_button[i]->draw_face(0);
525
526 // Copy face to background for next button's overlap.
527 // Still can't do state changes right.
528         }
529
530
531 //PRINT_TRACE
532         PlaybackConfig *playback_config = thread->edl->session->playback_config;
533         switch(number)
534         {
535                 case PreferencesThread::PLAYBACK_A:
536                 case PreferencesThread::PLAYBACK_B:
537                         playback_config->load_defaults(mwindow->defaults,
538                                 number == PreferencesThread::PLAYBACK_A ? 0 : 1);
539                         add_subwindow(dialog = new PlaybackPrefs(mwindow, this, number));
540                         break;
541
542                 case PreferencesThread::RECORD:
543                         add_subwindow(dialog = new RecordPrefs(mwindow, this));
544                         break;
545
546                 case PreferencesThread::PERFORMANCE:
547                         add_subwindow(dialog = new PerformancePrefs(mwindow, this));
548                         break;
549
550                 case PreferencesThread::INTERFACE:
551                         add_subwindow(dialog = new InterfacePrefs(mwindow, this));
552                         break;
553
554                 case PreferencesThread::APPEARANCE:
555                         add_subwindow(dialog = new AppearancePrefs(mwindow, this));
556                         break;
557
558                 case PreferencesThread::ABOUT:
559                         add_subwindow(dialog = new AboutPrefs(mwindow, this));
560                         break;
561         }
562
563 //PRINT_TRACE
564         if(dialog)
565         {
566                 dialog->draw_top_background(this, 0, 0, dialog->get_w(), dialog->get_h());
567 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
568                 dialog->create_objects();
569 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
570                 dialog->lower_window();
571                 dialog->show_window(0);
572         }
573
574         if(dialog2)
575         {
576                 dialog2->hide_window(0);
577                 delete dialog2;
578         }
579
580         return 0;
581 }
582
583
584 PreferencesButton::PreferencesButton(MWindow *mwindow,
585         PreferencesThread *thread,
586         int x,
587         int y,
588         int category,
589         const char *text,
590         VFrame **images)
591  : BC_GenericButton(x, y, text, images)
592 {
593         this->mwindow = mwindow;
594         this->thread = thread;
595         this->category = category;
596 }
597
598 int PreferencesButton::handle_event()
599 {
600         thread->window->set_current_dialog(category);
601         return 1;
602 }
603
604
605 PreferencesDialog::PreferencesDialog(MWindow *mwindow,
606         PreferencesWindow *pwindow)
607  : BC_SubWindow(xS(10), yS(40),
608         pwindow->get_w() - xS(20),
609         pwindow->get_h() - BC_GenericButton::calculate_h() - yS(10 + 40))
610 {
611         this->pwindow = pwindow;
612         this->mwindow = mwindow;
613         preferences = pwindow->thread->preferences;
614 }
615
616 PreferencesDialog::~PreferencesDialog()
617 {
618 }
619
620
621 // ============================== category window
622
623 PreferencesApply::PreferencesApply(MWindow *mwindow, PreferencesThread *thread)
624  : BC_GenericButton(thread->window->get_w() / 2 - BC_GenericButton::calculate_w(thread->window, _("Apply")) / 2,
625         thread->window->get_h() - BC_GenericButton::calculate_h() - yS(10),
626         _("Apply"))
627 {
628         this->mwindow = mwindow;
629         this->thread = thread;
630 }
631 int PreferencesApply::handle_event()
632 {
633         thread->apply_settings();
634         mwindow->save_defaults();
635         return 1;
636 }
637 int PreferencesApply::resize_event(int w, int h)
638 {
639         reposition_window(w/2 - get_w()/2, h-get_h()-yS(10));
640         return 1;
641 }
642
643
644 PreferencesOK::PreferencesOK(MWindow *mwindow, PreferencesThread *thread)
645  : BC_GenericButton(xS(10),
646         thread->window->get_h() - BC_GenericButton::calculate_h() - yS(10),
647         _("OK"))
648 {
649         this->mwindow = mwindow;
650         this->thread = thread;
651 }
652 int PreferencesOK::keypress_event()
653 {
654         if(get_keypress() == RETURN)
655         {
656                 thread->window->set_done(0);
657                 return 1;
658         }
659         return 0;
660 }
661 int PreferencesOK::handle_event()
662 {
663         thread->window->set_done(0);
664         return 1;
665 }
666 int PreferencesOK::resize_event(int w, int h)
667 {
668         reposition_window(xS(10), h-get_h()-yS(10));
669         return 1;
670 }
671
672
673 PreferencesCancel::PreferencesCancel(MWindow *mwindow, PreferencesThread *thread)
674  : BC_GenericButton(thread->window->get_w() - BC_GenericButton::calculate_w(thread->window, _("Cancel")) - xS(10),
675         thread->window->get_h() - BC_GenericButton::calculate_h() - yS(10),
676         _("Cancel"))
677 {
678         this->mwindow = mwindow;
679         this->thread = thread;
680 }
681 int PreferencesCancel::keypress_event()
682 {
683         if(get_keypress() == ESC)
684         {
685                 thread->window->set_done(1);
686                 return 1;
687         }
688         return 0;
689 }
690 int PreferencesCancel::handle_event()
691 {
692         thread->window->set_done(1);
693         return 1;
694 }
695 int PreferencesCancel::resize_event(int w, int h)
696 {
697         reposition_window(w-get_w()-xS(10), h-get_h()-yS(10));
698         return 1;
699 }
700
701
702 PreferencesCategory::PreferencesCategory(MWindow *mwindow, PreferencesThread *thread, int x, int y)
703  : BC_PopupTextBox(thread->window,
704                 &thread->window->categories,
705                 thread->category_to_text(thread->current_dialog),
706                 x, y, xS(200), yS(150))
707 {
708         this->mwindow = mwindow;
709         this->thread = thread;
710 }
711
712 PreferencesCategory::~PreferencesCategory()
713 {
714 }
715
716 int PreferencesCategory::handle_event()
717 {
718 SET_TRACE
719         thread->window->set_current_dialog(thread->text_to_category(get_text()));
720 SET_TRACE
721         return 1;
722 }