Draw icon on toolbar with MBS?

Is there a way to draw an icon on toolbar with the MBS plugin? I checked the examples but wasn’t able to find anything.

macOS only, as always.

I do this by adding an NSButtonMBS to the toolbar and then setting its image.

1 Like

Do you want to dynamically display an image, or perhaps just replace one in a toolbar every once in a while?

It’s every once in a while or at launch time, have you tried the built-in “Icon” property of a ToolbarButton?

If it’s dynamically (and potentially with animation), you can throw a canvas up there and draw whatever you want whenever you want.

Some more information on what I want to do:

The main window has a not really super pretty button at the bottom to get 20% off the full price if the users purchases early:

In the next version I’ve implemented a feature that the user can hide the main window if not needed. The purchase button wouldn’t be visible. The secondary window has a bottom bar, too. But the window is smaller and shows help. With this the huge button doesn’t fit into the window anymore:

I could add a huge toolbar item. But I want to do the button in 2 rows. Therefore, a regular toolbar icon is not high enough.

How do I add an NSButtonMBS or a canvas to the toolbar?

There is a project showing how to create an MBS toolbar with buttons and more in MBS Examples: /MacControls/Toolbar/Toolbar buttons/Toolbar buttons

@Jonathan Ashwell: what does the example have to do with the canvas I want in the toolbar?

Did I misunderstand the last line of your post?

Not sure. :grinning:

What do I do with an NSButtonMBS? The example is one of the few I found overly complex. I already have a toolbar. I can add a regular toolbar item just fine.

You can set the image property of the NSButtonMBS to display whatever you want – and if you want simply show a static picture make the button nonresponsive to clicks. You can also make the image a template, so it will automatically assume the correct active/inactive appearance as the context changes. I don’t know about adding a canvas (or rather its NS equivalent).

Hm… and how do I add such a button? In the example the NSButtonMBS is used for regular toolbar items.

@Christian Schmitz: can you help?

I am not sure where the problem is.

You can show a NSButtonMBS in a NSTOolbarMBS. and we have a NSButtonMBS constructor to take a NSImageMBS directly:

Constructor(Title as String, Image as NSImageMBS = nil, Type as Integer = 0)

And the button can have no border or no bezel if needed.

Well, there is the usual problem of me being dense.

dim t as NSToolbarMBS = window1.NSToolbarMBS

dim pic as new NSImageMBS(email_light)
dim b as new NSButtonMBS("bla", pic, 0)
t.items.Insert(0, b) 'doesn't compile

How do I add the button to the toolbar?

Doesn’t show anything in the toolbar:

dim t as NSToolbarMBS = window1.NSToolbarMBS
dim pic as new NSImageMBS(email_light)

dim items() as NSToolbarItemMBS = t.items
for each it as NSToolbarItemMBS in items
  if it.label = "other" then
    myButton = new NSButtonMBS("bla", pic, 0)
  end if
next

Did you check examples.

A toolbar has a set of identifiers and if one is selected to show, it may ask you for the actual control.
And please keep it in a Xojo array, so the object isn’t destroyed too early.

Yes, I checked the examples. And no, I don’t understand what I need to do. I only want to add a canvas and NOT a toolbar item. This I can do myself.

The example “View in titlebar” works:

v = new MyNSButtonMBS(420, 301, 50, 20)
v.Title = "Test"
dim pic as new NSImageMBS(email_light)
v.image = pic
v.bezelStyle = v.NSRoundRectBezelStyle
v.autoresizingMask = v.NSViewMinXMargin + v.NSViewMinYMargin // top right

w = new NSWindowMBS(self)

w.contentView.superview.addSubview v
w.show

Hey Beatrix,
Injecting a view into the window’s frame is NOT recommended any more. Others have reported problems with Big Sur when doing this (although that might have changed now we’re on Big Sur 11.0.1).

It’s a little awkward for me to explain how I do it, as I don’t use MBS, but I am sure that someone will be able to translate my ramblings into MBS compatible functions.

  1. Add a Toolbar Item of type button in the Xojo Toolbar editor.
  2. In the window open event, find the instance of the toolbar item, you can get the toolbar from the window, and a NSArray of items which correspond to the order you designed the toolbar.
  3. call NSToolbarItemSetView and pass in the .handle of the Canvas.
  4. Done.

@Christian_Schmitz: can you please tell me why the above code for NSButtonMBS doesn’t do anything? Alternatively, I’d be grateful if you could translate Sam to MBS.

lol!

for 3. get NSToolbarMBS object from the NSWindowMBS of the window and look in items there. Find your item.
for 3: assign your NSViewMBS object to it. You get by calling Canvas.NSViewMBS.