Author Topic: Not catching all mouse events with wxWidgets  (Read 7746 times)

Offline kodestiff

  • Multiple posting newcomer
  • *
  • Posts: 13
Not catching all mouse events with wxWidgets
« on: March 24, 2010, 07:15:27 pm »
Hi  I am am trying to catch mouse movements for a MouseOver function in an app created with Code::Blocks using the wxSmith plugin.  I have stumbled upon a puzzling problem.  EVT_MOUSEWHEEL calling the function in the EventTable works well, but all other macros have no result at all.  And the mousewheel is not really want I want  (I just used it to test...)

Here is a the basic problem code (mostly generated by the fantastic wxSmith plugin)
MouseMain.h
Code
#include <wx/frame.h>
#include <wx/statusbr.h>
//*)

class MouseFrame: public wxFrame
{
    public:

        MouseFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~MouseFrame();

    private:

        //(*Handlers(MouseFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnButton1Click(wxCommandEvent& event);
        void MouseOver(wxMouseEvent& event);
        //*)

        //(*Identifiers(MouseFrame)
        static const long ID_BUTTON1;
        static const long ID_STATICBITMAP1;
        static const long ID_PANEL1;
        static const long idMenuQuit;
        static const long idMenuAbout;
        static const long ID_STATUSBAR1;
        //*)

        //(*Declarations(MouseFrame)
        wxButton* Button1;
        wxStaticBitmap* StaticBitmap1;
        wxPanel* Panel1;
        wxStatusBar* StatusBar1;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // MOUSEMAIN_H

...and MouseMain.cpp
Code
#include "wx_pch.h"
#include "MouseMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(MouseFrame)
#include <wx/bitmap.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(MouseFrame)
const long MouseFrame::ID_BUTTON1 = wxNewId();
const long MouseFrame::ID_STATICBITMAP1 = wxNewId();
const long MouseFrame::ID_PANEL1 = wxNewId();
const long MouseFrame::idMenuQuit = wxNewId();
const long MouseFrame::idMenuAbout = wxNewId();
const long MouseFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(MouseFrame,wxFrame)
    EVT_RIGHT_DCLICK(MouseFrame::MouseOver)
    EVT_MOUSEWHEEL(MouseFrame::MouseOver)
    EVT_MOTION(MouseFrame::MouseOver)
    EVT_RIGHT_DOWN(MouseFrame::MouseOver)
    //(*EventTable(MouseFrame)
    //*)
END_EVENT_TABLE()

MouseFrame::MouseFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(MouseFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxMenuBar* MenuBar1;
    wxFlexGridSizer* FlexGridSizer1;
    wxMenu* Menu2;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(144,392), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
    Button1 = new wxButton(Panel1, ID_BUTTON1, _("TheButton"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    FlexGridSizer1->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticBitmap1 = new wxStaticBitmap(Panel1, ID_STATICBITMAP1, wxBitmap(wxImage(_T("B:\\temp\\mamail.jpg"))), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICBITMAP1"));
    FlexGridSizer1->Add(StaticBitmap1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(FlexGridSizer1);
    FlexGridSizer1->Fit(Panel1);
    FlexGridSizer1->SetSizeHints(Panel1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MouseFrame::OnButton1Click);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&MouseFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&MouseFrame::OnAbout);
    //*)
}

MouseFrame::~MouseFrame()
{
    //(*Destroy(MouseFrame)
    //*)
}

void MouseFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void MouseFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void MouseFrame::OnButton1Click(wxCommandEvent& event)
{
}

void MouseFrame::MouseOver(wxMouseEvent& event){
    wxMessageBox(_("MouseOver event!"));
}

So my big question:
Why are EVT_MOTION, EVT_RIGHT_DOWN or EVT_RIGHT_DCLICK not calling MouseFrame::MouseOver(wxMouseEvent& event) in the way EVT_MOUSEWHEEL does?  :?

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: Not catching all mouse events with wxWidgets
« Reply #1 on: March 24, 2010, 07:28:38 pm »
Here is a the basic problem code (mostly generated by the fantastic wxSmith plugin)

Mostly generated, but not all.  There are no event connections for the mouse-right-down, etc.  At the bottom of the (generated) code there should be something like

    Connect(wxEVT_RIGHT_DOWN,(wxObjectEventFunction)&MouseFrame::MouseOver);

But it isn't there.  Let wxSmith generate the mouse event code for you, it knows where to put the connection code.  You put the event connection code in there by hand in the the

   BEGIN_EVENT_TABLE
   ...
   END_EVENT_TABLE

block, and that is not correct for those events.

Ringo




Offline kodestiff

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: Not catching all mouse events with wxWidgets
« Reply #2 on: March 24, 2010, 07:37:00 pm »
Do you have any theory why the mouse wheel events are being caught then?

I have tried Connect(wxEVT_RIGHT_DOWN,(wxObjectEventFunction)&MouseFrame::MouseOver); , but without success...

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: Not catching all mouse events with wxWidgets
« Reply #3 on: March 24, 2010, 07:59:27 pm »
Are you putting this "Connect(...)" in yourself, or are you letting wxSmith do it?  There is a bunch of trivial code to be set up, and wxSmith does it automatically.

As for the mouse wheel events -- some events can be caught in the BEGIN_EVENT_TABLE block, and some can not.  Apparentely, the mouse wheel event may be caught there.  But the click events can not. (At least not on a wxFrame).  Use the events tab on the property page of wxSmith to add events for the mouse wheel, mouse clicks, etc and it will all work properly.

Ringo

Offline kodestiff

  • Multiple posting newcomer
  • *
  • Posts: 13