From 2dd94ee67282ddcd6975f3ceb29788b9320193d7 Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Sat, 10 Nov 2018 10:02:51 -0600 Subject: [PATCH] Use either wxCombobox or wxChoice depending on underlying config choice. (port UI change from master) #4462 --- src/GUI/OptionsGroup/Field.hpp | 10 ++++-- src/GUI/OptionsGroup/UI_Choice.cpp | 53 +++++++++++++++++++++--------- src/test/GUI/test_field_choice.cpp | 4 +-- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/GUI/OptionsGroup/Field.hpp b/src/GUI/OptionsGroup/Field.hpp index b83774288..cfb9c254a 100644 --- a/src/GUI/OptionsGroup/Field.hpp +++ b/src/GUI/OptionsGroup/Field.hpp @@ -15,9 +15,11 @@ #include "wx/checkbox.h" #include "wx/textctrl.h" #include "wx/combobox.h" +#include "wx/choice.h" #include "wx/arrstr.h" #include "wx/stattext.h" #include "wx/sizer.h" +#include "wx/ctrlsub.h" #include #include #include @@ -230,7 +232,7 @@ private: class UI_Choice : public UI_Window { public: UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id = wxID_ANY); - ~UI_Choice() { _choice->Destroy(); } + ~UI_Choice() { window->Destroy(); } std::string get_string() override; @@ -240,7 +242,8 @@ public: std::function on_change {nullptr}; /// Returns a bare pointer to the underlying combobox, usually for test interface - wxComboBox* choice() { return this->_choice; } + wxChoice* choice() { return this->_choice; } + wxComboBox* combo() { return this->_combo; } protected: virtual std::string LogChannel() override { return "UI_Choice"s; } @@ -250,7 +253,8 @@ protected: } } private: - wxComboBox* _choice {nullptr}; + wxComboBox* _combo {nullptr}; + wxChoice* _choice {nullptr}; }; diff --git a/src/GUI/OptionsGroup/UI_Choice.cpp b/src/GUI/OptionsGroup/UI_Choice.cpp index e6bf3a7cd..b36abd17f 100644 --- a/src/GUI/OptionsGroup/UI_Choice.cpp +++ b/src/GUI/OptionsGroup/UI_Choice.cpp @@ -5,38 +5,59 @@ namespace Slic3r { namespace GUI { UI_Choice::UI_Choice(wxWindow* parent, Slic3r::ConfigOptionDef _opt, wxWindowID id) : UI_Window(parent, _opt) { int style {0}; style |= wxTE_PROCESS_ENTER; - if (opt.gui_type.size() > 0 && opt.gui_type.compare("select_open"s)) style |= wxCB_READONLY; /// Load the values auto values {wxArrayString()}; for (auto v : opt.enum_values) values.Add(wxString(v)); - _choice = new wxComboBox(parent, id, - (opt.default_value != nullptr ? opt.default_value->getString() : ""), - wxDefaultPosition, _default_size(), values, style); - window = _choice; + if (opt.gui_type.size() > 0 && opt.gui_type.compare("select_open"s)) { + _choice = new wxChoice(parent, id, + wxDefaultPosition, _default_size(), values, style); + _choice->Bind(wxEVT_CHOICE, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); + window = _choice; + } else { + _combo = new wxComboBox(parent, id, + (opt.default_value != nullptr ? opt.default_value->getString() : ""), + wxDefaultPosition, _default_size(), values, style); + _combo->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); + window = _combo; + } - _choice->Bind(wxEVT_COMBOBOX, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); - _choice->Bind(wxEVT_TEXT_ENTER, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); - _choice->Bind(wxEVT_KILL_FOCUS, [this](wxFocusEvent& e) { if (this->on_kill_focus != nullptr) {this->on_kill_focus(""); this->_on_change("");} e.Skip(); }); + window->Bind(wxEVT_TEXT_ENTER, [this](wxCommandEvent& e) { this->_on_change(""); e.Skip(); }); + window->Bind(wxEVT_KILL_FOCUS, [this](wxFocusEvent& e) { if (this->on_kill_focus != nullptr) {this->on_kill_focus(""); this->_on_change("");} e.Skip(); }); } std::string UI_Choice::get_string() { - if (opt.enum_values.size() > 0) { - auto idx = this->_choice->GetSelection(); - if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx); + if (_combo != nullptr) { + if (opt.enum_values.size() > 0) { + auto idx = this->_combo->GetSelection(); + if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx); + } + return this->_combo->GetValue().ToStdString(); + } else { + if (opt.enum_values.size() > 0) { + auto idx = this->_choice->GetSelection(); + if (idx != wxNOT_FOUND) return this->opt.enum_values.at(idx); + } + return std::string(""); } - return this->_choice->GetValue().ToStdString(); } void UI_Choice::set_value(boost::any value) { auto result {std::find(opt.enum_values.cbegin(), opt.enum_values.cend(), boost::any_cast(value))}; - if (result == opt.enum_values.cend()) { - this->_choice->SetValue(wxString(boost::any_cast(value))); + if (_combo != nullptr) { + if (result == opt.enum_values.cend()) { + this->_combo->SetValue(wxString(boost::any_cast(value))); + } else { + auto idx = std::distance(opt.enum_values.cbegin(), result); + this->_combo->SetSelection(idx); + } } else { - auto idx = std::distance(opt.enum_values.cbegin(), result); - this->_choice->SetSelection(idx); + if (result != opt.enum_values.cend()) { + auto idx = std::distance(opt.enum_values.cbegin(), result); + this->_choice->SetSelection(idx); + } } } diff --git a/src/test/GUI/test_field_choice.cpp b/src/test/GUI/test_field_choice.cpp index 753292508..d2c17def1 100644 --- a/src/test/GUI/test_field_choice.cpp +++ b/src/test/GUI/test_field_choice.cpp @@ -70,14 +70,14 @@ SCENARIO( "UI_Choice: default values from options") { } } WHEN( "I set the string value to another item in the enumeration") { - test_field.choice()->SetValue("C"s); + test_field.combo()->SetValue("C"s); THEN( "get_string() returns the matching item in ConfigOptionDef") { REQUIRE(test_field.get_string() == "C"s); REQUIRE(test_field.choice()->FindString(simple_option.enum_values[1]) == 1); } } WHEN( "I set the string value to another item that is not in the enumeration") { - test_field.choice()->SetValue("F"s); + test_field.combo()->SetValue("F"s); THEN( "get_string() returns the matching item in ConfigOptionDef") { REQUIRE(test_field.get_string() == "F"s); REQUIRE(test_field.choice()->GetSelection() == wxNOT_FOUND);