]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: menuitem.cpp | |
3 | // Purpose: wxMenuItem implementation | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 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, | |
e7549107 | 46 | wxMenu *pSubMenu) |
d65c269b | 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") ) ; |
69991fff | 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; | |
59 | m_isRadioGroupStart = FALSE; | |
e9576ca5 SC |
60 | } |
61 | ||
62 | wxMenuItem::~wxMenuItem() | |
63 | { | |
64 | } | |
65 | ||
bf918b97 SC |
66 | // change item state |
67 | // ----------------- | |
51abe921 | 68 | |
bf918b97 SC |
69 | void wxMenuItem::SetBitmap(const wxBitmap& bitmap) |
70 | { | |
e40298d5 JS |
71 | m_bitmap = bitmap; |
72 | UpdateItemBitmap() ; | |
51abe921 SC |
73 | } |
74 | ||
bf918b97 | 75 | void wxMenuItem::UpdateItemBitmap() |
51abe921 | 76 | { |
e40298d5 JS |
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 | { | |
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 ) |
e40298d5 | 93 | SetMenuItemIconHandle( mhandle , index , |
b2f3b00b | 94 | kMenuIconRefType , (Handle) info.u.iconRef ) ; |
e40298d5 | 95 | } |
20b69855 | 96 | wxMacReleaseBitmapButton( &info ) ; |
23af264e | 97 | #endif |
e40298d5 | 98 | } |
51abe921 SC |
99 | } |
100 | ||
bf918b97 | 101 | void wxMenuItem::UpdateItemStatus() |
e9576ca5 | 102 | { |
e40298d5 JS |
103 | if ( !m_parentMenu ) |
104 | return ; | |
105 | ||
756c2704 SC |
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 | ||
a9412f8f | 134 | UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ; |
756c2704 SC |
135 | wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ; |
136 | UMASetMenuItemShortcut( mhandle , index , entry ) ; | |
137 | delete entry ; | |
138 | } | |
e9576ca5 SC |
139 | } |
140 | ||
bf918b97 SC |
141 | void wxMenuItem::UpdateItemText() |
142 | { | |
e40298d5 JS |
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 | ||
a9412f8f | 151 | UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ; |
e40298d5 JS |
152 | wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ; |
153 | UMASetMenuItemShortcut( mhandle , index , entry ) ; | |
154 | delete entry ; | |
bf918b97 | 155 | } |
e7549107 | 156 | |
e9576ca5 SC |
157 | |
158 | void wxMenuItem::Enable(bool bDoEnable) | |
159 | { | |
a2d3a3d1 | 160 | if (( m_isEnabled != bDoEnable |
bdacb147 | 161 | #if TARGET_CARBON |
a2d3a3d1 JS |
162 | // avoid changing menuitem state when menu is disabled |
163 | // eg. BeginAppModalStateForWindow() will disable menus and ignore this change | |
164 | // which in turn causes m_isEnabled to become out of sync with real menuitem state | |
165 | && !(m_parentMenu && !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu->GetHMenu()), 0)) ) | |
166 | // always update builtin menuitems | |
167 | || ( GetId() == wxApp::s_macPreferencesMenuItemId | |
168 | || GetId() == wxApp::s_macExitMenuItemId | |
169 | || GetId() == wxApp::s_macAboutMenuItemId | |
bdacb147 | 170 | #endif |
a2d3a3d1 | 171 | )) |
e40298d5 JS |
172 | { |
173 | wxMenuItemBase::Enable( bDoEnable ) ; | |
174 | UpdateItemStatus() ; | |
175 | } | |
bf918b97 SC |
176 | } |
177 | void wxMenuItem::UncheckRadio() | |
178 | { | |
e40298d5 JS |
179 | if ( m_isChecked ) |
180 | { | |
181 | wxMenuItemBase::Check( false ) ; | |
182 | UpdateItemStatus() ; | |
183 | } | |
e9576ca5 SC |
184 | } |
185 | ||
186 | void wxMenuItem::Check(bool bDoCheck) | |
187 | { | |
427ff662 | 188 | wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") ); |
e40298d5 JS |
189 | |
190 | if ( m_isChecked != bDoCheck ) | |
191 | { | |
192 | if ( GetKind() == wxITEM_RADIO ) | |
193 | { | |
194 | if ( bDoCheck ) | |
195 | { | |
196 | wxMenuItemBase::Check( bDoCheck ) ; | |
197 | UpdateItemStatus() ; | |
198 | ||
199 | // get the index of this item in the menu | |
200 | const wxMenuItemList& items = m_parentMenu->GetMenuItems(); | |
201 | int pos = items.IndexOf(this); | |
202 | wxCHECK_RET( pos != wxNOT_FOUND, | |
203 | _T("menuitem not found in the menu items list?") ); | |
204 | ||
205 | // get the radio group range | |
206 | int start, | |
207 | end; | |
208 | ||
209 | if ( m_isRadioGroupStart ) | |
210 | { | |
211 | // we already have all information we need | |
212 | start = pos; | |
213 | end = m_radioGroup.end; | |
214 | } | |
215 | else // next radio group item | |
216 | { | |
217 | // get the radio group end from the start item | |
218 | start = m_radioGroup.start; | |
219 | end = items.Item(start)->GetData()->m_radioGroup.end; | |
220 | } | |
221 | ||
222 | // also uncheck all the other items in this radio group | |
affd2611 | 223 | wxMenuItemList::compatibility_iterator node = items.Item(start); |
e40298d5 JS |
224 | for ( int n = start; n <= end && node; n++ ) |
225 | { | |
226 | if ( n != pos ) | |
227 | { | |
228 | ((wxMenuItem*)node->GetData())->UncheckRadio(); | |
229 | } | |
230 | node = node->GetNext(); | |
231 | } | |
232 | } | |
233 | } | |
234 | else | |
235 | { | |
236 | wxMenuItemBase::Check( bDoCheck ) ; | |
237 | UpdateItemStatus() ; | |
238 | } | |
239 | } | |
51abe921 SC |
240 | } |
241 | ||
242 | void wxMenuItem::SetText(const wxString& text) | |
243 | { | |
244 | // don't do anything if label didn't change | |
245 | if ( m_text == text ) | |
246 | return; | |
247 | ||
248 | wxMenuItemBase::SetText(text); | |
bf918b97 SC |
249 | |
250 | UpdateItemText() ; | |
251 | } | |
51abe921 | 252 | |
bf918b97 SC |
253 | // radio group stuff |
254 | // ----------------- | |
51abe921 | 255 | |
bf918b97 SC |
256 | void wxMenuItem::SetAsRadioGroupStart() |
257 | { | |
258 | m_isRadioGroupStart = TRUE; | |
51abe921 | 259 | } |
bf918b97 SC |
260 | |
261 | void wxMenuItem::SetRadioGroupStart(int start) | |
51abe921 | 262 | { |
bf918b97 SC |
263 | wxASSERT_MSG( !m_isRadioGroupStart, |
264 | _T("should only be called for the next radio items") ); | |
265 | ||
266 | m_radioGroup.start = start; | |
267 | } | |
268 | ||
269 | void wxMenuItem::SetRadioGroupEnd(int end) | |
270 | { | |
271 | wxASSERT_MSG( m_isRadioGroupStart, | |
272 | _T("should only be called for the first radio item") ); | |
273 | ||
274 | m_radioGroup.end = end; | |
51abe921 SC |
275 | } |
276 | ||
277 | // ---------------------------------------------------------------------------- | |
278 | // wxMenuItemBase | |
279 | // ---------------------------------------------------------------------------- | |
280 | ||
2f1ae414 SC |
281 | /* static */ |
282 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
283 | { | |
284 | return wxStripMenuCodes(text); | |
285 | } | |
286 | ||
51abe921 SC |
287 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, |
288 | int id, | |
289 | const wxString& name, | |
290 | const wxString& help, | |
d65c269b | 291 | wxItemKind kind, |
51abe921 SC |
292 | wxMenu *subMenu) |
293 | { | |
d65c269b | 294 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); |
51abe921 | 295 | } |