]> git.saurik.com Git - wxWidgets.git/blame - src/os2/menu.cpp
test wxExecute() in the sample
[wxWidgets.git] / src / os2 / menu.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: menu.cpp
3// Purpose: wxMenu, wxMenuBar, wxMenuItem
75f11ad7 4// Author: David Webster
0e320a79 5// Modified by:
75f11ad7 6// Created: 10/10/99
0e320a79 7// RCS-ID: $Id$
75f11ad7
DW
8// Copyright: (c) David Webster
9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
a4372af6
SN
12#ifdef __GNUG__
13 #pragma implementation "menu.h"
14#endif
15
75f11ad7
DW
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
0e320a79 18
75f11ad7 19#ifndef WX_PRECOMP
b59c98dd 20 #include "wx/app.h"
75f11ad7
DW
21 #include "wx/frame.h"
22 #include "wx/menu.h"
23 #include "wx/utils.h"
24 #include "wx/intl.h"
c5fb56c0 25 #include "wx/log.h"
75f11ad7 26#endif
0e320a79 27
75f11ad7
DW
28#if wxUSE_OWNER_DRAWN
29 #include "wx/ownerdrw.h"
0e320a79
DW
30#endif
31
75f11ad7 32#include "wx/os2/private.h"
0e320a79
DW
33
34// other standard headers
0e320a79
DW
35#include <string.h>
36
75f11ad7
DW
37// ----------------------------------------------------------------------------
38// global variables
39// ----------------------------------------------------------------------------
40
61243a51 41extern wxMenu* wxCurrentPopupMenu;
75f11ad7
DW
42
43// ----------------------------------------------------------------------------
44// constants
45// ----------------------------------------------------------------------------
46
61243a51
DW
47//
48// The (popup) menu title has this special id
49//
50static const int idMenuTitle = -2;
75f11ad7 51
f6bcfd97
BP
52//
53// The unique ID for Menus
54//
55#ifdef __VISAGECPP__
56USHORT wxMenu::m_nextMenuId = 0;
57#else
58static USHORT wxMenu::m_nextMenuId = 0;
59#endif
60
75f11ad7
DW
61// ----------------------------------------------------------------------------
62// macros
63// ----------------------------------------------------------------------------
64
75f11ad7
DW
65 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
66 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
0e320a79 67
2b33b728
SN
68// ----------------------------------------------------------------------------
69// static function for translating menu labels
70// ----------------------------------------------------------------------------
71
72static wxString TextToLabel(const wxString& rTitle)
73{
74 wxString Title;
75 const wxChar *pc;
987da0d4 76 for (pc = rTitle.c_str(); *pc != wxT('\0'); pc++ )
2b33b728
SN
77 {
78 if (*pc == wxT('&') )
79 {
80 if (*(pc+1) == wxT('&'))
81 {
82 pc++;
83 Title << wxT('&');
84 }
f6bcfd97 85 else
2b33b728
SN
86 Title << wxT('~');
87 }
2b33b728
SN
88 else
89 {
90 if ( *pc == wxT('~') )
91 {
92 // tildes must be doubled to prevent them from being
93 // interpreted as accelerator character prefix by PM ???
94 Title << *pc;
95 }
96 Title << *pc;
97 }
98 }
99 return Title;
100}
101
0e320a79
DW
102// ============================================================================
103// implementation
104// ============================================================================
105
75f11ad7
DW
106// ---------------------------------------------------------------------------
107// wxMenu construction, adding and removing menu items
108// ---------------------------------------------------------------------------
0e320a79 109
61243a51 110//
0e320a79 111// Construct a menu with optional title (then use append)
61243a51 112//
c5fb56c0 113void wxMenu::Init()
0e320a79 114{
61243a51
DW
115 m_bDoBreak = FALSE;
116
117 //
f23208ca
DW
118 // Create the menu (to be used as a submenu or a popup)
119 //
120 if ((m_hMenu = ::WinCreateWindow( HWND_DESKTOP
f6bcfd97 121 ,WC_MENU
f23208ca
DW
122 ,"Menu"
123 ,0L
124 ,0L
125 ,0L
126 ,0L
127 ,0L
128 ,NULLHANDLE
129 ,HWND_TOP
130 ,0L
131 ,NULL
132 ,NULL
a0606634 133 )) == 0)
61243a51
DW
134 {
135 wxLogLastError("WinLoadMenu");
136 }
a0606634
DW
137 m_vMenuData.iPosition = 0;
138 m_vMenuData.afStyle = MIS_SUBMENU | MIS_TEXT;
139 m_vMenuData.afAttribute = (USHORT)0;
f6bcfd97 140 m_vMenuData.id = m_nextMenuId++;
a0606634
DW
141 m_vMenuData.hwndSubMenu = m_hMenu;
142 m_vMenuData.hItem = NULLHANDLE;
61243a51
DW
143
144 //
145 // If we have a title, insert it in the beginning of the menu
146 //
f23208ca 147 if (!m_title.IsEmpty())
61243a51
DW
148 {
149 Append( idMenuTitle
150 ,m_title
151 );
c5fb56c0
DW
152 AppendSeparator();
153 }
938aa9c4
DW
154 for (int i = 0; i < 128; i++)
155 m_vAccels[i] = NULL;
156 m_nNextAccel = 0;
61243a51 157} // end of wxMenu::Init
0e320a79 158
61243a51 159//
0e320a79 160// The wxWindow destructor will take care of deleting the submenus.
61243a51 161//
0e320a79
DW
162wxMenu::~wxMenu()
163{
61243a51
DW
164 //
165 // We should free PM resources only if PM doesn't do it for us
c5fb56c0
DW
166 // which happens if we're attached to a menubar or a submenu of another
167 // menu
61243a51 168 if (!IsAttached() && !GetParent())
75f11ad7 169 {
61243a51 170 if (!::WinDestroyWindow((HWND)GetHmenu()) )
c5fb56c0 171 {
61243a51 172 wxLogLastError("WinDestroyWindow");
c5fb56c0 173 }
75f11ad7 174 }
0e320a79 175
c5fb56c0 176#if wxUSE_ACCEL
61243a51
DW
177 //
178 // Delete accels
179 //
f6bcfd97 180#if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
938aa9c4
DW
181 for (int i = 0; i < 128; i++)
182 {
183 if (m_vAccels[i])
184 {
185 delete m_vAccels[i];
186 m_vAccels[i] = NULL;
187 }
188 }
189// WX_CLEAR_ARRAY(m_vAccels);
f6bcfd97 190#endif
c5fb56c0 191#endif // wxUSE_ACCEL
61243a51 192} // end of wxMenu::~wxMenu
0e320a79
DW
193
194void wxMenu::Break()
195{
c5fb56c0 196 // this will take effect during the next call to Append()
61243a51
DW
197 m_bDoBreak = TRUE;
198} // end of wxMenu::Break
0e320a79 199
c5fb56c0
DW
200#if wxUSE_ACCEL
201
ab4fece8
DW
202void wxMenu::EndRadioGroup()
203{
204 //
205 // We're not inside a radio group any longer
206 //
207 m_nStartRadioGroup = -1;
208} // end of wxMenu::EndRadioGroup
209
61243a51
DW
210int wxMenu::FindAccel(
211 int nId
212) const
0e320a79 213{
61243a51 214 size_t n;
938aa9c4 215// size_t nCount = m_vAccels.GetCount();
61243a51 216
938aa9c4 217 for (n = 0; n < m_nNextAccel; n++)
c5fb56c0 218 {
938aa9c4
DW
219 if (m_vAccels[n] != NULL)
220 {
221 if (m_vAccels[n]->m_command == nId)
222 return n;
223 }
c5fb56c0 224 }
c5fb56c0 225 return wxNOT_FOUND;
61243a51 226} // end of wxMenu::FindAccel
75f11ad7 227
61243a51
DW
228void wxMenu::UpdateAccel(
229 wxMenuItem* pItem
230)
c5fb56c0 231{
a086de98
DW
232 if (pItem->IsSubMenu())
233 {
234 wxMenu* pSubmenu = pItem->GetSubMenu();
235 wxMenuItemList::Node* pNode = pSubmenu->GetMenuItems().GetFirst();
61243a51 236
a086de98
DW
237 while (pNode)
238 {
239 UpdateAccel(pNode->GetData());
240 pNode = pNode->GetNext();
241 }
242 }
243 else if (!pItem->IsSeparator())
c5fb56c0 244 {
61243a51 245 //
a086de98 246 // Find the (new) accel for this item
61243a51 247 //
a086de98 248 wxAcceleratorEntry* pAccel = wxGetAccelFromString(pItem->GetText());
61243a51 249 if (pAccel)
a086de98
DW
250 pAccel->m_command = pItem->GetId();
251
61243a51 252 //
a086de98 253 // Find the old one
61243a51 254 //
938aa9c4 255 size_t n = FindAccel(pItem->GetId());
61243a51 256
a086de98
DW
257 if (n == wxNOT_FOUND)
258 {
259 //
260 // No old, add new if any
261 //
262 if (pAccel)
938aa9c4
DW
263 {
264 if (m_nNextAccel < 128)
265 {
266 m_vAccels[m_nNextAccel] = pAccel;
267 m_nNextAccel++;
268 }
269 else
270 return; // skipping RebuildAccelTable() below
271 }
a086de98
DW
272 else
273 return; // skipping RebuildAccelTable() below
274 }
c5fb56c0 275 else
a086de98
DW
276 {
277 //
278 // Replace old with new or just remove the old one if no new
279 //
938aa9c4
DW
280 delete m_vAccels[n];
281 m_vAccels[n] = NULL;
c5fb56c0 282
a086de98
DW
283 if (pAccel)
284 m_vAccels[n] = pAccel;
a086de98
DW
285 }
286
287 if (IsAttached())
288 {
289 m_menuBar->RebuildAccelTable();
290 }
c5fb56c0 291 }
61243a51 292} // wxMenu::UpdateAccel
c5fb56c0 293
75f11ad7 294#endif // wxUSE_ACCEL
0e320a79 295
61243a51
DW
296//
297// Append a new item or submenu to the menu
298//
299bool wxMenu::DoInsertOrAppend(
300 wxMenuItem* pItem
301, size_t nPos
302)
c5fb56c0 303{
938aa9c4
DW
304 wxMenu* pSubmenu = pItem->GetSubMenu();
305 MENUITEM& rItem = (pSubmenu != NULL)?pSubmenu->m_vMenuData:
306 pItem->m_vMenuData;
307
a0606634
DW
308 ERRORID vError;
309 wxString sError;
45bedfdd 310 char zMsg[128];
c5fb56c0
DW
311#if wxUSE_ACCEL
312 UpdateAccel(pItem);
313#endif // wxUSE_ACCEL
0e320a79 314
f6bcfd97
BP
315 //
316 // rItem is the member MENUITEM for the menu items and the submenu's
317 // MENUITEM for submenus as required by ::MM_INSERTITEM message API
318 //
319
f6bcfd97
BP
320 if(pSubmenu != NULL)
321 {
322 wxASSERT_MSG(pSubmenu->GetHMenu(), wxT("invalid submenu"));
323 pSubmenu->SetParent(this);
324 rItem.afStyle |= MIS_SUBMENU | MIS_TEXT;
325 }
c3cea748 326
61243a51
DW
327 //
328 // If "Break" has just been called, insert a menu break before this item
75f11ad7 329 // (and don't forget to reset the flag)
61243a51
DW
330 //
331 if (m_bDoBreak)
332 {
f6bcfd97 333 rItem.afStyle |= MIS_BREAK;
61243a51 334 m_bDoBreak = FALSE;
75f11ad7 335 }
0e320a79 336
61243a51
DW
337 if (pItem->IsSeparator())
338 {
760ac9ab 339 rItem.afStyle |= MIS_SEPARATOR;
75f11ad7
DW
340 }
341
61243a51
DW
342 //
343 // Id is the numeric id for normal menu items and HMENU for submenus as
c3cea748 344 // required by ::MM_INSERTITEM message API
61243a51 345 //
c5fb56c0 346
f23208ca 347 if (pSubmenu != NULL)
61243a51
DW
348 {
349 wxASSERT_MSG(pSubmenu->GetHMenu(), wxT("invalid submenu"));
350 pSubmenu->SetParent(this);
75f11ad7 351
760ac9ab
DW
352 rItem.iPosition = 0; // submenus have a 0 position
353 rItem.id = (USHORT)pSubmenu->GetHMenu();
354 rItem.afStyle |= MIS_SUBMENU | MIS_TEXT;
75f11ad7 355 }
61243a51
DW
356 else
357 {
760ac9ab 358 rItem.id = pItem->GetId();
75f11ad7
DW
359 }
360
61243a51 361 BYTE* pData;
75f11ad7
DW
362
363#if wxUSE_OWNER_DRAWN
61243a51
DW
364 if (pItem->IsOwnerDrawn())
365 {
366 //
367 // Want to get {Measure|Draw}Item messages?
45bedfdd 368 // item draws itself, passing pointer to data doesn't work in OS/2
c3cea748 369 // Will eventually need to set the image handle somewhere into vItem.hItem
61243a51 370 //
f6bcfd97 371 rItem.afStyle |= MIS_OWNERDRAW;
23122f8c 372 pData = (BYTE*)NULL;
45bedfdd
DW
373 rItem.hItem = (HBITMAP)pItem->GetBitmap().GetHBITMAP();
374 pItem->m_vMenuData.afStyle = rItem.afStyle;
375 pItem->m_vMenuData.hItem = rItem.hItem;
75f11ad7
DW
376 }
377 else
378#endif
379 {
61243a51
DW
380 //
381 // Menu is just a normal string (passed in data parameter)
382 //
760ac9ab 383 rItem.afStyle |= MIS_TEXT;
c5fb56c0 384 pData = (char*)pItem->GetText().c_str();
75f11ad7 385 }
c5fb56c0 386
f6bcfd97 387 if (nPos == (size_t)-1)
75f11ad7 388 {
f6bcfd97
BP
389 rItem.iPosition = MIT_END;
390 }
391 else
392 {
393 rItem.iPosition = nPos;
c5fb56c0
DW
394 }
395
f6bcfd97
BP
396 APIRET rc;
397
398 rc = (APIRET)::WinSendMsg( GetHmenu()
399 ,MM_INSERTITEM
400 ,(MPARAM)&rItem
401 ,(MPARAM)pData
402 );
45bedfdd
DW
403#if wxUSE_OWNER_DRAWN
404 if (pItem->IsOwnerDrawn())
405 {
406 BOOL rc;
407 MENUITEM vMenuItem;
408
409 ::WinSendMsg( GetHmenu()
410 ,MM_QUERYITEM
411 ,MPFROM2SHORT( (USHORT)pItem->GetId()
412 ,(USHORT)(FALSE)
413 )
414 ,&vMenuItem
415 );
416 }
417#endif
a0606634 418 if (rc == MIT_MEMERROR || rc == MIT_ERROR)
c5fb56c0 419 {
a0606634
DW
420 vError = ::WinGetLastError(vHabmain);
421 sError = wxPMErrorToStr(vError);
422 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError);
c5fb56c0 423 wxLogLastError("Insert or AppendMenu");
c5fb56c0
DW
424 return FALSE;
425 }
426 else
427 {
61243a51
DW
428 //
429 // If we're already attached to the menubar, we must update it
430 //
1ec46a5b 431 if (IsAttached() && m_menuBar->IsAttached())
c5fb56c0
DW
432 {
433 m_menuBar->Refresh();
434 }
c5fb56c0 435 return TRUE;
75f11ad7 436 }
c5fb56c0 437 return FALSE;
61243a51 438} // end of wxMenu::DoInsertOrAppend
0e320a79 439
61243a51
DW
440bool wxMenu::DoAppend(
441 wxMenuItem* pItem
442)
0e320a79 443{
f95255e2
DW
444 wxCHECK_MSG( pItem, FALSE, _T("NULL item in wxMenu::DoAppend") );
445
446 bool bCheck = FALSE;
447
448 if (pItem->GetKind() == wxITEM_RADIO)
449 {
450 int nCount = GetMenuItemCount();
451
ab4fece8 452 if (m_nStartRadioGroup == -1)
f95255e2
DW
453 {
454 //
455 // Start a new radio group
456 //
ab4fece8 457 m_nStartRadioGroup = nCount;
f95255e2
DW
458
459 //
460 // For now it has just one element
461 //
462 pItem->SetAsRadioGroupStart();
ab4fece8 463 pItem->SetRadioGroupEnd(m_nStartRadioGroup);
f95255e2
DW
464
465 //
466 // Ensure that we have a checked item in the radio group
467 //
468 bCheck = TRUE;
469 }
470 else // extend the current radio group
471 {
472 //
473 // We need to update its end item
474 //
ab4fece8
DW
475 pItem->SetRadioGroupStart(m_nStartRadioGroup);
476 wxMenuItemList::Node* pNode = GetMenuItems().Item(m_nStartRadioGroup);
f95255e2 477
ab4fece8 478 if (pNode)
f95255e2 479 {
ab4fece8 480 pNode->GetData()->SetRadioGroupEnd(nCount);
f95255e2
DW
481 }
482 else
483 {
484 wxFAIL_MSG( _T("where is the radio group start item?") );
485 }
486 }
487 }
488 else // not a radio item
489 {
490 EndRadioGroup();
491 }
492 if (!wxMenuBase::DoAppend(pItem) || !DoInsertOrAppend(pItem))
493 {
494 return FALSE;
495 }
496 if (bCheck)
497 {
498 pItem->Check(TRUE);
499 }
500 return TRUE;
501} // end of wxMenu::DoInsert
0e320a79 502
61243a51
DW
503bool wxMenu::DoInsert(
504 size_t nPos
505, wxMenuItem* pItem
506)
0e320a79 507{
61243a51
DW
508 return ( wxMenuBase::DoInsert( nPos
509 ,pItem) &&
510 DoInsertOrAppend( pItem
511 ,nPos
512 ));
513} // end of wxMenu::DoInsert
0e320a79 514
61243a51
DW
515wxMenuItem* wxMenu::DoRemove(
516 wxMenuItem* pItem
517)
0e320a79 518{
61243a51
DW
519 //
520 // We need to find the items position in the child list
521 //
522 size_t nPos;
523 wxMenuItemList::Node* pNode = GetMenuItems().GetFirst();
524
525 for (nPos = 0; pNode; nPos++)
75f11ad7 526 {
61243a51 527 if (pNode->GetData() == pItem)
75f11ad7 528 break;
61243a51 529 pNode = pNode->GetNext();
0e320a79
DW
530 }
531
61243a51 532 //
c5fb56c0 533 // DoRemove() (unlike Remove) can only be called for existing item!
61243a51
DW
534 //
535 wxCHECK_MSG(pNode, NULL, wxT("bug in wxMenu::Remove logic"));
75f11ad7 536
c5fb56c0 537#if wxUSE_ACCEL
61243a51
DW
538 //
539 // Remove the corresponding accel from the accel table
540 //
541 int n = FindAccel(pItem->GetId());
75f11ad7 542
61243a51 543 if (n != wxNOT_FOUND)
75f11ad7 544 {
61243a51 545 delete m_vAccels[n];
938aa9c4 546 m_vAccels[n] = NULL;
c5fb56c0 547 }
61243a51
DW
548
549#endif // wxUSE_ACCEL
550 //
551 // Remove the item from the menu
552 //
553 ::WinSendMsg( GetHmenu()
554 ,MM_REMOVEITEM
555 ,MPFROM2SHORT(pItem->GetId(), TRUE)
556 ,(MPARAM)0
557 );
1ec46a5b 558 if (IsAttached() && m_menuBar->IsAttached())
61243a51
DW
559 {
560 //
561 // Otherwise, the chane won't be visible
562 //
c5fb56c0 563 m_menuBar->Refresh();
75f11ad7 564 }
0e320a79 565
61243a51
DW
566 //
567 // And from internal data structures
568 //
569 return wxMenuBase::DoRemove(pItem);
570} // end of wxMenu::DoRemove
0e320a79 571
75f11ad7
DW
572// ---------------------------------------------------------------------------
573// accelerator helpers
574// ---------------------------------------------------------------------------
575
c5fb56c0
DW
576#if wxUSE_ACCEL
577
61243a51
DW
578//
579// Create the wxAcceleratorEntries for our accels and put them into provided
75f11ad7 580// array - return the number of accels we have
61243a51
DW
581//
582size_t wxMenu::CopyAccels(
583 wxAcceleratorEntry* pAccels
584) const
75f11ad7 585{
61243a51
DW
586 size_t nCount = GetAccelCount();
587
588 for (size_t n = 0; n < nCount; n++)
75f11ad7 589 {
61243a51 590 *pAccels++ = *m_vAccels[n];
75f11ad7 591 }
61243a51
DW
592 return nCount;
593} // end of wxMenu::CopyAccels
0e320a79 594
75f11ad7
DW
595#endif // wxUSE_ACCEL
596
597// ---------------------------------------------------------------------------
c5fb56c0 598// set wxMenu title
75f11ad7
DW
599// ---------------------------------------------------------------------------
600
61243a51
DW
601void wxMenu::SetTitle(
602 const wxString& rLabel
603)
0e320a79 604{
61243a51
DW
605 bool bHasNoTitle = m_title.IsEmpty();
606 HWND hMenu = GetHmenu();
0e320a79 607
61243a51
DW
608 m_title = rLabel;
609 if (bHasNoTitle)
0e320a79 610 {
61243a51 611 if (!rLabel.IsEmpty())
75f11ad7 612 {
61243a51 613 if (!::WinSetWindowText(hMenu, rLabel.c_str()))
75f11ad7 614 {
61243a51 615 wxLogLastError("SetMenuTitle");
75f11ad7
DW
616 }
617 }
0e320a79 618 }
75f11ad7 619 else
0e320a79 620 {
61243a51 621 if (rLabel.IsEmpty() )
0e320a79 622 {
61243a51
DW
623 ::WinSendMsg( GetHmenu()
624 ,MM_REMOVEITEM
625 ,MPFROM2SHORT(hMenu, TRUE)
626 ,(MPARAM)0
627 );
0e320a79 628 }
75f11ad7 629 else
0e320a79 630 {
61243a51
DW
631 //
632 // Modify the title
633 //
634 if (!::WinSetWindowText(hMenu, rLabel.c_str()))
75f11ad7 635 {
61243a51 636 wxLogLastError("SetMenuTitle");
75f11ad7 637 }
0e320a79
DW
638 }
639 }
61243a51 640} // end of wxMenu::SetTitle
0e320a79 641
75f11ad7
DW
642// ---------------------------------------------------------------------------
643// event processing
644// ---------------------------------------------------------------------------
645
61243a51
DW
646bool wxMenu::OS2Command(
647 WXUINT WXUNUSED(uParam)
648, WXWORD vId
649)
0e320a79 650{
61243a51
DW
651 //
652 // Ignore commands from the menu title
653 //
75f11ad7 654
61243a51 655 if (vId != (WXWORD)idMenuTitle)
75f11ad7 656 {
0367c1c0
DW
657 SendEvent( vId
658 ,(int)::WinSendMsg( GetHmenu()
659 ,MM_QUERYITEMATTR
660 ,(MPARAM)vId
661 ,(MPARAM)MIA_CHECKED
662 )
663 );
61243a51 664 }
75f11ad7 665 return TRUE;
61243a51 666} // end of wxMenu::OS2Command
0e320a79 667
75f11ad7
DW
668// ---------------------------------------------------------------------------
669// other
670// ---------------------------------------------------------------------------
671
61243a51 672wxWindow* wxMenu::GetWindow() const
c5fb56c0 673{
61243a51 674 if (m_invokingWindow != NULL)
c5fb56c0
DW
675 return m_invokingWindow;
676 else if ( m_menuBar != NULL)
677 return m_menuBar->GetFrame();
678
679 return NULL;
61243a51 680} // end of wxMenu::GetWindow
0e320a79 681
45bedfdd
DW
682// recursive search for item by id
683wxMenuItem* wxMenu::FindItem(
684 int nItemId
685, ULONG hItem
686, wxMenu** ppItemMenu
687) const
688{
689 if ( ppItemMenu )
690 *ppItemMenu = NULL;
691
692 wxMenuItem* pItem = NULL;
693
694 for ( wxMenuItemList::Node *node = m_items.GetFirst();
695 node && !pItem;
696 node = node->GetNext() )
697 {
698 pItem = node->GetData();
699
700 if ( pItem->GetId() == nItemId && pItem->m_vMenuData.hItem == hItem)
701 {
702 if ( ppItemMenu )
703 *ppItemMenu = (wxMenu *)this;
704 }
705 else if ( pItem->IsSubMenu() )
706 {
72594e90
DW
707 pItem = pItem->GetSubMenu()->FindItem( nItemId
708 ,hItem
709 ,ppItemMenu
710 );
711 if (pItem)
712 break;
45bedfdd
DW
713 }
714 else
715 {
716 // don't exit the loop
717 pItem = NULL;
718 }
719 }
720 return pItem;
721} // end of wxMenu::FindItem
722
75f11ad7 723// ---------------------------------------------------------------------------
0e320a79 724// Menu Bar
75f11ad7
DW
725// ---------------------------------------------------------------------------
726
727void wxMenuBar::Init()
0e320a79
DW
728{
729 m_eventHandler = this;
e58dab20 730 m_menuBarFrame = NULL;
75f11ad7 731 m_hMenu = 0;
61243a51 732} // end of wxMenuBar::Init
0e320a79 733
75f11ad7
DW
734wxMenuBar::wxMenuBar()
735{
736 Init();
61243a51 737} // end of wxMenuBar::wxMenuBar
0e320a79 738
61243a51
DW
739wxMenuBar::wxMenuBar(
740 long WXUNUSED(lStyle)
741)
0e320a79 742{
75f11ad7 743 Init();
61243a51 744} // end of wxMenuBar::wxMenuBar
75f11ad7 745
61243a51
DW
746wxMenuBar::wxMenuBar(
747 int nCount
748, wxMenu* vMenus[]
749, const wxString sTitles[]
750)
75f11ad7
DW
751{
752 Init();
753
61243a51
DW
754 m_titles.Alloc(nCount);
755 for ( int i = 0; i < nCount; i++ )
c5fb56c0 756 {
61243a51
DW
757 m_menus.Append(vMenus[i]);
758 m_titles.Add(sTitles[i]);
759 vMenus[i]->Attach(this);
c5fb56c0 760 }
61243a51 761} // end of wxMenuBar::wxMenuBar
0e320a79
DW
762
763wxMenuBar::~wxMenuBar()
764{
61243a51 765} // end of wxMenuBar::~wxMenuBar
0e320a79 766
75f11ad7
DW
767// ---------------------------------------------------------------------------
768// wxMenuBar helpers
769// ---------------------------------------------------------------------------
770
61243a51 771void wxMenuBar::Refresh()
75f11ad7 772{
c5fb56c0 773 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
75f11ad7 774
e58dab20 775 WinSendMsg(GetWinHwnd(m_menuBarFrame), WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0);
f23208ca 776} // end of wxMenuBar::Refresh
75f11ad7
DW
777
778WXHMENU wxMenuBar::Create()
779{
61243a51 780 MENUITEM vItem;
f23208ca 781 HWND hFrame;
61243a51 782
75f11ad7 783 if (m_hMenu != 0 )
c5fb56c0 784 return m_hMenu;
75f11ad7 785
61243a51
DW
786 wxCHECK_MSG(!m_hMenu, TRUE, wxT("menubar already created"));
787
f23208ca
DW
788 //
789 // Menubars should be associated with a frame otherwise they are popups
790 //
e58dab20
DW
791 if (m_menuBarFrame != NULL)
792 hFrame = GetWinHwnd(m_menuBarFrame);
f23208ca
DW
793 else
794 hFrame = HWND_DESKTOP;
61243a51
DW
795 //
796 // Create an empty menu and then fill it with insertions
797 //
f6bcfd97
BP
798 if ((m_hMenu = ::WinCreateWindow( hFrame
799 ,WC_MENU
800 ,(PSZ)NULL
801 ,MS_ACTIONBAR | WS_SYNCPAINT | WS_VISIBLE
802 ,0L
803 ,0L
804 ,0L
805 ,0L
806 ,hFrame
807 ,HWND_TOP
808 ,FID_MENU
809 ,NULL
810 ,NULL
811 )) == 0)
75f11ad7 812 {
f6bcfd97 813 wxLogLastError("WinLoadMenu");
75f11ad7
DW
814 }
815 else
816 {
61243a51
DW
817 size_t nCount = GetMenuCount();
818
819 for (size_t i = 0; i < nCount; i++)
75f11ad7 820 {
c0dbf1fb
DW
821 APIRET rc;
822 ERRORID vError;
823 wxString sError;
f6bcfd97 824 HWND hSubMenu;
c3cea748
DW
825
826 //
827 // Set the parent and owner of the submenues to be the menubar, not the desktop
828 //
f6bcfd97
BP
829 hSubMenu = m_menus[i]->m_vMenuData.hwndSubMenu;
830 if (!::WinSetParent(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu, FALSE))
c3cea748
DW
831 {
832 vError = ::WinGetLastError(vHabmain);
833 sError = wxPMErrorToStr(vError);
834 wxLogError("Error setting parent for submenu. Error: %s\n", sError);
835 return NULLHANDLE;
836 }
837
f6bcfd97 838 if (!::WinSetOwner(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu))
c3cea748
DW
839 {
840 vError = ::WinGetLastError(vHabmain);
841 sError = wxPMErrorToStr(vError);
842 wxLogError("Error setting parent for submenu. Error: %s\n", sError);
843 return NULLHANDLE;
844 }
c0dbf1fb 845
dae16775
DW
846 m_menus[i]->m_vMenuData.iPosition = i;
847
f6bcfd97 848 rc = (APIRET)::WinSendMsg(m_hMenu, MM_INSERTITEM, (MPARAM)&m_menus[i]->m_vMenuData, (MPARAM)m_titles[i].c_str());
c0dbf1fb
DW
849 if (rc == MIT_MEMERROR || rc == MIT_ERROR)
850 {
851 vError = ::WinGetLastError(vHabmain);
852 sError = wxPMErrorToStr(vError);
853 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError);
854 return NULLHANDLE;
855 }
75f11ad7
DW
856 }
857 }
f6bcfd97 858 return m_hMenu;
61243a51 859} // end of wxMenuBar::Create
0e320a79 860
75f11ad7 861// ---------------------------------------------------------------------------
c5fb56c0 862// wxMenuBar functions to work with the top level submenus
75f11ad7
DW
863// ---------------------------------------------------------------------------
864
61243a51 865//
c5fb56c0
DW
866// NB: we don't support owner drawn top level items for now, if we do these
867// functions would have to be changed to use wxMenuItem as well
61243a51
DW
868//
869void wxMenuBar::EnableTop(
870 size_t nPos
871, bool bEnable
872)
0e320a79 873{
61243a51
DW
874 wxCHECK_RET(IsAttached(), wxT("doesn't work with unattached menubars"));
875 USHORT uFlag = 0;
876 SHORT nId;
0e320a79 877
61243a51
DW
878 if(!bEnable)
879 uFlag = MIA_DISABLED;
75f11ad7 880
61243a51
DW
881 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
882 if (nId == MIT_ERROR)
883 {
884 wxLogLastError("LogLastError");
885 return;
886 }
f6bcfd97 887 ::WinSendMsg((HWND)m_hMenu, MM_SETITEMATTR, MPFROM2SHORT(nId, TRUE), MPFROM2SHORT(MIA_DISABLED, uFlag));
c5fb56c0 888 Refresh();
61243a51 889} // end of wxMenuBar::EnableTop
75f11ad7 890
61243a51
DW
891void wxMenuBar::SetLabelTop(
892 size_t nPos
893, const wxString& rLabel
894)
75f11ad7 895{
61243a51
DW
896 SHORT nId;
897 MENUITEM vItem;
75f11ad7 898
61243a51
DW
899 wxCHECK_RET(nPos < GetMenuCount(), wxT("invalid menu index"));
900 m_titles[nPos] = rLabel;
75f11ad7 901
61243a51 902 if (!IsAttached())
c5fb56c0
DW
903 {
904 return;
905 }
75f11ad7 906
61243a51
DW
907 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
908 if (nId == MIT_ERROR)
75f11ad7 909 {
61243a51 910 wxLogLastError("LogLastError");
75f11ad7
DW
911 return;
912 }
61243a51
DW
913 if(!::WinSendMsg( (HWND)m_hMenu
914 ,MM_QUERYITEM
915 ,MPFROM2SHORT(nId, TRUE)
916 ,MPARAM(&vItem)
917 ))
75f11ad7 918 {
61243a51 919 wxLogLastError("QueryItem");
c5fb56c0 920 }
61243a51 921 nId = vItem.id;
c5fb56c0 922
61243a51 923 if (::WinSendMsg(GetHmenu(), MM_SETITEMTEXT, MPFROMSHORT(nId), (MPARAM)rLabel.c_str()));
c5fb56c0
DW
924 {
925 wxLogLastError("ModifyMenu");
75f11ad7 926 }
c5fb56c0 927 Refresh();
61243a51 928} // end of wxMenuBar::SetLabelTop
0e320a79 929
61243a51
DW
930wxString wxMenuBar::GetLabelTop(
931 size_t nPos
932) const
0e320a79 933{
61243a51 934 wxCHECK_MSG( nPos < GetMenuCount(), wxEmptyString,
c5fb56c0 935 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
61243a51
DW
936 return m_titles[nPos];
937} // end of wxMenuBar::GetLabelTop
75f11ad7 938
c5fb56c0
DW
939// ---------------------------------------------------------------------------
940// wxMenuBar construction
941// ---------------------------------------------------------------------------
75f11ad7 942
61243a51
DW
943wxMenu* wxMenuBar::Replace(
944 size_t nPos
945, wxMenu* pMenu
946, const wxString& rTitle
947)
75f11ad7 948{
61243a51 949 SHORT nId;
2b33b728 950 wxString Title = TextToLabel(rTitle);
61243a51
DW
951 wxMenu* pMenuOld = wxMenuBarBase::Replace( nPos
952 ,pMenu
2b33b728 953 ,Title
61243a51
DW
954 );
955
956
957 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
958 if (nId == MIT_ERROR)
959 {
960 wxLogLastError("LogLastError");
961 return NULL;
962 }
963 if (!pMenuOld)
c5fb56c0 964 return FALSE;
2b33b728 965 m_titles[nPos] = Title;
61243a51 966 if (IsAttached())
75f11ad7 967 {
f6bcfd97 968 ::WinSendMsg((HWND)m_hMenu, MM_REMOVEITEM, MPFROM2SHORT(nId, TRUE), (MPARAM)0);
2b33b728 969 ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)Title.c_str());
c5fb56c0
DW
970
971#if wxUSE_ACCEL
61243a51 972 if (pMenuOld->HasAccels() || pMenu->HasAccels())
c5fb56c0 973 {
61243a51
DW
974 //
975 // Need to rebuild accell table
976 //
c5fb56c0
DW
977 RebuildAccelTable();
978 }
979#endif // wxUSE_ACCEL
c5fb56c0
DW
980 Refresh();
981 }
61243a51
DW
982 return pMenuOld;
983} // end of wxMenuBar::Replace
75f11ad7 984
61243a51
DW
985bool wxMenuBar::Insert(
986 size_t nPos
987, wxMenu* pMenu
988, const wxString& rTitle
989)
75f11ad7 990{
2b33b728 991 wxString Title = TextToLabel(rTitle);
61243a51
DW
992 if (!wxMenuBarBase::Insert( nPos
993 ,pMenu
2b33b728 994 ,Title
61243a51 995 ))
c5fb56c0 996 return FALSE;
75f11ad7 997
2b33b728 998 m_titles.Insert( Title
61243a51
DW
999 ,nPos
1000 );
75f11ad7 1001
61243a51
DW
1002 if (IsAttached())
1003 {
2b33b728 1004 ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)Title.c_str());
c5fb56c0 1005#if wxUSE_ACCEL
61243a51 1006 if (pMenu->HasAccels())
c5fb56c0
DW
1007 {
1008 // need to rebuild accell table
1009 RebuildAccelTable();
1010 }
1011#endif // wxUSE_ACCEL
c5fb56c0 1012 Refresh();
75f11ad7 1013 }
c5fb56c0 1014 return TRUE;
61243a51 1015} // end of wxMenuBar::Insert
75f11ad7 1016
61243a51
DW
1017bool wxMenuBar::Append(
1018 wxMenu* pMenu
1019, const wxString& rTitle
1020)
0e320a79 1021{
61243a51
DW
1022 WXHMENU hSubmenu = pMenu ? pMenu->GetHMenu() : 0;
1023
1024 wxCHECK_MSG(hSubmenu, FALSE, wxT("can't append invalid menu to menubar"));
0e320a79 1025
2b33b728
SN
1026 wxString Title = TextToLabel(rTitle);
1027 if (!wxMenuBarBase::Append(pMenu, Title))
c5fb56c0 1028 return FALSE;
0e320a79 1029
2b33b728 1030 m_titles.Add(Title);
c5fb56c0 1031
c5fb56c0 1032 if ( IsAttached() )
0e320a79 1033 {
61243a51 1034 pMenu->m_vMenuData.iPosition = MIT_END;
2b33b728 1035 ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)Title.c_str());
c5fb56c0 1036#if wxUSE_ACCEL
61243a51 1037 if (pMenu->HasAccels())
c5fb56c0 1038 {
61243a51
DW
1039 //
1040 // Need to rebuild accell table
1041 //
c5fb56c0
DW
1042 RebuildAccelTable();
1043 }
1044#endif // wxUSE_ACCEL
c5fb56c0
DW
1045 Refresh();
1046 }
c5fb56c0 1047 return TRUE;
61243a51 1048} // end of wxMenuBar::Append
0e320a79 1049
61243a51
DW
1050wxMenu* wxMenuBar::Remove(
1051 size_t nPos
1052)
0e320a79 1053{
61243a51
DW
1054 wxMenu* pMenu = wxMenuBarBase::Remove(nPos);
1055 SHORT nId;
1056
1057 if (!pMenu)
c5fb56c0 1058 return NULL;
0e320a79 1059
61243a51
DW
1060 nId = SHORT1FROMMR(::WinSendMsg((HWND)GetHmenu(), MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
1061 if (nId == MIT_ERROR)
1062 {
1063 wxLogLastError("LogLastError");
1064 return NULL;
1065 }
1066 if (IsAttached())
1067 {
f6bcfd97 1068 ::WinSendMsg((HWND)GetHmenu(), MM_REMOVEITEM, MPFROM2SHORT(nId, TRUE), (MPARAM)0);
0e320a79 1069
c5fb56c0 1070#if wxUSE_ACCEL
61243a51 1071 if (pMenu->HasAccels())
c5fb56c0 1072 {
61243a51
DW
1073 //
1074 // Need to rebuild accell table
1075 //
c5fb56c0
DW
1076 RebuildAccelTable();
1077 }
1078#endif // wxUSE_ACCEL
c5fb56c0 1079 Refresh();
0e320a79 1080 }
61243a51
DW
1081 m_titles.Remove(nPos);
1082 return pMenu;
1083} // end of wxMenuBar::Remove
75f11ad7
DW
1084
1085#if wxUSE_ACCEL
c5fb56c0
DW
1086
1087void wxMenuBar::RebuildAccelTable()
1088{
61243a51
DW
1089 //
1090 // Merge the accelerators of all menus into one accel table
1091 //
1092 size_t nAccelCount = 0;
1093 size_t i;
1094 size_t nCount = GetMenuCount();
1095
1096 for (i = 0; i < nCount; i++)
75f11ad7
DW
1097 {
1098 nAccelCount += m_menus[i]->GetAccelCount();
1099 }
1100
61243a51 1101 if (nAccelCount)
0e320a79 1102 {
61243a51 1103 wxAcceleratorEntry* pAccelEntries = new wxAcceleratorEntry[nAccelCount];
75f11ad7
DW
1104
1105 nAccelCount = 0;
61243a51 1106 for (i = 0; i < nCount; i++)
75f11ad7 1107 {
61243a51 1108 nAccelCount += m_menus[i]->CopyAccels(&pAccelEntries[nAccelCount]);
75f11ad7 1109 }
61243a51
DW
1110 m_vAccelTable = wxAcceleratorTable( nAccelCount
1111 ,pAccelEntries
1112 );
1113 delete [] pAccelEntries;
0e320a79 1114 }
61243a51 1115} // end of wxMenuBar::RebuildAccelTable
c5fb56c0
DW
1116
1117#endif // wxUSE_ACCEL
1118
61243a51
DW
1119void wxMenuBar::Attach(
1120 wxFrame* pFrame
1121)
c5fb56c0
DW
1122{
1123 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
c5fb56c0
DW
1124
1125#if wxUSE_ACCEL
1126 RebuildAccelTable();
f6bcfd97
BP
1127 //
1128 // Ensure the accelerator table is set to the frame (not the client!)
1129 //
1130 if (!::WinSetAccelTable( vHabmain
e604d44b 1131 ,(HWND)pFrame->GetHWND()
f6bcfd97
BP
1132 ,m_vAccelTable.GetHACCEL()
1133 ))
1134 wxLogLastError("WinSetAccelTable");
75f11ad7 1135#endif // wxUSE_ACCEL
61243a51 1136} // end of wxMenuBar::Attach
0e320a79 1137
75f11ad7 1138void wxMenuBar::Detach()
0e320a79 1139{
61243a51 1140 ::WinDestroyWindow((HWND)m_hMenu);
c5fb56c0 1141 m_hMenu = (WXHMENU)NULL;
e58dab20 1142 m_menuBarFrame = NULL;
61243a51 1143} // end of wxMenuBar::Detach
75f11ad7
DW
1144
1145// ---------------------------------------------------------------------------
1146// wxMenuBar searching for menu items
1147// ---------------------------------------------------------------------------
1148
61243a51 1149//
75f11ad7 1150// Find the itemString in menuString, and return the item id or wxNOT_FOUND
61243a51
DW
1151//
1152int wxMenuBar::FindMenuItem(
1153 const wxString& rMenuString
1154, const wxString& rItemString
1155) const
75f11ad7 1156{
61243a51
DW
1157 wxString sMenuLabel = wxStripMenuCodes(rMenuString);
1158 size_t nCount = GetMenuCount();
1159
1160 for (size_t i = 0; i < nCount; i++)
75f11ad7 1161 {
61243a51 1162 wxString sTitle = wxStripMenuCodes(m_titles[i]);
75f11ad7 1163
61243a51
DW
1164 if (rMenuString == sTitle)
1165 return m_menus[i]->FindItem(rItemString);
1166 }
75f11ad7 1167 return wxNOT_FOUND;
61243a51 1168} // end of wxMenuBar::FindMenuItem
75f11ad7 1169
61243a51
DW
1170wxMenuItem* wxMenuBar::FindItem(
1171 int nId
1172, wxMenu** ppItemMenu
1173) const
75f11ad7 1174{
61243a51
DW
1175 if (ppItemMenu)
1176 *ppItemMenu = NULL;
1177
1178 wxMenuItem* pItem = NULL;
1179 size_t nCount = GetMenuCount();
0e320a79 1180
61243a51 1181 for (size_t i = 0; !pItem && (i < nCount); i++)
75f11ad7 1182 {
61243a51
DW
1183 pItem = m_menus[i]->FindItem( nId
1184 ,ppItemMenu
1185 );
75f11ad7 1186 }
61243a51
DW
1187 return pItem;
1188} // end of wxMenuBar::FindItem
75f11ad7 1189
45bedfdd
DW
1190wxMenuItem* wxMenuBar::FindItem(
1191 int nId
1192, ULONG hItem
1193, wxMenu** ppItemMenu
1194) const
1195{
1196 if (ppItemMenu)
1197 *ppItemMenu = NULL;
1198
1199 wxMenuItem* pItem = NULL;
1200 size_t nCount = GetMenuCount();
1201
1202 for (size_t i = 0; !pItem && (i < nCount); i++)
1203 {
1204 pItem = m_menus[i]->FindItem( nId
1205 ,hItem
1206 ,ppItemMenu
1207 );
1208 }
1209 return pItem;
1210} // end of wxMenuBar::FindItem
dae16775 1211