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