]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menuitem.cpp
<br><br><br> is now handled correctly, e.g. empty lines are inserted (unlike <p>...
[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
31#ifndef WX_PRECOMP
c2dcfdef
VZ
32 #include "wx/font.h"
33 #include "wx/bitmap.h"
34 #include "wx/settings.h"
35 #include "wx/font.h"
4e938f5b 36 #include "wx/window.h"
0c589ad0
BM
37 #include "wx/accel.h"
38 #include "wx/menu.h"
39 #include "wx/string.h"
2bda0e17
KB
40#endif
41
2bda0e17 42#include "wx/menuitem.h"
6776a0b2 43#include "wx/log.h"
2bda0e17 44
717a57c2
VZ
45#if wxUSE_ACCEL
46 #include "wx/accel.h"
47#endif // wxUSE_ACCEL
48
42e69d6b 49#include "wx/msw/private.h"
ce3ed50d 50
c2dcfdef 51// ---------------------------------------------------------------------------
974e8d94 52// macro
c2dcfdef
VZ
53// ---------------------------------------------------------------------------
54
974e8d94 55// hide the ugly cast
c2dcfdef
VZ
56#define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
57
974e8d94
VZ
58// conditional compilation
59#if wxUSE_OWNER_DRAWN
60 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
61#else // !wxUSE_OWNER_DRAWN
62 #define OWNER_DRAWN_ONLY( code )
63#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
64
2bda0e17
KB
65// ============================================================================
66// implementation
67// ============================================================================
68
69// ----------------------------------------------------------------------------
70// dynamic classes implementation
71// ----------------------------------------------------------------------------
72
73#if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
974e8d94
VZ
74 #if wxUSE_OWNER_DRAWN
75 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem, wxMenuItemBase, wxOwnerDrawn)
76 #else //!USE_OWNER_DRAWN
77 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
78 #endif //USE_OWNER_DRAWN
2bda0e17
KB
79#endif //USE_SHARED_LIBRARY
80
81// ----------------------------------------------------------------------------
82// wxMenuItem
83// ----------------------------------------------------------------------------
84
85// ctor & dtor
86// -----------
87
974e8d94
VZ
88wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
89 int id,
90 const wxString& text,
91 const wxString& strHelp,
2bda0e17
KB
92 bool bCheckable,
93 wxMenu *pSubMenu) :
47d67540 94#if wxUSE_OWNER_DRAWN
974e8d94
VZ
95 wxOwnerDrawn(text, bCheckable)
96#endif // owner drawn
2bda0e17 97{
223d09f6 98 wxASSERT_MSG( pParentMenu != NULL, wxT("a menu item should have a parent") );
2bda0e17 99
47d67540 100#if wxUSE_OWNER_DRAWN
2bda0e17
KB
101 // set default menu colors
102 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
c2dcfdef 103
2bda0e17
KB
104 SetTextColour(SYS_COLOR(MENUTEXT));
105 SetBackgroundColour(SYS_COLOR(MENU));
106
107 // we don't want normal items be owner-drawn
108 ResetOwnerDrawn();
109
110 #undef SYS_COLOR
974e8d94
VZ
111#endif // wxUSE_OWNER_DRAWN
112
113 m_parentMenu = pParentMenu;
114 m_subMenu = pSubMenu;
115 m_isEnabled = TRUE;
116 m_isChecked = FALSE;
117 m_id = id;
118 m_text = text;
119 m_isCheckable = bCheckable;
120 m_help = strHelp;
2bda0e17
KB
121}
122
c2dcfdef 123wxMenuItem::~wxMenuItem()
2bda0e17
KB
124{
125}
126
127// misc
128// ----
129
c2dcfdef
VZ
130// return the id for calling Win32 API functions
131int wxMenuItem::GetRealId() const
132{
974e8d94 133 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
c2dcfdef
VZ
134}
135
3dfac970
VZ
136// get item state
137// --------------
138
a8cfd0cb 139bool wxMenuItem::IsChecked() const
3dfac970 140{
a8cfd0cb 141 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
3dfac970 142
4aee367e 143 return (flag & MF_CHECKED) != 0;
3dfac970
VZ
144}
145
717a57c2
VZ
146wxString wxMenuItem::GetLabel() const
147{
148 return wxStripMenuCodes(m_text);
149}
150
151// accelerators
152// ------------
153
154#if wxUSE_ACCEL
155
156wxAcceleratorEntry *wxMenuItem::GetAccel() const
157{
158 return wxGetAccelFromString(GetText());
159}
160
161#endif // wxUSE_ACCEL
162
2bda0e17
KB
163// change item state
164// -----------------
165
717a57c2 166void wxMenuItem::Enable(bool enable)
2bda0e17 167{
717a57c2
VZ
168 if ( m_isEnabled == enable )
169 return;
170
171 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
172 GetRealId(),
173 MF_BYCOMMAND |
174 (enable ? MF_ENABLED : MF_GRAYED));
c2dcfdef 175
717a57c2
VZ
176 if ( rc == -1 ) {
177 wxLogLastError("EnableMenuItem");
c2dcfdef 178 }
717a57c2
VZ
179
180 wxMenuItemBase::Enable(enable);
2bda0e17
KB
181}
182
717a57c2 183void wxMenuItem::Check(bool check)
2bda0e17 184{
974e8d94 185 wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") );
c2dcfdef 186
717a57c2
VZ
187 if ( m_isChecked == check )
188 return;
c2dcfdef 189
717a57c2
VZ
190 long rc = CheckMenuItem(GetHMenuOf(m_parentMenu),
191 GetRealId(),
192 MF_BYCOMMAND |
193 (check ? MF_CHECKED : MF_UNCHECKED));
c2dcfdef 194
717a57c2
VZ
195 if ( rc == -1 ) {
196 wxLogLastError("CheckMenuItem");
c2dcfdef 197 }
717a57c2
VZ
198
199 wxMenuItemBase::Check(check);
c2dcfdef
VZ
200}
201
974e8d94 202void wxMenuItem::SetText(const wxString& text)
c2dcfdef
VZ
203{
204 // don't do anything if label didn't change
974e8d94 205 if ( m_text == text )
c2dcfdef
VZ
206 return;
207
974e8d94
VZ
208 wxMenuItemBase::SetText(text);
209 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
c2dcfdef 210
974e8d94 211 HMENU hMenu = GetHMenuOf(m_parentMenu);
717a57c2
VZ
212 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
213
214#if wxUSE_ACCEL
215 m_parentMenu->UpdateAccel(this);
216#endif // wxUSE_ACCEL
2bda0e17 217
c2dcfdef
VZ
218 UINT id = GetRealId();
219 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
220 if ( flagsOld == 0xFFFFFFFF )
221 {
222 wxLogLastError("GetMenuState");
223 }
224 else
225 {
226 if ( IsSubMenu() )
227 {
228 // high byte contains the number of items in a submenu for submenus
229 flagsOld &= 0xFF;
230 flagsOld |= MF_POPUP;
231 }
232
837e5743 233 LPCTSTR data;
974e8d94 234
c2dcfdef
VZ
235#if wxUSE_OWNER_DRAWN
236 if ( IsOwnerDrawn() )
237 {
238 flagsOld |= MF_OWNERDRAW;
837e5743 239 data = (LPCTSTR)this;
c2dcfdef
VZ
240 }
241 else
242#endif //owner drawn
243 {
244 flagsOld |= MF_STRING;
974e8d94 245 data = (char*) text.c_str();
c2dcfdef
VZ
246 }
247
248 if ( ::ModifyMenu(hMenu, id,
249 MF_BYCOMMAND | flagsOld,
a17e237f 250 id, data) == (int)0xFFFFFFFF )
c2dcfdef 251 {
223d09f6 252 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef
VZ
253 }
254 }
255}
2bda0e17 256
974e8d94
VZ
257void wxMenuItem::SetCheckable(bool checkable)
258{
259 wxMenuItemBase::SetCheckable(checkable);
260 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
261}
262
263// ----------------------------------------------------------------------------
264// wxMenuItemBase
265// ----------------------------------------------------------------------------
266
267wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
268 int id,
269 const wxString& name,
270 const wxString& help,
271 bool isCheckable,
272 wxMenu *subMenu)
273{
274 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
275}