]> git.saurik.com Git - wxWidgets.git/blob - src/os2/menuitem.cpp
compilation fix
[wxWidgets.git] / src / os2 / menuitem.cpp
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 // static function for translating menu labels
59 // ----------------------------------------------------------------------------
60
61 static wxString TextToLabel(const wxString& rTitle)
62 {
63 wxString Title;
64 const wxChar *pc;
65 for (pc = rTitle.c_str(); *pc != wxT('\0'); pc++ )
66 {
67 if (*pc == wxT('&') )
68 {
69 if (*(pc+1) == wxT('&'))
70 {
71 pc++;
72 Title << wxT('&');
73 }
74 else
75 Title << wxT('~');
76 }
77 else
78 {
79 if ( *pc == wxT('~') )
80 {
81 // tildes must be doubled to prevent them from being
82 // interpreted as accelerator character prefix by PM ???
83 Title << *pc;
84 }
85 Title << *pc;
86 }
87 }
88 return Title;
89 }
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // dynamic classes implementation
97 // ----------------------------------------------------------------------------
98
99 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
100
101 // ----------------------------------------------------------------------------
102 // wxMenuItem
103 // ----------------------------------------------------------------------------
104
105 // ctor & dtor
106 // -----------
107
108 wxMenuItem::wxMenuItem(
109 wxMenu* pParentMenu
110 , int nId
111 , const wxString& rText
112 , const wxString& rStrHelp
113 , bool bCheckable
114 , wxMenu* pSubMenu
115 )
116 #if wxUSE_OWNER_DRAWN
117 : wxOwnerDrawn( TextToLabel(rText)
118 ,bCheckable
119 )
120 #endif // owner drawn
121 {
122 wxASSERT_MSG(pParentMenu != NULL, wxT("a menu item should have a parent"));
123
124 #if wxUSE_OWNER_DRAWN
125
126 //
127 // Set default menu colors
128 //
129 #define SYS_COLOR(c) (wxSystemSettings::GetColour(wxSYS_COLOUR_##c))
130
131 SetTextColour(SYS_COLOR(MENUTEXT));
132 SetBackgroundColour(SYS_COLOR(MENU));
133
134 //
135 // We don't want normal items be owner-drawn
136 //
137 ResetOwnerDrawn();
138
139 #undef SYS_COLOR
140 #endif // wxUSE_OWNER_DRAWN
141
142 m_parentMenu = pParentMenu;
143 m_subMenu = pSubMenu;
144 m_isEnabled = TRUE;
145 m_isChecked = FALSE;
146 m_id = nId;
147 m_text = TextToLabel(rText);
148 m_isCheckable = bCheckable;
149 m_help = rStrHelp;
150 memset(&m_vMenuData, '\0', sizeof(m_vMenuData));
151 m_vMenuData.id= nId;
152 } // end of wxMenuItem::wxMenuItem
153
154 wxMenuItem::~wxMenuItem()
155 {
156 } // end of wxMenuItem::~wxMenuItem
157
158 //
159 // Misc
160 // ----
161
162 //
163 // Return the id for calling Win32 API functions
164 //
165 int wxMenuItem::GetRealId() const
166 {
167 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
168 } // end of wxMenuItem::GetRealId
169
170 //
171 // Get item state
172 // --------------
173 bool wxMenuItem::IsChecked() const
174 {
175 USHORT uFlag = SHORT1FROMMR(::WinSendMsg( GetHMenuOf(m_parentMenu)
176 ,MM_QUERYITEMATTR
177 ,MPFROM2SHORT(GetId(), TRUE)
178 ,MPFROMSHORT(MIA_CHECKED)
179 ));
180
181 return (uFlag & MIA_CHECKED);
182 } // end of wxMenuItem::IsChecked
183
184 wxString wxMenuItemBase::GetLabelFromText(
185 const wxString& rText
186 )
187 {
188 wxString label;
189 for ( const wxChar *pc = rText.c_str(); *pc; pc++ )
190 {
191 if ( *pc == wxT('~') || *pc == wxT('&') )
192 {
193 // '~' is the escape character for GTK+ and '&' is the one for
194 // wxWindows - skip both of them
195 continue;
196 }
197
198 label += *pc;
199 }
200 return label;
201 }
202
203 // change item state
204 // -----------------
205
206 void wxMenuItem::Enable(
207 bool bEnable
208 )
209 {
210 bool bOk;
211
212 if (m_isEnabled == bEnable)
213 return;
214 if (bEnable)
215 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
216 ,MM_SETITEMATTR
217 ,MPFROM2SHORT(GetRealId(), TRUE)
218 ,MPFROM2SHORT(MIA_DISABLED, FALSE)
219 );
220 else
221 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
222 ,MM_SETITEMATTR
223 ,MPFROM2SHORT(GetRealId(), TRUE)
224 ,MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED)
225 );
226 if (!bOk)
227 {
228 wxLogLastError("EnableMenuItem");
229 }
230 wxMenuItemBase::Enable(bEnable);
231 } // end of wxMenuItem::Enable
232
233 void wxMenuItem::Check(
234 bool bCheck
235 )
236 {
237 bool bOk;
238
239 wxCHECK_RET( m_isCheckable, wxT("only checkable items may be checked") );
240 if (m_isChecked == bCheck)
241 return;
242 if (bCheck)
243 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
244 ,MM_SETITEMATTR
245 ,MPFROM2SHORT(GetRealId(), TRUE)
246 ,MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED)
247 );
248 else
249 bOk = (bool)::WinSendMsg( GetHMenuOf(m_parentMenu)
250 ,MM_SETITEMATTR
251 ,MPFROM2SHORT(GetRealId(), TRUE)
252 ,MPFROM2SHORT(MIA_CHECKED, FALSE)
253 );
254 if (!bOk)
255 {
256 wxLogLastError("CheckMenuItem");
257 }
258 wxMenuItemBase::Check(bCheck);
259 } // end of wxMenuItem::Check
260
261 void wxMenuItem::SetText(
262 const wxString& rText
263 )
264 {
265 //
266 // Don't do anything if label didn't change
267 //
268
269 wxString Text = TextToLabel(rText);
270 if (m_text == Text)
271 return;
272
273 wxMenuItemBase::SetText(Text);
274 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetName(Text));
275
276 HWND hMenu = GetHMenuOf(m_parentMenu);
277
278 wxCHECK_RET(hMenu, wxT("menuitem without menu"));
279
280 #if wxUSE_ACCEL
281 m_parentMenu->UpdateAccel(this);
282 #endif // wxUSE_ACCEL
283
284 USHORT uId = GetRealId();
285 MENUITEM vItem;
286 USHORT uFlagsOld;
287
288 if (!::WinSendMsg( hMenu
289 ,MM_QUERYITEM
290 ,MPFROM2SHORT(uId, TRUE)
291 ,(MPARAM)&vItem
292 ))
293 {
294 wxLogLastError("GetMenuState");
295 }
296 else
297 {
298 uFlagsOld = vItem.afStyle;
299 if (IsSubMenu())
300 {
301 uFlagsOld |= MIS_SUBMENU;
302 }
303
304 BYTE* pData;
305
306 #if wxUSE_OWNER_DRAWN
307 if (IsOwnerDrawn())
308 {
309 uFlagsOld |= MIS_OWNERDRAW;
310 pData = (BYTE*)this;
311 }
312 else
313 #endif //owner drawn
314 {
315 uFlagsOld |= MIS_TEXT;
316 pData = (BYTE*)Text.c_str();
317 }
318
319 //
320 // Set the style
321 //
322 if (!::WinSendMsg( hMenu
323 ,MM_SETITEM
324 ,MPFROM2SHORT(uId, TRUE)
325 ,(MPARAM)&vItem
326 ))
327 {
328 wxLogLastError(wxT("ModifyMenu"));
329 }
330
331 //
332 // Set the text
333 //
334 if (::WinSendMsg( hMenu
335 ,MM_SETITEMTEXT
336 ,MPFROMSHORT(uId)
337 ,(MPARAM)pData
338 ))
339 {
340 wxLogLastError(wxT("ModifyMenu"));
341 }
342 }
343 } // end of wxMenuItem::SetText
344
345 void wxMenuItem::SetCheckable(
346 bool bCheckable
347 )
348 {
349 wxMenuItemBase::SetCheckable(bCheckable);
350 OWNER_DRAWN_ONLY(wxOwnerDrawn::SetCheckable(bCheckable));
351 } // end of wxMenuItem::SetCheckable
352
353 // ----------------------------------------------------------------------------
354 // wxMenuItemBase
355 // ----------------------------------------------------------------------------
356
357 wxMenuItem* wxMenuItemBase::New(
358 wxMenu* pParentMenu
359 , int nId
360 , const wxString& rName
361 , const wxString& rHelp
362 , bool bIsCheckable
363 , wxMenu* pSubMenu
364 )
365 {
366 return new wxMenuItem( pParentMenu
367 ,nId
368 ,rName
369 ,rHelp
370 ,bIsCheckable
371 ,pSubMenu
372 );
373 } // end of wxMenuItemBase::New
374