From ec0c65a80a895b761476408be5922b0fbe741985 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Fri, 19 Oct 2018 15:12:38 +0200 Subject: [PATCH] Fix CheckboxFileDialog, for real this time (I hope) --- src/slic3r/GUI/GUI_Utils.cpp | 50 +++++++++++++++++++----------------- src/slic3r/GUI/GUI_Utils.hpp | 12 ++++++--- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/slic3r/GUI/GUI_Utils.cpp b/src/slic3r/GUI/GUI_Utils.cpp index b538718e1..d1437d9b8 100644 --- a/src/slic3r/GUI/GUI_Utils.cpp +++ b/src/slic3r/GUI/GUI_Utils.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include "libslic3r/Config.hpp" @@ -16,6 +15,29 @@ namespace Slic3r { namespace GUI { +CheckboxFileDialog::ExtraPanel::ExtraPanel(wxWindow *parent) + : wxPanel(parent, wxID_ANY) +{ + // WARN: wxMSW does some extra shenanigans to calc the extra control size. + // It first calls the create function with a dummy empty wxDialog parent and saves its size. + // Afterwards, the create function is called again with the real parent. + // Additionally there's no way to pass any extra data to the create function (no closure), + // which is why we have to this stuff here. Grrr! + auto *dlg = dynamic_cast(parent); + const wxString checkbox_label(dlg != nullptr ? dlg->checkbox_label : wxString()); + + auto* sizer = new wxBoxSizer(wxHORIZONTAL); + cbox = new wxCheckBox(this, wxID_ANY, checkbox_label); + sizer->AddSpacer(5); + sizer->Add(this->cbox, 0, wxEXPAND | wxALL, 5); + SetSizer(sizer); + sizer->SetSizeHints(this); +} + +wxWindow* CheckboxFileDialog::ExtraPanel::ctor(wxWindow *parent) { + return new ExtraPanel(parent); +} + CheckboxFileDialog::CheckboxFileDialog(wxWindow *parent, const wxString &checkbox_label, bool checkbox_value, @@ -29,37 +51,19 @@ CheckboxFileDialog::CheckboxFileDialog(wxWindow *parent, const wxString &name ) : wxFileDialog(parent, message, default_dir, default_file, wildcard, style, pos, size, name) - , cbox(nullptr) + , checkbox_label(checkbox_label) { if (checkbox_label.IsEmpty()) { return; } - extra_control_creator = [this, checkbox_label](wxWindow *parent) -> wxWindow* { - wxPanel* panel = new wxPanel(parent, -1); - wxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); - this->cbox = new wxCheckBox(panel, wxID_HIGHEST + 1, checkbox_label); - this->cbox->SetValue(true); - sizer->AddSpacer(5); - sizer->Add(this->cbox, 0, wxEXPAND | wxALL | wxALIGN_CENTER_VERTICAL, 5); - panel->SetSizer(sizer); - sizer->SetSizeHints(panel); - - return panel; - }; - - SetExtraControlCreator(control_creator_trampoline); + SetExtraControlCreator(ExtraPanel::ctor); } bool CheckboxFileDialog::get_checkbox_value() const { - return this->cbox != nullptr ? cbox->IsChecked() : false; -} - -wxWindow* CheckboxFileDialog::control_creator_trampoline(wxWindow *parent) -{ - auto *self = dynamic_cast(parent); - return self != nullptr ? self->extra_control_creator(parent) : nullptr; + auto *extra_panel = dynamic_cast(GetExtraControl()); + return extra_panel != nullptr ? extra_panel->cbox->GetValue() : false; } diff --git a/src/slic3r/GUI/GUI_Utils.hpp b/src/slic3r/GUI/GUI_Utils.hpp index 827bacfce..fe96e5a1b 100644 --- a/src/slic3r/GUI/GUI_Utils.hpp +++ b/src/slic3r/GUI/GUI_Utils.hpp @@ -8,6 +8,7 @@ #include #include +#include class wxCheckBox; class wxTopLevelWindow; @@ -37,10 +38,15 @@ public: bool get_checkbox_value() const; private: - std::function extra_control_creator; - wxCheckBox *cbox; + struct ExtraPanel : public wxPanel + { + wxCheckBox *cbox; - static wxWindow* control_creator_trampoline(wxWindow *); + ExtraPanel(wxWindow *parent); + static wxWindow* ctor(wxWindow *parent); + }; + + wxString checkbox_label; };