]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
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 bool wxMenuItem::IsChecked() const
145 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
147 // don't "and" with MF_ENABLED because its value is 0
148 return (flag
& MF_DISABLED
) == 0;
154 void wxMenuItem::Enable(bool bDoEnable
)
156 if ( m_isEnabled
!= bDoEnable
) {
157 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
160 (bDoEnable
? MF_ENABLED
: MF_GRAYED
));
163 wxLogLastError("EnableMenuItem");
166 wxMenuItemBase::Enable(m_isEnabled
);
170 void wxMenuItem::Check(bool bDoCheck
)
172 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
174 if ( m_isChecked
!= bDoCheck
) {
175 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
178 (bDoCheck
? MF_CHECKED
: MF_UNCHECKED
));
181 wxLogLastError("CheckMenuItem");
184 wxMenuItemBase::Check(m_isChecked
);
188 void wxMenuItem::SetText(const wxString
& text
)
190 // don't do anything if label didn't change
191 if ( m_text
== text
)
194 wxMenuItemBase::SetText(text
);
195 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
197 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
199 UINT id
= GetRealId();
200 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
201 if ( flagsOld
== 0xFFFFFFFF )
203 wxLogLastError("GetMenuState");
209 // high byte contains the number of items in a submenu for submenus
211 flagsOld
|= MF_POPUP
;
216 #if wxUSE_OWNER_DRAWN
217 if ( IsOwnerDrawn() )
219 flagsOld
|= MF_OWNERDRAW
;
220 data
= (LPCTSTR
)this;
225 flagsOld
|= MF_STRING
;
226 data
= (char*) text
.c_str();
229 if ( ::ModifyMenu(hMenu
, id
,
230 MF_BYCOMMAND
| flagsOld
,
231 id
, data
) == (int)0xFFFFFFFF )
233 wxLogLastError(wxT("ModifyMenu"));
238 void wxMenuItem::SetCheckable(bool checkable
)
240 wxMenuItemBase::SetCheckable(checkable
);
241 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
250 const wxString
& name
,
251 const wxString
& help
,
255 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);