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