]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/menu.cpp
improved memory liberation (explicitly set to NULL after delete)
[wxWidgets.git] / src / mac / menu.cpp
index 17e8679a5b085b4b117bb69fd553e999d87fb751..e31651102caa61ddb110b34760d1cd802045718a 100644 (file)
@@ -408,112 +408,110 @@ void wxMenu::MacEnableMenu( bool bDoEnable )
 
 bool wxMenu::MacMenuSelect( wxEvtHandler* handler, long when , int macMenuId, int macMenuItemNum )
 {
-  int pos;
-  wxNode *node;
-
-       if ( m_macMenuId == macMenuId )
-       {
-               node = GetMenuItems().Nth(macMenuItemNum-1);
-               if (node) 
-               {
-                       wxMenuItem *pItem = (wxMenuItem*)node->Data();
-
-                       if (pItem->IsCheckable())
-                               pItem->Check(! pItem->IsChecked());
-
-                       wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, pItem->GetId());
-                       event.m_timeStamp = when;
-                       event.SetEventObject(handler);
-               event.SetInt( pItem->GetId() );
-                       {
-                               bool processed = false ;
+    int pos;
+    wxNode *node;
 
+    if ( m_macMenuId == macMenuId )
+    {
+        node = GetMenuItems().Nth(macMenuItemNum-1);
+        if (node) 
+        {
+            wxMenuItem *pItem = (wxMenuItem*)node->Data();
+            
+            if (pItem->IsCheckable())
+                pItem->Check(! pItem->IsChecked());
+            
+            wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, pItem->GetId());
+            event.m_timeStamp = when;
+            event.SetEventObject(handler);
+            event.SetInt( pItem->GetId() );
+            {
+                bool processed = false ;
+                
 #if WXWIN_COMPATIBILITY
-                           // Try a callback
-                           if (m_callback)
-                           {
-                                   (void) (*(m_callback)) (*this, event);
-                                   processed = TRUE;
-                           }
+                // Try a callback
+                if (m_callback)
+                {
+                    (void) (*(m_callback)) (*this, event);
+                    processed = TRUE;
+                }
 #endif                 
-                           // Try the menu's event handler
-                           if ( !processed && handler)
-                           {
-                                   processed = handler->ProcessEvent(event);
-                           }
-                       
-                               // Try the window the menu was popped up from (and up
-                               // through the hierarchy)
-                               if ( !processed && GetInvokingWindow())
-                               processed = GetInvokingWindow()->GetEventHandler()->ProcessEvent(event);
-                       }
-                       return true ;
-               }
-       }
-#ifndef __DARWIN__
-       else if ( macMenuId == kHMHelpMenuID )
-       {
-               int menuItem = firstUserHelpMenuItem-1 ;
-         for (pos = 0, node = GetMenuItems().First(); node; node = node->Next(), pos++) 
-         {     
-               wxMenuItem * pItem = (wxMenuItem *)  node->Data() ;
-               
-               wxMenu *pSubMenu = pItem->GetSubMenu() ;
-                       if ( pSubMenu != NULL )
-                       {
-                       }
-                       else
-                       {
-                               if ( pItem->GetId() != wxApp::s_macAboutMenuItemId )
-                                       ++menuItem ;
-                                       
-                               if ( menuItem == macMenuItemNum )
-                               {
-                                       wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, pItem->GetId());
-                                       event.m_timeStamp = when;
-                                       event.SetEventObject(handler);
-                                       event.SetInt( pItem->GetId() );
-                                       {
-                                               bool processed = false ;
+                // Try the menu's event handler
+                if ( !processed && handler)
+                {
+                    processed = handler->ProcessEvent(event);
+                }
+                
+                // Try the window the menu was popped up from (and up
+                // through the hierarchy)
+                if ( !processed && GetInvokingWindow())
+                    processed = GetInvokingWindow()->GetEventHandler()->ProcessEvent(event);
+            }
+            return true ;
+        }
+    }
+    else if ( macMenuId == kHMHelpMenuID )
+    {
+        int menuItem = firstUserHelpMenuItem-1 ;
+        for (pos = 0, node = GetMenuItems().First(); node; node = node->Next(), pos++) 
+        {      
+            wxMenuItem * pItem = (wxMenuItem *)  node->Data() ;
+            
+            wxMenu *pSubMenu = pItem->GetSubMenu() ;
+            if ( pSubMenu != NULL )
+            {
+            }
+            else
+            {
+                if ( pItem->GetId() != wxApp::s_macAboutMenuItemId )
+                    ++menuItem ;
+                
+                if ( menuItem == macMenuItemNum )
+                {
+                    wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, pItem->GetId());
+                    event.m_timeStamp = when;
+                    event.SetEventObject(handler);
+                    event.SetInt( pItem->GetId() );
+                    {
+                        bool processed = false ;
 #if WXWIN_COMPATIBILITY
-                                           // Try a callback
-                                           if (m_callback)
-                                           {
-                                                   (void) (*(m_callback)) (*this, event);
-                                                   processed = TRUE;
-                                           }
+                        // Try a callback
+                        if (m_callback)
+                        {
+                            (void) (*(m_callback)) (*this, event);
+                            processed = TRUE;
+                        }
 #endif                                 
-                                           // Try the menu's event handler
-                                           if ( !processed && handler)
-                                           {
-                                                   processed = handler->ProcessEvent(event);
-                                           }
-                                       
-                                               // Try the window the menu was popped up from (and up
-                                               // through the hierarchy)
-                                               if ( !processed && GetInvokingWindow())
-                                               processed = GetInvokingWindow()->GetEventHandler()->ProcessEvent(event);
-                                       }
-                                       return true ;
-                               }
-                       }
-         }
-       }
-#endif // __DARWIN__
-
-  for (pos = 0, node = GetMenuItems().First(); node; node = node->Next(), pos++) 
-  {    
-               wxMenuItem * pItem = (wxMenuItem *)  node->Data() ;
+                        // Try the menu's event handler
+                        if ( !processed && handler)
+                        {
+                            processed = handler->ProcessEvent(event);
+                        }
+                        
+                        // Try the window the menu was popped up from (and up
+                        // through the hierarchy)
+                        if ( !processed && GetInvokingWindow())
+                            processed = GetInvokingWindow()->GetEventHandler()->ProcessEvent(event);
+                    }
+                    return true ;
+                }
+            }
+        }
+    }
+    
+    for (pos = 0, node = GetMenuItems().First(); node; node = node->Next(), pos++) 
+    {  
+        wxMenuItem * pItem = (wxMenuItem *)  node->Data() ;
        
-               wxMenu *pSubMenu = pItem->GetSubMenu() ;
-               if ( pSubMenu != NULL )
-               {
-                       if ( pSubMenu->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
-                               return true ;
-               }
-  }
-
-       return false ;
+        wxMenu *pSubMenu = pItem->GetSubMenu() ;
+        if ( pSubMenu != NULL )
+        {
+            if ( pSubMenu->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
+                return true ;
+        }
+    }
+    
+    return false ;
 }
 
 // Menu Bar
@@ -579,7 +577,7 @@ wxMenuBar::~wxMenuBar()
 
 }
 
-void wxMenuBar::Refresh()
+void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect))
 {
     wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
 
@@ -616,27 +614,37 @@ void wxMenuBar::RebuildAccelTable()
 
 #endif // wxUSE_ACCEL
 
-
 void wxMenuBar::MacInstallMenuBar() 
 {
-       if ( s_macInstalledMenuBar == this )
-               return ;
+    if ( s_macInstalledMenuBar == this )
+        return ;
+               
+    wxStAppResource resload ;
                
-       Handle menubar = ::GetNewMBar( kwxMacMenuBarResource ) ;
-       wxString message ;
-       wxCHECK_RET( menubar != NULL, "can't read MBAR resource" );
-       ::SetMenuBar( menubar ) ;
-       ::DisposeHandle( menubar ) ;
-
-               MenuHandle menu = ::GetMenuHandle( kwxMacAppleMenuId ) ;
-               ::AppendResMenu(menu, 'DRVR');
+    Handle menubar = ::GetNewMBar( kwxMacMenuBarResource ) ;
+    wxString message ;
+    wxCHECK_RET( menubar != NULL, "can't read MBAR resource" );
+    ::SetMenuBar( menubar ) ;
+#if TARGET_API_MAC_CARBON
+    ::DisposeMenuBar( menubar ) ;
+#else
+    ::DisposeHandle( menubar ) ;
+#endif
+
+#if TARGET_API_MAC_OS8
+    MenuHandle menu = ::GetMenuHandle( kwxMacAppleMenuId ) ;
+    if ( CountMenuItems( menu ) == 2 )
+    {
+        ::AppendResMenu(menu, 'DRVR');
+    }
+#endif
  
-       for (int i = 0; i < m_menus.GetCount(); i++)
+       for (size_t i = 0; i < m_menus.GetCount(); i++)
        {
-                       Str255  label;
-       wxNode *node;
-       wxMenuItem *item;
-       int pos ;
+            Str255     label;
+            wxNode *node;
+            wxMenuItem *item;
+            int pos ;
                        wxMenu* menu = m_menus[i] , *subMenu = NULL ;
                
                        if( m_titles[i] == "?" || m_titles[i] == "&?"  || m_titles[i] == wxApp::s_macHelpMenuTitleName )
@@ -646,6 +654,11 @@ void wxMenuBar::MacInstallMenuBar()
                          {
                            continue ;
                          }
+
+                   for ( int i = CountMenuItems( mh ) ; i >= firstUserHelpMenuItem ; --i )
+                   {
+                     DeleteMenuItem( mh , i ) ;
+                   }
                                        
                                for (pos = 0 , node = menu->GetMenuItems().First(); node; node = node->Next(), pos++) 
                                {
@@ -704,7 +717,7 @@ void wxMenuBar::MacInstallMenuBar()
                                        }
                                }
                                ::InsertMenu(MAC_WXHMENU(m_menus[i]->GetHMenu()), 0);
-                               for ( int i = 0 ; i < submenus.GetCount() ; ++i )
+                               for ( size_t i = 0 ; i < submenus.GetCount() ; ++i )
                                {
                                  wxMenu* submenu = (wxMenu*) submenus[i] ;
                wxNode *subnode;
@@ -724,7 +737,6 @@ void wxMenuBar::MacInstallMenuBar()
                        }
                }
                ::DrawMenuBar() ;
-
        s_macInstalledMenuBar = this;
 }
 
@@ -836,11 +848,13 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
 
     m_titles.Insert(title, pos);
 
-    menu->Attach(this);
+    Str255 label ;
+               wxMenuItem::MacBuildMenuString( label, NULL , NULL , title , false );
+               UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , label ) ;
 
     if ( IsAttached() )
     {
-       if ( pos == (size_t) -1 )
+       if ( pos == (size_t) -1  || pos + 1 == m_menus.GetCount() )
                {
                        ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
                }
@@ -865,38 +879,33 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
 
 void wxMenuBar::MacMenuSelect(wxEvtHandler* handler, long when , int macMenuId, int macMenuItemNum)
 {
-       // first scan fast for direct commands, i.e. menus which have these commands directly in their own list
-
-       if ( macMenuId == kwxMacAppleMenuId && macMenuItemNum == 1 )
-       {
-                       wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, wxApp::s_macAboutMenuItemId );
-                       event.m_timeStamp = when;
-                       event.SetEventObject(handler);
-       event.SetInt( wxApp::s_macAboutMenuItemId );
-                       handler->ProcessEvent(event);
-       }
-       else
-       {               
-         for (int i = 0; i < m_menus.GetCount() ; i++)
-         {
-               if ( m_menus[i]->MacGetMenuId() == macMenuId
-#ifndef __DARWIN__
-                    || 
-                       ( macMenuId == kHMHelpMenuID && ( m_titles[i] == "?" || m_titles[i] == "&?"  || m_titles[i] == wxApp::s_macHelpMenuTitleName ) )
-#endif
-                       )
-               {
-                       if ( m_menus[i]->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
-                               return ;
-                       else
-                       {
-                               //TODO flag this as an error since it must contain the item
-                               return ;
-                       }
-               }
-               }
+    // first scan fast for direct commands, i.e. menus which have these commands directly in their own list
+    
+    if ( macMenuId == kwxMacAppleMenuId && macMenuItemNum == 1 )
+    {
+        wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, wxApp::s_macAboutMenuItemId );
+        event.m_timeStamp = when;
+        event.SetEventObject(handler);
+        event.SetInt( wxApp::s_macAboutMenuItemId );
+        handler->ProcessEvent(event);
+    }
+    else
+    {          
+        for (size_t i = 0; i < m_menus.GetCount() ; i++)
+        {
+            if ( m_menus[i]->MacGetMenuId() == macMenuId || ( macMenuId == kHMHelpMenuID && ( m_titles[i] == "?" || m_titles[i] == "&?"  || m_titles[i] == wxApp::s_macHelpMenuTitleName ) ) )
+            {
+                if ( m_menus[i]->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
+                    return ;
+                else
+                {
+                    //TODO flag this as an error since it must contain the item
+                    return ;
+                }
+            }
+        }
        
-         for (int i = 0; i < m_menus.GetCount(); i++)
+         for (size_t i = 0; i < m_menus.GetCount(); i++)
          {
                if ( m_menus[i]->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
                {
@@ -946,13 +955,17 @@ bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
         return FALSE;
 
     m_titles.Add(title);
+    
+    Str255 label ;
+               wxMenuItem::MacBuildMenuString( label, NULL , NULL , title , false );
+               UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , label ) ;
 
     if ( IsAttached() )
     {
-               if (s_macInstalledMenuBar == this)
-               {
-                       ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
-               }
+               if (s_macInstalledMenuBar == this)
+               {
+                       ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
+               }
 
 #if wxUSE_ACCEL
         if ( menu->HasAccels() )