]>
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 // ----------------------------------------------------------------------------
61 #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
63 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem
, wxMenuItemBase
, wxOwnerDrawn
)
64 #else //!USE_OWNER_DRAWN
65 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
66 #endif //USE_OWNER_DRAWN
67 #endif //USE_SHARED_LIBRARY
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
76 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
79 const wxString
& strHelp
,
83 wxOwnerDrawn(text
, bCheckable
)
86 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
89 // set default menu colors
90 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
92 SetTextColour(SYS_COLOR(MENUTEXT
));
93 SetBackgroundColour(SYS_COLOR(MENU
));
95 // we don't want normal items be owner-drawn
99 #endif // wxUSE_OWNER_DRAWN
101 m_parentMenu
= pParentMenu
;
102 m_subMenu
= pSubMenu
;
107 m_isCheckable
= bCheckable
;
111 wxMenuItem::~wxMenuItem()
118 // return the id for calling Win32 API functions
119 int wxMenuItem::GetRealId() const
121 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
127 bool wxMenuItem::IsChecked() const
130 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
132 // don't "and" with MF_ENABLED because its value is 0
133 return (flag & MF_DISABLED) == 0;
138 wxString
wxMenuItem::GetLabel() const
140 return wxStripMenuCodes(m_text
);
148 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
150 return wxGetAccelFromString(GetText());
153 #endif // wxUSE_ACCEL
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("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("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("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 = (char*) text.c_str();
240 if ( ::ModifyMenu(hMenu, id,
241 MF_BYCOMMAND | flagsOld,
242 id, data) == (int)0xFFFFFFFF )
244 wxLogLastError(wxT("ModifyMenu"));
250 void wxMenuItem::SetCheckable(bool checkable
)
252 wxMenuItemBase::SetCheckable(checkable
);
253 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
262 const wxString
& name
,
263 const wxString
& help
,
267 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);