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