]> git.saurik.com Git - wxWidgets.git/blame - src/os2/menuitem.cpp
fix for rather mysterious problem when deleting the list ctrl
[wxWidgets.git] / src / os2 / menuitem.cpp
CommitLineData
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
61static 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 }
0367c1c0 74 else
2b33b728
SN
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
116wxMenuItem::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 157 m_help = rStrHelp;
f6bcfd97
BP
158 memset(&m_vMenuData, '\0', sizeof(m_vMenuData));
159 m_vMenuData.id= nId;
22e90769 160} // end of wxMenuItem::wxMenuItem
0e320a79 161
75f11ad7 162wxMenuItem::~wxMenuItem()
0e320a79 163{
22e90769 164} // end of wxMenuItem::~wxMenuItem
0e320a79 165
22e90769
DW
166//
167// Misc
0e320a79
DW
168// ----
169
22e90769
DW
170//
171// Return the id for calling Win32 API functions
172//
75f11ad7
DW
173int wxMenuItem::GetRealId() const
174{
c5fb56c0 175 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
22e90769 176} // end of wxMenuItem::GetRealId
c5fb56c0 177
22e90769
DW
178//
179// Get item state
c5fb56c0 180// --------------
c5fb56c0
DW
181bool wxMenuItem::IsChecked() const
182{
22e90769
DW
183 USHORT uFlag = SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu)
184 ,MM_QUERYITEMATTR
185 ,MPFROM2SHORT(GetId(), TRUE)
186 ,MPFROMSHORT(MIA_CHECKED)
187 ));
188
189 return (uFlag & MIA_CHECKED);
190} // end of wxMenuItem::IsChecked
191
192wxString wxMenuItemBase::GetLabelFromText(
193 const wxString& rText
194)
c5fb56c0 195{
2b33b728
SN
196 wxString label;
197 for ( const wxChar *pc = rText.c_str(); *pc; pc++ )
198 {
199 if ( *pc == wxT('~') || *pc == wxT('&') )
200 {
201 // '~' is the escape character for GTK+ and '&' is the one for
202 // wxWindows - skip both of them
203 continue;
204 }
205
206 label += *pc;
207 }
208 return label;
75f11ad7
DW
209}
210
0e320a79
DW
211// change item state
212// -----------------
213
22e90769
DW
214void wxMenuItem::Enable(
215 bool bEnable
216)
0e320a79 217{
22e90769 218 bool bOk;
75f11ad7 219
22e90769
DW
220 if (m_isEnabled == bEnable)
221 return;
222 if (bEnable)
914589c2
DW
223 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
224 ,MM_SETITEMATTR
225 ,MPFROM2SHORT(GetRealId(), TRUE)
f6bcfd97 226 ,MPFROM2SHORT(MIA_DISABLED, FALSE)
914589c2 227 );
22e90769 228 else
914589c2
DW
229 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
230 ,MM_SETITEMATTR
231 ,MPFROM2SHORT(GetRealId(), TRUE)
f6bcfd97 232 ,MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED)
914589c2 233 );
22e90769
DW
234 if (!bOk)
235 {
c5fb56c0 236 wxLogLastError("EnableMenuItem");
0e320a79 237 }
22e90769
DW
238 wxMenuItemBase::Enable(bEnable);
239} // end of wxMenuItem::Enable
0e320a79 240
22e90769
DW
241void wxMenuItem::Check(
242 bool bCheck
243)
0e320a79 244{
22e90769 245 bool bOk;
75f11ad7 246
22e90769
DW
247 wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") );
248 if (m_isChecked == bCheck)
c5fb56c0 249 return;
22e90769 250 if (bCheck)
914589c2
DW
251 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
252 ,MM_SETITEMATTR
253 ,MPFROM2SHORT(GetRealId(), TRUE)
254 ,MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED)
255 );
22e90769 256 else
914589c2
DW
257 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
258 ,MM_SETITEMATTR
259 ,MPFROM2SHORT(GetRealId(), TRUE)
260 ,MPFROM2SHORT(MIA_CHECKED, FALSE)
261 );
22e90769
DW
262 if (!bOk)
263 {
f6bcfd97 264 wxLogLastError("CheckMenuItem");
75f11ad7 265 }
22e90769
DW
266 wxMenuItemBase::Check(bCheck);
267} // end of wxMenuItem::Check
75f11ad7 268
22e90769
DW
269void wxMenuItem::SetText(
270 const wxString& rText
271)
75f11ad7 272{
22e90769
DW
273 //
274 // Don't do anything if label didn't change
275 //
2b33b728
SN
276
277 wxString Text = TextToLabel(rText);
278 if (m_text == Text)
75f11ad7
DW
279 return;
280
2b33b728
SN
281 wxMenuItemBase::SetText(Text);
282 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text));
22e90769
DW
283
284 HWND hMenu = GetHMenuOf(m_parentMenu);
285
286 wxCHECK_RET(hMenu, wxT("menuitem without menu"));
75f11ad7 287
c5fb56c0
DW
288#if wxUSE_ACCEL
289 m_parentMenu->UpdateAccel(this);
290#endif // wxUSE_ACCEL
75f11ad7 291
22e90769
DW
292 USHORT uId = GetRealId();
293 MENUITEM vItem;
294 USHORT uFlagsOld;
914589c2 295
22e90769
DW
296 if (!::WinSendMsg( hMenu
297 ,MM_QUERYITEM
298 ,MPFROM2SHORT(uId, TRUE)
299 ,(MPARAM)&vItem
300 ))
75f11ad7
DW
301 {
302 wxLogLastError("GetMenuState");
303 }
304 else
305 {
22e90769
DW
306 uFlagsOld = vItem.afStyle;
307 if (IsSubMenu())
75f11ad7 308 {
22e90769 309 uFlagsOld |= MIS_SUBMENU;
75f11ad7
DW
310 }
311
22e90769 312 BYTE* pData;
c5fb56c0 313
75f11ad7 314#if wxUSE_OWNER_DRAWN
22e90769 315 if (IsOwnerDrawn())
75f11ad7 316 {
22e90769
DW
317 uFlagsOld |= MIS_OWNERDRAW;
318 pData = (BYTE*)this;
75f11ad7
DW
319 }
320 else
321#endif //owner drawn
322 {
22e90769 323 uFlagsOld |= MIS_TEXT;
2b33b728 324 pData = (BYTE*)Text.c_str();
22e90769
DW
325 }
326
327 //
328 // Set the style
329 //
330 if (!::WinSendMsg( hMenu
331 ,MM_SETITEM
332 ,MPFROM2SHORT(uId, TRUE)
333 ,(MPARAM)&vItem
334 ))
335 {
336 wxLogLastError(wxT("ModifyMenu"));
75f11ad7
DW
337 }
338
22e90769
DW
339 //
340 // Set the text
341 //
342 if (::WinSendMsg( hMenu
343 ,MM_SETITEMTEXT
344 ,MPFROMSHORT(uId)
345 ,(MPARAM)pData
346 ))
75f11ad7
DW
347 {
348 wxLogLastError(wxT("ModifyMenu"));
349 }
350 }
22e90769 351} // end of wxMenuItem::SetText
0e320a79 352
22e90769
DW
353void wxMenuItem::SetCheckable(
354 bool bCheckable
355)
c5fb56c0 356{
22e90769
DW
357 wxMenuItemBase::SetCheckable(bCheckable);
358 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable));
359} // end of wxMenuItem::SetCheckable
c5fb56c0
DW
360
361// ----------------------------------------------------------------------------
362// wxMenuItemBase
363// ----------------------------------------------------------------------------
364
22e90769
DW
365wxMenuItem* wxMenuItemBase::New(
366 wxMenu* pParentMenu
367, int nId
368, const wxString& rName
369, const wxString& rHelp
370, bool bIsCheckable
371, wxMenu* pSubMenu
372)
c5fb56c0 373{
22e90769
DW
374 return new wxMenuItem( pParentMenu
375 ,nId
376 ,rName
377 ,rHelp
378 ,bIsCheckable
379 ,pSubMenu
380 );
381} // end of wxMenuItemBase::New
382