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