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