1 /////////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxMenuItem implementation 
   4 // Author:      Stefan Csomor 
   8 // Copyright:   (c) Stefan Csomor 
   9 // Licence:     wxWindows licence 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  13 // headers & declarations 
  14 // ============================================================================ 
  18 #include "wx/menuitem.h" 
  20 #include "wx/mac/uma.h" 
  21 // ============================================================================ 
  23 // ============================================================================ 
  25 // ---------------------------------------------------------------------------- 
  26 // dynamic classes implementation 
  27 // ---------------------------------------------------------------------------- 
  29 #if !USE_SHARED_LIBRARY 
  30   IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
) 
  31 #endif  //USE_SHARED_LIBRARY 
  33 // ---------------------------------------------------------------------------- 
  35 // ---------------------------------------------------------------------------- 
  41 wxMenuItem::wxMenuItem(wxMenu 
*pParentMenu
, 
  44                        const wxString
& strHelp
, 
  47           : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
) 
  49     // TO DISCUSS on dev : whether we can veto id 0 
  50     // wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ; 
  52     // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines 
  53     // therefore these item must not be translated 
  54     if ( wxStripMenuCodes(m_text
).Upper() ==  wxT("EXIT") ) 
  56         m_text 
=wxT("Quit\tCtrl+Q") ; 
  59     m_radioGroup
.start 
= -1; 
  60     m_isRadioGroupStart 
= FALSE
; 
  63 wxMenuItem::~wxMenuItem()  
  70 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)  
  76 void wxMenuItem::UpdateItemBitmap()  
  81     MenuHandle mhandle 
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ; 
  82     MenuItemIndex index 
= m_parentMenu
->MacGetIndexFromItem( this ) ; 
  83     if( mhandle 
== NULL 
|| index 
== 0) 
  88         ControlButtonContentInfo info 
; 
  89         wxMacCreateBitmapButton( &info 
, m_bitmap 
, kControlContentCIconHandle 
) ; 
  90         if ( info
.contentType 
!= kControlNoContent 
) 
  92             if ( info
.contentType 
== kControlContentCIconHandle 
) 
  93                 SetMenuItemIconHandle( mhandle 
, index 
,  
  94                     kMenuColorIconType 
, (Handle
) info
.u
.cIconHandle 
) ; 
 100 void wxMenuItem::UpdateItemStatus()  
 106     if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
) 
 109             DisableMenuCommand( NULL 
, kHICommandPreferences 
) ; 
 111             EnableMenuCommand( NULL 
, kHICommandPreferences 
) ; 
 113     if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
) 
 116             DisableMenuCommand( NULL 
, kHICommandQuit 
) ; 
 118             EnableMenuCommand( NULL 
, kHICommandQuit 
) ; 
 122         MenuHandle mhandle 
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ; 
 123         MenuItemIndex index 
= m_parentMenu
->MacGetIndexFromItem( this ) ; 
 124         if( mhandle 
== NULL 
|| index 
== 0) 
 127           UMAEnableMenuItem( mhandle 
, index 
, m_isEnabled 
) ; 
 128           if ( IsCheckable() && IsChecked() ) 
 129             ::SetItemMark( mhandle 
, index 
, 0x12 ) ; // checkmark 
 131             ::SetItemMark( mhandle 
, index 
, 0 ) ; // no mark 
 133            UMASetMenuItemText( mhandle 
, index 
, m_text 
, wxFont::GetDefaultEncoding() ) ;  
 134            wxAcceleratorEntry 
*entry 
= wxGetAccelFromString( m_text 
) ; 
 135         UMASetMenuItemShortcut( mhandle 
, index 
, entry 
) ; 
 140 void wxMenuItem::UpdateItemText()  
 145     MenuHandle mhandle 
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ; 
 146     MenuItemIndex index 
= m_parentMenu
->MacGetIndexFromItem( this ) ; 
 147     if( mhandle 
== NULL 
|| index 
== 0) 
 150        UMASetMenuItemText( mhandle 
, index 
, m_text 
, wxFont::GetDefaultEncoding() ) ;  
 151        wxAcceleratorEntry 
*entry 
= wxGetAccelFromString( m_text 
) ; 
 152     UMASetMenuItemShortcut( mhandle 
, index 
, entry 
) ; 
 157 void wxMenuItem::Enable(bool bDoEnable
) 
 159     if ( m_isEnabled 
!= bDoEnable 
)  
 161         wxMenuItemBase::Enable( bDoEnable 
) ; 
 165 void wxMenuItem::UncheckRadio() 
 169         wxMenuItemBase::Check( false ) ; 
 174 void wxMenuItem::Check(bool bDoCheck
) 
 176     wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") ); 
 178     if ( m_isChecked 
!= bDoCheck 
)  
 180         if ( GetKind() == wxITEM_RADIO 
) 
 184                 wxMenuItemBase::Check( bDoCheck 
) ; 
 187                 // get the index of this item in the menu 
 188                 const wxMenuItemList
& items 
= m_parentMenu
->GetMenuItems(); 
 189                 int pos 
= items
.IndexOf(this); 
 190                 wxCHECK_RET( pos 
!= wxNOT_FOUND
, 
 191                              _T("menuitem not found in the menu items list?") ); 
 193                 // get the radio group range 
 197                 if ( m_isRadioGroupStart 
) 
 199                     // we already have all information we need 
 201                     end 
= m_radioGroup
.end
; 
 203                 else // next radio group item 
 205                     // get the radio group end from the start item 
 206                     start 
= m_radioGroup
.start
; 
 207                     end 
= items
.Item(start
)->GetData()->m_radioGroup
.end
; 
 210                 // also uncheck all the other items in this radio group 
 211                 wxMenuItemList::Node 
*node 
= items
.Item(start
); 
 212                 for ( int n 
= start
; n 
<= end 
&& node
; n
++ ) 
 216                         ((wxMenuItem
*)node
->GetData())->UncheckRadio(); 
 218                     node 
= node
->GetNext(); 
 224             wxMenuItemBase::Check( bDoCheck 
) ; 
 230 void wxMenuItem::SetText(const wxString
& text
) 
 232     // don't do anything if label didn't change 
 233     if ( m_text 
== text 
) 
 236     wxMenuItemBase::SetText(text
); 
 244 void wxMenuItem::SetAsRadioGroupStart() 
 246     m_isRadioGroupStart 
= TRUE
; 
 249 void wxMenuItem::SetRadioGroupStart(int start
) 
 251     wxASSERT_MSG( !m_isRadioGroupStart
, 
 252                   _T("should only be called for the next radio items") ); 
 254     m_radioGroup
.start 
= start
; 
 257 void wxMenuItem::SetRadioGroupEnd(int end
) 
 259     wxASSERT_MSG( m_isRadioGroupStart
, 
 260                   _T("should only be called for the first radio item") ); 
 262     m_radioGroup
.end 
= end
; 
 265 // ---------------------------------------------------------------------------- 
 267 // ---------------------------------------------------------------------------- 
 270 wxString 
wxMenuItemBase::GetLabelFromText(const wxString
& text
) 
 272     return wxStripMenuCodes(text
); 
 275 wxMenuItem 
*wxMenuItemBase::New(wxMenu 
*parentMenu
, 
 277                                 const wxString
& name
, 
 278                                 const wxString
& help
, 
 282     return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);