]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
f9e310d9a9994cc8a42def673c56bf4420b16ca1
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 // ----------------------------------------------------------------------------
74 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem
, wxMenuItemBase
, wxOwnerDrawn
)
75 #else //!USE_OWNER_DRAWN
76 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
77 #endif //USE_OWNER_DRAWN
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
86 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
89 const wxString
& strHelp
,
93 : wxOwnerDrawn(text
, bCheckable
)
96 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
99 // set default menu colors
100 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
102 SetTextColour(SYS_COLOR(MENUTEXT
));
103 SetBackgroundColour(SYS_COLOR(MENU
));
105 // we don't want normal items be owner-drawn
109 #endif // wxUSE_OWNER_DRAWN
111 m_parentMenu
= pParentMenu
;
112 m_subMenu
= pSubMenu
;
117 m_isCheckable
= bCheckable
;
121 wxMenuItem::~wxMenuItem()
128 // return the id for calling Win32 API functions
129 int wxMenuItem::GetRealId() const
131 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
137 bool wxMenuItem::IsChecked() const
139 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
141 return (flag
& MF_CHECKED
) != 0;
144 wxString
wxMenuItem::GetLabel() const
146 return wxStripMenuCodes(m_text
);
154 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
156 return wxGetAccelFromString(GetText());
159 #endif // wxUSE_ACCEL
164 void wxMenuItem::Enable(bool enable
)
166 if ( m_isEnabled
== enable
)
169 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
172 (enable
? MF_ENABLED
: MF_GRAYED
));
175 wxLogLastError("EnableMenuItem");
178 wxMenuItemBase::Enable(enable
);
181 void wxMenuItem::Check(bool check
)
183 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
185 if ( m_isChecked
== check
)
188 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
191 (check
? MF_CHECKED
: MF_UNCHECKED
));
194 wxLogLastError("CheckMenuItem");
197 wxMenuItemBase::Check(check
);
200 void wxMenuItem::SetText(const wxString
& text
)
202 // don't do anything if label didn't change
203 if ( m_text
== text
)
206 wxMenuItemBase::SetText(text
);
207 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
209 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
210 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
213 m_parentMenu
->UpdateAccel(this);
214 #endif // wxUSE_ACCEL
216 UINT id
= GetRealId();
217 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
218 if ( flagsOld
== 0xFFFFFFFF )
220 wxLogLastError("GetMenuState");
226 // high byte contains the number of items in a submenu for submenus
228 flagsOld
|= MF_POPUP
;
233 #if wxUSE_OWNER_DRAWN
234 if ( IsOwnerDrawn() )
236 flagsOld
|= MF_OWNERDRAW
;
237 data
= (LPCTSTR
)this;
242 flagsOld
|= MF_STRING
;
243 data
= (char*) text
.c_str();
246 if ( ::ModifyMenu(hMenu
, id
,
247 MF_BYCOMMAND
| flagsOld
,
248 id
, data
) == (int)0xFFFFFFFF )
250 wxLogLastError(wxT("ModifyMenu"));
255 void wxMenuItem::SetCheckable(bool checkable
)
257 wxMenuItemBase::SetCheckable(checkable
);
258 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
267 const wxString
& name
,
268 const wxString
& help
,
272 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);