]>
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 | ||
22e90769 DW |
78 | wxMenuItem::wxMenuItem( |
79 | wxMenu* pParentMenu | |
80 | , int nId | |
81 | , const wxString& rText | |
82 | , const wxString& rStrHelp | |
83 | , bool bCheckable | |
84 | , wxMenu* pSubMenu | |
85 | ) | |
75f11ad7 | 86 | #if wxUSE_OWNER_DRAWN |
22e90769 DW |
87 | : wxOwnerDrawn( rText |
88 | ,bCheckable | |
89 | ) | |
c5fb56c0 | 90 | #endif // owner drawn |
0e320a79 | 91 | { |
22e90769 | 92 | wxASSERT_MSG(pParentMenu != NULL, wxT("a menu item should have a parent")); |
75f11ad7 DW |
93 | |
94 | #if wxUSE_OWNER_DRAWN | |
22e90769 DW |
95 | |
96 | // | |
97 | // Set default menu colors | |
98 | // | |
75f11ad7 DW |
99 | #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c)) |
100 | ||
101 | SetTextColour(SYS_COLOR(MENUTEXT)); | |
102 | SetBackgroundColour(SYS_COLOR(MENU)); | |
103 | ||
22e90769 DW |
104 | // |
105 | // We don't want normal items be owner-drawn | |
106 | // | |
75f11ad7 DW |
107 | ResetOwnerDrawn(); |
108 | ||
109 | #undef SYS_COLOR | |
c5fb56c0 DW |
110 | #endif // wxUSE_OWNER_DRAWN |
111 | ||
112 | m_parentMenu = pParentMenu; | |
113 | m_subMenu = pSubMenu; | |
114 | m_isEnabled = TRUE; | |
115 | m_isChecked = FALSE; | |
22e90769 DW |
116 | m_id = nId; |
117 | m_text = rText; | |
c5fb56c0 | 118 | m_isCheckable = bCheckable; |
22e90769 DW |
119 | m_help = rStrHelp; |
120 | } // end of wxMenuItem::wxMenuItem | |
0e320a79 | 121 | |
75f11ad7 | 122 | wxMenuItem::~wxMenuItem() |
0e320a79 | 123 | { |
22e90769 | 124 | } // end of wxMenuItem::~wxMenuItem |
0e320a79 | 125 | |
22e90769 DW |
126 | // |
127 | // Misc | |
0e320a79 DW |
128 | // ---- |
129 | ||
22e90769 DW |
130 | // |
131 | // Return the id for calling Win32 API functions | |
132 | // | |
75f11ad7 DW |
133 | int wxMenuItem::GetRealId() const |
134 | { | |
c5fb56c0 | 135 | return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId(); |
22e90769 | 136 | } // end of wxMenuItem::GetRealId |
c5fb56c0 | 137 | |
22e90769 DW |
138 | // |
139 | // Get item state | |
c5fb56c0 | 140 | // -------------- |
c5fb56c0 DW |
141 | bool wxMenuItem::IsChecked() const |
142 | { | |
22e90769 DW |
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 | ) | |
c5fb56c0 | 155 | { |
22e90769 | 156 | return wxStripMenuCodes(rText); |
75f11ad7 DW |
157 | } |
158 | ||
c5fb56c0 DW |
159 | // accelerators |
160 | // ------------ | |
161 | ||
162 | #if wxUSE_ACCEL | |
163 | ||
164 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
0e320a79 | 165 | { |
c5fb56c0 | 166 | return wxGetAccelFromString(GetText()); |
0e320a79 DW |
167 | } |
168 | ||
c5fb56c0 DW |
169 | #endif // wxUSE_ACCEL |
170 | ||
0e320a79 DW |
171 | // change item state |
172 | // ----------------- | |
173 | ||
22e90769 DW |
174 | void wxMenuItem::Enable( |
175 | bool bEnable | |
176 | ) | |
0e320a79 | 177 | { |
22e90769 | 178 | bool bOk; |
75f11ad7 | 179 | |
22e90769 DW |
180 | if (m_isEnabled == bEnable) |
181 | return; | |
182 | if (bEnable) | |
914589c2 DW |
183 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
184 | ,MM_SETITEMATTR | |
185 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
186 | ,MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED) | |
187 | ); | |
22e90769 | 188 | else |
914589c2 DW |
189 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
190 | ,MM_SETITEMATTR | |
191 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
192 | ,MPFROM2SHORT(MIA_DISABLED, FALSE) | |
193 | ); | |
22e90769 DW |
194 | if (!bOk) |
195 | { | |
c5fb56c0 | 196 | wxLogLastError("EnableMenuItem"); |
0e320a79 | 197 | } |
22e90769 DW |
198 | wxMenuItemBase::Enable(bEnable); |
199 | } // end of wxMenuItem::Enable | |
0e320a79 | 200 | |
22e90769 DW |
201 | void wxMenuItem::Check( |
202 | bool bCheck | |
203 | ) | |
0e320a79 | 204 | { |
22e90769 | 205 | bool bOk; |
75f11ad7 | 206 | |
22e90769 DW |
207 | wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") ); |
208 | if (m_isChecked == bCheck) | |
c5fb56c0 | 209 | return; |
22e90769 | 210 | if (bCheck) |
914589c2 DW |
211 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
212 | ,MM_SETITEMATTR | |
213 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
214 | ,MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED) | |
215 | ); | |
22e90769 | 216 | else |
914589c2 DW |
217 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
218 | ,MM_SETITEMATTR | |
219 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
220 | ,MPFROM2SHORT(MIA_CHECKED, FALSE) | |
221 | ); | |
22e90769 DW |
222 | if (!bOk) |
223 | { | |
224 | wxLogLastError("EnableMenuItem"); | |
75f11ad7 | 225 | } |
22e90769 DW |
226 | wxMenuItemBase::Check(bCheck); |
227 | } // end of wxMenuItem::Check | |
75f11ad7 | 228 | |
22e90769 DW |
229 | void wxMenuItem::SetText( |
230 | const wxString& rText | |
231 | ) | |
75f11ad7 | 232 | { |
22e90769 DW |
233 | // |
234 | // Don't do anything if label didn't change | |
235 | // | |
236 | if (m_text == rText) | |
75f11ad7 DW |
237 | return; |
238 | ||
22e90769 DW |
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")); | |
75f11ad7 | 245 | |
c5fb56c0 DW |
246 | #if wxUSE_ACCEL |
247 | m_parentMenu->UpdateAccel(this); | |
248 | #endif // wxUSE_ACCEL | |
75f11ad7 | 249 | |
22e90769 DW |
250 | USHORT uId = GetRealId(); |
251 | MENUITEM vItem; | |
252 | USHORT uFlagsOld; | |
914589c2 | 253 | |
22e90769 DW |
254 | if (!::WinSendMsg( hMenu |
255 | ,MM_QUERYITEM | |
256 | ,MPFROM2SHORT(uId, TRUE) | |
257 | ,(MPARAM)&vItem | |
258 | )) | |
75f11ad7 DW |
259 | { |
260 | wxLogLastError("GetMenuState"); | |
261 | } | |
262 | else | |
263 | { | |
22e90769 DW |
264 | uFlagsOld = vItem.afStyle; |
265 | if (IsSubMenu()) | |
75f11ad7 | 266 | { |
22e90769 | 267 | uFlagsOld |= MIS_SUBMENU; |
75f11ad7 DW |
268 | } |
269 | ||
22e90769 | 270 | BYTE* pData; |
c5fb56c0 | 271 | |
75f11ad7 | 272 | #if wxUSE_OWNER_DRAWN |
22e90769 | 273 | if (IsOwnerDrawn()) |
75f11ad7 | 274 | { |
22e90769 DW |
275 | uFlagsOld |= MIS_OWNERDRAW; |
276 | pData = (BYTE*)this; | |
75f11ad7 DW |
277 | } |
278 | else | |
279 | #endif //owner drawn | |
280 | { | |
22e90769 DW |
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")); | |
75f11ad7 DW |
295 | } |
296 | ||
22e90769 DW |
297 | // |
298 | // Set the text | |
299 | // | |
300 | if (::WinSendMsg( hMenu | |
301 | ,MM_SETITEMTEXT | |
302 | ,MPFROMSHORT(uId) | |
303 | ,(MPARAM)pData | |
304 | )) | |
75f11ad7 DW |
305 | { |
306 | wxLogLastError(wxT("ModifyMenu")); | |
307 | } | |
308 | } | |
22e90769 | 309 | } // end of wxMenuItem::SetText |
0e320a79 | 310 | |
22e90769 DW |
311 | void wxMenuItem::SetCheckable( |
312 | bool bCheckable | |
313 | ) | |
c5fb56c0 | 314 | { |
22e90769 DW |
315 | wxMenuItemBase::SetCheckable(bCheckable); |
316 | OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable)); | |
317 | } // end of wxMenuItem::SetCheckable | |
c5fb56c0 DW |
318 | |
319 | // ---------------------------------------------------------------------------- | |
320 | // wxMenuItemBase | |
321 | // ---------------------------------------------------------------------------- | |
322 | ||
22e90769 DW |
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 | ) | |
c5fb56c0 | 331 | { |
22e90769 DW |
332 | return new wxMenuItem( pParentMenu |
333 | ,nId | |
334 | ,rName | |
335 | ,rHelp | |
336 | ,bIsCheckable | |
337 | ,pSubMenu | |
338 | ); | |
339 | } // end of wxMenuItemBase::New | |
340 |