]>
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
.c_str(); *pc
!= wxT('\0'); pc
++ )
69 if (*(pc
+1) == wxT('&'))
79 if ( *pc
== wxT('~') )
81 // tildes must be doubled to prevent them from being
82 // interpreted as accelerator character prefix by PM ???
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // dynamic classes implementation
97 // ----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
108 wxMenuItem::wxMenuItem(
111 , const wxString
& rText
112 , const wxString
& rStrHelp
116 : wxMenuItemBase(pParentMenu
, nId
, rText
, rStrHelp
, kind
, pSubMenu
)
117 #if wxUSE_OWNER_DRAWN
118 , wxOwnerDrawn( TextToLabel(rText
)
121 #endif // owner drawn
123 wxASSERT_MSG(pParentMenu
!= NULL
, wxT("a menu item should have a parent"));
125 #if wxUSE_OWNER_DRAWN
128 // Set default menu colors
130 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
132 SetTextColour(SYS_COLOR(MENUTEXT
));
133 SetBackgroundColour(SYS_COLOR(MENU
));
136 // We don't want normal items be owner-drawn
141 #endif // wxUSE_OWNER_DRAWN
143 m_text
= TextToLabel(rText
);
145 memset(&m_vMenuData
, '\0', sizeof(m_vMenuData
));
147 } // end of wxMenuItem::wxMenuItem
149 wxMenuItem::~wxMenuItem()
151 } // end of wxMenuItem::~wxMenuItem
158 // Return the id for calling Win32 API functions
160 int wxMenuItem::GetRealId() const
162 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
163 } // end of wxMenuItem::GetRealId
168 bool wxMenuItem::IsChecked() const
170 USHORT uFlag
= SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu
)
172 ,MPFROM2SHORT(GetId(), TRUE
)
173 ,MPFROMSHORT(MIA_CHECKED
)
176 return (uFlag
& MIA_CHECKED
);
177 } // end of wxMenuItem::IsChecked
179 wxString
wxMenuItemBase::GetLabelFromText(
180 const wxString
& rText
184 for ( const wxChar
*pc
= rText
.c_str(); *pc
; pc
++ )
186 if ( *pc
== wxT('~') || *pc
== wxT('&') )
188 // '~' is the escape character for GTK+ and '&' is the one for
189 // wxWindows - skip both of them
201 void wxMenuItem::Enable(
207 if (m_isEnabled
== bEnable
)
210 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
212 ,MPFROM2SHORT(GetRealId(), TRUE
)
213 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
216 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
218 ,MPFROM2SHORT(GetRealId(), TRUE
)
219 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
223 wxLogLastError("EnableMenuItem");
225 wxMenuItemBase::Enable(bEnable
);
226 } // end of wxMenuItem::Enable
228 void wxMenuItem::Check(
234 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
235 if (m_isChecked
== bCheck
)
238 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
240 ,MPFROM2SHORT(GetRealId(), TRUE
)
241 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
244 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
246 ,MPFROM2SHORT(GetRealId(), TRUE
)
247 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
251 wxLogLastError("CheckMenuItem");
253 wxMenuItemBase::Check(bCheck
);
254 } // end of wxMenuItem::Check
256 void wxMenuItem::SetText(
257 const wxString
& rText
261 // Don't do anything if label didn't change
264 wxString Text
= TextToLabel(rText
);
268 wxMenuItemBase::SetText(Text
);
269 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text
));
271 HWND hMenu
= GetHMenuOf(m_parentMenu
);
273 wxCHECK_RET(hMenu
, wxT("menuitem without menu"));
276 m_parentMenu
->UpdateAccel(this);
277 #endif // wxUSE_ACCEL
279 USHORT uId
= GetRealId();
283 if (!::WinSendMsg( hMenu
285 ,MPFROM2SHORT(uId
, TRUE
)
289 wxLogLastError("GetMenuState");
293 uFlagsOld
= vItem
.afStyle
;
296 uFlagsOld
|= MIS_SUBMENU
;
301 #if wxUSE_OWNER_DRAWN
304 uFlagsOld
|= MIS_OWNERDRAW
;
310 uFlagsOld
|= MIS_TEXT
;
311 pData
= (BYTE
*)Text
.c_str();
317 if (!::WinSendMsg( hMenu
319 ,MPFROM2SHORT(uId
, TRUE
)
323 wxLogLastError(wxT("ModifyMenu"));
329 if (::WinSendMsg( hMenu
335 wxLogLastError(wxT("ModifyMenu"));
338 } // end of wxMenuItem::SetText
340 void wxMenuItem::SetCheckable(
344 wxMenuItemBase::SetCheckable(bCheckable
);
345 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable
));
346 } // end of wxMenuItem::SetCheckable
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
352 wxMenuItem
* wxMenuItemBase::New(
355 , const wxString
& rName
356 , const wxString
& rHelp
361 return new wxMenuItem( pParentMenu
368 } // end of wxMenuItemBase::New