graphic art overlay modes, pref window resize, submenu fixes
[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 w = WIDTH, h = HEIGHT;
131         int min_w = mwindow->theme->preferencescategory_x;
132         for(int i = 0; i < CATEGORIES; i++) {
133                 min_w += PreferencesButton::calculate_w(mwindow->gui, category_to_text(i)) -
134                         mwindow->theme->preferences_category_overlap;
135         }
136         if( w < min_w ) w = min_w;
137         int x = scr_x + scr_w / 2 - w / 2;
138         int y = scr_h / 2 - h / 2;
139
140         window = new PreferencesWindow(mwindow, this, x, y, w, h);
141         window->create_objects();
142         mwindow->gui->unlock_window();
143
144         thread_running = 1;
145         return window;
146 }
147
148 void PreferencesThread::handle_close_event(int result)
149 {
150         thread_running = 0;
151         if(!result)
152         {
153                 apply_settings();
154                 mwindow->save_defaults();
155         }
156
157         window = 0;
158         delete preferences;
159         edl->Garbage::remove_user();
160         preferences = 0;
161         edl = 0;
162
163         mwindow->defaults->update("DEFAULTPREF", current_dialog);
164         if( mwindow->restart() )
165                 mwindow->gui->set_done(0);
166 }
167
168
169
170 int PreferencesThread::update_framerate()
171 {
172         if(thread_running)
173         {
174                 lock_gui("PreferencesThread::update_framerate");
175                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
176                 if(window) window->update_framerate();
177                 unlock_gui();
178         }
179         return 0;
180 }
181
182
183 void PreferencesThread::update_rates()
184 {
185         if(thread_running)
186         {
187                 lock_gui("PreferencesThread::update_framerate");
188                 PreferencesWindow *window = (PreferencesWindow*)get_gui();
189                 if(window) window->update_rates();
190                 unlock_gui();
191         }
192 }
193
194 int PreferencesThread::apply_settings()
195 {
196 // Compare sessions
197
198         PlaybackConfig *this_playback_config = edl->session->playback_config;
199         AudioOutConfig *this_aconfig = this_playback_config->aconfig;
200         VideoOutConfig *this_vconfig = this_playback_config->vconfig;
201         PlaybackConfig *playback_config = mwindow->edl->session->playback_config;
202         AudioOutConfig *aconfig = playback_config->aconfig;
203         VideoOutConfig *vconfig = playback_config->vconfig;
204
205         rerender =
206                 edl->session->need_rerender(mwindow->edl->session) ||
207                 (preferences->force_uniprocessor != mwindow->preferences->force_uniprocessor) ||
208                 this_playback_config->active_config != playback_config->active_config ||
209                 (*this_aconfig != *aconfig) || (*this_vconfig != *vconfig) ||
210                 !preferences->brender_asset->equivalent(*mwindow->preferences->brender_asset, 0, 1);
211
212         if( strcmp(preferences->theme, mwindow->preferences->theme) != 0 )
213                 mwindow->restart_status = -1; // reload, need new bcresources
214
215         mwindow->edl->copy_session(edl, 1);
216         mwindow->preferences->copy_from(preferences);
217         mwindow->init_brender();
218
219         BC_Signals::set_catch_segv(mwindow->preferences->trap_sigsegv);
220         BC_Signals::set_catch_intr(mwindow->preferences->trap_sigintr);
221         BC_WindowBase::get_resources()->popupmenu_btnup = mwindow->preferences->popupmenu_btnup;
222
223         mwindow->reset_android_remote();
224         mwindow->gui->ffmpeg_toggle->update(mwindow->preferences->ffmpeg_early_probe);
225         mwindow->gui->ffmpeg_toggle->set_tooltip( mwindow->preferences->ffmpeg_early_probe ?
226                 _("Try FFMpeg first") : _("Try FFMpeg last") );
227         mwindow->gui->mainshbtns->load(mwindow->preferences);
228         double tc_position = 
229                 mwindow->edl->session->get_frame_offset() / mwindow->edl->session->frame_rate;
230         mwindow->gui->mainclock->set_position_offset(tc_position);
231
232 //edl->session->recording_format->dump();
233 //mwindow->edl->session->recording_format->dump();
234
235         if(((mwindow->edl->session->output_w % 4) ||
236                 (mwindow->edl->session->output_h % 4)) &&
237                 mwindow->edl->session->playback_config->vconfig->driver == PLAYBACK_X11_GL)
238         {
239                 MainError::show_error(
240                         _("This project's dimensions are not multiples of 4 so\n"
241                         "it can't be rendered by OpenGL."));
242         }
243
244
245         if(redraw_meters)
246         {
247                 mwindow->cwindow->gui->lock_window("PreferencesThread::apply_settings");
248                 mwindow->cwindow->gui->meters->change_format(edl->session->meter_format,
249                         edl->session->min_meter_db,
250                         edl->session->max_meter_db);
251                 mwindow->cwindow->gui->unlock_window();
252
253
254
255                 for(int i = 0; i < mwindow->vwindows.size(); i++) {
256                         VWindow *vwindow = mwindow->vwindows.get(i);
257                         if( !vwindow->is_running() ) continue;
258                         vwindow->gui->lock_window("PreferencesThread::apply_settings");
259                         vwindow->gui->meters->change_format(edl->session->meter_format,
260                                 edl->session->min_meter_db,
261                                 edl->session->max_meter_db);
262                         vwindow->gui->unlock_window();
263
264                 }
265
266
267                 mwindow->gui->lock_window("PreferencesThread::apply_settings 1");
268                 mwindow->gui->set_meter_format(edl->session->meter_format,
269                         edl->session->min_meter_db,
270                         edl->session->max_meter_db);
271                 mwindow->gui->unlock_window();
272
273
274
275                 mwindow->lwindow->gui->lock_window("PreferencesThread::apply_settings");
276                 mwindow->lwindow->gui->panel->change_format(edl->session->meter_format,
277                         edl->session->min_meter_db,
278                         edl->session->max_meter_db);
279                 mwindow->lwindow->gui->unlock_window();
280         }
281
282         if(redraw_overlays)
283         {
284                 mwindow->gui->lock_window("PreferencesThread::apply_settings 2");
285                 mwindow->gui->draw_overlays(1);
286                 mwindow->gui->unlock_window();
287         }
288
289         if(redraw_times)
290         {
291                 mwindow->gui->lock_window("PreferencesThread::apply_settings 3");
292                 mwindow->gui->update(0, 0, 1, 0, 0, 1, 0);
293                 mwindow->gui->redraw_time_dependancies();
294                 mwindow->gui->unlock_window();
295         }
296
297         if(rerender)
298         {
299 //printf("PreferencesThread::apply_settings 1\n");
300 // This doesn't stop and restart, only reloads the assets before
301 // the next play command.
302                 mwindow->cwindow->playback_engine->que->send_command(CURRENT_FRAME,
303                         CHANGE_ALL,
304                         mwindow->edl,
305                         1);
306 //printf("PreferencesThread::apply_settings 10\n");
307         }
308
309         if(redraw_times || redraw_overlays)
310         {
311                 mwindow->gui->lock_window("PreferencesThread::apply_settings 4");
312                 mwindow->gui->flush();
313                 mwindow->gui->unlock_window();
314         }
315
316         return 0;
317 }
318
319 const char* PreferencesThread::category_to_text(int category)
320 {
321         PlaybackConfig *playback_config = edl->session->playback_config;
322         switch(category)
323         {
324                 case PLAYBACK_A:
325                         return playback_config->active_config==0 ?
326                                  _("*Playback A") : _("Playback A");
327                 case PLAYBACK_B:
328                         return playback_config->active_config==1 ?
329                                  _("*Playback B") : _("Playback B");
330                 case RECORD:
331                         return _("Recording");
332                 case PERFORMANCE:
333                         return _("Performance");
334                 case INTERFACE:
335                         return _("Interface");
336                 case ABOUT:
337                         return _("About");
338         }
339         return "";
340 }
341
342 int PreferencesThread::text_to_category(const char *category)
343 {
344 SET_TRACE
345         int min_result = -1, result, result_num = 0;
346         for(int i = 0; i < CATEGORIES; i++)
347         {
348                 result = labs(strcmp(category_to_text(i), category));
349                 if(result < min_result || min_result < 0)
350                 {
351                         min_result = result;
352                         result_num = i;
353                 }
354         }
355 SET_TRACE
356         return result_num;
357 }
358
359
360
361
362
363
364
365
366 PreferencesWindow::PreferencesWindow(MWindow *mwindow,
367         PreferencesThread *thread, int x, int y, int w, int h)
368  : BC_Window(_(PROGRAM_NAME ": Preferences"), x,y, w,h,w,h, 1)
369 {
370         this->mwindow = mwindow;
371         this->thread = thread;
372         dialog = 0;
373         category = 0;
374 }
375
376 PreferencesWindow::~PreferencesWindow()
377 {
378         lock_window("PreferencesWindow::~PreferencesWindow");
379         delete category;
380
381
382         if(dialog) delete dialog;
383
384
385         for(int i = 0; i < categories.total; i++)
386                 delete categories.values[i];
387         unlock_window();
388 }
389
390 void PreferencesWindow::create_objects()
391 {
392         BC_Button *button;
393
394         lock_window("PreferencesWindow::create_objects");
395         set_icon(mwindow->theme->get_image("mwindow_icon"));
396         mwindow->theme->draw_preferences_bg(this);
397         flash();
398
399         int x = mwindow->theme->preferencescategory_x;
400         int y = mwindow->theme->preferencescategory_y;
401         for(int i = 0; i < CATEGORIES; i++)
402         {
403                 add_subwindow(category_button[i] = new PreferencesButton(mwindow,
404                         thread,
405                         x,
406                         y,
407                         i,
408                         thread->category_to_text(i),
409                         (i == thread->current_dialog) ?
410                                 mwindow->theme->get_image_set("category_button_checked") :
411                                 mwindow->theme->get_image_set("category_button")));
412                 x += category_button[i]->get_w() -
413                         mwindow->theme->preferences_category_overlap;
414         }
415
416
417 //      for(int i = 0; i < CATEGORIES; i++)
418 //              categories.append(new BC_ListBoxItem(thread->category_to_text(i)));
419 //      category = new PreferencesCategory(mwindow,
420 //              thread,
421 //              mwindow->theme->preferencescategory_x,
422 //              mwindow->theme->preferencescategory_y);
423 //      category->create_objects();
424
425
426         add_subwindow(button = new PreferencesOK(mwindow, thread));
427         add_subwindow(new PreferencesApply(mwindow, thread));
428         add_subwindow(new PreferencesCancel(mwindow, thread));
429
430         set_current_dialog(thread->current_dialog);
431
432         show_window();
433         unlock_window();
434 }
435
436 int PreferencesWindow::update_framerate()
437 {
438         lock_window("PreferencesWindow::update_framerate");
439         if(thread->current_dialog < PreferencesThread::RECORD)
440         {
441                 dialog->draw_framerate(1);
442 //              flash();
443         }
444         unlock_window();
445         return 0;
446 }
447
448
449 void PreferencesWindow::update_rates()
450 {
451         lock_window("PreferencesWindow::update_rates");
452         if(thread->current_dialog == PreferencesThread::PERFORMANCE)
453         {
454                 dialog->update_rates();
455         }
456         unlock_window();
457 }
458
459
460 int PreferencesWindow::set_current_dialog(int number)
461 {
462         if(dialog) delete dialog;
463         dialog = 0;
464         thread->current_dialog = number;
465
466 //PRINT_TRACE
467         PreferencesDialog *dialog2 = dialog;
468         dialog = 0;
469 //PRINT_TRACE
470
471 // Redraw category buttons
472         for(int i = 0; i < CATEGORIES; i++)
473         {
474                 if(i == number)
475                 {
476                         category_button[i]->set_images(
477                                 mwindow->theme->get_image_set("category_button_checked"));
478                 }
479                 else
480                 {
481                         category_button[i]->set_images(
482                                 mwindow->theme->get_image_set("category_button"));
483                 }
484                 category_button[i]->draw_face(0);
485
486 // Copy face to background for next button's overlap.
487 // Still can't do state changes right.
488         }
489
490
491 //PRINT_TRACE
492         PlaybackConfig *playback_config = thread->edl->session->playback_config;
493         switch(number)
494         {
495                 case PreferencesThread::PLAYBACK_A:
496                 case PreferencesThread::PLAYBACK_B:
497                         playback_config->load_defaults(mwindow->defaults,
498                                 number == PreferencesThread::PLAYBACK_A ? 0 : 1);
499                         add_subwindow(dialog = new PlaybackPrefs(mwindow, this, number));
500                         break;
501
502                 case PreferencesThread::RECORD:
503                         add_subwindow(dialog = new RecordPrefs(mwindow, this));
504                         break;
505
506                 case PreferencesThread::PERFORMANCE:
507                         add_subwindow(dialog = new PerformancePrefs(mwindow, this));
508                         break;
509
510                 case PreferencesThread::INTERFACE:
511                         add_subwindow(dialog = new InterfacePrefs(mwindow, this));
512                         break;
513
514                 case PreferencesThread::ABOUT:
515                         add_subwindow(dialog = new AboutPrefs(mwindow, this));
516                         break;
517         }
518
519 //PRINT_TRACE
520         if(dialog)
521         {
522                 dialog->draw_top_background(this, 0, 0, dialog->get_w(), dialog->get_h());
523 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
524                 dialog->create_objects();
525 //printf("PreferencesWindow::set_current_dialog %d\n", __LINE__);
526                 dialog->lower_window();
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 int PreferencesApply::handle_event()
607 {
608         thread->apply_settings();
609         return 1;
610 }
611 int PreferencesApply::resize_event(int w, int h)
612 {
613         reposition_window(w/2 - get_w()/2, h-get_h()-10);
614         return 1;
615 }
616
617
618
619
620 PreferencesOK::PreferencesOK(MWindow *mwindow, PreferencesThread *thread)
621  : BC_GenericButton(10,
622         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
623         _("OK"))
624 {
625         this->mwindow = mwindow;
626         this->thread = thread;
627 }
628 int PreferencesOK::keypress_event()
629 {
630         if(get_keypress() == RETURN)
631         {
632                 thread->window->set_done(0);
633                 return 1;
634         }
635         return 0;
636 }
637 int PreferencesOK::handle_event()
638 {
639         thread->window->set_done(0);
640         return 1;
641 }
642 int PreferencesOK::resize_event(int w, int h)
643 {
644         reposition_window(10, h-get_h()-10);
645         return 1;
646 }
647
648
649
650 PreferencesCancel::PreferencesCancel(MWindow *mwindow, PreferencesThread *thread)
651  : BC_GenericButton(thread->window->get_w() - BC_GenericButton::calculate_w(thread->window, _("Cancel")) - 10,
652         thread->window->get_h() - BC_GenericButton::calculate_h() - 10,
653         _("Cancel"))
654 {
655         this->mwindow = mwindow;
656         this->thread = thread;
657 }
658 int PreferencesCancel::keypress_event()
659 {
660         if(get_keypress() == ESC)
661         {
662                 thread->window->set_done(1);
663                 return 1;
664         }
665         return 0;
666 }
667 int PreferencesCancel::handle_event()
668 {
669         thread->window->set_done(1);
670         return 1;
671 }
672 int PreferencesCancel::resize_event(int w, int h)
673 {
674         reposition_window(w-get_w()-10, h-get_h()-10);
675         return 1;
676 }
677
678
679
680
681
682
683
684
685
686
687 PreferencesCategory::PreferencesCategory(MWindow *mwindow, PreferencesThread *thread, int x, int y)
688  : BC_PopupTextBox(thread->window,
689                 &thread->window->categories,
690                 thread->category_to_text(thread->current_dialog),
691                 x,
692                 y,
693                 200,
694                 150)
695 {
696         this->mwindow = mwindow;
697         this->thread = thread;
698 }
699
700 PreferencesCategory::~PreferencesCategory()
701 {
702 }
703
704 int PreferencesCategory::handle_event()
705 {
706 SET_TRACE
707         thread->window->set_current_dialog(thread->text_to_category(get_text()));
708 SET_TRACE
709         return 1;
710 }