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