/////////////////////////////////////////////////////////////////////////////
// Name: event.cpp
-// Purpose: wxWindows sample demonstrating different event usage
+// Purpose: wxWidgets sample demonstrating different event usage
// Author: Vadim Zeitlin
// Modified by:
// Created: 31.01.01
// headers
// ----------------------------------------------------------------------------
-#if defined(__GNUG__) && !defined(__APPLE__)
- // DO NOT use event.cpp here, it breaks statics initialization in
- // src/common/event.cpp and nothing works at all then!
- #pragma implementation "eventsample.cpp"
- #pragma interface "eventsample.cpp"
-#endif
-
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#endif
// for all others, include the necessary headers (this file is usually all you
-// need because it includes almost all "standard" wxWindows headers)
+// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
+#ifndef __WXMSW__
+ #include "../sample.xpm"
+#endif
+
// ----------------------------------------------------------------------------
// event constants
// ----------------------------------------------------------------------------
-// declare a custom event type
-//
-// note that in wxWin 2.3+ these macros expand simply into the following code:
-//
-// extern const wxEventType wxEVT_MY_CUSTOM_COMMAND;
-//
-// const wxEventType wxEVT_MY_CUSTOM_COMMAND = wxNewEventType();
-//
-// and you may use this code directly if you don't care about 2.2 compatibility
-BEGIN_DECLARE_EVENT_TYPES()
- DECLARE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND, 7777)
-END_DECLARE_EVENT_TYPES()
-
-DEFINE_EVENT_TYPE(wxEVT_MY_CUSTOM_COMMAND)
+// define a custom event type (we don't need a separate declaration here but
+// usually you would use a matching wxDECLARE_EVENT in a header)
+wxDEFINE_EVENT(wxEVT_MY_CUSTOM_COMMAND, wxCommandEvent);
// it may also be convenient to define an event table macro for this event type
#define EVT_MY_CUSTOM_COMMAND(id, fn) \
DECLARE_EVENT_TABLE_ENTRY( \
- wxEVT_MY_CUSTOM_COMMAND, id, -1, \
- (wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)&fn, \
+ wxEVT_MY_CUSTOM_COMMAND, id, wxID_ANY, \
+ wxCommandEventHandler(fn), \
(wxObject *) NULL \
),
protected:
// number of pushed event handlers
- size_t m_nPush;
+ unsigned m_nPush;
private:
- // any class wishing to process wxWindows events must use this macro
+ // any class wishing to process wxWidgets events must use this macro
DECLARE_EVENT_TABLE()
};
}
private:
- size_t m_level;
+ unsigned m_level;
DECLARE_EVENT_TABLE()
};
};
// ----------------------------------------------------------------------------
-// event tables and other macros for wxWindows
+// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------
-// the event tables connect the wxWindows events with the functions (event
+// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
- EVT_MY_CUSTOM_COMMAND(-1, MyFrame::OnProcessCustom)
+ EVT_MY_CUSTOM_COMMAND(wxID_ANY, MyFrame::OnProcessCustom)
- // this would also work:
- //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, -1, MyFrame::OnProcessCustom)
+ // the line below would also work if OnProcessCustom() were defined as
+ // taking a wxEvent (as required by EVT_CUSTOM) and not wxCommandEvent
+ //EVT_CUSTOM(wxEVT_MY_CUSTOM_COMMAND, wxID_ANY, MyFrame::OnProcessCustom)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
EVT_MENU(Event_Test, MyEvtHandler::OnTest)
END_EVENT_TABLE()
-// Create a new application object: this macro will allow wxWindows to create
+// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also declares the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
+ if ( !wxApp::OnInit() )
+ return false;
+
// create the main application window
- MyFrame *frame = new MyFrame(_T("Event wxWindows Sample"),
+ MyFrame *frame = new MyFrame(_T("Event wxWidgets Sample"),
wxPoint(50, 50), wxSize(600, 340));
// and show it (the frames, unlike simple controls, are not shown when
// created initially)
- frame->Show(TRUE);
+ frame->Show(true);
// success: wxApp::OnRun() will be called which will enter the main message
- // loop and the application will run. If we returned FALSE here, the
+ // loop and the application will run. If we returned false here, the
// application would exit immediately.
- return TRUE;
+ return true;
}
// ----------------------------------------------------------------------------
// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
- : wxFrame((wxFrame *)NULL, -1, title, pos, size)
+ : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
{
+ SetIcon(wxICON(sample));
+
// init members
m_nPush = 0;
wxMenu *menuEvent = new wxMenu;
menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"),
_T("Connect or disconnect the dynamic event handler"),
- TRUE /* checkable */);
+ true /* checkable */);
menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"),
_T("Dynamic event sample - only works after Connect"));
menuEvent->AppendSeparator();
#if wxUSE_STATUSBAR
CreateStatusBar(3);
- SetStatusText(_T("Welcome to wxWindows event sample"));
+ SetStatusText(_T("Welcome to wxWidgets event sample"));
SetStatusText(_T("Dynamic: off"), Status_Dynamic);
SetStatusText(_T("Push count: 0"), Status_Push);
#endif // wxUSE_STATUSBAR
// crashes!
while ( m_nPush-- != 0 )
{
- PopEventHandler(TRUE /* delete handler */);
+ PopEventHandler(true /* delete handler */);
}
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
- // TRUE is to force the frame to close
- Close(TRUE);
+ // true is to force the frame to close
+ Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( wxT("Event sample shows different ways of using events\n")
- wxT("© 2001 Vadim Zeitlin"),
+ wxT("(c) 2001 Vadim Zeitlin"),
wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this );
}
{
if ( event.IsChecked() )
{
- // disconnect
- Connect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED,
- (wxObjectEventFunction)
- (wxEventFunction)
- (wxCommandEventFunction)&MyFrame::OnDynamic);
+ Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
+ wxCommandEventHandler(MyFrame::OnDynamic));
+#if wxUSE_STATUSBAR
SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
SetStatusText(_T("Dynamic: on"), Status_Dynamic);
+#endif // wxUSE_STATUSBAR
}
- else // connect
+ else // disconnect
{
- Disconnect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED);
+ Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
+ wxCommandEventHandler(MyFrame::OnDynamic));
+#if wxUSE_STATUSBAR
SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
SetStatusText(_T("Dynamic: off"), Status_Dynamic);
+#endif // wxUSE_STATUSBAR
}
}
{
PushEventHandler(new MyEvtHandler(++m_nPush));
+#if wxUSE_STATUSBAR
SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
+#endif // wxUSE_STATUSBAR
}
void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
{
wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
- PopEventHandler(TRUE /* delete handler */);
+ PopEventHandler(true /* delete handler */);
m_nPush--;
+#if wxUSE_STATUSBAR
SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
+#endif // wxUSE_STATUSBAR
}
void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))