]>
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 // don't "and" with MF_ENABLED because its value is 0
144 return (flag
& MF_DISABLED
) == 0;
147 wxString
wxMenuItem::GetLabel() const
149 return wxStripMenuCodes(m_text
);
157 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
159 return wxGetAccelFromString(GetText());
162 #endif // wxUSE_ACCEL
167 void wxMenuItem::Enable(bool enable
)
169 if ( m_isEnabled
== enable
)
172 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
175 (enable
? MF_ENABLED
: MF_GRAYED
));
178 wxLogLastError("EnableMenuItem");
181 wxMenuItemBase::Enable(enable
);
184 void wxMenuItem::Check(bool check
)
186 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
188 if ( m_isChecked
== check
)
191 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
194 (check
? MF_CHECKED
: MF_UNCHECKED
));
197 wxLogLastError("CheckMenuItem");
200 wxMenuItemBase::Check(check
);
203 void wxMenuItem::SetText(const wxString
& text
)
205 // don't do anything if label didn't change
206 if ( m_text
== text
)
209 wxMenuItemBase::SetText(text
);
210 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
212 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
213 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
216 m_parentMenu
->UpdateAccel(this);
217 #endif // wxUSE_ACCEL
219 UINT id
= GetRealId();
220 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
221 if ( flagsOld
== 0xFFFFFFFF )
223 wxLogLastError("GetMenuState");
229 // high byte contains the number of items in a submenu for submenus
231 flagsOld
|= MF_POPUP
;
236 #if wxUSE_OWNER_DRAWN
237 if ( IsOwnerDrawn() )
239 flagsOld
|= MF_OWNERDRAW
;
240 data
= (LPCTSTR
)this;
245 flagsOld
|= MF_STRING
;
246 data
= (char*) text
.c_str();
249 if ( ::ModifyMenu(hMenu
, id
,
250 MF_BYCOMMAND
| flagsOld
,
251 id
, data
) == (int)0xFFFFFFFF )
253 wxLogLastError(wxT("ModifyMenu"));
258 void wxMenuItem::SetCheckable(bool checkable
)
260 wxMenuItemBase::SetCheckable(checkable
);
261 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
270 const wxString
& name
,
271 const wxString
& help
,
275 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);