Button Question, strange behavior


#1

I Have a simple button component which is using paintButton, this is what jucer created as below called buttontools.h

[code]#ifndef BUTTONTOOLS_H
#define BUTTONTOOLS_H

#include <juce.h>

//==============================================================================
/**
An image based button class
*/
class OnOffButton : public Button

{
public:

//==============================================================================
/** Constructor */
OnOffButton(const String& buttonName);
/** Destructor */
~OnOffButton();

   /** @internal */

void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown);

private:

//==============================================================================
// (prevent copy constructor and operator= being generated..)
OnOffButton (const OnOffButton&);
const OnOffButton& operator= (const OnOffButton&);

};

OnOffButton::OnOffButton (const String& buttonName)
: Button(buttonName)
{
setSize(50,50);
}

OnOffButton::~OnOffButton()
{
}

void OnOffButton::paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)
{

if (getToggleState())
{
	g.setColour (Colour (Colours::red));
    g.fillEllipse (4.0f, 4.0f, 20.0f, 20.0f);
}
else
{
	g.setColour (Colour (Colours::blue));
    g.fillEllipse (4.0f, 4.0f, 20.0f, 20.0f);
}

}

#endif[/code]

Im using this like below, its suppose to toggle like and onoff button, works inside juce but does not toggle here!!!, however id does show up, am I missing something? Should be acting like a simple switch

[code]#include “…/…/juce.h”
#include “buttontools.h”

//==============================================================================
/** This is the component that sits inside the “hello world” window, filling its
content area. In this example, we’ll just write “hello world” inside it.
*/
class HelloWorldContentComponent : public Component

{

OnOffButton* myonoff;

public:
HelloWorldContentComponent()
{
//ONOFF SWITCH
addAndMakeVisible (myonoff = new OnOffButton (T(“OnOff”)));//Do This First!
myonoff->setBounds(30,60,26,26);
}

~HelloWorldContentComponent()
{
}

void paint (Graphics& g)
{
   
}

};[/code]


#2

Yes, you’re missing something - try reading the instructions for the Button toggle methods. You probably want to use Button::setClickingTogglesState.


#3

Thanx, i got confused as jucer subclases from the Button Type not ToggleButton, I just changed the line from Button to ToggleButton now its working.

class OnOffButton : public ToggleButton


#4

…no, it doesn’t have to be a ToggleButton. If you look at the ToggleButton class, you’ll see that it’s just a button that draws itself with a tick-box and automatically calls setClickingTogglesState().

It’s a waste of time making it a ToggleButton unless you want to use its features, so you might as well just call setClickingTogglesState yourself on a normal one.


#5