1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 #include "wx/wxprec.h"
18 #include "wx/menuitem.h"
19 #include "wx/stockitem.h"
26 #include "wx/mac/uma.h"
27 // ============================================================================
29 // ============================================================================
31 // ----------------------------------------------------------------------------
32 // dynamic classes implementation
33 // ----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
45 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
48 const wxString
& strHelp
,
51 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
53 // TO DISCUSS on dev : whether we can veto id 0
54 // wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
56 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
57 // therefore these item must not be translated
58 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
60 m_text
=wxT("Quit\tCtrl+Q") ;
63 m_radioGroup
.start
= -1;
64 m_isRadioGroupStart
= false;
67 wxMenuItem::~wxMenuItem()
74 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
80 void wxMenuItem::UpdateItemBitmap()
85 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
86 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
87 if( mhandle
== NULL
|| index
== 0)
92 ControlButtonContentInfo info
;
93 wxMacCreateBitmapButton( &info
, m_bitmap
, kControlContentCIconHandle
) ;
94 if ( info
.contentType
!= kControlNoContent
)
96 if ( info
.contentType
== kControlContentCIconHandle
)
97 SetMenuItemIconHandle( mhandle
, index
,
98 kMenuColorIconType
, (Handle
) info
.u
.cIconHandle
) ;
104 void wxMenuItem::UpdateItemStatus()
110 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
113 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
115 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
117 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
120 DisableMenuCommand( NULL
, kHICommandQuit
) ;
122 EnableMenuCommand( NULL
, kHICommandQuit
) ;
126 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
127 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
128 if( mhandle
== NULL
|| index
== 0)
131 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
132 if ( IsCheckable() && IsChecked() )
133 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
135 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
137 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
138 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( m_text
) ;
139 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
144 void wxMenuItem::UpdateItemText()
149 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
150 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
151 if( mhandle
== NULL
|| index
== 0)
154 wxString text
= m_text
;
155 if (text
.IsEmpty() && !IsSeparator())
157 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
158 text
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|wxSTOCK_WITH_MNEMONIC
);
161 UMASetMenuItemText( mhandle
, index
, text
, wxFont::GetDefaultEncoding() ) ;
162 wxAcceleratorEntry
*entry
= wxAcceleratorEntry::Create( text
) ;
163 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
168 void wxMenuItem::Enable(bool bDoEnable
)
170 if ( m_isEnabled
!= bDoEnable
)
172 wxMenuItemBase::Enable( bDoEnable
) ;
176 void wxMenuItem::UncheckRadio()
180 wxMenuItemBase::Check( false ) ;
185 void wxMenuItem::Check(bool bDoCheck
)
187 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
189 if ( m_isChecked
!= bDoCheck
)
191 if ( GetKind() == wxITEM_RADIO
)
195 wxMenuItemBase::Check( bDoCheck
) ;
198 // get the index of this item in the menu
199 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
200 int pos
= items
.IndexOf(this);
201 wxCHECK_RET( pos
!= wxNOT_FOUND
,
202 _T("menuitem not found in the menu items list?") );
204 // get the radio group range
208 if ( m_isRadioGroupStart
)
210 // we already have all information we need
212 end
= m_radioGroup
.end
;
214 else // next radio group item
216 // get the radio group end from the start item
217 start
= m_radioGroup
.start
;
218 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
221 // also uncheck all the other items in this radio group
222 wxMenuItemList::Node
*node
= items
.Item(start
);
223 for ( int n
= start
; n
<= end
&& node
; n
++ )
227 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
229 node
= node
->GetNext();
235 wxMenuItemBase::Check( bDoCheck
) ;
241 void wxMenuItem::SetText(const wxString
& text
)
243 // don't do anything if label didn't change
244 if ( m_text
== text
)
247 wxMenuItemBase::SetText(text
);
255 void wxMenuItem::SetAsRadioGroupStart()
257 m_isRadioGroupStart
= true;
260 void wxMenuItem::SetRadioGroupStart(int start
)
262 wxASSERT_MSG( !m_isRadioGroupStart
,
263 _T("should only be called for the next radio items") );
265 m_radioGroup
.start
= start
;
268 void wxMenuItem::SetRadioGroupEnd(int end
)
270 wxASSERT_MSG( m_isRadioGroupStart
,
271 _T("should only be called for the first radio item") );
273 m_radioGroup
.end
= end
;
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
281 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
283 return wxStripMenuCodes(text
);
286 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
288 const wxString
& name
,
289 const wxString
& help
,
293 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);