]>
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"
49 #include "wx/msw/private.h"
51 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
56 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
58 // conditional compilation
60 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
61 #else // !wxUSE_OWNER_DRAWN
62 #define OWNER_DRAWN_ONLY( code )
63 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // dynamic classes implementation
71 // ----------------------------------------------------------------------------
73 #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
75 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem
, wxMenuItemBase
, wxOwnerDrawn
)
76 #else //!USE_OWNER_DRAWN
77 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
78 #endif //USE_OWNER_DRAWN
79 #endif //USE_SHARED_LIBRARY
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
88 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
91 const wxString
& strHelp
,
95 wxOwnerDrawn(text
, bCheckable
)
98 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
100 #if wxUSE_OWNER_DRAWN
101 // set default menu colors
102 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
104 SetTextColour(SYS_COLOR(MENUTEXT
));
105 SetBackgroundColour(SYS_COLOR(MENU
));
107 // we don't want normal items be owner-drawn
111 #endif // wxUSE_OWNER_DRAWN
113 m_parentMenu
= pParentMenu
;
114 m_subMenu
= pSubMenu
;
119 m_isCheckable
= bCheckable
;
123 wxMenuItem::~wxMenuItem()
130 // return the id for calling Win32 API functions
131 int wxMenuItem::GetRealId() const
133 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
139 bool wxMenuItem::IsChecked() const
141 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
143 return (flag
& MF_CHECKED
) != 0;
146 wxString
wxMenuItem::GetLabel() const
148 return wxStripMenuCodes(m_text
);
156 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
158 return wxGetAccelFromString(GetText());
161 #endif // wxUSE_ACCEL
166 void wxMenuItem::Enable(bool enable
)
168 if ( m_isEnabled
== enable
)
171 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
174 (enable
? MF_ENABLED
: MF_GRAYED
));
177 wxLogLastError("EnableMenuItem");
180 wxMenuItemBase::Enable(enable
);
183 void wxMenuItem::Check(bool check
)
185 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
187 if ( m_isChecked
== check
)
190 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
193 (check
? MF_CHECKED
: MF_UNCHECKED
));
196 wxLogLastError("CheckMenuItem");
199 wxMenuItemBase::Check(check
);
202 void wxMenuItem::SetText(const wxString
& text
)
204 // don't do anything if label didn't change
205 if ( m_text
== text
)
208 wxMenuItemBase::SetText(text
);
209 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
211 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
212 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
215 m_parentMenu
->UpdateAccel(this);
216 #endif // wxUSE_ACCEL
218 UINT id
= GetRealId();
219 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
220 if ( flagsOld
== 0xFFFFFFFF )
222 wxLogLastError("GetMenuState");
228 // high byte contains the number of items in a submenu for submenus
230 flagsOld
|= MF_POPUP
;
235 #if wxUSE_OWNER_DRAWN
236 if ( IsOwnerDrawn() )
238 flagsOld
|= MF_OWNERDRAW
;
239 data
= (LPCTSTR
)this;
244 flagsOld
|= MF_STRING
;
245 data
= (char*) text
.c_str();
248 if ( ::ModifyMenu(hMenu
, id
,
249 MF_BYCOMMAND
| flagsOld
,
250 id
, data
) == (int)0xFFFFFFFF )
252 wxLogLastError(wxT("ModifyMenu"));
257 void wxMenuItem::SetCheckable(bool checkable
)
259 wxMenuItemBase::SetCheckable(checkable
);
260 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
269 const wxString
& name
,
270 const wxString
& help
,
274 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);