]>
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 } // end of wxMenuItem::wxMenuItem
160 wxMenuItem::~wxMenuItem()
162 } // end of wxMenuItem::~wxMenuItem
169 // Return the id for calling Win32 API functions
171 int wxMenuItem::GetRealId() const
173 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
174 } // end of wxMenuItem::GetRealId
179 bool wxMenuItem::IsChecked() const
181 USHORT uFlag
= SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu
)
183 ,MPFROM2SHORT(GetId(), TRUE
)
184 ,MPFROMSHORT(MIA_CHECKED
)
187 return (uFlag
& MIA_CHECKED
);
188 } // end of wxMenuItem::IsChecked
190 wxString
wxMenuItemBase::GetLabelFromText(
191 const wxString
& rText
195 for ( const wxChar
*pc
= rText
.c_str(); *pc
; pc
++ )
197 if ( *pc
== wxT('~') || *pc
== wxT('&') )
199 // '~' is the escape character for GTK+ and '&' is the one for
200 // wxWindows - skip both of them
214 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
216 return wxGetAccelFromString(GetText());
219 #endif // wxUSE_ACCEL
224 void wxMenuItem::Enable(
230 if (m_isEnabled
== bEnable
)
233 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
235 ,MPFROM2SHORT(GetRealId(), TRUE
)
236 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
239 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
241 ,MPFROM2SHORT(GetRealId(), TRUE
)
242 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
246 wxLogLastError("EnableMenuItem");
248 wxMenuItemBase::Enable(bEnable
);
249 } // end of wxMenuItem::Enable
251 void wxMenuItem::Check(
257 wxCHECK_RET( m_isCheckable
, wxT("only checkable items may be checked") );
258 if (m_isChecked
== bCheck
)
261 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
263 ,MPFROM2SHORT(GetRealId(), TRUE
)
264 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
267 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
269 ,MPFROM2SHORT(GetRealId(), TRUE
)
270 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
274 wxLogLastError("EnableMenuItem");
276 wxMenuItemBase::Check(bCheck
);
277 } // end of wxMenuItem::Check
279 void wxMenuItem::SetText(
280 const wxString
& rText
284 // Don't do anything if label didn't change
287 wxString Text
= TextToLabel(rText
);
291 wxMenuItemBase::SetText(Text
);
292 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text
));
294 HWND hMenu
= GetHMenuOf(m_parentMenu
);
296 wxCHECK_RET(hMenu
, wxT("menuitem without menu"));
299 m_parentMenu
->UpdateAccel(this);
300 #endif // wxUSE_ACCEL
302 USHORT uId
= GetRealId();
306 if (!::WinSendMsg( hMenu
308 ,MPFROM2SHORT(uId
, TRUE
)
312 wxLogLastError("GetMenuState");
316 uFlagsOld
= vItem
.afStyle
;
319 uFlagsOld
|= MIS_SUBMENU
;
324 #if wxUSE_OWNER_DRAWN
327 uFlagsOld
|= MIS_OWNERDRAW
;
333 uFlagsOld
|= MIS_TEXT
;
334 pData
= (BYTE
*)Text
.c_str();
340 if (!::WinSendMsg( hMenu
342 ,MPFROM2SHORT(uId
, TRUE
)
346 wxLogLastError(wxT("ModifyMenu"));
352 if (::WinSendMsg( hMenu
358 wxLogLastError(wxT("ModifyMenu"));
361 } // end of wxMenuItem::SetText
363 void wxMenuItem::SetCheckable(
367 wxMenuItemBase::SetCheckable(bCheckable
);
368 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable
));
369 } // end of wxMenuItem::SetCheckable
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 wxMenuItem
* wxMenuItemBase::New(
378 , const wxString
& rName
379 , const wxString
& rHelp
384 return new wxMenuItem( pParentMenu
391 } // end of wxMenuItemBase::New