]>
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
& rsText
112 , const wxString
& rsHelp
116 : wxMenuItemBase( pParentMenu
123 #if wxUSE_OWNER_DRAWN
124 , wxOwnerDrawn( TextToLabel(rsText
)
125 ,eKind
== wxITEM_CHECK
127 #endif // owner drawn
129 wxASSERT_MSG(pParentMenu
!= NULL
, wxT("a menu item should have a parent"));
132 } // end of wxMenuItem::wxMenuItem
134 wxMenuItem::wxMenuItem(
137 , const wxString
& rsText
138 , const wxString
& rsHelp
142 : wxMenuItemBase( pParentMenu
146 ,bIsCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
149 #if wxUSE_OWNER_DRAWN
150 , wxOwnerDrawn( TextToLabel(rsText
)
153 #endif // owner drawn
155 wxASSERT_MSG(pParentMenu
!= NULL
, wxT("a menu item should have a parent"));
156 memset(&m_vMenuData
, '\0', sizeof(m_vMenuData
));
157 m_vMenuData
.id
= (USHORT
)nId
;
160 } // end of wxMenuItem::wxMenuItem
162 void wxMenuItem::Init()
164 m_vRadioGroup
.m_nStart
= -1;
165 m_bIsRadioGroupStart
= FALSE
;
167 #if wxUSE_OWNER_DRAWN
169 // Set default menu colors
171 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
173 SetTextColour(SYS_COLOR(MENUTEXT
));
174 SetBackgroundColour(SYS_COLOR(MENU
));
179 // We don't want normal items be owner-drawn
184 // Tell the owner drawing code to to show the accel string as well
186 SetAccelString(m_text
.AfterFirst(_T('\t')));
187 #endif // wxUSE_OWNER_DRAWN
188 } // end of wxMenuItem::Init
190 wxMenuItem::~wxMenuItem()
192 } // end of wxMenuItem::~wxMenuItem
199 // Return the id for calling Win32 API functions
201 int wxMenuItem::GetRealId() const
203 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
204 } // end of wxMenuItem::GetRealId
209 bool wxMenuItem::IsChecked() const
211 USHORT uFlag
= SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu
)
213 ,MPFROM2SHORT(GetId(), TRUE
)
214 ,MPFROMSHORT(MIA_CHECKED
)
217 return (uFlag
& MIA_CHECKED
);
218 } // end of wxMenuItem::IsChecked
220 wxString
wxMenuItemBase::GetLabelFromText(
221 const wxString
& rText
225 for ( const wxChar
*pc
= rText
.c_str(); *pc
; pc
++ )
227 if ( *pc
== wxT('~') || *pc
== wxT('&') )
229 // '~' is the escape character for GTK+ and '&' is the one for
230 // wxWindows - skip both of them
243 void wxMenuItem::SetAsRadioGroupStart()
245 m_bIsRadioGroupStart
= TRUE
;
246 } // end of wxMenuItem::SetAsRadioGroupStart
248 void wxMenuItem::SetRadioGroupStart(
252 wxASSERT_MSG( !m_bIsRadioGroupStart
253 ,_T("should only be called for the next radio items")
256 m_vRadioGroup
.m_nStart
= nStart
;
257 } // wxMenuItem::SetRadioGroupStart
259 void wxMenuItem::SetRadioGroupEnd(
263 wxASSERT_MSG( m_bIsRadioGroupStart
264 ,_T("should only be called for the first radio item")
266 m_vRadioGroup
.m_nEnd
= nEnd
;
267 } // end of wxMenuItem::SetRadioGroupEnd
272 void wxMenuItem::Enable(
278 if (m_isEnabled
== bEnable
)
281 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
283 ,MPFROM2SHORT(GetRealId(), TRUE
)
284 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
287 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
289 ,MPFROM2SHORT(GetRealId(), TRUE
)
290 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
294 wxLogLastError("EnableMenuItem");
296 wxMenuItemBase::Enable(bEnable
);
297 } // end of wxMenuItem::Enable
299 void wxMenuItem::Check(
305 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
306 if (m_isChecked
== bCheck
)
309 HMENU hMenu
= GetHmenuOf(m_parentMenu
);
311 if (GetKind() == wxITEM_RADIO
)
314 // It doesn't make sense to uncheck a radio item - what would this do?
320 // Get the index of this item in the menu
322 const wxMenuItemList
& rItems
= m_parentMenu
->GetMenuItems();
323 int nPos
= rItems
.IndexOf(this);
325 wxCHECK_RET( nPos
!= wxNOT_FOUND
326 ,_T("menuitem not found in the menu items list?")
330 // Get the radio group range
335 if (m_bIsRadioGroupStart
)
338 // We already have all information we need
341 nEnd
= m_vRadioGroup
.m_nEnd
;
343 else // next radio group item
346 // Get the radio group end from the start item
348 nStart
= m_vRadioGroup
.m_nStart
;
349 nEnd
= rItems
.Item(nStart
)->GetData()->m_vRadioGroup
.m_nEnd
;
353 // Also uncheck all the other items in this radio group
355 wxMenuItemList::Node
* pNode
= rItems
.Item(nStart
);
357 for (int n
= nStart
; n
<= nEnd
&& pNode
; n
++)
363 ,MPFROM2SHORT(n
, TRUE
)
364 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
369 pNode
->GetData()->m_isChecked
= FALSE
;
372 ,MPFROM2SHORT(n
, TRUE
)
373 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
376 pNode
= pNode
->GetNext();
382 bOk
= (bool)::WinSendMsg( hMenu
384 ,MPFROM2SHORT(GetRealId(), TRUE
)
385 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
388 bOk
= (bool)::WinSendMsg( hMenu
390 ,MPFROM2SHORT(GetRealId(), TRUE
)
391 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
396 wxLogLastError("CheckMenuItem");
398 wxMenuItemBase::Check(bCheck
);
399 } // end of wxMenuItem::Check
401 void wxMenuItem::SetText(
402 const wxString
& rText
406 // Don't do anything if label didn't change
409 wxString sText
= TextToLabel(rText
);
413 wxMenuItemBase::SetText(sText
);
414 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(sText
));
416 HWND hMenu
= GetHmenuOf(m_parentMenu
);
418 wxCHECK_RET(hMenu
, wxT("menuitem without menu"));
421 m_parentMenu
->UpdateAccel(this);
422 #endif // wxUSE_ACCEL
424 USHORT uId
= GetRealId();
428 if (!::WinSendMsg( hMenu
430 ,MPFROM2SHORT(uId
, TRUE
)
434 wxLogLastError("GetMenuState");
438 uFlagsOld
= vItem
.afStyle
;
441 uFlagsOld
|= MIS_SUBMENU
;
446 #if wxUSE_OWNER_DRAWN
449 uFlagsOld
|= MIS_OWNERDRAW
;
455 uFlagsOld
|= MIS_TEXT
;
456 pData
= (BYTE
*)sText
.c_str();
462 if (!::WinSendMsg( hMenu
464 ,MPFROM2SHORT(uId
, TRUE
)
468 wxLogLastError(wxT("ModifyMenu"));
474 if (::WinSendMsg( hMenu
480 wxLogLastError(wxT("ModifyMenu"));
483 } // end of wxMenuItem::SetText
485 void wxMenuItem::SetCheckable(
489 wxMenuItemBase::SetCheckable(bCheckable
);
490 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable
));
491 } // end of wxMenuItem::SetCheckable
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 wxMenuItem
* wxMenuItemBase::New(
500 , const wxString
& rName
501 , const wxString
& rHelp
506 return new wxMenuItem( pParentMenu
513 } // end of wxMenuItemBase::New