1 ///////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "menuitem.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/bitmap.h"
36 #include "wx/settings.h"
38 #include "wx/window.h"
41 #include "wx/string.h"
44 #include "wx/menuitem.h"
51 #include "wx/msw/private.h"
54 // Implemented in menu.cpp
55 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
) ;
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
63 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
65 // conditional compilation
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
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // dynamic classes implementation
78 // ----------------------------------------------------------------------------
80 #if wxUSE_EXTENDED_RTTI
82 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxWriter
* , wxPersister
* , wxxVariantArray
& )
84 const wxMenuItem
* mitem
= dynamic_cast<const wxMenuItem
*>(object
) ;
85 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().IsEmpty() )
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
)
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
)
101 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
,"wx/menuitem.h",wxMenuItemStreamingCallback
)
103 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
104 wxPROPERTY( Parent
,wxMenu
*, SetMenu
, GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
105 wxPROPERTY( Id
,int, SetId
, GetId
, EMPTY_MACROVALUE
, 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
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
109 wxPROPERTY( SubMenu
,wxMenu
*, SetSubMenu
, GetSubMenu
, EMPTY_MACROVALUE
, 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()
115 wxBEGIN_HANDLERS_TABLE(wxMenuItem
)
116 wxEND_HANDLERS_TABLE()
118 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
* , Parent
, int , Id
, wxString
, Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
* , SubMenu
)
120 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
130 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
132 const wxString
& text
,
133 const wxString
& strHelp
,
136 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
137 #if wxUSE_OWNER_DRAWN
138 , wxOwnerDrawn(text
, kind
== wxITEM_CHECK
, true)
139 #endif // owner drawn
144 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
146 const wxString
& text
,
147 const wxString
& help
,
150 : wxMenuItemBase(parentMenu
, id
, text
, help
,
151 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
152 #if wxUSE_OWNER_DRAWN
153 , wxOwnerDrawn(text
, isCheckable
, true)
154 #endif // owner drawn
159 void wxMenuItem::Init()
161 m_radioGroup
.start
= -1;
162 m_isRadioGroupStart
= false;
164 #if wxUSE_OWNER_DRAWN
166 // when the color is not valid, wxOwnerDraw takes the default ones.
167 // If we set the colors here and they are changed by the user during
168 // the execution, then the colors are not updated until the application
169 // is restarted and our menus look bad
170 SetTextColour(wxNullColour
);
171 SetBackgroundColour(wxNullColour
);
173 // setting default colors switched ownerdraw on: switch it off again
176 // switch ownerdraw back on if using a non default margin
177 if ( GetId() != wxID_SEPARATOR
)
178 SetMarginWidth(GetMarginWidth());
180 // tell the owner drawing code to to show the accel string as well
181 SetAccelString(m_text
.AfterFirst(_T('\t')));
182 #endif // wxUSE_OWNER_DRAWN
185 wxMenuItem::~wxMenuItem()
192 // return the id for calling Win32 API functions
193 int wxMenuItem::GetRealId() const
195 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
201 bool wxMenuItem::IsChecked() const
203 // fix that RTTI is always getting the correct state (separators cannot be checked, but the call below
205 if ( GetId() == wxID_SEPARATOR
)
208 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
210 return (flag
& MF_CHECKED
) != 0;
214 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
216 return wxStripMenuCodes(text
);
222 void wxMenuItem::SetAsRadioGroupStart()
224 m_isRadioGroupStart
= true;
227 void wxMenuItem::SetRadioGroupStart(int start
)
229 wxASSERT_MSG( !m_isRadioGroupStart
,
230 _T("should only be called for the next radio items") );
232 m_radioGroup
.start
= start
;
235 void wxMenuItem::SetRadioGroupEnd(int end
)
237 wxASSERT_MSG( m_isRadioGroupStart
,
238 _T("should only be called for the first radio item") );
240 m_radioGroup
.end
= end
;
246 void wxMenuItem::Enable(bool enable
)
248 if ( m_isEnabled
== enable
)
251 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
254 (enable
? MF_ENABLED
: MF_GRAYED
));
257 wxLogLastError(wxT("EnableMenuItem"));
260 wxMenuItemBase::Enable(enable
);
263 void wxMenuItem::Check(bool check
)
265 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
267 if ( m_isChecked
== check
)
270 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
271 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
273 if ( GetKind() == wxITEM_RADIO
)
275 // it doesn't make sense to uncheck a radio item - what would this do?
279 // get the index of this item in the menu
280 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
281 int pos
= items
.IndexOf(this);
282 wxCHECK_RET( pos
!= wxNOT_FOUND
,
283 _T("menuitem not found in the menu items list?") );
285 // get the radio group range
289 if ( m_isRadioGroupStart
)
291 // we already have all information we need
293 end
= m_radioGroup
.end
;
295 else // next radio group item
297 // get the radio group end from the start item
298 start
= m_radioGroup
.start
;
299 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
303 // calling CheckMenuRadioItem() with such parameters hangs my system
304 // (NT4 SP6) and I suspect this could happen to the others as well - so
306 wxCHECK_RET( start
!= -1 && end
!= -1,
307 _T("invalid ::CheckMenuRadioItem() parameter(s)") );
309 if ( !::CheckMenuRadioItem(hmenu
,
310 start
, // the first radio group item
312 pos
, // the one to check
315 wxLogLastError(_T("CheckMenuRadioItem"));
319 // also uncheck all the other items in this radio group
320 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
321 for ( int n
= start
; n
<= end
&& node
; n
++ )
325 node
->GetData()->m_isChecked
= false;
328 node
= node
->GetNext();
333 if ( ::CheckMenuItem(hmenu
,
335 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
337 wxFAIL_MSG( _T("CheckMenuItem() failed, item not in the menu?") );
341 wxMenuItemBase::Check(check
);
344 void wxMenuItem::SetText(const wxString
& text
)
346 // don't do anything if label didn't change
347 if ( m_text
== text
)
350 wxMenuItemBase::SetText(text
);
351 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
352 #if wxUSE_OWNER_DRAWN
353 // tell the owner drawing code to to show the accel string as well
354 SetAccelString(text
.AfterFirst(_T('\t')));
357 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
358 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
361 m_parentMenu
->UpdateAccel(this);
362 #endif // wxUSE_ACCEL
364 UINT id
= GetRealId();
365 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
366 if ( flagsOld
== 0xFFFFFFFF )
368 // It's not an error, it means that the menu item doesn't exist
369 //wxLogLastError(wxT("GetMenuState"));
375 // high byte contains the number of items in a submenu for submenus
377 flagsOld
|= MF_POPUP
;
382 #if wxUSE_OWNER_DRAWN
383 if ( IsOwnerDrawn() )
385 flagsOld
|= MF_OWNERDRAW
;
386 data
= (LPCTSTR
)this;
391 flagsOld
|= MF_STRING
;
392 data
= (wxChar
*) text
.c_str();
396 // FIXME: complete this, applying the old
398 // However, the WinCE doc for SetMenuItemInfo
399 // says that you can't use it to set the menu
400 // item state; only data, id and type.
403 info
.cbSize
= sizeof(info
);
404 info
.fMask
= MIIM_TYPE
;
405 info
.fType
= MFT_STRING
;
406 info
.cch
= text
.Length();
407 info
.dwTypeData
= (LPTSTR
) data
;
408 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, & info
) )
410 wxLogLastError(wxT("SetMenuItemInfo"));
413 if ( ::ModifyMenu(hMenu
, id
,
414 MF_BYCOMMAND
| flagsOld
,
415 id
, data
) == (int)0xFFFFFFFF )
417 wxLogLastError(wxT("ModifyMenu"));
423 void wxMenuItem::SetCheckable(bool checkable
)
425 wxMenuItemBase::SetCheckable(checkable
);
426 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
435 const wxString
& name
,
436 const wxString
& help
,
440 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
443 #endif // wxUSE_MENUS