]>
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
,
90 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
92 , wxOwnerDrawn(GetLabelFromText(text
), kind
== wxItem_Check
)
95 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
98 // set default menu colors
99 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
101 SetTextColour(SYS_COLOR(MENUTEXT
));
102 SetBackgroundColour(SYS_COLOR(MENU
));
106 // we don't want normal items be owner-drawn
109 // tell the owner drawing code to to show the accel string as well
110 SetAccelString(text
.AfterFirst(_T('\t')));
111 #endif // wxUSE_OWNER_DRAWN
114 wxMenuItem::~wxMenuItem()
121 // return the id for calling Win32 API functions
122 int wxMenuItem::GetRealId() const
124 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
130 bool wxMenuItem::IsChecked() const
132 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
134 return (flag
& MF_CHECKED
) != 0;
138 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
140 return wxStripMenuCodes(text
);
146 void wxMenuItem::Enable(bool enable
)
148 if ( m_isEnabled
== enable
)
151 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
154 (enable
? MF_ENABLED
: MF_GRAYED
));
157 wxLogLastError(wxT("EnableMenuItem"));
160 wxMenuItemBase::Enable(enable
);
163 void wxMenuItem::Check(bool check
)
165 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
167 if ( m_isChecked
== check
)
170 long rc
= CheckMenuItem(GetHMenuOf(m_parentMenu
),
173 (check
? MF_CHECKED
: MF_UNCHECKED
));
176 wxLogLastError(wxT("CheckMenuItem"));
179 wxMenuItemBase::Check(check
);
182 void wxMenuItem::SetText(const wxString
& text
)
184 // don't do anything if label didn't change
185 if ( m_text
== text
)
188 wxMenuItemBase::SetText(text
);
189 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
191 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
192 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
195 m_parentMenu
->UpdateAccel(this);
196 #endif // wxUSE_ACCEL
198 UINT id
= GetRealId();
199 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
200 if ( flagsOld
== 0xFFFFFFFF )
202 wxLogLastError(wxT("GetMenuState"));
208 // high byte contains the number of items in a submenu for submenus
210 flagsOld
|= MF_POPUP
;
215 #if wxUSE_OWNER_DRAWN
216 if ( IsOwnerDrawn() )
218 flagsOld
|= MF_OWNERDRAW
;
219 data
= (LPCTSTR
)this;
224 flagsOld
|= MF_STRING
;
225 data
= (wxChar
*) text
.c_str();
228 if ( ::ModifyMenu(hMenu
, id
,
229 MF_BYCOMMAND
| flagsOld
,
230 id
, data
) == (int)0xFFFFFFFF )
232 wxLogLastError(wxT("ModifyMenu"));
237 void wxMenuItem::SetCheckable(bool checkable
)
239 wxMenuItemBase::SetCheckable(checkable
);
240 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
249 const wxString
& name
,
250 const wxString
& help
,
254 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
257 #endif // wxUSE_MENUS