Intercepting button::click()


#1

i’ve added a class called “ConfirmButton” that is based on the “TextButton” component. what i’d like to do is only pass a clicked message to the parent component (the one that contains the button in question) when the user clicks the button and then affirms their choice via an alert dialog (or maybe something else, but at the moment it’s an alert dialog).

i can intercept the click method with no problem. i can have the alert dialog pop up and do what i need, but i can’t seem to figure out how to then alert the parent component of the change without hardwiring it. it seems all the message passing stuff is protected/private so i can’t then just jump into standard juce code to handle the approved click message.

here is the code in question:


class ConfirmButton	:	public TextButton
{
public:

	ConfirmButton ( const String& name,
					const String& toolTip = String::empty,
					AlertWindow* dialog = NULL)
					: TextButton(name, toolTip)

	{
		confirmDialog = dialog;
		setClickingTogglesState(false);
	};

    /** Destructor. */
    ~ConfirmButton()
	{
	}


	void clicked()
	{
		confirmDialog->setCentreRelative(0.5f,0.5f);
		confirmDialog->setAlwaysOnTop(true);

		int v = confirmDialog->runModalLoop();

		if (v)
		{
			setToggleState(true, false);
		}

       confirmDialog->setVisible (false);
	}

private:
	Component *confirmDialog;

};

the setToggleState function can’t alert the button that it’s changed or i’ll end up back in the click() routine, so i have to pass it false for the notification flag.

thoughts?


#2

Try using postCommandMessage from your button and handleCommandMessage in the parent.

Also, shouldn’t clicked() be virtual?

Matt


#3

[quote=“matt”]Try using postCommandMessage from your button and handleCommandMessage in the parent.

Also, shouldn’t clicked() be virtual?

Matt[/quote]

hmm… okay, i’ll look into those.

virtual? heck if i know. i’m just now actually learning c++ so such subtleties are lost on me at the moment.

my goal is to have a simple means of implementing buttons that are “dangerous” (like “quit”) without having to have special case code for each and simply treat them like any other button. i was hoping to use the same basic button listener callback. (if button == quitButton exit the program rather than getting into additional validation in the buttonClicked routine).

i’m guessing the commandmessage stuff falls outside of this, but i’ll read up on them. i’m sure there are plenty of ways to skin this cat.


#4

so, duh, the click that triggers the confirm sequence also flows thru to the parent component so sending an additional click() is unnecessary. i’d have known that if i’d remembered to add a listener. sigh…


#5