From 520b47248356854bc732b735ddd997269db98677 Mon Sep 17 00:00:00 2001 From: twanvl Date: Mon, 11 Aug 2008 13:23:29 +0000 Subject: [PATCH] Resize list of stylesheets to fit available space git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1138 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/control/package_list.cpp | 4 ++++ src/gui/control/package_list.hpp | 8 ++++++-- src/gui/set/style_panel.cpp | 18 ++++++++++++++++++ src/gui/set/style_panel.hpp | 4 ++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/gui/control/package_list.cpp b/src/gui/control/package_list.cpp index 89e15925..e677359e 100644 --- a/src/gui/control/package_list.cpp +++ b/src/gui/control/package_list.cpp @@ -100,3 +100,7 @@ void PackageList::select(const String& name, bool send_event) { update(); return; } + +int PackageList::requiredWidth() const { + return (item_size.x + SPACING) * (int)itemCount(); +} diff --git a/src/gui/control/package_list.hpp b/src/gui/control/package_list.hpp index f207ab8d..896a8470 100644 --- a/src/gui/control/package_list.hpp +++ b/src/gui/control/package_list.hpp @@ -48,11 +48,15 @@ class PackageList : public GalleryList { /// Select the package with the given name, if it is not found, selects nothing void select(const String& name, bool send_event = true); + /// Required width to show all items + int requiredWidth() const; + using GalleryList::column_count; + protected: - /// Return how many items there are in the list - virtual size_t itemCount() const; /// Draw an item virtual void drawItem(DC& dc, int x, int y, size_t item); + /// Return how many items there are in the list + virtual size_t itemCount() const; private: // The default icon to use diff --git a/src/gui/set/style_panel.cpp b/src/gui/set/style_panel.cpp index 3a80ba34..418e45a9 100644 --- a/src/gui/set/style_panel.cpp +++ b/src/gui/set/style_panel.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -47,6 +48,23 @@ StylePanel::StylePanel(Window* parent, int id) s->SetSizeHints(this); SetSizer(s); } +void StylePanel::updateListSize() { + // how many columns fit? + size_t fit_columns = (size_t)((GetSize().y - 400) / 152); + // we only need enough columns to show all items + int x_room = GetSize().x - editor->GetBestSize().x - 6; + size_t need_columns = (size_t)ceil(list->requiredWidth() / (double)x_room); + size_t column_count = max(1u, min(fit_columns, need_columns)); + // change count + if (column_count != list->column_count) { + list->column_count = column_count; + static_cast(GetParent())->fixMinWindowSize(); + } +} +bool StylePanel::Layout() { + updateListSize(); + return SetWindowPanel::Layout(); +} void StylePanel::onChangeSet() { list->showData(set->game->name() + _("-*")); diff --git a/src/gui/set/style_panel.hpp b/src/gui/set/style_panel.hpp index 8bb3a52a..6e802d1d 100644 --- a/src/gui/set/style_panel.hpp +++ b/src/gui/set/style_panel.hpp @@ -50,6 +50,10 @@ class StylePanel : public SetWindowPanel { void onStyleSelect(wxCommandEvent&); void onUseForAll(wxCommandEvent&); void onUseCustom(wxCommandEvent&); + + /// Determine the best size for the list of stylesheets based on available space + void updateListSize(); + virtual bool Layout(); }; // ----------------------------------------------------------------------------- : EOF