]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menuitem.cpp
no need to %import gdi.i
[wxWidgets.git] / src / msw / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 11.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_MENUS
28
29 #include "wx/menuitem.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/font.h"
33 #include "wx/bitmap.h"
34 #include "wx/settings.h"
35 #include "wx/window.h"
36 #include "wx/accel.h"
37 #include "wx/string.h"
38 #include "wx/log.h"
39 #include "wx/menu.h"
40 #endif
41
42 #if wxUSE_ACCEL
43 #include "wx/accel.h"
44 #endif // wxUSE_ACCEL
45
46 #include "wx/msw/private.h"
47
48 #ifdef __WXWINCE__
49 // Implemented in menu.cpp
50 UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ;
51 #endif
52
53 // ---------------------------------------------------------------------------
54 // macro
55 // ---------------------------------------------------------------------------
56
57 // hide the ugly cast
58 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
59
60 // conditional compilation
61 #if wxUSE_OWNER_DRAWN
62 #define OWNER_DRAWN_ONLY( code ) if ( IsOwnerDrawn() ) code
63 #else // !wxUSE_OWNER_DRAWN
64 #define OWNER_DRAWN_ONLY( code )
65 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // dynamic classes implementation
73 // ----------------------------------------------------------------------------
74
75 #if wxUSE_EXTENDED_RTTI
76
77 bool wxMenuItemStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & )
78 {
79 const wxMenuItem * mitem = dynamic_cast<const wxMenuItem*>(object) ;
80 if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().empty() )
81 {
82 // we don't stream out the first two items for menus with a title, they will be reconstructed
83 if ( mitem->GetMenu()->FindItemByPosition(0) == mitem || mitem->GetMenu()->FindItemByPosition(1) == mitem )
84 return false ;
85 }
86 return true ;
87 }
88
89 wxBEGIN_ENUM( wxItemKind )
90 wxENUM_MEMBER( wxITEM_SEPARATOR )
91 wxENUM_MEMBER( wxITEM_NORMAL )
92 wxENUM_MEMBER( wxITEM_CHECK )
93 wxENUM_MEMBER( wxITEM_RADIO )
94 wxEND_ENUM( wxItemKind )
95
96 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem, wxObject,"wx/menuitem.h",wxMenuItemStreamingCallback)
97
98 wxBEGIN_PROPERTIES_TABLE(wxMenuItem)
99 wxPROPERTY( Parent,wxMenu*, SetMenu, GetMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
100 wxPROPERTY( Id,int, SetId, GetId, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
101 wxPROPERTY( Text, wxString , SetText, GetText, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
102 wxPROPERTY( Help, wxString , SetHelp, GetHelp, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
103 wxREADONLY_PROPERTY( Kind, wxItemKind , GetKind , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
104 wxPROPERTY( SubMenu,wxMenu*, SetSubMenu, GetSubMenu, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
105 wxPROPERTY( Enabled , bool , Enable , IsEnabled , wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
106 wxPROPERTY( Checked , bool , Check , IsChecked , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
107 wxPROPERTY( Checkable , bool , SetCheckable , IsCheckable , wxxVariant((bool)false) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
108 wxEND_PROPERTIES_TABLE()
109
110 wxBEGIN_HANDLERS_TABLE(wxMenuItem)
111 wxEND_HANDLERS_TABLE()
112
113 wxDIRECT_CONSTRUCTOR_6( wxMenuItem , wxMenu* , Parent , int , Id , wxString , Text , wxString , Help , wxItemKind , Kind , wxMenu* , SubMenu )
114 #else
115 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
116 #endif
117
118 // ----------------------------------------------------------------------------
119 // wxMenuItem
120 // ----------------------------------------------------------------------------
121
122 // ctor & dtor
123 // -----------
124
125 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
126 int id,
127 const wxString& text,
128 const wxString& strHelp,
129 wxItemKind kind,
130 wxMenu *pSubMenu)
131 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
132 #if wxUSE_OWNER_DRAWN
133 , wxOwnerDrawn(text, kind == wxITEM_CHECK, true)
134 #endif // owner drawn
135 {
136 Init();
137 }
138
139 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
140 int id,
141 const wxString& text,
142 const wxString& help,
143 bool isCheckable,
144 wxMenu *subMenu)
145 : wxMenuItemBase(parentMenu, id, text, help,
146 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
147 #if wxUSE_OWNER_DRAWN
148 , wxOwnerDrawn(text, isCheckable, true)
149 #endif // owner drawn
150 {
151 Init();
152 }
153
154 void wxMenuItem::Init()
155 {
156 m_radioGroup.start = -1;
157 m_isRadioGroupStart = false;
158
159 #if wxUSE_OWNER_DRAWN
160
161 // when the color is not valid, wxOwnerDraw takes the default ones.
162 // If we set the colors here and they are changed by the user during
163 // the execution, then the colors are not updated until the application
164 // is restarted and our menus look bad
165 SetTextColour(wxNullColour);
166 SetBackgroundColour(wxNullColour);
167
168 // setting default colors switched ownerdraw on: switch it off again
169 ResetOwnerDrawn();
170
171 // switch ownerdraw back on if using a non default margin
172 if ( GetId() != wxID_SEPARATOR )
173 SetMarginWidth(GetMarginWidth());
174
175 // tell the owner drawing code to show the accel string as well
176 SetAccelString(m_text.AfterFirst(_T('\t')));
177 #endif // wxUSE_OWNER_DRAWN
178 }
179
180 wxMenuItem::~wxMenuItem()
181 {
182 }
183
184 // misc
185 // ----
186
187 // return the id for calling Win32 API functions
188 int wxMenuItem::GetRealId() const
189 {
190 return m_subMenu ? (int)m_subMenu->GetHMenu() : GetId();
191 }
192
193 // get item state
194 // --------------
195
196 bool wxMenuItem::IsChecked() const
197 {
198 // fix that RTTI is always getting the correct state (separators cannot be checked, but the call below
199 // returns true
200 if ( GetId() == wxID_SEPARATOR )
201 return false ;
202
203 int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
204
205 return (flag & MF_CHECKED) != 0;
206 }
207
208 /* static */
209 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
210 {
211 return wxStripMenuCodes(text);
212 }
213
214 // radio group stuff
215 // -----------------
216
217 void wxMenuItem::SetAsRadioGroupStart()
218 {
219 m_isRadioGroupStart = true;
220 }
221
222 void wxMenuItem::SetRadioGroupStart(int start)
223 {
224 wxASSERT_MSG( !m_isRadioGroupStart,
225 _T("should only be called for the next radio items") );
226
227 m_radioGroup.start = start;
228 }
229
230 void wxMenuItem::SetRadioGroupEnd(int end)
231 {
232 wxASSERT_MSG( m_isRadioGroupStart,
233 _T("should only be called for the first radio item") );
234
235 m_radioGroup.end = end;
236 }
237
238 // change item state
239 // -----------------
240
241 void wxMenuItem::Enable(bool enable)
242 {
243 if ( m_isEnabled == enable )
244 return;
245
246 long rc = EnableMenuItem(GetHMenuOf(m_parentMenu),
247 GetRealId(),
248 MF_BYCOMMAND |
249 (enable ? MF_ENABLED : MF_GRAYED));
250
251 if ( rc == -1 ) {
252 wxLogLastError(wxT("EnableMenuItem"));
253 }
254
255 wxMenuItemBase::Enable(enable);
256 }
257
258 void wxMenuItem::Check(bool check)
259 {
260 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
261
262 if ( m_isChecked == check )
263 return;
264
265 int flags = check ? MF_CHECKED : MF_UNCHECKED;
266 HMENU hmenu = GetHMenuOf(m_parentMenu);
267
268 if ( GetKind() == wxITEM_RADIO )
269 {
270 // it doesn't make sense to uncheck a radio item - what would this do?
271 if ( !check )
272 return;
273
274 // get the index of this item in the menu
275 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
276 int pos = items.IndexOf(this);
277 wxCHECK_RET( pos != wxNOT_FOUND,
278 _T("menuitem not found in the menu items list?") );
279
280 // get the radio group range
281 int start,
282 end;
283
284 if ( m_isRadioGroupStart )
285 {
286 // we already have all information we need
287 start = pos;
288 end = m_radioGroup.end;
289 }
290 else // next radio group item
291 {
292 // get the radio group end from the start item
293 start = m_radioGroup.start;
294 end = items.Item(start)->GetData()->m_radioGroup.end;
295 }
296
297 #ifdef __WIN32__
298 // calling CheckMenuRadioItem() with such parameters hangs my system
299 // (NT4 SP6) and I suspect this could happen to the others as well - so
300 // don't do it!
301 wxCHECK_RET( start != -1 && end != -1,
302 _T("invalid ::CheckMenuRadioItem() parameter(s)") );
303
304 if ( !::CheckMenuRadioItem(hmenu,
305 start, // the first radio group item
306 end, // the last one
307 pos, // the one to check
308 MF_BYPOSITION) )
309 {
310 wxLogLastError(_T("CheckMenuRadioItem"));
311 }
312 #endif // __WIN32__
313
314 // also uncheck all the other items in this radio group
315 wxMenuItemList::compatibility_iterator node = items.Item(start);
316 for ( int n = start; n <= end && node; n++ )
317 {
318 if ( n != pos )
319 {
320 node->GetData()->m_isChecked = false;
321 }
322
323 node = node->GetNext();
324 }
325 }
326 else // check item
327 {
328 if ( ::CheckMenuItem(hmenu,
329 GetRealId(),
330 MF_BYCOMMAND | flags) == (DWORD)-1 )
331 {
332 wxFAIL_MSG( _T("CheckMenuItem() failed, item not in the menu?") );
333 }
334 }
335
336 wxMenuItemBase::Check(check);
337 }
338
339 void wxMenuItem::SetText(const wxString& text)
340 {
341 // don't do anything if label didn't change
342 if ( m_text == text )
343 return;
344
345 wxMenuItemBase::SetText(text);
346 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) );
347 #if wxUSE_OWNER_DRAWN
348 // tell the owner drawing code to to show the accel string as well
349 SetAccelString(text.AfterFirst(_T('\t')));
350 #endif
351
352 HMENU hMenu = GetHMenuOf(m_parentMenu);
353 wxCHECK_RET( hMenu, wxT("menuitem without menu") );
354
355 #if wxUSE_ACCEL
356 m_parentMenu->UpdateAccel(this);
357 #endif // wxUSE_ACCEL
358
359 UINT id = GetRealId();
360 UINT flagsOld = ::GetMenuState(hMenu, id, MF_BYCOMMAND);
361 if ( flagsOld == 0xFFFFFFFF )
362 {
363 // It's not an error, it means that the menu item doesn't exist
364 //wxLogLastError(wxT("GetMenuState"));
365 }
366 else
367 {
368 if ( IsSubMenu() )
369 {
370 // high byte contains the number of items in a submenu for submenus
371 flagsOld &= 0xFF;
372 flagsOld |= MF_POPUP;
373 }
374
375 LPCTSTR data;
376
377 #if wxUSE_OWNER_DRAWN
378 if ( IsOwnerDrawn() )
379 {
380 flagsOld |= MF_OWNERDRAW;
381 data = (LPCTSTR)this;
382 }
383 else
384 #endif //owner drawn
385 {
386 flagsOld |= MF_STRING;
387 data = (wxChar*) text.c_str();
388 }
389
390 #ifdef __WXWINCE__
391 // FIXME: complete this, applying the old
392 // flags.
393 // However, the WinCE doc for SetMenuItemInfo
394 // says that you can't use it to set the menu
395 // item state; only data, id and type.
396 MENUITEMINFO info;
397 wxZeroMemory(info);
398 info.cbSize = sizeof(info);
399 info.fMask = MIIM_TYPE;
400 info.fType = MFT_STRING;
401 info.cch = text.length();
402 info.dwTypeData = (LPTSTR) data ;
403 if ( !::SetMenuItemInfo(hMenu, id, FALSE, & info) )
404 {
405 wxLogLastError(wxT("SetMenuItemInfo"));
406 }
407 #else
408 if ( ::ModifyMenu(hMenu, id,
409 MF_BYCOMMAND | flagsOld,
410 id, data) == (int)0xFFFFFFFF )
411 {
412 wxLogLastError(wxT("ModifyMenu"));
413 }
414 #endif
415 }
416 }
417
418 void wxMenuItem::SetCheckable(bool checkable)
419 {
420 wxMenuItemBase::SetCheckable(checkable);
421 OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) );
422 }
423
424 // ----------------------------------------------------------------------------
425 // wxMenuItemBase
426 // ----------------------------------------------------------------------------
427
428 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
429 int id,
430 const wxString& name,
431 const wxString& help,
432 wxItemKind kind,
433 wxMenu *subMenu)
434 {
435 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
436 }
437
438 #endif // wxUSE_MENUS