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