Search code examples
c++macosmacos-carbon

SetItemIcon() stopped working on Mac 10.13, what is the alternative?


SetItemIcon() a Carbon API function which is used to set an icon for a menu item apparently have stopped working on macOS 10.13 High Sierra. I mean it now displays a bunch of distorted pixels instead of an actual icon. It was working fine since 10.0 through 10.12. Function is defined in Menus.h

/*
 *  SetItemIcon()
 *  
 *  Mac OS X threading:
 *    Not thread safe
 *  
 *  Availability:
 *    Mac OS X:         in version 10.0 and later in Carbon.framework
 *    CarbonLib:        in CarbonLib 1.0 and later
 *    Non-Carbon CFM:   in InterfaceLib 7.1 and later
 */
extern void 
SetItemIcon(
  MenuRef         theMenu,
  MenuItemIndex   item,
  short           iconIndex)                                  AVAILABLE_MAC_OS_X_VERSION_10_0_AND_LATER;

And is used in code like this:

SetItemIcon((MenuHandle)hMenu, uItem+1, hbmp-256);

And is built with XCode Version 3.2.5.

Is there a way to fix or workaround this issue?

P.S. Icons are stored in an .r resource file in some old strange HEX text format:

resource 'ICON' (300) {
    $"0000 0000 0000 0000 0000 0000 0000 0000"
    $"0000 0000 0000 0000 0000 0000 0000 0000"
    $"0000 0000 0000 0000 0000 0000 0000 0F00"
    $"0000 FE00 000F FC00 00FF F800 03FF FF80"
    $"00FF F800 000F FC00 0000 FE00 0000 0F"
};

Solution

  • A working alternative to using SetItemIcon() is to use SetMenuItemIconHandle() which still works fine on 10.13.

    SetMenuItemIconHandle((MenuHandle)hMenu, uItem+1, kMenuIconResourceType, (Handle) CFSTR("myicon.icns")); 
    

    Icons will need to be converted from the HEX text format to an .icns file and added into project and app bundle.