]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
fixed an over-optimisation
[wxWidgets.git] / src / msw / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
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
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "menuitem.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
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"
37 #endif
38
39 #include "wx/ownerdrw.h"
40 #include "wx/menuitem.h"
41 #include "wx/log.h"
42
43 #include <windows.h>
44
45 #ifdef GetClassInfo
46 #undef GetClassInfo
47 #endif
48
49 #ifdef GetClassName
50 #undef GetClassName
51 #endif
52
53 // ---------------------------------------------------------------------------
54 // convenience macro
55 // ---------------------------------------------------------------------------
56
57 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
58
59 // ============================================================================
60 // implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // dynamic classes implementation
65 // ----------------------------------------------------------------------------
66
67 #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
68 #if wxUSE_OWNER_DRAWN
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
83 wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id,
84 const wxString& strName, const wxString& strHelp,
85 bool bCheckable,
86 wxMenu *pSubMenu) :
87 #if wxUSE_OWNER_DRAWN
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 {
95 wxASSERT_MSG( pParentMenu != NULL, "a menu item should have a parent" );
96
97 #if wxUSE_OWNER_DRAWN
98 // set default menu colors
99 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
100
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
110 m_pParentMenu = pParentMenu;
111 m_pSubMenu = pSubMenu;
112 m_bEnabled = TRUE;
113 m_idItem = id;
114 }
115
116 wxMenuItem::~wxMenuItem()
117 {
118 }
119
120 // misc
121 // ----
122
123 // return the id for calling Win32 API functions
124 int wxMenuItem::GetRealId() const
125 {
126 return m_pSubMenu ? (int)m_pSubMenu->GetHMenu() : GetId();
127 }
128
129 // delete the sub menu
130 // -------------------
131 void wxMenuItem::DeleteSubMenu()
132 {
133 delete m_pSubMenu;
134 m_pSubMenu = NULL;
135 }
136
137 // change item state
138 // -----------------
139
140 void wxMenuItem::Enable(bool bDoEnable)
141 {
142 if ( m_bEnabled != bDoEnable ) {
143 long rc = EnableMenuItem(GetHMenuOf(m_pParentMenu),
144 GetRealId(),
145 MF_BYCOMMAND |
146 (bDoEnable ? MF_ENABLED : MF_GRAYED));
147
148 if ( rc == -1 ) {
149 wxLogLastError("EnableMenuItem");
150 }
151
152 m_bEnabled = bDoEnable;
153 }
154 }
155
156 void wxMenuItem::Check(bool bDoCheck)
157 {
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
174 void 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);
183
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 }
221