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