Can't get small toolbar icons

Mac OSX specific forum
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Can't get small toolbar icons

Post by coco2 »

I can only get large icons to show, small ones not showing. Is this is a bug?

Code: Select all

 If OpenWindow(0, 0, 0, 150, 25, "ToolBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    If CreateToolBar(0, WindowID(0))
      UsePNGImageDecoder()
      Path$ = #PB_Compiler_Home  + "Examples" + #PS$ + "Sources" + #PS$ + "Data" + #PS$ + "ToolBar" + #PS$ + ""
      ToolBarImageButton(0, LoadImage(0, Path$ + "New.png"))
      ToolBarImageButton(1, LoadImage(1, Path$ + "Open.png"))
      ToolBarImageButton(2, LoadImage(2, Path$ + "Save.png"))
    EndIf
    Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_Menu
        Debug "ToolBar ID: "+Str(EventMenu())
      EndIf
    Until Event = #PB_Event_CloseWindow 
  EndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Can't get small toolbar icons

Post by mk-soft »

It is not a bug.
Small ToolBarIcon is no longer supported by macOS.
The PureBasic IDE uses the CenterImage function for macOS to display the 16x16 icons to 24x24 icons

See Module ToolBarStandardButton

Code: Select all

Procedure CenterImage(Image, NewImage, Width, Height, Depth = 32, BackColor = #PB_Image_Transparent)
  Protected r1, x, y, dx, dy, image2
  
  r1 = CreateImage(NewImage, Width, Height, Depth, BackColor)
  If r1
    If NewImage = #PB_Any
      NewImage = r1
    EndIf
    dx = ImageWidth(Image)
    dy = ImageHeight(Image)
    x = (Width - dx) / 2
    y = (Height - dy) / 2
    If StartDrawing(ImageOutput(NewImage))
      DrawAlphaImage(ImageID(Image), x, y)
      StopDrawing()
    EndIf
  EndIf
  ProcedureReturn r1
EndProcedure
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Can't get small toolbar icons

Post by Shardik »

mk-soft wrote: Sat May 25, 2024 12:38 pm Small ToolBarIcon is no longer supported by macOS.
Unfortunately that's correct. Beginning with MacOS 13.0.1 Apple dropped the support of small icons in toolbars.

But however with some sort of hack of PureBasic's ToolBar it's still possible to display small toolbar icons (24x24) even with the most recent version of MacOS. In this thread more than 10 years ago I demonstrated how to add dummy buttons to the ToolBar and replace them with PureBasic gadgets like a ComboBox, a ProgressBar and a TrackBarGadget. I have modified this approach to even display small toolbar icons (24x24) in MacOS versions newer than 13.0.0.

This approach has the drawback that a click onto a small icon in MacOS 13.0.1 and newer has to be detected with EventGadget() and not with EventMenu() like the standard ToolBarImageButtons.

I have tested my example code successfully with PB 6.04 x64 and these 4 MacOS versions:
  • 10.13.6 'High Sierra'
  • 12.7.5 'Monterey'
  • 13.6.7 'Ventura'
  • 14.5 'Sonoma'

Code: Select all

EnableExplicit

Enumeration NSToolbarSizeMode
  #NSToolbarSizeModeDefault
  #NSToolbarSizeModeRegular
  #NSToolbarSizeModeSmall
EndEnumeration

Enumeration ToolbarButtons
  #SmallIconToolBarButton
  #RegularIconToolBarButton
EndEnumeration

Procedure.S GetMacOSMajorAndMinorVersion()
  Protected MacOSVersion.S

  MacOSVersion = StringField(PeekS(CocoaMessage(0, CocoaMessage(0,
    CocoaMessage(0, 0, "NSProcessInfo processInfo"),
    "operatingSystemVersionString"), "UTF8String"), -1, #PB_UTF8), 2, " ")

  ProcedureReturn StringField(MacOSVersion, 1, ".") + "." +
    StringField(MacOSVersion, 2, ".")
EndProcedure

Procedure ChangeToolBarButtonToSmallSize(ToolBarID.I, GadgetID.I, ImageID.I)
  Protected Item.I
  Protected ItemArray.I
  Protected Size.NSSize

  ; ----- Define standard ButtonImageGadget
  ButtonImageGadget(#SmallIconToolbarButton, 12, 10, 24, 24, ImageID(0))
  
  Size\width = GadgetWidth(0)
  Size\height = GadgetHeight(0)
  
  ; ----- Get item object of ToolBar button
  ItemArray = CocoaMessage(0, ToolBarID(ToolBarID), "items")
  Item = CocoaMessage(0, ItemArray, "objectAtIndex:", 0)
  
  ; ----- Replace ToolBar button by ButtonImageGadget
  CocoaMessage(0, Item, "setView:", GadgetID(0))
  
  ; ----- Change ToolBar button size to that of ButtonImageGadget
  CocoaMessage(0, Item, "setMaxSize:@", @Size)
  CocoaMessage(0, Item, "setMinSize:@", @Size)
EndProcedure

OpenWindow(0, 200, 100, 280, 50, "ToolBar with small icon 24x24",
  #PB_Window_SystemMenu)
OpenWindow(1, 500, 100, 280, 50, "ToolBar with regular icon 32x32",
  #PB_Window_SystemMenu)

; ----- Create image for ToolBar buttons

CreateImage(0, 16, 16)

StartDrawing(ImageOutput(0))
  Box(0, 0, 16, 16, #Red)
  Box(4, 4, 8, 8, #Yellow)
StopDrawing()

; ----- Create standard ToolBar in 1st window
CreateToolBar(0, WindowID(0))
  
If GetMacOSMajorAndMinorVersion() < "13.0.1"
  CocoaMessage(0, ToolBarID(0),
    "setSizeMode:", #NSToolbarSizeModeSmall)
  
  ; ----- Add standard ToolBarImageButton
  ToolBarImageButton(#SmallIconToolbarButton, ImageID(0))
Else
  ; ----- Add empty dummy image to ToolBar in 1st window
  ToolBarImageButton(0, ImageID(0))

  ; ----- Change ToolBarButton to small size 24x24
  ChangeToolBarButtonToSmallSize(0, #SmallIconToolBarButton, 0)
EndIf

; ----- Create standard ToolBar in 2nd window
CreateToolBar(1, WindowID(1))

; ----- Add standard ToolBarImageButton to ToolBar of 2nd window
If GetMacOSMajorAndMinorVersion() < "13.0.1"
  CocoaMessage(0, ToolBarID(1),
    "setSizeMode:", #NSToolbarSizeModeRegular)
EndIf

ToolBarImageButton(#RegularIconToolbarButton, ImageID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = #SmallIconToolBarButton
        Debug "Left click on small toolbar icon 24x24 in 1st window."
      EndIf
    Case #PB_Event_Menu
      Select EventMenu()
        Case #SmallIconToolBarButton
          Debug "Left click on small toolbar icon 24x24 in 1st window."
        Case #RegularIconToolBarButton
          Debug "Left click on regular toolbar icon 32x32 in 2nd window."
      EndSelect
  EndSelect
ForEver
Post Reply