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