]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
Nth -> Item
[wxWidgets.git] / src / msw / menu.cpp
CommitLineData
2bda0e17
KB
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$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
c2dcfdef
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
c626a8b7 21 #pragma implementation "menu.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
c626a8b7 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_MENUS
32
2bda0e17 33#ifndef WX_PRECOMP
c626a8b7
VZ
34 #include "wx/frame.h"
35 #include "wx/menu.h"
36 #include "wx/utils.h"
0c589ad0 37 #include "wx/intl.h"
717a57c2 38 #include "wx/log.h"
2bda0e17
KB
39#endif
40
47d67540 41#if wxUSE_OWNER_DRAWN
c626a8b7 42 #include "wx/ownerdrw.h"
2bda0e17
KB
43#endif
44
45#include "wx/msw/private.h"
2bda0e17 46
39d2f9a7
JS
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
2bda0e17 61// other standard headers
2bda0e17
KB
62#include <string.h>
63
c626a8b7
VZ
64// ----------------------------------------------------------------------------
65// global variables
66// ----------------------------------------------------------------------------
67
68extern wxMenu *wxCurrentPopupMenu;
69
b8d3a4f1
VZ
70// ----------------------------------------------------------------------------
71// constants
72// ----------------------------------------------------------------------------
73
74// the (popup) menu title has this special id
75static const int idMenuTitle = -2;
76
77// ----------------------------------------------------------------------------
0472ece7 78// private functions
b8d3a4f1 79// ----------------------------------------------------------------------------
c626a8b7 80
0472ece7
VZ
81// make the given menu item default
82static void SetDefaultMenuItem(HMENU hmenu, UINT id)
83{
4676948b 84#ifndef __WXWINCE__
0472ece7
VZ
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 }
4676948b
JS
95#endif
96}
97
98#ifdef __WXWINCE__
99UINT 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;
0472ece7 108}
4676948b 109#endif
2bda0e17
KB
110
111// ============================================================================
112// implementation
113// ============================================================================
114
0472ece7
VZ
115IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
116IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
117
c2dcfdef
VZ
118// ---------------------------------------------------------------------------
119// wxMenu construction, adding and removing menu items
120// ---------------------------------------------------------------------------
2bda0e17
KB
121
122// Construct a menu with optional title (then use append)
717a57c2 123void wxMenu::Init()
c626a8b7 124{
ad9bb75f 125 m_doBreak = FALSE;
0472ece7 126 m_startRadioGroup = -1;
c626a8b7 127
717a57c2
VZ
128 // create the menu
129 m_hMenu = (WXHMENU)CreatePopupMenu();
130 if ( !m_hMenu )
131 {
f6bcfd97 132 wxLogLastError(wxT("CreatePopupMenu"));
717a57c2
VZ
133 }
134
135 // if we have a title, insert it in the beginning of the menu
c626a8b7
VZ
136 if ( !!m_title )
137 {
ad9bb75f
VZ
138 Append(idMenuTitle, m_title);
139 AppendSeparator();
c626a8b7 140 }
2bda0e17
KB
141}
142
143// The wxWindow destructor will take care of deleting the submenus.
b8d3a4f1 144wxMenu::~wxMenu()
2bda0e17 145{
717a57c2
VZ
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() )
c2dcfdef 150 {
ad9bb75f
VZ
151 if ( !::DestroyMenu(GetHmenu()) )
152 {
f6bcfd97 153 wxLogLastError(wxT("DestroyMenu"));
ad9bb75f 154 }
c2dcfdef 155 }
c626a8b7 156
ad9bb75f
VZ
157#if wxUSE_ACCEL
158 // delete accels
159 WX_CLEAR_ARRAY(m_accels);
160#endif // wxUSE_ACCEL
2bda0e17
KB
161}
162
b8d3a4f1 163void wxMenu::Break()
2bda0e17 164{
717a57c2 165 // this will take effect during the next call to Append()
c2dcfdef 166 m_doBreak = TRUE;
2bda0e17
KB
167}
168
0472ece7
VZ
169void wxMenu::Attach(wxMenuBarBase *menubar)
170{
171 wxMenuBase::Attach(menubar);
172
173 EndRadioGroup();
174}
175
717a57c2
VZ
176#if wxUSE_ACCEL
177
178int 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
190void wxMenu::UpdateAccel(wxMenuItem *item)
2bda0e17 191{
f6bcfd97 192 if ( item->IsSubMenu() )
717a57c2 193 {
f6bcfd97 194 wxMenu *submenu = item->GetSubMenu();
222ed1d6 195 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
f6bcfd97
BP
196 while ( node )
197 {
198 UpdateAccel(node->GetData());
199
200 node = node->GetNext();
201 }
717a57c2 202 }
f6bcfd97 203 else if ( !item->IsSeparator() )
717a57c2 204 {
f6bcfd97
BP
205 // find the (new) accel for this item
206 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
717a57c2 207 if ( accel )
f6bcfd97
BP
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 }
717a57c2 220 else
f6bcfd97
BP
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
b54e41c5 227 m_accels.RemoveAt(n);
f6bcfd97 228 }
717a57c2 229
f6bcfd97
BP
230 if ( IsAttached() )
231 {
232 m_menuBar->RebuildAccelTable();
233 }
42e69d6b 234 }
f6bcfd97 235 //else: it is a separator, they can't have accels, nothing to do
717a57c2
VZ
236}
237
238#endif // wxUSE_ACCEL
239
240// append a new item or submenu to the menu
241bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
242{
243#if wxUSE_ACCEL
244 UpdateAccel(pItem);
d427503c 245#endif // wxUSE_ACCEL
42e69d6b 246
c626a8b7 247 UINT flags = 0;
2bda0e17 248
c2dcfdef
VZ
249 // if "Break" has just been called, insert a menu break before this item
250 // (and don't forget to reset the flag)
c626a8b7
VZ
251 if ( m_doBreak ) {
252 flags |= MF_MENUBREAK;
253 m_doBreak = FALSE;
254 }
255
256 if ( pItem->IsSeparator() ) {
257 flags |= MF_SEPARATOR;
258 }
2bda0e17 259
c2dcfdef
VZ
260 // id is the numeric id for normal menu items and HMENU for submenus as
261 // required by ::AppendMenu() API
c626a8b7 262 UINT id;
c2dcfdef
VZ
263 wxMenu *submenu = pItem->GetSubMenu();
264 if ( submenu != NULL ) {
717a57c2
VZ
265 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
266
267 submenu->SetParent(this);
2bda0e17 268
c2dcfdef 269 id = (UINT)submenu->GetHMenu();
2bda0e17 270
c626a8b7
VZ
271 flags |= MF_POPUP;
272 }
273 else {
274 id = pItem->GetId();
275 }
2bda0e17 276
39d2f9a7
JS
277#ifdef __WXWINCE__
278 wxString strippedString;
279#endif
280
837e5743 281 LPCTSTR pData;
2bda0e17 282
47d67540 283#if wxUSE_OWNER_DRAWN
c626a8b7
VZ
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;
837e5743 287 pData = (LPCTSTR)pItem;
c626a8b7
VZ
288 }
289 else
2bda0e17 290#endif
c626a8b7
VZ
291 {
292 // menu is just a normal string (passed in data parameter)
293 flags |= MF_STRING;
8fb3a512 294
39d2f9a7
JS
295#ifdef __WXWINCE__
296 strippedString = wxStripMenuCodes(pItem->GetText());
297 pData = (wxChar*)strippedString.c_str();
298#else
f5166ed4 299 pData = (wxChar*)pItem->GetText().c_str();
39d2f9a7 300#endif
c626a8b7 301 }
2bda0e17 302
717a57c2
VZ
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 )
c626a8b7 314 {
f6bcfd97 315 wxLogLastError(wxT("Insert or AppendMenu"));
717a57c2
VZ
316
317 return FALSE;
c626a8b7 318 }
42e69d6b 319
0472ece7
VZ
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 }
42e69d6b
VZ
327#endif // __WIN32__
328
0472ece7
VZ
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
338void wxMenu::EndRadioGroup()
339{
0472ece7
VZ
340 // we're not inside a radio group any longer
341 m_startRadioGroup = -1;
2bda0e17
KB
342}
343
717a57c2 344bool wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 345{
0472ece7
VZ
346 wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") );
347
be15b995
VZ
348 bool check = FALSE;
349
546bfbea 350 if ( item->GetKind() == wxITEM_RADIO )
0472ece7 351 {
be15b995
VZ
352 int count = GetMenuItemCount();
353
0472ece7
VZ
354 if ( m_startRadioGroup == -1 )
355 {
356 // start a new radio group
be15b995
VZ
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);
222ed1d6 370 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
be15b995
VZ
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 }
0472ece7
VZ
380 }
381 }
382 else // not a radio item
383 {
384 EndRadioGroup();
385 }
386
be15b995
VZ
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;
2bda0e17
KB
399}
400
717a57c2 401bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 402{
717a57c2 403 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
2bda0e17
KB
404}
405
717a57c2 406wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 407{
717a57c2
VZ
408 // we need to find the items position in the child list
409 size_t pos;
222ed1d6 410 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2 411 for ( pos = 0; node; pos++ )
c626a8b7 412 {
717a57c2 413 if ( node->GetData() == item )
c626a8b7 414 break;
717a57c2
VZ
415
416 node = node->GetNext();
c626a8b7
VZ
417 }
418
717a57c2
VZ
419 // DoRemove() (unlike Remove) can only be called for existing item!
420 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 421
717a57c2
VZ
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];
c626a8b7 428
b54e41c5 429 m_accels.RemoveAt(n);
c626a8b7 430 }
717a57c2
VZ
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 {
f6bcfd97 437 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
438 }
439
4bc1afd5 440 if ( IsAttached() && m_menuBar->IsAttached() )
717a57c2
VZ
441 {
442 // otherwise, the chane won't be visible
443 m_menuBar->Refresh();
444 }
2bda0e17 445
717a57c2
VZ
446 // and from internal data structures
447 return wxMenuBase::DoRemove(item);
448}
d427503c 449
42e69d6b
VZ
450// ---------------------------------------------------------------------------
451// accelerator helpers
452// ---------------------------------------------------------------------------
453
717a57c2
VZ
454#if wxUSE_ACCEL
455
42e69d6b
VZ
456// create the wxAcceleratorEntries for our accels and put them into provided
457// array - return the number of accels we have
458size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
459{
460 size_t count = GetAccelCount();
461 for ( size_t n = 0; n < count; n++ )
462 {
974e8d94 463 *accels++ = *m_accels[n];
42e69d6b
VZ
464 }
465
466 return count;
467}
468
d427503c
VZ
469#endif // wxUSE_ACCEL
470
c2dcfdef 471// ---------------------------------------------------------------------------
717a57c2 472// set wxMenu title
c2dcfdef
VZ
473// ---------------------------------------------------------------------------
474
2bda0e17
KB
475void wxMenu::SetTitle(const wxString& label)
476{
c626a8b7
VZ
477 bool hasNoTitle = m_title.IsEmpty();
478 m_title = label;
b8d3a4f1 479
c50f1fb9 480 HMENU hMenu = GetHmenu();
b8d3a4f1 481
c626a8b7 482 if ( hasNoTitle )
b8d3a4f1 483 {
c626a8b7
VZ
484 if ( !label.IsEmpty() )
485 {
717a57c2
VZ
486 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
487 (unsigned)idMenuTitle, m_title) ||
488 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
c626a8b7 489 {
f6bcfd97 490 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
491 }
492 }
b8d3a4f1
VZ
493 }
494 else
495 {
c626a8b7
VZ
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 {
f6bcfd97 502 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
503 }
504 }
505 else
506 {
507 // modify the title
4676948b
JS
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
c626a8b7 521 if ( !ModifyMenu(hMenu, 0u,
717a57c2
VZ
522 MF_BYPOSITION | MF_STRING,
523 (unsigned)idMenuTitle, m_title) )
c626a8b7 524 {
f6bcfd97 525 wxLogLastError(wxT("ModifyMenu"));
c626a8b7 526 }
4676948b 527#endif
c626a8b7 528 }
b8d3a4f1 529 }
b8d3a4f1 530
42e69d6b 531#ifdef __WIN32__
c626a8b7
VZ
532 // put the title string in bold face
533 if ( !m_title.IsEmpty() )
a3f4e9e8 534 {
0472ece7 535 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 536 }
717a57c2 537#endif // Win32
2bda0e17
KB
538}
539
c2dcfdef
VZ
540// ---------------------------------------------------------------------------
541// event processing
542// ---------------------------------------------------------------------------
2bda0e17 543
debe6624 544bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 545{
a3f4e9e8
VZ
546 // ignore commands from the menu title
547
548 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
549 if ( id != (WXWORD)idMenuTitle )
550 {
3ca6a5f0
BP
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
4676948b
JS
555 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ;
556 SendEvent(id, menuState & MF_CHECKED);
a3f4e9e8
VZ
557 }
558
559 return TRUE;
2bda0e17
KB
560}
561
c2dcfdef
VZ
562// ---------------------------------------------------------------------------
563// other
564// ---------------------------------------------------------------------------
2bda0e17 565
717a57c2
VZ
566wxWindow *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;
c2dcfdef
VZ
574}
575
576// ---------------------------------------------------------------------------
2bda0e17 577// Menu Bar
c2dcfdef
VZ
578// ---------------------------------------------------------------------------
579
580void wxMenuBar::Init()
2bda0e17 581{
c626a8b7 582 m_eventHandler = this;
c626a8b7 583 m_hMenu = 0;
39d2f9a7
JS
584#ifdef __WXWINCE__
585 m_toolBar = NULL;
586#endif
cba2db0c 587}
2bda0e17 588
c2dcfdef
VZ
589wxMenuBar::wxMenuBar()
590{
591 Init();
592}
593
cba2db0c
JS
594wxMenuBar::wxMenuBar( long WXUNUSED(style) )
595{
c2dcfdef 596 Init();
2bda0e17
KB
597}
598
c2dcfdef 599wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
2bda0e17 600{
c2dcfdef
VZ
601 Init();
602
a8cfd0cb 603 m_titles.Alloc(count);
c2dcfdef 604
a8cfd0cb
VZ
605 for ( int i = 0; i < count; i++ )
606 {
607 m_menus.Append(menus[i]);
608 m_titles.Add(titles[i]);
2bda0e17 609
a8cfd0cb
VZ
610 menus[i]->Attach(this);
611 }
2bda0e17
KB
612}
613
b8d3a4f1 614wxMenuBar::~wxMenuBar()
2bda0e17 615{
39d2f9a7
JS
616 // In Windows CE, the menubar is always associated
617 // with a toolbar, which destroys the menu implicitly.
618#ifndef __WXWINCE__
7a0363dd
JS
619 // we should free Windows resources only if Windows doesn't do it for us
620 // which happens if we're attached to a frame
621 if (m_hMenu && !IsAttached())
622 {
623 ::DestroyMenu((HMENU)m_hMenu);
624 m_hMenu = (WXHMENU)NULL;
625 }
39d2f9a7 626#endif
c2dcfdef 627}
2bda0e17 628
c2dcfdef
VZ
629// ---------------------------------------------------------------------------
630// wxMenuBar helpers
631// ---------------------------------------------------------------------------
632
633void wxMenuBar::Refresh()
634{
065de612 635 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
c2dcfdef 636
39d2f9a7
JS
637#ifdef __WXWINCE__
638 if (GetToolBar())
639 {
640 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
641 }
642#else
1e6feb95 643 DrawMenuBar(GetHwndOf(GetFrame()));
39d2f9a7 644#endif
c2dcfdef
VZ
645}
646
647WXHMENU wxMenuBar::Create()
648{
39d2f9a7
JS
649#ifdef __WXWINCE__
650 if ( m_hMenu != 0 )
651 return m_hMenu;
652
653 if (!GetToolBar())
654 return 0;
655
656 HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
657 HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
658 if (hMenu)
659 {
660 TBBUTTON tbButton;
661 memset(&tbButton, 0, sizeof(TBBUTTON));
662 tbButton.iBitmap = I_IMAGENONE;
663 tbButton.fsState = TBSTATE_ENABLED;
664 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
665
666 size_t i;
667 for (i = 0; i < GetMenuCount(); i++)
668 {
669 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
670 tbButton.dwData = (DWORD)hPopupMenu;
671 wxString label = wxStripMenuCodes(GetLabelTop(i));
672 tbButton.iString = (int) label.c_str();
673
674 tbButton.idCommand = NewControlId();
675 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, i, (LPARAM)&tbButton))
676 {
677 wxLogLastError(wxT("TB_INSERTBUTTON"));
678 }
679 }
680 }
681 m_hMenu = (WXHMENU) hMenu;
682 return m_hMenu;
683#else
3ca6a5f0 684 if ( m_hMenu != 0 )
717a57c2 685 return m_hMenu;
1cf27c63 686
c2dcfdef 687 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 688
c2dcfdef 689 if ( !m_hMenu )
c626a8b7 690 {
f6bcfd97 691 wxLogLastError(wxT("CreateMenu"));
c626a8b7 692 }
c2dcfdef 693 else
c626a8b7 694 {
222ed1d6
MB
695 size_t count = GetMenuCount(), i;
696 wxMenuList::iterator it;
697 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
c2dcfdef
VZ
698 {
699 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
222ed1d6 700 (UINT)(*it)->GetHMenu(),
c2dcfdef
VZ
701 m_titles[i]) )
702 {
f6bcfd97 703 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
704 }
705 }
c626a8b7 706 }
c626a8b7 707
c2dcfdef 708 return m_hMenu;
39d2f9a7 709#endif
2bda0e17
KB
710}
711
c2dcfdef 712// ---------------------------------------------------------------------------
3dfac970 713// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
714// ---------------------------------------------------------------------------
715
3dfac970
VZ
716// NB: we don't support owner drawn top level items for now, if we do these
717// functions would have to be changed to use wxMenuItem as well
2bda0e17 718
a8cfd0cb 719void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 720{
717a57c2
VZ
721 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
722
723 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 724
c626a8b7 725 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
adc6fb16
VZ
726
727 Refresh();
2bda0e17
KB
728}
729
a8cfd0cb 730void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 731{
717a57c2
VZ
732 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
733
734 m_titles[pos] = label;
735
736 if ( !IsAttached() )
737 {
738 return;
739 }
740 //else: have to modify the existing menu
741
8cd85069 742 UINT id;
c2dcfdef
VZ
743 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
744 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 745 {
223d09f6 746 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
747
748 return;
749 }
750
751 if ( flagsOld & MF_POPUP )
752 {
753 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f
VZ
754 flagsOld &= 0xff;
755 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
c626a8b7
VZ
756 }
757 else
8cd85069
VZ
758 {
759 id = pos;
760 }
761
4676948b
JS
762#ifdef __WXWINCE__
763 MENUITEMINFO info;
764 wxZeroMemory(info);
765 info.cbSize = sizeof(info);
766 info.fMask = MIIM_TYPE;
767 info.fType = MFT_STRING;
768 info.cch = label.Length();
769 info.dwTypeData = (LPTSTR) label.c_str();
770 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
771 {
772 wxLogLastError(wxT("SetMenuItemInfo"));
773 }
774
775#else
c50f1fb9 776 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
4676948b 777 id, label) == (int)0xFFFFFFFF )
c2dcfdef 778 {
f6bcfd97 779 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 780 }
4676948b 781#endif
717a57c2
VZ
782
783 Refresh();
2bda0e17
KB
784}
785
a8cfd0cb 786wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 787{
717a57c2
VZ
788 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
789 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 790
717a57c2 791 return m_titles[pos];
2bda0e17
KB
792}
793
ad9bb75f
VZ
794// ---------------------------------------------------------------------------
795// wxMenuBar construction
796// ---------------------------------------------------------------------------
797
a8cfd0cb 798wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 799{
ad9bb75f
VZ
800 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
801 if ( !menuOld )
f7f50f49
VZ
802 return NULL;
803
ad9bb75f 804 m_titles[pos] = title;
a8cfd0cb 805
ad9bb75f 806 if ( IsAttached() )
a8cfd0cb 807 {
ad9bb75f
VZ
808 // can't use ModifyMenu() because it deletes the submenu it replaces
809 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
a8cfd0cb 810 {
f6bcfd97 811 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 812 }
1cf27c63 813
ad9bb75f
VZ
814 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
815 MF_BYPOSITION | MF_POPUP | MF_STRING,
816 (UINT)GetHmenuOf(menu), title) )
817 {
f6bcfd97 818 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
819 }
820
717a57c2
VZ
821#if wxUSE_ACCEL
822 if ( menuOld->HasAccels() || menu->HasAccels() )
823 {
824 // need to rebuild accell table
825 RebuildAccelTable();
826 }
827#endif // wxUSE_ACCEL
828
ad9bb75f 829 Refresh();
a8cfd0cb 830 }
ad9bb75f
VZ
831
832 return menuOld;
1cf27c63
UM
833}
834
a8cfd0cb 835bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 836{
ad9bb75f 837 if ( !wxMenuBarBase::Insert(pos, menu, title) )
a8cfd0cb 838 return FALSE;
1cf27c63 839
ad9bb75f
VZ
840 m_titles.Insert(title, pos);
841
ad9bb75f
VZ
842 if ( IsAttached() )
843 {
39d2f9a7
JS
844#ifdef __WXWINCE__
845 if (!GetToolBar())
846 return FALSE;
847 TBBUTTON tbButton;
848 memset(&tbButton, 0, sizeof(TBBUTTON));
849 tbButton.iBitmap = I_IMAGENONE;
850 tbButton.fsState = TBSTATE_ENABLED;
851 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
852
853 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
854 tbButton.dwData = (DWORD)hPopupMenu;
855 wxString label = wxStripMenuCodes(title);
856 tbButton.iString = (int) label.c_str();
857
858 tbButton.idCommand = NewControlId();
859 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
860 {
861 wxLogLastError(wxT("TB_INSERTBUTTON"));
862 return FALSE;
863 }
864#else
ad9bb75f
VZ
865 if ( !::InsertMenu(GetHmenu(), pos,
866 MF_BYPOSITION | MF_POPUP | MF_STRING,
867 (UINT)GetHmenuOf(menu), title) )
868 {
f6bcfd97 869 wxLogLastError(wxT("InsertMenu"));
ad9bb75f 870 }
39d2f9a7 871#endif
717a57c2
VZ
872#if wxUSE_ACCEL
873 if ( menu->HasAccels() )
874 {
875 // need to rebuild accell table
876 RebuildAccelTable();
877 }
878#endif // wxUSE_ACCEL
879
ad9bb75f 880 Refresh();
a8cfd0cb 881 }
ad9bb75f
VZ
882
883 return TRUE;
1cf27c63
UM
884}
885
ad9bb75f 886bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 887{
ad9bb75f
VZ
888 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
889 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
2bda0e17 890
717a57c2
VZ
891 if ( !wxMenuBarBase::Append(menu, title) )
892 return FALSE;
893
717a57c2
VZ
894 m_titles.Add(title);
895
ad9bb75f
VZ
896 if ( IsAttached() )
897 {
39d2f9a7
JS
898#ifdef __WXWINCE__
899 if (!GetToolBar())
900 return FALSE;
901 TBBUTTON tbButton;
902 memset(&tbButton, 0, sizeof(TBBUTTON));
903 tbButton.iBitmap = I_IMAGENONE;
904 tbButton.fsState = TBSTATE_ENABLED;
905 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
906
907 size_t pos = GetMenuCount();
908 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
909 tbButton.dwData = (DWORD)hPopupMenu;
910 wxString label = wxStripMenuCodes(title);
911 tbButton.iString = (int) label.c_str();
912
913 tbButton.idCommand = NewControlId();
914 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
915 {
916 wxLogLastError(wxT("TB_INSERTBUTTON"));
917 return FALSE;
918 }
919#else
ad9bb75f
VZ
920 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
921 (UINT)submenu, title) )
922 {
923 wxLogLastError(wxT("AppendMenu"));
924 }
39d2f9a7 925#endif
ad9bb75f 926
717a57c2
VZ
927#if wxUSE_ACCEL
928 if ( menu->HasAccels() )
929 {
39d2f9a7 930 // need to rebuild accelerator table
717a57c2
VZ
931 RebuildAccelTable();
932 }
933#endif // wxUSE_ACCEL
934
ad9bb75f
VZ
935 Refresh();
936 }
2bda0e17 937
a8cfd0cb 938 return TRUE;
2bda0e17
KB
939}
940
a8cfd0cb 941wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 942{
a8cfd0cb
VZ
943 wxMenu *menu = wxMenuBarBase::Remove(pos);
944 if ( !menu )
945 return NULL;
c626a8b7 946
ad9bb75f
VZ
947 if ( IsAttached() )
948 {
39d2f9a7
JS
949#ifdef __WXWINCE__
950 if (GetToolBar())
951 {
952 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
953 {
954 wxLogLastError(wxT("TB_DELETEBUTTON"));
955 }
956 }
957#else
ad9bb75f
VZ
958 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
959 {
f6bcfd97 960 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 961 }
39d2f9a7 962#endif
717a57c2
VZ
963#if wxUSE_ACCEL
964 if ( menu->HasAccels() )
965 {
966 // need to rebuild accell table
967 RebuildAccelTable();
968 }
969#endif // wxUSE_ACCEL
970
ad9bb75f
VZ
971 Refresh();
972 }
2bda0e17 973
ba8c1601 974 m_titles.RemoveAt(pos);
2bda0e17 975
a8cfd0cb 976 return menu;
2bda0e17
KB
977}
978
d427503c 979#if wxUSE_ACCEL
717a57c2
VZ
980
981void wxMenuBar::RebuildAccelTable()
982{
983 // merge the accelerators of all menus into one accel table
42e69d6b 984 size_t nAccelCount = 0;
a8cfd0cb 985 size_t i, count = GetMenuCount();
222ed1d6
MB
986 wxMenuList::iterator it;
987 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
42e69d6b 988 {
222ed1d6 989 nAccelCount += (*it)->GetAccelCount();
42e69d6b
VZ
990 }
991
5df1250b 992 if ( nAccelCount )
42e69d6b 993 {
5df1250b 994 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 995
5df1250b 996 nAccelCount = 0;
222ed1d6 997 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
5df1250b 998 {
222ed1d6 999 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
5df1250b
VZ
1000 }
1001
1002 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 1003
5df1250b
VZ
1004 delete [] accelEntries;
1005 }
717a57c2
VZ
1006}
1007
1008#endif // wxUSE_ACCEL
1009
1010void wxMenuBar::Attach(wxFrame *frame)
1011{
1e6feb95 1012 wxMenuBarBase::Attach(frame);
717a57c2 1013
717a57c2
VZ
1014#if wxUSE_ACCEL
1015 RebuildAccelTable();
d427503c 1016#endif // wxUSE_ACCEL
42e69d6b
VZ
1017}
1018
1cf27c63
UM
1019void wxMenuBar::Detach()
1020{
1e6feb95 1021 wxMenuBarBase::Detach();
2bda0e17
KB
1022}
1023
1e6feb95 1024#endif // wxUSE_MENUS