]>
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 // ============================================================================
17 #pragma implementation "menuitem.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/bitmap.h"
26 #include "wx/settings.h"
28 #include "wx/window.h"
31 #include "wx/string.h"
34 #include "wx/menuitem.h"
41 #include "wx/os2/private.h"
43 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
48 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
50 // conditional compilation
52 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
53 #else // !wxUSE_OWNER_DRAWN
54 #define OWNER_DRAWN_ONLY( code )
55 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // dynamic classes implementation
63 // ----------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem
, wxMenuItemBase
, wxOwnerDrawn
)
67 #else //!USE_OWNER_DRAWN
68 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
69 #endif //USE_OWNER_DRAWN
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
78 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
81 const wxString
& strHelp
,
85 : wxOwnerDrawn(text
, bCheckable
)
88 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
91 // set default menu colors
92 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
94 SetTextColour(SYS_COLOR(MENUTEXT
));
95 SetBackgroundColour(SYS_COLOR(MENU
));
97 // we don't want normal items be owner-drawn
101 #endif // wxUSE_OWNER_DRAWN
103 m_parentMenu
= pParentMenu
;
104 m_subMenu
= pSubMenu
;
109 m_isCheckable
= bCheckable
;
113 wxMenuItem::~wxMenuItem()
120 // return the id for calling Win32 API functions
121 int wxMenuItem::GetRealId() const
123 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
129 bool wxMenuItem::IsChecked() const
132 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
134 // don't "and" with MF_ENABLED because its value is 0
135 return (flag & MF_DISABLED) == 0;
140 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
142 return wxStripMenuCodes(text
);
150 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
152 return wxGetAccelFromString(GetText());
155 #endif // wxUSE_ACCEL
160 void wxMenuItem::Enable(bool enable
)
162 if ( m_isEnabled
== enable
)
165 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
168 (enable ? MF_ENABLED : MF_GRAYED));
171 wxLogLastError("EnableMenuItem");
174 wxMenuItemBase::Enable(enable
);
177 void wxMenuItem::Check(bool check
)
179 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
181 if ( m_isChecked
== check
)
184 long rc = CheckMenuItem(GetHMenuOf(m_parentMenu),
187 (check ? MF_CHECKED : MF_UNCHECKED));
190 wxLogLastError("CheckMenuItem");
193 wxMenuItemBase::Check(check
);
196 void wxMenuItem::SetText(const wxString
& text
)
198 // don't do anything if label didn't change
199 if ( m_text
== text
)
202 wxMenuItemBase::SetText(text
);
203 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
205 HMENU hMenu = GetHMenuOf(m_parentMenu);
206 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
209 m_parentMenu->UpdateAccel(this);
210 #endif // wxUSE_ACCEL
212 UINT id = GetRealId();
213 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
214 if ( flagsOld == 0xFFFFFFFF )
216 wxLogLastError("GetMenuState");
222 // high byte contains the number of items in a submenu for submenus
224 flagsOld |= MF_POPUP;
229 #if wxUSE_OWNER_DRAWN
230 if ( IsOwnerDrawn() )
232 flagsOld |= MF_OWNERDRAW;
233 data = (LPCTSTR)this;
238 flagsOld |= MF_STRING;
239 data = (char*) text.c_str();
242 if ( ::ModifyMenu(hMenu, id,
243 MF_BYCOMMAND | flagsOld,
244 id, data) == (int)0xFFFFFFFF )
246 wxLogLastError(wxT("ModifyMenu"));
252 void wxMenuItem::SetCheckable(bool checkable
)
254 wxMenuItemBase::SetCheckable(checkable
);
255 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
264 const wxString
& name
,
265 const wxString
& help
,
269 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);