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