X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/08e78a5412fb62c39d78f3cc13c3af1d97803eca..20125017a4bccd0920ee21e178950b0de0e4d63b:/samples/menu/menu.cpp?ds=sidebyside diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index f0355d0d78..a99f581954 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -41,25 +41,23 @@ // not all ports have support for EVT_CONTEXT_MENU yet, don't define // USE_CONTEXT_MENU for those which don't -#if defined(__WXMOTIF__) || defined(__WXPM__) || defined(__WXX11__) || defined(__WXMGL__) +#if defined(__WXMOTIF__) || defined(__WXPM__) || defined(__WXX11__) #define USE_CONTEXT_MENU 0 #else #define USE_CONTEXT_MENU 1 #endif -// this sample is usefull when new port is developed -// and usually new port has majority of flags turned off +// this sample is useful when a new port is developed +// and usually a new port has majority of flags turned off #if wxUSE_LOG && wxUSE_TEXTCTRL #define USE_LOG_WINDOW 1 #else #define USE_LOG_WINDOW 0 #endif -#if wxUSE_OWNER_DRAWN || defined(__WXGTK__) #include "copy.xpm" -#endif -#ifndef __WXMSW__ +#ifndef wxHAS_IMAGES_IN_RESOURCES #include "../sample.xpm" #endif @@ -90,6 +88,7 @@ protected: void OnClearLog(wxCommandEvent& event); void OnClearLogUpdateUI(wxUpdateUIEvent& event); #endif // USE_LOG_WINDOW + void OnShowDialog(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); @@ -140,13 +139,19 @@ protected: void OnMenuOpen(wxMenuEvent& event) { #if USE_LOG_WINDOW - LogMenuOpenOrClose(event, wxT("opened")); event.Skip(); + LogMenuOpenCloseOrHighlight(event, wxT("opened")); event.Skip(); #endif } void OnMenuClose(wxMenuEvent& event) { #if USE_LOG_WINDOW - LogMenuOpenOrClose(event, wxT("closed")); event.Skip(); + LogMenuOpenCloseOrHighlight(event, wxT("closed")); event.Skip(); +#endif + } + void OnMenuHighlight(wxMenuEvent& event) + { +#if USE_LOG_WINDOW + LogMenuOpenCloseOrHighlight(event, wxT("highlighted")); event.Skip(); #endif } @@ -155,7 +160,9 @@ protected: void OnSize(wxSizeEvent& event); private: - void LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what); +#if USE_LOG_WINDOW + void LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what); +#endif void ShowContextMenu(const wxPoint& pos); wxMenu *CreateDummyMenu(wxString *title); @@ -179,6 +186,51 @@ private: DECLARE_EVENT_TABLE() }; +class MyDialog : public wxDialog +{ +public: + MyDialog(wxWindow* parent); + +#if USE_CONTEXT_MENU + void OnContextMenu(wxContextMenuEvent& event); +#else + void OnRightUp(wxMouseEvent& event) + { ShowContextMenu(event.GetPosition()); } +#endif + + void OnMenuOpen(wxMenuEvent& event) + { +#if USE_LOG_WINDOW + LogMenuOpenCloseOrHighlight(event, wxT("opened")); event.Skip(); +#endif + } + void OnMenuClose(wxMenuEvent& event) + { +#if USE_LOG_WINDOW + LogMenuOpenCloseOrHighlight(event, wxT("closed")); event.Skip(); +#endif + } + void OnMenuHighlight(wxMenuEvent& event) + { +#if USE_LOG_WINDOW + LogMenuOpenCloseOrHighlight(event, wxT("highlighted")); event.Skip(); +#endif + } + +private: +#if USE_LOG_WINDOW + void LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what); +#endif + void ShowContextMenu(const wxPoint& pos); + +#if USE_LOG_WINDOW + // the control used for logging + wxTextCtrl *m_textctrl; +#endif + + DECLARE_EVENT_TABLE() +}; + // A small helper class which intercepts all menu events and logs them class MyEvtHandler : public wxEvtHandler { @@ -208,6 +260,7 @@ enum #if USE_LOG_WINDOW Menu_File_ClearLog = 100, #endif + Menu_File_ShowDialog, Menu_MenuBar_Toggle = 200, Menu_MenuBar_Append, @@ -277,6 +330,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Menu_File_ClearLog, MyFrame::OnClearLog) EVT_UPDATE_UI(Menu_File_ClearLog, MyFrame::OnClearLogUpdateUI) #endif + EVT_MENU(Menu_File_ShowDialog, MyFrame::OnShowDialog) EVT_MENU(Menu_Help_About, MyFrame::OnAbout) @@ -331,10 +385,22 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU_OPEN(MyFrame::OnMenuOpen) EVT_MENU_CLOSE(MyFrame::OnMenuClose) + EVT_MENU_HIGHLIGHT_ALL(MyFrame::OnMenuHighlight) EVT_SIZE(MyFrame::OnSize) END_EVENT_TABLE() +BEGIN_EVENT_TABLE(MyDialog, wxDialog) +#if USE_CONTEXT_MENU + EVT_CONTEXT_MENU(MyDialog::OnContextMenu) +#else + EVT_RIGHT_UP(MyDialog::OnRightUp) +#endif + EVT_MENU_OPEN(MyDialog::OnMenuOpen) + EVT_MENU_CLOSE(MyDialog::OnMenuClose) + EVT_MENU_HIGHLIGHT_ALL(MyDialog::OnMenuHighlight) +END_EVENT_TABLE() + BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler) EVT_MENU(wxID_ANY, MyEvtHandler::OnMenuEvent) END_EVENT_TABLE() @@ -365,8 +431,6 @@ bool MyApp::OnInit() frame->SetStatusText(wxT("Welcome to wxWidgets menu sample")); #endif // wxUSE_STATUSBAR - SetTopWindow(frame); - return true; } @@ -468,13 +532,15 @@ MyFrame::MyFrame() #if USE_LOG_WINDOW wxMenuItem *item = new wxMenuItem(fileMenu, Menu_File_ClearLog, wxT("Clear &log\tCtrl-L")); -#if wxUSE_OWNER_DRAWN || defined(__WXGTK__) item->SetBitmap(copy_xpm); -#endif fileMenu->Append(item); fileMenu->AppendSeparator(); #endif // USE_LOG_WINDOW + fileMenu->Append(Menu_File_ShowDialog, wxT("Show &Dialog\tCtrl-D"), + wxT("Show a dialog")); + fileMenu->AppendSeparator(); + fileMenu->Append(Menu_File_Quit, wxT("E&xit\tAlt-X"), wxT("Quit menu sample")); wxMenu *menubarMenu = new wxMenu; @@ -511,15 +577,15 @@ MyFrame::MyFrame() wxMenu *menuMenu = new wxMenu; menuMenu->Append(Menu_Menu_Append, wxT("&Append menu item\tAlt-A"), - wxT("Append a menu item to the last menu")); + wxT("Append a menu item to the 'Test' menu")); menuMenu->Append(Menu_Menu_AppendSub, wxT("&Append sub menu\tAlt-S"), - wxT("Append a sub menu to the last menu")); + wxT("Append a sub menu to the 'Test' menu")); menuMenu->Append(Menu_Menu_Insert, wxT("&Insert menu item\tAlt-I"), - wxT("Insert a menu item in head of the last menu")); + wxT("Insert a menu item in head of the 'Test' menu")); menuMenu->Append(Menu_Menu_Delete, wxT("&Delete menu item\tAlt-D"), - wxT("Delete the last menu item from the last menu")); + wxT("Delete the last menu item from the 'Test' menu")); menuMenu->Append(Menu_Menu_DeleteSub, wxT("Delete last &submenu\tAlt-K"), - wxT("Delete the last submenu from the last menu")); + wxT("Delete the last submenu from the 'Test' menu")); menuMenu->AppendSeparator(); menuMenu->Append(Menu_Menu_Enable, wxT("&Enable menu item\tAlt-E"), wxT("Enable or disable the last menu item"), true); @@ -581,7 +647,7 @@ MyFrame::MyFrame() m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_textctrl)); wxLogMessage(wxT("Brief explanations: the commands in the \"Menu\" menu ") - wxT("append/insert/delete items to/from the last menu.\n") + wxT("append/insert/delete items to/from the \"Test\" menu.\n") wxT("The commands in the \"Menubar\" menu work with the ") wxT("menubar itself.\n\n") wxT("Right click the band below to test popup menus.\n")); @@ -622,12 +688,13 @@ wxMenu *MyFrame::CreateDummyMenu(wxString *title) wxMenuItem *MyFrame::GetLastMenuItem() const { wxMenuBar *menubar = GetMenuBar(); - wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); + wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test")); + wxCHECK_MSG( menu, NULL, wxT("no 'Test' menu?") ); wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast(); if ( !node ) { - wxLogWarning(wxT("No last item in the last menu!")); + wxLogWarning(wxT("No last item in the 'Test' menu!")); return NULL; } @@ -681,6 +748,12 @@ void MyFrame::OnClearLogUpdateUI(wxUpdateUIEvent& event) #endif // USE_LOG_WINDOW +void MyFrame::OnShowDialog(wxCommandEvent& WXUNUSED(event)) +{ + MyDialog dlg(this); + dlg.ShowModal(); +} + void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { (void)wxMessageBox(wxT("wxWidgets menu sample\n(c) 1999-2001 Vadim Zeitlin"), @@ -693,9 +766,9 @@ void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event)) wxMenuBar *mbar = GetMenuBar(); size_t count = mbar->GetMenuCount(); - if ( count == 2 ) + if ( count == 4 ) { - // don't let delete the first 2 menus + // don't let delete the first 4 menus wxLogError(wxT("Can't delete any more menus")); } else @@ -708,7 +781,9 @@ void MyFrame::OnInsertMenu(wxCommandEvent& WXUNUSED(event)) { wxString title; wxMenu *menu = CreateDummyMenu(&title); - GetMenuBar()->Insert(0, menu, title); + // Insert before the 'Help' menu + // Otherwise repeated Deletes will remove the 'Test' menu + GetMenuBar()->Insert(4, menu, title); } void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event)) @@ -814,7 +889,8 @@ void MyFrame::OnDummy(wxCommandEvent& event) void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event)) { wxMenuBar *menubar = GetMenuBar(); - wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); + wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test")); + wxCHECK_RET( menu, wxT("no 'Test' menu?") ); menu->AppendSeparator(); menu->Append(Menu_Dummy_Third, wxT("&Third dummy item\tCtrl-F3"), @@ -824,8 +900,8 @@ void MyFrame::OnAppendMenuItem(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event)) { wxMenuBar *menubar = GetMenuBar(); - - wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 2); + wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test")); + wxCHECK_RET( menu, wxT("no 'Test' menu?") ); menu->Append(Menu_Dummy_Last, wxT("&Dummy sub menu"), CreateDummyMenu(NULL), wxT("Dummy sub menu help")); @@ -834,7 +910,8 @@ void MyFrame::OnAppendSubMenu(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event)) { wxMenuBar *menubar = GetMenuBar(); - wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); + wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test")); + wxCHECK_RET( menu, wxT("no 'Test' menu?") ); size_t count = menu->GetMenuItemCount(); if ( !count ) @@ -850,7 +927,8 @@ void MyFrame::OnDeleteMenuItem(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDeleteSubMenu(wxCommandEvent& WXUNUSED(event)) { wxMenuBar *menubar = GetMenuBar(); - wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 2); + wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test")); + wxCHECK_RET( menu, wxT("no 'Test' menu?") ); for ( int n = menu->GetMenuItemCount() - 1; n >=0 ; --n ) { @@ -868,7 +946,8 @@ void MyFrame::OnDeleteSubMenu(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnInsertMenuItem(wxCommandEvent& WXUNUSED(event)) { wxMenuBar *menubar = GetMenuBar(); - wxMenu *menu = menubar->GetMenu(menubar->GetMenuCount() - 1); + wxMenu *menu = menubar->GetMenu(menubar->FindMenu("Test")); + wxCHECK_RET( menu, wxT("no 'Test' menu?") ); menu->Insert(0, wxMenuItem::New(menu, Menu_Dummy_Fourth, wxT("Fourth dummy item\tCtrl-F4"))); @@ -889,8 +968,11 @@ void MyFrame::OnCheckMenuItem(wxCommandEvent& WXUNUSED(event)) { wxMenuItem *item = GetLastMenuItem(); + if (item && item->IsCheckable()) + { item->Toggle(); } +} void MyFrame::OnUpdateCheckMenuItemUI(wxUpdateUIEvent& event) { @@ -1118,14 +1200,20 @@ void MyFrame::OnTestRadio(wxCommandEvent& event) } #if USE_LOG_WINDOW -void MyFrame::LogMenuOpenOrClose(const wxMenuEvent& event, const wxChar *what) +void MyFrame::LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what) { wxString msg; msg << wxT("A ") << ( event.IsPopup() ? wxT("popup ") : wxT("") ) << wxT("menu has been ") - << what - << wxT("."); + << what; + + if ( event.GetEventType() == wxEVT_MENU_HIGHLIGHT ) + { + msg << wxT(" (id=") << event.GetId() << wxT(")"); + } + + msg << wxT("."); wxLogStatus(this, msg.c_str()); } @@ -1185,3 +1273,76 @@ void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event)) #endif // __WXUNIVERSAL__ } +// ---------------------------------------------------------------------------- +// MyDialog +// ---------------------------------------------------------------------------- + +MyDialog::MyDialog(wxWindow* parent) + : wxDialog(parent, wxID_ANY, "Test Dialog") +{ +#if USE_LOG_WINDOW + // create the log text window + m_textctrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, + wxDefaultPosition, wxDefaultSize, + wxTE_MULTILINE); + m_textctrl->SetEditable(false); + + m_textctrl->AppendText(wxT("Dialogs do not have menus, but popup menus should function the same\n\n") + wxT("Right click this text ctrl to test popup menus.\n")); +#endif +#ifdef __POCKETPC__ + EnableContextMenu(); +#endif +} + +#if USE_LOG_WINDOW +void MyDialog::LogMenuOpenCloseOrHighlight(const wxMenuEvent& event, const wxChar *what) +{ + wxString msg; + msg << wxT("A ") + << ( event.IsPopup() ? wxT("popup ") : wxT("") ) + << wxT("menu has been ") + << what; + if ( event.GetEventType() == wxEVT_MENU_HIGHLIGHT ) + { + msg << wxT(" (id=") << event.GetId() << wxT(")"); + } + msg << wxT(".\n"); + + m_textctrl->AppendText(msg); +} +#endif // USE_LOG_WINDOW +#if USE_CONTEXT_MENU +void MyDialog::OnContextMenu(wxContextMenuEvent& event) +{ + wxPoint point = event.GetPosition(); + // If from keyboard + if (point.x == -1 && point.y == -1) { + wxSize size = GetSize(); + point.x = size.x / 2; + point.y = size.y / 2; + } else { + point = ScreenToClient(point); + } + ShowContextMenu(point); +} +#endif + +void MyDialog::ShowContextMenu(const wxPoint& pos) +{ + wxMenu menu; + + menu.Append(Menu_Help_About, wxT("&About")); + menu.Append(Menu_Popup_ToBeDeleted, wxT("To be &deleted")); + menu.AppendCheckItem(Menu_Popup_ToBeChecked, wxT("To be &checked")); + menu.Append(Menu_Popup_ToBeGreyed, wxT("To be &greyed"), + wxT("This menu item should be initially greyed out")); + menu.AppendSeparator(); + menu.Append(Menu_File_Quit, wxT("E&xit")); + + menu.Delete(Menu_Popup_ToBeDeleted); + menu.Check(Menu_Popup_ToBeChecked, true); + menu.Enable(Menu_Popup_ToBeGreyed, false); + + PopupMenu(&menu, pos); +}