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