]>
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 | ||
2b33b728 SN |
57 | // ---------------------------------------------------------------------------- |
58 | // static function for translating menu labels | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | static wxString TextToLabel(const wxString& rTitle) | |
62 | { | |
63 | wxString Title; | |
64 | const wxChar *pc; | |
65 | for (pc = rTitle; *pc != wxT('\0'); pc++ ) | |
66 | { | |
67 | if (*pc == wxT('&') ) | |
68 | { | |
69 | if (*(pc+1) == wxT('&')) | |
70 | { | |
71 | pc++; | |
72 | Title << wxT('&'); | |
73 | } | |
74 | else | |
75 | Title << wxT('~'); | |
76 | } | |
77 | // else if (*pc == wxT('/')) | |
78 | // { | |
79 | // Title << wxT('\\'); | |
80 | // } | |
81 | else | |
82 | { | |
83 | if ( *pc == wxT('~') ) | |
84 | { | |
85 | // tildes must be doubled to prevent them from being | |
86 | // interpreted as accelerator character prefix by PM ??? | |
87 | Title << *pc; | |
88 | } | |
89 | Title << *pc; | |
90 | } | |
91 | } | |
92 | return Title; | |
93 | } | |
94 | ||
0e320a79 DW |
95 | // ============================================================================ |
96 | // implementation | |
97 | // ============================================================================ | |
98 | ||
99 | // ---------------------------------------------------------------------------- | |
100 | // dynamic classes implementation | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
c5fb56c0 DW |
103 | #if wxUSE_OWNER_DRAWN |
104 | IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem, wxMenuItemBase, wxOwnerDrawn) | |
105 | #else //!USE_OWNER_DRAWN | |
106 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase) | |
107 | #endif //USE_OWNER_DRAWN | |
0e320a79 DW |
108 | |
109 | // ---------------------------------------------------------------------------- | |
110 | // wxMenuItem | |
111 | // ---------------------------------------------------------------------------- | |
112 | ||
113 | // ctor & dtor | |
114 | // ----------- | |
115 | ||
22e90769 DW |
116 | wxMenuItem::wxMenuItem( |
117 | wxMenu* pParentMenu | |
118 | , int nId | |
119 | , const wxString& rText | |
120 | , const wxString& rStrHelp | |
121 | , bool bCheckable | |
122 | , wxMenu* pSubMenu | |
123 | ) | |
75f11ad7 | 124 | #if wxUSE_OWNER_DRAWN |
2b33b728 | 125 | : wxOwnerDrawn( TextToLabel(rText) |
22e90769 DW |
126 | ,bCheckable |
127 | ) | |
c5fb56c0 | 128 | #endif // owner drawn |
0e320a79 | 129 | { |
22e90769 | 130 | wxASSERT_MSG(pParentMenu != NULL, wxT("a menu item should have a parent")); |
75f11ad7 DW |
131 | |
132 | #if wxUSE_OWNER_DRAWN | |
22e90769 DW |
133 | |
134 | // | |
135 | // Set default menu colors | |
136 | // | |
75f11ad7 DW |
137 | #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c)) |
138 | ||
139 | SetTextColour(SYS_COLOR(MENUTEXT)); | |
140 | SetBackgroundColour(SYS_COLOR(MENU)); | |
141 | ||
22e90769 DW |
142 | // |
143 | // We don't want normal items be owner-drawn | |
144 | // | |
75f11ad7 DW |
145 | ResetOwnerDrawn(); |
146 | ||
147 | #undef SYS_COLOR | |
c5fb56c0 DW |
148 | #endif // wxUSE_OWNER_DRAWN |
149 | ||
150 | m_parentMenu = pParentMenu; | |
151 | m_subMenu = pSubMenu; | |
152 | m_isEnabled = TRUE; | |
153 | m_isChecked = FALSE; | |
22e90769 | 154 | m_id = nId; |
2b33b728 | 155 | m_text = TextToLabel(rText); |
c5fb56c0 | 156 | m_isCheckable = bCheckable; |
22e90769 DW |
157 | m_help = rStrHelp; |
158 | } // end of wxMenuItem::wxMenuItem | |
0e320a79 | 159 | |
75f11ad7 | 160 | wxMenuItem::~wxMenuItem() |
0e320a79 | 161 | { |
22e90769 | 162 | } // end of wxMenuItem::~wxMenuItem |
0e320a79 | 163 | |
22e90769 DW |
164 | // |
165 | // Misc | |
0e320a79 DW |
166 | // ---- |
167 | ||
22e90769 DW |
168 | // |
169 | // Return the id for calling Win32 API functions | |
170 | // | |
75f11ad7 DW |
171 | int wxMenuItem::GetRealId() const |
172 | { | |
c5fb56c0 | 173 | return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId(); |
22e90769 | 174 | } // end of wxMenuItem::GetRealId |
c5fb56c0 | 175 | |
22e90769 DW |
176 | // |
177 | // Get item state | |
c5fb56c0 | 178 | // -------------- |
c5fb56c0 DW |
179 | bool wxMenuItem::IsChecked() const |
180 | { | |
22e90769 DW |
181 | USHORT uFlag = SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu) |
182 | ,MM_QUERYITEMATTR | |
183 | ,MPFROM2SHORT(GetId(), TRUE) | |
184 | ,MPFROMSHORT(MIA_CHECKED) | |
185 | )); | |
186 | ||
187 | return (uFlag & MIA_CHECKED); | |
188 | } // end of wxMenuItem::IsChecked | |
189 | ||
190 | wxString wxMenuItemBase::GetLabelFromText( | |
191 | const wxString& rText | |
192 | ) | |
c5fb56c0 | 193 | { |
2b33b728 SN |
194 | wxString label; |
195 | for ( const wxChar *pc = rText.c_str(); *pc; pc++ ) | |
196 | { | |
197 | if ( *pc == wxT('~') || *pc == wxT('&') ) | |
198 | { | |
199 | // '~' is the escape character for GTK+ and '&' is the one for | |
200 | // wxWindows - skip both of them | |
201 | continue; | |
202 | } | |
203 | ||
204 | label += *pc; | |
205 | } | |
206 | return label; | |
75f11ad7 DW |
207 | } |
208 | ||
c5fb56c0 DW |
209 | // accelerators |
210 | // ------------ | |
211 | ||
212 | #if wxUSE_ACCEL | |
213 | ||
214 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
0e320a79 | 215 | { |
c5fb56c0 | 216 | return wxGetAccelFromString(GetText()); |
0e320a79 DW |
217 | } |
218 | ||
c5fb56c0 DW |
219 | #endif // wxUSE_ACCEL |
220 | ||
0e320a79 DW |
221 | // change item state |
222 | // ----------------- | |
223 | ||
22e90769 DW |
224 | void wxMenuItem::Enable( |
225 | bool bEnable | |
226 | ) | |
0e320a79 | 227 | { |
22e90769 | 228 | bool bOk; |
75f11ad7 | 229 | |
22e90769 DW |
230 | if (m_isEnabled == bEnable) |
231 | return; | |
232 | if (bEnable) | |
914589c2 DW |
233 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
234 | ,MM_SETITEMATTR | |
235 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
236 | ,MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED) | |
237 | ); | |
22e90769 | 238 | else |
914589c2 DW |
239 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
240 | ,MM_SETITEMATTR | |
241 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
242 | ,MPFROM2SHORT(MIA_DISABLED, FALSE) | |
243 | ); | |
22e90769 DW |
244 | if (!bOk) |
245 | { | |
c5fb56c0 | 246 | wxLogLastError("EnableMenuItem"); |
0e320a79 | 247 | } |
22e90769 DW |
248 | wxMenuItemBase::Enable(bEnable); |
249 | } // end of wxMenuItem::Enable | |
0e320a79 | 250 | |
22e90769 DW |
251 | void wxMenuItem::Check( |
252 | bool bCheck | |
253 | ) | |
0e320a79 | 254 | { |
22e90769 | 255 | bool bOk; |
75f11ad7 | 256 | |
22e90769 DW |
257 | wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") ); |
258 | if (m_isChecked == bCheck) | |
c5fb56c0 | 259 | return; |
22e90769 | 260 | if (bCheck) |
914589c2 DW |
261 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
262 | ,MM_SETITEMATTR | |
263 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
264 | ,MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED) | |
265 | ); | |
22e90769 | 266 | else |
914589c2 DW |
267 | bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu) |
268 | ,MM_SETITEMATTR | |
269 | ,MPFROM2SHORT(GetRealId(), TRUE) | |
270 | ,MPFROM2SHORT(MIA_CHECKED, FALSE) | |
271 | ); | |
22e90769 DW |
272 | if (!bOk) |
273 | { | |
274 | wxLogLastError("EnableMenuItem"); | |
75f11ad7 | 275 | } |
22e90769 DW |
276 | wxMenuItemBase::Check(bCheck); |
277 | } // end of wxMenuItem::Check | |
75f11ad7 | 278 | |
22e90769 DW |
279 | void wxMenuItem::SetText( |
280 | const wxString& rText | |
281 | ) | |
75f11ad7 | 282 | { |
22e90769 DW |
283 | // |
284 | // Don't do anything if label didn't change | |
285 | // | |
2b33b728 SN |
286 | |
287 | wxString Text = TextToLabel(rText); | |
288 | if (m_text == Text) | |
75f11ad7 DW |
289 | return; |
290 | ||
2b33b728 SN |
291 | wxMenuItemBase::SetText(Text); |
292 | OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text)); | |
22e90769 DW |
293 | |
294 | HWND hMenu = GetHMenuOf(m_parentMenu); | |
295 | ||
296 | wxCHECK_RET(hMenu, wxT("menuitem without menu")); | |
75f11ad7 | 297 | |
c5fb56c0 DW |
298 | #if wxUSE_ACCEL |
299 | m_parentMenu->UpdateAccel(this); | |
300 | #endif // wxUSE_ACCEL | |
75f11ad7 | 301 | |
22e90769 DW |
302 | USHORT uId = GetRealId(); |
303 | MENUITEM vItem; | |
304 | USHORT uFlagsOld; | |
914589c2 | 305 | |
22e90769 DW |
306 | if (!::WinSendMsg( hMenu |
307 | ,MM_QUERYITEM | |
308 | ,MPFROM2SHORT(uId, TRUE) | |
309 | ,(MPARAM)&vItem | |
310 | )) | |
75f11ad7 DW |
311 | { |
312 | wxLogLastError("GetMenuState"); | |
313 | } | |
314 | else | |
315 | { | |
22e90769 DW |
316 | uFlagsOld = vItem.afStyle; |
317 | if (IsSubMenu()) | |
75f11ad7 | 318 | { |
22e90769 | 319 | uFlagsOld |= MIS_SUBMENU; |
75f11ad7 DW |
320 | } |
321 | ||
22e90769 | 322 | BYTE* pData; |
c5fb56c0 | 323 | |
75f11ad7 | 324 | #if wxUSE_OWNER_DRAWN |
22e90769 | 325 | if (IsOwnerDrawn()) |
75f11ad7 | 326 | { |
22e90769 DW |
327 | uFlagsOld |= MIS_OWNERDRAW; |
328 | pData = (BYTE*)this; | |
75f11ad7 DW |
329 | } |
330 | else | |
331 | #endif //owner drawn | |
332 | { | |
22e90769 | 333 | uFlagsOld |= MIS_TEXT; |
2b33b728 | 334 | pData = (BYTE*)Text.c_str(); |
22e90769 DW |
335 | } |
336 | ||
337 | // | |
338 | // Set the style | |
339 | // | |
340 | if (!::WinSendMsg( hMenu | |
341 | ,MM_SETITEM | |
342 | ,MPFROM2SHORT(uId, TRUE) | |
343 | ,(MPARAM)&vItem | |
344 | )) | |
345 | { | |
346 | wxLogLastError(wxT("ModifyMenu")); | |
75f11ad7 DW |
347 | } |
348 | ||
22e90769 DW |
349 | // |
350 | // Set the text | |
351 | // | |
352 | if (::WinSendMsg( hMenu | |
353 | ,MM_SETITEMTEXT | |
354 | ,MPFROMSHORT(uId) | |
355 | ,(MPARAM)pData | |
356 | )) | |
75f11ad7 DW |
357 | { |
358 | wxLogLastError(wxT("ModifyMenu")); | |
359 | } | |
360 | } | |
22e90769 | 361 | } // end of wxMenuItem::SetText |
0e320a79 | 362 | |
22e90769 DW |
363 | void wxMenuItem::SetCheckable( |
364 | bool bCheckable | |
365 | ) | |
c5fb56c0 | 366 | { |
22e90769 DW |
367 | wxMenuItemBase::SetCheckable(bCheckable); |
368 | OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable)); | |
369 | } // end of wxMenuItem::SetCheckable | |
c5fb56c0 DW |
370 | |
371 | // ---------------------------------------------------------------------------- | |
372 | // wxMenuItemBase | |
373 | // ---------------------------------------------------------------------------- | |
374 | ||
22e90769 DW |
375 | wxMenuItem* wxMenuItemBase::New( |
376 | wxMenu* pParentMenu | |
377 | , int nId | |
378 | , const wxString& rName | |
379 | , const wxString& rHelp | |
380 | , bool bIsCheckable | |
381 | , wxMenu* pSubMenu | |
382 | ) | |
c5fb56c0 | 383 | { |
22e90769 DW |
384 | return new wxMenuItem( pParentMenu |
385 | ,nId | |
386 | ,rName | |
387 | ,rHelp | |
388 | ,bIsCheckable | |
389 | ,pSubMenu | |
390 | ); | |
391 | } // end of wxMenuItemBase::New | |
392 |