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