ToolbarButton width

Hi!

There doesn’t seem to be any way I can easily get the width of a ToolbarButton on a Toolbar.

Is there some funky way to calculate it somehow, or something I’ve missed?

My app is just a toolbar that needs to resize to fit all buttons on, so settings the window width to the sum of all the ToolbarButton widths would be ideal.

macOS does that on its own. You get arrows when the toolbar is too wide for the window. Except for macOS BS and that only in my app and not in an example.

I need to make my window automatically wider so all toolbar buttons all fit on, I don’t want the >> button to have to appear. But I’m not sure if it’s possible to calculate the width somehow.

Can you not use the following code to determine the screen width or height :

self.intScreen_W = Screen(0).Width
self.intScreen_H = Screen(0).Height

Screen(0).Width is referring to the main screen width. if you know the screen width, the number of icons on your toolbar and space in between, you can calculate the width and height of your toolbar icons. But maybe I am misunderstanding your problem.

I hope you find a solution soon.

Chris

Then I’d recommend using your own toolbar where you have control.

I found the solution using the MBS plugins. I’ll write it here in case anyone else ever needs it:

Var tlbWidth as Integer
Var tlb as NSToolbarMBS = Window1.NSToolbarMBS
Var tlbView as NSViewMBS = tlb.toolbarView
Var endPadding as Integer = 5 //padding at the end of a toolbar

Window1.Width = 1000 //large width - must be bigger than all buttons can ever be so we get the correct values next

for each v as NSViewMBS in tlbView.subviews
  if v.className = "NSToolbarItemViewer" then
    if tlbWidth < v.frameLeft + v.frameWidth + endPadding then tlbWidth = v.frameLeft + v.frameWidth + endPadding //only record the right-most button's left+width
  end if
next

Window1.Width = tlbWidth