Toolbars and Toggle Buttons

Is it possible to have a toggle button stay ON when another toggle button is ON on a toolbar. It seems when I click another toggle button, the one that was on becomes depressed. Am I missing something simple?

It seems toggle buttons react like radio buttons as well. If you want multiple selections, you can probably use PushButtons and manage their pushed state so each toggles, a bit like a series of CheckBoxes.

I have 2 buttons on my toolbar which this relate to. Show Grid and Snap to Grid. Obviously either one or both could be on together, off together or one or the other on or off. How can I achieve this?

I just tried to experiment with regular toolbar PushButtons but they do not seem to respond to the Pushed state. So what I was describing won’t work.

The Xojo Toolbar is really limited. Apparently, it is not possible to change the icon for a given item, so it is not possible to simulate a a Pushed state.

Your best bet seems to be to create your own toolbar with a canvas or a rectangle, and place on it a series of canvas containing icons, and labels for captions. Then you will be able to manage each individual button state through mouseDown in lieu of Action and have multiple selections. Jim Mc Kay uses in his Black Window method in to place an object over the window top bar. It works fine for regular windows.

see https://forum.xojo.com/10914-black-windows

The “Toggle button”(s) work fine on a Toolbar and each successive click changes the visible pushed state of each of the two buttons independently on Windows 7, Xojo 2013r2.

What are the details of the difficulty being faced?

[quote=112636:@Syed Hassan]The “Toggle button”(s) work fine on a Toolbar and each successive click changes the visible pushed state of each of the two buttons independently on Windows 7, Xojo 2013r2.

What are the details of the difficulty being faced?[/quote]
Having more than one button toggled on at a time. I.e push a button on, push it off.

The following code will toggle the Btn1 and Btn2 on the toolbar so that only one of these will be in Pushed state. However at the startup both these toolbar buttons will not be in Pushed state.

//
// Here are the steps.
//
// Toolbar1 is part of the project and Toolbar11 is in Window1. 
//
// Add following four toolbar buttons to Toolbar1
//
// Index    Caption   Name          Style
// 0        Grid On   tbbGridOn     Toggle button
// 1        Snap On   tbbSnapOn     Toggle button
// 2        Btn1      tbbBtn1       Toggle button
// 3        Btn2      tbbBtn2       Toggle button
//
//
// Add "Action" event of Toolbar11 and place the following code in that "Action" event.
//
  //
  // Start of file.
  //
  //
  // 0  - Grid On   - tbbGridOn
  // 1  - Snap On   - tbbSnapOn
  // 2  - Btn1      - tbbBtn1
  // 3  - Btn2      - tbbBtn2
  //
  Dim tb As ToolButton
  Select Case item.Name
  Case "tbbGridOn"
    //
    // Handle "Grid On" click
    //
  Case "tbbSnapOn"
    //
    // Handle "Snap On" click
    //
  Case "tbbBtn1"
    //
    // Handle "Btn1" click
    //
    tb = ToolButton( Toolbar11.Item(2))
    If tb.Pushed = True Then
      tb = ToolButton( Toolbar11.Item(3))
      tb.Pushed = False
    End If
  Case "tbbBtn2"
    //
    // Handle "Btn2" click
    //
    tb = ToolButton( Toolbar11.Item(3))
    If tb.Pushed = True Then
      tb = ToolButton( Toolbar11.Item(2))
      tb.Pushed = False
    End If
  End Select
  //
  // End of file.
  //

As a default, ToggleButtons act like that without any code. When you click a new one, the others are unpushed. I think what Mike is trying to do is to be able to have two buttons pushed at once.

[quote=112619:@Mike Charlesworth]Is it possible to have a toggle button stay ON when another toggle button is ON on a toolbar. It seems when I click another toggle button, the one that was on becomes depressed. Am I missing something simple?
[/quote]

I do not believe this is achievable. When you set a button as pushed, it unpushes the others, in code or by click. ToggleButtons do not seem to be what you need.

Here is a way to set a toolbar with PushButtons that act as ToggleButtons. In this example the two first ones are pushed :

Here is the code I use in the TollBar Action event.

[code]Sub Action(item As ToolItem)

dim tb as ToolButton
Select Case item.Caption
Case “One”
//
// Handle “” click
//
buttonpushed(0) = not buttonpushed(0)
tb = ToolButton( Toolbar11.Item(0))
if buttonpushed(0) = true then
tb.icon = acoustic_guitar_pushed
else
tb.icon = acoustic_guitar
end if
Case “Two”
//
// Handle “” click
//
buttonpushed(1) = not buttonpushed(1)
tb = ToolButton( Toolbar11.Item(1))
if buttonpushed(1) = true then
tb.icon = acorn_pushed
else
tb.icon = acorn
end if
Case “Three”
//
// Handle “” click
//
buttonpushed(2) = not buttonpushed(2)
tb = ToolButton( Toolbar11.Item(2))
if buttonpushed(2) = true then
tb.icon = account_level_filtering_pushed
else
tb.icon = account_level_filtering
end if
end select
End Sub
[/code]

The “pushed” icon variation is obtained by modifying the original icon to add a darker grey background and making the picture darker.

The variable buttonpushed() is set at the window level and it keeps the pushed state of each button.

This is not as smooth as original ToggleButtons, but that is the simplest solution. If you really want exactly the same effect as ToggleButtons with multiple selections, use a canvas over the top of the window and use custom pictures, as I believe it is done in the Xojo IDE. See post above.

The above sample code compiles and executes and does just that for Toolbar buttons “Btn1” and “Btn2” on Windows 7, Xojo 2013r2. When “Btn1” is clicked and as a result it is in Pushed state and if at that time “Btn2” was already in Pushed state then “Btn2” Pushded state is set to False. And same for the “Btn2” when it is clicked by user. This means that both “Btn1” and “Btn2” will not have their Pushed state True at the same time. While “Grid On” and “Snap On” buttons are “Toggle button”(s) on their own and do not change the state of other buttons on the toolbar so these two can be both in Pushed state True or False in any combination (4 off, True True, True False, False True, False False).

If there has been a misunderstanding and the actual requirement differs from the implemented example, then the sample code can be modified as needed.

Could be a platform difference. On a Mac, ToggleButtons act as radio buttons. When one is pushed, the others are released.

Why bother ? The OP seems to have lost all interest into this discussion, anyway :confused:

Michel, I have not lost interest at all. And other people may also be finding this thread useful for the same reason or may in the future so people should always continue to bother posting.

Thanks Syed I will try your example but I think its like Michel said, OSX the toolbar toggle buttons act like radio buttons.

Nice to know you still care.

Right, checked it and you can only have one toggle button pushed at a time on OSX. Real pain for me but it is what it is.

Have you looked at the example I posted with regular buttons and “pushed” icons ?

Alternatively, you can use a custom toolbar and simulate the regular togglebuttons two states with pictures lifted from the screen, or even use checkboxes.

I’m wondering if there is a declare I can use?

I quickly looked at NSButtonCell_Class but it is a rather complex reading.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSButtonCell_Class/Reference/Reference.html#//apple_ref/occ/cl/NSButtonCell

I think I need to use setSelectedItemIdentifier