]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
21 #include "wx/bitmap.h"
22 #include "wx/settings.h"
24 #include "wx/window.h"
27 #include "wx/string.h"
30 #include "wx/menuitem.h"
37 #include "wx/os2/private.h"
39 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
44 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
46 // conditional compilation
48 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
49 #else // !wxUSE_OWNER_DRAWN
50 #define OWNER_DRAWN_ONLY( code )
51 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
58 // dynamic classes implementation
59 // ----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem
, wxMenuItemBase
, wxOwnerDrawn
)
63 #else //!USE_OWNER_DRAWN
64 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
65 #endif //USE_OWNER_DRAWN
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
74 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
77 const wxString
& strHelp
,
81 wxOwnerDrawn(text
, bCheckable
)
84 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
87 // set default menu colors
88 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
90 SetTextColour(SYS_COLOR(MENUTEXT
));
91 SetBackgroundColour(SYS_COLOR(MENU
));
93 // we don't want normal items be owner-drawn
97 #endif // wxUSE_OWNER_DRAWN
99 m_parentMenu
= pParentMenu
;
100 m_subMenu
= pSubMenu
;
105 m_isCheckable
= bCheckable
;
109 wxMenuItem::~wxMenuItem()
116 // return the id for calling Win32 API functions
117 int wxMenuItem::GetRealId() const
119 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
125 bool wxMenuItem::IsChecked() const
128 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
130 // don't "and" with MF_ENABLED because its value is 0
131 return (flag & MF_DISABLED) == 0;
136 wxString
wxMenuItem::GetLabel() const
138 return wxStripMenuCodes(m_text
);
146 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
148 return wxGetAccelFromString(GetText());
151 #endif // wxUSE_ACCEL
156 void wxMenuItem::Enable(bool enable
)
158 if ( m_isEnabled
== enable
)
161 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
164 (enable ? MF_ENABLED : MF_GRAYED));
167 wxLogLastError("EnableMenuItem");
170 wxMenuItemBase::Enable(enable
);
173 void wxMenuItem::Check(bool check
)
175 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
177 if ( m_isChecked
== check
)
180 long rc = CheckMenuItem(GetHMenuOf(m_parentMenu),
183 (check ? MF_CHECKED : MF_UNCHECKED));
186 wxLogLastError("CheckMenuItem");
189 wxMenuItemBase::Check(check
);
192 void wxMenuItem::SetText(const wxString
& text
)
194 // don't do anything if label didn't change
195 if ( m_text
== text
)
198 wxMenuItemBase::SetText(text
);
199 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
201 HMENU hMenu = GetHMenuOf(m_parentMenu);
202 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
205 m_parentMenu->UpdateAccel(this);
206 #endif // wxUSE_ACCEL
208 UINT id = GetRealId();
209 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
210 if ( flagsOld == 0xFFFFFFFF )
212 wxLogLastError("GetMenuState");
218 // high byte contains the number of items in a submenu for submenus
220 flagsOld |= MF_POPUP;
225 #if wxUSE_OWNER_DRAWN
226 if ( IsOwnerDrawn() )
228 flagsOld |= MF_OWNERDRAW;
229 data = (LPCTSTR)this;
234 flagsOld |= MF_STRING;
235 data = (char*) text.c_str();
238 if ( ::ModifyMenu(hMenu, id,
239 MF_BYCOMMAND | flagsOld,
240 id, data) == (int)0xFFFFFFFF )
242 wxLogLastError(wxT("ModifyMenu"));
248 void wxMenuItem::SetCheckable(bool checkable
)
250 wxMenuItemBase::SetCheckable(checkable
);
251 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
260 const wxString
& name
,
261 const wxString
& help
,
265 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);