]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
wxMenuBarBase for MSW (untested)
[wxWidgets.git] / src / msw / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 11.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "menuitem.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/font.h"
33 #include "wx/bitmap.h"
34 #include "wx/settings.h"
35 #include "wx/font.h"
36 #include "wx/window.h"
37 #include "wx/accel.h"
38 #include "wx/menu.h"
39 #include "wx/string.h"
40 #endif
41
42 #include "wx/menuitem.h"
43 #include "wx/log.h"
44
45 #include "wx/msw/private.h"
46
47 // ---------------------------------------------------------------------------
48 // macro
49 // ---------------------------------------------------------------------------
50
51 // hide the ugly cast
52 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
53
54 // conditional compilation
55 #if wxUSE_OWNER_DRAWN
56 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
57 #else // !wxUSE_OWNER_DRAWN
58 #define OWNER_DRAWN_ONLY( code )
59 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // dynamic classes implementation
67 // ----------------------------------------------------------------------------
68
69 #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY
70 #if wxUSE_OWNER_DRAWN
71 IMPLEMENT_DYNAMIC_CLASS2(wxMenuItem, wxMenuItemBase, wxOwnerDrawn)
72 #else //!USE_OWNER_DRAWN
73 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
74 #endif //USE_OWNER_DRAWN
75 #endif //USE_SHARED_LIBRARY
76
77 // ----------------------------------------------------------------------------
78 // wxMenuItem
79 // ----------------------------------------------------------------------------
80
81 // ctor & dtor
82 // -----------
83
84 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
85 int id,
86 const wxString& text,
87 const wxString& strHelp,
88 bool bCheckable,
89 wxMenu *pSubMenu) :
90 #if wxUSE_OWNER_DRAWN
91 wxOwnerDrawn(text, bCheckable)
92 #endif // owner drawn
93 {
94 wxASSERT_MSG( pParentMenu != NULL, wxT("a menu item should have a parent") );
95
96 #if wxUSE_OWNER_DRAWN
97 // set default menu colors
98 #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c))
99
100 SetTextColour(SYS_COLOR(MENUTEXT));
101 SetBackgroundColour(SYS_COLOR(MENU));
102
103 // we don't want normal items be owner-drawn
104 ResetOwnerDrawn();
105
106 #undef SYS_COLOR
107 #endif // wxUSE_OWNER_DRAWN
108
109 m_parentMenu = pParentMenu;
110 m_subMenu = pSubMenu;
111 m_isEnabled = TRUE;
112 m_isChecked = FALSE;
113 m_id = id;
114 m_text = text;
115 m_isCheckable = bCheckable;
116 m_help = strHelp;
117 }
118
119 wxMenuItem::~wxMenuItem()
120 {
121 }
122
123 // misc
124 // ----
125
126 // return the id for calling Win32 API functions
127 int wxMenuItem::GetRealId() const
128 {
129 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
130 }
131
132 // delete the sub menu
133 // -------------------
134 void wxMenuItem::DeleteSubMenu()
135 {
136 delete m_subMenu;
137 m_subMenu = NULL;
138 }
139
140 // get item state
141 // --------------
142
143 bool wxMenuItem::IsChecked() const
144 {
145 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
146
147 // don't "and" with MF_ENABLED because its value is 0
148 return (flag & MF_DISABLED) == 0;
149 }
150
151 // change item state
152 // -----------------
153
154 void wxMenuItem::Enable(bool bDoEnable)
155 {
156 if ( m_isEnabled != bDoEnable ) {
157 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
158 GetRealId(),
159 MF_BYCOMMAND |
160 (bDoEnable ? MF_ENABLED : MF_GRAYED));
161
162 if ( rc == -1 ) {
163 wxLogLastError("EnableMenuItem");
164 }
165
166 wxMenuItemBase::Enable(m_isEnabled);
167 }
168 }
169
170 void wxMenuItem::Check(bool bDoCheck)
171 {
172 wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") );
173
174 if ( m_isChecked != bDoCheck ) {
175 long rc = CheckMenuItem(GetHMenuOf(m_parentMenu),
176 GetId(),
177 MF_BYCOMMAND |
178 (bDoCheck ? MF_CHECKED : MF_UNCHECKED));
179
180 if ( rc == -1 ) {
181 wxLogLastError("CheckMenuItem");
182 }
183
184 wxMenuItemBase::Check(m_isChecked);
185 }
186 }
187
188 void wxMenuItem::SetText(const wxString& text)
189 {
190 // don't do anything if label didn't change
191 if ( m_text == text )
192 return;
193
194 wxMenuItemBase::SetText(text);
195 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
196
197 HMENU hMenu = GetHMenuOf(m_parentMenu);
198
199 UINT id = GetRealId();
200 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
201 if ( flagsOld == 0xFFFFFFFF )
202 {
203 wxLogLastError("GetMenuState");
204 }
205 else
206 {
207 if ( IsSubMenu() )
208 {
209 // high byte contains the number of items in a submenu for submenus
210 flagsOld &= 0xFF;
211 flagsOld |= MF_POPUP;
212 }
213
214 LPCTSTR data;
215
216 #if wxUSE_OWNER_DRAWN
217 if ( IsOwnerDrawn() )
218 {
219 flagsOld |= MF_OWNERDRAW;
220 data = (LPCTSTR)this;
221 }
222 else
223 #endif //owner drawn
224 {
225 flagsOld |= MF_STRING;
226 data = (char*) text.c_str();
227 }
228
229 if ( ::ModifyMenu(hMenu, id,
230 MF_BYCOMMAND | flagsOld,
231 id, data) == (int)0xFFFFFFFF )
232 {
233 wxLogLastError(wxT("ModifyMenu"));
234 }
235 }
236 }
237
238 void wxMenuItem::SetCheckable(bool checkable)
239 {
240 wxMenuItemBase::SetCheckable(checkable);
241 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
242 }
243
244 // ----------------------------------------------------------------------------
245 // wxMenuItemBase
246 // ----------------------------------------------------------------------------
247
248 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
249 int id,
250 const wxString& name,
251 const wxString& help,
252 bool isCheckable,
253 wxMenu *subMenu)
254 {
255 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
256 }