]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/menuitem.cpp
3c065c11fd96d1796236b6f4fa8c555092ff0e57
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"));
126 } // end of wxMenuItem::wxMenuItem
128 wxMenuItem::wxMenuItem(
131 , const wxString
& rText
132 , const wxString
& rStrHelp
136 : wxMenuItemBase(pParentMenu
, nId
, rText
, rStrHelp
, bIsCheckable
? kITEM_CHECK
: kITEM_NORMAL
, pSubMenu
)
137 #if wxUSE_OWNER_DRAWN
138 , wxOwnerDrawn( TextToLabel(rText
)
141 #endif // owner drawn
143 wxASSERT_MSG(pParentMenu
!= NULL
, wxT("a menu item should have a parent"));
146 } // end of wxMenuItem::wxMenuItem
148 void wxMenuItem::Init()
150 m_radioGroup
.start
= -1;
151 m_isRadioGroupStart
= FALSE
;
153 #if wxUSE_OWNER_DRAWN
154 // set default menu colors
155 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
157 SetTextColour(SYS_COLOR(MENUTEXT
));
158 SetBackgroundColour(SYS_COLOR(MENU
));
162 // we don't want normal items be owner-drawn
165 // tell the owner drawing code to to show the accel string as well
166 SetAccelString(m_text
.AfterFirst(_T('\t')));
167 #endif // wxUSE_OWNER_DRAWN
170 wxMenuItem::~wxMenuItem()
172 } // end of wxMenuItem::~wxMenuItem
179 // Return the id for calling Win32 API functions
181 int wxMenuItem::GetRealId() const
183 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
184 } // end of wxMenuItem::GetRealId
189 bool wxMenuItem::IsChecked() const
191 USHORT uFlag
= SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu
)
193 ,MPFROM2SHORT(GetId(), TRUE
)
194 ,MPFROMSHORT(MIA_CHECKED
)
197 return (uFlag
& MIA_CHECKED
);
198 } // end of wxMenuItem::IsChecked
200 wxString
wxMenuItemBase::GetLabelFromText(
201 const wxString
& rText
205 for ( const wxChar
*pc
= rText
.c_str(); *pc
; pc
++ )
207 if ( *pc
== wxT('~') || *pc
== wxT('&') )
209 // '~' is the escape character for GTK+ and '&' is the one for
210 // wxWindows - skip both of them
222 void wxMenuItem::SetAsRadioGroupStart()
224 m_bIsRadioGroupStart
= TRUE
;
225 } // end of wxMenuItem::SetAsRadioGroupStart
227 void wxMenuItem::SetRadioGroupStart(
231 wxASSERT_MSG( !m_bIsRadioGroupStart
,
232 _T("should only be called for the next radio items") );
234 m_vRadioGroup
.m_nStart
= nStart
;
235 } // end of wxMenuItem::SetRadioGroupStart
237 void wxMenuItem::SetRadioGroupEnd(
241 wxASSERT_MSG( m_bIsRadioGroupStart
,
242 _T("should only be called for the first radio item") );
244 m_vRadioGroup
.m_nEnd
= nEnd
;
245 } // end of wxMenuItem::SetRadioGroupEnd
250 void wxMenuItem::Enable(
256 if (m_isEnabled
== bEnable
)
259 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
261 ,MPFROM2SHORT(GetRealId(), TRUE
)
262 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
265 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
267 ,MPFROM2SHORT(GetRealId(), TRUE
)
268 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
272 wxLogLastError("EnableMenuItem");
274 wxMenuItemBase::Enable(bEnable
);
275 } // end of wxMenuItem::Enable
277 void wxMenuItem::Check(
283 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
284 if (m_isChecked
== bCheck
)
288 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
290 ,MPFROM2SHORT(GetRealId(), TRUE
)
291 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
294 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
296 ,MPFROM2SHORT(GetRealId(), TRUE
)
297 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
301 wxLogLastError("CheckMenuItem");
303 wxMenuItemBase::Check(bCheck
);
304 } // end of wxMenuItem::Check
306 void wxMenuItem::SetText(
307 const wxString
& rText
311 // Don't do anything if label didn't change
314 wxString Text
= TextToLabel(rText
);
318 wxMenuItemBase::SetText(Text
);
319 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text
));
321 HWND hMenu
= GetHMenuOf(m_parentMenu
);
323 wxCHECK_RET(hMenu
, wxT("menuitem without menu"));
326 m_parentMenu
->UpdateAccel(this);
327 #endif // wxUSE_ACCEL
329 USHORT uId
= GetRealId();
333 if (!::WinSendMsg( hMenu
335 ,MPFROM2SHORT(uId
, TRUE
)
339 wxLogLastError("GetMenuState");
343 uFlagsOld
= vItem
.afStyle
;
346 uFlagsOld
|= MIS_SUBMENU
;
351 #if wxUSE_OWNER_DRAWN
354 uFlagsOld
|= MIS_OWNERDRAW
;
360 uFlagsOld
|= MIS_TEXT
;
361 pData
= (BYTE
*)Text
.c_str();
367 if (!::WinSendMsg( hMenu
369 ,MPFROM2SHORT(uId
, TRUE
)
373 wxLogLastError(wxT("ModifyMenu"));
379 if (::WinSendMsg( hMenu
385 wxLogLastError(wxT("ModifyMenu"));
388 } // end of wxMenuItem::SetText
390 void wxMenuItem::SetCheckable(
394 wxMenuItemBase::SetCheckable(bCheckable
);
395 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable
));
396 } // end of wxMenuItem::SetCheckable
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 wxMenuItem
* wxMenuItemBase::New(
405 , const wxString
& rName
406 , const wxString
& rHelp
411 return new wxMenuItem( pParentMenu
418 } // end of wxMenuItemBase::New