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