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