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