nsMenuItemMBS subclassing

I am subclassing nsMenuItemMBS to nsMenuItem_pf (to add some properties) and adding those to nsMenuMBS via NSMenuMBS.additem.

However, when I come to retrieve those items e.g. using nsMenuMBS.item(i) the returned item is an nsMenuItemMBS and not the subclass of nsMenuItem_pf.

What am I missing?

Answering my own question - it seems that I also need to keep an array of nsMenuItem_pf’s to refer back to. Shame as I thought that was one of the ideas behind subclassing.

You’re misunderstanding how the plugin works. NSMenu.Item returns a NSMenuItem, not your sublcass. Monkeybread Xojo plugin - NSMenuMBS methods

I do seem to be. I thought it was returning the NSMenuItem that I had inserted using addItem - i.e. the subclass. That is, I was kinda following the instructions given in nsmenuitemMBS.tag to add additional properties.

Where are you using the function and what are you trying to do?

I’m using it in a subclass of popupMenu to add icons and tags(variants) to the xojo class.

This isn’t enough for me to offer solutions. NSMenuItemMBS has an Image property, I don’t understand what properties you’ve added, why, and where you’re trying to retrieve them. From this description alone, you don’t need a subclass.

If you’ve got it all working though, there isn’t really a need to answer me :slight_smile:

The subclass of popupMenu adds a number of methods to allow various things like resizing the popup’s width depending on the length of the selected item’s title.
PopupMenu_pf overwrites addrow to allow addition of different icons to the rows as nsMenutItemMBS subclasses via nsPopUpMenuButtonMBS and its associated menu.
The subclass of nsMenuItemMBS simply adds a tag_pf property as variant.

Simply put, I’m trying to create a standard popupMenu with icons in each line item and variants as tags.

Gotcha. You wouldn’t need to subclass NSMenuItemMBS if you create and track your own array of tags in the PopupMenu subclass. It sounds like you’ve got it working with an array of your NSMenuItemMBS subclass, so no need to take action.

If you need any more help, you’ve got me interested :slight_smile:

I thought I had it working - but now the nsMenuItemMBS.enabled property doesn’t seem to work.
It works OK when using nsMenuMBS on its own, but doesn’t seem to when derived from popupMenu.

Example:
Public Sub AddRow(s as string, hTag as variant, pic as picture, hEnabled as boolean = true)

Dim item As New NSMenuItemMBS(s)

item.Enabled = hEnabled
Dim img As New NSImageMBS(pic)
img.setSize(16,16)
item.image = img

menu.addItem(item)

items.append(item)
tags.append(hTag)

End Sub

PS where has that icon gone to insert code into the forum?

It’s the </> symbol above the editor. Alternatively you can wrap your code in three backticks. Adding xojo after the three backticks and adding a new line will activate syntax highlighting.

I’m going to have to take a look at this tomorrow. Looking at the “Popupmenu Icons for Cocoa” example it looks like it can be done.

AFAIK you can not enabled / disable items in a Popup menu. You can however display the menu as a contextual menu and position it so that it appears to be a popup menu or dropdown menu.

Well, our plugin manages an object cache.
So as long as the nsMenuItem_pf object exists somewhere in your app, e.g. an array of nsMenuItem_pf, our plugin should give you the right one in the .item() function.
You may need to cast it to your subclass to access properties.

1 Like

I gave it a shot and concur with @Sam_Rowlands findings. I wasn’t able to disable the item.

@Christian_Schmitz that’s great to know, thank you!

2 Likes

Letze know if you have ideas for improvements.

Thank you all for your inputs: @Christian_Schmitz, @Tim_Parnell, @Sam_Rowlands.
For non-enabled items my solution is to just not display them.

1 Like