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