updated samples/html/widget to avoid confusion
[wxWidgets.git] / src / msw / menuitem.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: menuitem.cpp
3// Purpose: wxMenuItem implementation
4// Author: Vadim Zeitlin
c2dcfdef 5// Modified by:
2bda0e17
KB
6// Created: 11.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
c2dcfdef
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
a3b46648 20#ifdef __GNUG__
c2dcfdef 21 #pragma implementation "menuitem.h"
a3b46648 22#endif
2bda0e17
KB
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
c2dcfdef 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_MENUS
32
2bda0e17 33#ifndef WX_PRECOMP
c2dcfdef
VZ
34 #include "wx/font.h"
35 #include "wx/bitmap.h"
36 #include "wx/settings.h"
37 #include "wx/font.h"
4e938f5b 38 #include "wx/window.h"
0c589ad0
BM
39 #include "wx/accel.h"
40 #include "wx/menu.h"
41 #include "wx/string.h"
2bda0e17
KB
42#endif
43
2bda0e17 44#include "wx/menuitem.h"
6776a0b2 45#include "wx/log.h"
2bda0e17 46
717a57c2
VZ
47#if wxUSE_ACCEL
48 #include "wx/accel.h"
49#endif // wxUSE_ACCEL
50
42e69d6b 51#include "wx/msw/private.h"
ce3ed50d 52
c2dcfdef 53// ---------------------------------------------------------------------------
974e8d94 54// macro
c2dcfdef
VZ
55// ---------------------------------------------------------------------------
56
974e8d94 57// hide the ugly cast
c2dcfdef
VZ
58#define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
59
974e8d94
VZ
60// conditional compilation
61#if wxUSE_OWNER_DRAWN
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
66
2bda0e17
KB
67// ============================================================================
68// implementation
69// ============================================================================
70
71// ----------------------------------------------------------------------------
72// dynamic classes implementation
73// ----------------------------------------------------------------------------
74
621b3e21 75IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
2bda0e17
KB
76
77// ----------------------------------------------------------------------------
78// wxMenuItem
79// ----------------------------------------------------------------------------
80
81// ctor & dtor
82// -----------
83
974e8d94
VZ
84wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
85 int id,
86 const wxString& text,
87 const wxString& strHelp,
d65c269b 88 wxItemKind kind,
90002c49 89 wxMenu *pSubMenu)
d65c269b 90 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
47d67540 91#if wxUSE_OWNER_DRAWN
d65c269b 92 , wxOwnerDrawn(GetLabelFromText(text), kind == wxItem_Check)
974e8d94 93#endif // owner drawn
2bda0e17 94{
223d09f6 95 wxASSERT_MSG( pParentMenu != NULL, wxT("a menu item should have a parent") );
2bda0e17 96
47d67540 97#if wxUSE_OWNER_DRAWN
2bda0e17 98 // set default menu colors
a756f210 99 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
c2dcfdef 100
2bda0e17
KB
101 SetTextColour(SYS_COLOR(MENUTEXT));
102 SetBackgroundColour(SYS_COLOR(MENU));
103
6d5b2a57
VZ
104 #undef SYS_COLOR
105
2bda0e17
KB
106 // we don't want normal items be owner-drawn
107 ResetOwnerDrawn();
108
6d5b2a57
VZ
109 // tell the owner drawing code to to show the accel string as well
110 SetAccelString(text.AfterFirst(_T('\t')));
974e8d94 111#endif // wxUSE_OWNER_DRAWN
2bda0e17
KB
112}
113
c2dcfdef 114wxMenuItem::~wxMenuItem()
2bda0e17
KB
115{
116}
117
118// misc
119// ----
120
c2dcfdef
VZ
121// return the id for calling Win32 API functions
122int wxMenuItem::GetRealId() const
123{
974e8d94 124 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
c2dcfdef
VZ
125}
126
3dfac970
VZ
127// get item state
128// --------------
129
a8cfd0cb 130bool wxMenuItem::IsChecked() const
3dfac970 131{
a8cfd0cb 132 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
3dfac970 133
4aee367e 134 return (flag & MF_CHECKED) != 0;
3dfac970
VZ
135}
136
3b59cdbf
VZ
137/* static */
138wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
717a57c2 139{
4a3563c5 140 return wxStripMenuCodes(text);
717a57c2
VZ
141}
142
2bda0e17
KB
143// change item state
144// -----------------
145
717a57c2 146void wxMenuItem::Enable(bool enable)
2bda0e17 147{
717a57c2
VZ
148 if ( m_isEnabled == enable )
149 return;
150
151 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
152 GetRealId(),
153 MF_BYCOMMAND |
154 (enable ? MF_ENABLED : MF_GRAYED));
c2dcfdef 155
717a57c2 156 if ( rc == -1 ) {
f6bcfd97 157 wxLogLastError(wxT("EnableMenuItem"));
c2dcfdef 158 }
717a57c2
VZ
159
160 wxMenuItemBase::Enable(enable);
2bda0e17
KB
161}
162
717a57c2 163void wxMenuItem::Check(bool check)
2bda0e17 164{
d65c269b 165 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
c2dcfdef 166
717a57c2
VZ
167 if ( m_isChecked == check )
168 return;
c2dcfdef 169
717a57c2
VZ
170 long rc = CheckMenuItem(GetHMenuOf(m_parentMenu),
171 GetRealId(),
172 MF_BYCOMMAND |
173 (check ? MF_CHECKED : MF_UNCHECKED));
c2dcfdef 174
717a57c2 175 if ( rc == -1 ) {
f6bcfd97 176 wxLogLastError(wxT("CheckMenuItem"));
c2dcfdef 177 }
717a57c2
VZ
178
179 wxMenuItemBase::Check(check);
c2dcfdef
VZ
180}
181
974e8d94 182void wxMenuItem::SetText(const wxString& text)
c2dcfdef
VZ
183{
184 // don't do anything if label didn't change
974e8d94 185 if ( m_text == text )
c2dcfdef
VZ
186 return;
187
974e8d94
VZ
188 wxMenuItemBase::SetText(text);
189 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
c2dcfdef 190
974e8d94 191 HMENU hMenu = GetHMenuOf(m_parentMenu);
717a57c2
VZ
192 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
193
194#if wxUSE_ACCEL
195 m_parentMenu->UpdateAccel(this);
196#endif // wxUSE_ACCEL
2bda0e17 197
c2dcfdef
VZ
198 UINT id = GetRealId();
199 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
200 if ( flagsOld == 0xFFFFFFFF )
201 {
f6bcfd97 202 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
203 }
204 else
205 {
206 if ( IsSubMenu() )
207 {
208 // high byte contains the number of items in a submenu for submenus
209 flagsOld &= 0xFF;
210 flagsOld |= MF_POPUP;
211 }
212
837e5743 213 LPCTSTR data;
974e8d94 214
c2dcfdef
VZ
215#if wxUSE_OWNER_DRAWN
216 if ( IsOwnerDrawn() )
217 {
218 flagsOld |= MF_OWNERDRAW;
837e5743 219 data = (LPCTSTR)this;
c2dcfdef
VZ
220 }
221 else
222#endif //owner drawn
223 {
224 flagsOld |= MF_STRING;
f5166ed4 225 data = (wxChar*) text.c_str();
c2dcfdef
VZ
226 }
227
228 if ( ::ModifyMenu(hMenu, id,
229 MF_BYCOMMAND | flagsOld,
a17e237f 230 id, data) == (int)0xFFFFFFFF )
c2dcfdef 231 {
223d09f6 232 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef
VZ
233 }
234 }
235}
2bda0e17 236
974e8d94
VZ
237void wxMenuItem::SetCheckable(bool checkable)
238{
239 wxMenuItemBase::SetCheckable(checkable);
240 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
241}
242
243// ----------------------------------------------------------------------------
244// wxMenuItemBase
245// ----------------------------------------------------------------------------
246
247wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
248 int id,
249 const wxString& name,
250 const wxString& help,
d65c269b 251 wxItemKind kind,
974e8d94
VZ
252 wxMenu *subMenu)
253{
d65c269b 254 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 255}
1e6feb95
VZ
256
257#endif // wxUSE_MENUS