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