]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
ab1ada12200adf415a6d169abfea98dd8a0ee63a
[wxWidgets.git] / src / msw / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menu.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
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/menu.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/frame.h"
33 #include "wx/utils.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/image.h"
37 #endif
38
39 #if wxUSE_OWNER_DRAWN
40 #include "wx/ownerdrw.h"
41 #endif
42
43 #include "wx/scopedarray.h"
44
45 #include "wx/msw/private.h"
46 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
47
48 #ifdef __WXWINCE__
49 #include <windows.h>
50 #include <windowsx.h>
51 #include <tchar.h>
52 #include <ole2.h>
53 #include <shellapi.h>
54 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
55 #include <aygshell.h>
56 #endif
57
58 #include "wx/msw/wince/missing.h"
59
60 #endif
61
62 // other standard headers
63 #include <string.h>
64
65 #if wxUSE_OWNER_DRAWN
66 #include "wx/dynlib.h"
67 #endif
68
69 #ifndef MNS_CHECKORBMP
70 #define MNS_CHECKORBMP 0x04000000
71 #endif
72 #ifndef MIM_STYLE
73 #define MIM_STYLE 0x00000010
74 #endif
75
76 // ----------------------------------------------------------------------------
77 // global variables
78 // ----------------------------------------------------------------------------
79
80 // ----------------------------------------------------------------------------
81 // constants
82 // ----------------------------------------------------------------------------
83
84 // the (popup) menu title has this special id
85 static const UINT idMenuTitle = (UINT)-3;
86
87 // ----------------------------------------------------------------------------
88 // private functions
89 // ----------------------------------------------------------------------------
90
91 namespace
92 {
93
94 // make the given menu item default
95 void SetDefaultMenuItem(HMENU WXUNUSED_IN_WINCE(hmenu),
96 UINT WXUNUSED_IN_WINCE(id))
97 {
98 #ifndef __WXWINCE__
99 MENUITEMINFO mii;
100 wxZeroMemory(mii);
101 mii.cbSize = sizeof(MENUITEMINFO);
102 mii.fMask = MIIM_STATE;
103 mii.fState = MFS_DEFAULT;
104
105 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
106 {
107 wxLogLastError(wxT("SetMenuItemInfo"));
108 }
109 #endif // !__WXWINCE__
110 }
111
112 // make the given menu item owner-drawn
113 void SetOwnerDrawnMenuItem(HMENU WXUNUSED_IN_WINCE(hmenu),
114 UINT WXUNUSED_IN_WINCE(id),
115 ULONG_PTR WXUNUSED_IN_WINCE(data),
116 BOOL WXUNUSED_IN_WINCE(byPositon = FALSE))
117 {
118 #ifndef __WXWINCE__
119 MENUITEMINFO mii;
120 wxZeroMemory(mii);
121 mii.cbSize = sizeof(MENUITEMINFO);
122 mii.fMask = MIIM_FTYPE | MIIM_DATA;
123 mii.fType = MFT_OWNERDRAW;
124 mii.dwItemData = data;
125
126 if ( reinterpret_cast<wxMenuItem*>(data)->IsSeparator() )
127 mii.fType |= MFT_SEPARATOR;
128
129 if ( !::SetMenuItemInfo(hmenu, id, byPositon, &mii) )
130 {
131 wxLogLastError(wxT("SetMenuItemInfo"));
132 }
133 #endif // !__WXWINCE__
134 }
135
136 #ifdef __WXWINCE__
137 UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
138 {
139 MENUITEMINFO info;
140 wxZeroMemory(info);
141 info.cbSize = sizeof(info);
142 info.fMask = MIIM_STATE;
143 // MF_BYCOMMAND is zero so test MF_BYPOSITION
144 if ( !::GetMenuItemInfo(hMenu, id, flags & MF_BYPOSITION ? TRUE : FALSE , & info) )
145 {
146 wxLogLastError(wxT("GetMenuItemInfo"));
147 }
148 return info.fState;
149 }
150 #endif // __WXWINCE__
151
152 inline bool IsGreaterThanStdSize(const wxBitmap& bmp)
153 {
154 return bmp.GetWidth() > ::GetSystemMetrics(SM_CXMENUCHECK) ||
155 bmp.GetHeight() > ::GetSystemMetrics(SM_CYMENUCHECK);
156 }
157
158 } // anonymous namespace
159
160 // ============================================================================
161 // implementation
162 // ============================================================================
163
164 #include "wx/listimpl.cpp"
165
166 WX_DEFINE_LIST( wxMenuInfoList )
167
168 #if wxUSE_EXTENDED_RTTI
169
170 WX_DEFINE_FLAGS( wxMenuStyle )
171
172 wxBEGIN_FLAGS( wxMenuStyle )
173 wxFLAGS_MEMBER(wxMENU_TEAROFF)
174 wxEND_FLAGS( wxMenuStyle )
175
176 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler,"wx/menu.h")
177
178 wxCOLLECTION_TYPE_INFO( wxMenuItem * , wxMenuItemList ) ;
179
180 template<> void wxCollectionToVariantArray( wxMenuItemList const &theList, wxxVariantArray &value)
181 {
182 wxListCollectionToVariantArray<wxMenuItemList::compatibility_iterator>( theList , value ) ;
183 }
184
185 wxBEGIN_PROPERTIES_TABLE(wxMenu)
186 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_MENU_SELECTED , wxCommandEvent)
187 wxPROPERTY( Title, wxString , SetTitle, GetTitle, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
188 wxREADONLY_PROPERTY_FLAGS( MenuStyle , wxMenuStyle , long , GetStyle , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
189 wxPROPERTY_COLLECTION( MenuItems , wxMenuItemList , wxMenuItem* , Append , GetMenuItems , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
190 wxEND_PROPERTIES_TABLE()
191
192 wxBEGIN_HANDLERS_TABLE(wxMenu)
193 wxEND_HANDLERS_TABLE()
194
195 wxDIRECT_CONSTRUCTOR_2( wxMenu , wxString , Title , long , MenuStyle )
196
197 WX_DEFINE_FLAGS( wxMenuBarStyle )
198
199 wxBEGIN_FLAGS( wxMenuBarStyle )
200 wxFLAGS_MEMBER(wxMB_DOCKABLE)
201 wxEND_FLAGS( wxMenuBarStyle )
202
203 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
204 bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxWriter * , wxPersister * , wxxVariantArray & )
205 {
206 return true ;
207 }
208
209 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar, wxWindow ,"wx/menu.h",wxMenuBarStreamingCallback)
210
211 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo, wxObject , "wx/menu.h" )
212
213 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo)
214 wxREADONLY_PROPERTY( Menu , wxMenu* , GetMenu , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
215 wxREADONLY_PROPERTY( Title , wxString , GetTitle , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
216 wxEND_PROPERTIES_TABLE()
217
218 wxBEGIN_HANDLERS_TABLE(wxMenuInfo)
219 wxEND_HANDLERS_TABLE()
220
221 wxCONSTRUCTOR_2( wxMenuInfo , wxMenu* , Menu , wxString , Title )
222
223 wxCOLLECTION_TYPE_INFO( wxMenuInfo * , wxMenuInfoList ) ;
224
225 template<> void wxCollectionToVariantArray( wxMenuInfoList const &theList, wxxVariantArray &value)
226 {
227 wxListCollectionToVariantArray<wxMenuInfoList::compatibility_iterator>( theList , value ) ;
228 }
229
230 wxBEGIN_PROPERTIES_TABLE(wxMenuBar)
231 wxPROPERTY_COLLECTION( MenuInfos , wxMenuInfoList , wxMenuInfo* , Append , GetMenuInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
232 wxEND_PROPERTIES_TABLE()
233
234 wxBEGIN_HANDLERS_TABLE(wxMenuBar)
235 wxEND_HANDLERS_TABLE()
236
237 wxCONSTRUCTOR_DUMMY( wxMenuBar )
238
239 #else
240 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
241 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
242 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject)
243 #endif
244
245 const wxMenuInfoList& wxMenuBar::GetMenuInfos() const
246 {
247 wxMenuInfoList* list = const_cast< wxMenuInfoList* >( &m_menuInfos ) ;
248 WX_CLEAR_LIST( wxMenuInfoList , *list ) ;
249 for( size_t i = 0 ; i < GetMenuCount() ; ++i )
250 {
251 wxMenuInfo* info = new wxMenuInfo() ;
252 info->Create( const_cast<wxMenuBar*>(this)->GetMenu(i) , GetMenuLabel(i) ) ;
253 list->Append( info ) ;
254 }
255 return m_menuInfos ;
256 }
257
258 // ---------------------------------------------------------------------------
259 // wxMenu construction, adding and removing menu items
260 // ---------------------------------------------------------------------------
261
262 // Construct a menu with optional title (then use append)
263 void wxMenu::Init()
264 {
265 m_doBreak = false;
266 m_startRadioGroup = -1;
267
268 #if wxUSE_OWNER_DRAWN
269 m_ownerDrawn = false;
270 m_maxBitmapWidth = 0;
271 m_maxAccelWidth = -1;
272 #endif // wxUSE_OWNER_DRAWN
273
274 // create the menu
275 m_hMenu = (WXHMENU)CreatePopupMenu();
276 if ( !m_hMenu )
277 {
278 wxLogLastError(wxT("CreatePopupMenu"));
279 }
280
281 // if we have a title, insert it in the beginning of the menu
282 if ( !m_title.empty() )
283 {
284 Append(idMenuTitle, m_title);
285 AppendSeparator();
286 }
287 }
288
289 // The wxWindow destructor will take care of deleting the submenus.
290 wxMenu::~wxMenu()
291 {
292 // we should free Windows resources only if Windows doesn't do it for us
293 // which happens if we're attached to a menubar or a submenu of another
294 // menu
295 if ( !IsAttached() && !GetParent() )
296 {
297 if ( !::DestroyMenu(GetHmenu()) )
298 {
299 wxLogLastError(wxT("DestroyMenu"));
300 }
301 }
302
303 #if wxUSE_ACCEL
304 // delete accels
305 WX_CLEAR_ARRAY(m_accels);
306 #endif // wxUSE_ACCEL
307 }
308
309 void wxMenu::Break()
310 {
311 // this will take effect during the next call to Append()
312 m_doBreak = true;
313 }
314
315 void wxMenu::Attach(wxMenuBarBase *menubar)
316 {
317 wxMenuBase::Attach(menubar);
318
319 EndRadioGroup();
320 }
321
322 #if wxUSE_ACCEL
323
324 int wxMenu::FindAccel(int id) const
325 {
326 size_t n, count = m_accels.GetCount();
327 for ( n = 0; n < count; n++ )
328 {
329 if ( m_accels[n]->m_command == id )
330 return n;
331 }
332
333 return wxNOT_FOUND;
334 }
335
336 void wxMenu::UpdateAccel(wxMenuItem *item)
337 {
338 if ( item->IsSubMenu() )
339 {
340 wxMenu *submenu = item->GetSubMenu();
341 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
342 while ( node )
343 {
344 UpdateAccel(node->GetData());
345
346 node = node->GetNext();
347 }
348 }
349 else if ( !item->IsSeparator() )
350 {
351 // recurse upwards: we should only modify m_accels of the top level
352 // menus, not of the submenus as wxMenuBar doesn't look at them
353 // (alternative and arguable cleaner solution would be to recurse
354 // downwards in GetAccelCount() and CopyAccels())
355 if ( GetParent() )
356 {
357 GetParent()->UpdateAccel(item);
358 return;
359 }
360
361 // find the (new) accel for this item
362 wxAcceleratorEntry *accel = wxAcceleratorEntry::Create(item->GetItemLabel());
363 if ( accel )
364 accel->m_command = item->GetId();
365
366 // find the old one
367 int n = FindAccel(item->GetId());
368 if ( n == wxNOT_FOUND )
369 {
370 // no old, add new if any
371 if ( accel )
372 m_accels.Add(accel);
373 else
374 return; // skipping RebuildAccelTable() below
375 }
376 else
377 {
378 // replace old with new or just remove the old one if no new
379 delete m_accels[n];
380 if ( accel )
381 m_accels[n] = accel;
382 else
383 m_accels.RemoveAt(n);
384 }
385
386 if ( IsAttached() )
387 {
388 GetMenuBar()->RebuildAccelTable();
389 }
390
391 ResetMaxAccelWidth();
392 }
393 //else: it is a separator, they can't have accels, nothing to do
394 }
395
396 #endif // wxUSE_ACCEL
397
398 namespace
399 {
400
401 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
402 HBITMAP GetHBitmapForMenu(wxMenuItem *pItem, bool checked = true)
403 {
404 // Under versions of Windows older than Vista we can't pass HBITMAP
405 // directly as hbmpItem for 2 reasons:
406 // 1. We can't draw it with transparency then (this is not
407 // very important now but would be with themed menu bg)
408 // 2. Worse, Windows inverts the bitmap for the selected
409 // item and this looks downright ugly
410 //
411 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
412 // HBMMENU_CALLBACK when inserting it
413 //
414 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
415 // drawn using the classic theme instead of the current one and it does
416 // handle transparency just fine so do use the real bitmap there
417 #if wxUSE_IMAGE
418 if ( wxGetWinVersion() >= wxWinVersion_Vista )
419 {
420 wxBitmap bmp = pItem->GetBitmap(checked);
421 if ( bmp.IsOk() )
422 {
423 // we must use PARGB DIB for the menu bitmaps so ensure that we do
424 wxImage img(bmp.ConvertToImage());
425 if ( !img.HasAlpha() )
426 {
427 img.InitAlpha();
428 pItem->SetBitmap(img, checked);
429 }
430
431 return GetHbitmapOf(pItem->GetBitmap(checked));
432 }
433 //else: bitmap is not set
434
435 return NULL;
436 }
437 #endif // wxUSE_IMAGE
438
439 return HBMMENU_CALLBACK;
440 }
441
442 } // anonymous namespace
443
444 // append a new item or submenu to the menu
445 bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
446 {
447 #if wxUSE_ACCEL
448 UpdateAccel(pItem);
449 #endif // wxUSE_ACCEL
450
451 // we should support disabling the item even prior to adding it to the menu
452 UINT flags = pItem->IsEnabled() ? MF_ENABLED : MF_GRAYED;
453
454 // if "Break" has just been called, insert a menu break before this item
455 // (and don't forget to reset the flag)
456 if ( m_doBreak ) {
457 flags |= MF_MENUBREAK;
458 m_doBreak = false;
459 }
460
461 if ( pItem->IsSeparator() ) {
462 flags |= MF_SEPARATOR;
463 }
464
465 // id is the numeric id for normal menu items and HMENU for submenus as
466 // required by ::AppendMenu() API
467 UINT_PTR id;
468 wxMenu *submenu = pItem->GetSubMenu();
469 if ( submenu != NULL ) {
470 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
471
472 submenu->SetParent(this);
473
474 id = (UINT_PTR)submenu->GetHMenu();
475
476 flags |= MF_POPUP;
477 }
478 else {
479 id = pItem->GetMSWId();
480 }
481
482
483 // prepare to insert the item in the menu
484 wxString itemText = pItem->GetItemLabel();
485 LPCTSTR pData = NULL;
486 if ( pos == (size_t)-1 )
487 {
488 // append at the end (note that the item is already appended to
489 // internal data structures)
490 pos = GetMenuItemCount() - 1;
491 }
492
493 // adjust position to account for the title, if any
494 if ( !m_title.empty() )
495 pos += 2; // for the title itself and its separator
496
497 BOOL ok = false;
498
499 #if wxUSE_OWNER_DRAWN
500 // Under older systems mixing owner-drawn and non-owner-drawn items results
501 // in inconsistent margins, so we force this one to be owner-drawn if any
502 // other items already are.
503 if ( m_ownerDrawn )
504 pItem->SetOwnerDrawn(true);
505 #endif // wxUSE_OWNER_DRAWN
506
507 // check if we have something more than a simple text item
508 #if wxUSE_OWNER_DRAWN
509 if ( pItem->IsOwnerDrawn() )
510 {
511 #ifndef __DMC__
512
513 if ( !m_ownerDrawn && !pItem->IsSeparator() )
514 {
515 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
516 // drawn item under the previous versions and we also have to use
517 // them in any case if the item has custom colours or font
518 static const wxWinVersion winver = wxGetWinVersion();
519 bool mustUseOwnerDrawn = winver < wxWinVersion_98 ||
520 pItem->GetTextColour().Ok() ||
521 pItem->GetBackgroundColour().Ok() ||
522 pItem->GetFont().Ok();
523
524 if ( !mustUseOwnerDrawn )
525 {
526 const wxBitmap& bmpUnchecked = pItem->GetBitmap(false),
527 bmpChecked = pItem->GetBitmap(true);
528
529 if ( (bmpUnchecked.Ok() && IsGreaterThanStdSize(bmpUnchecked)) ||
530 (bmpChecked.Ok() && IsGreaterThanStdSize(bmpChecked)) )
531 {
532 mustUseOwnerDrawn = true;
533 }
534 }
535
536 // use InsertMenuItem() if possible as it's guaranteed to look
537 // correct while our owner-drawn code is not
538 if ( !mustUseOwnerDrawn )
539 {
540 WinStruct<MENUITEMINFO> mii;
541 mii.fMask = MIIM_STRING | MIIM_DATA;
542
543 // don't set hbmpItem for the checkable items as it would
544 // be used for both checked and unchecked state
545 if ( pItem->IsCheckable() )
546 {
547 mii.fMask |= MIIM_CHECKMARKS;
548 mii.hbmpChecked = GetHBitmapForMenu(pItem, true);
549 mii.hbmpUnchecked = GetHBitmapForMenu(pItem, false);
550 }
551 else if ( pItem->GetBitmap().IsOk() )
552 {
553 mii.fMask |= MIIM_BITMAP;
554 mii.hbmpItem = GetHBitmapForMenu(pItem);
555 }
556
557 mii.cch = itemText.length();
558 mii.dwTypeData = const_cast<wxChar *>(itemText.wx_str());
559
560 if ( flags & MF_POPUP )
561 {
562 mii.fMask |= MIIM_SUBMENU;
563 mii.hSubMenu = GetHmenuOf(pItem->GetSubMenu());
564 }
565 else
566 {
567 mii.fMask |= MIIM_ID;
568 mii.wID = id;
569 }
570
571 mii.dwItemData = reinterpret_cast<ULONG_PTR>(pItem);
572
573 ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
574 if ( !ok )
575 {
576 wxLogLastError(wxT("InsertMenuItem()"));
577 }
578 else // InsertMenuItem() ok
579 {
580 // we need to remove the extra indent which is reserved for
581 // the checkboxes by default as it looks ugly unless check
582 // boxes are used together with bitmaps and this is not the
583 // case in wx API
584 WinStruct<MENUINFO> mi;
585
586 // don't call SetMenuInfo() directly, this would prevent
587 // the app from starting up under Windows 95/NT 4
588 typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
589
590 wxDynamicLibrary dllUser(wxT("user32"));
591 wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
592 if ( pfnSetMenuInfo )
593 {
594 mi.fMask = MIM_STYLE;
595 mi.dwStyle = MNS_CHECKORBMP;
596 if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
597 {
598 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
599 }
600 }
601
602 // tell the item that it's not really owner-drawn but only
603 // needs to draw its bitmap, the rest is done by Windows
604 pItem->SetOwnerDrawn(false);
605 }
606 }
607 }
608 #endif // __DMC__
609
610 if ( !ok )
611 {
612 // item draws itself, pass pointer to it in data parameter
613 flags |= MF_OWNERDRAW;
614 pData = (LPCTSTR)pItem;
615
616 bool updateAllMargins = false;
617
618 // get size of bitmap always return valid value (0 for invalid bitmap),
619 // so we don't needed check if bitmap is valid ;)
620 int uncheckedW = pItem->GetBitmap(false).GetWidth();
621 int checkedW = pItem->GetBitmap(true).GetWidth();
622
623 if ( m_maxBitmapWidth < uncheckedW )
624 {
625 m_maxBitmapWidth = uncheckedW;
626 updateAllMargins = true;
627 }
628
629 if ( m_maxBitmapWidth < checkedW )
630 {
631 m_maxBitmapWidth = checkedW;
632 updateAllMargins = true;
633 }
634
635 // make other item ownerdrawn and update margin width for equals alignment
636 if ( !m_ownerDrawn || updateAllMargins )
637 {
638 // we must use position in SetOwnerDrawnMenuItem because
639 // all separators have the same id
640 int pos = 0;
641 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
642 while (node)
643 {
644 wxMenuItem* item = node->GetData();
645
646 if ( !item->IsOwnerDrawn())
647 {
648 item->SetOwnerDrawn(true);
649 SetOwnerDrawnMenuItem(GetHmenu(), pos,
650 reinterpret_cast<ULONG_PTR>(item), TRUE);
651 }
652
653 item->SetMarginWidth(m_maxBitmapWidth);
654
655 node = node->GetNext();
656 pos++;
657 }
658
659 // set menu as ownerdrawn
660 m_ownerDrawn = true;
661
662 ResetMaxAccelWidth();
663 }
664 // only update our margin for equals alignment to other item
665 else if ( !updateAllMargins )
666 {
667 pItem->SetMarginWidth(m_maxBitmapWidth);
668 }
669 }
670 }
671 else
672 #endif // wxUSE_OWNER_DRAWN
673 {
674 // item is just a normal string (passed in data parameter)
675 flags |= MF_STRING;
676
677 #ifdef __WXWINCE__
678 itemText = wxMenuItem::GetLabelText(itemText);
679 #endif
680
681 pData = (wxChar*)itemText.wx_str();
682 }
683
684 // item might have already been inserted by InsertMenuItem() above
685 if ( !ok )
686 {
687 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
688 {
689 wxLogLastError(wxT("InsertMenu[Item]()"));
690
691 return false;
692 }
693 }
694
695
696 // if we just appended the title, highlight it
697 if ( id == idMenuTitle )
698 {
699 // visually select the menu title
700 SetDefaultMenuItem(GetHmenu(), id);
701 }
702
703 // if we're already attached to the menubar, we must update it
704 if ( IsAttached() && GetMenuBar()->IsAttached() )
705 {
706 GetMenuBar()->Refresh();
707 }
708
709 return true;
710 }
711
712 void wxMenu::EndRadioGroup()
713 {
714 // we're not inside a radio group any longer
715 m_startRadioGroup = -1;
716 }
717
718 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
719 {
720 wxCHECK_MSG( item, NULL, wxT("NULL item in wxMenu::DoAppend") );
721
722 bool check = false;
723
724 if ( item->GetKind() == wxITEM_RADIO )
725 {
726 int count = GetMenuItemCount();
727
728 if ( m_startRadioGroup == -1 )
729 {
730 // start a new radio group
731 m_startRadioGroup = count;
732
733 // for now it has just one element
734 item->SetAsRadioGroupStart();
735 item->SetRadioGroupEnd(m_startRadioGroup);
736
737 // ensure that we have a checked item in the radio group
738 check = true;
739 }
740 else // extend the current radio group
741 {
742 // we need to update its end item
743 item->SetRadioGroupStart(m_startRadioGroup);
744 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
745
746 if ( node )
747 {
748 node->GetData()->SetRadioGroupEnd(count);
749 }
750 else
751 {
752 wxFAIL_MSG( wxT("where is the radio group start item?") );
753 }
754 }
755 }
756 else // not a radio item
757 {
758 EndRadioGroup();
759 }
760
761 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
762 {
763 return NULL;
764 }
765
766 if ( check )
767 {
768 // check the item initially
769 item->Check(true);
770 }
771
772 return item;
773 }
774
775 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
776 {
777 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
778 return item;
779 else
780 return NULL;
781 }
782
783 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
784 {
785 // we need to find the item's position in the child list
786 size_t pos;
787 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
788 for ( pos = 0; node; pos++ )
789 {
790 if ( node->GetData() == item )
791 break;
792
793 node = node->GetNext();
794 }
795
796 // DoRemove() (unlike Remove) can only be called for an existing item!
797 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
798
799 #if wxUSE_ACCEL
800 // remove the corresponding accel from the accel table
801 int n = FindAccel(item->GetId());
802 if ( n != wxNOT_FOUND )
803 {
804 delete m_accels[n];
805
806 m_accels.RemoveAt(n);
807
808 ResetMaxAccelWidth();
809 }
810 //else: this item doesn't have an accel, nothing to do
811 #endif // wxUSE_ACCEL
812
813 // remove the item from the menu
814 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
815 {
816 wxLogLastError(wxT("RemoveMenu"));
817 }
818
819 if ( IsAttached() && GetMenuBar()->IsAttached() )
820 {
821 // otherwise, the change won't be visible
822 GetMenuBar()->Refresh();
823 }
824
825 // and from internal data structures
826 return wxMenuBase::DoRemove(item);
827 }
828
829 // ---------------------------------------------------------------------------
830 // accelerator helpers
831 // ---------------------------------------------------------------------------
832
833 #if wxUSE_ACCEL
834
835 // create the wxAcceleratorEntries for our accels and put them into the provided
836 // array - return the number of accels we have
837 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
838 {
839 size_t count = GetAccelCount();
840 for ( size_t n = 0; n < count; n++ )
841 {
842 *accels++ = *m_accels[n];
843 }
844
845 return count;
846 }
847
848 wxAcceleratorTable *wxMenu::CreateAccelTable() const
849 {
850 const size_t count = m_accels.size();
851 wxScopedArray<wxAcceleratorEntry> accels(new wxAcceleratorEntry[count]);
852 CopyAccels(accels.get());
853
854 return new wxAcceleratorTable(count, accels.get());
855 }
856
857 #endif // wxUSE_ACCEL
858
859 // ---------------------------------------------------------------------------
860 // ownerdrawn helpers
861 // ---------------------------------------------------------------------------
862
863 #if wxUSE_OWNER_DRAWN
864
865 void wxMenu::CalculateMaxAccelWidth()
866 {
867 wxASSERT_MSG( m_maxAccelWidth == -1, wxT("it's really needed?") );
868
869 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
870 while (node)
871 {
872 wxMenuItem* item = node->GetData();
873
874 if ( item->IsOwnerDrawn() )
875 {
876 int width = item->MeasureAccelWidth();
877 if (width > m_maxAccelWidth )
878 m_maxAccelWidth = width;
879 }
880
881 node = node->GetNext();
882 }
883 }
884
885 #endif // wxUSE_OWNER_DRAWN
886
887 // ---------------------------------------------------------------------------
888 // set wxMenu title
889 // ---------------------------------------------------------------------------
890
891 void wxMenu::SetTitle(const wxString& label)
892 {
893 bool hasNoTitle = m_title.empty();
894 m_title = label;
895
896 HMENU hMenu = GetHmenu();
897
898 if ( hasNoTitle )
899 {
900 if ( !label.empty() )
901 {
902 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
903 idMenuTitle, m_title.wx_str()) ||
904 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
905 {
906 wxLogLastError(wxT("InsertMenu"));
907 }
908 }
909 }
910 else
911 {
912 if ( label.empty() )
913 {
914 // remove the title and the separator after it
915 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
916 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
917 {
918 wxLogLastError(wxT("RemoveMenu"));
919 }
920 }
921 else
922 {
923 // modify the title
924 #ifdef __WXWINCE__
925 MENUITEMINFO info;
926 wxZeroMemory(info);
927 info.cbSize = sizeof(info);
928 info.fMask = MIIM_TYPE;
929 info.fType = MFT_STRING;
930 info.cch = m_title.length();
931 info.dwTypeData = const_cast<wxChar *>(m_title.wx_str());
932 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
933 {
934 wxLogLastError(wxT("SetMenuItemInfo"));
935 }
936 #else
937 if ( !ModifyMenu(hMenu, 0u,
938 MF_BYPOSITION | MF_STRING,
939 idMenuTitle, m_title.wx_str()) )
940 {
941 wxLogLastError(wxT("ModifyMenu"));
942 }
943 #endif
944 }
945 }
946
947 #ifdef __WIN32__
948 // put the title string in bold face
949 if ( !m_title.empty() )
950 {
951 SetDefaultMenuItem(GetHmenu(), idMenuTitle);
952 }
953 #endif // Win32
954 }
955
956 // ---------------------------------------------------------------------------
957 // event processing
958 // ---------------------------------------------------------------------------
959
960 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id_)
961 {
962 const int id = (signed short)id_;
963
964 // ignore commands from the menu title
965 if ( id != (int)idMenuTitle )
966 {
967 // update the check item when it's clicked
968 wxMenuItem * const item = FindItem(id);
969 if ( item && item->IsCheckable() )
970 item->Toggle();
971
972 // get the status of the menu item: note that it has been just changed
973 // by Toggle() above so here we already get the new state of the item
974 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
975 SendEvent(id, menuState & MF_CHECKED);
976 }
977
978 return true;
979 }
980
981 // ---------------------------------------------------------------------------
982 // other
983 // ---------------------------------------------------------------------------
984
985 wxWindow *wxMenu::GetWindow() const
986 {
987 if ( m_invokingWindow != NULL )
988 return m_invokingWindow;
989 else if ( GetMenuBar() != NULL)
990 return GetMenuBar()->GetFrame();
991
992 return NULL;
993 }
994
995 // ---------------------------------------------------------------------------
996 // Menu Bar
997 // ---------------------------------------------------------------------------
998
999 void wxMenuBar::Init()
1000 {
1001 m_eventHandler = this;
1002 m_hMenu = 0;
1003 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
1004 m_toolBar = NULL;
1005 #endif
1006 // Not using a combined wxToolBar/wxMenuBar? then use
1007 // a commandbar in WinCE .NET just to implement the
1008 // menubar.
1009 #if defined(WINCE_WITH_COMMANDBAR)
1010 m_commandBar = NULL;
1011 m_adornmentsAdded = false;
1012 #endif
1013 }
1014
1015 wxMenuBar::wxMenuBar()
1016 {
1017 Init();
1018 }
1019
1020 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
1021 {
1022 Init();
1023 }
1024
1025 wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
1026 {
1027 Init();
1028
1029 m_titles.Alloc(count);
1030
1031 for ( size_t i = 0; i < count; i++ )
1032 {
1033 m_menus.Append(menus[i]);
1034 m_titles.Add(titles[i]);
1035
1036 menus[i]->Attach(this);
1037 }
1038 }
1039
1040 wxMenuBar::~wxMenuBar()
1041 {
1042 // In Windows CE (not .NET), the menubar is always associated
1043 // with a toolbar, which destroys the menu implicitly.
1044 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1045 if (GetToolBar())
1046 {
1047 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
1048 if (toolMenuBar)
1049 toolMenuBar->SetMenuBar(NULL);
1050 }
1051 #else
1052 // we should free Windows resources only if Windows doesn't do it for us
1053 // which happens if we're attached to a frame
1054 if (m_hMenu && !IsAttached())
1055 {
1056 #if defined(WINCE_WITH_COMMANDBAR)
1057 ::DestroyWindow((HWND) m_commandBar);
1058 m_commandBar = (WXHWND) NULL;
1059 #else
1060 ::DestroyMenu((HMENU)m_hMenu);
1061 #endif
1062 m_hMenu = (WXHMENU)NULL;
1063 }
1064 #endif
1065 }
1066
1067 // ---------------------------------------------------------------------------
1068 // wxMenuBar helpers
1069 // ---------------------------------------------------------------------------
1070
1071 void wxMenuBar::Refresh()
1072 {
1073 if ( IsFrozen() )
1074 return;
1075
1076 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1077
1078 #if defined(WINCE_WITHOUT_COMMANDBAR)
1079 if (GetToolBar())
1080 {
1081 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
1082 }
1083 #elif defined(WINCE_WITH_COMMANDBAR)
1084 if (m_commandBar)
1085 DrawMenuBar((HWND) m_commandBar);
1086 #else
1087 DrawMenuBar(GetHwndOf(GetFrame()));
1088 #endif
1089 }
1090
1091 WXHMENU wxMenuBar::Create()
1092 {
1093 // Note: this doesn't work at all on Smartphone,
1094 // since you have to use resources.
1095 // We'll have to find another way to add a menu
1096 // by changing/adding menu items to an existing menu.
1097 #if defined(WINCE_WITHOUT_COMMANDBAR)
1098 if ( m_hMenu != 0 )
1099 return m_hMenu;
1100
1101 wxToolMenuBar * const bar = static_cast<wxToolMenuBar *>(GetToolBar());
1102 if ( !bar )
1103 return NULL;
1104
1105 HWND hCommandBar = GetHwndOf(bar);
1106
1107 // notify comctl32.dll about the version of the headers we use before using
1108 // any other TB_XXX messages
1109 SendMessage(hCommandBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
1110
1111 TBBUTTON tbButton;
1112 wxZeroMemory(tbButton);
1113 tbButton.iBitmap = I_IMAGENONE;
1114 tbButton.fsState = TBSTATE_ENABLED;
1115 tbButton.fsStyle = TBSTYLE_DROPDOWN |
1116 TBSTYLE_NO_DROPDOWN_ARROW |
1117 TBSTYLE_AUTOSIZE;
1118
1119 for ( unsigned i = 0; i < GetMenuCount(); i++ )
1120 {
1121 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu();
1122 tbButton.dwData = (DWORD)hPopupMenu;
1123 wxString label = wxStripMenuCodes(GetMenuLabel(i));
1124 tbButton.iString = (int) label.wx_str();
1125
1126 tbButton.idCommand = NewControlId();
1127 if ( !::SendMessage(hCommandBar, TB_INSERTBUTTON, i, (LPARAM)&tbButton) )
1128 {
1129 wxLogLastError(wxT("TB_INSERTBUTTON"));
1130 }
1131 }
1132
1133 m_hMenu = bar->GetHMenu();
1134 return m_hMenu;
1135 #else // !__WXWINCE__
1136 if ( m_hMenu != 0 )
1137 return m_hMenu;
1138
1139 m_hMenu = (WXHMENU)::CreateMenu();
1140
1141 if ( !m_hMenu )
1142 {
1143 wxLogLastError(wxT("CreateMenu"));
1144 }
1145 else
1146 {
1147 size_t count = GetMenuCount(), i;
1148 wxMenuList::iterator it;
1149 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1150 {
1151 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
1152 (UINT_PTR)(*it)->GetHMenu(),
1153 m_titles[i].wx_str()) )
1154 {
1155 wxLogLastError(wxT("AppendMenu"));
1156 }
1157 }
1158 }
1159
1160 return m_hMenu;
1161 #endif // __WXWINCE__/!__WXWINCE__
1162 }
1163
1164 int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
1165 {
1166 wxASSERT(menu);
1167 wxASSERT(menu->GetHMenu());
1168 wxASSERT(m_hMenu);
1169
1170 #if defined(__WXWINCE__)
1171 int totalMSWItems = GetMenuCount();
1172 #else
1173 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
1174 #endif
1175
1176 int i; // For old C++ compatibility
1177 for(i=wxpos; i<totalMSWItems; i++)
1178 {
1179 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1180 return i;
1181 }
1182 for(i=0; i<wxpos; i++)
1183 {
1184 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1185 return i;
1186 }
1187 wxFAIL;
1188 return -1;
1189 }
1190
1191 // ---------------------------------------------------------------------------
1192 // wxMenuBar functions to work with the top level submenus
1193 // ---------------------------------------------------------------------------
1194
1195 // NB: we don't support owner drawn top level items for now, if we do these
1196 // functions would have to be changed to use wxMenuItem as well
1197
1198 void wxMenuBar::EnableTop(size_t pos, bool enable)
1199 {
1200 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1201 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1202
1203 int flag = enable ? MF_ENABLED : MF_GRAYED;
1204
1205 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
1206
1207 Refresh();
1208 }
1209
1210 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
1211 {
1212 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1213
1214 m_titles[pos] = label;
1215
1216 if ( !IsAttached() )
1217 {
1218 return;
1219 }
1220 //else: have to modify the existing menu
1221
1222 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
1223
1224 UINT_PTR id;
1225 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
1226 if ( flagsOld == 0xFFFFFFFF )
1227 {
1228 wxLogLastError(wxT("GetMenuState"));
1229
1230 return;
1231 }
1232
1233 if ( flagsOld & MF_POPUP )
1234 {
1235 // HIBYTE contains the number of items in the submenu in this case
1236 flagsOld &= 0xff;
1237 id = (UINT_PTR)::GetSubMenu((HMENU)m_hMenu, mswpos);
1238 }
1239 else
1240 {
1241 id = pos;
1242 }
1243
1244 #ifdef __WXWINCE__
1245 MENUITEMINFO info;
1246 wxZeroMemory(info);
1247 info.cbSize = sizeof(info);
1248 info.fMask = MIIM_TYPE;
1249 info.fType = MFT_STRING;
1250 info.cch = label.length();
1251 info.dwTypeData = const_cast<wxChar *>(label.wx_str());
1252 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, &info) )
1253 {
1254 wxLogLastError(wxT("SetMenuItemInfo"));
1255 }
1256
1257 #else
1258 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
1259 id, label.wx_str()) == (int)0xFFFFFFFF )
1260 {
1261 wxLogLastError(wxT("ModifyMenu"));
1262 }
1263 #endif
1264
1265 Refresh();
1266 }
1267
1268 wxString wxMenuBar::GetMenuLabel(size_t pos) const
1269 {
1270 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1271 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1272
1273 return m_titles[pos];
1274 }
1275
1276 // ---------------------------------------------------------------------------
1277 // wxMenuBar construction
1278 // ---------------------------------------------------------------------------
1279
1280 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1281 {
1282 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1283 if ( !menuOld )
1284 return NULL;
1285
1286 m_titles[pos] = title;
1287
1288 #if defined(WINCE_WITHOUT_COMMANDBAR)
1289 if (IsAttached())
1290 #else
1291 if (GetHmenu())
1292 #endif
1293 {
1294 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1295
1296 // can't use ModifyMenu() because it deletes the submenu it replaces
1297 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
1298 {
1299 wxLogLastError(wxT("RemoveMenu"));
1300 }
1301
1302 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
1303 MF_BYPOSITION | MF_POPUP | MF_STRING,
1304 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1305 {
1306 wxLogLastError(wxT("InsertMenu"));
1307 }
1308
1309 #if wxUSE_ACCEL
1310 if ( menuOld->HasAccels() || menu->HasAccels() )
1311 {
1312 // need to rebuild accell table
1313 RebuildAccelTable();
1314 }
1315 #endif // wxUSE_ACCEL
1316
1317 if (IsAttached())
1318 Refresh();
1319 }
1320
1321 return menuOld;
1322 }
1323
1324 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1325 {
1326 // Find out which MSW item before which we'll be inserting before
1327 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1328 // If IsAttached() is false this won't be used anyway
1329 bool isAttached =
1330 #if defined(WINCE_WITHOUT_COMMANDBAR)
1331 IsAttached();
1332 #else
1333 (GetHmenu() != 0);
1334 #endif
1335
1336 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
1337 ? -1 // append the menu
1338 : MSWPositionForWxMenu(GetMenu(pos),pos);
1339
1340 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1341 return false;
1342
1343 m_titles.Insert(title, pos);
1344
1345 if ( isAttached )
1346 {
1347 #if defined(WINCE_WITHOUT_COMMANDBAR)
1348 if (!GetToolBar())
1349 return false;
1350 TBBUTTON tbButton;
1351 memset(&tbButton, 0, sizeof(TBBUTTON));
1352 tbButton.iBitmap = I_IMAGENONE;
1353 tbButton.fsState = TBSTATE_ENABLED;
1354 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1355
1356 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1357 tbButton.dwData = (DWORD)hPopupMenu;
1358 wxString label = wxStripMenuCodes(title);
1359 tbButton.iString = (int) label.wx_str();
1360
1361 tbButton.idCommand = NewControlId();
1362 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1363 {
1364 wxLogLastError(wxT("TB_INSERTBUTTON"));
1365 return false;
1366 }
1367 wxUnusedVar(mswpos);
1368 #else
1369 if ( !::InsertMenu(GetHmenu(), mswpos,
1370 MF_BYPOSITION | MF_POPUP | MF_STRING,
1371 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1372 {
1373 wxLogLastError(wxT("InsertMenu"));
1374 }
1375 #endif
1376 #if wxUSE_ACCEL
1377 if ( menu->HasAccels() )
1378 {
1379 // need to rebuild accell table
1380 RebuildAccelTable();
1381 }
1382 #endif // wxUSE_ACCEL
1383
1384 if (IsAttached())
1385 Refresh();
1386 }
1387
1388 return true;
1389 }
1390
1391 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1392 {
1393 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
1394 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
1395
1396 if ( !wxMenuBarBase::Append(menu, title) )
1397 return false;
1398
1399 m_titles.Add(title);
1400
1401 #if defined(WINCE_WITHOUT_COMMANDBAR)
1402 if (IsAttached())
1403 #else
1404 if (GetHmenu())
1405 #endif
1406 {
1407 #if defined(WINCE_WITHOUT_COMMANDBAR)
1408 if (!GetToolBar())
1409 return false;
1410 TBBUTTON tbButton;
1411 memset(&tbButton, 0, sizeof(TBBUTTON));
1412 tbButton.iBitmap = I_IMAGENONE;
1413 tbButton.fsState = TBSTATE_ENABLED;
1414 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1415
1416 size_t pos = GetMenuCount();
1417 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1418 tbButton.dwData = (DWORD)hPopupMenu;
1419 wxString label = wxStripMenuCodes(title);
1420 tbButton.iString = (int) label.wx_str();
1421
1422 tbButton.idCommand = NewControlId();
1423 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1424 {
1425 wxLogLastError(wxT("TB_INSERTBUTTON"));
1426 return false;
1427 }
1428 #else
1429 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1430 (UINT_PTR)submenu, title.wx_str()) )
1431 {
1432 wxLogLastError(wxT("AppendMenu"));
1433 }
1434 #endif
1435
1436 #if wxUSE_ACCEL
1437 if ( menu->HasAccels() )
1438 {
1439 // need to rebuild accelerator table
1440 RebuildAccelTable();
1441 }
1442 #endif // wxUSE_ACCEL
1443
1444 if (IsAttached())
1445 Refresh();
1446 }
1447
1448 return true;
1449 }
1450
1451 wxMenu *wxMenuBar::Remove(size_t pos)
1452 {
1453 wxMenu *menu = wxMenuBarBase::Remove(pos);
1454 if ( !menu )
1455 return NULL;
1456
1457 #if defined(WINCE_WITHOUT_COMMANDBAR)
1458 if (IsAttached())
1459 #else
1460 if (GetHmenu())
1461 #endif
1462 {
1463 #if defined(WINCE_WITHOUT_COMMANDBAR)
1464 if (GetToolBar())
1465 {
1466 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1467 {
1468 wxLogLastError(wxT("TB_DELETEBUTTON"));
1469 }
1470 }
1471 #else
1472 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
1473 {
1474 wxLogLastError(wxT("RemoveMenu"));
1475 }
1476 #endif
1477
1478 #if wxUSE_ACCEL
1479 if ( menu->HasAccels() )
1480 {
1481 // need to rebuild accell table
1482 RebuildAccelTable();
1483 }
1484 #endif // wxUSE_ACCEL
1485
1486 if (IsAttached())
1487 Refresh();
1488 }
1489
1490 m_titles.RemoveAt(pos);
1491
1492 return menu;
1493 }
1494
1495 #if wxUSE_ACCEL
1496
1497 void wxMenuBar::RebuildAccelTable()
1498 {
1499 // merge the accelerators of all menus into one accel table
1500 size_t nAccelCount = 0;
1501 size_t i, count = GetMenuCount();
1502 wxMenuList::iterator it;
1503 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1504 {
1505 nAccelCount += (*it)->GetAccelCount();
1506 }
1507
1508 if ( nAccelCount )
1509 {
1510 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1511
1512 nAccelCount = 0;
1513 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1514 {
1515 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1516 }
1517
1518 SetAcceleratorTable(wxAcceleratorTable(nAccelCount, accelEntries));
1519
1520 delete [] accelEntries;
1521 }
1522 }
1523
1524 #endif // wxUSE_ACCEL
1525
1526 void wxMenuBar::Attach(wxFrame *frame)
1527 {
1528 wxMenuBarBase::Attach(frame);
1529
1530 #if defined(WINCE_WITH_COMMANDBAR)
1531 if (!m_hMenu)
1532 this->Create();
1533 if (!m_commandBar)
1534 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1535 if (m_commandBar)
1536 {
1537 if (m_hMenu)
1538 {
1539 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1540 {
1541 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1542 }
1543 }
1544 }
1545 #endif
1546
1547 #if wxUSE_ACCEL
1548 RebuildAccelTable();
1549 #endif // wxUSE_ACCEL
1550 }
1551
1552 #if defined(WINCE_WITH_COMMANDBAR)
1553 bool wxMenuBar::AddAdornments(long style)
1554 {
1555 if (m_adornmentsAdded || !m_commandBar)
1556 return false;
1557
1558 if (style & wxCLOSE_BOX)
1559 {
1560 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1561 {
1562 wxLogLastError(wxT("CommandBar_AddAdornments"));
1563 }
1564 else
1565 {
1566 return true;
1567 }
1568 }
1569 return false;
1570 }
1571 #endif
1572
1573 void wxMenuBar::Detach()
1574 {
1575 wxMenuBarBase::Detach();
1576 }
1577
1578 #endif // wxUSE_MENUS