1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
17 #include "wx/menuitem.h"
19 #include <wx/mac/uma.h>
20 // ============================================================================
22 // ============================================================================
24 // ----------------------------------------------------------------------------
25 // dynamic classes implementation
26 // ----------------------------------------------------------------------------
28 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
30 #endif //USE_SHARED_LIBRARY
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
37 // Helper Functions to get Mac Menus the way they should be ;-)
40 void wxMacCtoPString(const char* theCString
, Str255 thePString
);
42 // remove inappropriate characters, if useShortcuts is false, the ampersand will not auto-generate a mac menu-shortcut
44 int wxMenuItem::MacBuildMenuString(StringPtr outMacItemText
, SInt16
*outMacShortcutChar
, UInt8
*outMacModifiers
, const char *inItemText
, bool useShortcuts
)
46 char *p
= (char *) &outMacItemText
[1] ;
47 short macModifiers
= 0 ;
48 char macShortCut
= 0 ;
49 const char *inItemName
;
50 wxString inItemTextMac
;
52 if (wxApp::s_macDefaultEncodingIsPC
)
54 inItemTextMac
= wxMacMakeMacStringFromPC( inItemText
) ;
55 inItemName
= inItemTextMac
;
59 inItemName
= inItemText
;
62 if ( useShortcuts
&& !wxApp::s_macSupportPCMenuShortcuts
)
63 useShortcuts
= false ;
65 // we have problems with a leading hypen - it will be taken as a separator
67 while ( *inItemName
== '-' )
72 switch ( *inItemName
)
74 // special characters for macintosh menus -> use some replacement
107 macShortCut
= *inItemName
;
113 // win-like accelerators
119 if (strncmp("Ctrl", inItemName
, 4) == 0)
121 inItemName
= inItemName
+ 5;
122 macShortCut
= *inItemName
;
124 else if (strncmp("Cntrl", inItemName
, 5) == 0)
126 inItemName
= inItemName
+ 6;
127 macShortCut
= *inItemName
;
129 else if (strncmp("Alt", inItemName
, 3) == 0)
131 inItemName
= inItemName
+ 4;
132 macModifiers
|= kMenuOptionModifier
;
133 macShortCut
= *inItemName
;
135 else if (strncmp("Shift", inItemName
, 5) == 0)
137 inItemName
= inItemName
+ 6;
138 macModifiers
|= kMenuShiftModifier
;
139 macShortCut
= *inItemName
;
141 else if (strncmp("F", inItemName
, 1) == 0)
143 inItemName
+= strlen( inItemName
) ;
144 // no function keys at the moment
145 // macModifiers |= kMenuShiftModifier ;
146 // macShortCut = *inItemName ;
154 if ( *inItemName
== 0 )
165 outMacItemText
[0] = (p
- (char *)outMacItemText
) - 1;
166 if ( outMacShortcutChar
)
167 *outMacShortcutChar
= macShortCut
;
168 if ( outMacModifiers
)
169 *outMacModifiers
= macModifiers
;
177 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
, int id
,
178 const wxString
& text
, const wxString
& strHelp
,
182 wxASSERT( pParentMenu
!= NULL
);
184 m_parentMenu
= pParentMenu
;
185 m_subMenu
= pSubMenu
;
190 m_isCheckable
= bCheckable
;
194 if ( m_text
== "E&xit" ||m_text
== "Exit" )
196 m_text
= "Quit\tCtrl+Q" ;
200 wxMenuItem::~wxMenuItem()
204 bool wxMenuItem::IsChecked() const
206 return wxMenuItemBase::IsChecked() ;
209 wxString
wxMenuItem::GetLabel() const
211 return wxStripMenuCodes(m_text
);
219 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
221 return wxGetAccelFromString(GetText());
224 #endif // wxUSE_ACCEL
231 // delete the sub menu
232 void wxMenuItem::DeleteSubMenu()
234 wxASSERT( m_subMenu != NULL );
245 void wxMenuItem::Enable(bool bDoEnable
)
247 if ( m_isEnabled
!= bDoEnable
) {
248 if ( m_subMenu
== NULL
)
251 if ( m_parentMenu
->GetHMenu() )
253 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
257 UMAEnableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
259 UMADisableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
266 if ( m_parentMenu
->GetHMenu() )
268 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
272 UMAEnableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
274 UMADisableMenuItem( m_parentMenu
->GetHMenu() , index
) ;
279 m_isEnabled
= bDoEnable
;
283 void wxMenuItem::Check(bool bDoCheck
)
285 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
287 if ( m_isChecked
!= bDoCheck
)
289 m_isChecked
= bDoCheck
;
290 if ( m_parentMenu
->GetHMenu() )
292 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
296 ::SetItemMark( m_parentMenu
->GetHMenu() , index
, 0x12 ) ; // checkmark
298 ::SetItemMark( m_parentMenu
->GetHMenu() , index
, 0 ) ; // no mark
304 void wxMenuItem::SetText(const wxString
& text
)
306 // don't do anything if label didn't change
307 if ( m_text
== text
)
310 wxMenuItemBase::SetText(text
);
311 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
313 wxCHECK_RET( m_parentMenu
&& m_parentMenu
->GetHMenu(), wxT("menuitem without menu") );
314 if ( m_parentMenu
->GetHMenu() )
316 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
320 MacBuildMenuString( label
, NULL
, NULL
, text
,false);
321 UMASetMenuItemText( m_parentMenu
->GetHMenu() , index
, label
) ; // checkmark
326 m_parentMenu
->UpdateAccel(this);
327 #endif // wxUSE_ACCEL
330 void wxMenuItem::SetCheckable(bool checkable
)
332 wxMenuItemBase::SetCheckable(checkable
);
333 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
341 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
343 return wxStripMenuCodes(text
);
346 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
348 const wxString
& name
,
349 const wxString
& help
,
353 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);