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