]>
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 // ----------------------------------------------------------------------------
75 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
84 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
87 const wxString
& strHelp
,
91 : wxOwnerDrawn(GetLabelFromText(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::GetColour(wxSYS_COLOUR_##c))
100 SetTextColour(SYS_COLOR(MENUTEXT
));
101 SetBackgroundColour(SYS_COLOR(MENU
));
105 // we don't want normal items be owner-drawn
108 // tell the owner drawing code to to show the accel string as well
109 SetAccelString(text
.AfterFirst(_T('\t')));
110 #endif // wxUSE_OWNER_DRAWN
112 m_parentMenu
= pParentMenu
;
113 m_subMenu
= pSubMenu
;
118 m_isCheckable
= bCheckable
;
122 wxMenuItem::~wxMenuItem()
129 // return the id for calling Win32 API functions
130 int wxMenuItem::GetRealId() const
132 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
138 bool wxMenuItem::IsChecked() const
140 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
142 return (flag
& MF_CHECKED
) != 0;
146 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
148 return wxStripMenuCodes(text
);
154 void wxMenuItem::Enable(bool enable
)
156 if ( m_isEnabled
== enable
)
159 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
162 (enable
? MF_ENABLED
: MF_GRAYED
));
165 wxLogLastError(wxT("EnableMenuItem"));
168 wxMenuItemBase::Enable(enable
);
171 void wxMenuItem::Check(bool check
)
173 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
175 if ( m_isChecked
== check
)
178 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
181 (check
? MF_CHECKED
: MF_UNCHECKED
));
184 wxLogLastError(wxT("CheckMenuItem"));
187 wxMenuItemBase::Check(check
);
190 void wxMenuItem::SetText(const wxString
& text
)
192 // don't do anything if label didn't change
193 if ( m_text
== text
)
196 wxMenuItemBase::SetText(text
);
197 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
199 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
200 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
203 m_parentMenu
->UpdateAccel(this);
204 #endif // wxUSE_ACCEL
206 UINT id
= GetRealId();
207 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
208 if ( flagsOld
== 0xFFFFFFFF )
210 wxLogLastError(wxT("GetMenuState"));
216 // high byte contains the number of items in a submenu for submenus
218 flagsOld
|= MF_POPUP
;
223 #if wxUSE_OWNER_DRAWN
224 if ( IsOwnerDrawn() )
226 flagsOld
|= MF_OWNERDRAW
;
227 data
= (LPCTSTR
)this;
232 flagsOld
|= MF_STRING
;
233 data
= (wxChar
*) text
.c_str();
236 if ( ::ModifyMenu(hMenu
, id
,
237 MF_BYCOMMAND
| flagsOld
,
238 id
, data
) == (int)0xFFFFFFFF )
240 wxLogLastError(wxT("ModifyMenu"));
245 void wxMenuItem::SetCheckable(bool checkable
)
247 wxMenuItemBase::SetCheckable(checkable
);
248 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
257 const wxString
& name
,
258 const wxString
& help
,
262 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);
265 #endif // wxUSE_MENUS