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