[solved]Problem with wxMouseEvent

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.
Post Reply
Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

[solved]Problem with wxMouseEvent

Post by Sterl » Sat May 07, 2005 2:33 pm

I've tried to use mouse events but it dosen't work.

Here is samples of my code :

in mypanel.h

Code: Select all

class myPanel : public wxPanel
{
public:
    // class constructor
   myPanel(wxWindow* parent);
   // class destructor
  ~myPanel();
	
   void onMouseDown(wxMouseEvent& event);
private:    
  DECLARE_EVENT_TABLE()
};
in myPanel.ccp

Code: Select all

BEGIN_EVENT_TABLE(myPanel, wxPanel)
   EVT_LEFT_UP(myPanel::onMouseDown)
END_EVENT_TABLE()

void myPanel::onMouseDown(wxMouseEvent& event)
{
  wxMessageDialog msg(NULL,"it works !");
  msg.ShowModal();
}
Is there something wrong?
Last edited by Sterl on Mon May 30, 2005 4:14 pm, edited 1 time in total.

User avatar
T-Rex
Moderator
Moderator
Posts: 1195
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Post by T-Rex » Sat May 07, 2005 4:28 pm

Code: Select all

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

class MyApp : public wxApp
{
public:
	virtual bool OnInit();
};

class myPanel : public wxPanel
{
public:
    // class constructor
   myPanel(wxWindow* parent);
   // class destructor
  ~myPanel();       
   void onMouseDown(wxMouseEvent& event);
private:   
  DECLARE_EVENT_TABLE()
}; 

BEGIN_EVENT_TABLE(myPanel, wxPanel)
   EVT_LEFT_UP(myPanel::onMouseDown)
END_EVENT_TABLE()

myPanel::myPanel(wxWindow* parent)
: wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER)
{
}

myPanel::~myPanel()
{
}

void myPanel::onMouseDown(wxMouseEvent& event)
{
  wxMessageDialog msg(NULL,"it works !");
  msg.ShowModal();
} 

class MyFrame : public wxFrame
{
public:
	MyFrame(wxWindow * parent);
};

MyFrame::MyFrame(wxWindow * parent)
: wxFrame(parent, -1, "Test", wxPoint(100,100), wxSize(400,300))
{		
	wxBoxSizer * sizer = new wxBoxSizer(wxVERTICAL);
	SetSizer(sizer);
	myPanel * panel = new myPanel(this);
	sizer->Add(panel, 1, wxEXPAND);	
	Centre();
}

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	MyFrame * frame = new MyFrame(NULL);	
	frame->Show();	
	SetTopWindow(frame);
	return true;
}
Works perfectly under MSVC .NET 2003

Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

Post by Sterl » Sat May 07, 2005 4:39 pm

Ok thanks for the help. :wink:

I've understood what was wrong. In fact, i had a wxStaticBitmap added to my panel and i had to put the declaration of the event table in the declaration class of the staticBitmap.

In your example, you define an empty panel with a certain size and that's why that works.

I thauth the event would be propagated to the parent but apparently it is not.

Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

Post by Sterl » Wed May 25, 2005 6:31 pm

I've changed my version into wxWidgets 2.6.0 and now the caputre of mouse event does'nt work anymore. I didn't change anything in my code... :cry:

Help please !
:wink:

User avatar
T-Rex
Moderator
Moderator
Posts: 1195
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Post by T-Rex » Wed May 25, 2005 8:47 pm

It's strange... the sample above works fine with wxWidgets-2.5.x and 2.6.0
Maybe you have overlapped windows or maybe you missed smth else? Post your code (or upload it somewhere and post the link)... and we'll see what is possible to do.

Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

Post by Sterl » Wed May 25, 2005 9:09 pm

here is the code :

ZonexStaticBitmap.h

Code: Select all

#ifndef __ZONEXSTATICBITMAP_H
#define __ZONEXSTATICBITMAP_H

class ZonexStaticBitmap : public wxStaticBitmap
{
	public:
		ZonexStaticBitmap(wxWindow* parent,int iWidth,int iHeight);
		~ZonexStaticBitmap();
		void leftMouseDown(wxMouseEvent& event);
	private:	
	  DECLARE_EVENT_TABLE()
};

#endif // __ZONEXSTATICBITMAP_H
ZonexStaticBitmap.cpp :

Code: Select all

#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif


#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "zonexStaticBitmap.h"
#include "mainController.h"

BEGIN_EVENT_TABLE(ZonexStaticBitmap, wxStaticBitmap)
   EVT_LEFT_UP(ZonexStaticBitmap::leftMouseDown)
END_EVENT_TABLE()


ZonexStaticBitmap::ZonexStaticBitmap(wxWindow* parent,int iWidth,int iHeight)
                  : wxStaticBitmap(parent,-1,wxBitmap(iWidth,iHeight))
{
}

ZonexStaticBitmap::~ZonexStaticBitmap()
{
}

void ZonexStaticBitmap::leftMouseDown(wxMouseEvent& event)
{
MainController::GetInstance()->leftMouseDown(event.GetPosition());
}
I've downloaded the devpack 2.6.0 with the version manager of Dev c++ 4.9.9.2.

Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

Post by Sterl » Fri May 27, 2005 2:59 pm

Somebody has the same probleme with dev C++ 4.9.9.2 and downloaded devpack 2.6.0 ?

User avatar
T-Rex
Moderator
Moderator
Posts: 1195
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Post by T-Rex » Sat May 28, 2005 2:05 pm

Code: Select all

#include <wx/wx.h>

namespace MainController
{

	class wxMainController
	{
	public:		
		void leftMouseDown(wxPoint pt);
	};

	wxMainController * Instance = NULL;

	void CreateInstance() {if(!Instance) Instance = new wxMainController;}
	wxMainController * GetInstance() {return Instance;}
	void DestroyInstance() {if(Instance) delete Instance;}

	void wxMainController::leftMouseDown(wxPoint pt)
	{
		wxMessageBox("wxMainController::leftMouseDown works fine");
	}
}

using namespace MainController;

class ZonexStaticBitmap : public wxStaticBitmap
{
        public:
                ZonexStaticBitmap(wxWindow* parent,int iWidth,int iHeight);
                ~ZonexStaticBitmap();
                void leftMouseDown(wxMouseEvent& event);
        private:       
          DECLARE_EVENT_TABLE()
}; 

BEGIN_EVENT_TABLE(ZonexStaticBitmap, wxStaticBitmap)
   EVT_LEFT_UP(ZonexStaticBitmap::leftMouseDown)
END_EVENT_TABLE()


ZonexStaticBitmap::ZonexStaticBitmap(wxWindow* parent,int iWidth,int iHeight)
                  : wxStaticBitmap(parent,-1,wxBitmap(iWidth,iHeight))
{
	wxMemoryDC dc;
	dc.SelectObject(GetBitmap());
	dc.SetBackground(*wxWHITE_BRUSH);
	dc.Clear();
	dc.SetBrush(wxBrush(wxColour(0,0,127), wxCROSSDIAG_HATCH));
	dc.DrawRectangle(0,0,iWidth, iHeight);
}

ZonexStaticBitmap::~ZonexStaticBitmap()
{
}

void ZonexStaticBitmap::leftMouseDown(wxMouseEvent& event)
{
	wxMessageBox("MouseDown works fine");
	MainController::GetInstance()->leftMouseDown(event.GetPosition());
}

class MyFrame : public wxFrame
{
public:
	MyFrame(wxWindow * parent);
	ZonexStaticBitmap * bitmap;
	DECLARE_EVENT_TABLE()	
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
END_EVENT_TABLE()

MyFrame::MyFrame(wxWindow * parent)
: wxFrame(parent, -1, "Test", wxPoint(100,100), wxSize(400,300))
{		
	wxBoxSizer * sizer = new wxBoxSizer(wxVERTICAL);
	SetSizer(sizer);
	bitmap = new ZonexStaticBitmap(this, 400,300);
	sizer->Add(bitmap, 1);	
	Centre();
}

class MyApp : public wxApp
{
public:
	virtual bool OnInit();
	virtual int OnExit();
};

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	MainController::CreateInstance();
	MyFrame * frame = new MyFrame(NULL);	
	frame->Show();	
	SetTopWindow(frame);
	return true;
}

int MyApp::OnExit()
{	
	MainController::DestroyInstance();
	return 1;
}
I don't know what is MainController, so I created the namespace with the same name to make the source compatible with your sample.
It works fine with MSVC.NET 2003 and Dev-Cpp 4.9.9.2 - beta 6.7 + wxWidgets 2.6 DevPack

Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

Post by Sterl » Sat May 28, 2005 3:37 pm

Thanks for the code. I will analyse it and adapt to my project. :wink:
I'll keep you inform if it works....

Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

Post by Sterl » Sat May 28, 2005 5:01 pm

Ok i've found the problem. Your code works well. But in my project the ZonexStaticBitmap is placed into a wxStaticBox and the staticBox seems to stop all mouse event. That was not the case with the 2.4.2 devpacks.

Try this code in your exemple and you will see the problem....

Code: Select all

MyFrame::MyFrame(wxWindow * parent)
: wxFrame(parent, -1, "Test", wxPoint(100,100), wxSize(400,300))
{               
  wxStaticBox* zonexStaticBox = new wxStaticBox(this,-1,wxString("Window label"),wxDefaultPosition,wxDefaultSize);
  wxStaticBoxSizer* zonexStaticBoxSizer = new wxStaticBoxSizer(zonexStaticBox,wxHORIZONTAL);
  SetSizer(zonexStaticBoxSizer);
  bitmap = new ZonexStaticBitmap(this, 400,300);
  zonexStaticBoxSizer->Add(bitmap, 1);       
  Centre();
}
do you have a way to solve it ? :wink:

User avatar
T-Rex
Moderator
Moderator
Posts: 1195
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Post by T-Rex » Sat May 28, 2005 6:02 pm

I looked at the documentation and found this:
Also, please note that because of this, the order in which you create new controls is important. Create your wxStaticBox control before any siblings that are to appear inside the wxStaticBox in order to preserve the correct Z-Order of controls.
But it doesn't work.... :?:

But if I create the wxStaticBox after bitmap, everything works fine:

Code: Select all

MyFrame::MyFrame(wxWindow * parent)
: wxFrame(parent, -1, "Test", wxPoint(100,100), wxSize(400,300))
{  
  
  bitmap = new ZonexStaticBitmap(this, 400,300);
  wxStaticBox* zonexStaticBox = new wxStaticBox(this,10001,wxString("Window label"),wxDefaultPosition,wxSize(400,300));
  wxStaticBoxSizer* zonexStaticBoxSizer = new wxStaticBoxSizer(zonexStaticBox,wxHORIZONTAL);
  SetSizer(zonexStaticBoxSizer);  
  zonexStaticBoxSizer->Add(bitmap, 1);         
  Centre();
}

Sterl
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sun Dec 19, 2004 3:07 pm

Post by Sterl » Sat May 28, 2005 6:14 pm

I've put the staticBitmap declaration before staticSizer one and now it works perfectly. Thanks for the advice. :wink:

It may be a bug !

Post Reply