Owner drawn button - display problem Topic is solved

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
medienluemmel
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Jun 19, 2006 6:56 pm

Owner drawn button - display problem

Post by medienluemmel »

Hello,

I've tried to design my own flat button control with the following code:

Code: Select all

BEGIN_EVENT_TABLE (CFlatButton, wxPanel)
	EVT_PAINT (CFlatButton::OnPaint)
	EVT_LEFT_DOWN (CFlatButton::OnLeftClick)
	EVT_ENTER_WINDOW (CFlatButton::OnMouseIn)
	EVT_LEAVE_WINDOW (CFlatButton::OnMouseOut)
	EVT_LEFT_UP (CFlatButton::OnMouseUp)
END_EVENT_TABLE ()

IMPLEMENT_DYNAMIC_CLASS (CFlatButton, wxPanel)

CFlatButton::CFlatButton (wxWindow* parent, wxWindowID id, const wxString& label,
	const wxPoint& pos, const wxSize& size, const bool& b_border)
{
	wxPanel::Create (parent, id, pos, size, 0, "CFlatButton");
	SetLabel (label);
	SetFont (parent->GetFont ());
	s_label = label;
	wxClientDC dc ((wxWindow *) this);
	int w_c, h_c;
	dc.GetTextExtent (s_label, &w_c, &h_c);
	SetSize (w_c + 10, h_c);
	i_state = 0; //i_state: 0 = normal; 1 = hover (mouseover); 2 = clicked
}

void CFlatButton::OnLeftClick (wxMouseEvent &event)
{
	wxCommandEvent myevent (wxEVT_COMMAND_BUTTON_CLICKED, GetId());
	wxPostEvent (this, myevent);
	i_state = 2;
	Refresh ();
	event.Skip ();
}

void CFlatButton::OnMouseIn (wxMouseEvent &event)
{
	i_state = 1;
	Refresh ();
	event.Skip ();
}

void CFlatButton::OnMouseOut (wxMouseEvent &event)
{
	i_state = 0;
	Refresh ();
	event.Skip ();
}

void CFlatButton::OnMouseDown (wxMouseEvent &event)
{
	i_state = 2;
	Refresh ();
	event.Skip ();
}

void CFlatButton::OnMouseUp (wxMouseEvent &event)
{
	i_state = 0;
	Refresh ();
	event.Skip ();
}

void CFlatButton::OnPaint (wxPaintEvent& WXUNUSED (event))
{
        // Painting control
        // if i_state == 0 - normal design
        // if i_state == 1 - bright text colour
        // if i_state == 2 - bright background colour

}
Everything works fine, but there is one problem:

When I click the Flatbutton and another dialog is shown, the Flatbutton state stays in pressed mode (because neither OnMouseUp nor OnMouseOut is called).

What can I do to release the button?

Thanks for any help!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

Send the event in OnMouseUp, not OnLeftClick. That's the normal behavior of buttons anyway: You can click (and hold) inside a button, move the mouse out and release the button, then no action will be triggered.
Use the source, Luke!
medienluemmel
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Jun 19, 2006 6:56 pm

Post by medienluemmel »

doublemax wrote:Send the event in OnMouseUp, not OnLeftClick. That's the normal behavior of buttons anyway: You can click (and hold) inside a button, move the mouse out and release the button, then no action will be triggered.
Thank you very much, it works - but you have to change the code in OnMouseUp to check, if you're still on the button. I did it like this:

Code: Select all

void CFlatButton::OnMouseUp (wxMouseEvent &event)
{
	if (event.GetX () > 0 && event.GetX () < GetSize ().GetWidth () &&
		event.GetY () > 0 && event.GetY () < GetSize ().GetHeight ())
	{
		wxCommandEvent myevent (wxEVT_COMMAND_BUTTON_CLICKED, GetId());
		wxPostEvent (this, myevent);
	}
	i_state = 0;
	Refresh ();
	event.Skip ();
}
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

You shouldn't get a mouseup event if the mouse is not over the button. Which platform did you test this on?
Use the source, Luke!
medienluemmel
Earned a small fee
Earned a small fee
Posts: 20
Joined: Mon Jun 19, 2006 6:56 pm

Post by medienluemmel »

doublemax wrote:You shouldn't get a mouseup event if the mouse is not over the button. Which platform did you test this on?
Linux GTK.
Post Reply