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"
33 #include "wx/bitmap.h"
34 #include "wx/settings.h"
35 #include "wx/window.h"
37 #include "wx/string.h"
46 #include "wx/msw/private.h"
49 // Implemented in menu.cpp
50 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
) ;
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
58 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
60 // conditional compilation
62 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
63 #else // !wxUSE_OWNER_DRAWN
64 #define OWNER_DRAWN_ONLY( code )
65 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
72 // dynamic classes implementation
73 // ----------------------------------------------------------------------------
75 #if wxUSE_EXTENDED_RTTI
77 bool wxMenuItemStreamingCallback( const wxObject
*object
, wxWriter
* , wxPersister
* , wxxVariantArray
& )
79 const wxMenuItem
* mitem
= dynamic_cast<const wxMenuItem
*>(object
) ;
80 if ( mitem
->GetMenu() && !mitem
->GetMenu()->GetTitle().empty() )
82 // we don't stream out the first two items for menus with a title, they will be reconstructed
83 if ( mitem
->GetMenu()->FindItemByPosition(0) == mitem
|| mitem
->GetMenu()->FindItemByPosition(1) == mitem
)
89 wxBEGIN_ENUM( wxItemKind
)
90 wxENUM_MEMBER( wxITEM_SEPARATOR
)
91 wxENUM_MEMBER( wxITEM_NORMAL
)
92 wxENUM_MEMBER( wxITEM_CHECK
)
93 wxENUM_MEMBER( wxITEM_RADIO
)
94 wxEND_ENUM( wxItemKind
)
96 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem
, wxObject
,"wx/menuitem.h",wxMenuItemStreamingCallback
)
98 wxBEGIN_PROPERTIES_TABLE(wxMenuItem
)
99 wxPROPERTY( Parent
,wxMenu
*, SetMenu
, GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
100 wxPROPERTY( Id
,int, SetId
, GetId
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
101 wxPROPERTY( Text
, wxString
, SetText
, GetText
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
102 wxPROPERTY( Help
, wxString
, SetHelp
, GetHelp
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
103 wxREADONLY_PROPERTY( Kind
, wxItemKind
, GetKind
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
104 wxPROPERTY( SubMenu
,wxMenu
*, SetSubMenu
, GetSubMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
105 wxPROPERTY( Enabled
, bool , Enable
, IsEnabled
, wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
106 wxPROPERTY( Checked
, bool , Check
, IsChecked
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
107 wxPROPERTY( Checkable
, bool , SetCheckable
, IsCheckable
, wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
108 wxEND_PROPERTIES_TABLE()
110 wxBEGIN_HANDLERS_TABLE(wxMenuItem
)
111 wxEND_HANDLERS_TABLE()
113 wxDIRECT_CONSTRUCTOR_6( wxMenuItem
, wxMenu
* , Parent
, int , Id
, wxString
, Text
, wxString
, Help
, wxItemKind
, Kind
, wxMenu
* , SubMenu
)
115 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
125 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
127 const wxString
& text
,
128 const wxString
& strHelp
,
131 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
132 #if wxUSE_OWNER_DRAWN
133 , wxOwnerDrawn(text
, kind
== wxITEM_CHECK
, true)
134 #endif // owner drawn
139 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
141 const wxString
& text
,
142 const wxString
& help
,
145 : wxMenuItemBase(parentMenu
, id
, text
, help
,
146 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
147 #if wxUSE_OWNER_DRAWN
148 , wxOwnerDrawn(text
, isCheckable
, true)
149 #endif // owner drawn
154 void wxMenuItem::Init()
156 m_radioGroup
.start
= -1;
157 m_isRadioGroupStart
= false;
159 #if wxUSE_OWNER_DRAWN
161 // when the color is not valid, wxOwnerDraw takes the default ones.
162 // If we set the colors here and they are changed by the user during
163 // the execution, then the colors are not updated until the application
164 // is restarted and our menus look bad
165 SetTextColour(wxNullColour
);
166 SetBackgroundColour(wxNullColour
);
168 // setting default colors switched ownerdraw on: switch it off again
171 // switch ownerdraw back on if using a non default margin
172 if ( GetId() != wxID_SEPARATOR
)
173 SetMarginWidth(GetMarginWidth());
175 // tell the owner drawing code to show the accel string as well
176 SetAccelString(m_text
.AfterFirst(_T('\t')));
177 #endif // wxUSE_OWNER_DRAWN
180 wxMenuItem::~wxMenuItem()
187 // return the id for calling Win32 API functions
188 int wxMenuItem::GetRealId() const
190 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
196 bool wxMenuItem::IsChecked() const
198 // fix that RTTI is always getting the correct state (separators cannot be checked, but the call below
200 if ( GetId() == wxID_SEPARATOR
)
203 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
205 return (flag
& MF_CHECKED
) != 0;
209 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
211 return wxStripMenuCodes(text
);
217 void wxMenuItem::SetAsRadioGroupStart()
219 m_isRadioGroupStart
= true;
222 void wxMenuItem::SetRadioGroupStart(int start
)
224 wxASSERT_MSG( !m_isRadioGroupStart
,
225 _T("should only be called for the next radio items") );
227 m_radioGroup
.start
= start
;
230 void wxMenuItem::SetRadioGroupEnd(int end
)
232 wxASSERT_MSG( m_isRadioGroupStart
,
233 _T("should only be called for the first radio item") );
235 m_radioGroup
.end
= end
;
241 void wxMenuItem::Enable(bool enable
)
243 if ( m_isEnabled
== enable
)
246 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
249 (enable
? MF_ENABLED
: MF_GRAYED
));
252 wxLogLastError(wxT("EnableMenuItem"));
255 wxMenuItemBase::Enable(enable
);
258 void wxMenuItem::Check(bool check
)
260 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
262 if ( m_isChecked
== check
)
265 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
266 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
268 if ( GetKind() == wxITEM_RADIO
)
270 // it doesn't make sense to uncheck a radio item - what would this do?
274 // get the index of this item in the menu
275 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
276 int pos
= items
.IndexOf(this);
277 wxCHECK_RET( pos
!= wxNOT_FOUND
,
278 _T("menuitem not found in the menu items list?") );
280 // get the radio group range
284 if ( m_isRadioGroupStart
)
286 // we already have all information we need
288 end
= m_radioGroup
.end
;
290 else // next radio group item
292 // get the radio group end from the start item
293 start
= m_radioGroup
.start
;
294 end
= items
.Item(start
)->GetData()->m_radioGroup
.end
;
298 // calling CheckMenuRadioItem() with such parameters hangs my system
299 // (NT4 SP6) and I suspect this could happen to the others as well - so
301 wxCHECK_RET( start
!= -1 && end
!= -1,
302 _T("invalid ::CheckMenuRadioItem() parameter(s)") );
304 if ( !::CheckMenuRadioItem(hmenu
,
305 start
, // the first radio group item
307 pos
, // the one to check
310 wxLogLastError(_T("CheckMenuRadioItem"));
314 // also uncheck all the other items in this radio group
315 wxMenuItemList::compatibility_iterator node
= items
.Item(start
);
316 for ( int n
= start
; n
<= end
&& node
; n
++ )
320 node
->GetData()->m_isChecked
= false;
323 node
= node
->GetNext();
328 if ( ::CheckMenuItem(hmenu
,
330 MF_BYCOMMAND
| flags
) == (DWORD
)-1 )
332 wxFAIL_MSG( _T("CheckMenuItem() failed, item not in the menu?") );
336 wxMenuItemBase::Check(check
);
339 void wxMenuItem::SetText(const wxString
& text
)
341 // don't do anything if label didn't change
342 if ( m_text
== text
)
345 wxMenuItemBase::SetText(text
);
346 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
347 #if wxUSE_OWNER_DRAWN
348 // tell the owner drawing code to to show the accel string as well
349 SetAccelString(text
.AfterFirst(_T('\t')));
352 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
353 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
356 m_parentMenu
->UpdateAccel(this);
357 #endif // wxUSE_ACCEL
359 UINT id
= GetRealId();
360 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
361 if ( flagsOld
== 0xFFFFFFFF )
363 // It's not an error, it means that the menu item doesn't exist
364 //wxLogLastError(wxT("GetMenuState"));
370 // high byte contains the number of items in a submenu for submenus
372 flagsOld
|= MF_POPUP
;
377 #if wxUSE_OWNER_DRAWN
378 if ( IsOwnerDrawn() )
380 flagsOld
|= MF_OWNERDRAW
;
381 data
= (LPCTSTR
)this;
386 flagsOld
|= MF_STRING
;
387 data
= (wxChar
*) text
.c_str();
391 // FIXME: complete this, applying the old
393 // However, the WinCE doc for SetMenuItemInfo
394 // says that you can't use it to set the menu
395 // item state; only data, id and type.
398 info
.cbSize
= sizeof(info
);
399 info
.fMask
= MIIM_TYPE
;
400 info
.fType
= MFT_STRING
;
401 info
.cch
= text
.length();
402 info
.dwTypeData
= (LPTSTR
) data
;
403 if ( !::SetMenuItemInfo(hMenu
, id
, FALSE
, & info
) )
405 wxLogLastError(wxT("SetMenuItemInfo"));
408 if ( ::ModifyMenu(hMenu
, id
,
409 MF_BYCOMMAND
| flagsOld
,
410 id
, data
) == (int)0xFFFFFFFF )
412 wxLogLastError(wxT("ModifyMenu"));
418 void wxMenuItem::SetCheckable(bool checkable
)
420 wxMenuItemBase::SetCheckable(checkable
);
421 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
428 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
430 const wxString
& name
,
431 const wxString
& help
,
435 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
438 #endif // wxUSE_MENUS