]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/menu/menu.cpp
1. fixed wxBase RPM template
[wxWidgets.git] / samples / menu / menu.cpp
index 26cb1baeac73284ebd759a19b69c7018e5f14480..365e990140d582c6bdf90e7371ea975eec6c6460 100644 (file)
 
 #ifndef WX_PRECOMP
     #include <wx/wx.h>
-
     #include <wx/log.h>
 #endif
 
+#if !wxUSE_MENUS
+    // nice try...
+    #error "menu sample requires wxUSE_MENUS=1"
+#endif // wxUSE_MENUS
+
+#include "copy.xpm"
+
+#ifdef __WXUNIVERSAL__
+    #include "wx/univ/theme.h"
+
+    WX_USE_THEME(win32);
+    WX_USE_THEME(gtk);
+
+    // not implemented yet
+    #define wxMessageBox
+#endif // __WXUNIVERSAL__
+
 // ----------------------------------------------------------------------------
 // classes
 // ----------------------------------------------------------------------------
@@ -47,7 +63,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 +106,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 +201,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
 // ============================================================================
@@ -183,7 +224,9 @@ bool MyApp::OnInit()
 
     frame->Show(TRUE);
 
+#if wxUSE_STATUSBAR
     frame->SetStatusText("Hello, wxWindows");
+#endif // wxUSE_STATUSBAR
 
     SetTopWindow(frame);
 
@@ -202,11 +245,18 @@ MyFrame::MyFrame()
     m_menu = NULL;
     m_countDummy = 0;
 
-    CreateStatusBar();
+#if wxUSE_STATUSBAR
+    CreateStatusBar(2);
+#endif // wxUSE_STATUSBAR
 
     // create the menubar
     wxMenu *fileMenu = new wxMenu;
-    fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit toolbar sample" );
+    fileMenu->Append(Menu_File_Quit, "E&xit\tAlt-X", "Quit toolbar sample");
+
+    wxMenuItem *itemBitmap = new wxMenuItem(fileMenu, Menu_File_Quit,
+                                            "Quit with &bitmap\tAlt-Q");
+    itemBitmap->SetBitmap(wxBitmap(copy_xpm));
+    fileMenu->Append(itemBitmap);
 
     wxMenu *menubarMenu = new wxMenu;
     menubarMenu->Append(Menu_MenuBar_Append, "&Append menu\tCtrl-A",
@@ -267,6 +317,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 +363,27 @@ 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 ");
+    }
+
+#if wxUSE_STATUSBAR
+    SetStatusText(msg, 1);
+#endif // wxUSE_STATUSBAR
+}
+
+// ----------------------------------------------------------------------------
+// menu callbacks
+// ----------------------------------------------------------------------------
+
 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 {
     Close(TRUE);
@@ -408,11 +490,11 @@ void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event))
 {
     wxMenuBar *menubar = GetMenuBar();
-    
+
     wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1);
-    
-    menu->Append(Menu_Dummy_Last, "Dummy sub menu\tCtrl-F2",
-                 CreateDummyMenu(NULL));
+
+    menu->Append(Menu_Dummy_Last, "Dummy sub menu",
+                 CreateDummyMenu(NULL), "Dummy sub menu help");
 }
 
 void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event))
@@ -460,6 +542,8 @@ void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event))
 
 void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event)
 {
+    wxLogNull nolog;
+
     wxMenuItem *item = GetLastMenuItem();
 
     event.Enable(item && item->IsCheckable());
@@ -496,7 +580,7 @@ void MyFrame::OnGetMenuItemInfo(wxCommandEvent& WXUNUSED(event))
         msg << "The item is " << (item->IsEnabled() ? "enabled"
                                                     : "disabled")
             << '\n';
-                                            
+
         if ( item->IsCheckable() )
         {
             msg << "It is checkable and " << (item->IsChecked() ? "" : "un")
@@ -573,9 +657,16 @@ void MyFrame::OnRightDown(wxMouseEvent &event )
     menu.AppendSeparator();
     menu.Append(Menu_File_Quit, "E&xit");
 
-    //menu.Delete(Menu_Popup_ToBeDeleted);
+    menu.Delete(Menu_Popup_ToBeDeleted);
     menu.Check(Menu_Popup_ToBeChecked, TRUE);
     menu.Enable(Menu_Popup_ToBeGreyed, FALSE);
 
     PopupMenu( &menu, event.GetX(), event.GetY() );
+
+    // test for destroying items in popup menus
+#if 0
+    menu.Destroy(Menu_Popup_Submenu);
+
+    PopupMenu( &menu, event.GetX(), event.GetY() );
+#endif // 0
 }