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
, int id
,
179 const wxString
& text
, const wxString
& strHelp
,
183 wxASSERT( pParentMenu
!= NULL
);
185 m_parentMenu
= pParentMenu
;
186 m_subMenu
= pSubMenu
;
191 m_isCheckable
= bCheckable
;
195 if ( m_text
== "E&xit" ||m_text
== "Exit" )
197 m_text
= "Quit\tCtrl+Q" ;
201 wxMenuItem::~wxMenuItem()
205 bool wxMenuItem::IsChecked() const
207 return wxMenuItemBase::IsChecked() ;
210 wxString
wxMenuItem::GetLabel() const
212 return wxStripMenuCodes(m_text
);
220 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
222 return wxGetAccelFromString(GetText());
225 #endif // wxUSE_ACCEL
232 // delete the sub menu
233 void wxMenuItem::DeleteSubMenu()
235 wxASSERT( m_subMenu != NULL );
246 void wxMenuItem::Enable(bool bDoEnable
)
248 if ( m_isEnabled
!= bDoEnable
) {
249 if ( m_subMenu
== NULL
)
252 if ( m_parentMenu
->GetHMenu() )
254 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
258 UMAEnableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
260 UMADisableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
267 if ( m_parentMenu
->GetHMenu() )
269 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
273 UMAEnableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
275 UMADisableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
280 m_isEnabled
= bDoEnable
;
284 void wxMenuItem::Check(bool bDoCheck
)
286 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
288 if ( m_isChecked
!= bDoCheck
)
290 m_isChecked
= bDoCheck
;
291 if ( m_parentMenu
->GetHMenu() )
293 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
297 ::SetItemMark( m_parentMenu
->GetHMenu() , index
, 0x12 ) ; // checkmark
299 ::SetItemMark( m_parentMenu
->GetHMenu() , index
, 0 ) ; // no mark
305 void wxMenuItem::SetText(const wxString
& text
)
307 // don't do anything if label didn't change
308 if ( m_text
== text
)
311 wxMenuItemBase::SetText(text
);
312 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
314 wxCHECK_RET( m_parentMenu
&& m_parentMenu
->GetHMenu(), wxT("menuitem without menu") );
315 if ( m_parentMenu
->GetHMenu() )
317 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
321 MacBuildMenuString( label
, NULL
, NULL
, text
,false);
322 UMASetMenuItemText( m_parentMenu
->GetHMenu() , index
, label
) ; // checkmark
327 m_parentMenu
->UpdateAccel(this);
328 #endif // wxUSE_ACCEL
331 void wxMenuItem::SetCheckable(bool checkable
)
333 wxMenuItemBase::SetCheckable(checkable
);
334 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
342 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
344 return wxStripMenuCodes(text
);
347 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
349 const wxString
& name
,
350 const wxString
& help
,
354 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);