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