]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menuitem.cpp
added wxEXEC_NOHIDE flag, modified wxExecute() to take a combination of bit flags...
[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
621b3e21 75IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
2bda0e17
KB
76
77// ----------------------------------------------------------------------------
78// wxMenuItem
79// ----------------------------------------------------------------------------
80
81// ctor & dtor
82// -----------
83
974e8d94
VZ
84wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
85 int id,
86 const wxString& text,
87 const wxString& strHelp,
d65c269b 88 wxItemKind kind,
90002c49 89 wxMenu *pSubMenu)
d65c269b 90 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
47d67540 91#if wxUSE_OWNER_DRAWN
546bfbea 92 , wxOwnerDrawn(GetLabelFromText(text), kind == wxITEM_CHECK)
974e8d94 93#endif // owner drawn
2bda0e17 94{
223d09f6 95 wxASSERT_MSG( pParentMenu != NULL, wxT("a menu item should have a parent") );
2bda0e17 96
0472ece7
VZ
97 m_startRadioGroup =
98 m_endRadioGroup = -1;
99
47d67540 100#if wxUSE_OWNER_DRAWN
2bda0e17 101 // set default menu colors
a756f210 102 #define SYS_COLOR(c) (wxSystemSettings::GetColour(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 114#endif // wxUSE_OWNER_DRAWN
2bda0e17
KB
115}
116
c2dcfdef 117wxMenuItem::~wxMenuItem()
2bda0e17
KB
118{
119}
120
121// misc
122// ----
123
c2dcfdef
VZ
124// return the id for calling Win32 API functions
125int wxMenuItem::GetRealId() const
126{
974e8d94 127 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
c2dcfdef
VZ
128}
129
3dfac970
VZ
130// get item state
131// --------------
132
a8cfd0cb 133bool wxMenuItem::IsChecked() const
3dfac970 134{
a8cfd0cb 135 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
3dfac970 136
4aee367e 137 return (flag & MF_CHECKED) != 0;
3dfac970
VZ
138}
139
3b59cdbf
VZ
140/* static */
141wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
717a57c2 142{
4a3563c5 143 return wxStripMenuCodes(text);
717a57c2
VZ
144}
145
2bda0e17
KB
146// change item state
147// -----------------
148
717a57c2 149void wxMenuItem::Enable(bool enable)
2bda0e17 150{
717a57c2
VZ
151 if ( m_isEnabled == enable )
152 return;
153
154 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
155 GetRealId(),
156 MF_BYCOMMAND |
157 (enable ? MF_ENABLED : MF_GRAYED));
c2dcfdef 158
717a57c2 159 if ( rc == -1 ) {
f6bcfd97 160 wxLogLastError(wxT("EnableMenuItem"));
c2dcfdef 161 }
717a57c2
VZ
162
163 wxMenuItemBase::Enable(enable);
2bda0e17
KB
164}
165
717a57c2 166void wxMenuItem::Check(bool check)
2bda0e17 167{
d65c269b 168 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
c2dcfdef 169
717a57c2
VZ
170 if ( m_isChecked == check )
171 return;
c2dcfdef 172
0472ece7
VZ
173 int flags = check ? MF_CHECKED : MF_UNCHECKED;
174 HMENU hmenu = GetHMenuOf(m_parentMenu);
175
546bfbea 176 if ( GetKind() == wxITEM_RADIO )
0472ece7
VZ
177 {
178 // it doesn't make sense to uncheck a radio item - what would this do?
179 if ( !check )
180 return;
181
182 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
183 int pos = items.IndexOf(this);
184 wxCHECK_RET( pos != wxNOT_FOUND,
185 _T("menuitem not found in the menu items list?") );
186
187#ifdef __WIN32__
188 if ( !::CheckMenuRadioItem(hmenu,
189 m_startRadioGroup, // first group item
190 m_endRadioGroup, // last one
191 pos, // the one to check
192 MF_BYPOSITION | flags) )
193 {
194 wxLogLastError(_T("CheckMenuRadioItem"));
195 }
196#endif // __WIN32__
197
198 // also uncheck all the other items in this radio group
199 wxMenuItemList::Node *node = items.Item(m_startRadioGroup);
200 for ( int n = m_startRadioGroup; n <= m_endRadioGroup && node; n++ )
201 {
202 if ( n != pos )
203 {
204 node->GetData()->m_isChecked = FALSE;
205 }
206
207 // we also have to do it in the menu for Win16 (under Win32
208 // CheckMenuRadioItem() does it for us)
209#ifndef __WIN32__
210 ::CheckMenuItem(hmenu, n, n == pos ? MF_CHECKED : MF_UNCHECKED);
211#endif // Win16
212
213 node = node->GetNext();
214 }
215 }
216 else // check item
217 {
218 if ( ::CheckMenuItem(hmenu,
219 GetRealId(),
220 MF_BYCOMMAND | flags) == -1 )
221 {
222 wxLogLastError(wxT("CheckMenuItem"));
223 }
c2dcfdef 224 }
717a57c2
VZ
225
226 wxMenuItemBase::Check(check);
c2dcfdef
VZ
227}
228
974e8d94 229void wxMenuItem::SetText(const wxString& text)
c2dcfdef
VZ
230{
231 // don't do anything if label didn't change
974e8d94 232 if ( m_text == text )
c2dcfdef
VZ
233 return;
234
974e8d94
VZ
235 wxMenuItemBase::SetText(text);
236 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
c2dcfdef 237
974e8d94 238 HMENU hMenu = GetHMenuOf(m_parentMenu);
717a57c2
VZ
239 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
240
241#if wxUSE_ACCEL
242 m_parentMenu->UpdateAccel(this);
243#endif // wxUSE_ACCEL
2bda0e17 244
c2dcfdef
VZ
245 UINT id = GetRealId();
246 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
247 if ( flagsOld == 0xFFFFFFFF )
248 {
f6bcfd97 249 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
250 }
251 else
252 {
253 if ( IsSubMenu() )
254 {
255 // high byte contains the number of items in a submenu for submenus
256 flagsOld &= 0xFF;
257 flagsOld |= MF_POPUP;
258 }
259
837e5743 260 LPCTSTR data;
974e8d94 261
c2dcfdef
VZ
262#if wxUSE_OWNER_DRAWN
263 if ( IsOwnerDrawn() )
264 {
265 flagsOld |= MF_OWNERDRAW;
837e5743 266 data = (LPCTSTR)this;
c2dcfdef
VZ
267 }
268 else
269#endif //owner drawn
270 {
271 flagsOld |= MF_STRING;
f5166ed4 272 data = (wxChar*) text.c_str();
c2dcfdef
VZ
273 }
274
275 if ( ::ModifyMenu(hMenu, id,
276 MF_BYCOMMAND | flagsOld,
a17e237f 277 id, data) == (int)0xFFFFFFFF )
c2dcfdef 278 {
223d09f6 279 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef
VZ
280 }
281 }
282}
2bda0e17 283
974e8d94
VZ
284void wxMenuItem::SetCheckable(bool checkable)
285{
286 wxMenuItemBase::SetCheckable(checkable);
287 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
288}
289
290// ----------------------------------------------------------------------------
291// wxMenuItemBase
292// ----------------------------------------------------------------------------
293
294wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
295 int id,
296 const wxString& name,
297 const wxString& help,
d65c269b 298 wxItemKind kind,
974e8d94
VZ
299 wxMenu *subMenu)
300{
d65c269b 301 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 302}
1e6feb95
VZ
303
304#endif // wxUSE_MENUS