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