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