]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/menu/menu.cpp
wxFileSelector should have file name only, not path, passed to 3rd arg
[wxWidgets.git] / samples / menu / menu.cpp
index 529577f79931bafaaaf75f06c51a9032a5c2b63d..bb2e4de3b6d16a553bca316258550a483ad2e311 100644 (file)
 
 #ifndef WX_PRECOMP
     #include <wx/wx.h>
-
     #include <wx/log.h>
 #endif
 
+#include "copy.xpm"
+
 // ----------------------------------------------------------------------------
 // classes
 // ----------------------------------------------------------------------------
@@ -47,7 +48,9 @@ class MyFrame: public wxFrame
 public:
     MyFrame();
 
-    virtual ~MyFrame() { delete m_menu; }
+    virtual ~MyFrame();
+
+    void LogMenuEvent(const wxCommandEvent& event);
 
     void OnQuit(wxCommandEvent& event);
     void OnAbout(wxCommandEvent& event);
@@ -88,6 +91,25 @@ private:
     DECLARE_EVENT_TABLE()
 };
 
+// A small helper class which intercepts all menu events and logs them
+class MyEvtHandler : public wxEvtHandler
+{
+public:
+    MyEvtHandler(MyFrame *frame) { m_frame = frame; }
+
+    void OnMenuEvent(wxCommandEvent& event)
+    {
+        m_frame->LogMenuEvent(event);
+
+        event.Skip();
+    }
+
+private:
+    MyFrame *m_frame;
+
+    DECLARE_EVENT_TABLE()
+};
+
 // ----------------------------------------------------------------------------
 // constants
 // ----------------------------------------------------------------------------
@@ -164,6 +186,10 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_RIGHT_DOWN(MyFrame::OnRightDown)
 END_EVENT_TABLE()
 
+BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
+    EVT_MENU(-1, MyEvtHandler::OnMenuEvent)
+END_EVENT_TABLE()
+
 // ============================================================================
 // implementation
 // ============================================================================
@@ -202,11 +228,15 @@ MyFrame::MyFrame()
     m_menu = NULL;
     m_countDummy = 0;
 
-    CreateStatusBar();
+    CreateStatusBar(2);
 
     // create the menubar
     wxMenu *fileMenu = new wxMenu;
     fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit toolbar sample" );
+    
+    wxMenuItem *bitmap_menu_item = new wxMenuItem( fileMenu, Menu_File_Quit, "Quit with &bitmap\tAlt-Q" );
+    bitmap_menu_item->SetBitmap( wxBitmap( copy_xpm ) );
+    fileMenu->Append( bitmap_menu_item ); 
 
     wxMenu *menubarMenu = new wxMenu;
     menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A",
@@ -267,6 +297,17 @@ MyFrame::MyFrame()
 
     // associate the menu bar with the frame
     SetMenuBar(menuBar);
+
+    // intercept all menu events and log them in this custom event handler
+    PushEventHandler(new MyEvtHandler(this));
+}
+
+MyFrame::~MyFrame()
+{
+    delete m_menu;
+
+    // delete the event handler installed in ctor
+    PopEventHandler(TRUE);
 }
 
 wxMenu *MyFrame::CreateDummyMenu(wxString *title)
@@ -302,6 +343,25 @@ wxMenuItem *MyFrame::GetLastMenuItem() const
     }
 }
 
+void MyFrame::LogMenuEvent(const wxCommandEvent& event)
+{
+    int id = event.GetId();
+    if (!GetMenuBar()->FindItem(id))
+        return;
+    wxString msg = wxString::Format("Menu command %d", id);
+    if ( GetMenuBar()->FindItem(id)->IsCheckable() )
+    {
+        msg += wxString::Format(" (the item is currently %schecked)",
+                                event.IsChecked() ? "" : "not ");
+    }
+
+    SetStatusText(msg, 1);
+}
+
+// ----------------------------------------------------------------------------
+// menu callbacks
+// ----------------------------------------------------------------------------
+
 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 {
     Close(TRUE);