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 SInt16 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
)
83 macShortCut
= *inItemName
;
89 // win-like accelerators
94 bool explicitCommandKey
= false ;
95 while( *inItemName
&& !skip
)
97 if (wxStrnicmp("Ctrl", inItemName
, 4) == 0)
99 inItemName
= inItemName
+ 5;
100 explicitCommandKey
= true ;
102 else if (wxStrnicmp("Cntrl", inItemName
, 5) == 0)
104 inItemName
= inItemName
+ 6;
105 explicitCommandKey
= true ;
107 else if (wxStrnicmp("Alt", inItemName
, 3) == 0)
109 inItemName
= inItemName
+ 4;
110 macModifiers
|= kMenuOptionModifier
;
112 else if (wxStrnicmp("Shift", inItemName
, 5) == 0)
114 inItemName
= inItemName
+ 6;
115 macModifiers
|= kMenuShiftModifier
;
124 if ( strlen(inItemName
) == 1 )
126 macShortCut
= *inItemName
;
128 else if ( !wxStricmp( inItemName
, "Delete" ) || !wxStricmp( inItemName
, "Del" ) )
130 macShortCut
= WXK_DELETE
;
132 else if ( !wxStricmp( inItemName
, "Back" ) || !wxStricmp( inItemName
, "Backspace" ) )
134 macShortCut
= WXK_BACK
;
136 else if ( !wxStricmp( inItemName
, "Return" ) )
138 macShortCut
= WXK_RETURN
;
140 else if ( !wxStricmp( inItemName
, "Enter" ) )
142 macShortCut
= kEnterCharCode
;
144 else if ( *inItemName
== 'F' )
146 int fkey
= atol(inItemName
+1) ;
147 if (fkey
>= 1 && fkey
< 15 )
149 macShortCut
= WXK_F1
+ fkey
- 1 ;
151 if ( !explicitCommandKey
)
152 macModifiers
|= kMenuNoCommandModifier
;
156 inItemName
+= strlen( inItemName
) ;
158 if ( *inItemName
== 0 )
169 outMacItemText
[0] = (p
- (char *)outMacItemText
) - 1;
170 if ( outMacShortcutChar
)
171 *outMacShortcutChar
= macShortCut
;
172 if ( outMacModifiers
)
173 *outMacModifiers
= macModifiers
;
181 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
183 const wxString
& text
,
184 const wxString
& strHelp
,
187 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
189 // VZ: what about translations?? (FIXME)
190 if ( m_text
== "E&xit" ||m_text
== "Exit" ||m_text
.Left(5) == "Exit\t" || m_text
.Left(6) == "E&xit\t" )
192 m_text
= "Quit\tCtrl+Q" ;
196 wxMenuItem::~wxMenuItem()
200 bool wxMenuItem::IsChecked() const
202 return wxMenuItemBase::IsChecked() ;
205 wxString
wxMenuItem::GetLabel() const
207 return wxStripMenuCodes(m_text
);
215 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
217 return wxGetAccelFromString(GetText());
220 #endif // wxUSE_ACCEL
227 // delete the sub menu
228 void wxMenuItem::DeleteSubMenu()
230 wxASSERT( m_subMenu != NULL );
241 void wxMenuItem::Enable(bool bDoEnable
)
243 if ( m_isEnabled
!= bDoEnable
) {
244 if ( m_subMenu
== NULL
)
247 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
249 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
253 UMAEnableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
255 UMADisableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
262 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
264 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
268 UMAEnableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
270 UMADisableMenuItem( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
) ;
275 m_isEnabled
= bDoEnable
;
279 void wxMenuItem::Check(bool bDoCheck
)
281 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
283 if ( m_isChecked
!= bDoCheck
)
285 m_isChecked
= bDoCheck
;
286 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
288 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
292 ::SetItemMark( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
, 0x12 ) ; // checkmark
294 ::SetItemMark( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
, 0 ) ; // no mark
300 void wxMenuItem::SetText(const wxString
& text
)
302 // don't do anything if label didn't change
303 if ( m_text
== text
)
306 wxMenuItemBase::SetText(text
);
307 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
309 wxCHECK_RET( m_parentMenu
&& m_parentMenu
->GetHMenu(), wxT("menuitem without menu") );
310 if ( MAC_WXHMENU(m_parentMenu
->GetHMenu()) )
312 int index
= m_parentMenu
->MacGetIndexFromItem( this ) ;
316 MacBuildMenuString( label
, NULL
, NULL
, text
,false);
317 ::SetMenuItemText( MAC_WXHMENU(m_parentMenu
->GetHMenu()) , index
, label
) ; // checkmark
322 m_parentMenu
->UpdateAccel(this);
323 #endif // wxUSE_ACCEL
326 void wxMenuItem::SetCheckable(bool checkable
)
328 wxMenuItemBase::SetCheckable(checkable
);
329 // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
337 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
339 return wxStripMenuCodes(text
);
342 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
344 const wxString
& name
,
345 const wxString
& help
,
349 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);