]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/event/event.cpp
Add test for Push/PopStatusText().
[wxWidgets.git] / samples / event / event.cpp
index a0647391aee01dcde7ac622b26e1d96d9618a635..d2b7679af15a20def4c5d51fddb015f5194d5159 100644 (file)
@@ -5,7 +5,7 @@
 // Modified by:
 // Created:     31.01.01
 // RCS-ID:      $Id$
-// Copyright:   (c) 2001 Vadim Zeitlin
+// Copyright:   (c) 2001-2009 Vadim Zeitlin
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
     #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, wxID_ANY, \
-        (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxCommandEventFunction, &fn ), \
+        wxCommandEventHandler(fn), \
         (wxObject *) NULL \
     ),
 
@@ -84,6 +77,9 @@ public:
 
     void OnQuit(wxCommandEvent& event);
     void OnAbout(wxCommandEvent& event);
+#ifdef wxHAS_EVENT_BIND
+    void OnBind(wxCommandEvent& event);
+#endif // wxHAS_EVENT_BIND
     void OnConnect(wxCommandEvent& event);
     void OnDynamic(wxCommandEvent& event);
     void OnPushEventHandler(wxCommandEvent& event);
@@ -95,11 +91,37 @@ public:
 
     void OnUpdateUIPop(wxUpdateUIEvent& event);
 
-protected:
+private:
+    // symbolic names for the status bar fields
+    enum
+    {
+        Status_Main = 0,
+        Status_Dynamic,
+        Status_Push
+    };
+
+    void UpdateDynamicStatus(bool on)
+    {
+#if wxUSE_STATUSBAR
+        if ( on )
+        {
+            SetStatusText("You can now use \"Dynamic\" item in the menu");
+            SetStatusText("Dynamic: on", Status_Dynamic);
+        }
+        else
+        {
+            SetStatusText("You can no more use \"Dynamic\" item in the menu");
+            SetStatusText("Dynamic: off", Status_Dynamic);
+        }
+#endif // wxUSE_STATUSBAR
+    }
+
     // number of pushed event handlers
     unsigned m_nPush;
 
-private:
+    // the button to whose event we connect dynamically
+    wxButton *m_btnDynamic;
+
     // any class wishing to process wxWidgets events must use this macro
     DECLARE_EVENT_TABLE()
 };
@@ -112,7 +134,7 @@ public:
 
     void OnTest(wxCommandEvent& event)
     {
-        wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
+        wxLogMessage(wxT("This is the pushed test event handler #%u"), m_level);
 
         // if we don't skip the event, the other event handlers won't get it:
         // try commenting out this line and see what changes
@@ -135,6 +157,7 @@ enum
     // menu items
     Event_Quit = 1,
     Event_About,
+    Event_Bind,
     Event_Connect,
     Event_Dynamic,
     Event_Push,
@@ -143,14 +166,6 @@ enum
     Event_Test
 };
 
-// status bar fields
-enum
-{
-    Status_Main = 0,
-    Status_Dynamic,
-    Status_Push
-};
-
 // ----------------------------------------------------------------------------
 // event tables and other macros for wxWidgets
 // ----------------------------------------------------------------------------
@@ -162,6 +177,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(Event_Quit,  MyFrame::OnQuit)
     EVT_MENU(Event_About, MyFrame::OnAbout)
 
+#ifdef wxHAS_EVENT_BIND
+    EVT_MENU(Event_Bind, MyFrame::OnBind)
+#endif // wxHAS_EVENT_BIND
     EVT_MENU(Event_Connect, MyFrame::OnConnect)
 
     EVT_MENU(Event_Custom, MyFrame::OnFireCustom)
@@ -200,8 +218,11 @@ IMPLEMENT_APP(MyApp)
 // '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 wxWidgets Sample"),
+    MyFrame *frame = new MyFrame(wxT("Event wxWidgets Sample"),
                                  wxPoint(50, 50), wxSize(600, 340));
 
     // and show it (the frames, unlike simple controls, are not shown when
@@ -220,49 +241,65 @@ bool MyApp::OnInit()
 
 // frame constructor
 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
-       : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
+       : wxFrame(NULL, wxID_ANY, title, pos, size)
 {
+    SetIcon(wxICON(sample));
+
     // init members
     m_nPush = 0;
+    m_btnDynamic = NULL;
 
     // create a menu bar
     wxMenu *menuFile = new wxMenu;
 
-    menuFile->Append(Event_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
+    menuFile->Append(Event_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
     menuFile->AppendSeparator();
-    menuFile->Append(Event_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
+    menuFile->Append(Event_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
 
     wxMenu *menuEvent = new wxMenu;
-    menuEvent->Append(Event_Connect, _T("&Connect\tCtrl-C"),
-                     _T("Connect or disconnect the dynamic event handler"),
-                     true /* checkable */);
-    menuEvent->Append(Event_Dynamic, _T("&Dynamic event\tCtrl-D"),
-                      _T("Dynamic event sample - only works after Connect"));
+#ifdef wxHAS_EVENT_BIND
+    menuEvent->AppendCheckItem(Event_Bind, "&Bind\tCtrl-B",
+                               "Bind or unbind a dynamic event handler");
+#endif // wxHAS_EVENT_BIND
+    menuEvent->AppendCheckItem(Event_Connect, wxT("&Connect\tCtrl-C"),
+                     wxT("Connect or disconnect the dynamic event handler"));
+    menuEvent->Append(Event_Dynamic, wxT("&Dynamic event\tCtrl-D"),
+                      wxT("Dynamic event sample - only works after Connect"));
     menuEvent->AppendSeparator();
-    menuEvent->Append(Event_Push, _T("&Push event handler\tCtrl-P"),
-                      _T("Push event handler for test event"));
-    menuEvent->Append(Event_Pop, _T("P&op event handler\tCtrl-O"),
-                      _T("Pop event handler for test event"));
-    menuEvent->Append(Event_Test, _T("Test event\tCtrl-T"),
-                      _T("Test event processed by pushed event handler"));
+    menuEvent->Append(Event_Push, wxT("&Push event handler\tCtrl-P"),
+                      wxT("Push event handler for test event"));
+    menuEvent->Append(Event_Pop, wxT("P&op event handler\tCtrl-O"),
+                      wxT("Pop event handler for test event"));
+    menuEvent->Append(Event_Test, wxT("Test event\tCtrl-T"),
+                      wxT("Test event processed by pushed event handler"));
     menuEvent->AppendSeparator();
-    menuEvent->Append(Event_Custom, _T("Fire c&ustom event\tCtrl-U"),
-                      _T("Generate a custom event"));
+    menuEvent->Append(Event_Custom, wxT("Fire c&ustom event\tCtrl-U"),
+                      wxT("Generate a custom event"));
 
     // now append the freshly created menu to the menu bar...
     wxMenuBar *menuBar = new wxMenuBar();
-    menuBar->Append(menuFile, _T("&File"));
-    menuBar->Append(menuEvent, _T("&Event"));
+    menuBar->Append(menuFile, wxT("&File"));
+    menuBar->Append(menuEvent, wxT("&Event"));
 
     // ... and attach this menu bar to the frame
     SetMenuBar(menuBar);
 
 #if wxUSE_STATUSBAR
     CreateStatusBar(3);
-    SetStatusText(_T("Welcome to wxWidgets event sample"));
-    SetStatusText(_T("Dynamic: off"), Status_Dynamic);
-    SetStatusText(_T("Push count: 0"), Status_Push);
+    SetStatusText(wxT("Welcome to wxWidgets event sample"));
+    SetStatusText(wxT("Dynamic: off"), Status_Dynamic);
+    SetStatusText(wxT("Push count: 0"), Status_Push);
 #endif // wxUSE_STATUSBAR
+
+    wxPanel * const panel = new wxPanel(this);
+    wxSizer * const sizer = new wxBoxSizer(wxHORIZONTAL);
+    const wxSizerFlags centreY(wxSizerFlags().Centre().Border());
+    sizer->Add(new wxStaticText(panel, wxID_ANY,
+        "This button will only work if its handler is dynamically connected"),
+        centreY);
+    m_btnDynamic = new wxButton(panel, Event_Dynamic, "&Dynamic button");
+    sizer->Add(m_btnDynamic, centreY);
+    panel->SetSizer(sizer);
 }
 
 MyFrame::~MyFrame()
@@ -287,49 +324,83 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 
 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
 {
-    wxMessageBox( wxT("Event sample shows different ways of using events\n")
-                  wxT("(c) 2001 Vadim Zeitlin"),
-                  wxT("About Event Sample"), wxOK | wxICON_INFORMATION, this );
+    wxMessageBox("Event sample shows different ways of using events\n"
+                 "(c) 2001-2009 Vadim Zeitlin",
+                 "About wxWidgets Event Sample",
+                 wxOK | wxICON_INFORMATION, this);
 }
 
 // ----------------------------------------------------------------------------
 // dynamic event handling stuff
 // ----------------------------------------------------------------------------
 
-void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
+void MyFrame::OnDynamic(wxCommandEvent& event)
 {
+    wxString origin;
+    if ( event.GetEventObject() == this )
+        origin = "menu item";
+    else if ( event.GetEventObject() == m_btnDynamic )
+        origin = "button";
+    else
+        origin = "unknown event source";
+
     wxMessageBox
     (
-        wxT("This is a dynamic event handler which can be connected ")
-        wxT("and disconnected at run-time."),
-        wxT("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this
+        "This message box is shown from the dynamically connected "
+        "event handler in response to event generated by " + origin,
+        "wxWidgets Event Sample", wxOK | wxICON_INFORMATION, this
     );
 }
 
-void MyFrame::OnConnect(wxCommandEvent& event)
+#ifdef wxHAS_EVENT_BIND
+
+void MyFrame::OnBind(wxCommandEvent& event)
 {
     if ( event.IsChecked() )
     {
-        // disconnect
-        Connect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED,
-                (wxObjectEventFunction)
-                (wxEventFunction)
-                (wxCommandEventFunction)&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
+        // as we bind directly to the button, there is no need to use an id
+        // here: the button will only ever get its own events
+        m_btnDynamic->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnDynamic,
+                           this);
+
+        // but we do need the id for the menu command as the frame gets all of
+        // them
+        Bind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnDynamic, this,
+             Event_Dynamic);
     }
-    else // connect
+    else // disconnect
     {
-        Disconnect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED);
+        m_btnDynamic->Unbind(wxEVT_COMMAND_BUTTON_CLICKED,
+                             &MyFrame::OnDynamic, this);
+        Unbind(wxEVT_COMMAND_MENU_SELECTED, &MyFrame::OnDynamic, this,
+               Event_Dynamic);
+    }
 
-#if wxUSE_STATUSBAR
-        SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
-        SetStatusText(_T("Dynamic: off"), Status_Dynamic);
-#endif // wxUSE_STATUSBAR
+    UpdateDynamicStatus(event.IsChecked());
+}
+
+#endif // wxHAS_EVENT_BIND
+
+void MyFrame::OnConnect(wxCommandEvent& event)
+{
+    if ( event.IsChecked() )
+    {
+        m_btnDynamic->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,
+                              wxCommandEventHandler(MyFrame::OnDynamic),
+                              NULL, this);
+        Connect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
+                wxCommandEventHandler(MyFrame::OnDynamic));
+    }
+    else // disconnect
+    {
+        m_btnDynamic->Disconnect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,
+                                 wxCommandEventHandler(MyFrame::OnDynamic),
+                                 NULL, this);
+        Disconnect(Event_Dynamic, wxEVT_COMMAND_MENU_SELECTED,
+                   wxCommandEventHandler(MyFrame::OnDynamic));
     }
+
+    UpdateDynamicStatus(event.IsChecked());
 }
 
 // ----------------------------------------------------------------------------
@@ -341,25 +412,25 @@ void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
     PushEventHandler(new MyEvtHandler(++m_nPush));
 
 #if wxUSE_STATUSBAR
-    SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
+    SetStatusText(wxString::Format(wxT("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!") );
+    wxCHECK_RET( m_nPush, wxT("this command should be disabled!") );
 
     PopEventHandler(true /* delete handler */);
     m_nPush--;
 
 #if wxUSE_STATUSBAR
-    SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
+    SetStatusText(wxString::Format(wxT("Push count: %u"), m_nPush), Status_Push);
 #endif // wxUSE_STATUSBAR
 }
 
 void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
 {
-    wxLogMessage(_T("This is the test event handler in the main frame"));
+    wxLogMessage(wxT("This is the test event handler in the main frame"));
 }
 
 void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
@@ -380,6 +451,6 @@ void MyFrame::OnFireCustom(wxCommandEvent& WXUNUSED(event))
 
 void MyFrame::OnProcessCustom(wxCommandEvent& WXUNUSED(event))
 {
-    wxLogMessage(_T("Got a custom event!"));
+    wxLogMessage(wxT("Got a custom event!"));
 }