]> git.saurik.com Git - wxWidgets.git/blame - src/os2/menuitem.cpp
More scrolling support
[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 }
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
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
c5fb56c0
DW
211// accelerators
212// ------------
213
214#if wxUSE_ACCEL
215
216wxAcceleratorEntry *wxMenuItem::GetAccel() const
0e320a79 217{
c5fb56c0 218 return wxGetAccelFromString(GetText());
0e320a79
DW
219}
220
c5fb56c0
DW
221#endif // wxUSE_ACCEL
222
0e320a79
DW
223// change item state
224// -----------------
225
22e90769
DW
226void wxMenuItem::Enable(
227 bool bEnable
228)
0e320a79 229{
22e90769 230 bool bOk;
75f11ad7 231
22e90769
DW
232 if (m_isEnabled == bEnable)
233 return;
234 if (bEnable)
914589c2
DW
235 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
236 ,MM_SETITEMATTR
237 ,MPFROM2SHORT(GetRealId(), TRUE)
f6bcfd97 238 ,MPFROM2SHORT(MIA_DISABLED, FALSE)
914589c2 239 );
22e90769 240 else
914589c2
DW
241 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
242 ,MM_SETITEMATTR
243 ,MPFROM2SHORT(GetRealId(), TRUE)
f6bcfd97 244 ,MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED)
914589c2 245 );
22e90769
DW
246 if (!bOk)
247 {
c5fb56c0 248 wxLogLastError("EnableMenuItem");
0e320a79 249 }
22e90769
DW
250 wxMenuItemBase::Enable(bEnable);
251} // end of wxMenuItem::Enable
0e320a79 252
22e90769
DW
253void wxMenuItem::Check(
254 bool bCheck
255)
0e320a79 256{
22e90769 257 bool bOk;
75f11ad7 258
22e90769
DW
259 wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") );
260 if (m_isChecked == bCheck)
c5fb56c0 261 return;
22e90769 262 if (bCheck)
914589c2
DW
263 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
264 ,MM_SETITEMATTR
265 ,MPFROM2SHORT(GetRealId(), TRUE)
266 ,MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED)
267 );
22e90769 268 else
914589c2
DW
269 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
270 ,MM_SETITEMATTR
271 ,MPFROM2SHORT(GetRealId(), TRUE)
272 ,MPFROM2SHORT(MIA_CHECKED, FALSE)
273 );
22e90769
DW
274 if (!bOk)
275 {
f6bcfd97 276 wxLogLastError("CheckMenuItem");
75f11ad7 277 }
22e90769
DW
278 wxMenuItemBase::Check(bCheck);
279} // end of wxMenuItem::Check
75f11ad7 280
22e90769
DW
281void wxMenuItem::SetText(
282 const wxString& rText
283)
75f11ad7 284{
22e90769
DW
285 //
286 // Don't do anything if label didn't change
287 //
2b33b728
SN
288
289 wxString Text = TextToLabel(rText);
290 if (m_text == Text)
75f11ad7
DW
291 return;
292
2b33b728
SN
293 wxMenuItemBase::SetText(Text);
294 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text));
22e90769
DW
295
296 HWND hMenu = GetHMenuOf(m_parentMenu);
297
298 wxCHECK_RET(hMenu, wxT("menuitem without menu"));
75f11ad7 299
c5fb56c0
DW
300#if wxUSE_ACCEL
301 m_parentMenu->UpdateAccel(this);
302#endif // wxUSE_ACCEL
75f11ad7 303
22e90769
DW
304 USHORT uId = GetRealId();
305 MENUITEM vItem;
306 USHORT uFlagsOld;
914589c2 307
22e90769
DW
308 if (!::WinSendMsg( hMenu
309 ,MM_QUERYITEM
310 ,MPFROM2SHORT(uId, TRUE)
311 ,(MPARAM)&vItem
312 ))
75f11ad7
DW
313 {
314 wxLogLastError("GetMenuState");
315 }
316 else
317 {
22e90769
DW
318 uFlagsOld = vItem.afStyle;
319 if (IsSubMenu())
75f11ad7 320 {
22e90769 321 uFlagsOld |= MIS_SUBMENU;
75f11ad7
DW
322 }
323
22e90769 324 BYTE* pData;
c5fb56c0 325
75f11ad7 326#if wxUSE_OWNER_DRAWN
22e90769 327 if (IsOwnerDrawn())
75f11ad7 328 {
22e90769
DW
329 uFlagsOld |= MIS_OWNERDRAW;
330 pData = (BYTE*)this;
75f11ad7
DW
331 }
332 else
333#endif //owner drawn
334 {
22e90769 335 uFlagsOld |= MIS_TEXT;
2b33b728 336 pData = (BYTE*)Text.c_str();
22e90769
DW
337 }
338
339 //
340 // Set the style
341 //
342 if (!::WinSendMsg( hMenu
343 ,MM_SETITEM
344 ,MPFROM2SHORT(uId, TRUE)
345 ,(MPARAM)&vItem
346 ))
347 {
348 wxLogLastError(wxT("ModifyMenu"));
75f11ad7
DW
349 }
350
22e90769
DW
351 //
352 // Set the text
353 //
354 if (::WinSendMsg( hMenu
355 ,MM_SETITEMTEXT
356 ,MPFROMSHORT(uId)
357 ,(MPARAM)pData
358 ))
75f11ad7
DW
359 {
360 wxLogLastError(wxT("ModifyMenu"));
361 }
362 }
22e90769 363} // end of wxMenuItem::SetText
0e320a79 364
22e90769
DW
365void wxMenuItem::SetCheckable(
366 bool bCheckable
367)
c5fb56c0 368{
22e90769
DW
369 wxMenuItemBase::SetCheckable(bCheckable);
370 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable));
371} // end of wxMenuItem::SetCheckable
c5fb56c0
DW
372
373// ----------------------------------------------------------------------------
374// wxMenuItemBase
375// ----------------------------------------------------------------------------
376
22e90769
DW
377wxMenuItem* wxMenuItemBase::New(
378 wxMenu* pParentMenu
379, int nId
380, const wxString& rName
381, const wxString& rHelp
382, bool bIsCheckable
383, wxMenu* pSubMenu
384)
c5fb56c0 385{
22e90769
DW
386 return new wxMenuItem( pParentMenu
387 ,nId
388 ,rName
389 ,rHelp
390 ,bIsCheckable
391 ,pSubMenu
392 );
393} // end of wxMenuItemBase::New
394