Toolbars and Toggle Buttons

Indeed. toolbarSelectableItemIdentifiers should have been set by Xojo already. It is worth a try anyway. From the Apple doc, it does seem like several items can be selected at once. Hopefully the Xojo code will not conflict.

Yes, i’m not at all hot on declares though, I have tried:

Declare sub setSelectedItemIdentifier lib "Cocoa" selector "setSelectedItemIdentifier:" (handle as integer,id as CFStringRef)

arguments: handle being the toolbar handle and id being the name of the tool item. No joy though, perhaps someone hot on declares could help with this one.

I agree, this is how I read it

This much difference in functionality between Windows and OSX does not seem possible for such a basic GUI component. May be a member of Xojo technical team could answer this.

Do not have access to an OSX machine so cannot try the sample code.

Please add a separator between two toolbar “Toggle buttons” and see if this brings about any change on OSX.

No tried this as well. It appears the grouping is the toolbar container

There must be a way to group these toolbar buttons and the solution in this case sould be to have each such buttton in a group by iteself.

I think that this is an OS X thing and can’t be easily changed. My solution is to use a SegmentedControl. You replace the standard button with a SegmentedControl by using the NSToolbarItem’s setView: selector in the Open event of the window.

In that case, toolbar being such a basic GUI component, maybe Xojo needs to align the toolbar button behaviour across all these supported groups of operating systems and this may have to be reported as a bug.

I don’t think this is possible on OS X: since users are allowed to modify the toolbar, they could mix up toggle button groups. So toggling per group is only doable with a SegmentedControl (as in OS X’s Finder). On the standard OS X apps toggling of standard buttons (the ones where you can set an icon like in Xojo) is only used in the Preferences windows of all these apps - and there the user is not allowed to change the toolbar. So I don’t see what Xojo could do about that - though I’m not 100% sure.

Did you get an eye on MacOSLib ?

I am not sure, but I think I saw declare for Toolbar…

They are allowed to modify the toolbar based on what the developer provide (default bar, other available buttons).

When you say “no joy”, it means you cannot get this to select a button, or that not two buttons can be selected at once ?

I have been to
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Toolbars/Tasks/SelectableItems.html

And from the description :

[code]setSelectedItemIdentifier:
Sets the receiver’s selected item to the specified toolbar item.

  • (void)setSelectedItemIdentifier:(NSString *)itemIdentifier
    Parameters
    itemIdentifier
    The identifier of the item to select. itemIdentifier may be any identifier returned by toolbarSelectableItemIdentifiers:, even if it is not currently in the toolbar.
    Discussion
    Typically, a toolbar will manage the selection of items automatically. This method can be used to select identifiers of custom view items, or to force a selection change. See toolbarSelectableItemIdentifiers: for more details. If itemIdentifier is not recognized by the receiver, the current selected item identifier does not change.

Availability
Available in OS X v10.3 and later.
See Also
– selectedItemIdentifier
toolbarSelectableItemIdentifiers: (NSToolbarDelegate)
Declared In
NSToolbar.h[/code]

I am not quite sure we have all the information needed. How to you set the handle to a particular toolbutton ? I see no such property for that class. Or is it the handle to the toolbar (which is available) ? I suppose CFStringRef is the toolbutton name. Or is it something else ?

I tried with the handle of the toolbar and the name of a button, it does not do anything.

This window is the result of a mix between actual ToolBar buttons and canvases containing a transparent mask for the Selected state. I used a declare to allow objects over the toolbar. When the button is clicked, it sets the canvas over it containing the “selected” picture on or off. These are normal buttons.

The canvas lets the click event of the button work even if when it is visible, so nothing has to be changed in the Action event.

In terms of GUI, such a solution provides exactly the same behavior as real toggle buttons.

I tried again, but the setSelectedItemIdentifier declare never works. I checked Apple doc and it seems to be fine, except I am not so sure about the handle, if it is the toolbar as provided by Xojo, or if the button itself has one for Objective C. Fact is it does not do anything.

On the other hand, the mask over a button as described in my previous post does work flawlessly. Between a wish for an elegant declare and a working solution, maybe the latter is a valid option …

Just found out that there is a NSToolbarItemGroup, so my comment was entirely wrong.

So the solution would be to create a group for every toggleButton, so it does not interfere with the next ?

Now the challenge is to get to a declare that works from :

[code]NSToolbarItemGroup is a subclass of NSToolbarItem which contains subitems. The views and labels of the subitems are used, but the parent’s attributes take precedence.

To configure an instance of NSToolbarItemGroup, you first create the individual toolbar items that will be the subitems:

NSToolbarItem *item1 = [[[NSToolbarItem alloc] initWithItemIdentifier:@“Item1”] autorelease];
NSToolbarItem *item2 = [[[NSToolbarItem alloc] initWithItemIdentifier:@“Item2”] autorelease];
[item1 setImage:[NSImage imageNamed:@“LeftArrow”]];
[item2 setImage:[NSImage imageNamed:@“RightArrow”]];
[item1 setLabel:@“Prev”];
[item2 setLabel:@“Next”];
and then put them in a grouped item:

NSToolbarItemGroup *group = [[[NSToolbarItemGroup alloc] initWithItemIdentifier:@“GroupItem”] autorelease];
[group setSubitems:[NSArray arrayWithObjects:item1, item2, nil]];
In this configuration, you get two grouped items, and two labels. This differs from ordinary NSToolbarItem objects because they are attached—the user drags them together as a single item rather than separately.
[/code]

The first part is done through Xojo code.

The second one, the actual call to create a group, I have to admit not to understand fully.

It does not seem to refer to the toolbar handle ?

Michel, I will play around with this and give it a try. However I would suspect that NSToolbarItemGroup is already implemented in the Xojo framework and that it groups all the buttons in the toolbar. I’m not sure we will be able to override this, as you say we would need to create a group for each individual button that was to be toggled on/off for it not to affect the other groups. However if Xojo have already grouped all the buttons in the toolbar i’m not sure a group within the group could be created.

Xojo, could you offer comment on this?

[quote=113838:@Mike Charlesworth]Michel, I will play around with this and give it a try. However I would suspect that NSToolbarItemGroup is already implemented in the Xojo framework and that it groups all the buttons in the toolbar. I’m not sure we will be able to override this, as you say we would need to create a group for each individual button that was to be toggled on/off for it not to affect the other groups. However if Xojo have already grouped all the buttons in the toolbar i’m not sure a group within the group could be created.

Xojo, could you offer comment on this?[/quote]

I do not think Xojo does anything, as far as grouping is concerned. Apple documentation is as obfuscated as Alain Greenspan discourse, but from what I understand, if nothing is done the whole toolbar is one group, NSToolbarItemGroup creates a new group with only such and such controls (item1 and item2 in their example). Somewhere there they mention segmented control to illustrate.

Their example seems pretty simple : create items, then decide which ones you want in a given group. So I would gather that after you have created the buttons in Xojo, a declare call to NSToolbarItemGroup should group items. As I wrote, I may not be versed enough in Objective C to fully understand NSToolbarItemGroup, because I would expect such a function to refer to a specific toolbar before assessing items in the group. Reading the doc, I cannot locate the reference to the ToolBar. But yet again, I was never able to select anything with selectedItemIdentifier. The ToolBar handle was right, but I am not so sure about the item ID.

We need some declare specialist to decipher Apple’s charade :wink:

[quote=113372:@Michel Bujardet]

This window is the result of a mix between actual ToolBar buttons and canvases containing a transparent mask for the Selected state. I used a declare to allow objects over the toolbar. When the button is clicked, it sets the canvas over it containing the “selected” picture on or off. These are normal buttons.

The canvas lets the click event of the button work even if when it is visible, so nothing has to be changed in the Action event.

In terms of GUI, such a solution provides exactly the same behavior as real toggle buttons.[/quote]

Michel, Do you have an example project of your implementation I could have a look at. This looks good. I would just wish we could do it with declares as it looks like it should be possible.

What worries me with the current toolbar is the different functionality on Windows and OSX. This sounds like it should be addressed but I guess it would be so far down the list of to-do’s it may never get done. I haven’t heard of may people requiring it.