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