wxMenuItem::IsChecked() corrected
[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/font.h"
33 #include "wx/bitmap.h"
34 #include "wx/settings.h"
35 #include "wx/font.h"
36 #include "wx/window.h"
37 #include "wx/accel.h"
38 #include "wx/menu.h"
39 #include "wx/string.h"
40 #endif
41
42 #include "wx/menuitem.h"
43 #include "wx/log.h"
44
45 #if wxUSE_ACCEL
46 #include "wx/accel.h"
47 #endif // wxUSE_ACCEL
48
49 #include "wx/msw/private.h"
50
51 // ---------------------------------------------------------------------------
52 // macro
53 // ---------------------------------------------------------------------------
54
55 // hide the ugly cast
56 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
57
58 // conditional compilation
59 #if wxUSE_OWNER_DRAWN
60 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
61 #else // !wxUSE_OWNER_DRAWN
62 #define OWNER_DRAWN_ONLY( code )
63 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // dynamic classes implementation
71 // ----------------------------------------------------------------------------
72
73 #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
74 #if wxUSE_OWNER_DRAWN
75 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem, wxMenuItemBase, wxOwnerDrawn)
76 #else //!USE_OWNER_DRAWN
77 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
78 #endif //USE_OWNER_DRAWN
79 #endif //USE_SHARED_LIBRARY
80
81 // ----------------------------------------------------------------------------
82 // wxMenuItem
83 // ----------------------------------------------------------------------------
84
85 // ctor & dtor
86 // -----------
87
88 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
89 int id,
90 const wxString& text,
91 const wxString& strHelp,
92 bool bCheckable,
93 wxMenu *pSubMenu) :
94 #if wxUSE_OWNER_DRAWN
95 wxOwnerDrawn(text, bCheckable)
96 #endif // owner drawn
97 {
98 wxASSERT_MSG( pParentMenu != NULL, wxT("a menu item should have a parent") );
99
100 #if wxUSE_OWNER_DRAWN
101 // set default menu colors
102 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
103
104 SetTextColour(SYS_COLOR(MENUTEXT));
105 SetBackgroundColour(SYS_COLOR(MENU));
106
107 // we don't want normal items be owner-drawn
108 ResetOwnerDrawn();
109
110 #undef SYS_COLOR
111 #endif // wxUSE_OWNER_DRAWN
112
113 m_parentMenu = pParentMenu;
114 m_subMenu = pSubMenu;
115 m_isEnabled = TRUE;
116 m_isChecked = FALSE;
117 m_id = id;
118 m_text = text;
119 m_isCheckable = bCheckable;
120 m_help = strHelp;
121 }
122
123 wxMenuItem::~wxMenuItem()
124 {
125 }
126
127 // misc
128 // ----
129
130 // return the id for calling Win32 API functions
131 int wxMenuItem::GetRealId() const
132 {
133 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
134 }
135
136 // get item state
137 // --------------
138
139 bool wxMenuItem::IsChecked() const
140 {
141 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
142
143 return (flag & MF_CHECKED) != 0;
144 }
145
146 wxString wxMenuItem::GetLabel() const
147 {
148 return wxStripMenuCodes(m_text);
149 }
150
151 // accelerators
152 // ------------
153
154 #if wxUSE_ACCEL
155
156 wxAcceleratorEntry *wxMenuItem::GetAccel() const
157 {
158 return wxGetAccelFromString(GetText());
159 }
160
161 #endif // wxUSE_ACCEL
162
163 // change item state
164 // -----------------
165
166 void wxMenuItem::Enable(bool enable)
167 {
168 if ( m_isEnabled == enable )
169 return;
170
171 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
172 GetRealId(),
173 MF_BYCOMMAND |
174 (enable ? MF_ENABLED : MF_GRAYED));
175
176 if ( rc == -1 ) {
177 wxLogLastError("EnableMenuItem");
178 }
179
180 wxMenuItemBase::Enable(enable);
181 }
182
183 void wxMenuItem::Check(bool check)
184 {
185 wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") );
186
187 if ( m_isChecked == check )
188 return;
189
190 long rc = CheckMenuItem(GetHMenuOf(m_parentMenu),
191 GetRealId(),
192 MF_BYCOMMAND |
193 (check ? MF_CHECKED : MF_UNCHECKED));
194
195 if ( rc == -1 ) {
196 wxLogLastError("CheckMenuItem");
197 }
198
199 wxMenuItemBase::Check(check);
200 }
201
202 void wxMenuItem::SetText(const wxString& text)
203 {
204 // don't do anything if label didn't change
205 if ( m_text == text )
206 return;
207
208 wxMenuItemBase::SetText(text);
209 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
210
211 HMENU hMenu = GetHMenuOf(m_parentMenu);
212 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
213
214 #if wxUSE_ACCEL
215 m_parentMenu->UpdateAccel(this);
216 #endif // wxUSE_ACCEL
217
218 UINT id = GetRealId();
219 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
220 if ( flagsOld == 0xFFFFFFFF )
221 {
222 wxLogLastError("GetMenuState");
223 }
224 else
225 {
226 if ( IsSubMenu() )
227 {
228 // high byte contains the number of items in a submenu for submenus
229 flagsOld &= 0xFF;
230 flagsOld |= MF_POPUP;
231 }
232
233 LPCTSTR data;
234
235 #if wxUSE_OWNER_DRAWN
236 if ( IsOwnerDrawn() )
237 {
238 flagsOld |= MF_OWNERDRAW;
239 data = (LPCTSTR)this;
240 }
241 else
242 #endif //owner drawn
243 {
244 flagsOld |= MF_STRING;
245 data = (char*) text.c_str();
246 }
247
248 if ( ::ModifyMenu(hMenu, id,
249 MF_BYCOMMAND | flagsOld,
250 id, data) == (int)0xFFFFFFFF )
251 {
252 wxLogLastError(wxT("ModifyMenu"));
253 }
254 }
255 }
256
257 void wxMenuItem::SetCheckable(bool checkable)
258 {
259 wxMenuItemBase::SetCheckable(checkable);
260 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
261 }
262
263 // ----------------------------------------------------------------------------
264 // wxMenuItemBase
265 // ----------------------------------------------------------------------------
266
267 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
268 int id,
269 const wxString& name,
270 const wxString& help,
271 bool isCheckable,
272 wxMenu *subMenu)
273 {
274 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
275 }