]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: menuitem.cpp | |
3 | // Purpose: wxMenuItem implementation | |
4 | // Author: Vadim Zeitlin | |
c2dcfdef | 5 | // Modified by: |
2bda0e17 KB |
6 | // Created: 11.11.97 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
6c9a19aa | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
c2dcfdef VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c2dcfdef | 21 | #pragma implementation "menuitem.h" |
a3b46648 | 22 | #endif |
2bda0e17 KB |
23 | |
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
c2dcfdef | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_MENUS |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
c2dcfdef VZ |
34 | #include "wx/font.h" |
35 | #include "wx/bitmap.h" | |
36 | #include "wx/settings.h" | |
37 | #include "wx/font.h" | |
4e938f5b | 38 | #include "wx/window.h" |
0c589ad0 BM |
39 | #include "wx/accel.h" |
40 | #include "wx/menu.h" | |
41 | #include "wx/string.h" | |
2bda0e17 KB |
42 | #endif |
43 | ||
2bda0e17 | 44 | #include "wx/menuitem.h" |
6776a0b2 | 45 | #include "wx/log.h" |
2bda0e17 | 46 | |
717a57c2 VZ |
47 | #if wxUSE_ACCEL |
48 | #include "wx/accel.h" | |
49 | #endif // wxUSE_ACCEL | |
50 | ||
42e69d6b | 51 | #include "wx/msw/private.h" |
ce3ed50d | 52 | |
4676948b JS |
53 | #ifdef __WXWINCE__ |
54 | // Implemented in menu.cpp | |
55 | UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ; | |
56 | #endif | |
57 | ||
c2dcfdef | 58 | // --------------------------------------------------------------------------- |
974e8d94 | 59 | // macro |
c2dcfdef VZ |
60 | // --------------------------------------------------------------------------- |
61 | ||
974e8d94 | 62 | // hide the ugly cast |
c2dcfdef VZ |
63 | #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu()) |
64 | ||
974e8d94 VZ |
65 | // conditional compilation |
66 | #if wxUSE_OWNER_DRAWN | |
67 | #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code | |
68 | #else // !wxUSE_OWNER_DRAWN | |
69 | #define OWNER_DRAWN_ONLY( code ) | |
70 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
71 | ||
2bda0e17 KB |
72 | // ============================================================================ |
73 | // implementation | |
74 | // ============================================================================ | |
75 | ||
76 | // ---------------------------------------------------------------------------- | |
77 | // dynamic classes implementation | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
e70b4f10 SC |
80 | #if wxUSE_EXTENDED_RTTI |
81 | ||
82 | bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & ) | |
83 | { | |
84 | const wxMenuItem * mitem = dynamic_cast<const wxMenuItem*>(object) ; | |
85 | if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().IsEmpty() ) | |
86 | { | |
87 | // we don't stream out the first two items for menus with a title, they will be reconstructed | |
88 | if ( mitem->GetMenu()->FindItemByPosition(0) == mitem || mitem->GetMenu()->FindItemByPosition(1) == mitem ) | |
89 | return false ; | |
90 | } | |
91 | return true ; | |
92 | } | |
93 | ||
3ff066a4 SC |
94 | wxBEGIN_ENUM( wxItemKind ) |
95 | wxENUM_MEMBER( wxITEM_SEPARATOR ) | |
96 | wxENUM_MEMBER( wxITEM_NORMAL ) | |
97 | wxENUM_MEMBER( wxITEM_CHECK ) | |
98 | wxENUM_MEMBER( wxITEM_RADIO ) | |
99 | wxEND_ENUM( wxItemKind ) | |
e70b4f10 SC |
100 | |
101 | IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem, wxObject,"wx/menuitem.h",wxMenuItemStreamingCallback) | |
102 | ||
3ff066a4 SC |
103 | wxBEGIN_PROPERTIES_TABLE(wxMenuItem) |
104 | wxPROPERTY( Parent,wxMenu*, SetMenu, GetMenu, , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
105 | wxPROPERTY( Id,int, SetId, GetId, , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
106 | wxPROPERTY( Text, wxString , SetText, GetText, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
107 | wxPROPERTY( Help, wxString , SetHelp, GetHelp, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
108 | wxREADONLY_PROPERTY( Kind, wxItemKind , GetKind , , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
109 | wxPROPERTY( SubMenu,wxMenu*, SetSubMenu, GetSubMenu, , 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
110 | wxPROPERTY( Enabled , bool , Enable , IsEnabled , wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
111 | wxPROPERTY( Checked , bool , Check , IsChecked , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
112 | wxPROPERTY( Checkable , bool , SetCheckable , IsCheckable , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
113 | wxEND_PROPERTIES_TABLE() | |
114 | ||
115 | wxBEGIN_HANDLERS_TABLE(wxMenuItem) | |
116 | wxEND_HANDLERS_TABLE() | |
117 | ||
118 | wxDIRECT_CONSTRUCTOR_6( wxMenuItem , wxMenu* , Parent , int , Id , wxString , Text , wxString , Help , wxItemKind , Kind , wxMenu* , SubMenu ) | |
e70b4f10 | 119 | #else |
621b3e21 | 120 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) |
e70b4f10 | 121 | #endif |
2bda0e17 KB |
122 | |
123 | // ---------------------------------------------------------------------------- | |
124 | // wxMenuItem | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | // ctor & dtor | |
128 | // ----------- | |
129 | ||
974e8d94 VZ |
130 | wxMenuItem::wxMenuItem(wxMenu *pParentMenu, |
131 | int id, | |
132 | const wxString& text, | |
133 | const wxString& strHelp, | |
d65c269b | 134 | wxItemKind kind, |
90002c49 | 135 | wxMenu *pSubMenu) |
d65c269b | 136 | : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu) |
47d67540 | 137 | #if wxUSE_OWNER_DRAWN |
51f8b246 | 138 | , wxOwnerDrawn(text, kind == wxITEM_CHECK, true) |
974e8d94 | 139 | #endif // owner drawn |
2bda0e17 | 140 | { |
2368dcda VZ |
141 | Init(); |
142 | } | |
2bda0e17 | 143 | |
2368dcda VZ |
144 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
145 | int id, | |
146 | const wxString& text, | |
147 | const wxString& help, | |
148 | bool isCheckable, | |
149 | wxMenu *subMenu) | |
150 | : wxMenuItemBase(parentMenu, id, text, help, | |
151 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) | |
152 | #if wxUSE_OWNER_DRAWN | |
19b42379 | 153 | , wxOwnerDrawn(text, isCheckable, true) |
2368dcda VZ |
154 | #endif // owner drawn |
155 | { | |
156 | Init(); | |
157 | } | |
158 | ||
159 | void wxMenuItem::Init() | |
160 | { | |
be15b995 VZ |
161 | m_radioGroup.start = -1; |
162 | m_isRadioGroupStart = FALSE; | |
0472ece7 | 163 | |
47d67540 | 164 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 165 | // set default menu colors |
a756f210 | 166 | #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c)) |
c2dcfdef | 167 | |
2bda0e17 KB |
168 | SetTextColour(SYS_COLOR(MENUTEXT)); |
169 | SetBackgroundColour(SYS_COLOR(MENU)); | |
170 | ||
6d5b2a57 VZ |
171 | #undef SYS_COLOR |
172 | ||
2bda0e17 KB |
173 | // we don't want normal items be owner-drawn |
174 | ResetOwnerDrawn(); | |
175 | ||
6d5b2a57 | 176 | // tell the owner drawing code to to show the accel string as well |
2368dcda | 177 | SetAccelString(m_text.AfterFirst(_T('\t'))); |
974e8d94 | 178 | #endif // wxUSE_OWNER_DRAWN |
2bda0e17 KB |
179 | } |
180 | ||
c2dcfdef | 181 | wxMenuItem::~wxMenuItem() |
2bda0e17 KB |
182 | { |
183 | } | |
184 | ||
185 | // misc | |
186 | // ---- | |
187 | ||
c2dcfdef VZ |
188 | // return the id for calling Win32 API functions |
189 | int wxMenuItem::GetRealId() const | |
190 | { | |
974e8d94 | 191 | return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId(); |
c2dcfdef VZ |
192 | } |
193 | ||
3dfac970 VZ |
194 | // get item state |
195 | // -------------- | |
196 | ||
a8cfd0cb | 197 | bool wxMenuItem::IsChecked() const |
3dfac970 | 198 | { |
e70b4f10 SC |
199 | // fix that RTTI is always getting the correct state (separators cannot be checked, but the call below |
200 | // returns true | |
201 | if ( GetId() == wxID_SEPARATOR ) | |
202 | return false ; | |
203 | ||
a8cfd0cb | 204 | int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND); |
3dfac970 | 205 | |
4aee367e | 206 | return (flag & MF_CHECKED) != 0; |
3dfac970 VZ |
207 | } |
208 | ||
3b59cdbf VZ |
209 | /* static */ |
210 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
717a57c2 | 211 | { |
4a3563c5 | 212 | return wxStripMenuCodes(text); |
717a57c2 VZ |
213 | } |
214 | ||
be15b995 VZ |
215 | // radio group stuff |
216 | // ----------------- | |
217 | ||
218 | void wxMenuItem::SetAsRadioGroupStart() | |
219 | { | |
220 | m_isRadioGroupStart = TRUE; | |
221 | } | |
222 | ||
223 | void wxMenuItem::SetRadioGroupStart(int start) | |
224 | { | |
225 | wxASSERT_MSG( !m_isRadioGroupStart, | |
226 | _T("should only be called for the next radio items") ); | |
227 | ||
228 | m_radioGroup.start = start; | |
229 | } | |
230 | ||
231 | void wxMenuItem::SetRadioGroupEnd(int end) | |
232 | { | |
233 | wxASSERT_MSG( m_isRadioGroupStart, | |
234 | _T("should only be called for the first radio item") ); | |
235 | ||
236 | m_radioGroup.end = end; | |
237 | } | |
238 | ||
2bda0e17 KB |
239 | // change item state |
240 | // ----------------- | |
241 | ||
717a57c2 | 242 | void wxMenuItem::Enable(bool enable) |
2bda0e17 | 243 | { |
717a57c2 VZ |
244 | if ( m_isEnabled == enable ) |
245 | return; | |
246 | ||
247 | long rc = EnableMenuItem(GetHMenuOf(m_parentMenu), | |
248 | GetRealId(), | |
249 | MF_BYCOMMAND | | |
250 | (enable ? MF_ENABLED : MF_GRAYED)); | |
c2dcfdef | 251 | |
717a57c2 | 252 | if ( rc == -1 ) { |
f6bcfd97 | 253 | wxLogLastError(wxT("EnableMenuItem")); |
c2dcfdef | 254 | } |
717a57c2 VZ |
255 | |
256 | wxMenuItemBase::Enable(enable); | |
2bda0e17 KB |
257 | } |
258 | ||
717a57c2 | 259 | void wxMenuItem::Check(bool check) |
2bda0e17 | 260 | { |
d65c269b | 261 | wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") ); |
c2dcfdef | 262 | |
717a57c2 VZ |
263 | if ( m_isChecked == check ) |
264 | return; | |
c2dcfdef | 265 | |
0472ece7 VZ |
266 | int flags = check ? MF_CHECKED : MF_UNCHECKED; |
267 | HMENU hmenu = GetHMenuOf(m_parentMenu); | |
268 | ||
546bfbea | 269 | if ( GetKind() == wxITEM_RADIO ) |
0472ece7 VZ |
270 | { |
271 | // it doesn't make sense to uncheck a radio item - what would this do? | |
272 | if ( !check ) | |
273 | return; | |
274 | ||
be15b995 | 275 | // get the index of this item in the menu |
0472ece7 VZ |
276 | const wxMenuItemList& items = m_parentMenu->GetMenuItems(); |
277 | int pos = items.IndexOf(this); | |
278 | wxCHECK_RET( pos != wxNOT_FOUND, | |
279 | _T("menuitem not found in the menu items list?") ); | |
280 | ||
be15b995 VZ |
281 | // get the radio group range |
282 | int start, | |
283 | end; | |
284 | ||
285 | if ( m_isRadioGroupStart ) | |
286 | { | |
287 | // we already have all information we need | |
288 | start = pos; | |
289 | end = m_radioGroup.end; | |
290 | } | |
291 | else // next radio group item | |
292 | { | |
293 | // get the radio group end from the start item | |
294 | start = m_radioGroup.start; | |
295 | end = items.Item(start)->GetData()->m_radioGroup.end; | |
296 | } | |
297 | ||
0472ece7 | 298 | #ifdef __WIN32__ |
be15b995 VZ |
299 | // calling CheckMenuRadioItem() with such parameters hangs my system |
300 | // (NT4 SP6) and I suspect this could happen to the others as well - so | |
301 | // don't do it! | |
302 | wxCHECK_RET( start != -1 && end != -1, | |
303 | _T("invalid ::CheckMenuRadioItem() parameter(s)") ); | |
304 | ||
0472ece7 | 305 | if ( !::CheckMenuRadioItem(hmenu, |
be15b995 VZ |
306 | start, // the first radio group item |
307 | end, // the last one | |
308 | pos, // the one to check | |
0f243af3 | 309 | MF_BYPOSITION) ) |
0472ece7 VZ |
310 | { |
311 | wxLogLastError(_T("CheckMenuRadioItem")); | |
312 | } | |
313 | #endif // __WIN32__ | |
314 | ||
315 | // also uncheck all the other items in this radio group | |
222ed1d6 | 316 | wxMenuItemList::compatibility_iterator node = items.Item(start); |
be15b995 | 317 | for ( int n = start; n <= end && node; n++ ) |
0472ece7 VZ |
318 | { |
319 | if ( n != pos ) | |
320 | { | |
321 | node->GetData()->m_isChecked = FALSE; | |
322 | } | |
323 | ||
324 | // we also have to do it in the menu for Win16 (under Win32 | |
325 | // CheckMenuRadioItem() does it for us) | |
326 | #ifndef __WIN32__ | |
327 | ::CheckMenuItem(hmenu, n, n == pos ? MF_CHECKED : MF_UNCHECKED); | |
328 | #endif // Win16 | |
329 | ||
330 | node = node->GetNext(); | |
331 | } | |
332 | } | |
333 | else // check item | |
334 | { | |
335 | if ( ::CheckMenuItem(hmenu, | |
336 | GetRealId(), | |
9f385aa0 | 337 | MF_BYCOMMAND | flags) == (DWORD)-1 ) |
0472ece7 VZ |
338 | { |
339 | wxLogLastError(wxT("CheckMenuItem")); | |
340 | } | |
c2dcfdef | 341 | } |
717a57c2 VZ |
342 | |
343 | wxMenuItemBase::Check(check); | |
c2dcfdef VZ |
344 | } |
345 | ||
974e8d94 | 346 | void wxMenuItem::SetText(const wxString& text) |
c2dcfdef VZ |
347 | { |
348 | // don't do anything if label didn't change | |
974e8d94 | 349 | if ( m_text == text ) |
c2dcfdef VZ |
350 | return; |
351 | ||
974e8d94 VZ |
352 | wxMenuItemBase::SetText(text); |
353 | OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) ); | |
2a2a71e3 JS |
354 | #if wxUSE_OWNER_DRAWN |
355 | // tell the owner drawing code to to show the accel string as well | |
356 | SetAccelString(text.AfterFirst(_T('\t'))); | |
357 | #endif | |
c2dcfdef | 358 | |
974e8d94 | 359 | HMENU hMenu = GetHMenuOf(m_parentMenu); |
717a57c2 VZ |
360 | wxCHECK_RET( hMenu, wxT("menuitem without menu") ); |
361 | ||
362 | #if wxUSE_ACCEL | |
363 | m_parentMenu->UpdateAccel(this); | |
364 | #endif // wxUSE_ACCEL | |
2bda0e17 | 365 | |
c2dcfdef VZ |
366 | UINT id = GetRealId(); |
367 | UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND); | |
368 | if ( flagsOld == 0xFFFFFFFF ) | |
369 | { | |
f6bcfd97 | 370 | wxLogLastError(wxT("GetMenuState")); |
c2dcfdef VZ |
371 | } |
372 | else | |
373 | { | |
374 | if ( IsSubMenu() ) | |
375 | { | |
376 | // high byte contains the number of items in a submenu for submenus | |
377 | flagsOld &= 0xFF; | |
378 | flagsOld |= MF_POPUP; | |
379 | } | |
380 | ||
837e5743 | 381 | LPCTSTR data; |
974e8d94 | 382 | |
c2dcfdef VZ |
383 | #if wxUSE_OWNER_DRAWN |
384 | if ( IsOwnerDrawn() ) | |
385 | { | |
386 | flagsOld |= MF_OWNERDRAW; | |
837e5743 | 387 | data = (LPCTSTR)this; |
c2dcfdef VZ |
388 | } |
389 | else | |
390 | #endif //owner drawn | |
391 | { | |
392 | flagsOld |= MF_STRING; | |
f5166ed4 | 393 | data = (wxChar*) text.c_str(); |
c2dcfdef VZ |
394 | } |
395 | ||
4676948b JS |
396 | #ifdef __WXWINCE__ |
397 | // FIXME: complete this, applying the old | |
09785dd3 JS |
398 | // flags. |
399 | // However, the WinCE doc for SetMenuItemInfo | |
400 | // says that you can't use it to set the menu | |
401 | // item state; only data, id and type. | |
4676948b JS |
402 | MENUITEMINFO info; |
403 | wxZeroMemory(info); | |
404 | info.cbSize = sizeof(info); | |
405 | info.fMask = MIIM_TYPE; | |
406 | info.fType = MFT_STRING; | |
407 | info.cch = text.Length(); | |
408 | info.dwTypeData = (LPTSTR) data ; | |
409 | if ( !SetMenuItemInfo(hMenu, id, FALSE, & info) ) | |
410 | { | |
411 | wxLogLastError(wxT("SetMenuItemInfo")); | |
412 | } | |
413 | #else | |
c2dcfdef VZ |
414 | if ( ::ModifyMenu(hMenu, id, |
415 | MF_BYCOMMAND | flagsOld, | |
a17e237f | 416 | id, data) == (int)0xFFFFFFFF ) |
c2dcfdef | 417 | { |
223d09f6 | 418 | wxLogLastError(wxT("ModifyMenu")); |
c2dcfdef | 419 | } |
4676948b | 420 | #endif |
c2dcfdef VZ |
421 | } |
422 | } | |
2bda0e17 | 423 | |
974e8d94 VZ |
424 | void wxMenuItem::SetCheckable(bool checkable) |
425 | { | |
426 | wxMenuItemBase::SetCheckable(checkable); | |
427 | OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) ); | |
428 | } | |
429 | ||
430 | // ---------------------------------------------------------------------------- | |
431 | // wxMenuItemBase | |
432 | // ---------------------------------------------------------------------------- | |
433 | ||
434 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
435 | int id, | |
436 | const wxString& name, | |
437 | const wxString& help, | |
d65c269b | 438 | wxItemKind kind, |
974e8d94 VZ |
439 | wxMenu *subMenu) |
440 | { | |
d65c269b | 441 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); |
974e8d94 | 442 | } |
1e6feb95 VZ |
443 | |
444 | #endif // wxUSE_MENUS |