]>
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"
35 #include "wx/bitmap.h"
36 #include "wx/settings.h"
38 #include "wx/window.h"
41 #include "wx/string.h"
44 #include "wx/menuitem.h"
51 #include "wx/msw/private.h"
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
58 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
60 // conditional compilation
62 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
63 #else // !wxUSE_OWNER_DRAWN
64 #define OWNER_DRAWN_ONLY( code )
65 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
72 // dynamic classes implementation
73 // ----------------------------------------------------------------------------
76 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem
, wxMenuItemBase
, wxOwnerDrawn
)
77 #else //!USE_OWNER_DRAWN
78 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
79 #endif //USE_OWNER_DRAWN
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
88 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
91 const wxString
& strHelp
,
95 : wxOwnerDrawn(GetLabelFromText(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::GetColour(wxSYS_COLOUR_##c))
104 SetTextColour(SYS_COLOR(MENUTEXT
));
105 SetBackgroundColour(SYS_COLOR(MENU
));
109 // we don't want normal items be owner-drawn
112 // tell the owner drawing code to to show the accel string as well
113 SetAccelString(text
.AfterFirst(_T('\t')));
114 #endif // wxUSE_OWNER_DRAWN
116 m_parentMenu
= pParentMenu
;
117 m_subMenu
= pSubMenu
;
122 m_isCheckable
= bCheckable
;
126 wxMenuItem::~wxMenuItem()
133 // return the id for calling Win32 API functions
134 int wxMenuItem::GetRealId() const
136 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
142 bool wxMenuItem::IsChecked() const
144 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
146 return (flag
& MF_CHECKED
) != 0;
150 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
152 return wxStripMenuCodes(text
);
158 void wxMenuItem::Enable(bool enable
)
160 if ( m_isEnabled
== enable
)
163 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
166 (enable
? MF_ENABLED
: MF_GRAYED
));
169 wxLogLastError(wxT("EnableMenuItem"));
172 wxMenuItemBase::Enable(enable
);
175 void wxMenuItem::Check(bool check
)
177 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
179 if ( m_isChecked
== check
)
182 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
185 (check
? MF_CHECKED
: MF_UNCHECKED
));
188 wxLogLastError(wxT("CheckMenuItem"));
191 wxMenuItemBase::Check(check
);
194 void wxMenuItem::SetText(const wxString
& text
)
196 // don't do anything if label didn't change
197 if ( m_text
== text
)
200 wxMenuItemBase::SetText(text
);
201 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
203 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
204 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
207 m_parentMenu
->UpdateAccel(this);
208 #endif // wxUSE_ACCEL
210 UINT id
= GetRealId();
211 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
212 if ( flagsOld
== 0xFFFFFFFF )
214 wxLogLastError(wxT("GetMenuState"));
220 // high byte contains the number of items in a submenu for submenus
222 flagsOld
|= MF_POPUP
;
227 #if wxUSE_OWNER_DRAWN
228 if ( IsOwnerDrawn() )
230 flagsOld
|= MF_OWNERDRAW
;
231 data
= (LPCTSTR
)this;
236 flagsOld
|= MF_STRING
;
237 data
= (wxChar
*) text
.c_str();
240 if ( ::ModifyMenu(hMenu
, id
,
241 MF_BYCOMMAND
| flagsOld
,
242 id
, data
) == (int)0xFFFFFFFF )
244 wxLogLastError(wxT("ModifyMenu"));
249 void wxMenuItem::SetCheckable(bool checkable
)
251 wxMenuItemBase::SetCheckable(checkable
);
252 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
261 const wxString
& name
,
262 const wxString
& help
,
266 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);
269 #endif // wxUSE_MENUS