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