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