]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
09c4986f1538b66c267869c7f4d8123f34aa1895
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "menuitem.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/bitmap.h"
34 #include "wx/settings.h"
36 #include "wx/window.h"
39 #include "wx/string.h"
42 #include "wx/menuitem.h"
45 #include "wx/msw/private.h"
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
52 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
54 // conditional compilation
56 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
57 #else // !wxUSE_OWNER_DRAWN
58 #define OWNER_DRAWN_ONLY( code )
59 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // dynamic classes implementation
67 // ----------------------------------------------------------------------------
69 #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
71 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem
, wxMenuItemBase
, wxOwnerDrawn
)
72 #else //!USE_OWNER_DRAWN
73 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
74 #endif //USE_OWNER_DRAWN
75 #endif //USE_SHARED_LIBRARY
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
84 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
87 const wxString
& strHelp
,
91 wxOwnerDrawn(text
, bCheckable
)
94 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
97 // set default menu colors
98 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
100 SetTextColour(SYS_COLOR(MENUTEXT
));
101 SetBackgroundColour(SYS_COLOR(MENU
));
103 // we don't want normal items be owner-drawn
107 #endif // wxUSE_OWNER_DRAWN
109 m_parentMenu
= pParentMenu
;
110 m_subMenu
= pSubMenu
;
115 m_isCheckable
= bCheckable
;
119 wxMenuItem::~wxMenuItem()
126 // return the id for calling Win32 API functions
127 int wxMenuItem::GetRealId() const
129 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
132 // delete the sub menu
133 // -------------------
134 void wxMenuItem::DeleteSubMenu()
143 void wxMenuItem::Enable(bool bDoEnable
)
145 if ( m_isEnabled
!= bDoEnable
) {
146 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
149 (bDoEnable
? MF_ENABLED
: MF_GRAYED
));
152 wxLogLastError("EnableMenuItem");
155 wxMenuItemBase::Enable(m_isEnabled
);
159 void wxMenuItem::Check(bool bDoCheck
)
161 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
163 if ( m_isChecked
!= bDoCheck
) {
164 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
167 (bDoCheck
? MF_CHECKED
: MF_UNCHECKED
));
170 wxLogLastError("CheckMenuItem");
173 wxMenuItemBase::Check(m_isChecked
);
177 void wxMenuItem::SetText(const wxString
& text
)
179 // don't do anything if label didn't change
180 if ( m_text
== text
)
183 wxMenuItemBase::SetText(text
);
184 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
186 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
188 UINT id
= GetRealId();
189 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
190 if ( flagsOld
== 0xFFFFFFFF )
192 wxLogLastError("GetMenuState");
198 // high byte contains the number of items in a submenu for submenus
200 flagsOld
|= MF_POPUP
;
205 #if wxUSE_OWNER_DRAWN
206 if ( IsOwnerDrawn() )
208 flagsOld
|= MF_OWNERDRAW
;
209 data
= (LPCTSTR
)this;
214 flagsOld
|= MF_STRING
;
215 data
= (char*) text
.c_str();
218 if ( ::ModifyMenu(hMenu
, id
,
219 MF_BYCOMMAND
| flagsOld
,
220 id
, data
) == (int)0xFFFFFFFF )
222 wxLogLastError(wxT("ModifyMenu"));
227 void wxMenuItem::SetCheckable(bool checkable
)
229 wxMenuItemBase::SetCheckable(checkable
);
230 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
239 const wxString
& name
,
240 const wxString
& help
,
244 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);