setToggleState() in ApplicationCommandTarget::perform()


#1

I can’t get the following code to work as I would like, i.e, toggle the button image. I’m calling it from my ApplicationCommandTarget::perform(const InvocationInfo& info)

if(toggleButton->getToggleState()){
toggleButton->setToggleState(0, false);
}
else{
toggleButton->setToggleState(1, false);
}

In my perform method I use info.originatingComponent to get a pointer to the button that was pressed. I check to make sure it is the right button and I try to set it’s toggle state but it isn’t working. I think it does change states but changes straight back immediately? If I run in debug mode however perform is only called once per button press. If I try the above code from another button function it swaps images no problem. Any ideas? I’m still trying to get my head around the ApplicationCommandTarget, it’s getting clearer but I’m sure I’ve missed something along the way.


#2

Hi Rory,
Remove this code and your toggle button should work properly. ToggleButton can update themselves. You can look at the working of boldButton(button name is bold) in Fonts and Text option of juce demo.(location :juce/extras/juce demo/Source/demos/FontsAndTextDemo.cpp ) to understand the working.

if(toggleButton->getToggleState()){
toggleButton->setToggleState(0, false);
}
else{
toggleButton->setToggleState(1, false);
}

#3

Thanks Vishvesh for your reply. I’m not actually using a toggle button, I’m using a toolbar button with two images. In the docs it says the second image will be used when the image is in a toggled state but I can’t seem to do it. I’m trying to create a play button that changes to a stop button when users click it. No luck so far. I can’t goggle it from anywhere outside my perform routine but can’t see to toggle it in there.


#4

U need to use the toggle button for this. The normal button has three stages
a) Mouse is over the button.
b) When the button is normal. ( nothing is happening on it )
c) When the button is being held down. ( When u leave the mouse button it comes to its normal state ).

So to toggle the button u need to have toggle button rather than normal text button or image button.


#5

Thanks guys. My confusion leads from the fact that in the docs for toolbarbutton it sates that the fourth paramter to a toolbarbutton constructor is:
toggledOnImage:

a drawable object that the button can use as its icon if the button is in a toggled-on state (see the Button::getToggleState() method). If 0 is passed-in here, then the normal image will be used instead, regardless of the toggle state. The object that is passed-in here will be kept by this object and will be deleted when no longer needed or when this button is deleted.

ToggleButton also doesn’t let me seem to choose a picture for when it is toggled?


#6

I didn’t get time to test ToolbarButton. But from the constructor it looks like you have got it right.

I still believe, you don’t have to set the toggle state of the button. Please try your old code without this

if(toggleButton->getToggleState()){
toggleButton->setToggleState(0, false);
}
else{
toggleButton->setToggleState(1, false);
}

Anyway I will give it a shot in the evening and I will let you know how it goes.


#7

I’m pretty sure I already tried that. In fact I would also have epxected it to work without that code. When it didn’t I added the code to force the change. I’m having problems with the latest tip at present so I can’t build my project again but once I do I will try it and let you know. Thanks for your patience on this!


#8

Without my earlier code it still doesn’t toggle images as I expect it should. Perhaps I’m using the wrong approach?


#9

Hey Rory

Toolbar buttons aren’t really supposed to toggle like a switch or checkbox, if you look at any Mac app’s toolbar buttons non of them really toggle on/off. In my experience toolbar buttons usually just operate in a “push” state that then invokes some sort of action (toolbar buttons are usually shortcuts if you will for menu items). So the toggledOnImage param is just the image that the button uses when it is pressed.


#10

my suggestion is ToggleButton… check that class …


#11

From what I can see from the docs a ToggleButton won’t do what I want. A drawable button on the other hand is what I think I need to use. I can then toggle its state myself. I’ll let you know if it works.


#12

Yes, drawable button did the trick but now I’m getting some odd behaviour with it. I’m pretty sure that using drawablebutton is a valid approach.