[Done] macOS ToolBarImageButton Size

Mac OSX specific forum
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

[Done] macOS ToolBarImageButton Size

Post by mk-soft »

macOS Ventura 13.0.1

Before someone reports this as a bug. Some methods have been removed from the NSToolBar. This means that there is no longer a "Use Small Size" method and the ToolBar icons are always large.

Link: https://developer.apple.com/forums/thread/696389
Link: https://developer.apple.com/documentati ... guage=objc

Code: Select all

;-TOP

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  #NSWindowToolbarStyleAutomatic = 0 ; 
  #NSWindowToolbarStyleExpanded = 1  ; Top Left
  #NSWindowToolbarStylePreference = 2; Top Center
  #NSWindowToolbarStyleUnified = 3   ; TitleBar Large
  #NSWindowToolbarStyleUnifiedCompact = 4 ; TitleBar without text
CompilerEndIf

Procedure SetToolBarStyle(Window, Style)
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    If OSVersion() > #PB_OS_MacOSX_10_14
      CocoaMessage(0, WindowID(Window), "setToolbarStyle:", Style)
    EndIf
  CompilerEndIf
EndProcedure

If OpenWindow(0, 0, 0, 350, 25, "ToolBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
    ; Fix on Big Sur to old style expanded
    SetToolBarStyle(0, #NSWindowToolbarStyleExpanded)
  CompilerEndIf
  
  CreateImage(0,16,16)
  StartDrawing(ImageOutput(0))
  Box(0,0,16,16,RGB(255,255,255))
  Box(4,4,8,8,RGB(255,0,0))
  StopDrawing()
  CreateImage(1,16,16)
  StartDrawing(ImageOutput(1))
  Box(0,0,16,16,RGB(255,0,0))
  Box(4,4,8,8,RGB(255,255,255))
  StopDrawing()
  If CreateToolBar(0, WindowID(0))
    ToolBarImageButton(0,ImageID(0))
    ToolBarImageButton(1,ImageID(1))
  EndIf
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Update Module ToolBarStandardButton for all OS
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
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [NO PB BUG] macOS ToolBarImageButton Size

Post by mk-soft »

Small Workaround for old projects ...

Update

Code: Select all

;-TOP by mk-soft, v1.01.1, 04.03.2023

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  
  Procedure MiscToolBarImageButton(ButtonID, ImageID, Mode = 0, Text$ = "")
    Protected ImageCenter, rect.NSRECT, x, y
    
    If ImageID
      CocoaMessage(rect, ImageID, "alignmentRect")
      If rect\size\width < 24.0
        ImageCenter = CreateImage(#PB_Any, 24, 24, 32, #PB_Image_Transparent)
        If StartDrawing(ImageOutput(ImageCenter))
          x = (24.0 - rect\size\width) * 0.5
          y = (24.0 - rect\size\height) * 0.5
          DrawAlphaImage(ImageID, x, y)
          StopDrawing()
          ImageID = ImageID(ImageCenter)
        EndIf
        ToolBarImageButton(ButtonID, ImageID, Mode, Text$)
        FreeImage(ImageCenter)
        ProcedureReturn #True
      EndIf
    EndIf
    ToolBarImageButton(ButtonID, ImageID, Mode, Text$)
  EndProcedure
  
  Macro ToolBarImageButton(ButtonID, ImageID, Mode = 0, Text = "")
    MiscToolBarImageButton(ButtonID, ImageID, Mode, Text)
  EndMacro
  
  ; ----
  
  #NSWindowToolbarStyleAutomatic = 0 ; 
  #NSWindowToolbarStyleExpanded = 1  ; Top Left
  #NSWindowToolbarStylePreference = 2; Top Center
  #NSWindowToolbarStyleUnified = 3   ; TitleBar Large
  #NSWindowToolbarStyleUnifiedCompact = 4 ; TitleBar without text

  Procedure SetToolBarStyle(Window, Style)
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      If OSVersion() > #PB_OS_MacOSX_10_14
        CocoaMessage(0, WindowID(Window), "setToolbarStyle:", Style)
      EndIf
    CompilerEndIf
  EndProcedure
  
CompilerEndIf

;- Example

CompilerIf #PB_Compiler_IsMainFile
  
  If OpenWindow(0, 0, 0, 350, 25, "ToolBar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      ; Fix on Big Sur to old style expanded
      SetToolBarStyle(0, #NSWindowToolbarStyleExpanded)
    CompilerEndIf
    
    CreateImage(0,16,16)
    StartDrawing(ImageOutput(0))
    Box(0,0,16,16,RGB(255,255,255))
    Box(4,4,8,8,RGB(255,0,0))
    StopDrawing()
    CreateImage(1,16,16)
    StartDrawing(ImageOutput(1))
    Box(0,0,16,16,RGB(255,0,0))
    Box(4,4,8,8,RGB(255,255,255))
    StopDrawing()
    If CreateToolBar(0, WindowID(0))
      ToolBarImageButton(0,ImageID(0))
      ToolBarImageButton(1,ImageID(1))
    EndIf
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf 
CompilerEndIf
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
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [NO PB BUG] macOS ToolBarImageButton Size

Post by WilliamL »

I'm not seeing any size difference no matter which 'style' I use.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [NO PB BUG] macOS ToolBarImageButton Size

Post by mk-soft »

Which Xcode version do you have?
Version 14.2 (14C18)

Can you still change the size of the ToolBar in the PB IDE via context?
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
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [NO PB BUG] macOS ToolBarImageButton Size

Post by WilliamL »

I'm not sure which version of Command Line Tools 14.2 I have. I haven't installed XCode just the Command Line Tools. The size change of the ToolBarImage has never worked on the M1 here. I can change the position to the right and all on one line. I tried making the images 32x32 but I didn't see any change in size. I'm not sure what you mean by 'PB IDE via context'.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [NO PB BUG] macOS ToolBarImageButton Size

Post by mk-soft »

With the right mouse button on the ToolBar you can set the items. However, it is not saved.
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
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [NO PB BUG] macOS ToolBarImageButton Size

Post by WilliamL »

Oh, yes, I see. In the IDE (6.01b5) there is a box to check for 'small' icons but it doesn't change the icons. It appears that the icons are 'large'. I ran 5.73 and the IDE icons were tiny and when I right-clicked and 'Customize Tool' bar was selected the Icons got 'large' then I was unable to change them again no matter what I tried. I can only get tiny icons (in 5.73) when I first run the IDE. In 6.01b5 the icons are always large and can't seem to be changed.

On my old Air (Intel-Monterey 12.5) using 5.73 the icons in the IDE change between large and small just fine. Using 6.00b5 on the Air the icons don't change either.

[Later]
It seems that if I select 'small icons' in 6.01b5 then quit the IDE and run it again the icons will be small until I try to change them and they stay large from then on.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Done] macOS ToolBarImageButton Size

Post by Fred »

Also put the patch in PureBasic, as it's more inline with other OS. If you want the new style you can styl applying it manually.
Post Reply