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