1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/menuitem.h"
30 #include "wx/stockitem.h"
34 #include "wx/bitmap.h"
35 #include "wx/settings.h"
36 #include "wx/window.h"
38 #include "wx/string.h"
47 #include "wx/msw/private.h"
50 // Implemented in menu.cpp
51 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
) ;
54 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
59 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
61 // conditional compilation
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
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // dynamic classes implementation
74 // ----------------------------------------------------------------------------
76 #if wxUSE_EXTENDED_RTTI
78 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxWriter
* , wxPersister
* , wxxVariantArray
& )
80 const wxMenuItem
* mitem
= dynamic_cast<const wxMenuItem
*>(object
) ;
81 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
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
)
90 wxBEGIN_ENUM( wxItemKind
)
91 wxENUM_MEMBER( wxITEM_SEPARATOR
)
92 wxENUM_MEMBER( wxITEM_NORMAL
)
93 wxENUM_MEMBER( wxITEM_CHECK
)
94 wxENUM_MEMBER( wxITEM_RADIO
)
95 wxEND_ENUM( wxItemKind
)
97 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
,"wx/menuitem.h",wxMenuItemStreamingCallback
)
99 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
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") )
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") )
104 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
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"))
109 wxEND_PROPERTIES_TABLE()
111 wxBEGIN_HANDLERS_TABLE(wxMenuItem
)
112 wxEND_HANDLERS_TABLE()
114 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
* , Parent
, int , Id
, wxString
, Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
* , SubMenu
)
116 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
126 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
128 const wxString
& text
,
129 const wxString
& strHelp
,
132 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
133 #if wxUSE_OWNER_DRAWN
134 , wxOwnerDrawn(text
, kind
== wxITEM_CHECK
, true)
135 #endif // owner drawn
140 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
142 const wxString
& text
,
143 const wxString
& help
,
146 : wxMenuItemBase(parentMenu
, id
, text
, help
,
147 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
148 #if wxUSE_OWNER_DRAWN
149 , wxOwnerDrawn(text
, isCheckable
, true)
150 #endif // owner drawn
155 void wxMenuItem::Init()
157 m_radioGroup
.start
= -1;
158 m_isRadioGroupStart
= false;
160 #if wxUSE_OWNER_DRAWN
162 // when the color is not valid, wxOwnerDraw takes the default ones.
163 // If we set the colors here and they are changed by the user during
164 // the execution, then the colors are not updated until the application
165 // is restarted and our menus look bad
166 SetTextColour(wxNullColour
);
167 SetBackgroundColour(wxNullColour
);
169 // setting default colors switched ownerdraw on: switch it off again
172 // switch ownerdraw back on if using a non default margin
173 if ( !IsSeparator() )
174 SetMarginWidth(GetMarginWidth());
176 // tell the owner drawing code to show the accel string as well
177 SetAccelString(m_text
.AfterFirst(_T('\t')));
178 #endif // wxUSE_OWNER_DRAWN
181 wxMenuItem::~wxMenuItem()
188 // return the id for calling Win32 API functions
189 WXWPARAM
wxMenuItem::GetMSWId() const
191 // we must use ids in unsigned short range with Windows functions, if we
192 // pass ids > USHRT_MAX to them they get very confused (e.g. start
193 // generating WM_COMMAND messages with negative high word of wParam), so
194 // use the cast to ensure the id is in range
195 return m_subMenu
? wxPtrToUInt(m_subMenu
->GetHMenu())
196 : wx_static_cast(unsigned short, GetId());
202 bool wxMenuItem::IsChecked() const
204 // fix that RTTI is always getting the correct state (separators cannot be checked, but the call below
209 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetMSWId(), MF_BYCOMMAND
);
211 return (flag
& MF_CHECKED
) != 0;
215 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
217 return wxStripMenuCodes(text
);
223 void wxMenuItem::SetAsRadioGroupStart()
225 m_isRadioGroupStart
= true;
228 void wxMenuItem::SetRadioGroupStart(int start
)
230 wxASSERT_MSG( !m_isRadioGroupStart
,
231 _T("should only be called for the next radio items") );
233 m_radioGroup
.start
= start
;
236 void wxMenuItem::SetRadioGroupEnd(int end
)
238 wxASSERT_MSG( m_isRadioGroupStart
,
239 _T("should only be called for the first radio item") );
241 m_radioGroup
.end
= end
;
247 void wxMenuItem::Enable(bool enable
)
249 if ( m_isEnabled
== enable
)
252 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
255 (enable
? MF_ENABLED
: MF_GRAYED
));
258 wxLogLastError(wxT("EnableMenuItem"));
261 wxMenuItemBase::Enable(enable
);
264 void wxMenuItem::Check(bool check
)
266 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
268 if ( m_isChecked
== check
)
271 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
272 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
274 if ( GetKind() == wxITEM_RADIO
)
276 // it doesn't make sense to uncheck a radio item - what would this do?
280 // get the index of this item in the menu
281 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
282 int pos
= items
.IndexOf(this);
283 wxCHECK_RET( pos
!= wxNOT_FOUND
,
284 _T("menuitem not found in the menu items list?") );
286 // get the radio group range
290 if ( m_isRadioGroupStart
)
292 // we already have all information we need
294 end
= m_radioGroup
.end
;
296 else // next radio group item
298 // get the radio group end from the start item
299 start
= m_radioGroup
.start
;
300 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
304 // calling CheckMenuRadioItem() with such parameters hangs my system
305 // (NT4 SP6) and I suspect this could happen to the others as well - so
307 wxCHECK_RET( start
!= -1 && end
!= -1,
308 _T("invalid ::CheckMenuRadioItem() parameter(s)") );
310 if ( !::CheckMenuRadioItem(hmenu
,
311 start
, // the first radio group item
313 pos
, // the one to check
316 wxLogLastError(_T("CheckMenuRadioItem"));
320 // also uncheck all the other items in this radio group
321 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
322 for ( int n
= start
; n
<= end
&& node
; n
++ )
326 node
->GetData()->m_isChecked
= false;
329 node
= node
->GetNext();
334 if ( ::CheckMenuItem(hmenu
,
336 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
338 wxFAIL_MSG( _T("CheckMenuItem() failed, item not in the menu?") );
342 wxMenuItemBase::Check(check
);
345 void wxMenuItem::SetItemLabel(const wxString
& txt
)
349 // don't do anything if label didn't change
353 // wxMenuItemBase will do stock ID checks
354 wxMenuItemBase::SetItemLabel(text
);
356 // m_text could now be different from 'text' if we are a stock menu item,
357 // so use only m_text below
359 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(m_text
) );
360 #if wxUSE_OWNER_DRAWN
361 // tell the owner drawing code to to show the accel string as well
362 SetAccelString(m_text
.AfterFirst(_T('\t')));
365 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
366 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
369 m_parentMenu
->UpdateAccel(this);
370 #endif // wxUSE_ACCEL
372 UINT id
= GetMSWId();
373 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
374 if ( flagsOld
== 0xFFFFFFFF )
376 // It's not an error, it means that the menu item doesn't exist
377 //wxLogLastError(wxT("GetMenuState"));
383 // high byte contains the number of items in a submenu for submenus
385 flagsOld
|= MF_POPUP
;
390 #if wxUSE_OWNER_DRAWN
391 if ( IsOwnerDrawn() )
393 flagsOld
|= MF_OWNERDRAW
;
394 data
= (LPCTSTR
)this;
399 flagsOld
|= MF_STRING
;
400 data
= (wxChar
*) m_text
.wx_str();
404 // FIXME: complete this, applying the old
406 // However, the WinCE doc for SetMenuItemInfo
407 // says that you can't use it to set the menu
408 // item state; only data, id and type.
411 info
.cbSize
= sizeof(info
);
412 info
.fMask
= MIIM_TYPE
;
413 info
.fType
= MFT_STRING
;
414 info
.cch
= m_text
.length();
415 info
.dwTypeData
= (LPTSTR
) data
;
416 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, & info
) )
418 wxLogLastError(wxT("SetMenuItemInfo"));
421 if ( ::ModifyMenu(hMenu
, id
,
422 MF_BYCOMMAND
| flagsOld
,
423 id
, data
) == (int)0xFFFFFFFF )
425 wxLogLastError(wxT("ModifyMenu"));
431 void wxMenuItem::SetCheckable(bool checkable
)
433 wxMenuItemBase::SetCheckable(checkable
);
434 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
443 const wxString
& name
,
444 const wxString
& help
,
448 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
451 #endif // wxUSE_MENUS