1 ///////////////////////////////////////////////////////////////////////////////
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"
20 #include "wx/menuitem.h"
22 #include "wx/mac/uma.h"
23 // ============================================================================
25 // ============================================================================
27 // ----------------------------------------------------------------------------
28 // dynamic classes implementation
29 // ----------------------------------------------------------------------------
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
33 #endif //USE_SHARED_LIBRARY
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
43 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
46 const wxString
& strHelp
,
49 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
51 wxASSERT_MSG( id
!= 0 || pSubMenu
!= NULL
, wxT("A MenuItem ID of Zero does not work under Mac") ) ;
53 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
54 // therefore these item must not be translated
55 if ( wxStripMenuCodes(m_text
).Upper() == wxT("EXIT") )
57 m_text
=wxT("Quit\tCtrl+Q") ;
60 m_radioGroup
.start
= -1;
61 m_isRadioGroupStart
= FALSE
;
64 wxMenuItem::~wxMenuItem()
71 void wxMenuItem::SetBitmap(const wxBitmap
& bitmap
)
77 void wxMenuItem::UpdateItemBitmap()
82 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
83 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
84 if( mhandle
== NULL
|| index
== 0)
89 ControlButtonContentInfo info
;
90 wxMacCreateBitmapButton( &info
, m_bitmap
) ;
91 if ( info
.contentType
!= kControlNoContent
)
93 if ( info
.contentType
== kControlContentIconRef
)
94 SetMenuItemIconHandle( mhandle
, index
,
95 kMenuIconRefType
, (Handle
) info
.u
.iconRef
) ;
97 wxMacReleaseBitmapButton( &info
) ;
102 void wxMenuItem::UpdateItemStatus()
108 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId
)
111 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
113 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
115 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId
)
118 DisableMenuCommand( NULL
, kHICommandQuit
) ;
120 EnableMenuCommand( NULL
, kHICommandQuit
) ;
124 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
125 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
126 if( mhandle
== NULL
|| index
== 0)
129 UMAEnableMenuItem( mhandle
, index
, m_isEnabled
) ;
130 if ( IsCheckable() && IsChecked() )
131 ::SetItemMark( mhandle
, index
, 0x12 ) ; // checkmark
133 ::SetItemMark( mhandle
, index
, 0 ) ; // no mark
135 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
136 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
137 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
142 void wxMenuItem::UpdateItemText()
147 MenuHandle mhandle
= MAC_WXHMENU(m_parentMenu
->GetHMenu()) ;
148 MenuItemIndex index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
149 if( mhandle
== NULL
|| index
== 0)
152 UMASetMenuItemText( mhandle
, index
, m_text
, wxFont::GetDefaultEncoding() ) ;
153 wxAcceleratorEntry
*entry
= wxGetAccelFromString( m_text
) ;
154 UMASetMenuItemShortcut( mhandle
, index
, entry
) ;
159 void wxMenuItem::Enable(bool bDoEnable
)
161 if (( m_isEnabled
!= bDoEnable
163 // avoid changing menuitem state when menu is disabled
164 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
165 // which in turn causes m_isEnabled to become out of sync with real menuitem state
166 && !(m_parentMenu
&& !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu
->GetHMenu()), 0)) )
167 // always update builtin menuitems
168 || ( GetId() == wxApp::s_macPreferencesMenuItemId
169 || GetId() == wxApp::s_macExitMenuItemId
170 || GetId() == wxApp::s_macAboutMenuItemId
174 wxMenuItemBase::Enable( bDoEnable
) ;
178 void wxMenuItem::UncheckRadio()
182 wxMenuItemBase::Check( false ) ;
187 void wxMenuItem::Check(bool bDoCheck
)
189 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
191 if ( m_isChecked
!= bDoCheck
)
193 if ( GetKind() == wxITEM_RADIO
)
197 wxMenuItemBase::Check( bDoCheck
) ;
200 // get the index of this item in the menu
201 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
202 int pos
= items
.IndexOf(this);
203 wxCHECK_RET( pos
!= wxNOT_FOUND
,
204 _T("menuitem not found in the menu items list?") );
206 // get the radio group range
210 if ( m_isRadioGroupStart
)
212 // we already have all information we need
214 end
= m_radioGroup
.end
;
216 else // next radio group item
218 // get the radio group end from the start item
219 start
= m_radioGroup
.start
;
220 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
223 // also uncheck all the other items in this radio group
224 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
225 for ( int n
= start
; n
<= end
&& node
; n
++ )
229 ((wxMenuItem
*)node
->GetData())->UncheckRadio();
231 node
= node
->GetNext();
237 wxMenuItemBase::Check( bDoCheck
) ;
243 void wxMenuItem::SetText(const wxString
& text
)
245 // don't do anything if label didn't change
246 if ( m_text
== text
)
249 wxMenuItemBase::SetText(text
);
257 void wxMenuItem::SetAsRadioGroupStart()
259 m_isRadioGroupStart
= TRUE
;
262 void wxMenuItem::SetRadioGroupStart(int start
)
264 wxASSERT_MSG( !m_isRadioGroupStart
,
265 _T("should only be called for the next radio items") );
267 m_radioGroup
.start
= start
;
270 void wxMenuItem::SetRadioGroupEnd(int end
)
272 wxASSERT_MSG( m_isRadioGroupStart
,
273 _T("should only be called for the first radio item") );
275 m_radioGroup
.end
= end
;
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
283 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
285 return wxStripMenuCodes(text
);
288 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
290 const wxString
& name
,
291 const wxString
& help
,
295 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);