2010-07-19 19:53:29 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file preferences_base.cpp
|
|
|
|
/// @brief Base preferences dialogue classes
|
|
|
|
/// @ingroup configuration_ui
|
|
|
|
|
2013-01-30 04:35:37 +01:00
|
|
|
#include "preferences_base.h"
|
|
|
|
|
|
|
|
#include "colour_button.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "preferences.h"
|
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
#include <libaegisub/exception.h>
|
2013-01-30 04:35:37 +01:00
|
|
|
#include <libaegisub/path.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2010-07-19 19:53:29 +02:00
|
|
|
|
|
|
|
#include <wx/checkbox.h>
|
|
|
|
#include <wx/combobox.h>
|
2011-10-28 22:15:10 +02:00
|
|
|
#include <wx/dirdlg.h>
|
2011-10-11 02:06:44 +02:00
|
|
|
#include <wx/event.h>
|
|
|
|
#include <wx/fontdlg.h>
|
|
|
|
#include <wx/listctrl.h>
|
2010-07-19 19:53:29 +02:00
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/spinctrl.h>
|
|
|
|
#include <wx/stattext.h>
|
|
|
|
#include <wx/treebook.h>
|
|
|
|
|
2014-04-04 02:24:27 +02:00
|
|
|
#undef Bool
|
|
|
|
|
2012-02-28 02:22:40 +01:00
|
|
|
#define OPTION_UPDATER(type, evttype, opt, body) \
|
|
|
|
class type { \
|
|
|
|
std::string name; \
|
|
|
|
Preferences *parent; \
|
|
|
|
public: \
|
|
|
|
type(std::string const& n, Preferences *p) : name(n), parent(p) { } \
|
|
|
|
void operator()(evttype& evt) { \
|
|
|
|
evt.Skip(); \
|
2014-05-23 00:40:16 +02:00
|
|
|
parent->SetOption(agi::make_unique<agi::opt>(name, body)); \
|
2012-02-28 02:22:40 +01:00
|
|
|
} \
|
2010-08-26 20:38:20 +02:00
|
|
|
}
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2012-12-23 00:18:38 +01:00
|
|
|
OPTION_UPDATER(StringUpdater, wxCommandEvent, OptionValueString, from_wx(evt.GetString()));
|
2011-12-24 01:36:19 +01:00
|
|
|
OPTION_UPDATER(IntUpdater, wxSpinEvent, OptionValueInt, evt.GetInt());
|
|
|
|
OPTION_UPDATER(IntCBUpdater, wxCommandEvent, OptionValueInt, evt.GetInt());
|
|
|
|
OPTION_UPDATER(DoubleUpdater, wxSpinEvent, OptionValueDouble, evt.GetInt());
|
|
|
|
OPTION_UPDATER(BoolUpdater, wxCommandEvent, OptionValueBool, !!evt.GetInt());
|
2013-01-13 19:32:13 +01:00
|
|
|
OPTION_UPDATER(ColourUpdater, wxThreadEvent, OptionValueColor, evt.GetPayload<agi::Color>());
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
static void browse_button(wxTextCtrl *ctrl) {
|
2013-11-21 18:13:36 +01:00
|
|
|
wxDirDialog dlg(nullptr, _("Please choose the folder:"), config::path->Decode(from_wx(ctrl->GetValue())).wstring());
|
2011-10-11 02:06:44 +02:00
|
|
|
if (dlg.ShowModal() == wxID_OK) {
|
2012-01-08 02:34:37 +01:00
|
|
|
wxString dir = dlg.GetPath();
|
2011-10-11 02:06:44 +02:00
|
|
|
if (!dir.empty())
|
|
|
|
ctrl->SetValue(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void font_button(Preferences *parent, wxTextCtrl *name, wxSpinCtrl *size) {
|
|
|
|
wxFont font;
|
|
|
|
font.SetFaceName(name->GetValue());
|
|
|
|
font.SetPointSize(size->GetValue());
|
|
|
|
font = wxGetFontFromUser(parent, font);
|
|
|
|
if (font.IsOk()) {
|
|
|
|
name->SetValue(font.GetFaceName());
|
2012-01-20 16:14:41 +01:00
|
|
|
size->SetValue(font.GetPointSize());
|
2013-12-12 03:25:13 +01:00
|
|
|
// wxGTK doesn't generate wxEVT_SPINCTRL from SetValue
|
|
|
|
wxSpinEvent evt(wxEVT_SPINCTRL);
|
2012-01-20 16:14:41 +01:00
|
|
|
evt.SetInt(font.GetPointSize());
|
|
|
|
size->ProcessWindowEvent(evt);
|
2011-10-11 02:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 20:38:20 +02:00
|
|
|
OptionPage::OptionPage(wxTreebook *book, Preferences *parent, wxString name, int style)
|
2011-10-11 02:06:44 +02:00
|
|
|
: wxScrolled<wxPanel>(book, -1, wxDefaultPosition, wxDefaultSize, wxVSCROLL)
|
2011-12-26 23:20:49 +01:00
|
|
|
, sizer(new wxBoxSizer(wxVERTICAL))
|
2010-08-26 20:38:20 +02:00
|
|
|
, parent(parent)
|
|
|
|
{
|
2011-10-11 02:06:44 +02:00
|
|
|
if (style & PAGE_SUB)
|
2010-07-19 19:53:29 +02:00
|
|
|
book->AddSubPage(this, name, true);
|
2011-10-11 02:06:44 +02:00
|
|
|
else
|
2010-07-19 19:53:29 +02:00
|
|
|
book->AddPage(this, name, true);
|
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
if (style & PAGE_SCROLL)
|
2010-07-19 19:53:29 +02:00
|
|
|
SetScrollbars(0, 20, 0, 50);
|
2011-10-11 02:06:44 +02:00
|
|
|
else
|
2010-07-19 19:53:29 +02:00
|
|
|
SetScrollbars(0, 0, 0, 0);
|
2012-01-20 16:14:47 +01:00
|
|
|
DisableKeyboardScrolling();
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
template<class T>
|
|
|
|
void OptionPage::Add(wxSizer *sizer, wxString const& label, T *control) {
|
2012-05-15 16:07:14 +02:00
|
|
|
sizer->Add(new wxStaticText(this, -1, label), 1, wxALIGN_CENTRE_VERTICAL);
|
2011-11-16 22:59:39 +01:00
|
|
|
sizer->Add(control, wxSizerFlags().Expand());
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
void OptionPage::CellSkip(wxFlexGridSizer *flex) {
|
2012-04-27 21:07:36 +02:00
|
|
|
flex->AddStretchSpacer();
|
2011-10-11 02:06:44 +02:00
|
|
|
}
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2012-02-28 02:22:40 +01:00
|
|
|
wxControl *OptionPage::OptionAdd(wxFlexGridSizer *flex, const wxString &name, const char *opt_name, double min, double max, double inc) {
|
2012-04-11 05:43:08 +02:00
|
|
|
parent->AddChangeableOption(opt_name);
|
2014-05-31 05:32:07 +02:00
|
|
|
const auto opt = OPT_GET(opt_name);
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
switch (opt->GetType()) {
|
2014-03-31 18:24:56 +02:00
|
|
|
case agi::OptionType::Bool: {
|
2014-05-31 05:32:07 +02:00
|
|
|
auto cb = new wxCheckBox(this, -1, name);
|
2010-07-19 19:53:29 +02:00
|
|
|
flex->Add(cb, 1, wxEXPAND, 0);
|
|
|
|
cb->SetValue(opt->GetBool());
|
2013-12-12 03:25:13 +01:00
|
|
|
cb->Bind(wxEVT_CHECKBOX, BoolUpdater(opt_name, parent));
|
2012-02-28 02:22:40 +01:00
|
|
|
return cb;
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case agi::OptionType::Int: {
|
2014-05-31 05:32:07 +02:00
|
|
|
auto sc = new wxSpinCtrl(this, -1, std::to_wstring((int)opt->GetInt()), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, min, max, opt->GetInt());
|
2013-12-12 03:25:13 +01:00
|
|
|
sc->Bind(wxEVT_SPINCTRL, IntUpdater(opt_name, parent));
|
2011-10-11 02:06:44 +02:00
|
|
|
Add(flex, name, sc);
|
2012-02-28 02:22:40 +01:00
|
|
|
return sc;
|
2010-08-26 20:38:20 +02:00
|
|
|
}
|
2011-10-11 02:06:44 +02:00
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case agi::OptionType::Double: {
|
2014-05-31 05:32:07 +02:00
|
|
|
auto scd = new wxSpinCtrlDouble(this, -1, std::to_wstring(opt->GetDouble()), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, min, max, opt->GetDouble(), inc);
|
2013-12-12 03:25:13 +01:00
|
|
|
scd->Bind(wxEVT_SPINCTRL, DoubleUpdater(opt_name, parent));
|
2011-10-11 02:06:44 +02:00
|
|
|
Add(flex, name, scd);
|
2012-02-28 02:22:40 +01:00
|
|
|
return scd;
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case agi::OptionType::String: {
|
2014-05-31 05:32:07 +02:00
|
|
|
auto text = new wxTextCtrl(this, -1 , to_wx(opt->GetString()));
|
2013-12-12 03:25:13 +01:00
|
|
|
text->Bind(wxEVT_TEXT, StringUpdater(opt_name, parent));
|
2011-10-11 02:06:44 +02:00
|
|
|
Add(flex, name, text);
|
2012-02-28 02:22:40 +01:00
|
|
|
return text;
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case agi::OptionType::Color: {
|
2013-11-21 18:13:36 +01:00
|
|
|
auto cb = new ColourButton(this, wxSize(40,10), false, opt->GetColor());
|
2013-01-13 19:32:13 +01:00
|
|
|
cb->Bind(EVT_COLOR, ColourUpdater(opt_name, parent));
|
2011-10-11 02:06:44 +02:00
|
|
|
Add(flex, name, cb);
|
2012-02-28 02:22:40 +01:00
|
|
|
return cb;
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2014-05-31 05:32:07 +02:00
|
|
|
throw agi::InternalError("Unsupported type");
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
void OptionPage::OptionChoice(wxFlexGridSizer *flex, const wxString &name, const wxArrayString &choices, const char *opt_name) {
|
2012-04-11 05:43:08 +02:00
|
|
|
parent->AddChangeableOption(opt_name);
|
2014-05-31 05:32:07 +02:00
|
|
|
const auto opt = OPT_GET(opt_name);
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto cb = new wxComboBox(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, choices, wxCB_READONLY | wxCB_DROPDOWN);
|
2011-10-11 02:06:44 +02:00
|
|
|
Add(flex, name, cb);
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2010-08-26 20:38:20 +02:00
|
|
|
switch (opt->GetType()) {
|
2014-03-31 18:24:56 +02:00
|
|
|
case agi::OptionType::Int: {
|
2010-11-11 05:48:29 +01:00
|
|
|
int val = opt->GetInt();
|
2013-09-20 01:25:53 +02:00
|
|
|
cb->Select(val < (int)choices.size() ? val : 0);
|
2013-12-12 03:25:13 +01:00
|
|
|
cb->Bind(wxEVT_COMBOBOX, IntCBUpdater(opt_name, parent));
|
2010-07-19 19:53:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-03-31 18:24:56 +02:00
|
|
|
case agi::OptionType::String: {
|
2012-12-23 00:18:38 +01:00
|
|
|
wxString val(to_wx(opt->GetString()));
|
2011-10-13 01:08:29 +02:00
|
|
|
if (cb->FindString(val) != wxNOT_FOUND)
|
|
|
|
cb->SetStringSelection(val);
|
2012-05-13 02:58:11 +02:00
|
|
|
else if (!choices.empty())
|
2011-10-13 01:08:29 +02:00
|
|
|
cb->SetSelection(0);
|
2013-12-12 03:25:13 +01:00
|
|
|
cb->Bind(wxEVT_COMBOBOX, StringUpdater(opt_name, parent));
|
2010-07-19 19:53:29 +02:00
|
|
|
break;
|
2011-10-13 01:08:29 +02:00
|
|
|
}
|
2011-10-11 02:06:44 +02:00
|
|
|
|
2010-07-19 19:53:29 +02:00
|
|
|
default:
|
2014-05-31 05:32:07 +02:00
|
|
|
throw agi::InternalError("Unsupported type");
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxFlexGridSizer* OptionPage::PageSizer(wxString name) {
|
2014-05-31 05:32:07 +02:00
|
|
|
auto tmp_sizer = new wxStaticBoxSizer(wxHORIZONTAL, this, name);
|
2010-07-19 19:53:29 +02:00
|
|
|
sizer->Add(tmp_sizer, 0,wxEXPAND, 5);
|
2013-11-21 18:13:36 +01:00
|
|
|
auto flex = new wxFlexGridSizer(2,5,5);
|
2010-07-19 19:53:29 +02:00
|
|
|
flex->AddGrowableCol(0,1);
|
|
|
|
tmp_sizer->Add(flex, 1, wxEXPAND, 5);
|
|
|
|
sizer->AddSpacer(8);
|
|
|
|
return flex;
|
|
|
|
}
|
|
|
|
|
2012-02-28 02:22:40 +01:00
|
|
|
void OptionPage::OptionBrowse(wxFlexGridSizer *flex, const wxString &name, const char *opt_name, wxControl *enabler, bool do_enable) {
|
2012-04-11 05:43:08 +02:00
|
|
|
parent->AddChangeableOption(opt_name);
|
2014-05-31 05:32:07 +02:00
|
|
|
const auto opt = OPT_GET(opt_name);
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
if (opt->GetType() != agi::OptionType::String)
|
2014-05-31 05:32:07 +02:00
|
|
|
throw agi::InternalError("Option must be agi::OptionType::String for BrowseButton.");
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto text = new wxTextCtrl(this, -1 , to_wx(opt->GetString()));
|
2012-05-15 16:06:34 +02:00
|
|
|
text->SetMinSize(wxSize(160, -1));
|
2013-12-12 03:25:13 +01:00
|
|
|
text->Bind(wxEVT_TEXT, StringUpdater(opt_name, parent));
|
2011-10-11 02:06:44 +02:00
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto browse = new wxButton(this, -1, _("Browse..."));
|
2013-12-12 03:25:13 +01:00
|
|
|
browse->Bind(wxEVT_BUTTON, std::bind(browse_button, text));
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto button_sizer = new wxBoxSizer(wxHORIZONTAL);
|
2011-10-11 02:06:44 +02:00
|
|
|
button_sizer->Add(text, wxSizerFlags(1).Expand());
|
|
|
|
button_sizer->Add(browse, wxSizerFlags().Expand());
|
2010-07-19 19:53:29 +02:00
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
Add(flex, name, button_sizer);
|
2012-02-28 02:22:40 +01:00
|
|
|
|
|
|
|
if (enabler) {
|
|
|
|
if (do_enable) {
|
|
|
|
EnableIfChecked(enabler, text);
|
|
|
|
EnableIfChecked(enabler, browse);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DisableIfChecked(enabler, text);
|
|
|
|
DisableIfChecked(enabler, browse);
|
|
|
|
}
|
|
|
|
}
|
2010-07-19 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
2011-10-11 02:06:44 +02:00
|
|
|
void OptionPage::OptionFont(wxSizer *sizer, std::string opt_prefix) {
|
2014-05-31 05:32:07 +02:00
|
|
|
const auto face_opt = OPT_GET(opt_prefix + "Font Face");
|
|
|
|
const auto size_opt = OPT_GET(opt_prefix + "Font Size");
|
2011-10-11 02:06:44 +02:00
|
|
|
|
2012-04-11 05:43:08 +02:00
|
|
|
parent->AddChangeableOption(face_opt->GetName());
|
|
|
|
parent->AddChangeableOption(size_opt->GetName());
|
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto font_name = new wxTextCtrl(this, -1, to_wx(face_opt->GetString()));
|
2012-05-15 16:06:34 +02:00
|
|
|
font_name->SetMinSize(wxSize(160, -1));
|
2013-12-12 03:25:13 +01:00
|
|
|
font_name->Bind(wxEVT_TEXT, StringUpdater(face_opt->GetName().c_str(), parent));
|
2011-10-11 02:06:44 +02:00
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto font_size = new wxSpinCtrl(this, -1, std::to_wstring((int)size_opt->GetInt()), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 3, 42, size_opt->GetInt());
|
2013-12-12 03:25:13 +01:00
|
|
|
font_size->Bind(wxEVT_SPINCTRL, IntUpdater(size_opt->GetName().c_str(), parent));
|
2011-10-11 02:06:44 +02:00
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto pick_btn = new wxButton(this, -1, _("Choose..."));
|
2013-12-12 03:25:13 +01:00
|
|
|
pick_btn->Bind(wxEVT_BUTTON, std::bind(font_button, parent, font_name, font_size));
|
2011-10-11 02:06:44 +02:00
|
|
|
|
2014-05-31 05:32:07 +02:00
|
|
|
auto button_sizer = new wxBoxSizer(wxHORIZONTAL);
|
2011-10-11 02:06:44 +02:00
|
|
|
button_sizer->Add(font_name, wxSizerFlags(1).Expand());
|
|
|
|
button_sizer->Add(pick_btn, wxSizerFlags().Expand());
|
|
|
|
|
|
|
|
Add(sizer, _("Font Face"), button_sizer);
|
|
|
|
Add(sizer, _("Font Size"), font_size);
|
|
|
|
}
|
2012-02-28 02:22:40 +01:00
|
|
|
|
|
|
|
void OptionPage::EnableIfChecked(wxControl *cbx, wxControl *ctrl) {
|
2014-05-31 05:32:07 +02:00
|
|
|
auto cb = dynamic_cast<wxCheckBox*>(cbx);
|
2012-02-28 02:22:40 +01:00
|
|
|
if (!cb) return;
|
|
|
|
|
|
|
|
ctrl->Enable(cb->IsChecked());
|
2014-05-31 05:32:07 +02:00
|
|
|
cb->Bind(wxEVT_CHECKBOX, [=](wxCommandEvent& evt) { ctrl->Enable(!!evt.GetInt()); });
|
2012-02-28 02:22:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OptionPage::DisableIfChecked(wxControl *cbx, wxControl *ctrl) {
|
2014-05-31 05:32:07 +02:00
|
|
|
auto cb = dynamic_cast<wxCheckBox*>(cbx);
|
2012-02-28 02:22:40 +01:00
|
|
|
if (!cb) return;
|
|
|
|
|
|
|
|
ctrl->Enable(!cb->IsChecked());
|
2014-05-31 05:32:07 +02:00
|
|
|
cb->Bind(wxEVT_CHECKBOX, [=](wxCommandEvent& evt) { ctrl->Enable(!evt.GetInt()); });
|
2012-02-28 02:22:40 +01:00
|
|
|
}
|