From d3da4f984e015ee247783170593f828a19fafbaf Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 28 Sep 2022 17:41:27 -0400 Subject: [PATCH] FileTypes: Refactor icon handling to use BControlLook::ComposeIconSize(). Fixes #17907. --- src/preferences/filetypes/FileTypesWindow.cpp | 25 ++++++++++++------- src/preferences/filetypes/IconView.cpp | 22 +++++++++------- src/preferences/filetypes/IconView.h | 9 ++++--- .../filetypes/MimeTypeListView.cpp | 19 ++++++++------ 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/preferences/filetypes/FileTypesWindow.cpp b/src/preferences/filetypes/FileTypesWindow.cpp index c771569f97..e0d148110a 100644 --- a/src/preferences/filetypes/FileTypesWindow.cpp +++ b/src/preferences/filetypes/FileTypesWindow.cpp @@ -96,6 +96,8 @@ static const char* kAttributeNames[] = { class TypeIconView : public IconView { + typedef IconView _inherited; + public: TypeIconView(const char* name); virtual ~TypeIconView(); @@ -135,7 +137,7 @@ TypeIconView::TypeIconView(const char* name) : IconView(name) { ShowEmptyFrame(false); - SetIconSize(48); + SetIconSize((icon_size)48); } @@ -176,12 +178,13 @@ TypeIconView::Draw(BRect updateRect) font_height fontHeight; GetFontHeight(&fontHeight); + const BRect bitmapRect = _inherited::BitmapRect(); float y = fontHeight.ascent; if (IconSource() == kNoIcon) { // center text in the middle of the icon - y += (IconSize() - fontHeight.ascent - fontHeight.descent) / 2.0f; + y += (bitmapRect.Height() - fontHeight.ascent - fontHeight.descent) / 2.0f; } else - y += IconSize() + 3.0f; + y += bitmapRect.Height() + 3.0f; DrawString(text, BPoint(ceilf((Bounds().Width() - StringWidth(text)) / 2.0f), ceilf(y))); @@ -191,12 +194,14 @@ TypeIconView::Draw(BRect updateRect) void TypeIconView::GetPreferredSize(float* _width, float* _height) { + const BRect bitmapRect = _inherited::BitmapRect(); + if (_width) { float a = StringWidth(B_TRANSLATE("(from application)")); float b = StringWidth(B_TRANSLATE("(from super type)")); float width = max_c(a, b); - if (width < IconSize()) - width = IconSize(); + if (width < bitmapRect.Width()) + width = bitmapRect.Width(); *_width = ceilf(width); } @@ -205,7 +210,7 @@ TypeIconView::GetPreferredSize(float* _width, float* _height) font_height fontHeight; GetFontHeight(&fontHeight); - *_height = IconSize() + 3.0f + ceilf(fontHeight.ascent + *_height = bitmapRect.Height() + 3.0f + ceilf(fontHeight.ascent + fontHeight.descent); } } @@ -214,6 +219,8 @@ TypeIconView::GetPreferredSize(float* _width, float* _height) BRect TypeIconView::BitmapRect() const { + const BRect bitmapRect = _inherited::BitmapRect(); + if (IconSource() == kNoIcon) { // this also defines the drop target area font_height fontHeight; @@ -222,14 +229,14 @@ TypeIconView::BitmapRect() const float width = StringWidth(B_TRANSLATE("no icon")) + 8.0f; float height = ceilf(fontHeight.ascent + fontHeight.descent) + 6.0f; float x = (Bounds().Width() - width) / 2.0f; - float y = ceilf((IconSize() - fontHeight.ascent - fontHeight.descent) + float y = ceilf((bitmapRect.Height() - fontHeight.ascent - fontHeight.descent) / 2.0f) - 3.0f; return BRect(x, y, x + width, y + height); } - float x = (Bounds().Width() - IconSize()) / 2.0f; - return BRect(x, 0.0f, x + IconSize() - 1, IconSize() - 1); + float x = (Bounds().Width() - bitmapRect.Width()) / 2.0f; + return BRect(x, 0.0f, x + bitmapRect.Width(), bitmapRect.Height()); } diff --git a/src/preferences/filetypes/IconView.cpp b/src/preferences/filetypes/IconView.cpp index 12bf32e989..9efeb407e6 100644 --- a/src/preferences/filetypes/IconView.cpp +++ b/src/preferences/filetypes/IconView.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -497,13 +498,14 @@ Icon::AdoptData(uint8* data, size_t size) /*static*/ BBitmap* -Icon::AllocateBitmap(int32 size, int32 space) +Icon::AllocateBitmap(icon_size size, int32 space) { int32 kSpace = B_RGBA32; if (space == -1) space = kSpace; - BBitmap* bitmap = new (nothrow) BBitmap(BRect(0, 0, size - 1, size - 1), + BBitmap* bitmap = new (nothrow) BBitmap(BRect(BPoint(0, 0), + be_control_look->ComposeIconSize(size)), (color_space)space); if (bitmap == NULL || bitmap->InitCheck() != B_OK) { delete bitmap; @@ -520,7 +522,7 @@ Icon::AllocateBitmap(int32 size, int32 space) IconView::IconView(const char* name, uint32 flags) : BControl(name, NULL, NULL, B_WILL_DRAW | flags), fModificationMessage(NULL), - fIconSize(B_LARGE_ICON), + fIconSize((icon_size)0), fIcon(NULL), fHeapIcon(NULL), fHasRef(false), @@ -531,6 +533,7 @@ IconView::IconView(const char* name, uint32 flags) fDropTarget(false), fShowEmptyFrame(true) { + SetIconSize(B_LARGE_ICON); } @@ -716,7 +719,7 @@ IconView::AcceptsDrag(const BMessage* message) BRect IconView::BitmapRect() const { - return BRect(0, 0, fIconSize - 1, fIconSize - 1); + return fIconRect; } @@ -761,10 +764,10 @@ void IconView::GetPreferredSize(float* _width, float* _height) { if (_width) - *_width = fIconSize; + *_width = fIconRect.Width(); if (_height) - *_height = fIconSize; + *_height = fIconRect.Height(); } @@ -1034,7 +1037,7 @@ IconView::Update() icon = Icon::AllocateBitmap(fIconSize); if (icon != NULL && info.GetTrackerIcon(icon, - (icon_size)fIconSize) != B_OK) { + (icon_size)(icon->Bounds().IntegerWidth() + 1)) != B_OK) { delete icon; return; } @@ -1058,16 +1061,17 @@ IconView::Update() void -IconView::SetIconSize(int32 size) +IconView::SetIconSize(icon_size size) { if (size < B_MINI_ICON) size = B_MINI_ICON; if (size > 256) - size = 256; + size = (icon_size)256; if (size == fIconSize) return; fIconSize = size; + fIconRect = BRect(BPoint(0, 0), be_control_look->ComposeIconSize(fIconSize)); Update(); } diff --git a/src/preferences/filetypes/IconView.h b/src/preferences/filetypes/IconView.h index d7f3762e9c..78c2d91800 100644 --- a/src/preferences/filetypes/IconView.h +++ b/src/preferences/filetypes/IconView.h @@ -61,7 +61,7 @@ public: void AdoptMini(BBitmap* mini); void AdoptData(uint8* data, size_t size); - static BBitmap* AllocateBitmap(int32 size, int32 space = -1); + static BBitmap* AllocateBitmap(icon_size size, int32 space = -1); private: BBitmap* fLarge; @@ -105,7 +105,7 @@ public: void Unset(); void Update(); - void SetIconSize(int32 size); + void SetIconSize(icon_size size); void ShowIconHeap(bool show); void ShowEmptyFrame(bool show); status_t SetTarget(const BMessenger& target); @@ -113,7 +113,7 @@ public: status_t Invoke(BMessage* message = NULL); ::Icon* Icon(); - int32 IconSize() const { return fIconSize; } + icon_size IconSize() const { return fIconSize; } icon_source IconSource() const { return fSource; } status_t GetRef(entry_ref& ref) const; status_t GetMimeType(BMimeType& type) const; @@ -144,7 +144,8 @@ private: BMessenger fTarget; BMessage* fModificationMessage; - int32 fIconSize; + icon_size fIconSize; + BRect fIconRect; BBitmap* fIcon; BBitmap* fHeapIcon; diff --git a/src/preferences/filetypes/MimeTypeListView.cpp b/src/preferences/filetypes/MimeTypeListView.cpp index 32b2870b92..742ec96c1d 100644 --- a/src/preferences/filetypes/MimeTypeListView.cpp +++ b/src/preferences/filetypes/MimeTypeListView.cpp @@ -8,6 +8,7 @@ #include "MimeTypeListView.h" #include +#include #include #include @@ -90,7 +91,8 @@ MimeTypeItem::DrawItem(BView* owner, BRect frame, bool complete) owner->FillRect(rect, B_SOLID_LOW); } - BBitmap bitmap(BRect(0, 0, B_MINI_ICON - 1, B_MINI_ICON - 1), B_RGBA32); + const BRect iconRect(BPoint(0, 0), be_control_look->ComposeIconSize(B_MINI_ICON)); + BBitmap bitmap(iconRect, B_RGBA32); BMimeType mimeType(fType.String()); status_t status = icon_for_type(mimeType, bitmap, B_MINI_ICON); if (status < B_OK) { @@ -101,8 +103,8 @@ MimeTypeItem::DrawItem(BView* owner, BRect frame, bool complete) } if (status == B_OK) { - BPoint point(rect.left + 2.0f, - rect.top + (rect.Height() - B_MINI_ICON) / 2.0f); + BPoint point(rect.left + 2.0f, + rect.top + (rect.Height() - iconRect.Height()) / 2.0f); owner->SetDrawingMode(B_OP_ALPHA); owner->DrawBitmap(&bitmap, point); @@ -110,7 +112,7 @@ MimeTypeItem::DrawItem(BView* owner, BRect frame, bool complete) owner->SetDrawingMode(B_OP_COPY); - owner->MovePenTo(rect.left + B_MINI_ICON + 8.0f, frame.top + fBaselineOffset); + owner->MovePenTo(rect.left + iconRect.Width() + 8.0f, frame.top + fBaselineOffset); owner->DrawString(Text()); owner->SetLowColor(lowColor); @@ -128,10 +130,11 @@ MimeTypeItem::Update(BView* owner, const BFont* font) BStringItem::Update(owner, font); if (fShowIcon) { - SetWidth(Width() + B_MINI_ICON + 2.0f); + const BSize iconSize = be_control_look->ComposeIconSize(B_MINI_ICON); + SetWidth(Width() + iconSize.Width() + 2.0f); - if (Height() < B_MINI_ICON + 4.0f) - SetHeight(B_MINI_ICON + 4.0f); + if (Height() < (iconSize.Height() + 4.0f)) + SetHeight(iconSize.Height() + 4.0f); font_height fontHeight; font->GetHeight(&fontHeight); @@ -544,7 +547,7 @@ MimeTypeListView::MessageReceived(BMessage* message) /*! \brief This method makes sure a new MIME type will be selected. - + If it's not in the list yet, it will be selected as soon as it's added. */