]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menuitem.cpp | |
3 | // Purpose: wxMenuItem implementation | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/10/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // headers & declarations | |
14 | // ============================================================================ | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma implementation "menuitem.h" | |
18 | #endif | |
19 | ||
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 | ||
34 | #include "wx/menuitem.h" | |
35 | #include "wx/log.h" | |
36 | ||
37 | #if wxUSE_ACCEL | |
38 | #include "wx/accel.h" | |
39 | #endif // wxUSE_ACCEL | |
40 | ||
41 | #include "wx/os2/private.h" | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | // macro | |
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | // hide the ugly cast | |
48 | #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu()) | |
49 | ||
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 | ||
57 | // ============================================================================ | |
58 | // implementation | |
59 | // ============================================================================ | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // dynamic classes implementation | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
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 | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // wxMenuItem | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | // ctor & dtor | |
76 | // ----------- | |
77 | ||
78 | wxMenuItem::wxMenuItem( | |
79 | wxMenu* pParentMenu | |
80 | , int nId | |
81 | , const wxString& rText | |
82 | , const wxString& rStrHelp | |
83 | , bool bCheckable | |
84 | , wxMenu* pSubMenu | |
85 | ) | |
86 | #if wxUSE_OWNER_DRAWN | |
87 | : wxOwnerDrawn( rText | |
88 | ,bCheckable | |
89 | ) | |
90 | #endif // owner drawn | |
91 | { | |
92 | wxASSERT_MSG(pParentMenu != NULL, wxT("a menu item should have a parent")); | |
93 | ||
94 | #if wxUSE_OWNER_DRAWN | |
95 | ||
96 | // | |
97 | // Set default menu colors | |
98 | // | |
99 | #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c)) | |
100 | ||
101 | SetTextColour(SYS_COLOR(MENUTEXT)); | |
102 | SetBackgroundColour(SYS_COLOR(MENU)); | |
103 | ||
104 | // | |
105 | // We don't want normal items be owner-drawn | |
106 | // | |
107 | ResetOwnerDrawn(); | |
108 | ||
109 | #undef SYS_COLOR | |
110 | #endif // wxUSE_OWNER_DRAWN | |
111 | ||
112 | m_parentMenu = pParentMenu; | |
113 | m_subMenu = pSubMenu; | |
114 | m_isEnabled = TRUE; | |
115 | m_isChecked = FALSE; | |
116 | m_id = nId; | |
117 | m_text = rText; | |
118 | m_isCheckable = bCheckable; | |
119 | m_help = rStrHelp; | |
120 | } // end of wxMenuItem::wxMenuItem | |
121 | ||
122 | wxMenuItem::~wxMenuItem() | |
123 | { | |
124 | } // end of wxMenuItem::~wxMenuItem | |
125 | ||
126 | // | |
127 | // Misc | |
128 | // ---- | |
129 | ||
130 | // | |
131 | // Return the id for calling Win32 API functions | |
132 | // | |
133 | int wxMenuItem::GetRealId() const | |
134 | { | |
135 | return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId(); | |
136 | } // end of wxMenuItem::GetRealId | |
137 | ||
138 | // | |
139 | // Get item state | |
140 | // -------------- | |
141 | bool wxMenuItem::IsChecked() const | |
142 | { | |
143 | USHORT uFlag = SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu) | |
144 | ,MM_QUERYITEMATTR | |
145 | ,MPFROM2SHORT(GetId(), TRUE) | |
146 | ,MPFROMSHORT(MIA_CHECKED) | |
147 | )); | |
148 | ||
149 | return (uFlag & MIA_CHECKED); | |
150 | } // end of wxMenuItem::IsChecked | |
151 | ||
152 | wxString wxMenuItemBase::GetLabelFromText( | |
153 | const wxString& rText | |
154 | ) | |
155 | { | |
156 | return wxStripMenuCodes(rText); | |
157 | } | |
158 | ||
159 | // accelerators | |
160 | // ------------ | |
161 | ||
162 | #if wxUSE_ACCEL | |
163 | ||
164 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
165 | { | |
166 | return wxGetAccelFromString(GetText()); | |
167 | } | |
168 | ||
169 | #endif // wxUSE_ACCEL | |
170 | ||
171 | // change item state | |
172 | // ----------------- | |
173 | ||
174 | void wxMenuItem::Enable( | |
175 | bool bEnable | |
176 | ) | |
177 | { | |
178 | bool bOk; | |
179 | ||
180 | if (m_isEnabled == bEnable) | |
181 | return; | |
182 | if (bEnable) | |
183 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) | |
184 | ,MM_SETITEMATTR | |
185 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
186 | ,MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED) | |
187 | ); | |
188 | else | |
189 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) | |
190 | ,MM_SETITEMATTR | |
191 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
192 | ,MPFROM2SHORT(MIA_DISABLED, FALSE) | |
193 | ); | |
194 | if (!bOk) | |
195 | { | |
196 | wxLogLastError("EnableMenuItem"); | |
197 | } | |
198 | wxMenuItemBase::Enable(bEnable); | |
199 | } // end of wxMenuItem::Enable | |
200 | ||
201 | void wxMenuItem::Check( | |
202 | bool bCheck | |
203 | ) | |
204 | { | |
205 | bool bOk; | |
206 | ||
207 | wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") ); | |
208 | if (m_isChecked == bCheck) | |
209 | return; | |
210 | if (bCheck) | |
211 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) | |
212 | ,MM_SETITEMATTR | |
213 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
214 | ,MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED) | |
215 | ); | |
216 | else | |
217 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) | |
218 | ,MM_SETITEMATTR | |
219 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
220 | ,MPFROM2SHORT(MIA_CHECKED, FALSE) | |
221 | ); | |
222 | if (!bOk) | |
223 | { | |
224 | wxLogLastError("EnableMenuItem"); | |
225 | } | |
226 | wxMenuItemBase::Check(bCheck); | |
227 | } // end of wxMenuItem::Check | |
228 | ||
229 | void wxMenuItem::SetText( | |
230 | const wxString& rText | |
231 | ) | |
232 | { | |
233 | // | |
234 | // Don't do anything if label didn't change | |
235 | // | |
236 | if (m_text == rText) | |
237 | return; | |
238 | ||
239 | wxMenuItemBase::SetText(rText); | |
240 | OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(rText)); | |
241 | ||
242 | HWND hMenu = GetHMenuOf(m_parentMenu); | |
243 | ||
244 | wxCHECK_RET(hMenu, wxT("menuitem without menu")); | |
245 | ||
246 | #if wxUSE_ACCEL | |
247 | m_parentMenu->UpdateAccel(this); | |
248 | #endif // wxUSE_ACCEL | |
249 | ||
250 | USHORT uId = GetRealId(); | |
251 | MENUITEM vItem; | |
252 | USHORT uFlagsOld; | |
253 | ||
254 | if (!::WinSendMsg( hMenu | |
255 | ,MM_QUERYITEM | |
256 | ,MPFROM2SHORT(uId, TRUE) | |
257 | ,(MPARAM)&vItem | |
258 | )) | |
259 | { | |
260 | wxLogLastError("GetMenuState"); | |
261 | } | |
262 | else | |
263 | { | |
264 | uFlagsOld = vItem.afStyle; | |
265 | if (IsSubMenu()) | |
266 | { | |
267 | uFlagsOld |= MIS_SUBMENU; | |
268 | } | |
269 | ||
270 | BYTE* pData; | |
271 | ||
272 | #if wxUSE_OWNER_DRAWN | |
273 | if (IsOwnerDrawn()) | |
274 | { | |
275 | uFlagsOld |= MIS_OWNERDRAW; | |
276 | pData = (BYTE*)this; | |
277 | } | |
278 | else | |
279 | #endif //owner drawn | |
280 | { | |
281 | uFlagsOld |= MIS_TEXT; | |
282 | pData = (BYTE*)rText.c_str(); | |
283 | } | |
284 | ||
285 | // | |
286 | // Set the style | |
287 | // | |
288 | if (!::WinSendMsg( hMenu | |
289 | ,MM_SETITEM | |
290 | ,MPFROM2SHORT(uId, TRUE) | |
291 | ,(MPARAM)&vItem | |
292 | )) | |
293 | { | |
294 | wxLogLastError(wxT("ModifyMenu")); | |
295 | } | |
296 | ||
297 | // | |
298 | // Set the text | |
299 | // | |
300 | if (::WinSendMsg( hMenu | |
301 | ,MM_SETITEMTEXT | |
302 | ,MPFROMSHORT(uId) | |
303 | ,(MPARAM)pData | |
304 | )) | |
305 | { | |
306 | wxLogLastError(wxT("ModifyMenu")); | |
307 | } | |
308 | } | |
309 | } // end of wxMenuItem::SetText | |
310 | ||
311 | void wxMenuItem::SetCheckable( | |
312 | bool bCheckable | |
313 | ) | |
314 | { | |
315 | wxMenuItemBase::SetCheckable(bCheckable); | |
316 | OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable)); | |
317 | } // end of wxMenuItem::SetCheckable | |
318 | ||
319 | // ---------------------------------------------------------------------------- | |
320 | // wxMenuItemBase | |
321 | // ---------------------------------------------------------------------------- | |
322 | ||
323 | wxMenuItem* wxMenuItemBase::New( | |
324 | wxMenu* pParentMenu | |
325 | , int nId | |
326 | , const wxString& rName | |
327 | , const wxString& rHelp | |
328 | , bool bIsCheckable | |
329 | , wxMenu* pSubMenu | |
330 | ) | |
331 | { | |
332 | return new wxMenuItem( pParentMenu | |
333 | ,nId | |
334 | ,rName | |
335 | ,rHelp | |
336 | ,bIsCheckable | |
337 | ,pSubMenu | |
338 | ); | |
339 | } // end of wxMenuItemBase::New | |
340 |