]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/menuitem.cpp
0b9094a12f4c524257e157a24749c34064865f5b
[wxWidgets.git] / src / mac / carbon / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
15
16 #include "wx/wxprec.h"
17
18 #include "wx/app.h"
19 #include "wx/menu.h"
20 #include "wx/menuitem.h"
21
22 #include "wx/mac/uma.h"
23 // ============================================================================
24 // implementation
25 // ============================================================================
26
27 // ----------------------------------------------------------------------------
28 // dynamic classes implementation
29 // ----------------------------------------------------------------------------
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
33 #endif //USE_SHARED_LIBRARY
34
35 // ----------------------------------------------------------------------------
36 // wxMenuItem
37 // ----------------------------------------------------------------------------
38
39 //
40 // ctor & dtor
41 // -----------
42
43 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
44 int id,
45 const wxString& text,
46 const wxString& strHelp,
47 wxItemKind kind,
48 wxMenu *pSubMenu)
49 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
50 {
51 wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
52
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") )
56 {
57 m_text =wxT("Quit\tCtrl+Q") ;
58 }
59
60 m_radioGroup.start = -1;
61 m_isRadioGroupStart = FALSE;
62 }
63
64 wxMenuItem::~wxMenuItem()
65 {
66 }
67
68 // change item state
69 // -----------------
70
71 void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
72 {
73 m_bitmap = bitmap;
74 UpdateItemBitmap() ;
75 }
76
77 void wxMenuItem::UpdateItemBitmap()
78 {
79 if ( !m_parentMenu )
80 return ;
81
82 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
83 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
84 if( mhandle == NULL || index == 0)
85 return ;
86
87 if ( m_bitmap.Ok() )
88 {
89 ControlButtonContentInfo info ;
90 wxMacCreateBitmapButton( &info , m_bitmap ) ;
91 if ( info.contentType != kControlNoContent )
92 {
93 if ( info.contentType == kControlContentIconRef )
94 SetMenuItemIconHandle( mhandle , index ,
95 kMenuIconRefType , (Handle) info.u.iconRef ) ;
96 }
97 wxMacReleaseBitmapButton( &info ) ;
98
99 }
100 }
101
102 void wxMenuItem::UpdateItemStatus()
103 {
104 if ( !m_parentMenu )
105 return ;
106
107 #if TARGET_CARBON
108 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId)
109 {
110 if ( !IsEnabled() )
111 DisableMenuCommand( NULL , kHICommandPreferences ) ;
112 else
113 EnableMenuCommand( NULL , kHICommandPreferences ) ;
114 }
115 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId)
116 {
117 if ( !IsEnabled() )
118 DisableMenuCommand( NULL , kHICommandQuit ) ;
119 else
120 EnableMenuCommand( NULL , kHICommandQuit ) ;
121 }
122 #endif
123 {
124 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
125 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
126 if( mhandle == NULL || index == 0)
127 return ;
128
129 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
130 if ( IsCheckable() && IsChecked() )
131 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
132 else
133 ::SetItemMark( mhandle , index , 0 ) ; // no mark
134
135 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
136 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
137 UMASetMenuItemShortcut( mhandle , index , entry ) ;
138 delete entry ;
139 }
140 }
141
142 void wxMenuItem::UpdateItemText()
143 {
144 if ( !m_parentMenu )
145 return ;
146
147 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
148 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
149 if( mhandle == NULL || index == 0)
150 return ;
151
152 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
153 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
154 UMASetMenuItemShortcut( mhandle , index , entry ) ;
155 delete entry ;
156 }
157
158
159 void wxMenuItem::Enable(bool bDoEnable)
160 {
161 if (( m_isEnabled != bDoEnable
162 #if TARGET_CARBON
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
171 #endif
172 ))
173 {
174 wxMenuItemBase::Enable( bDoEnable ) ;
175 UpdateItemStatus() ;
176 }
177 }
178 void wxMenuItem::UncheckRadio()
179 {
180 if ( m_isChecked )
181 {
182 wxMenuItemBase::Check( false ) ;
183 UpdateItemStatus() ;
184 }
185 }
186
187 void wxMenuItem::Check(bool bDoCheck)
188 {
189 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
190
191 if ( m_isChecked != bDoCheck )
192 {
193 if ( GetKind() == wxITEM_RADIO )
194 {
195 if ( bDoCheck )
196 {
197 wxMenuItemBase::Check( bDoCheck ) ;
198 UpdateItemStatus() ;
199
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?") );
205
206 // get the radio group range
207 int start,
208 end;
209
210 if ( m_isRadioGroupStart )
211 {
212 // we already have all information we need
213 start = pos;
214 end = m_radioGroup.end;
215 }
216 else // next radio group item
217 {
218 // get the radio group end from the start item
219 start = m_radioGroup.start;
220 end = items.Item(start)->GetData()->m_radioGroup.end;
221 }
222
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++ )
226 {
227 if ( n != pos )
228 {
229 ((wxMenuItem*)node->GetData())->UncheckRadio();
230 }
231 node = node->GetNext();
232 }
233 }
234 }
235 else
236 {
237 wxMenuItemBase::Check( bDoCheck ) ;
238 UpdateItemStatus() ;
239 }
240 }
241 }
242
243 void wxMenuItem::SetText(const wxString& text)
244 {
245 // don't do anything if label didn't change
246 if ( m_text == text )
247 return;
248
249 wxMenuItemBase::SetText(text);
250
251 UpdateItemText() ;
252 }
253
254 // radio group stuff
255 // -----------------
256
257 void wxMenuItem::SetAsRadioGroupStart()
258 {
259 m_isRadioGroupStart = TRUE;
260 }
261
262 void wxMenuItem::SetRadioGroupStart(int start)
263 {
264 wxASSERT_MSG( !m_isRadioGroupStart,
265 _T("should only be called for the next radio items") );
266
267 m_radioGroup.start = start;
268 }
269
270 void wxMenuItem::SetRadioGroupEnd(int end)
271 {
272 wxASSERT_MSG( m_isRadioGroupStart,
273 _T("should only be called for the first radio item") );
274
275 m_radioGroup.end = end;
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxMenuItemBase
280 // ----------------------------------------------------------------------------
281
282 /* static */
283 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
284 {
285 return wxStripMenuCodes(text);
286 }
287
288 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
289 int id,
290 const wxString& name,
291 const wxString& help,
292 wxItemKind kind,
293 wxMenu *subMenu)
294 {
295 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
296 }