]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/menuitem.cpp
a5ad2a759f5a3429c572d6475e18556f4b64541d
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
242 void wxMenuItem::SetAsRadioGroupStart()
244 m_bIsRadioGroupStart
= TRUE
;
245 } // end of wxMenuItem::SetAsRadioGroupStart
247 void wxMenuItem::SetRadioGroupStart(
251 wxASSERT_MSG( !m_bIsRadioGroupStart
,
252 _T("should only be called for the next radio items") );
254 m_vRadioGroup
.m_nStart
= nStart
;
255 } // end of wxMenuItem::SetRadioGroupStart
257 void wxMenuItem::SetRadioGroupEnd(
261 wxASSERT_MSG( m_bIsRadioGroupStart
,
262 _T("should only be called for the first radio item") );
264 m_vRadioGroup
.m_nEnd
= nEnd
;
265 } // end of wxMenuItem::SetRadioGroupEnd
271 void wxMenuItem::Enable(
277 if (m_isEnabled
== bEnable
)
280 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
282 ,MPFROM2SHORT(GetRealId(), TRUE
)
283 ,MPFROM2SHORT(MIA_DISABLED
, FALSE
)
286 bOk
= (bool)::WinSendMsg( GetHMenuOf(m_parentMenu
)
288 ,MPFROM2SHORT(GetRealId(), TRUE
)
289 ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
)
293 wxLogLastError("EnableMenuItem");
295 wxMenuItemBase::Enable(bEnable
);
296 } // end of wxMenuItem::Enable
298 void wxMenuItem::Check(
304 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
305 if (m_isChecked
== bCheck
)
308 HMENU hMenu
= GetHmenuOf(m_parentMenu
);
309 if ( GetKind() == wxITEM_RADIO
)
312 // It doesn't make sense to uncheck a radio item - what would this do?
318 // Get the index of this item in the menu
320 const wxMenuItemList
& rItems
= m_parentMenu
->GetMenuItems();
321 int nPos
= rItems
.IndexOf(this);
325 wxCHECK_RET( nPos
!= wxNOT_FOUND
,
326 _T("menuitem not found in the menu items list?") );
329 // Get the radio group range
332 if (m_bIsRadioGroupStart
)
334 // we already have all information we need
336 nEnd
= m_vRadioGroup
.m_nEnd
;
338 else // Next radio group item
341 // Get the radio group end from the start item
343 nStart
= m_vRadioGroup
.m_nStart
;
344 nEnd
= rItems
.Item(nStart
)->GetData()->m_vRadioGroup
.m_nEnd
;
348 // Also uncheck all the other items in this radio group
350 wxMenuItemList::Node
* pNode
= rItems
.Item(nStart
);
352 for (int n
= nStart
; n
<= nEnd
&& pNode
; n
++)
356 pNode
->GetData()->m_isChecked
= FALSE
;
361 bOk
= (bool)::WinSendMsg( hMenu
363 ,MPFROM2SHORT(n
, TRUE
)
364 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
369 bOk
= (bool)::WinSendMsg( hMenu
371 ,MPFROM2SHORT(n
, TRUE
)
372 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
375 pNode
= pNode
->GetNext();
381 bOk
= (bool)::WinSendMsg( hMenu
383 ,MPFROM2SHORT(GetRealId(), TRUE
)
384 ,MPFROM2SHORT(MIA_CHECKED
, MIA_CHECKED
)
387 bOk
= (bool)::WinSendMsg( hMenu
389 ,MPFROM2SHORT(GetRealId(), TRUE
)
390 ,MPFROM2SHORT(MIA_CHECKED
, FALSE
)
395 wxLogLastError("CheckMenuItem");
397 wxMenuItemBase::Check(bCheck
);
398 } // end of wxMenuItem::Check
400 void wxMenuItem::SetText(
401 const wxString
& rText
405 // Don't do anything if label didn't change
408 wxString sText
= TextToLabel(rText
);
412 wxMenuItemBase::SetText(sText
);
413 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(sText
));
415 HWND hMenu
= GetHmenuOf(m_parentMenu
);
417 wxCHECK_RET(hMenu
, wxT("menuitem without menu"));
420 m_parentMenu
->UpdateAccel(this);
421 #endif // wxUSE_ACCEL
423 USHORT uId
= GetRealId();
427 if (!::WinSendMsg( hMenu
429 ,MPFROM2SHORT(uId
, TRUE
)
433 wxLogLastError("GetMenuState");
437 uFlagsOld
= vItem
.afStyle
;
440 uFlagsOld
|= MIS_SUBMENU
;
445 #if wxUSE_OWNER_DRAWN
448 uFlagsOld
|= MIS_OWNERDRAW
;
454 uFlagsOld
|= MIS_TEXT
;
455 pData
= (BYTE
*)sText
.c_str();
461 if (!::WinSendMsg( hMenu
463 ,MPFROM2SHORT(uId
, TRUE
)
467 wxLogLastError(wxT("ModifyMenu"));
473 if (::WinSendMsg( hMenu
479 wxLogLastError(wxT("ModifyMenu"));
482 } // end of wxMenuItem::SetText
484 void wxMenuItem::SetCheckable(
488 wxMenuItemBase::SetCheckable(bCheckable
);
489 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable
));
490 } // end of wxMenuItem::SetCheckable
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 wxMenuItem
* wxMenuItemBase::New(
499 , const wxString
& rName
500 , const wxString
& rHelp
505 return new wxMenuItem( pParentMenu
512 } // end of wxMenuItemBase::New