]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
wxUniv compilation fixes
[wxWidgets.git] / src / msw / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
21 #pragma implementation "menu.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/frame.h"
35 #include "wx/menu.h"
36 #include "wx/utils.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
39 #endif
40
41 #if wxUSE_OWNER_DRAWN
42 #include "wx/ownerdrw.h"
43 #endif
44
45 #include "wx/msw/private.h"
46
47 #ifdef __WXWINCE__
48 #include <windows.h>
49 #include <windowsx.h>
50 #include <tchar.h>
51 #include <ole2.h>
52 #include <commctrl.h>
53 #include <aygshell.h>
54
55 #ifndef TBSTYLE_NO_DROPDOWN_ARROW
56 #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
57 #endif
58
59 #endif
60
61 // other standard headers
62 #include <string.h>
63
64 // ----------------------------------------------------------------------------
65 // global variables
66 // ----------------------------------------------------------------------------
67
68 extern wxMenu *wxCurrentPopupMenu;
69
70 // ----------------------------------------------------------------------------
71 // constants
72 // ----------------------------------------------------------------------------
73
74 // the (popup) menu title has this special id
75 static const int idMenuTitle = -2;
76
77 // ----------------------------------------------------------------------------
78 // private functions
79 // ----------------------------------------------------------------------------
80
81 // make the given menu item default
82 static void SetDefaultMenuItem(HMENU hmenu, UINT id)
83 {
84 #ifndef __WXWINCE__
85 MENUITEMINFO mii;
86 wxZeroMemory(mii);
87 mii.cbSize = sizeof(MENUITEMINFO);
88 mii.fMask = MIIM_STATE;
89 mii.fState = MFS_DEFAULT;
90
91 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
92 {
93 wxLogLastError(wxT("SetMenuItemInfo"));
94 }
95 #endif
96 }
97
98 #ifdef __WXWINCE__
99 UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
100 {
101 MENUITEMINFO info;
102 wxZeroMemory(info);
103 info.cbSize = sizeof(info);
104 info.fMask = MIIM_STATE;
105 if ( !GetMenuItemInfo(hMenu, id, flags & MF_BYCOMMAND ? FALSE : TRUE, & info) )
106 wxLogLastError(wxT("GetMenuItemInfo"));
107 return info.fState;
108 }
109 #endif
110
111 // ============================================================================
112 // implementation
113 // ============================================================================
114
115 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
116 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
117
118 // ---------------------------------------------------------------------------
119 // wxMenu construction, adding and removing menu items
120 // ---------------------------------------------------------------------------
121
122 // Construct a menu with optional title (then use append)
123 void wxMenu::Init()
124 {
125 m_doBreak = FALSE;
126 m_startRadioGroup = -1;
127
128 // create the menu
129 m_hMenu = (WXHMENU)CreatePopupMenu();
130 if ( !m_hMenu )
131 {
132 wxLogLastError(wxT("CreatePopupMenu"));
133 }
134
135 // if we have a title, insert it in the beginning of the menu
136 if ( !!m_title )
137 {
138 Append(idMenuTitle, m_title);
139 AppendSeparator();
140 }
141 }
142
143 // The wxWindow destructor will take care of deleting the submenus.
144 wxMenu::~wxMenu()
145 {
146 // we should free Windows resources only if Windows doesn't do it for us
147 // which happens if we're attached to a menubar or a submenu of another
148 // menu
149 if ( !IsAttached() && !GetParent() )
150 {
151 if ( !::DestroyMenu(GetHmenu()) )
152 {
153 wxLogLastError(wxT("DestroyMenu"));
154 }
155 }
156
157 #if wxUSE_ACCEL
158 // delete accels
159 WX_CLEAR_ARRAY(m_accels);
160 #endif // wxUSE_ACCEL
161 }
162
163 void wxMenu::Break()
164 {
165 // this will take effect during the next call to Append()
166 m_doBreak = TRUE;
167 }
168
169 void wxMenu::Attach(wxMenuBarBase *menubar)
170 {
171 wxMenuBase::Attach(menubar);
172
173 EndRadioGroup();
174 }
175
176 #if wxUSE_ACCEL
177
178 int wxMenu::FindAccel(int id) const
179 {
180 size_t n, count = m_accels.GetCount();
181 for ( n = 0; n < count; n++ )
182 {
183 if ( m_accels[n]->m_command == id )
184 return n;
185 }
186
187 return wxNOT_FOUND;
188 }
189
190 void wxMenu::UpdateAccel(wxMenuItem *item)
191 {
192 if ( item->IsSubMenu() )
193 {
194 wxMenu *submenu = item->GetSubMenu();
195 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
196 while ( node )
197 {
198 UpdateAccel(node->GetData());
199
200 node = node->GetNext();
201 }
202 }
203 else if ( !item->IsSeparator() )
204 {
205 // find the (new) accel for this item
206 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
207 if ( accel )
208 accel->m_command = item->GetId();
209
210 // find the old one
211 int n = FindAccel(item->GetId());
212 if ( n == wxNOT_FOUND )
213 {
214 // no old, add new if any
215 if ( accel )
216 m_accels.Add(accel);
217 else
218 return; // skipping RebuildAccelTable() below
219 }
220 else
221 {
222 // replace old with new or just remove the old one if no new
223 delete m_accels[n];
224 if ( accel )
225 m_accels[n] = accel;
226 else
227 m_accels.RemoveAt(n);
228 }
229
230 if ( IsAttached() )
231 {
232 m_menuBar->RebuildAccelTable();
233 }
234 }
235 //else: it is a separator, they can't have accels, nothing to do
236 }
237
238 #endif // wxUSE_ACCEL
239
240 // append a new item or submenu to the menu
241 bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
242 {
243 #if wxUSE_ACCEL
244 UpdateAccel(pItem);
245 #endif // wxUSE_ACCEL
246
247 UINT flags = 0;
248
249 // if "Break" has just been called, insert a menu break before this item
250 // (and don't forget to reset the flag)
251 if ( m_doBreak ) {
252 flags |= MF_MENUBREAK;
253 m_doBreak = FALSE;
254 }
255
256 if ( pItem->IsSeparator() ) {
257 flags |= MF_SEPARATOR;
258 }
259
260 // id is the numeric id for normal menu items and HMENU for submenus as
261 // required by ::AppendMenu() API
262 UINT id;
263 wxMenu *submenu = pItem->GetSubMenu();
264 if ( submenu != NULL ) {
265 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
266
267 submenu->SetParent(this);
268
269 id = (UINT)submenu->GetHMenu();
270
271 flags |= MF_POPUP;
272 }
273 else {
274 id = pItem->GetId();
275 }
276
277 #ifdef __WXWINCE__
278 wxString strippedString;
279 #endif
280
281 LPCTSTR pData;
282
283 #if wxUSE_OWNER_DRAWN
284 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
285 // item draws itself, pass pointer to it in data parameter
286 flags |= MF_OWNERDRAW;
287 pData = (LPCTSTR)pItem;
288 }
289 else
290 #endif
291 {
292 // menu is just a normal string (passed in data parameter)
293 flags |= MF_STRING;
294
295 #ifdef __WXWINCE__
296 strippedString = wxStripMenuCodes(pItem->GetText());
297 pData = (wxChar*)strippedString.c_str();
298 #else
299 pData = (wxChar*)pItem->GetText().c_str();
300 #endif
301 }
302
303 BOOL ok;
304 if ( pos == (size_t)-1 )
305 {
306 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
307 }
308 else
309 {
310 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
311 }
312
313 if ( !ok )
314 {
315 wxLogLastError(wxT("Insert or AppendMenu"));
316
317 return FALSE;
318 }
319
320 // if we just appended the title, highlight it
321 #ifdef __WIN32__
322 if ( (int)id == idMenuTitle )
323 {
324 // visually select the menu title
325 SetDefaultMenuItem(GetHmenu(), id);
326 }
327 #endif // __WIN32__
328
329 // if we're already attached to the menubar, we must update it
330 if ( IsAttached() && m_menuBar->IsAttached() )
331 {
332 m_menuBar->Refresh();
333 }
334
335 return TRUE;
336 }
337
338 void wxMenu::EndRadioGroup()
339 {
340 // we're not inside a radio group any longer
341 m_startRadioGroup = -1;
342 }
343
344 bool wxMenu::DoAppend(wxMenuItem *item)
345 {
346 wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") );
347
348 bool check = FALSE;
349
350 if ( item->GetKind() == wxITEM_RADIO )
351 {
352 int count = GetMenuItemCount();
353
354 if ( m_startRadioGroup == -1 )
355 {
356 // start a new radio group
357 m_startRadioGroup = count;
358
359 // for now it has just one element
360 item->SetAsRadioGroupStart();
361 item->SetRadioGroupEnd(m_startRadioGroup);
362
363 // ensure that we have a checked item in the radio group
364 check = TRUE;
365 }
366 else // extend the current radio group
367 {
368 // we need to update its end item
369 item->SetRadioGroupStart(m_startRadioGroup);
370 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
371
372 if ( node )
373 {
374 node->GetData()->SetRadioGroupEnd(count);
375 }
376 else
377 {
378 wxFAIL_MSG( _T("where is the radio group start item?") );
379 }
380 }
381 }
382 else // not a radio item
383 {
384 EndRadioGroup();
385 }
386
387 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
388 {
389 return FALSE;
390 }
391
392 if ( check )
393 {
394 // check the item initially
395 item->Check(TRUE);
396 }
397
398 return TRUE;
399 }
400
401 bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
402 {
403 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
404 }
405
406 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
407 {
408 // we need to find the items position in the child list
409 size_t pos;
410 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
411 for ( pos = 0; node; pos++ )
412 {
413 if ( node->GetData() == item )
414 break;
415
416 node = node->GetNext();
417 }
418
419 // DoRemove() (unlike Remove) can only be called for existing item!
420 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
421
422 #if wxUSE_ACCEL
423 // remove the corresponding accel from the accel table
424 int n = FindAccel(item->GetId());
425 if ( n != wxNOT_FOUND )
426 {
427 delete m_accels[n];
428
429 m_accels.RemoveAt(n);
430 }
431 //else: this item doesn't have an accel, nothing to do
432 #endif // wxUSE_ACCEL
433
434 // remove the item from the menu
435 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
436 {
437 wxLogLastError(wxT("RemoveMenu"));
438 }
439
440 if ( IsAttached() && m_menuBar->IsAttached() )
441 {
442 // otherwise, the chane won't be visible
443 m_menuBar->Refresh();
444 }
445
446 // and from internal data structures
447 return wxMenuBase::DoRemove(item);
448 }
449
450 // ---------------------------------------------------------------------------
451 // accelerator helpers
452 // ---------------------------------------------------------------------------
453
454 #if wxUSE_ACCEL
455
456 // create the wxAcceleratorEntries for our accels and put them into provided
457 // array - return the number of accels we have
458 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
459 {
460 size_t count = GetAccelCount();
461 for ( size_t n = 0; n < count; n++ )
462 {
463 *accels++ = *m_accels[n];
464 }
465
466 return count;
467 }
468
469 #endif // wxUSE_ACCEL
470
471 // ---------------------------------------------------------------------------
472 // set wxMenu title
473 // ---------------------------------------------------------------------------
474
475 void wxMenu::SetTitle(const wxString& label)
476 {
477 bool hasNoTitle = m_title.IsEmpty();
478 m_title = label;
479
480 HMENU hMenu = GetHmenu();
481
482 if ( hasNoTitle )
483 {
484 if ( !label.IsEmpty() )
485 {
486 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
487 (unsigned)idMenuTitle, m_title) ||
488 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
489 {
490 wxLogLastError(wxT("InsertMenu"));
491 }
492 }
493 }
494 else
495 {
496 if ( label.IsEmpty() )
497 {
498 // remove the title and the separator after it
499 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
500 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
501 {
502 wxLogLastError(wxT("RemoveMenu"));
503 }
504 }
505 else
506 {
507 // modify the title
508 #ifdef __WXWINCE__
509 MENUITEMINFO info;
510 wxZeroMemory(info);
511 info.cbSize = sizeof(info);
512 info.fMask = MIIM_TYPE;
513 info.fType = MFT_STRING;
514 info.cch = m_title.Length();
515 info.dwTypeData = (LPTSTR) m_title.c_str();
516 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
517 {
518 wxLogLastError(wxT("SetMenuItemInfo"));
519 }
520 #else
521 if ( !ModifyMenu(hMenu, 0u,
522 MF_BYPOSITION | MF_STRING,
523 (unsigned)idMenuTitle, m_title) )
524 {
525 wxLogLastError(wxT("ModifyMenu"));
526 }
527 #endif
528 }
529 }
530
531 #ifdef __WIN32__
532 // put the title string in bold face
533 if ( !m_title.IsEmpty() )
534 {
535 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
536 }
537 #endif // Win32
538 }
539
540 // ---------------------------------------------------------------------------
541 // event processing
542 // ---------------------------------------------------------------------------
543
544 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
545 {
546 // ignore commands from the menu title
547
548 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
549 if ( id != (WXWORD)idMenuTitle )
550 {
551 // VZ: previosuly, the command int was set to id too which was quite
552 // useless anyhow (as it could be retrieved using GetId()) and
553 // uncompatible with wxGTK, so now we use the command int instead
554 // to pass the checked status
555 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ;
556 SendEvent(id, menuState & MF_CHECKED);
557 }
558
559 return TRUE;
560 }
561
562 // ---------------------------------------------------------------------------
563 // other
564 // ---------------------------------------------------------------------------
565
566 wxWindow *wxMenu::GetWindow() const
567 {
568 if ( m_invokingWindow != NULL )
569 return m_invokingWindow;
570 else if ( m_menuBar != NULL)
571 return m_menuBar->GetFrame();
572
573 return NULL;
574 }
575
576 // ---------------------------------------------------------------------------
577 // Menu Bar
578 // ---------------------------------------------------------------------------
579
580 void wxMenuBar::Init()
581 {
582 m_eventHandler = this;
583 m_hMenu = 0;
584 #ifdef __WXWINCE__
585 m_toolBar = NULL;
586 #endif
587 }
588
589 wxMenuBar::wxMenuBar()
590 {
591 Init();
592 }
593
594 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
595 {
596 Init();
597 }
598
599 wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
600 {
601 Init();
602
603 m_titles.Alloc(count);
604
605 for ( int i = 0; i < count; i++ )
606 {
607 m_menus.Append(menus[i]);
608 m_titles.Add(titles[i]);
609
610 menus[i]->Attach(this);
611 }
612 }
613
614 wxMenuBar::~wxMenuBar()
615 {
616 // In Windows CE, the menubar is always associated
617 // with a toolbar, which destroys the menu implicitly.
618 #ifdef __WXWINCE__
619 if (GetToolBar())
620 GetToolBar()->SetMenuBar(NULL);
621 #else
622 // we should free Windows resources only if Windows doesn't do it for us
623 // which happens if we're attached to a frame
624 if (m_hMenu && !IsAttached())
625 {
626 ::DestroyMenu((HMENU)m_hMenu);
627 m_hMenu = (WXHMENU)NULL;
628 }
629 #endif
630 }
631
632 // ---------------------------------------------------------------------------
633 // wxMenuBar helpers
634 // ---------------------------------------------------------------------------
635
636 void wxMenuBar::Refresh()
637 {
638 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
639
640 #ifdef __WXWINCE__
641 if (GetToolBar())
642 {
643 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
644 }
645 #else
646 DrawMenuBar(GetHwndOf(GetFrame()));
647 #endif
648 }
649
650 WXHMENU wxMenuBar::Create()
651 {
652 // Note: this totally doesn't work on Smartphone,
653 // since you have to use resources.
654 // We'll have to find another way to add a menu
655 // by changing/adding menu items to an existing menu.
656 #ifdef __WXWINCE__
657 if ( m_hMenu != 0 )
658 return m_hMenu;
659
660 if (!GetToolBar())
661 return 0;
662
663 HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
664 HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
665 if (hMenu)
666 {
667 TBBUTTON tbButton;
668 memset(&tbButton, 0, sizeof(TBBUTTON));
669 tbButton.iBitmap = I_IMAGENONE;
670 tbButton.fsState = TBSTATE_ENABLED;
671 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
672
673 size_t i;
674 for (i = 0; i < GetMenuCount(); i++)
675 {
676 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
677 tbButton.dwData = (DWORD)hPopupMenu;
678 wxString label = wxStripMenuCodes(GetLabelTop(i));
679 tbButton.iString = (int) label.c_str();
680
681 int position = i;
682
683 tbButton.idCommand = NewControlId();
684 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
685 {
686 wxLogLastError(wxT("TB_INSERTBUTTON"));
687 }
688 }
689 }
690 m_hMenu = (WXHMENU) hMenu;
691 return m_hMenu;
692 #else
693 if ( m_hMenu != 0 )
694 return m_hMenu;
695
696 m_hMenu = (WXHMENU)::CreateMenu();
697
698 if ( !m_hMenu )
699 {
700 wxLogLastError(wxT("CreateMenu"));
701 }
702 else
703 {
704 size_t count = GetMenuCount(), i;
705 wxMenuList::iterator it;
706 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
707 {
708 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
709 (UINT)(*it)->GetHMenu(),
710 m_titles[i]) )
711 {
712 wxLogLastError(wxT("AppendMenu"));
713 }
714 }
715 }
716
717 return m_hMenu;
718 #endif
719 }
720
721 // ---------------------------------------------------------------------------
722 // wxMenuBar functions to work with the top level submenus
723 // ---------------------------------------------------------------------------
724
725 // NB: we don't support owner drawn top level items for now, if we do these
726 // functions would have to be changed to use wxMenuItem as well
727
728 void wxMenuBar::EnableTop(size_t pos, bool enable)
729 {
730 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
731
732 int flag = enable ? MF_ENABLED : MF_GRAYED;
733
734 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
735
736 Refresh();
737 }
738
739 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
740 {
741 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
742
743 m_titles[pos] = label;
744
745 if ( !IsAttached() )
746 {
747 return;
748 }
749 //else: have to modify the existing menu
750
751 UINT id;
752 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
753 if ( flagsOld == 0xFFFFFFFF )
754 {
755 wxLogLastError(wxT("GetMenuState"));
756
757 return;
758 }
759
760 if ( flagsOld & MF_POPUP )
761 {
762 // HIBYTE contains the number of items in the submenu in this case
763 flagsOld &= 0xff;
764 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
765 }
766 else
767 {
768 id = pos;
769 }
770
771 #ifdef __WXWINCE__
772 MENUITEMINFO info;
773 wxZeroMemory(info);
774 info.cbSize = sizeof(info);
775 info.fMask = MIIM_TYPE;
776 info.fType = MFT_STRING;
777 info.cch = label.Length();
778 info.dwTypeData = (LPTSTR) label.c_str();
779 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
780 {
781 wxLogLastError(wxT("SetMenuItemInfo"));
782 }
783
784 #else
785 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
786 id, label) == (int)0xFFFFFFFF )
787 {
788 wxLogLastError(wxT("ModifyMenu"));
789 }
790 #endif
791
792 Refresh();
793 }
794
795 wxString wxMenuBar::GetLabelTop(size_t pos) const
796 {
797 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
798 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
799
800 return m_titles[pos];
801 }
802
803 // ---------------------------------------------------------------------------
804 // wxMenuBar construction
805 // ---------------------------------------------------------------------------
806
807 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
808 {
809 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
810 if ( !menuOld )
811 return NULL;
812
813 m_titles[pos] = title;
814
815 if ( IsAttached() )
816 {
817 // can't use ModifyMenu() because it deletes the submenu it replaces
818 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
819 {
820 wxLogLastError(wxT("RemoveMenu"));
821 }
822
823 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
824 MF_BYPOSITION | MF_POPUP | MF_STRING,
825 (UINT)GetHmenuOf(menu), title) )
826 {
827 wxLogLastError(wxT("InsertMenu"));
828 }
829
830 #if wxUSE_ACCEL
831 if ( menuOld->HasAccels() || menu->HasAccels() )
832 {
833 // need to rebuild accell table
834 RebuildAccelTable();
835 }
836 #endif // wxUSE_ACCEL
837
838 Refresh();
839 }
840
841 return menuOld;
842 }
843
844 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
845 {
846 if ( !wxMenuBarBase::Insert(pos, menu, title) )
847 return FALSE;
848
849 m_titles.Insert(title, pos);
850
851 if ( IsAttached() )
852 {
853 #ifdef __WXWINCE__
854 if (!GetToolBar())
855 return FALSE;
856 TBBUTTON tbButton;
857 memset(&tbButton, 0, sizeof(TBBUTTON));
858 tbButton.iBitmap = I_IMAGENONE;
859 tbButton.fsState = TBSTATE_ENABLED;
860 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
861
862 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
863 tbButton.dwData = (DWORD)hPopupMenu;
864 wxString label = wxStripMenuCodes(title);
865 tbButton.iString = (int) label.c_str();
866
867 tbButton.idCommand = NewControlId();
868 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
869 {
870 wxLogLastError(wxT("TB_INSERTBUTTON"));
871 return FALSE;
872 }
873 #else
874 if ( !::InsertMenu(GetHmenu(), pos,
875 MF_BYPOSITION | MF_POPUP | MF_STRING,
876 (UINT)GetHmenuOf(menu), title) )
877 {
878 wxLogLastError(wxT("InsertMenu"));
879 }
880 #endif
881 #if wxUSE_ACCEL
882 if ( menu->HasAccels() )
883 {
884 // need to rebuild accell table
885 RebuildAccelTable();
886 }
887 #endif // wxUSE_ACCEL
888
889 Refresh();
890 }
891
892 return TRUE;
893 }
894
895 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
896 {
897 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
898 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
899
900 if ( !wxMenuBarBase::Append(menu, title) )
901 return FALSE;
902
903 m_titles.Add(title);
904
905 if ( IsAttached() )
906 {
907 #ifdef __WXWINCE__
908 if (!GetToolBar())
909 return FALSE;
910 TBBUTTON tbButton;
911 memset(&tbButton, 0, sizeof(TBBUTTON));
912 tbButton.iBitmap = I_IMAGENONE;
913 tbButton.fsState = TBSTATE_ENABLED;
914 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
915
916 size_t pos = GetMenuCount();
917 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
918 tbButton.dwData = (DWORD)hPopupMenu;
919 wxString label = wxStripMenuCodes(title);
920 tbButton.iString = (int) label.c_str();
921
922 tbButton.idCommand = NewControlId();
923 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
924 {
925 wxLogLastError(wxT("TB_INSERTBUTTON"));
926 return FALSE;
927 }
928 #else
929 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
930 (UINT)submenu, title) )
931 {
932 wxLogLastError(wxT("AppendMenu"));
933 }
934 #endif
935
936 #if wxUSE_ACCEL
937 if ( menu->HasAccels() )
938 {
939 // need to rebuild accelerator table
940 RebuildAccelTable();
941 }
942 #endif // wxUSE_ACCEL
943
944 Refresh();
945 }
946
947 return TRUE;
948 }
949
950 wxMenu *wxMenuBar::Remove(size_t pos)
951 {
952 wxMenu *menu = wxMenuBarBase::Remove(pos);
953 if ( !menu )
954 return NULL;
955
956 if ( IsAttached() )
957 {
958 #ifdef __WXWINCE__
959 if (GetToolBar())
960 {
961 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
962 {
963 wxLogLastError(wxT("TB_DELETEBUTTON"));
964 }
965 }
966 #else
967 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
968 {
969 wxLogLastError(wxT("RemoveMenu"));
970 }
971 #endif
972 #if wxUSE_ACCEL
973 if ( menu->HasAccels() )
974 {
975 // need to rebuild accell table
976 RebuildAccelTable();
977 }
978 #endif // wxUSE_ACCEL
979
980 Refresh();
981 }
982
983 m_titles.RemoveAt(pos);
984
985 return menu;
986 }
987
988 #if wxUSE_ACCEL
989
990 void wxMenuBar::RebuildAccelTable()
991 {
992 // merge the accelerators of all menus into one accel table
993 size_t nAccelCount = 0;
994 size_t i, count = GetMenuCount();
995 wxMenuList::iterator it;
996 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
997 {
998 nAccelCount += (*it)->GetAccelCount();
999 }
1000
1001 if ( nAccelCount )
1002 {
1003 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1004
1005 nAccelCount = 0;
1006 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1007 {
1008 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1009 }
1010
1011 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
1012
1013 delete [] accelEntries;
1014 }
1015 }
1016
1017 #endif // wxUSE_ACCEL
1018
1019 void wxMenuBar::Attach(wxFrame *frame)
1020 {
1021 wxMenuBarBase::Attach(frame);
1022
1023 #if wxUSE_ACCEL
1024 RebuildAccelTable();
1025 #endif // wxUSE_ACCEL
1026 }
1027
1028 void wxMenuBar::Detach()
1029 {
1030 wxMenuBarBase::Detach();
1031 }
1032
1033 #endif // wxUSE_MENUS