A ToggleButton with an Animation

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
llt
In need of some credit
In need of some credit
Posts: 1
Joined: Fri Jul 02, 2010 10:14 am

A ToggleButton with an Animation

Post by llt »

Hi,
i want to have a wxToggleButton with an wxAnimationCtrl in it.
But the Button i currently have only gets the Click events of the Button. When i click onto the Animation into the Button, then the Button change not his State.
I can get the events of the wxAnimationCtrl and handel them but the Button its self does not toggle.
My Qustion is what do i have to do that the Button by Clicks on the wxAnimationCtrl behaves the same as by Clicks on the Button.

wxAnimatedButton.h:

Code: Select all


class wxAnimatedButton : public wxToggleButton
{
public:
	wxAnimatedButton( wxWindow *parent,
		wxWindowID id,
		wxUChar *animationBytes1,
		WXUINT animationlen1,
		const wxPoint& pos = wxDefaultPosition,
		const wxSize& size = wxDefaultSize,
		const wxString& name = "animatedButton")
	{Create(parent, id, animationBytes1, animationlen1,  pos, size, name);};
	

	~wxAnimatedButton();


	bool Create(wxWindow *parent,
		wxWindowID id,
		wxUChar *animationBytes1,
		WXUINT animationlen1,
		const wxPoint& pos = wxDefaultPosition,
		const wxSize& size = wxDefaultSize,
		const wxString& name = "animatedButton");

	   
   void SetPos(const wxPoint& pos);   

   void StartAnimation( void );
   void StopAnimation( void );


private:
	wxAnimation *animation;
	wxAnimationCtrl *ctrl;
	wxMemoryInputStream *mis;

	int mBorderStyle;  

protected:
}; 

/****************************************************/
// Define a custom event handler
class wxAniBtnEvtHandler : public wxEvtHandler
{
public:
    wxAniBtnEvtHandler( wxWindowID id, wxAnimatedButton* button ) { mID = id; pButton = button;}


private:
   
	void OnLeftMouseDown(wxMouseEvent &event);
	void OnLeftMouseUp(wxMouseEvent &event);
	void OnMouseMove(wxMouseEvent &event);

	wxWindowID mID;
	wxAnimatedButton* pButton;

    DECLARE_EVENT_TABLE()
};
wxAnimatedButton.cpp:

Code: Select all

#include "wxAnimatedButton.h"
//-----------------------------------------------------------------------------------
//EVENT TABLE // 
//-----------------------------------------------------------------------------------
//Zuordnung was ausgelöst werden soll wenn etwas passiert
BEGIN_EVENT_TABLE (wxAniBtnEvtHandler, wxEvtHandler) 
  EVT_LEFT_DOWN(wxAniBtnEvtHandler::OnLeftMouseDown)
  EVT_LEFT_UP(wxAniBtnEvtHandler::OnLeftMouseUp)
  EVT_MOTION(wxAniBtnEvtHandler::OnMouseMove)
END_EVENT_TABLE()
//------------------------------------------------------------------+
wxAnimatedButton :: ~wxAnimatedButton(void)
{
	// Controller löschen
	wxDELETE( ctrl );	
	// Animation löschen
	wxDELETE( animation );	
	// Bildinformationspuffer freigeben
	wxDELETE( mis );
}
//------------------------------------------------------------------+
bool wxAnimatedButton::Create(wxWindow *parent, wxWindowID id,
		wxUChar *animationBytes1, WXUINT animationlen1,		
		const wxPoint& pos, const wxSize& size, const wxString& name)
{
	ctrl = NULL;

	mBorderStyle = wxBORDER_RAISED;
	mClicked = false;
	
	// Animation erstellen
	animation = new wxAnimation( );

	// Bildinformationspuffer laden
	mis = new wxMemoryInputStream( animationBytes1, animationlen1 * sizeof( wxUChar ) );
	
	wxSize panelSize(0,0);

	// Glückte der Ladevorgang
	if( animation -> Load( *mis, wxANIMATION_TYPE_GIF ) )
	{
		// Controller erstellen
		ctrl = new wxAnimationCtrl( parent, id, *animation1 );	
		panelSize = ctrl->GetSize();
		wxDELETE( ctrl );

		panelSize.x += 10;
		panelSize.y += 10;

		if(size != wxDefaultSize)
			panelSize = size;
	}


	if(!wxToggleButton::Create (parent, id, "Ani", pos, panelSize, mBorderStyle))
	{
		return false;
	}
//	this->PushEventHandler(new wxAniBtnEvtHandler(id, this));
	
	// Glückte der Ladevorgang
	if( animation -> IsOk() )
	{
		// Controller erstellen
		ctrl = new wxAnimationCtrl( this, id, *animation, wxPoint(2, 2) );

		ctrl->PushEventHandler(new wxAniBtnEvtHandler(id, this));

		// Animation anzeigen
		ctrl -> Show( true );
	}

	SetValidator(wxDefaultValidator);
		
	return true;
}
//------------------------------------------------------------------+

//------------------------------------------------------------------+
void wxAnimatedButton::SetPos(const wxPoint& pos)
{
        Move(pos);
}
//------------------------------------------------------------------+

//------------------------------------------------------------------+
void wxAnimatedButton :: StartAnimation( void )
{
	// wurde die Animation korrekt geladen
	if( ctrl != NULL )
	{
		// Animation anzeigen und starten
		ctrl -> Show( true );
		ctrl -> Play( );
	}
}
//------------------------------------------------------------------+

//------------------------------------------------------------------+
void wxAnimatedButton :: StopAnimation( void )
{
	// wurde die Animation korrekt geladen
	if( ctrl != NULL )
	{
		// Animation stoppen
		ctrl -> Stop( );
	}
}

//-----------------------------------------------------------------------------------
void wxAniBtnEvtHandler::OnLeftMouseDown(wxMouseEvent &event)
{
	wxPostEvent( pButton, event );
}
//-----------------------------------------------------------------------------------
void wxAniBtnEvtHandler::OnLeftMouseUp(wxMouseEvent &event)
{
	wxPostEvent( pButton, event );
}
//-----------------------------------------------------------------------------------
void wxAniBtnEvtHandler::OnMouseMove(wxMouseEvent &event)
{	
	wxPostEvent( pButton, event );
//	event.ResumePropagation(wxEVENT_PROPAGATE_MAX);
//	event.Skip(); //alternate Code i tryed
}
Greetings Maxi
orbitcowboy
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Mon Jul 23, 2007 9:01 am

Re: A ToggleButton with an Animation

Post by orbitcowboy »

Has anybody found a solution for this?
OS: Ubuntu 9.04 (32/64-Bit), Debian Lenny (32-Bit)
Compiler: gcc/g++-4.3.3 , gcc/g++-4.4.0
wxWidgets: 2.8.10,2.9.0