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