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