]>
Commit | Line | Data |
---|---|---|
524c47aa SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/menuitem_osx.cpp | |
3 | // Purpose: wxMenuItem implementation | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
b5b208a1 | 7 | // RCS-ID: $Id$ |
524c47aa SC |
8 | // Copyright: (c) Stefan Csomor |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
11fed901 SC |
14 | #if wxUSE_MENUS |
15 | ||
524c47aa SC |
16 | #include "wx/menuitem.h" |
17 | #include "wx/stockitem.h" | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/app.h" | |
21 | #include "wx/menu.h" | |
22 | #endif // WX_PRECOMP | |
23 | ||
24 | #include "wx/osx/private.h" | |
25 | ||
26 | IMPLEMENT_ABSTRACT_CLASS( wxMenuItemImpl , wxObject ) | |
27 | ||
28 | wxMenuItemImpl::~wxMenuItemImpl() | |
29 | { | |
30 | } | |
31 | ||
524c47aa SC |
32 | wxMenuItem::wxMenuItem(wxMenu *pParentMenu, |
33 | int id, | |
34 | const wxString& t, | |
35 | const wxString& strHelp, | |
36 | wxItemKind kind, | |
37 | wxMenu *pSubMenu) | |
38 | :wxMenuItemBase(pParentMenu, id, t, strHelp, kind, pSubMenu) | |
39 | { | |
40 | wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ; | |
41 | ||
42 | // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines | |
43 | // therefore these item must not be translated | |
44 | if ( wxStripMenuCodes(m_text).Upper() == wxT("EXIT") ) | |
45 | m_text = wxT("Quit\tCtrl+Q") ; | |
46 | ||
47 | m_radioGroup.start = -1; | |
48 | m_isRadioGroupStart = false; | |
03647350 | 49 | |
524c47aa SC |
50 | wxString text = wxStripMenuCodes(m_text); |
51 | if (text.IsEmpty() && !IsSeparator()) | |
52 | { | |
53 | wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?")); | |
54 | text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR|wxSTOCK_WITH_MNEMONIC); | |
55 | } | |
03647350 | 56 | |
524c47aa | 57 | wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( m_text ) ; |
1ea5ef01 SC |
58 | // use accessors for ID and Kind because they might have been changed in the base constructor |
59 | m_peer = wxMenuItemImpl::Create( this, pParentMenu, GetId(), text, entry, strHelp, GetKind(), pSubMenu ); | |
524c47aa SC |
60 | delete entry; |
61 | } | |
62 | ||
63 | wxMenuItem::~wxMenuItem() | |
64 | { | |
65 | delete m_peer; | |
66 | } | |
67 | ||
68 | // change item state | |
69 | // ----------------- | |
70 | ||
71 | void wxMenuItem::SetBitmap(const wxBitmap& bitmap) | |
72 | { | |
73 | m_bitmap = bitmap; | |
74 | UpdateItemBitmap(); | |
75 | } | |
76 | ||
77 | void wxMenuItem::Enable(bool bDoEnable) | |
78 | { | |
79 | if (( m_isEnabled != bDoEnable | |
80 | // avoid changing menuitem state when menu is disabled | |
81 | // eg. BeginAppModalStateForWindow() will disable menus and ignore this change | |
82 | // which in turn causes m_isEnabled to become out of sync with real menuitem state | |
83 | #if wxOSX_USE_CARBON | |
03647350 | 84 | && !(m_parentMenu && !IsMenuItemEnabled(MAC_WXHMENU(m_parentMenu->GetHMenu()), 0)) |
524c47aa SC |
85 | #endif |
86 | ) | |
87 | // always update builtin menuitems | |
88 | || ( GetId() == wxApp::s_macPreferencesMenuItemId | |
89 | || GetId() == wxApp::s_macExitMenuItemId | |
90 | || GetId() == wxApp::s_macAboutMenuItemId | |
91 | )) | |
92 | { | |
93 | wxMenuItemBase::Enable( bDoEnable ) ; | |
94 | UpdateItemStatus() ; | |
95 | } | |
96 | } | |
97 | ||
98 | void wxMenuItem::UncheckRadio() | |
99 | { | |
100 | if ( m_isChecked ) | |
101 | { | |
102 | wxMenuItemBase::Check( false ) ; | |
103 | UpdateItemStatus() ; | |
104 | } | |
105 | } | |
106 | ||
107 | void wxMenuItem::Check(bool bDoCheck) | |
108 | { | |
109 | wxCHECK_RET( IsCheckable() && !IsSeparator(), wxT("only checkable items may be checked") ); | |
110 | ||
111 | if ( m_isChecked != bDoCheck ) | |
112 | { | |
113 | if ( GetKind() == wxITEM_RADIO ) | |
114 | { | |
115 | if ( bDoCheck ) | |
116 | { | |
117 | wxMenuItemBase::Check( bDoCheck ) ; | |
118 | UpdateItemStatus() ; | |
119 | ||
120 | // get the index of this item in the menu | |
121 | const wxMenuItemList& items = m_parentMenu->GetMenuItems(); | |
122 | int pos = items.IndexOf(this); | |
123 | wxCHECK_RET( pos != wxNOT_FOUND, | |
9a83f860 | 124 | wxT("menuitem not found in the menu items list?") ); |
524c47aa SC |
125 | |
126 | // get the radio group range | |
127 | int start, end; | |
128 | ||
129 | if ( m_isRadioGroupStart ) | |
130 | { | |
131 | // we already have all information we need | |
132 | start = pos; | |
133 | end = m_radioGroup.end; | |
134 | } | |
135 | else // next radio group item | |
136 | { | |
137 | // get the radio group end from the start item | |
138 | start = m_radioGroup.start; | |
139 | end = items.Item(start)->GetData()->m_radioGroup.end; | |
140 | } | |
141 | ||
142 | // also uncheck all the other items in this radio group | |
143 | wxMenuItemList::compatibility_iterator node = items.Item(start); | |
144 | for ( int n = start; n <= end && node; n++ ) | |
145 | { | |
146 | if ( n != pos ) | |
147 | ((wxMenuItem*)node->GetData())->UncheckRadio(); | |
148 | ||
149 | node = node->GetNext(); | |
150 | } | |
151 | } | |
152 | } | |
153 | else | |
154 | { | |
155 | wxMenuItemBase::Check( bDoCheck ) ; | |
156 | UpdateItemStatus() ; | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | void wxMenuItem::SetItemLabel(const wxString& text) | |
162 | { | |
163 | // don't do anything if label didn't change | |
164 | if ( m_text == text ) | |
165 | return; | |
166 | ||
167 | wxMenuItemBase::SetItemLabel(text); | |
168 | ||
169 | UpdateItemText() ; | |
170 | } | |
171 | ||
172 | ||
173 | void wxMenuItem::UpdateItemBitmap() | |
174 | { | |
175 | if ( !m_parentMenu ) | |
176 | return; | |
03647350 | 177 | |
524c47aa SC |
178 | if ( m_bitmap.Ok() ) |
179 | { | |
22756322 | 180 | GetPeer()->SetBitmap( m_bitmap ); |
524c47aa SC |
181 | } |
182 | } | |
183 | ||
184 | void wxMenuItem::UpdateItemStatus() | |
185 | { | |
186 | if ( !m_parentMenu ) | |
187 | return ; | |
188 | ||
189 | if ( IsSeparator() ) | |
190 | return ; | |
03647350 | 191 | |
524c47aa | 192 | if ( IsCheckable() && IsChecked() ) |
22756322 | 193 | GetPeer()->Check( true ); |
524c47aa | 194 | else |
22756322 | 195 | GetPeer()->Check( false ); |
03647350 | 196 | |
22756322 | 197 | GetPeer()->Enable( IsEnabled() ); |
524c47aa SC |
198 | } |
199 | ||
200 | void wxMenuItem::UpdateItemText() | |
201 | { | |
202 | if ( !m_parentMenu ) | |
203 | return ; | |
204 | ||
205 | wxString text = wxStripMenuCodes(m_text); | |
206 | if (text.IsEmpty() && !IsSeparator()) | |
207 | { | |
208 | wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?")); | |
209 | text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR|wxSTOCK_WITH_MNEMONIC); | |
210 | } | |
03647350 | 211 | |
524c47aa | 212 | wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( m_text ) ; |
22756322 | 213 | GetPeer()->SetLabel( text, entry ); |
524c47aa SC |
214 | delete entry ; |
215 | } | |
216 | ||
217 | // radio group stuff | |
218 | // ----------------- | |
219 | ||
220 | void wxMenuItem::SetAsRadioGroupStart() | |
221 | { | |
222 | m_isRadioGroupStart = true; | |
223 | } | |
224 | ||
225 | void wxMenuItem::SetRadioGroupStart(int start) | |
226 | { | |
227 | wxASSERT_MSG( !m_isRadioGroupStart, | |
228 | wxT("should only be called for the next radio items") ); | |
229 | ||
230 | m_radioGroup.start = start; | |
231 | } | |
232 | ||
233 | void wxMenuItem::SetRadioGroupEnd(int end) | |
234 | { | |
235 | wxASSERT_MSG( m_isRadioGroupStart, | |
236 | wxT("should only be called for the first radio item") ); | |
237 | ||
238 | m_radioGroup.end = end; | |
239 | } | |
240 | ||
241 | // ---------------------------------------------------------------------------- | |
242 | // wxMenuItemBase | |
243 | // ---------------------------------------------------------------------------- | |
244 | ||
245 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
246 | int id, | |
247 | const wxString& name, | |
248 | const wxString& help, | |
249 | wxItemKind kind, | |
250 | wxMenu *subMenu) | |
251 | { | |
252 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); | |
253 | } | |
11fed901 SC |
254 | |
255 | #endif |