wxIcon loading on macOS under 3.1.2 broken

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
72deluxe
Experienced Solver
Experienced Solver
Posts: 71
Joined: Tue May 20, 2008 10:55 pm

wxIcon loading on macOS under 3.1.2 broken

Post by 72deluxe » Wed Jan 23, 2019 9:15 pm

Hello

Under wxWidgets 3.1.2, I am finding that icons inside .icns no longer load. Here's some sample code:

Code: Select all

wxString imagepath("/System/Library/Extensions/IOStorageFamily.kext/Contents/Resources/Internal.icns");
for(int i=wxBITMAP_TYPE_BMP;i<wxBITMAP_TYPE_MAX;++i)
{
  wxIcon icon;
  if(icon.LoadFile(imagepath, (wxBitmapType)i, 50,50))
  {
    ::wxMessageBox("HURRAY!");
  }
}
I used to be able to load images this way if I used wxBITMAP_TYPE_ICON under 3.1.0 as I think it resolved to the Carbon sources? wxBITMAP_TYPE_ICON forced wxIcon::LoadIconFromFile (src/osx/carbon/icon.cpp) to load from an icns using ReadIconFromFSRef.

But this no longer works under 3.1.2 using Cocoa. What can I do to actually force it to load using ReadIconFromFSRef ? Which wxBitmapType should I use?

72deluxe
Experienced Solver
Experienced Solver
Posts: 71
Joined: Tue May 20, 2008 10:55 pm

Re: wxIcon loading on macOS under 3.1.2 broken

Post by 72deluxe » Wed Jan 23, 2019 10:12 pm

Further investigation: there is a ICNS handler under wxBitmap but this appears to be for resources embedded within the application only. I tried the following and it didn't work:

Code: Select all

wxBitmap::InitStandardHandlers();
wxBitmapHandler *h = wxBitmap::FindHandler(wxBITMAP_TYPE_ICON_RESOURCE);
/* wxImage::FindHandler(wxBITMAP_TYPE_ICON_RESOURCE); will not return anything - that's using a different list of handlers. You want the wxBitmap list of handlers. */
if (h)
{
  wxBitmap b;
  if (h->LoadFile(&b, imagepath, wxBITMAP_TYPE_ANY, 50, 50))
  {
    b.SaveFile("/somewhere/test.png", wxBITMAP_TYPE_PNG);
  }
}
....but the call to h->LoadFile fails. It seems to have the sort of code you'd want for creating a bitmap from a given .icns path but seems to be for resources for the app only, unless I am mistaken.

It'd be really useful if you could construct a wxIcon or wxBitmap from a .icns with a single function.
(I found a reference to loading icns using Carbon on the wiki but it's out of date: https://wiki.wxwidgets.org/WxIcon)

Post Reply