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