X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b823f5a145f4823ce823591824855c34204936e4..ab8247f4b9bfdc56b549020cf10d3fe1d7bcfbcd:/src/msw/menu.cpp diff --git a/src/msw/menu.cpp b/src/msw/menu.cpp index 4a1f194854..77923bb06a 100644 --- a/src/msw/menu.cpp +++ b/src/msw/menu.cpp @@ -9,17 +9,8 @@ // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// - -// ============================================================================ -// headers & declarations -// ============================================================================ - -// wxWindows headers -// ----------------- - #ifdef __GNUG__ #pragma implementation "menu.h" -#pragma implementation "menuitem.h" #endif // For compilers that support precompilation, includes "wx.h". @@ -35,8 +26,8 @@ #include "wx/utils.h" #endif -#if USE_OWNER_DRAWN -#include "wx/ownerdrw.h" +#if wxUSE_OWNER_DRAWN + #include "wx/ownerdrw.h" #endif #include "wx/msw/private.h" @@ -48,9 +39,19 @@ // ---------------------- #include +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- + +// the (popup) menu title has this special id +static const int idMenuTitle = -2; + +// ---------------------------------------------------------------------------- +// wxWindows macros +// ---------------------------------------------------------------------------- #if !USE_SHARED_LIBRARY -IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) -IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) + IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) + IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) #endif // ============================================================================ @@ -72,9 +73,11 @@ wxMenu::wxMenu(const wxString& Title, const wxFunction func) m_hMenu = (WXHMENU) CreatePopupMenu(); m_savehMenu = 0 ; m_topLevelMenu = this; + m_clientData = (void*) NULL; + if (m_title != "") { - Append(-2, m_title) ; + Append(idMenuTitle, m_title) ; AppendSeparator() ; } @@ -82,7 +85,7 @@ wxMenu::wxMenu(const wxString& Title, const wxFunction func) } // The wxWindow destructor will take care of deleting the submenus. -wxMenu::~wxMenu(void) +wxMenu::~wxMenu() { if (m_hMenu) DestroyMenu((HMENU) m_hMenu); @@ -132,7 +135,7 @@ wxMenu::~wxMenu(void) */ } -void wxMenu::Break(void) +void wxMenu::Break() { m_doBreak = TRUE ; } @@ -175,9 +178,8 @@ void wxMenu::Append(wxMenuItem *pItem) } LPCSTR pData; - wxString name(""); -#if USE_OWNER_DRAWN +#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; @@ -188,27 +190,25 @@ void wxMenu::Append(wxMenuItem *pItem) { // menu is just a normal string (passed in data parameter) flags |= MF_STRING; - name = pItem->GetName(); - pData = (const char*) name; + pData = pItem->GetName(); } - // VZ: what does this magic -2 mean? I just copied the code but have no idea - // about what it does... ### - if ( pItem->GetId() == -2 ) { - flags |= MF_DISABLED | MF_GRAYED; + // visually select the menu title + if ( id == idMenuTitle ) + { + // TODO use SetMenuItemInfo(MFS_DEFAULT) to put it in bold face } HMENU hMenu = (HMENU)((m_hMenu == 0) ? m_savehMenu : m_hMenu); - if ( !AppendMenu(hMenu, flags, id, pData) ) { - // wxLogLastError("AppendMenu"); + wxLogLastError("AppendMenu"); } m_noItems++; } -void wxMenu::AppendSeparator(void) +void wxMenu::AppendSeparator() { Append(new wxMenuItem(this, ID_SEPARATOR)); } @@ -231,10 +231,10 @@ void wxMenu::Append(int Id, const wxString& label, void wxMenu::Delete(int id) { wxNode *node; - wxMenuItem *item; int pos; HMENU menu; + wxMenuItem *item = NULL; for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) { item = (wxMenuItem *)node->Data(); if (item->GetId() == id) @@ -301,18 +301,64 @@ bool wxMenu::Checked(int Id) const void wxMenu::SetTitle(const wxString& label) { - m_title = label ; - if (m_hMenu) - ModifyMenu((HMENU)m_hMenu, 0, - MF_BYPOSITION | MF_STRING | MF_DISABLED, - (UINT)-2, (const char *)m_title); - else if (m_savehMenu) - ModifyMenu((HMENU)m_savehMenu, 0, - MF_BYPOSITION | MF_STRING | MF_DISABLED, - (UINT)-2, (const char *)m_title); + bool hasNoTitle = m_title.IsEmpty(); + m_title = label; + + HMENU hMenu = (HMENU)((m_hMenu == 0) ? m_savehMenu : m_hMenu); + + if ( hasNoTitle ) + { + if ( !label.IsEmpty() ) + { + if ( !InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, + (unsigned)idMenuTitle, m_title) || + !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) + { + wxLogLastError("InsertMenu"); + } + } + } + else + { + if ( label.IsEmpty() ) + { + // remove the title and the separator after it + if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || + !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) + { + wxLogLastError("RemoveMenu"); + } + } + else + { + // modify the title + if ( !ModifyMenu(hMenu, 0u, + MF_BYPOSITION | MF_STRING, + (unsigned)idMenuTitle, m_title) ) + { + wxLogLastError("ModifyMenu"); + } + } + } + +#ifndef __WIN16__ + // put the title string in bold face + if ( !m_title.IsEmpty() ) + { + MENUITEMINFO mii; + mii.cbSize = sizeof(mii); + mii.fMask = MIIM_STATE; + mii.fState = MFS_DEFAULT; + + if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) ) + { + wxLogLastError("SetMenuItemInfo"); + } + } +#endif } -const wxString& wxMenu::GetTitle() const +const wxString wxMenu::GetTitle() const { return m_title; } @@ -357,8 +403,9 @@ void wxMenu::SetLabel(int Id, const wxString& label) item->SetName(label); } -wxString wxMenu::GetLabel(int Id) const +wxString wxMenu::GetLabel(int id) const { +/* static char tmp[128] ; int len; if (m_hMenu) @@ -369,16 +416,30 @@ wxString wxMenu::GetLabel(int Id) const len = 0 ; tmp[len] = '\0' ; return wxString(tmp) ; + +*/ + wxMenuItem *pItem = FindItemForId(id) ; + if (pItem) + return pItem->GetName() ; + else + return wxEmptyString; } bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) { - wxCommandEvent event(wxEVENT_TYPE_MENU_COMMAND); - event.SetEventObject( this ); - event.SetId( id ); - event.SetInt( id ); - ProcessCommand(event); - return TRUE; + // ignore commands from the menu title + + // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! + if ( id != (WXWORD)idMenuTitle ) + { + wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED); + event.SetEventObject( this ); + event.SetId( id ); + event.SetInt( id ); + ProcessCommand(event); + } + + return TRUE; } // Finds the item id matching the given string, -1 if not found. @@ -446,8 +507,10 @@ void wxMenu::SetHelpString(int itemId, const wxString& helpString) wxString wxMenu::GetHelpString (int itemId) const { wxMenuItem *item = FindItemForId (itemId); - wxString str(""); - return (item == NULL) ? str : item->GetHelp(); + if (item) + return item->GetHelp(); + else + return wxEmptyString; } void wxMenu::ProcessCommand(wxCommandEvent & event) @@ -457,7 +520,7 @@ void wxMenu::ProcessCommand(wxCommandEvent & event) // Try a callback if (m_callback) { - (void) (*(m_callback)) (*this, event); + (void)(*(m_callback))(*this, event); processed = TRUE; } @@ -470,13 +533,14 @@ void wxMenu::ProcessCommand(wxCommandEvent & event) // Try the window the menu was popped up from (and up // through the hierarchy) if ( !processed && GetInvokingWindow()) - processed = GetInvokingWindow()->ProcessEvent(event); + processed = GetInvokingWindow()->GetEventHandler()->ProcessEvent(event); } extern wxMenu *wxCurrentPopupMenu; bool wxWindow::PopupMenu(wxMenu *menu, int x, int y) { menu->SetInvokingWindow(this); + menu->UpdateUI(); HWND hWnd = (HWND) GetHWND(); HMENU hMenu = (HMENU)menu->m_hMenu; @@ -495,10 +559,19 @@ bool wxWindow::PopupMenu(wxMenu *menu, int x, int y) } // Menu Bar -wxMenuBar::wxMenuBar(void) +wxMenuBar::wxMenuBar() { m_eventHandler = this; + m_menuCount = 0; + m_menus = NULL; + m_titles = NULL; + m_menuBarFrame = NULL; + m_hMenu = 0; +} +wxMenuBar::wxMenuBar( long WXUNUSED(style) ) +{ + m_eventHandler = this; m_menuCount = 0; m_menus = NULL; m_titles = NULL; @@ -522,7 +595,7 @@ wxMenuBar::wxMenuBar(int N, wxMenu *Menus[], const wxString Titles[]) m_hMenu = 0; } -wxMenuBar::~wxMenuBar(void) +wxMenuBar::~wxMenuBar() { // In fact, don't want menu to be destroyed before MDI // shuffling has taken place. Let it be destroyed @@ -627,7 +700,7 @@ bool wxMenuBar::Checked(int Id) const if (!item) return FALSE; - int Flag ; + int Flag = 0; if (itemMenu->m_hMenu) Flag=GetMenuState((HMENU)itemMenu->m_hMenu, Id, MF_BYCOMMAND) ; @@ -852,8 +925,11 @@ wxMenuItem *wxMenuBar::FindItemForId (int Id, wxMenu ** itemMenu) const wxMenuItem *item = NULL; int i; for (i = 0; i < m_menuCount; i++) - if ((item = m_menus[i]->FindItemForId (Id, itemMenu))) + { + item = m_menus[i]->FindItemForId (Id, itemMenu); + if (item) return item; + } return NULL; } @@ -900,3 +976,42 @@ WXHMENU wxMenu::GetHMenu() const return 0; } +// Update a menu and all submenus recursively. +// source is the object that has the update event handlers +// defined for it. If NULL, the menu or associated window +// will be used. +void wxMenu::UpdateUI(wxEvtHandler* source) +{ + if (!source && GetInvokingWindow()) + source = GetInvokingWindow()->GetEventHandler(); + if (!source) + source = GetEventHandler(); + if (!source) + source = this; + + wxNode* node = GetItems().First(); + while (node) + { + wxMenuItem* item = (wxMenuItem*) node->Data(); + if ( !item->IsSeparator() ) + { + wxWindowID id = item->GetId(); + wxUpdateUIEvent event(id); + event.SetEventObject( source ); + + if (source->ProcessEvent(event)) + { + if (event.GetSetText()) + SetLabel(id, event.GetText()); + if (event.GetSetChecked()) + Check(id, event.GetChecked()); + if (event.GetSetEnabled()) + Enable(id, event.GetEnabled()); + } + + if (item->GetSubMenu()) + item->GetSubMenu()->UpdateUI(source); + } + node = node->Next(); + } +}