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