1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
8 // Copyright: (c) AUTHOR
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 // ----------------------------------------------------------------------------
38 // Helper Functions to get Mac Menus the way they should be ;-)
41 void wxMacCtoPString(const char* theCString
, Str255 thePString
);
43 // remove inappropriate characters, if useShortcuts is false, the ampersand will not auto-generate a mac menu-shortcut
45 int wxMenuItem::MacBuildMenuString(StringPtr outMacItemText
, SInt16
*outMacShortcutChar
, UInt8
*outMacModifiers
, const char *inItemText
, bool useShortcuts
)
47 char *p
= (char *) &outMacItemText
[1] ;
48 short macModifiers
= 0 ;
49 char macShortCut
= 0 ;
50 const char *inItemName
;
51 wxString inItemTextMac
;
53 if (wxApp::s_macDefaultEncodingIsPC
)
55 inItemTextMac
= wxMacMakeMacStringFromPC( inItemText
) ;
56 inItemName
= inItemTextMac
;
60 inItemName
= inItemText
;
63 if ( useShortcuts
&& !wxApp::s_macSupportPCMenuShortcuts
)
64 useShortcuts
= false ;
66 // we have problems with a leading hypen - it will be taken as a separator
68 while ( *inItemName
== '-' )
73 switch ( *inItemName
)
75 // special characters for macintosh menus -> use some replacement
108 macShortCut
= *inItemName
;
114 // win-like accelerators
120 if (strncmp("Ctrl", inItemName
, 4) == 0)
122 inItemName
= inItemName
+ 5;
123 macShortCut
= *inItemName
;
125 else if (strncmp("Cntrl", inItemName
, 5) == 0)
127 inItemName
= inItemName
+ 6;
128 macShortCut
= *inItemName
;
130 else if (strncmp("Alt", inItemName
, 3) == 0)
132 inItemName
= inItemName
+ 4;
133 macModifiers
|= kMenuOptionModifier
;
134 macShortCut
= *inItemName
;
136 else if (strncmp("Shift", inItemName
, 5) == 0)
138 inItemName
= inItemName
+ 6;
139 macModifiers
|= kMenuShiftModifier
;
140 macShortCut
= *inItemName
;
142 else if (strncmp("F", inItemName
, 1) == 0)
144 inItemName
+= strlen( inItemName
) ;
145 // no function keys at the moment
146 // macModifiers |= kMenuShiftModifier ;
147 // macShortCut = *inItemName ;
155 if ( *inItemName
== 0 )
166 outMacItemText
[0] = (p
- (char *)outMacItemText
) - 1;
167 if ( outMacShortcutChar
)
168 *outMacShortcutChar
= macShortCut
;
169 if ( outMacModifiers
)
170 *outMacModifiers
= macModifiers
;
178 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
180 const wxString
& text
,
181 const wxString
& strHelp
,
184 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
186 // VZ: what about translations?? (FIXME)
187 if ( m_text
== "E&xit" ||m_text
== "Exit" ||m_text
.Left(5) == "Exit\t" || m_text
.Left(6) == "E&xit\t" )
189 m_text
= "Quit\tCtrl+Q" ;
193 wxMenuItem::~wxMenuItem()
197 bool wxMenuItem::IsChecked() const
199 return wxMenuItemBase::IsChecked() ;
202 wxString
wxMenuItem::GetLabel() const
204 return wxStripMenuCodes(m_text
);
212 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
214 return wxGetAccelFromString(GetText());
217 #endif // wxUSE_ACCEL
224 // delete the sub menu
225 void wxMenuItem::DeleteSubMenu()
227 wxASSERT( m_subMenu != NULL );
238 void wxMenuItem::Enable(bool bDoEnable
)
240 if ( m_isEnabled
!= bDoEnable
) {
241 if ( m_subMenu
== NULL
)
244 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
246 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
250 UMAEnableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
252 UMADisableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
259 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
261 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
265 UMAEnableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
267 UMADisableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
272 m_isEnabled
= bDoEnable
;
276 void wxMenuItem::Check(bool bDoCheck
)
278 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
280 if ( m_isChecked
!= bDoCheck
)
282 m_isChecked
= bDoCheck
;
283 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
285 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
289 ::SetItemMark( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
, 0x12 ) ; // checkmark
291 ::SetItemMark( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
, 0 ) ; // no mark
297 void wxMenuItem::SetText(const wxString
& text
)
299 // don't do anything if label didn't change
300 if ( m_text
== text
)
303 wxMenuItemBase::SetText(text
);
304 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
306 wxCHECK_RET( m_parentMenu
&& m_parentMenu
->GetHMenu(), wxT("menuitem without menu") );
307 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
309 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
313 MacBuildMenuString( label
, NULL
, NULL
, text
,false);
314 ::SetMenuItemText( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
, label
) ; // checkmark
319 m_parentMenu
->UpdateAccel(this);
320 #endif // wxUSE_ACCEL
323 void wxMenuItem::SetCheckable(bool checkable
)
325 wxMenuItemBase::SetCheckable(checkable
);
326 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
334 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
336 return wxStripMenuCodes(text
);
339 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
341 const wxString
& name
,
342 const wxString
& help
,
346 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);