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