]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menuitem.cpp
an assert added to check that we're not doing something stupid
[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"
0c589ad0
BM
36 #include "wx/accel.h"
37 #include "wx/menu.h"
38 #include "wx/string.h"
39 #include "wx/window.h"
2bda0e17
KB
40#endif
41
42#include "wx/ownerdrw.h"
43#include "wx/menuitem.h"
6776a0b2 44#include "wx/log.h"
2bda0e17 45
42e69d6b 46#include "wx/msw/private.h"
ce3ed50d 47
c2dcfdef
VZ
48// ---------------------------------------------------------------------------
49// convenience macro
50// ---------------------------------------------------------------------------
51
52#define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
53
2bda0e17
KB
54// ============================================================================
55// implementation
56// ============================================================================
57
58// ----------------------------------------------------------------------------
59// dynamic classes implementation
60// ----------------------------------------------------------------------------
61
62#if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
47d67540 63#if wxUSE_OWNER_DRAWN
2bda0e17
KB
64 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem, wxObject, wxOwnerDrawn)
65#else //!USE_OWNER_DRAWN
66 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
67#endif //USE_OWNER_DRAWN
68
69#endif //USE_SHARED_LIBRARY
70
71// ----------------------------------------------------------------------------
72// wxMenuItem
73// ----------------------------------------------------------------------------
74
75// ctor & dtor
76// -----------
77
78wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id,
3aadbb82 79 const wxString& strName, const wxString& strHelp,
2bda0e17
KB
80 bool bCheckable,
81 wxMenu *pSubMenu) :
47d67540 82#if wxUSE_OWNER_DRAWN
2bda0e17
KB
83 wxOwnerDrawn(strName, bCheckable),
84#else //no owner drawn support
85 m_bCheckable(bCheckable),
86 m_strName(strName),
87#endif //owner drawn
88 m_strHelp(strHelp)
89{
837e5743 90 wxASSERT_MSG( pParentMenu != NULL, _T("a menu item should have a parent") );
2bda0e17 91
47d67540 92#if wxUSE_OWNER_DRAWN
2bda0e17
KB
93 // set default menu colors
94 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
c2dcfdef 95
2bda0e17
KB
96 SetTextColour(SYS_COLOR(MENUTEXT));
97 SetBackgroundColour(SYS_COLOR(MENU));
98
99 // we don't want normal items be owner-drawn
100 ResetOwnerDrawn();
101
102 #undef SYS_COLOR
103#endif
104
c2dcfdef
VZ
105 m_pParentMenu = pParentMenu;
106 m_pSubMenu = pSubMenu;
107 m_bEnabled = TRUE;
3a5ffa81 108 m_bChecked = FALSE;
c2dcfdef 109 m_idItem = id;
2bda0e17
KB
110}
111
c2dcfdef 112wxMenuItem::~wxMenuItem()
2bda0e17
KB
113{
114}
115
116// misc
117// ----
118
c2dcfdef
VZ
119// return the id for calling Win32 API functions
120int wxMenuItem::GetRealId() const
121{
122 return m_pSubMenu ? (int)m_pSubMenu->GetHMenu() : GetId();
123}
124
2bda0e17 125// delete the sub menu
c2dcfdef 126// -------------------
2bda0e17
KB
127void wxMenuItem::DeleteSubMenu()
128{
c2dcfdef
VZ
129 delete m_pSubMenu;
130 m_pSubMenu = NULL;
2bda0e17
KB
131}
132
133// change item state
134// -----------------
135
136void wxMenuItem::Enable(bool bDoEnable)
137{
c2dcfdef
VZ
138 if ( m_bEnabled != bDoEnable ) {
139 long rc = EnableMenuItem(GetHMenuOf(m_pParentMenu),
140 GetRealId(),
141 MF_BYCOMMAND |
142 (bDoEnable ? MF_ENABLED : MF_GRAYED));
2bda0e17 143
c2dcfdef
VZ
144 if ( rc == -1 ) {
145 wxLogLastError("EnableMenuItem");
146 }
147
148 m_bEnabled = bDoEnable;
149 }
2bda0e17
KB
150}
151
152void wxMenuItem::Check(bool bDoCheck)
153{
837e5743 154 wxCHECK_RET( IsCheckable(), _T("only checkable items may be checked") );
c2dcfdef
VZ
155
156 if ( m_bChecked != bDoCheck ) {
157 long rc = CheckMenuItem(GetHMenuOf(m_pParentMenu),
158 GetId(),
159 MF_BYCOMMAND |
160 (bDoCheck ? MF_CHECKED : MF_UNCHECKED));
161
162 if ( rc == -1 ) {
163 wxLogLastError("CheckMenuItem");
164 }
165
166 m_bChecked = bDoCheck;
167 }
168}
169
170void wxMenuItem::SetName(const wxString& strName)
171{
172 // don't do anything if label didn't change
173 if ( m_strName == strName )
174 return;
175
176 m_strName = strName;
177
178 HMENU hMenu = GetHMenuOf(m_pParentMenu);
2bda0e17 179
c2dcfdef
VZ
180 UINT id = GetRealId();
181 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
182 if ( flagsOld == 0xFFFFFFFF )
183 {
184 wxLogLastError("GetMenuState");
185 }
186 else
187 {
188 if ( IsSubMenu() )
189 {
190 // high byte contains the number of items in a submenu for submenus
191 flagsOld &= 0xFF;
192 flagsOld |= MF_POPUP;
193 }
194
837e5743 195 LPCTSTR data;
c2dcfdef
VZ
196#if wxUSE_OWNER_DRAWN
197 if ( IsOwnerDrawn() )
198 {
199 flagsOld |= MF_OWNERDRAW;
837e5743 200 data = (LPCTSTR)this;
c2dcfdef
VZ
201 }
202 else
203#endif //owner drawn
204 {
205 flagsOld |= MF_STRING;
206 data = strName;
207 }
208
209 if ( ::ModifyMenu(hMenu, id,
210 MF_BYCOMMAND | flagsOld,
211 id, data) == 0xFFFFFFFF )
212 {
837e5743 213 wxLogLastError(_T("ModifyMenu"));
c2dcfdef
VZ
214 }
215 }
216}
2bda0e17 217