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