]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "menuitem.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/bitmap.h"
36 #include "wx/settings.h"
38 #include "wx/window.h"
41 #include "wx/string.h"
44 #include "wx/menuitem.h"
51 #include "wx/msw/private.h"
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
58 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
60 // conditional compilation
62 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
63 #else // !wxUSE_OWNER_DRAWN
64 #define OWNER_DRAWN_ONLY( code )
65 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
72 // dynamic classes implementation
73 // ----------------------------------------------------------------------------
75 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
84 wxMenuItem::wxMenuItem(wxMenu
*pParentMenu
,
87 const wxString
& strHelp
,
90 : wxMenuItemBase(pParentMenu
, id
, text
, strHelp
, kind
, pSubMenu
)
92 , wxOwnerDrawn(GetLabelFromText(text
), kind
== wxItem_Check
)
95 wxASSERT_MSG( pParentMenu
!= NULL
, wxT("a menu item should have a parent") );
100 #if wxUSE_OWNER_DRAWN
101 // set default menu colors
102 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
104 SetTextColour(SYS_COLOR(MENUTEXT
));
105 SetBackgroundColour(SYS_COLOR(MENU
));
109 // we don't want normal items be owner-drawn
112 // tell the owner drawing code to to show the accel string as well
113 SetAccelString(text
.AfterFirst(_T('\t')));
114 #endif // wxUSE_OWNER_DRAWN
117 wxMenuItem::~wxMenuItem()
124 // return the id for calling Win32 API functions
125 int wxMenuItem::GetRealId() const
127 return m_subMenu
? (int)m_subMenu
->GetHMenu() : GetId();
133 bool wxMenuItem::IsChecked() const
135 int flag
= ::GetMenuState(GetHMenuOf(m_parentMenu
), GetId(), MF_BYCOMMAND
);
137 return (flag
& MF_CHECKED
) != 0;
141 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
143 return wxStripMenuCodes(text
);
149 void wxMenuItem::Enable(bool enable
)
151 if ( m_isEnabled
== enable
)
154 long rc
= EnableMenuItem(GetHMenuOf(m_parentMenu
),
157 (enable
? MF_ENABLED
: MF_GRAYED
));
160 wxLogLastError(wxT("EnableMenuItem"));
163 wxMenuItemBase::Enable(enable
);
166 void wxMenuItem::Check(bool check
)
168 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
170 if ( m_isChecked
== check
)
173 int flags
= check
? MF_CHECKED
: MF_UNCHECKED
;
174 HMENU hmenu
= GetHMenuOf(m_parentMenu
);
176 if ( GetKind() == wxItem_Radio
)
178 // it doesn't make sense to uncheck a radio item - what would this do?
182 const wxMenuItemList
& items
= m_parentMenu
->GetMenuItems();
183 int pos
= items
.IndexOf(this);
184 wxCHECK_RET( pos
!= wxNOT_FOUND
,
185 _T("menuitem not found in the menu items list?") );
188 if ( !::CheckMenuRadioItem(hmenu
,
189 m_startRadioGroup
, // first group item
190 m_endRadioGroup
, // last one
191 pos
, // the one to check
192 MF_BYPOSITION
| flags
) )
194 wxLogLastError(_T("CheckMenuRadioItem"));
198 // also uncheck all the other items in this radio group
199 wxMenuItemList::Node
*node
= items
.Item(m_startRadioGroup
);
200 for ( int n
= m_startRadioGroup
; n
<= m_endRadioGroup
&& node
; n
++ )
204 node
->GetData()->m_isChecked
= FALSE
;
207 // we also have to do it in the menu for Win16 (under Win32
208 // CheckMenuRadioItem() does it for us)
210 ::CheckMenuItem(hmenu
, n
, n
== pos
? MF_CHECKED
: MF_UNCHECKED
);
213 node
= node
->GetNext();
218 if ( ::CheckMenuItem(hmenu
,
220 MF_BYCOMMAND
| flags
) == -1 )
222 wxLogLastError(wxT("CheckMenuItem"));
226 wxMenuItemBase::Check(check
);
229 void wxMenuItem::SetText(const wxString
& text
)
231 // don't do anything if label didn't change
232 if ( m_text
== text
)
235 wxMenuItemBase::SetText(text
);
236 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text
) );
238 HMENU hMenu
= GetHMenuOf(m_parentMenu
);
239 wxCHECK_RET( hMenu
, wxT("menuitem without menu") );
242 m_parentMenu
->UpdateAccel(this);
243 #endif // wxUSE_ACCEL
245 UINT id
= GetRealId();
246 UINT flagsOld
= ::GetMenuState(hMenu
, id
, MF_BYCOMMAND
);
247 if ( flagsOld
== 0xFFFFFFFF )
249 wxLogLastError(wxT("GetMenuState"));
255 // high byte contains the number of items in a submenu for submenus
257 flagsOld
|= MF_POPUP
;
262 #if wxUSE_OWNER_DRAWN
263 if ( IsOwnerDrawn() )
265 flagsOld
|= MF_OWNERDRAW
;
266 data
= (LPCTSTR
)this;
271 flagsOld
|= MF_STRING
;
272 data
= (wxChar
*) text
.c_str();
275 if ( ::ModifyMenu(hMenu
, id
,
276 MF_BYCOMMAND
| flagsOld
,
277 id
, data
) == (int)0xFFFFFFFF )
279 wxLogLastError(wxT("ModifyMenu"));
284 void wxMenuItem::SetCheckable(bool checkable
)
286 wxMenuItemBase::SetCheckable(checkable
);
287 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable
) );
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
296 const wxString
& name
,
297 const wxString
& help
,
301 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
304 #endif // wxUSE_MENUS