X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9f104fbfce353d62694ea0b191b186e5467dc916..9abe166a3073d9e9ceb0110429a45adc6517d76f:/src/msw/menu.cpp diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index 66cf7c98ef..4cca3f9e59 100644 --- a/src/msw/menu.cpp +++ b/src/msw/menu.cpp @@ -32,6 +32,7 @@ #include "wx/frame.h" #include "wx/menu.h" #include "wx/utils.h" + #include "wx/intl.h" #endif #if wxUSE_OWNER_DRAWN @@ -68,10 +69,6 @@ static const int idMenuTitle = -2; IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) #endif -// convenience macros -#define GetHMENU() ((HMENU)GetHMenu()) -#define GetHMenuOf(menu) ((HMENU)menu->GetHMenu()) - // ============================================================================ // implementation // ============================================================================ @@ -81,9 +78,9 @@ static const int idMenuTitle = -2; // --------------------------------------------------------------------------- // Construct a menu with optional title (then use append) -wxMenu::wxMenu(const wxString& title, const wxFunction func) - : m_title(title) +void wxMenu::Init(const wxString& title, const wxFunction func ) { + m_title = title; m_parent = NULL; m_eventHandler = this; m_pInvokingWindow = NULL; @@ -101,9 +98,7 @@ wxMenu::wxMenu(const wxString& title, const wxFunction func) AppendSeparator() ; } -#if WXWIN_COMPATIBILITY Callback(func); -#endif } // The wxWindow destructor will take care of deleting the submenus. @@ -143,11 +138,12 @@ void wxMenu::Break() // function appends a new item or submenu to the menu void wxMenu::Append(wxMenuItem *pItem) { - wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" ); + wxCHECK_RET( pItem != NULL, wxT("can't append NULL item to the menu") ); +#if wxUSE_ACCEL // check for accelerators: they are given after '\t' wxString label = pItem->GetName(); - int posTab = label.Find('\t'); + int posTab = label.Find(wxT('\t')); if ( posTab != wxNOT_FOUND ) { // parse the accelerator string int keyCode = 0; @@ -162,7 +158,7 @@ void wxMenu::Append(wxMenuItem *pItem) else if ( current == _("shift") ) accelFlags |= wxACCEL_SHIFT; else { - wxLogDebug(_T("Unknown accel modifier: '%s'"), + wxLogDebug(wxT("Unknown accel modifier: '%s'"), current.c_str()); } @@ -174,7 +170,7 @@ void wxMenu::Append(wxMenuItem *pItem) } if ( current.IsEmpty() ) { - wxLogDebug(_T("No accel key found, accel string ignored.")); + wxLogDebug(wxT("No accel key found, accel string ignored.")); } else { if ( current.Len() == 1 ) { @@ -182,18 +178,31 @@ void wxMenu::Append(wxMenuItem *pItem) keyCode = wxToupper(current[0U]); } else { - // it should be a function key + // is it a function key? if ( current[0U] == 'f' && isdigit(current[1U]) && (current.Len() == 2 || (current.Len() == 3 && isdigit(current[2U]))) ) { int n; - sscanf(current.c_str() + 1, "%d", &n); + wxSscanf(current.c_str() + 1, wxT("%d"), &n); keyCode = VK_F1 + n - 1; } else { - wxLogDebug(_T("Unreckognized accel key '%s', accel " - "string ignored."), current.c_str()); + // several special cases + current.MakeUpper(); + if ( current == wxT("DEL") ) { + keyCode = VK_DELETE; + } + else if ( current == wxT("PGUP") ) { + keyCode = VK_PRIOR; + } + else if ( current == wxT("PGDN") ) { + keyCode = VK_NEXT; + } + else { + wxLogDebug(wxT("Unrecognized accel key '%s', accel " + "string ignored."), current.c_str()); + } } } } @@ -205,6 +214,7 @@ void wxMenu::Append(wxMenuItem *pItem) m_accelIds.Add(pItem->GetId()); } } +#endif // wxUSE_ACCEL UINT flags = 0; @@ -238,13 +248,13 @@ void wxMenu::Append(wxMenuItem *pItem) id = pItem->GetId(); } - LPCSTR pData; + LPCTSTR pData; #if wxUSE_OWNER_DRAWN if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? // item draws itself, pass pointer to it in data parameter flags |= MF_OWNERDRAW; - pData = (LPCSTR)pItem; + pData = (LPCTSTR)pItem; } else #endif @@ -254,14 +264,14 @@ void wxMenu::Append(wxMenuItem *pItem) pData = label; } - if ( !::AppendMenu(GetHMENU(), flags, id, pData) ) + if ( !::AppendMenu(GetHmenu(), flags, id, pData) ) { wxLogLastError("AppendMenu"); } else { #ifdef __WIN32__ - if ( id == idMenuTitle ) + if ( (int)id == idMenuTitle ) { // visually select the menu title MENUITEMINFO mii; @@ -269,9 +279,9 @@ void wxMenu::Append(wxMenuItem *pItem) mii.fMask = MIIM_STATE; mii.fState = MFS_DEFAULT; - if ( !SetMenuItemInfo(GetHMENU(), (unsigned)id, FALSE, &mii) ) + if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) ) { - wxLogLastError("SetMenuItemInfo"); + wxLogLastError(wxT("SetMenuItemInfo")); } } #endif // __WIN32__ @@ -318,9 +328,9 @@ void wxMenu::Delete(int id) break; } - wxCHECK_RET( node, "wxMenu::Delete(): item doesn't exist" ); + wxCHECK_RET( node, wxT("wxMenu::Delete(): item doesn't exist") ); - HMENU menu = GetHMENU(); + HMENU menu = GetHmenu(); wxMenu *pSubMenu = item->GetSubMenu(); if ( pSubMenu != NULL ) { @@ -343,6 +353,8 @@ void wxMenu::Delete(int id) delete item; } +#if wxUSE_ACCEL + // --------------------------------------------------------------------------- // accelerator helpers // --------------------------------------------------------------------------- @@ -360,6 +372,8 @@ size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const return count; } +#endif // wxUSE_ACCEL + // --------------------------------------------------------------------------- // wxMenu functions implemented in wxMenuItem // --------------------------------------------------------------------------- @@ -367,7 +381,7 @@ size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const void wxMenu::Enable(int id, bool Flag) { wxMenuItem *item = FindItemForId(id); - wxCHECK_RET( item != NULL, "can't enable non-existing menu item" ); + wxCHECK_RET( item != NULL, wxT("can't enable non-existing menu item") ); item->Enable(Flag); } @@ -375,7 +389,7 @@ void wxMenu::Enable(int id, bool Flag) bool wxMenu::IsEnabled(int id) const { wxMenuItem *item = FindItemForId(id); - wxCHECK_MSG( item != NULL, FALSE, "invalid item id" ); + wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") ); return item->IsEnabled(); } @@ -383,7 +397,7 @@ bool wxMenu::IsEnabled(int id) const void wxMenu::Check(int id, bool Flag) { wxMenuItem *item = FindItemForId(id); - wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" ); + wxCHECK_RET( item != NULL, wxT("can't get status of non-existing menu item") ); item->Check(Flag); } @@ -391,7 +405,7 @@ void wxMenu::Check(int id, bool Flag) bool wxMenu::IsChecked(int id) const { wxMenuItem *item = FindItemForId(id); - wxCHECK_MSG( item != NULL, FALSE, "invalid item id" ); + wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") ); return item->IsChecked(); } @@ -399,7 +413,7 @@ bool wxMenu::IsChecked(int id) const void wxMenu::SetLabel(int id, const wxString& label) { wxMenuItem *item = FindItemForId(id) ; - wxCHECK_RET( item, "wxMenu::SetLabel: no such item" ); + wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); item->SetName(label); } @@ -411,7 +425,7 @@ wxString wxMenu::GetLabel(int id) const if (pItem) label = pItem->GetName() ; else - wxFAIL_MSG("wxMenu::GetLabel: item doesn't exist"); + wxFAIL_MSG(wxT("wxMenu::GetLabel: item doesn't exist")); return label; } @@ -422,7 +436,7 @@ void wxMenu::SetHelpString(int itemId, const wxString& helpString) if (item) item->SetHelp(helpString); else - wxFAIL_MSG("wxMenu::SetHelpString: item doesn't exist"); + wxFAIL_MSG(wxT("wxMenu::SetHelpString: item doesn't exist")); } wxString wxMenu::GetHelpString (int itemId) const @@ -432,7 +446,7 @@ wxString wxMenu::GetHelpString (int itemId) const if (item) help = item->GetHelp(); else - wxFAIL_MSG("wxMenu::GetHelpString: item doesn't exist"); + wxFAIL_MSG(wxT("wxMenu::GetHelpString: item doesn't exist")); return help; } @@ -446,7 +460,7 @@ void wxMenu::SetTitle(const wxString& label) bool hasNoTitle = m_title.IsEmpty(); m_title = label; - HMENU hMenu = GetHMENU(); + HMENU hMenu = GetHmenu(); if ( hasNoTitle ) { @@ -456,7 +470,7 @@ void wxMenu::SetTitle(const wxString& label) (unsigned)idMenuTitle, m_title) || !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) { - wxLogLastError("InsertMenu"); + wxLogLastError(wxT("InsertMenu")); } } } @@ -530,14 +544,12 @@ bool wxMenu::ProcessCommand(wxCommandEvent & event) { bool processed = FALSE; -#if WXWIN_COMPATIBILITY // Try a callback if (m_callback) { (void)(*(m_callback))(*this, event); processed = TRUE; } -#endif // WXWIN_COMPATIBILITY // Try the menu's event handler if ( !processed && GetEventHandler()) @@ -615,32 +627,11 @@ wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const // other // --------------------------------------------------------------------------- -bool wxWindow::PopupMenu(wxMenu *menu, int x, int y) -{ - menu->SetInvokingWindow(this); - menu->UpdateUI(); - - HWND hWnd = (HWND) GetHWND(); - HMENU hMenu = (HMENU)menu->GetHMenu(); - POINT point; - point.x = x; - point.y = y; - ::ClientToScreen(hWnd, &point); - wxCurrentPopupMenu = menu; - ::TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hWnd, NULL); - wxYield(); - wxCurrentPopupMenu = NULL; - - menu->SetInvokingWindow(NULL); - - return TRUE; -} - void wxMenu::Attach(wxMenuBar *menubar) { // menu can be in at most one menubar because otherwise they would both // delete the menu pointer - wxASSERT_MSG( !m_menuBar, "menu belongs to 2 menubars, expect a crash" ); + wxASSERT_MSG( !m_menuBar, wxT("menu belongs to 2 menubars, expect a crash") ); m_menuBar = menubar; m_savehMenu = m_hMenu; @@ -649,7 +640,7 @@ void wxMenu::Attach(wxMenuBar *menubar) void wxMenu::Detach() { - wxASSERT_MSG( m_menuBar, "can't detach menu if it's not attached" ); + wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") ); m_hMenu = m_savehMenu; m_savehMenu = 0; @@ -712,14 +703,17 @@ wxMenuBar::~wxMenuBar() void wxMenuBar::Refresh() { - wxCHECK_RET( m_menuBarFrame, "can't refresh a menubar withotu a frame" ); + wxCHECK_RET( m_menuBarFrame, wxT("can't refresh a menubar withotu a frame") ); DrawMenuBar((HWND)m_menuBarFrame->GetHWND()) ; } WXHMENU wxMenuBar::Create() { - wxCHECK_MSG( !m_hMenu, TRUE, "menubar already created" ); + if (m_hMenu != 0 ) + return m_hMenu; + + wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") ); m_hMenu = (WXHMENU)::CreateMenu(); @@ -754,7 +748,7 @@ void wxMenuBar::Enable(int id, bool enable) wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_RET( item, "attempt to enable an item which doesn't exist" ); + wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); item->Enable(enable); } @@ -773,8 +767,8 @@ void wxMenuBar::Check(int id, bool check) wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_RET( item, "attempt to check an item which doesn't exist" ); - wxCHECK_RET( item->IsCheckable(), "attempt to check an uncheckable item" ); + wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); + wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); item->Check(check); } @@ -784,9 +778,9 @@ bool wxMenuBar::IsChecked(int id) const wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_MSG( item, FALSE, "wxMenuBar::IsChecked(): no such item" ); + wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") ); - int flag = ::GetMenuState(GetHMenuOf(itemMenu), id, MF_BYCOMMAND); + int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND); return (flag & MF_CHECKED) != 0; } @@ -796,11 +790,12 @@ bool wxMenuBar::IsEnabled(int id) const wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_MSG( item, FALSE, "wxMenuBar::IsEnabled(): no such item" ); + wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") ); - int flag = ::GetMenuState(GetHMenuOf(itemMenu), id, MF_BYCOMMAND) ; + int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND) ; - return (flag & MF_ENABLED) != 0; + // don't "and" with MF_ENABLED because its value is 0 + return (flag & MF_DISABLED) == 0; } void wxMenuBar::SetLabel(int id, const wxString& label) @@ -808,7 +803,7 @@ void wxMenuBar::SetLabel(int id, const wxString& label) wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_RET( item, "wxMenuBar::SetLabel(): no such item" ); + wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); item->SetName(label); } @@ -818,7 +813,7 @@ wxString wxMenuBar::GetLabel(int id) const wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_MSG( item, "", "wxMenuBar::GetLabel(): no such item" ); + wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetLabel(): no such item") ); return item->GetName(); } @@ -828,7 +823,7 @@ void wxMenuBar::SetHelpString (int id, const wxString& helpString) wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_RET( item, "wxMenuBar::SetHelpString(): no such item" ); + wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); item->SetHelp(helpString); } @@ -838,7 +833,7 @@ wxString wxMenuBar::GetHelpString (int id) const wxMenu *itemMenu = NULL; wxMenuItem *item = FindItemForId(id, &itemMenu) ; - wxCHECK_MSG( item, "", "wxMenuBar::GetHelpString(): no such item" ); + wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetHelpString(): no such item") ); return item->GetHelp(); } @@ -856,7 +851,7 @@ void wxMenuBar::SetLabelTop(int pos, const wxString& label) UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); if ( flagsOld == 0xFFFFFFFF ) { - wxLogLastError("GetMenuState"); + wxLogLastError(wxT("GetMenuState")); return; } @@ -872,8 +867,8 @@ void wxMenuBar::SetLabelTop(int pos, const wxString& label) id = pos; } - if ( ::ModifyMenu(GetHMENU(), pos, MF_BYPOSITION | MF_STRING | flagsOld, - id, label) == 0xFFFFFFFF ) + if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld, + id, label) == (int)0xFFFFFFFF ) { wxLogLastError("ModifyMenu"); } @@ -885,7 +880,7 @@ wxString wxMenuBar::GetLabelTop(int pos) const len++; // for the NUL character wxString label; - ::GetMenuString(GetHMENU(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND); + ::GetMenuString(GetHmenu(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND); label.UngetWriteBuf(); return label; @@ -903,7 +898,7 @@ bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos) if ( ::RemoveMenu((HMENU)m_hMenu, (UINT)pos, MF_BYPOSITION) ) { // VZ: I'm not sure about what's going on here, so I leave an assert - wxASSERT_MSG( m_menus[pos] == a_menu, "what is this parameter for??" ); + wxASSERT_MSG( m_menus[pos] == a_menu, wxT("what is this parameter for??") ); a_menu->Detach(); @@ -920,7 +915,7 @@ bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos) return FALSE; } -bool wxMenuBar::OnAppend(wxMenu *a_menu, const char *title) +bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title) { WXHMENU submenu = a_menu->GetHMenu(); if ( !submenu ) @@ -931,10 +926,10 @@ bool wxMenuBar::OnAppend(wxMenu *a_menu, const char *title) a_menu->Attach(this); - if ( !::AppendMenu(GetHMENU(), MF_POPUP | MF_STRING, + if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, (UINT)submenu, title) ) { - wxLogLastError("AppendMenu"); + wxLogLastError(wxT("AppendMenu")); } Refresh(); @@ -945,6 +940,75 @@ bool wxMenuBar::OnAppend(wxMenu *a_menu, const char *title) // --------------------------------------------------------------------------- // wxMenuBar construction // --------------------------------------------------------------------------- +int wxMenuBar::FindMenu(const wxString& title) +{ + wxString menuTitle = wxStripMenuCodes(title); + for ( int i = 0; i < m_menuCount; i++ ) + { + wxString title = wxStripMenuCodes(m_titles[i]); + if ( menuTitle == title ) + return i; + } + + return wxNOT_FOUND; + +} + + +void wxMenuBar::ReplaceMenu(int pos, wxMenu * new_menu, const wxString& title) +{ + if (m_menuBarFrame) return; + + if ( pos >= 0 && pos < m_menuCount ) + { + wxMenu *old_menu = m_menus[pos]; + m_menus[pos] = new_menu; + delete old_menu; + } + +} + + +void wxMenuBar::Insert(int pos, wxMenu * menu, const wxString& title) +{ + if (m_menuBarFrame) return; + if ( pos < 0 && pos >= m_menuCount ) return; + + m_menuCount ++; + wxMenu **new_menus = new wxMenu *[m_menuCount]; + wxString *new_titles = new wxString[m_menuCount]; + int i; + + for (i = 0; i < pos; i++) + { + new_menus[i] = m_menus[i]; + m_menus[i] = NULL; + new_titles[i] = m_titles[i]; + m_titles[i] = wxT(""); + } + + new_menus[pos] = (wxMenu *)menu; + new_titles[i] = title; + + for (i = pos+1; i < m_menuCount; i++) + { + new_menus[i] = m_menus[i-1]; + m_menus[i-1] = NULL; + new_titles[i] = m_titles[i-1]; + m_titles[i-1] = wxT(""); + } + if (m_menus) + { + delete[]m_menus; + delete[]m_titles; + } + m_menus = new_menus; + m_titles = new_titles; + + menu->SetParent(this); + +} + void wxMenuBar::Append (wxMenu * menu, const wxString& title) { @@ -961,7 +1025,7 @@ void wxMenuBar::Append (wxMenu * menu, const wxString& title) new_menus[i] = m_menus[i]; m_menus[i] = NULL; new_titles[i] = m_titles[i]; - m_titles[i] = ""; + m_titles[i] = wxT(""); } if (m_menus) { @@ -1009,11 +1073,12 @@ void wxMenuBar::Delete(wxMenu * menu, int i) void wxMenuBar::Attach(wxFrame *frame) { - wxASSERT_MSG( !m_menuBarFrame, _T("menubar already attached!") ); + wxASSERT_MSG( !m_menuBarFrame, wxT("menubar already attached!") ); m_menuBarFrame = frame; - // create the accel table - we consider that the toolbar construction is +#if wxUSE_ACCEL + // create the accel table - we consider that the menubar construction is // finished size_t nAccelCount = 0; int i; @@ -1022,19 +1087,31 @@ void wxMenuBar::Attach(wxFrame *frame) nAccelCount += m_menus[i]->GetAccelCount(); } - wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; - - nAccelCount = 0; - for ( i = 0; i < m_menuCount; i++ ) + if ( nAccelCount ) { - nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); - } + wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; - m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); + nAccelCount = 0; + for ( i = 0; i < m_menuCount; i++ ) + { + nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); + } + + m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); + + delete [] accelEntries; + } +#endif // wxUSE_ACCEL +} - delete [] accelEntries; +void wxMenuBar::Detach() +{ +// ::DestroyMenu((HMENU)m_hMenu); + m_hMenu = (WXHMENU)NULL; + m_menuBarFrame = NULL; } + // --------------------------------------------------------------------------- // wxMenuBar searching for menu items // --------------------------------------------------------------------------- @@ -1090,7 +1167,7 @@ WXHMENU wxMenu::GetHMenu() const else if ( m_savehMenu != 0 ) return m_savehMenu; - wxFAIL_MSG("wxMenu without HMENU"); + wxFAIL_MSG(wxT("wxMenu without HMENU")); return 0; }