]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/menuitem.cpp
wxSYS_COLOUR_WINDOW is better for window backgrounds on PPC/Smartphone
[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 #if wxUSE_BMPBUTTON
90 ControlButtonContentInfo info ;
91 wxMacCreateBitmapButton( &info , m_bitmap ) ;
92 if ( info.contentType != kControlNoContent )
93 {
94 if ( info.contentType == kControlContentIconRef )
95 SetMenuItemIconHandle( mhandle , index ,
96 kMenuIconRefType , (Handle) info.u.iconRef ) ;
97 }
98 wxMacReleaseBitmapButton( &info ) ;
99 #endif
100 }
101 }
102
103 void wxMenuItem::UpdateItemStatus()
104 {
105 if ( !m_parentMenu )
106 return ;
107
108 #if TARGET_CARBON
109 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId)
110 {
111 if ( !IsEnabled() )
112 DisableMenuCommand( NULL , kHICommandPreferences ) ;
113 else
114 EnableMenuCommand( NULL , kHICommandPreferences ) ;
115 }
116 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId)
117 {
118 if ( !IsEnabled() )
119 DisableMenuCommand( NULL , kHICommandQuit ) ;
120 else
121 EnableMenuCommand( NULL , kHICommandQuit ) ;
122 }
123 #endif
124 {
125 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
126 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
127 if( mhandle == NULL || index == 0)
128 return ;
129
130 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
131 if ( IsCheckable() && IsChecked() )
132 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
133 else
134 ::SetItemMark( mhandle , index , 0 ) ; // no mark
135
136 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
137 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
138 UMASetMenuItemShortcut( mhandle , index , entry ) ;
139 delete entry ;
140 }
141 }
142
143 void wxMenuItem::UpdateItemText()
144 {
145 if ( !m_parentMenu )
146 return ;
147
148 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
149 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
150 if( mhandle == NULL || index == 0)
151 return ;
152
153 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
154 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
155 UMASetMenuItemShortcut( mhandle , index , entry ) ;
156 delete entry ;
157 }
158
159
160 void wxMenuItem::Enable(bool bDoEnable)
161 {
162 if (( m_isEnabled != bDoEnable
163 #if TARGET_CARBON
164 // avoid changing menuitem state when menu is disabled
165 // eg. BeginAppModalStateForWindow() will disable menus and ignore this change
166 // which in turn causes m_isEnabled to become out of sync with real menuitem state
167 && !(m_parentMenu && !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu->GetHMenu()), 0)) )
168 // always update builtin menuitems
169 || ( GetId() == wxApp::s_macPreferencesMenuItemId
170 || GetId() == wxApp::s_macExitMenuItemId
171 || GetId() == wxApp::s_macAboutMenuItemId
172 #endif
173 ))
174 {
175 wxMenuItemBase::Enable( bDoEnable ) ;
176 UpdateItemStatus() ;
177 }
178 }
179 void wxMenuItem::UncheckRadio()
180 {
181 if ( m_isChecked )
182 {
183 wxMenuItemBase::Check( false ) ;
184 UpdateItemStatus() ;
185 }
186 }
187
188 void wxMenuItem::Check(bool bDoCheck)
189 {
190 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
191
192 if ( m_isChecked != bDoCheck )
193 {
194 if ( GetKind() == wxITEM_RADIO )
195 {
196 if ( bDoCheck )
197 {
198 wxMenuItemBase::Check( bDoCheck ) ;
199 UpdateItemStatus() ;
200
201 // get the index of this item in the menu
202 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
203 int pos = items.IndexOf(this);
204 wxCHECK_RET( pos != wxNOT_FOUND,
205 _T("menuitem not found in the menu items list?") );
206
207 // get the radio group range
208 int start,
209 end;
210
211 if ( m_isRadioGroupStart )
212 {
213 // we already have all information we need
214 start = pos;
215 end = m_radioGroup.end;
216 }
217 else // next radio group item
218 {
219 // get the radio group end from the start item
220 start = m_radioGroup.start;
221 end = items.Item(start)->GetData()->m_radioGroup.end;
222 }
223
224 // also uncheck all the other items in this radio group
225 wxMenuItemList::compatibility_iterator node = items.Item(start);
226 for ( int n = start; n <= end && node; n++ )
227 {
228 if ( n != pos )
229 {
230 ((wxMenuItem*)node->GetData())->UncheckRadio();
231 }
232 node = node->GetNext();
233 }
234 }
235 }
236 else
237 {
238 wxMenuItemBase::Check( bDoCheck ) ;
239 UpdateItemStatus() ;
240 }
241 }
242 }
243
244 void wxMenuItem::SetText(const wxString& text)
245 {
246 // don't do anything if label didn't change
247 if ( m_text == text )
248 return;
249
250 wxMenuItemBase::SetText(text);
251
252 UpdateItemText() ;
253 }
254
255 // radio group stuff
256 // -----------------
257
258 void wxMenuItem::SetAsRadioGroupStart()
259 {
260 m_isRadioGroupStart = TRUE;
261 }
262
263 void wxMenuItem::SetRadioGroupStart(int start)
264 {
265 wxASSERT_MSG( !m_isRadioGroupStart,
266 _T("should only be called for the next radio items") );
267
268 m_radioGroup.start = start;
269 }
270
271 void wxMenuItem::SetRadioGroupEnd(int end)
272 {
273 wxASSERT_MSG( m_isRadioGroupStart,
274 _T("should only be called for the first radio item") );
275
276 m_radioGroup.end = end;
277 }
278
279 // ----------------------------------------------------------------------------
280 // wxMenuItemBase
281 // ----------------------------------------------------------------------------
282
283 /* static */
284 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
285 {
286 return wxStripMenuCodes(text);
287 }
288
289 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
290 int id,
291 const wxString& name,
292 const wxString& help,
293 wxItemKind kind,
294 wxMenu *subMenu)
295 {
296 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
297 }