BOOST_FOREACH.
[dcpomatic.git] / src / wx / content_colour_conversion_dialog.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "wx_util.h"
22 #include "check_box.h"
23 #include "content_colour_conversion_dialog.h"
24 #include "colour_conversion_editor.h"
25 #include "lib/colour_conversion.h"
26 #include "lib/config.h"
27 #include "lib/util.h"
28 #include <wx/statline.h>
29 #include <iostream>
30
31 using std::string;
32 using std::vector;
33 using std::cout;
34 using boost::optional;
35
36 ContentColourConversionDialog::ContentColourConversionDialog (wxWindow* parent, bool yuv)
37         : wxDialog (parent, wxID_ANY, _("Colour conversion"))
38         , _editor (new ColourConversionEditor (this, yuv))
39         , _setting (false)
40 {
41         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
42         SetSizer (overall_sizer);
43
44         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_Y_GAP - 2, DCPOMATIC_SIZER_X_GAP);
45         _preset_check = new CheckBox (this, _("Use preset"));
46         table->Add (_preset_check, 0, wxALIGN_CENTER_VERTICAL);
47         _preset_choice = new wxChoice (this, wxID_ANY);
48         table->Add (_preset_choice);
49
50         overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
51         overall_sizer->Add (new wxStaticLine (this, wxID_ANY), 0, wxEXPAND);
52         overall_sizer->Add (_editor);
53
54         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
55         if (buttons) {
56                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
57         }
58
59         overall_sizer->Layout ();
60         overall_sizer->SetSizeHints (this);
61
62         _preset_check->Bind (wxEVT_CHECKBOX, boost::bind (&ContentColourConversionDialog::preset_check_clicked, this));
63         _preset_choice->Bind (wxEVT_CHOICE, boost::bind (&ContentColourConversionDialog::preset_choice_changed, this));
64
65         _editor_connection = _editor->Changed.connect (boost::bind (&ContentColourConversionDialog::check_for_preset, this));
66
67         for (auto const& i: PresetColourConversion::all ()) {
68                 _preset_choice->Append (std_to_wx (i.name));
69         }
70 }
71
72 ColourConversion
73 ContentColourConversionDialog::get () const
74 {
75         return _editor->get ();
76 }
77
78 void
79 ContentColourConversionDialog::set (ColourConversion c)
80 {
81         _setting = true;
82         _editor->set (c);
83         _setting = false;
84
85         check_for_preset ();
86 }
87
88 void
89 ContentColourConversionDialog::check_for_preset ()
90 {
91         if (_setting) {
92                 return;
93         }
94
95         optional<size_t> preset = _editor->get().preset ();
96
97         _preset_check->SetValue (static_cast<bool>(preset));
98         _preset_choice->Enable (static_cast<bool>(preset));
99         if (preset) {
100                 _preset_choice->SetSelection (preset.get ());
101         } else {
102                 _preset_choice->SetSelection (-1);
103         }
104 }
105
106 void
107 ContentColourConversionDialog::preset_check_clicked ()
108 {
109         if (_preset_check->GetValue ()) {
110                 _preset_choice->SetSelection (0);
111                 preset_choice_changed ();
112         } else {
113                 _preset_choice->SetSelection (-1);
114                 _preset_choice->Enable (false);
115         }
116 }
117
118 void
119 ContentColourConversionDialog::preset_choice_changed ()
120 {
121         vector<PresetColourConversion> presets = PresetColourConversion::all ();
122         int const s = _preset_choice->GetCurrentSelection();
123         if (s != -1) {
124                 set (presets[s].conversion);
125         }
126 }