Panel with Elements as Button - Click only works on panel 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
Morfio
Experienced Solver
Experienced Solver
Posts: 67
Joined: Thu Jul 07, 2005 4:35 pm

Panel with Elements as Button - Click only works on panel

Post by Morfio » Thu Dec 22, 2005 8:37 am

Hi,

I want to write my own button-component. I've got a panel, on this a static bitmap and a static text. In my gui the event is pushed only if I click on the panel, but not on the static bitmap/text. How can I make them pushing the event, too?

Code: Select all

// lknopf.h
#ifndef LKNOPF_H_
#define LKNOPF_H_

#include <wx/wx.h>
#include <wx/event.h>

class LKnopf : public wxPanel {

	public:
		LKnopf(wxWindow *, wxWindowID, const wxString &, const wxString &);
		
	protected:
		wxBoxSizer *bsHauptanzeige;
		wxStaticBitmap *sbmpKnopf;
		wxStaticText *stBeschriftung;
};

#endif /*LKNOPF_H_*/

Code: Select all

// lknopf.cpp
#include "lknopf.h"

LKnopf::LKnopf(wxWindow *parent, wxWindowID id, const wxString &bild, const wxString &beschriftung) :
																	wxPanel(parent, id) {
	bsHauptanzeige = new wxBoxSizer(wxVERTICAL);
	sbmpKnopf = new wxStaticBitmap(this, -1, wxBitmap(bild, wxBITMAP_TYPE_PNG));
	stBeschriftung = new wxStaticText(this, -1, beschriftung);
	stBeschriftung->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(LKnopf::klick));
	bsHauptanzeige->Add(sbmpKnopf, 0, wxALIGN_CENTER);
	bsHauptanzeige->Add(stBeschriftung, 0, wxALIGN_CENTER | wxTOP, 5);
	SetSizer(bsHauptanzeige);
}

Code: Select all

// Code with my own button (very dirty, just tests)
VAdministration::VAdministration(wxWindow *parent, wxWindowID id) :
								wxDialog(parent, id, _("Administration"), wxDefaultPosition, wxDefaultSize) {
	wxBoxSizer *test = new wxBoxSizer(wxHORIZONTAL);
	test2 = new LKnopf(this, ADMIN_TEST, _("anzeigen.png"), _("Anzeigenbearbeitung"));
	test->Add(test2);
	SetSizer(test);
	
	test2->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(VAdministration::klick));
}
Thank you all,

Morfio ...

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Thu Dec 22, 2005 8:57 am

It is the normal process.

If you look at the wxMouseEvent, it directly derives from wxEvent and not from wxCommandEvent so it is not passed to the parent window if not processed.

If you want this process, you must derive your own wxStatic* from the bases, use an event table to intercept mouse events and process them to the parent panel.
What is little and green, witch go up and down ??
Yoda playing with the force.

Morfio
Experienced Solver
Experienced Solver
Posts: 67
Joined: Thu Jul 07, 2005 4:35 pm

Post by Morfio » Thu Dec 22, 2005 9:26 am

Ok, I did it and it works perfectly. But how can I process the event I try to connect when I put more of my buttons in my application?

Code: Select all

// My Button
#ifndef LKNOPF_H_
#define LKNOPF_H_

#include <wx/wx.h>
#include <wx/event.h>

class LStaticText : public wxStaticText {

	public:
		LStaticText(wxWindow *, wxWindowID, const wxString &);
		
	protected:
		DECLARE_EVENT_TABLE()

};

class LStaticBitmap : public wxStaticBitmap {

	public:
		LStaticBitmap(wxWindow *, wxWindowID, const wxBitmap &, const wxPoint & = wxDefaultPosition, const wxSize & = wxDefaultSize, long = 0, const wxString & = _("staticBitmap"));
		
	protected:
		DECLARE_EVENT_TABLE()

};

class LKnopf : public wxPanel {

	public:
		LKnopf(wxWindow *, wxWindowID, const wxString &, const wxString &);
		
		void klick(wxMouseEvent &);
		
	protected:
		wxBoxSizer *bsHauptanzeige;
		LStaticBitmap *sbmpKnopf;
		LStaticText *stBeschriftung;
};

#endif /*LKNOPF_H_*/


#include "lknopf.h"

BEGIN_EVENT_TABLE(LStaticText, wxStaticText)
	EVT_LEFT_DOWN(LKnopf::klick)
END_EVENT_TABLE()

LStaticText::LStaticText(wxWindow *parent, wxWindowID id, const wxString &label) : wxStaticText(parent, id, label) {
}

BEGIN_EVENT_TABLE(LStaticBitmap, wxStaticBitmap)
	EVT_LEFT_DOWN(LKnopf::klick)
END_EVENT_TABLE()

LStaticBitmap::LStaticBitmap(wxWindow *parent, wxWindowID id, const wxBitmap &label, const wxPoint &pos, const wxSize &size, long style, const wxString &name) :
							wxStaticBitmap(parent, id, label, pos, size, style, name) {
}

LKnopf::LKnopf(wxWindow *parent, wxWindowID id, const wxString &bild, const wxString &beschriftung) :
																	wxPanel(parent, id) {
	bsHauptanzeige = new wxBoxSizer(wxVERTICAL);
	sbmpKnopf = new LStaticBitmap(this, -1, wxBitmap(bild, wxBITMAP_TYPE_PNG));
	stBeschriftung = new LStaticText(this, -1, beschriftung);
	stBeschriftung->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(VAdministration::klick));
	bsHauptanzeige->Add(sbmpKnopf, 0, wxALIGN_CENTER);
	bsHauptanzeige->Add(stBeschriftung, 0, wxALIGN_CENTER | wxTOP, 5);
	SetSizer(bsHauptanzeige);
}

void LKnopf::klick(wxMouseEvent &event) {
	printf("jo2\n");
	event.Skip();
}


// my test
VAdministration::VAdministration(wxWindow *parent, wxWindowID id) :
								wxDialog(parent, id, _("Administration"), wxDefaultPosition, wxDefaultSize) {
	wxBoxSizer *test = new wxBoxSizer(wxHORIZONTAL);
	test2 = new LKnopf(this, ADMIN_TEST, _("anzeigen.png"), _("Anzeigenbearbeitung"));
	test->Add(test2);
	SetSizer(test);
	
	test2->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(VAdministration::klick));
}

Now only the panel execute VAdministration::klick. Thats realy correct (see the event tables). But I want that all components on the panel execute the event. Have I to deliver this function and if how can I do that?

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Thu Dec 22, 2005 9:29 am

Cursor wrote:It is the normal process.

If you look at the wxMouseEvent, it directly derives from wxEvent and not from wxCommandEvent so it is not passed to the parent window if not processed.

If you want this process, you must derive your own wxStatic* from the bases, use an event table to intercept mouse events and process them to the parent panel.
Or just simply set the eventhandler. (wxWindow::SetEventHandler)

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Re: Panel with Elements as Button - Click only works on pane

Post by upCASE » Thu Dec 22, 2005 9:30 am

Hi!
Morfio wrote:I want to write my own button-component. I've got a panel, on this a static bitmap and a static text. In my gui the event is pushed only if I click on the panel, but not on the static bitmap/text. How can I make them pushing the event, too?
You can use Connect() for the label and bitmap, just like you tried. I do that for a thumbnail control with about the same setup and it works just fine.
Not that you should set the event sink to the panel when using Connect(). Instead of

Code: Select all

tBeschriftung->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(LKnopf::klick));
try

Code: Select all

tBeschriftung->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(LKnopf::klick),NULL,this);
to have LKnopf process the event. There is no need to create derived bitmaps and labels.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Morfio
Experienced Solver
Experienced Solver
Posts: 67
Joined: Thu Jul 07, 2005 4:35 pm

Re: Panel with Elements as Button - Click only works on pane

Post by Morfio » Thu Dec 22, 2005 9:43 am

upCASE wrote:Hi!

Code: Select all

tBeschriftung->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(LKnopf::klick),NULL,this);
Now it proccesses the LKnopf::klick, but I want to proccess VAdministration::klick without set it staticly:

Code: Select all

tBeschriftung->Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler(VAdministration::klick),NULL,this);
because I want to use this button more than one time with different event. What I want is:

Code: Select all

LKnopf button1 = new LKnopf....
button1->Connect(..., wxMouseEvent(class::method1), ...);
LKnopf button2 = new LKnopf....
button2->Connect(..., wxMouseEvent(class::method2), ...);
LKnopf button3 = new LKnopf....
button3->Connect(..., wxMouseEvent(class::method3), ...);
I think I have to deliver the event-function to the LKnopf-Class, but I don't know how to do this. Something like that

Code: Select all

LKnopf::LKnopf(..., myDeliveredEvent, ...) {
   sbmpKnopf = new wxStaticBitmap(this...);
   sbmpKnopf->Connect(..., myDeliveredEvent, ...);
}

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Re: Panel with Elements as Button - Click only works on pane

Post by upCASE » Thu Dec 22, 2005 10:26 am

Morfio wrote:because I want to use this button more than one time with different event. What I want is:

Code: Select all

LKnopf button1 = new LKnopf....
button1->Connect(..., wxMouseEvent(class::method1), ...);
LKnopf button2 = new LKnopf....
button2->Connect(..., wxMouseEvent(class::method2), ...);
LKnopf button3 = new LKnopf....
button3->Connect(..., wxMouseEvent(class::method3), ...);
I think I have to deliver the event-function to the LKnopf-Class, but I don't know how to do this. Something like that
Then connect it to the correct event handler function and set the event sink to the VAdministration instance (in your case "parent").
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Morfio
Experienced Solver
Experienced Solver
Posts: 67
Joined: Thu Jul 07, 2005 4:35 pm

Post by Morfio » Thu Dec 22, 2005 10:50 am

Ok, now I set the EventHandler:

stBeschriftung->SetEventHandler(this);
sbmpKnopf->SetEventHandler(this);

But now the static bitmap isn't repainted any more. How can I avoid this?

Morfio
Experienced Solver
Experienced Solver
Posts: 67
Joined: Thu Jul 07, 2005 4:35 pm

Post by Morfio » Thu Dec 22, 2005 11:10 am

Ahh, SetNextHandler works. Is this the correct way?

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Dec 22, 2005 11:35 am

Morfio wrote:Ahh, SetNextHandler works. Is this the correct way?
It's not the "wrong" way :)
PushEventHandler() might work, too.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Morfio
Experienced Solver
Experienced Solver
Posts: 67
Joined: Thu Jul 07, 2005 4:35 pm

Post by Morfio » Thu Dec 22, 2005 11:53 am

Ok, thank you all.

How can I mark the thread as "problem fixed"?

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Thu Dec 22, 2005 1:04 pm

Morfio wrote:How can I mark the thread as "problem fixed"?
Click either "assist" for awarding 1 point or "accepted" for awarding 5 points and closing the question next to the post of the one that helped you. Do not click on your own posts. This will close the thread too, but not award points.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

Post Reply