From 5a95794913a053611c5887ee7460d2da8ed75ac5 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Mon, 13 Sep 2021 10:29:41 +0200 Subject: [PATCH] OSX specific: Improvements for wxMultiChoiceDialog: Height of a ChoiceListBox will respect to items count This improvement fixed #6926 - Checkbox columns in modal windows are stretched (macOS) --- src/slic3r/GUI/GUI_Factories.cpp | 41 ++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/slic3r/GUI/GUI_Factories.cpp b/src/slic3r/GUI/GUI_Factories.cpp index 92ba427b5..4fec12d14 100644 --- a/src/slic3r/GUI/GUI_Factories.cpp +++ b/src/slic3r/GUI/GUI_Factories.cpp @@ -16,6 +16,10 @@ #include #include "slic3r/Utils/FixModelByWin10.hpp" +#ifdef __APPLE__ +#include "wx/dcclient.h" +#include "slic3r/Utils/MacDarkMode.hpp" +#endif namespace Slic3r { @@ -211,7 +215,6 @@ static int GetSelectedChoices( wxArrayInt& selections, const wxString& caption, const wxArrayString& choices) { -#ifdef _WIN32 wxMultiChoiceDialog dialog(nullptr, message, caption, choices); wxGetApp().UpdateDlgDarkUI(&dialog); @@ -219,6 +222,39 @@ static int GetSelectedChoices( wxArrayInt& selections, // deselects the first item which is selected by default dialog.SetSelections(selections); +#ifdef __APPLE__ + // Improvements for ChoiceListBox: Height of control will restect to items count + for (auto child : dialog.GetChildren()) + if (dynamic_cast(child) && !choices.IsEmpty()) { + wxClientDC dc(child); + + int height = dc.GetTextExtent(choices[0]).y; + int width = 0; + for (const auto& string : choices) + width = std::max(width, dc.GetTextExtent(string).x); + + // calculate best size of ListBox + height += 3 * mac_max_scaling_factor(); // extend height by margins + width += 3 * height; // extend width by checkbox width and margins + + // don't make the listbox too tall (limit height to around 10 items) + // but don't make it too small neither + int list_height = wxMax(height * wxMin(wxMax(choices.Count(), 3), 10), 70); + wxSize sz_best = wxSize(width, list_height); + + wxSize sz = child->GetSize(); + child->SetMinSize(sz_best); + + // extend Dialog size, if calculated best size of ListBox is bigger then its size + wxSize dlg_sz = dialog.GetSize(); + if (int delta_x = sz_best.x - sz.x; delta_x > 0) dlg_sz.x += delta_x; + if (int delta_y = sz_best.y - sz.y; delta_y > 0) dlg_sz.y += delta_y; + dialog.SetSize(dlg_sz); + + break; + } +#endif + if (dialog.ShowModal() != wxID_OK) { // NB: intentionally do not clear the selections array here, the caller @@ -229,9 +265,6 @@ static int GetSelectedChoices( wxArrayInt& selections, selections = dialog.GetSelections(); return static_cast(selections.GetCount()); -#else - return wxGetSelectedChoices(selections, message, caption, choices); -#endif } static wxMenu* create_settings_popupmenu(wxMenu* parent_menu, const bool is_object_settings, wxDataViewItem item/*, ModelConfig& config*/)