From baa56732fc8387ae352cc2ed9c5ee4d641f5044f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 7 Nov 2023 00:03:56 +0100 Subject: [PATCH] Allow to get the size of all images in wxWithImages This simplifies wxGenericTreeCtrl code but doesn't change much yet. --- include/wx/withimages.h | 19 +++++++++++++++++++ src/generic/treectlg.cpp | 39 +++++++++++++++------------------------ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/include/wx/withimages.h b/include/wx/withimages.h index c3f556f388..97a7c3c123 100644 --- a/include/wx/withimages.h +++ b/include/wx/withimages.h @@ -129,6 +129,25 @@ public: return m_imageList; } + // Return logical bitmap size that should be used for all images. + // + // Returns (0, 0) if we don't have any images. + wxSize GetImageLogicalSize(const wxWindow* window) const + { + wxSize size; + + // Prefer to use the image list here if we have it because we must have + // already decided for the best size to use when creating it. + // + // Otherwise we need to compute the best size here ourselves. + if ( m_imageList ) + size = m_imageList->GetSize(); + else if ( !m_images.empty() ) + size = wxBitmapBundle::GetConsensusSizeFor(window, m_images); + + return window->FromPhys(size); + } + // Return logical size of the image to use or (0, 0) if there are none. wxSize GetImageLogicalSize(const wxWindow* window, int iconIndex) const { diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index e3dcd34ea0..a27d785699 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -744,9 +744,7 @@ wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point, // assuming every image (normal and selected) has the same size! if ( (GetImage() != NO_IMAGE) && theCtrl->HasImages() ) { - int image_h; - theCtrl->GetImageLogicalSize(theCtrl, GetImage(), - image_w, image_h); + image_w = theCtrl->GetImageLogicalSize(theCtrl).x; } int state_w = -1; @@ -879,11 +877,11 @@ wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl* control, int text_h = m_heightText + 2; int image_h = 0, image_w = 0; - int image = GetCurrentImage(); - if ( image != NO_IMAGE && control->HasImages() ) + if ( GetCurrentImage() != NO_IMAGE && control->HasImages() ) { - control->GetImageLogicalSize(control, image, image_w, image_h); - image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT; + const wxSize imageSize = control->GetImageLogicalSize(control); + image_h = imageSize.y; + image_w = imageSize.x + MARGIN_BETWEEN_IMAGE_AND_TEXT; } int state_h = 0, state_w = 0; @@ -2410,15 +2408,7 @@ void wxGenericTreeCtrl::CalculateLineHeight() if ( HasImages() ) { // Calculate a m_lineHeight value from the normal Image sizes. - // May be toggle off. Then wxGenericTreeCtrl will spread when - // necessary (which might look ugly). - int n = GetImageCount(); - for (int i = 0; i < n ; i++) - { - int width = 0, height = 0; - GetImageLogicalSize(this, i, width, height); - if (height > m_lineHeight) m_lineHeight = height; - } + m_lineHeight = GetImageLogicalSize(this).y; } if ( m_imagesState.HasImages() ) @@ -2459,7 +2449,10 @@ void wxGenericTreeCtrl::OnImagesChanged() { if ( HasImages() ) { - // We call it solely for the side effect of updating the image list. + // We call it solely for the side effect of updating the image list, + // which may be used by the application code using this class, even if + // we don't use it ourselves, but it also has an important side effect + // of ensuring that we can always get the size to use for the images. GetUpdatedImageListFor(this); UpdateAfterImageListChange(); @@ -2547,8 +2540,9 @@ void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc) { if ( HasImages() ) { - GetImageLogicalSize(this, image, image_w, image_h); - image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT; + const wxSize imageSize = GetImageLogicalSize(this); + image_h = imageSize.y; + image_w = imageSize.x + MARGIN_BETWEEN_IMAGE_AND_TEXT; } else { @@ -3462,12 +3456,9 @@ bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item, if ( textOnly ) { int image_w = 0; - int image = ((wxGenericTreeItem*) item.m_pItem)->GetCurrentImage(); - if ( image != NO_IMAGE && HasImages() ) + if ( i->GetCurrentImage() != NO_IMAGE && HasImages() ) { - int image_h; - GetImageLogicalSize( this, image, image_w, image_h ); - image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT; + image_w = GetImageLogicalSize(this).x + MARGIN_BETWEEN_IMAGE_AND_TEXT; } int state_w = 0;