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 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
39 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
42 const wxString
& strHelp
,
45 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
47 // TO DISCUSS on dev : whether we can veto id 0
48 // wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
50 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
51 // therefore these item must not be translated
52 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
54 m_text
=wxT("Quit\tCtrl+Q") ;
57 m_radioGroup
.start
= -1;
58 m_isRadioGroupStart
= FALSE
;
61 wxMenuItem::~wxMenuItem()
68 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
74 void wxMenuItem::UpdateItemBitmap()
79 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
80 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
81 if( mhandle
== NULL
|| index
== 0)
86 ControlButtonContentInfo info
;
87 wxMacCreateBitmapButton( &info
, m_bitmap
, kControlContentCIconHandle
) ;
88 if ( info
.contentType
!= kControlNoContent
)
90 if ( info
.contentType
== kControlContentCIconHandle
)
91 SetMenuItemIconHandle( mhandle
, index
,
92 kMenuColorIconType
, (Handle
) info
.u
.cIconHandle
) ;
98 void wxMenuItem::UpdateItemStatus()
104 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
107 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
109 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
111 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
114 DisableMenuCommand( NULL
, kHICommandQuit
) ;
116 EnableMenuCommand( NULL
, kHICommandQuit
) ;
120 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
121 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
122 if( mhandle
== NULL
|| index
== 0)
125 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
126 if ( IsCheckable() && IsChecked() )
127 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
129 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
131 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
132 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
133 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
138 void wxMenuItem::UpdateItemText()
143 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
144 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
145 if( mhandle
== NULL
|| index
== 0)
148 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
149 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
150 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
155 void wxMenuItem::Enable(bool bDoEnable
)
157 if ( m_isEnabled
!= bDoEnable
)
159 wxMenuItemBase::Enable( bDoEnable
) ;
163 void wxMenuItem::UncheckRadio()
167 wxMenuItemBase::Check( false ) ;
172 void wxMenuItem::Check(bool bDoCheck
)
174 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
176 if ( m_isChecked
!= bDoCheck
)
178 if ( GetKind() == wxITEM_RADIO
)
182 wxMenuItemBase::Check( bDoCheck
) ;
185 // get the index of this item in the menu
186 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
187 int pos
= items
.IndexOf(this);
188 wxCHECK_RET( pos
!= wxNOT_FOUND
,
189 _T("menuitem not found in the menu items list?") );
191 // get the radio group range
195 if ( m_isRadioGroupStart
)
197 // we already have all information we need
199 end
= m_radioGroup
.end
;
201 else // next radio group item
203 // get the radio group end from the start item
204 start
= m_radioGroup
.start
;
205 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
208 // also uncheck all the other items in this radio group
209 wxMenuItemList::Node
*node
= items
.Item(start
);
210 for ( int n
= start
; n
<= end
&& node
; n
++ )
214 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
216 node
= node
->GetNext();
222 wxMenuItemBase::Check( bDoCheck
) ;
228 void wxMenuItem::SetText(const wxString
& text
)
230 // don't do anything if label didn't change
231 if ( m_text
== text
)
234 wxMenuItemBase::SetText(text
);
242 void wxMenuItem::SetAsRadioGroupStart()
244 m_isRadioGroupStart
= TRUE
;
247 void wxMenuItem::SetRadioGroupStart(int start
)
249 wxASSERT_MSG( !m_isRadioGroupStart
,
250 _T("should only be called for the next radio items") );
252 m_radioGroup
.start
= start
;
255 void wxMenuItem::SetRadioGroupEnd(int end
)
257 wxASSERT_MSG( m_isRadioGroupStart
,
258 _T("should only be called for the first radio item") );
260 m_radioGroup
.end
= end
;
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
268 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
270 return wxStripMenuCodes(text
);
273 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
275 const wxString
& name
,
276 const wxString
& help
,
280 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);