]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
17 #pragma implementation "menuitem.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/bitmap.h"
26 #include "wx/settings.h"
28 #include "wx/window.h"
31 #include "wx/string.h"
34 #include "wx/menuitem.h"
41 #include "wx/os2/private.h"
43 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
48 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
50 // conditional compilation
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
57 // ----------------------------------------------------------------------------
58 // static function for translating menu labels
59 // ----------------------------------------------------------------------------
61 static wxString
TextToLabel(const wxString
& rTitle
)
65 for (pc
= rTitle
; *pc
!= wxT('\0'); pc
++ )
69 if (*(pc
+1) == wxT('&'))
77 // else if (*pc == wxT('/'))
79 // Title << wxT('\\');
83 if ( *pc
== wxT('~') )
85 // tildes must be doubled to prevent them from being
86 // interpreted as accelerator character prefix by PM ???
95 // ============================================================================
97 // ============================================================================
99 // ----------------------------------------------------------------------------
100 // dynamic classes implementation
101 // ----------------------------------------------------------------------------
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
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
116 wxMenuItem::wxMenuItem(
119 , const wxString
& rText
120 , const wxString
& rStrHelp
124 #if wxUSE_OWNER_DRAWN
125 : wxOwnerDrawn( TextToLabel(rText
)
128 #endif // owner drawn
130 wxASSERT_MSG(pParentMenu
!= NULL
, wxT("a menu item should have a parent"));
132 #if wxUSE_OWNER_DRAWN
135 // Set default menu colors
137 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
139 SetTextColour(SYS_COLOR(MENUTEXT
));
140 SetBackgroundColour(SYS_COLOR(MENU
));
143 // We don't want normal items be owner-drawn
148 #endif // wxUSE_OWNER_DRAWN
150 m_parentMenu
= pParentMenu
;
151 m_subMenu
= pSubMenu
;
155 m_text
= TextToLabel(rText
);
156 m_isCheckable
= bCheckable
;
158 memset(&m_vMenuData
, '\0', sizeof(m_vMenuData
));
160 } // end of wxMenuItem::wxMenuItem
162 wxMenuItem::~wxMenuItem()
164 } // end of wxMenuItem::~wxMenuItem
171 // Return the id for calling Win32 API functions
173 int wxMenuItem::GetRealId() const
175 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
176 } // end of wxMenuItem::GetRealId
181 bool wxMenuItem::IsChecked() const
183 USHORT uFlag
= SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu
)
185 ,MPFROM2SHORT(GetId(), TRUE
)
186 ,MPFROMSHORT(MIA_CHECKED
)
189 return (uFlag
& MIA_CHECKED
);
190 } // end of wxMenuItem::IsChecked
192 wxString
wxMenuItemBase::GetLabelFromText(
193 const wxString
& rText
197 for ( const wxChar
*pc
= rText
.c_str(); *pc
; pc
++ )
199 if ( *pc
== wxT('~') || *pc
== wxT('&') )
201 // '~' is the escape character for GTK+ and '&' is the one for
202 // wxWindows - skip both of them
216 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
218 return wxGetAccelFromString(GetText());
221 #endif // wxUSE_ACCEL
226 void wxMenuItem::Enable(
232 if (m_isEnabled
== bEnable
)
235 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
237 ,MPFROM2SHORT(GetRealId(), TRUE
)
238 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
241 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
243 ,MPFROM2SHORT(GetRealId(), TRUE
)
244 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
248 wxLogLastError("EnableMenuItem");
250 wxMenuItemBase::Enable(bEnable
);
251 } // end of wxMenuItem::Enable
253 void wxMenuItem::Check(
259 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
260 if (m_isChecked
== bCheck
)
263 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
265 ,MPFROM2SHORT(GetRealId(), TRUE
)
266 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
269 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
271 ,MPFROM2SHORT(GetRealId(), TRUE
)
272 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
276 wxLogLastError("CheckMenuItem");
278 wxMenuItemBase::Check(bCheck
);
279 } // end of wxMenuItem::Check
281 void wxMenuItem::SetText(
282 const wxString
& rText
286 // Don't do anything if label didn't change
289 wxString Text
= TextToLabel(rText
);
293 wxMenuItemBase::SetText(Text
);
294 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text
));
296 HWND hMenu
= GetHMenuOf(m_parentMenu
);
298 wxCHECK_RET(hMenu
, wxT("menuitem without menu"));
301 m_parentMenu
->UpdateAccel(this);
302 #endif // wxUSE_ACCEL
304 USHORT uId
= GetRealId();
308 if (!::WinSendMsg( hMenu
310 ,MPFROM2SHORT(uId
, TRUE
)
314 wxLogLastError("GetMenuState");
318 uFlagsOld
= vItem
.afStyle
;
321 uFlagsOld
|= MIS_SUBMENU
;
326 #if wxUSE_OWNER_DRAWN
329 uFlagsOld
|= MIS_OWNERDRAW
;
335 uFlagsOld
|= MIS_TEXT
;
336 pData
= (BYTE
*)Text
.c_str();
342 if (!::WinSendMsg( hMenu
344 ,MPFROM2SHORT(uId
, TRUE
)
348 wxLogLastError(wxT("ModifyMenu"));
354 if (::WinSendMsg( hMenu
360 wxLogLastError(wxT("ModifyMenu"));
363 } // end of wxMenuItem::SetText
365 void wxMenuItem::SetCheckable(
369 wxMenuItemBase::SetCheckable(bCheckable
);
370 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable
));
371 } // end of wxMenuItem::SetCheckable
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 wxMenuItem
* wxMenuItemBase::New(
380 , const wxString
& rName
381 , const wxString
& rHelp
386 return new wxMenuItem( pParentMenu
393 } // end of wxMenuItemBase::New