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