]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
7c62184ce2bcf2607f6f032dc8c2b857be84f9b6
[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 return GetMenuBar() ? GetMenuBar()->GetFrame() : GetInvokingWindow();
988 }
989
990 // ---------------------------------------------------------------------------
991 // Menu Bar
992 // ---------------------------------------------------------------------------
993
994 void wxMenuBar::Init()
995 {
996 m_eventHandler = this;
997 m_hMenu = 0;
998 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
999 m_toolBar = NULL;
1000 #endif
1001 // Not using a combined wxToolBar/wxMenuBar? then use
1002 // a commandbar in WinCE .NET just to implement the
1003 // menubar.
1004 #if defined(WINCE_WITH_COMMANDBAR)
1005 m_commandBar = NULL;
1006 m_adornmentsAdded = false;
1007 #endif
1008 }
1009
1010 wxMenuBar::wxMenuBar()
1011 {
1012 Init();
1013 }
1014
1015 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
1016 {
1017 Init();
1018 }
1019
1020 wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
1021 {
1022 Init();
1023
1024 m_titles.Alloc(count);
1025
1026 for ( size_t i = 0; i < count; i++ )
1027 {
1028 m_menus.Append(menus[i]);
1029 m_titles.Add(titles[i]);
1030
1031 menus[i]->Attach(this);
1032 }
1033 }
1034
1035 wxMenuBar::~wxMenuBar()
1036 {
1037 // In Windows CE (not .NET), the menubar is always associated
1038 // with a toolbar, which destroys the menu implicitly.
1039 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1040 if (GetToolBar())
1041 {
1042 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
1043 if (toolMenuBar)
1044 toolMenuBar->SetMenuBar(NULL);
1045 }
1046 #else
1047 // we should free Windows resources only if Windows doesn't do it for us
1048 // which happens if we're attached to a frame
1049 if (m_hMenu && !IsAttached())
1050 {
1051 #if defined(WINCE_WITH_COMMANDBAR)
1052 ::DestroyWindow((HWND) m_commandBar);
1053 m_commandBar = (WXHWND) NULL;
1054 #else
1055 ::DestroyMenu((HMENU)m_hMenu);
1056 #endif
1057 m_hMenu = (WXHMENU)NULL;
1058 }
1059 #endif
1060 }
1061
1062 // ---------------------------------------------------------------------------
1063 // wxMenuBar helpers
1064 // ---------------------------------------------------------------------------
1065
1066 void wxMenuBar::Refresh()
1067 {
1068 if ( IsFrozen() )
1069 return;
1070
1071 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1072
1073 #if defined(WINCE_WITHOUT_COMMANDBAR)
1074 if (GetToolBar())
1075 {
1076 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
1077 }
1078 #elif defined(WINCE_WITH_COMMANDBAR)
1079 if (m_commandBar)
1080 DrawMenuBar((HWND) m_commandBar);
1081 #else
1082 DrawMenuBar(GetHwndOf(GetFrame()));
1083 #endif
1084 }
1085
1086 WXHMENU wxMenuBar::Create()
1087 {
1088 // Note: this doesn't work at all on Smartphone,
1089 // since you have to use resources.
1090 // We'll have to find another way to add a menu
1091 // by changing/adding menu items to an existing menu.
1092 #if defined(WINCE_WITHOUT_COMMANDBAR)
1093 if ( m_hMenu != 0 )
1094 return m_hMenu;
1095
1096 wxToolMenuBar * const bar = static_cast<wxToolMenuBar *>(GetToolBar());
1097 if ( !bar )
1098 return NULL;
1099
1100 HWND hCommandBar = GetHwndOf(bar);
1101
1102 // notify comctl32.dll about the version of the headers we use before using
1103 // any other TB_XXX messages
1104 SendMessage(hCommandBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
1105
1106 TBBUTTON tbButton;
1107 wxZeroMemory(tbButton);
1108 tbButton.iBitmap = I_IMAGENONE;
1109 tbButton.fsState = TBSTATE_ENABLED;
1110 tbButton.fsStyle = TBSTYLE_DROPDOWN |
1111 TBSTYLE_NO_DROPDOWN_ARROW |
1112 TBSTYLE_AUTOSIZE;
1113
1114 for ( unsigned i = 0; i < GetMenuCount(); i++ )
1115 {
1116 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu();
1117 tbButton.dwData = (DWORD)hPopupMenu;
1118 wxString label = wxStripMenuCodes(GetMenuLabel(i));
1119 tbButton.iString = (int) label.wx_str();
1120
1121 tbButton.idCommand = NewControlId();
1122 if ( !::SendMessage(hCommandBar, TB_INSERTBUTTON, i, (LPARAM)&tbButton) )
1123 {
1124 wxLogLastError(wxT("TB_INSERTBUTTON"));
1125 }
1126 }
1127
1128 m_hMenu = bar->GetHMenu();
1129 return m_hMenu;
1130 #else // !__WXWINCE__
1131 if ( m_hMenu != 0 )
1132 return m_hMenu;
1133
1134 m_hMenu = (WXHMENU)::CreateMenu();
1135
1136 if ( !m_hMenu )
1137 {
1138 wxLogLastError(wxT("CreateMenu"));
1139 }
1140 else
1141 {
1142 size_t count = GetMenuCount(), i;
1143 wxMenuList::iterator it;
1144 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1145 {
1146 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
1147 (UINT_PTR)(*it)->GetHMenu(),
1148 m_titles[i].wx_str()) )
1149 {
1150 wxLogLastError(wxT("AppendMenu"));
1151 }
1152 }
1153 }
1154
1155 return m_hMenu;
1156 #endif // __WXWINCE__/!__WXWINCE__
1157 }
1158
1159 int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
1160 {
1161 wxASSERT(menu);
1162 wxASSERT(menu->GetHMenu());
1163 wxASSERT(m_hMenu);
1164
1165 #if defined(__WXWINCE__)
1166 int totalMSWItems = GetMenuCount();
1167 #else
1168 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
1169 #endif
1170
1171 int i; // For old C++ compatibility
1172 for(i=wxpos; i<totalMSWItems; i++)
1173 {
1174 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1175 return i;
1176 }
1177 for(i=0; i<wxpos; i++)
1178 {
1179 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
1180 return i;
1181 }
1182 wxFAIL;
1183 return -1;
1184 }
1185
1186 // ---------------------------------------------------------------------------
1187 // wxMenuBar functions to work with the top level submenus
1188 // ---------------------------------------------------------------------------
1189
1190 // NB: we don't support owner drawn top level items for now, if we do these
1191 // functions would have to be changed to use wxMenuItem as well
1192
1193 void wxMenuBar::EnableTop(size_t pos, bool enable)
1194 {
1195 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1196 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1197
1198 int flag = enable ? MF_ENABLED : MF_GRAYED;
1199
1200 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
1201
1202 Refresh();
1203 }
1204
1205 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
1206 {
1207 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1208
1209 m_titles[pos] = label;
1210
1211 if ( !IsAttached() )
1212 {
1213 return;
1214 }
1215 //else: have to modify the existing menu
1216
1217 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
1218
1219 UINT_PTR id;
1220 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
1221 if ( flagsOld == 0xFFFFFFFF )
1222 {
1223 wxLogLastError(wxT("GetMenuState"));
1224
1225 return;
1226 }
1227
1228 if ( flagsOld & MF_POPUP )
1229 {
1230 // HIBYTE contains the number of items in the submenu in this case
1231 flagsOld &= 0xff;
1232 id = (UINT_PTR)::GetSubMenu((HMENU)m_hMenu, mswpos);
1233 }
1234 else
1235 {
1236 id = pos;
1237 }
1238
1239 #ifdef __WXWINCE__
1240 MENUITEMINFO info;
1241 wxZeroMemory(info);
1242 info.cbSize = sizeof(info);
1243 info.fMask = MIIM_TYPE;
1244 info.fType = MFT_STRING;
1245 info.cch = label.length();
1246 info.dwTypeData = const_cast<wxChar *>(label.wx_str());
1247 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, &info) )
1248 {
1249 wxLogLastError(wxT("SetMenuItemInfo"));
1250 }
1251
1252 #else
1253 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
1254 id, label.wx_str()) == (int)0xFFFFFFFF )
1255 {
1256 wxLogLastError(wxT("ModifyMenu"));
1257 }
1258 #endif
1259
1260 Refresh();
1261 }
1262
1263 wxString wxMenuBar::GetMenuLabel(size_t pos) const
1264 {
1265 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1266 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1267
1268 return m_titles[pos];
1269 }
1270
1271 // ---------------------------------------------------------------------------
1272 // wxMenuBar construction
1273 // ---------------------------------------------------------------------------
1274
1275 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1276 {
1277 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1278 if ( !menuOld )
1279 return NULL;
1280
1281 m_titles[pos] = title;
1282
1283 #if defined(WINCE_WITHOUT_COMMANDBAR)
1284 if (IsAttached())
1285 #else
1286 if (GetHmenu())
1287 #endif
1288 {
1289 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1290
1291 // can't use ModifyMenu() because it deletes the submenu it replaces
1292 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
1293 {
1294 wxLogLastError(wxT("RemoveMenu"));
1295 }
1296
1297 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
1298 MF_BYPOSITION | MF_POPUP | MF_STRING,
1299 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1300 {
1301 wxLogLastError(wxT("InsertMenu"));
1302 }
1303
1304 #if wxUSE_ACCEL
1305 if ( menuOld->HasAccels() || menu->HasAccels() )
1306 {
1307 // need to rebuild accell table
1308 RebuildAccelTable();
1309 }
1310 #endif // wxUSE_ACCEL
1311
1312 if (IsAttached())
1313 Refresh();
1314 }
1315
1316 return menuOld;
1317 }
1318
1319 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1320 {
1321 // Find out which MSW item before which we'll be inserting before
1322 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1323 // If IsAttached() is false this won't be used anyway
1324 bool isAttached =
1325 #if defined(WINCE_WITHOUT_COMMANDBAR)
1326 IsAttached();
1327 #else
1328 (GetHmenu() != 0);
1329 #endif
1330
1331 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
1332 ? -1 // append the menu
1333 : MSWPositionForWxMenu(GetMenu(pos),pos);
1334
1335 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1336 return false;
1337
1338 m_titles.Insert(title, pos);
1339
1340 if ( isAttached )
1341 {
1342 #if defined(WINCE_WITHOUT_COMMANDBAR)
1343 if (!GetToolBar())
1344 return false;
1345 TBBUTTON tbButton;
1346 memset(&tbButton, 0, sizeof(TBBUTTON));
1347 tbButton.iBitmap = I_IMAGENONE;
1348 tbButton.fsState = TBSTATE_ENABLED;
1349 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1350
1351 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1352 tbButton.dwData = (DWORD)hPopupMenu;
1353 wxString label = wxStripMenuCodes(title);
1354 tbButton.iString = (int) label.wx_str();
1355
1356 tbButton.idCommand = NewControlId();
1357 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1358 {
1359 wxLogLastError(wxT("TB_INSERTBUTTON"));
1360 return false;
1361 }
1362 wxUnusedVar(mswpos);
1363 #else
1364 if ( !::InsertMenu(GetHmenu(), mswpos,
1365 MF_BYPOSITION | MF_POPUP | MF_STRING,
1366 (UINT_PTR)GetHmenuOf(menu), title.wx_str()) )
1367 {
1368 wxLogLastError(wxT("InsertMenu"));
1369 }
1370 #endif
1371 #if wxUSE_ACCEL
1372 if ( menu->HasAccels() )
1373 {
1374 // need to rebuild accell table
1375 RebuildAccelTable();
1376 }
1377 #endif // wxUSE_ACCEL
1378
1379 if (IsAttached())
1380 Refresh();
1381 }
1382
1383 return true;
1384 }
1385
1386 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1387 {
1388 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
1389 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
1390
1391 if ( !wxMenuBarBase::Append(menu, title) )
1392 return false;
1393
1394 m_titles.Add(title);
1395
1396 #if defined(WINCE_WITHOUT_COMMANDBAR)
1397 if (IsAttached())
1398 #else
1399 if (GetHmenu())
1400 #endif
1401 {
1402 #if defined(WINCE_WITHOUT_COMMANDBAR)
1403 if (!GetToolBar())
1404 return false;
1405 TBBUTTON tbButton;
1406 memset(&tbButton, 0, sizeof(TBBUTTON));
1407 tbButton.iBitmap = I_IMAGENONE;
1408 tbButton.fsState = TBSTATE_ENABLED;
1409 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1410
1411 size_t pos = GetMenuCount();
1412 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1413 tbButton.dwData = (DWORD)hPopupMenu;
1414 wxString label = wxStripMenuCodes(title);
1415 tbButton.iString = (int) label.wx_str();
1416
1417 tbButton.idCommand = NewControlId();
1418 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1419 {
1420 wxLogLastError(wxT("TB_INSERTBUTTON"));
1421 return false;
1422 }
1423 #else
1424 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1425 (UINT_PTR)submenu, title.wx_str()) )
1426 {
1427 wxLogLastError(wxT("AppendMenu"));
1428 }
1429 #endif
1430
1431 #if wxUSE_ACCEL
1432 if ( menu->HasAccels() )
1433 {
1434 // need to rebuild accelerator table
1435 RebuildAccelTable();
1436 }
1437 #endif // wxUSE_ACCEL
1438
1439 if (IsAttached())
1440 Refresh();
1441 }
1442
1443 return true;
1444 }
1445
1446 wxMenu *wxMenuBar::Remove(size_t pos)
1447 {
1448 wxMenu *menu = wxMenuBarBase::Remove(pos);
1449 if ( !menu )
1450 return NULL;
1451
1452 #if defined(WINCE_WITHOUT_COMMANDBAR)
1453 if (IsAttached())
1454 #else
1455 if (GetHmenu())
1456 #endif
1457 {
1458 #if defined(WINCE_WITHOUT_COMMANDBAR)
1459 if (GetToolBar())
1460 {
1461 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1462 {
1463 wxLogLastError(wxT("TB_DELETEBUTTON"));
1464 }
1465 }
1466 #else
1467 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
1468 {
1469 wxLogLastError(wxT("RemoveMenu"));
1470 }
1471 #endif
1472
1473 #if wxUSE_ACCEL
1474 if ( menu->HasAccels() )
1475 {
1476 // need to rebuild accell table
1477 RebuildAccelTable();
1478 }
1479 #endif // wxUSE_ACCEL
1480
1481 if (IsAttached())
1482 Refresh();
1483 }
1484
1485 m_titles.RemoveAt(pos);
1486
1487 return menu;
1488 }
1489
1490 #if wxUSE_ACCEL
1491
1492 void wxMenuBar::RebuildAccelTable()
1493 {
1494 // merge the accelerators of all menus into one accel table
1495 size_t nAccelCount = 0;
1496 size_t i, count = GetMenuCount();
1497 wxMenuList::iterator it;
1498 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1499 {
1500 nAccelCount += (*it)->GetAccelCount();
1501 }
1502
1503 if ( nAccelCount )
1504 {
1505 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1506
1507 nAccelCount = 0;
1508 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1509 {
1510 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1511 }
1512
1513 SetAcceleratorTable(wxAcceleratorTable(nAccelCount, accelEntries));
1514
1515 delete [] accelEntries;
1516 }
1517 }
1518
1519 #endif // wxUSE_ACCEL
1520
1521 void wxMenuBar::Attach(wxFrame *frame)
1522 {
1523 wxMenuBarBase::Attach(frame);
1524
1525 #if defined(WINCE_WITH_COMMANDBAR)
1526 if (!m_hMenu)
1527 this->Create();
1528 if (!m_commandBar)
1529 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1530 if (m_commandBar)
1531 {
1532 if (m_hMenu)
1533 {
1534 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1535 {
1536 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1537 }
1538 }
1539 }
1540 #endif
1541
1542 #if wxUSE_ACCEL
1543 RebuildAccelTable();
1544 #endif // wxUSE_ACCEL
1545 }
1546
1547 #if defined(WINCE_WITH_COMMANDBAR)
1548 bool wxMenuBar::AddAdornments(long style)
1549 {
1550 if (m_adornmentsAdded || !m_commandBar)
1551 return false;
1552
1553 if (style & wxCLOSE_BOX)
1554 {
1555 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1556 {
1557 wxLogLastError(wxT("CommandBar_AddAdornments"));
1558 }
1559 else
1560 {
1561 return true;
1562 }
1563 }
1564 return false;
1565 }
1566 #endif
1567
1568 void wxMenuBar::Detach()
1569 {
1570 wxMenuBarBase::Detach();
1571 }
1572
1573 #endif // wxUSE_MENUS