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
)
98 wxMenuItem
::wxMenuItem(wxMenu
*parentMenu
,
100 const wxString
& text
,
101 const wxString
& help
,
104 : wxMenuItemBase(parentMenu
, id
, text
, help
,
105 isCheckable ? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
106 #if wxUSE_OWNER_DRAWN
107 , wxOwnerDrawn(GetLabelFromText(text
), isCheckable
)
108 #endif // owner drawn
113 void wxMenuItem
::Init()
116 m_endRadioGroup
= -1;
118 #if wxUSE_OWNER_DRAWN
119 // set default menu colors
120 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
122 SetTextColour(SYS_COLOR(MENUTEXT
));
123 SetBackgroundColour(SYS_COLOR(MENU
));
127 // we don't want normal items be owner-drawn
130 // tell the owner drawing code to to show the accel string as well
131 SetAccelString(m_text
.AfterFirst(_T('\t')));
132 #endif // wxUSE_OWNER_DRAWN
135 wxMenuItem
::~wxMenuItem()
142 // return the id for calling Win32 API functions
143 int wxMenuItem
::GetRealId() const
145 return m_subMenu ?
(int)m_subMenu
->GetHMenu() : GetId();
151 bool wxMenuItem
::IsChecked() const
153 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
155 return (flag
& MF_CHECKED
) != 0;
159 wxString wxMenuItemBase
::GetLabelFromText(const wxString
& text
)
161 return wxStripMenuCodes(text
);
167 void wxMenuItem
::Enable(bool enable
)
169 if ( m_isEnabled
== enable
)
172 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
175 (enable ? MF_ENABLED
: MF_GRAYED
));
178 wxLogLastError(wxT("EnableMenuItem"));
181 wxMenuItemBase
::Enable(enable
);
184 void wxMenuItem
::Check(bool check
)
186 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
188 if ( m_isChecked
== check
)
191 int flags
= check ? MF_CHECKED
: MF_UNCHECKED
;
192 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
194 if ( GetKind() == wxITEM_RADIO
)
196 // it doesn't make sense to uncheck a radio item - what would this do?
200 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
201 int pos
= items
.IndexOf(this);
202 wxCHECK_RET( pos
!= wxNOT_FOUND
,
203 _T("menuitem not found in the menu items list?") );
206 if ( !::CheckMenuRadioItem(hmenu
,
207 m_startRadioGroup
, // first group item
208 m_endRadioGroup
, // last one
209 pos
, // the one to check
210 MF_BYPOSITION
| flags
) )
212 wxLogLastError(_T("CheckMenuRadioItem"));
216 // also uncheck all the other items in this radio group
217 wxMenuItemList
::Node
*node
= items
.Item(m_startRadioGroup
);
218 for ( int n
= m_startRadioGroup
; n
<= m_endRadioGroup
&& node
; n
++ )
222 node
->GetData()->m_isChecked
= FALSE
;
225 // we also have to do it in the menu for Win16 (under Win32
226 // CheckMenuRadioItem() does it for us)
228 ::CheckMenuItem(hmenu
, n
, n
== pos ? MF_CHECKED
: MF_UNCHECKED
);
231 node
= node
->GetNext();
236 if ( ::CheckMenuItem(hmenu
,
238 MF_BYCOMMAND
| flags
) == -1 )
240 wxLogLastError(wxT("CheckMenuItem"));
244 wxMenuItemBase
::Check(check
);
247 void wxMenuItem
::SetText(const wxString
& text
)
249 // don't do anything if label didn't change
250 if ( m_text
== text
)
253 wxMenuItemBase
::SetText(text
);
254 OWNER_DRAWN_ONLY( wxOwnerDrawn
::SetName(text
) );
256 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
257 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
260 m_parentMenu
->UpdateAccel(this);
261 #endif // wxUSE_ACCEL
263 UINT id
= GetRealId();
264 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
265 if ( flagsOld
== 0xFFFFFFFF )
267 wxLogLastError(wxT("GetMenuState"));
273 // high byte contains the number of items in a submenu for submenus
275 flagsOld
|= MF_POPUP
;
280 #if wxUSE_OWNER_DRAWN
281 if ( IsOwnerDrawn() )
283 flagsOld
|= MF_OWNERDRAW
;
284 data
= (LPCTSTR
)this;
289 flagsOld
|= MF_STRING
;
290 data
= (wxChar
*) text
.c_str();
293 if ( ::ModifyMenu(hMenu
, id
,
294 MF_BYCOMMAND
| flagsOld
,
295 id
, data
) == (int)0xFFFFFFFF )
297 wxLogLastError(wxT("ModifyMenu"));
302 void wxMenuItem
::SetCheckable(bool checkable
)
304 wxMenuItemBase
::SetCheckable(checkable
);
305 OWNER_DRAWN_ONLY( wxOwnerDrawn
::SetCheckable(checkable
) );
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 wxMenuItem
*wxMenuItemBase
::New(wxMenu
*parentMenu
,
314 const wxString
& name
,
315 const wxString
& help
,
319 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
322 #endif // wxUSE_MENUS