]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: menuitem.cpp | |
3 | // Purpose: wxMenuItem implementation | |
4 | // Author: Vadim Zeitlin | |
c2dcfdef | 5 | // Modified by: |
2bda0e17 KB |
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 | ||
c2dcfdef VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
a3b46648 | 20 | #ifdef __GNUG__ |
c2dcfdef | 21 | #pragma implementation "menuitem.h" |
a3b46648 | 22 | #endif |
2bda0e17 KB |
23 | |
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
c2dcfdef | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
c2dcfdef VZ |
32 | #include "wx/font.h" |
33 | #include "wx/bitmap.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/font.h" | |
4e938f5b | 36 | #include "wx/window.h" |
0c589ad0 BM |
37 | #include "wx/accel.h" |
38 | #include "wx/menu.h" | |
39 | #include "wx/string.h" | |
2bda0e17 KB |
40 | #endif |
41 | ||
2bda0e17 | 42 | #include "wx/menuitem.h" |
6776a0b2 | 43 | #include "wx/log.h" |
2bda0e17 | 44 | |
42e69d6b | 45 | #include "wx/msw/private.h" |
ce3ed50d | 46 | |
c2dcfdef | 47 | // --------------------------------------------------------------------------- |
974e8d94 | 48 | // macro |
c2dcfdef VZ |
49 | // --------------------------------------------------------------------------- |
50 | ||
974e8d94 | 51 | // hide the ugly cast |
c2dcfdef VZ |
52 | #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu()) |
53 | ||
974e8d94 VZ |
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 | ||
2bda0e17 KB |
61 | // ============================================================================ |
62 | // implementation | |
63 | // ============================================================================ | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // dynamic classes implementation | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | #if !defined(USE_SHARED_LIBRARY) || !USE_SHARED_LIBRARY | |
974e8d94 VZ |
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 | |
2bda0e17 KB |
75 | #endif //USE_SHARED_LIBRARY |
76 | ||
77 | // ---------------------------------------------------------------------------- | |
78 | // wxMenuItem | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
81 | // ctor & dtor | |
82 | // ----------- | |
83 | ||
974e8d94 VZ |
84 | wxMenuItem::wxMenuItem(wxMenu *pParentMenu, |
85 | int id, | |
86 | const wxString& text, | |
87 | const wxString& strHelp, | |
2bda0e17 KB |
88 | bool bCheckable, |
89 | wxMenu *pSubMenu) : | |
47d67540 | 90 | #if wxUSE_OWNER_DRAWN |
974e8d94 VZ |
91 | wxOwnerDrawn(text, bCheckable) |
92 | #endif // owner drawn | |
2bda0e17 | 93 | { |
223d09f6 | 94 | wxASSERT_MSG( pParentMenu != NULL, wxT("a menu item should have a parent") ); |
2bda0e17 | 95 | |
47d67540 | 96 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
97 | // set default menu colors |
98 | #define SYS_COLOR(c) (wxSystemSettings::GetSystemColour(wxSYS_COLOUR_##c)) | |
c2dcfdef | 99 | |
2bda0e17 KB |
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 | |
974e8d94 VZ |
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; | |
2bda0e17 KB |
117 | } |
118 | ||
c2dcfdef | 119 | wxMenuItem::~wxMenuItem() |
2bda0e17 KB |
120 | { |
121 | } | |
122 | ||
123 | // misc | |
124 | // ---- | |
125 | ||
c2dcfdef VZ |
126 | // return the id for calling Win32 API functions |
127 | int wxMenuItem::GetRealId() const | |
128 | { | |
974e8d94 | 129 | return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId(); |
c2dcfdef VZ |
130 | } |
131 | ||
2bda0e17 | 132 | // delete the sub menu |
c2dcfdef | 133 | // ------------------- |
2bda0e17 KB |
134 | void wxMenuItem::DeleteSubMenu() |
135 | { | |
974e8d94 VZ |
136 | delete m_subMenu; |
137 | m_subMenu = NULL; | |
2bda0e17 KB |
138 | } |
139 | ||
3dfac970 VZ |
140 | // get item state |
141 | // -------------- | |
142 | ||
a8cfd0cb | 143 | bool wxMenuItem::IsChecked() const |
3dfac970 | 144 | { |
a8cfd0cb | 145 | int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND); |
3dfac970 VZ |
146 | |
147 | // don't "and" with MF_ENABLED because its value is 0 | |
148 | return (flag & MF_DISABLED) == 0; | |
149 | } | |
150 | ||
2bda0e17 KB |
151 | // change item state |
152 | // ----------------- | |
153 | ||
154 | void wxMenuItem::Enable(bool bDoEnable) | |
155 | { | |
974e8d94 VZ |
156 | if ( m_isEnabled != bDoEnable ) { |
157 | long rc = EnableMenuItem(GetHMenuOf(m_parentMenu), | |
c2dcfdef VZ |
158 | GetRealId(), |
159 | MF_BYCOMMAND | | |
160 | (bDoEnable ? MF_ENABLED : MF_GRAYED)); | |
2bda0e17 | 161 | |
c2dcfdef VZ |
162 | if ( rc == -1 ) { |
163 | wxLogLastError("EnableMenuItem"); | |
164 | } | |
165 | ||
974e8d94 | 166 | wxMenuItemBase::Enable(m_isEnabled); |
c2dcfdef | 167 | } |
2bda0e17 KB |
168 | } |
169 | ||
170 | void wxMenuItem::Check(bool bDoCheck) | |
171 | { | |
974e8d94 | 172 | wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") ); |
c2dcfdef | 173 | |
974e8d94 VZ |
174 | if ( m_isChecked != bDoCheck ) { |
175 | long rc = CheckMenuItem(GetHMenuOf(m_parentMenu), | |
c2dcfdef VZ |
176 | GetId(), |
177 | MF_BYCOMMAND | | |
178 | (bDoCheck ? MF_CHECKED : MF_UNCHECKED)); | |
179 | ||
180 | if ( rc == -1 ) { | |
181 | wxLogLastError("CheckMenuItem"); | |
182 | } | |
183 | ||
974e8d94 | 184 | wxMenuItemBase::Check(m_isChecked); |
c2dcfdef VZ |
185 | } |
186 | } | |
187 | ||
974e8d94 | 188 | void wxMenuItem::SetText(const wxString& text) |
c2dcfdef VZ |
189 | { |
190 | // don't do anything if label didn't change | |
974e8d94 | 191 | if ( m_text == text ) |
c2dcfdef VZ |
192 | return; |
193 | ||
974e8d94 VZ |
194 | wxMenuItemBase::SetText(text); |
195 | OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) ); | |
c2dcfdef | 196 | |
974e8d94 | 197 | HMENU hMenu = GetHMenuOf(m_parentMenu); |
2bda0e17 | 198 | |
c2dcfdef VZ |
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 | ||
837e5743 | 214 | LPCTSTR data; |
974e8d94 | 215 | |
c2dcfdef VZ |
216 | #if wxUSE_OWNER_DRAWN |
217 | if ( IsOwnerDrawn() ) | |
218 | { | |
219 | flagsOld |= MF_OWNERDRAW; | |
837e5743 | 220 | data = (LPCTSTR)this; |
c2dcfdef VZ |
221 | } |
222 | else | |
223 | #endif //owner drawn | |
224 | { | |
225 | flagsOld |= MF_STRING; | |
974e8d94 | 226 | data = (char*) text.c_str(); |
c2dcfdef VZ |
227 | } |
228 | ||
229 | if ( ::ModifyMenu(hMenu, id, | |
230 | MF_BYCOMMAND | flagsOld, | |
a17e237f | 231 | id, data) == (int)0xFFFFFFFF ) |
c2dcfdef | 232 | { |
223d09f6 | 233 | wxLogLastError(wxT("ModifyMenu")); |
c2dcfdef VZ |
234 | } |
235 | } | |
236 | } | |
2bda0e17 | 237 | |
974e8d94 VZ |
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 | } |