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