diff options
author | Vojtech Kral <vojtech@kral.hk> | 2018-10-17 15:01:10 +0300 |
---|---|---|
committer | Vojtech Kral <vojtech@kral.hk> | 2018-10-17 15:09:18 +0300 |
commit | d4371b60897acc4d95a5b24d8a1a379338bb4375 (patch) | |
tree | 9b6a3b58f89d0b74b8b2e8e90f5d1e6000cdf423 /src/slic3r/GUI/GUI_Utils.cpp | |
parent | 2e274b5646c938d76a3f3e9e7813cbc6aea5c884 (diff) |
Refactor window position & size persistence
in a way that is hopefully robust wrt. platform quirks
Diffstat (limited to 'src/slic3r/GUI/GUI_Utils.cpp')
-rw-r--r-- | src/slic3r/GUI/GUI_Utils.cpp | 99 |
1 files changed, 81 insertions, 18 deletions
diff --git a/src/slic3r/GUI/GUI_Utils.cpp b/src/slic3r/GUI/GUI_Utils.cpp index b8cd60944..8fff2d208 100644 --- a/src/slic3r/GUI/GUI_Utils.cpp +++ b/src/slic3r/GUI/GUI_Utils.cpp @@ -1,16 +1,23 @@ #include "GUI_Utils.hpp" +#include <algorithm> +#include <boost/lexical_cast.hpp> +#include <boost/format.hpp> + +#include <wx/toplevel.h> #include <wx/sizer.h> #include <wx/panel.h> #include <wx/checkbox.h> +#include "libslic3r/Config.hpp" + namespace Slic3r { namespace GUI { CheckboxFileDialog::CheckboxFileDialog(wxWindow *parent, - const wxString &checkbox_label, + const wxString &checkbox_label, bool checkbox_value, const wxString &message, const wxString &default_dir, @@ -24,31 +31,87 @@ CheckboxFileDialog::CheckboxFileDialog(wxWindow *parent, : wxFileDialog(parent, message, default_dir, default_file, wildcard, style, pos, size, name) , cbox(nullptr) { - 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; - }; + 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(*extra_control_creator.target<ExtraControlCreatorFunction>()); } bool CheckboxFileDialog::get_checkbox_value() const { - return this->cbox != nullptr ? cbox->IsChecked() : false; + return this->cbox != nullptr ? cbox->IsChecked() : false; +} + + + +WindowMetrics WindowMetrics::from_window(wxTopLevelWindow *window) +{ + WindowMetrics res; + res.rect = window->GetScreenRect(); + res.maximized = window->IsMaximized(); + return res; +} + +boost::optional<WindowMetrics> WindowMetrics::deserialize(const std::string &str) +{ + std::vector<std::string> metrics_str; + metrics_str.reserve(5); + + if (!unescape_strings_cstyle(str, metrics_str) || metrics_str.size() != 5) { + return boost::none; + } + + int metrics[5]; + try { + for (size_t i = 0; i < 5; i++) { + metrics[i] = boost::lexical_cast<int>(metrics_str[i]); + } + } catch(const boost::bad_lexical_cast &) { + return boost::none; + } + + if ((metrics[4] & ~1) != 0) { // Checks if the maximized flag is 1 or 0 + metrics[4] = 0; + } + + WindowMetrics res; + res.rect = wxRect(metrics[0], metrics[1], metrics[2], metrics[3]); + res.maximized = metrics[4]; + + return res; +} + +void WindowMetrics::sanitize_for_display(const wxRect &screen_rect) +{ + rect = rect.Intersect(screen_rect); +} + +std::string WindowMetrics::serialize() +{ + return (boost::format("%1%; %2%; %3%; %4%; %5%") + % rect.GetX() + % rect.GetY() + % rect.GetWidth() + % rect.GetHeight() + % static_cast<int>(maximized) + ).str(); } + } } |