]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menuitem.cpp
Added expose event compression.
[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,
2bda0e17 88 bool bCheckable,
90002c49 89 wxMenu *pSubMenu)
47d67540 90#if wxUSE_OWNER_DRAWN
6d5b2a57 91 : wxOwnerDrawn(GetLabelFromText(text), bCheckable)
974e8d94 92#endif // owner drawn
2bda0e17 93{
223d09f6 94 wxASSERT_MSG( pParentMenu != NULL, wxT("a menu item should have a parent") );
2bda0e17 95
47d67540 96#if wxUSE_OWNER_DRAWN
2bda0e17 97 // set default menu colors
a756f210 98 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
c2dcfdef 99
2bda0e17
KB
100 SetTextColour(SYS_COLOR(MENUTEXT));
101 SetBackgroundColour(SYS_COLOR(MENU));
102
6d5b2a57
VZ
103 #undef SYS_COLOR
104
2bda0e17
KB
105 // we don't want normal items be owner-drawn
106 ResetOwnerDrawn();
107
6d5b2a57
VZ
108 // tell the owner drawing code to to show the accel string as well
109 SetAccelString(text.AfterFirst(_T('\t')));
974e8d94
VZ
110#endif // wxUSE_OWNER_DRAWN
111
112 m_parentMenu = pParentMenu;
113 m_subMenu = pSubMenu;
114 m_isEnabled = TRUE;
115 m_isChecked = FALSE;
116 m_id = id;
117 m_text = text;
118 m_isCheckable = bCheckable;
119 m_help = strHelp;
2bda0e17
KB
120}
121
c2dcfdef 122wxMenuItem::~wxMenuItem()
2bda0e17
KB
123{
124}
125
126// misc
127// ----
128
c2dcfdef
VZ
129// return the id for calling Win32 API functions
130int wxMenuItem::GetRealId() const
131{
974e8d94 132 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
c2dcfdef
VZ
133}
134
3dfac970
VZ
135// get item state
136// --------------
137
a8cfd0cb 138bool wxMenuItem::IsChecked() const
3dfac970 139{
a8cfd0cb 140 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
3dfac970 141
4aee367e 142 return (flag & MF_CHECKED) != 0;
3dfac970
VZ
143}
144
3b59cdbf
VZ
145/* static */
146wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
717a57c2 147{
4a3563c5 148 return wxStripMenuCodes(text);
717a57c2
VZ
149}
150
2bda0e17
KB
151// change item state
152// -----------------
153
717a57c2 154void wxMenuItem::Enable(bool enable)
2bda0e17 155{
717a57c2
VZ
156 if ( m_isEnabled == enable )
157 return;
158
159 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
160 GetRealId(),
161 MF_BYCOMMAND |
162 (enable ? MF_ENABLED : MF_GRAYED));
c2dcfdef 163
717a57c2 164 if ( rc == -1 ) {
f6bcfd97 165 wxLogLastError(wxT("EnableMenuItem"));
c2dcfdef 166 }
717a57c2
VZ
167
168 wxMenuItemBase::Enable(enable);
2bda0e17
KB
169}
170
717a57c2 171void wxMenuItem::Check(bool check)
2bda0e17 172{
974e8d94 173 wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") );
c2dcfdef 174
717a57c2
VZ
175 if ( m_isChecked == check )
176 return;
c2dcfdef 177
717a57c2
VZ
178 long rc = CheckMenuItem(GetHMenuOf(m_parentMenu),
179 GetRealId(),
180 MF_BYCOMMAND |
181 (check ? MF_CHECKED : MF_UNCHECKED));
c2dcfdef 182
717a57c2 183 if ( rc == -1 ) {
f6bcfd97 184 wxLogLastError(wxT("CheckMenuItem"));
c2dcfdef 185 }
717a57c2
VZ
186
187 wxMenuItemBase::Check(check);
c2dcfdef
VZ
188}
189
974e8d94 190void wxMenuItem::SetText(const wxString& text)
c2dcfdef
VZ
191{
192 // don't do anything if label didn't change
974e8d94 193 if ( m_text == text )
c2dcfdef
VZ
194 return;
195
974e8d94
VZ
196 wxMenuItemBase::SetText(text);
197 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
c2dcfdef 198
974e8d94 199 HMENU hMenu = GetHMenuOf(m_parentMenu);
717a57c2
VZ
200 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
201
202#if wxUSE_ACCEL
203 m_parentMenu->UpdateAccel(this);
204#endif // wxUSE_ACCEL
2bda0e17 205
c2dcfdef
VZ
206 UINT id = GetRealId();
207 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
208 if ( flagsOld == 0xFFFFFFFF )
209 {
f6bcfd97 210 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
211 }
212 else
213 {
214 if ( IsSubMenu() )
215 {
216 // high byte contains the number of items in a submenu for submenus
217 flagsOld &= 0xFF;
218 flagsOld |= MF_POPUP;
219 }
220
837e5743 221 LPCTSTR data;
974e8d94 222
c2dcfdef
VZ
223#if wxUSE_OWNER_DRAWN
224 if ( IsOwnerDrawn() )
225 {
226 flagsOld |= MF_OWNERDRAW;
837e5743 227 data = (LPCTSTR)this;
c2dcfdef
VZ
228 }
229 else
230#endif //owner drawn
231 {
232 flagsOld |= MF_STRING;
f5166ed4 233 data = (wxChar*) text.c_str();
c2dcfdef
VZ
234 }
235
236 if ( ::ModifyMenu(hMenu, id,
237 MF_BYCOMMAND | flagsOld,
a17e237f 238 id, data) == (int)0xFFFFFFFF )
c2dcfdef 239 {
223d09f6 240 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef
VZ
241 }
242 }
243}
2bda0e17 244
974e8d94
VZ
245void wxMenuItem::SetCheckable(bool checkable)
246{
247 wxMenuItemBase::SetCheckable(checkable);
248 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
249}
250
251// ----------------------------------------------------------------------------
252// wxMenuItemBase
253// ----------------------------------------------------------------------------
254
255wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
256 int id,
257 const wxString& name,
258 const wxString& help,
259 bool isCheckable,
260 wxMenu *subMenu)
261{
262 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
263}
1e6feb95
VZ
264
265#endif // wxUSE_MENUS