1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: William Osborne
8 // Copyright: (c) William Osborne
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "menu.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
42 #include "wx/ownerdrw.h"
45 // other standard headers
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 extern wxMenu
*wxCurrentPopupMenu
;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // the (popup) menu title has this special id
61 static const int idMenuTitle
= -3;
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // ============================================================================
69 // ============================================================================
71 #include <wx/listimpl.cpp>
73 WX_DEFINE_LIST( wxMenuInfoList
) ;
75 #if wxUSE_EXTENDED_RTTI
77 WX_DEFINE_FLAGS( wxMenuStyle
)
79 wxBEGIN_FLAGS( wxMenuStyle
)
80 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
81 wxEND_FLAGS( wxMenuStyle
)
83 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
85 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
87 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
89 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
92 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
93 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
94 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
95 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
96 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
97 wxEND_PROPERTIES_TABLE()
99 wxBEGIN_HANDLERS_TABLE(wxMenu
)
100 wxEND_HANDLERS_TABLE()
102 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
104 WX_DEFINE_FLAGS( wxMenuBarStyle
)
106 wxBEGIN_FLAGS( wxMenuBarStyle
)
107 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
108 wxEND_FLAGS( wxMenuBarStyle
)
110 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
111 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
116 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
118 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
120 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
121 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
122 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
123 wxEND_PROPERTIES_TABLE()
125 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
126 wxEND_HANDLERS_TABLE()
128 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
130 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
132 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
134 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
137 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
138 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
139 wxEND_PROPERTIES_TABLE()
141 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
142 wxEND_HANDLERS_TABLE()
144 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
147 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
148 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
149 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
152 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
154 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
155 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
156 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
158 wxMenuInfo
* info
= new wxMenuInfo() ;
159 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
160 list
->Append( info
) ;
165 // ---------------------------------------------------------------------------
166 // wxMenu construction, adding and removing menu items
167 // ---------------------------------------------------------------------------
169 // Construct a menu with optional title (then use append)
174 // The wxWindow destructor will take care of deleting the submenus.
183 void wxMenu::Attach(wxMenuBarBase
*menubar
)
185 wxMenuBase::Attach(menubar
);
190 int wxMenu::FindAccel(int id
) const
195 void wxMenu::UpdateAccel(wxMenuItem
*item
)
199 #endif // wxUSE_ACCEL
201 // append a new item or submenu to the menu
202 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
204 if ( IsAttached() && GetMenuBar()->IsAttached() )
206 // Regenerate the menu resource
207 GetMenuBar()->Refresh();
213 void wxMenu::EndRadioGroup()
217 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
219 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
221 if(!wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
))
225 else if(IsAttached() && GetMenuBar()->IsAttached())
227 // Regenerate the menu resource
228 GetMenuBar()->Refresh();
234 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
236 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
242 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
244 // we need to find the items position in the child list
246 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
247 for ( pos
= 0; node
; pos
++ )
249 if ( node
->GetData() == item
)
252 node
= node
->GetNext();
255 // DoRemove() (unlike Remove) can only be called for existing item!
256 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
258 // remove the item from the menu
259 wxMenuItem
*ret
=wxMenuBase::DoRemove(item
);
261 if ( IsAttached() && GetMenuBar()->IsAttached() )
263 // Regenerate the menu resource
264 GetMenuBar()->Refresh();
270 // ---------------------------------------------------------------------------
271 // accelerator helpers
272 // ---------------------------------------------------------------------------
276 // create the wxAcceleratorEntries for our accels and put them into provided
277 // array - return the number of accels we have
278 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
280 size_t count
= GetAccelCount();
281 for ( size_t n
= 0; n
< count
; n
++ )
283 *accels
++ = *m_accels
[n
];
289 #endif // wxUSE_ACCEL
291 // ---------------------------------------------------------------------------
293 // ---------------------------------------------------------------------------
295 void wxMenu::SetTitle(const wxString
& label
)
299 if ( IsAttached() && GetMenuBar()->IsAttached() )
301 // Regenerate the menu resource
302 GetMenuBar()->Refresh();
306 // ---------------------------------------------------------------------------
308 // ---------------------------------------------------------------------------
310 bool wxMenu::PalmCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
315 // ---------------------------------------------------------------------------
317 // ---------------------------------------------------------------------------
319 wxWindow
*wxMenu::GetWindow() const
324 // ---------------------------------------------------------------------------
326 // ---------------------------------------------------------------------------
328 void wxMenuBar::Init()
332 wxMenuBar::wxMenuBar()
336 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
340 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
344 wxMenuBar::~wxMenuBar()
348 // ---------------------------------------------------------------------------
350 // ---------------------------------------------------------------------------
352 void wxMenuBar::Refresh()
354 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
356 // Regenerate the menu resource
360 WXHMENU
wxMenuBar::Create()
365 int wxMenuBar::PalmPositionForWxMenu(wxMenu
*menu
, int wxpos
)
370 // ---------------------------------------------------------------------------
371 // wxMenuBar functions to work with the top level submenus
372 // ---------------------------------------------------------------------------
374 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
376 // Palm OS does not have support for grayed or disabled items
379 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
381 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
383 m_titles
[pos
]=wxStripMenuCodes(label
);
390 // Regenerate the menu resource
394 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
396 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
397 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
399 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
402 // ---------------------------------------------------------------------------
403 // wxMenuBar construction
404 // ---------------------------------------------------------------------------
406 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
408 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
412 m_titles
[pos
]=wxStripMenuCodes(title
);
416 // Regenerate the menu resource
423 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
425 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
428 m_titles
.Insert(wxStripMenuCodes(title
), pos
);
432 // Regenerate the menu resource
439 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
441 if ( !wxMenuBarBase::Append(menu
, title
) )
444 m_titles
.Add(wxStripMenuCodes(title
));
448 // Regenerate the menu resource
455 wxMenu
*wxMenuBar::Remove(size_t pos
)
457 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
461 m_titles
.RemoveAt(pos
);
465 // Regenerate the menu resource
474 void wxMenuBar::RebuildAccelTable()
478 #endif // wxUSE_ACCEL
480 int wxMenuBar::ProcessCommand(int ItemID
)
485 int MenuNum
=(ItemID
/1000)-1;
486 int ItemNum
=(ItemID
-(1000*(MenuNum
+1)));
488 // Should never happen, but it doesn't hurt to check anyway.
489 if(MenuNum
>GetMenuCount())
493 wxMenu
*ActiveMenu
=GetMenu(MenuNum
);
495 // Make sure this is a valid item.
496 if(ItemNum
>ActiveMenu
->GetMenuItemCount())
500 wxMenuItem
*ActiveItem
=ActiveMenu
->FindItemByPosition(ItemNum
);
501 int ActiveID
=ActiveItem
->GetId();
506 /* Palm OS does not have good dynamic menu support. About all you can do with
507 * the standard API calls is to add new items to an existing drop-down menu and
508 * hide/show items in a drop-down menu. It is impossible to add, hide, or
509 * change the label on a drop-down menu.
511 * The easiest and simplest way around this limitation is to modify the Palm OS
512 * MenuBarType structure directly. This gives limited ability to change the
513 * label on a drop-down menu. I have not been able to find a safe way to add,
514 * delete, or resize drop-down menus in OS 6.
516 * The following routine attempt to work around these limitations present in the
517 * Palm OS API to provide limited dynamic menu support. This solution is far
518 * from perfect, but the only other option is to wait for PalmSource to add full
519 * dynamic menu support, or to recreate the Palm OS menu system from scratch.
521 * This system is limited in that no more than 4 drop-down menus are allowed per
522 * menu bar, and the label for each drop-down menu is limited to 8 characters of
523 * text. However, this menu system should work for most applications.
525 * Basically the menu routines select one of four menu bars, depending on
526 * whether or not the requested menu bar has one, two, three, or four drop-down
529 * These four "template" menu bars contain one, two, three, or four drop-down
530 * menus. Each menu has a dummy menu item attached to it to allow the Palm OS
531 * MenuAddItem function to add the real items.
533 * The labels on the drop-down menus are then replaced with the labels of the
536 * The menu is then attached to the active window and the MenuAddItem API
537 * function is called to add the items to each drop-down menu. Finally,
538 * MenuHideItem is called to remove the dummy items from each drop-down menu.
540 void wxMenuBar::LoadMenu()
545 // Handle to the currently running application database
548 // Get app database reference - needed for some Palm OS Menu API calls.
549 SysGetModuleDatabase(SysGetRefNum(), NULL
, &AppDB
);
551 // Get the number of menus
552 int NumMenus
=GetMenuCount();
554 // Set up the pointers and handles
555 char *PalmOSMenuBarPtr
;
556 MemHandle PalmOSMenuBar
;
558 // Load the menu template and set up the menu pointers
561 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',1000);
562 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
564 PalmOSMenuBarPtr
+=74;
568 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',2000);
569 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
571 PalmOSMenuBarPtr
+=116;
575 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',3000);
576 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
578 PalmOSMenuBarPtr
+=158;
582 // We support a maximum of 4 menus, so make sure that do not create
583 // more than we can handle.
586 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',4000);
587 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
589 PalmOSMenuBarPtr
+=200;
592 // Set the proper names for the drop-down triggers.
593 for(i
=0;i
<NumMenus
;i
++)
595 // Clear out the old label
596 char buffer
[8]={' ',' ',' ',' ',' ',' ',' ',' '};
597 MemMove(PalmOSMenuBarPtr
,buffer
,8);
599 wxString MenuTitle
=m_titles
.Item(i
);
601 // Make sure we don't copy more than 8 bytes for the label
602 int LengthToCopy
=MenuTitle
.length();
606 MemMove(PalmOSMenuBarPtr
,MenuTitle
,LengthToCopy
);
607 PalmOSMenuBarPtr
+=11;
610 // We are done with the menu pointer.
611 MemHandleUnlock(PalmOSMenuBar
);
612 DmReleaseResource(PalmOSMenuBar
);
614 // We must make the menu active before we can add items to the drop-down
616 FrmSetMenu(FrmGetActiveForm(),AppDB
,NumMenus
*1000);
618 /* Add the menu items to the drop-down triggers. This must be done after
619 * setting the triggers, because setting the names of drop-down triggers
620 * that have a variable number of items requires carefull calculation of
621 * the offsets in the MenuBarType structure. Setting the triggers first
624 for(i
=0;i
<NumMenus
;i
++)
626 wxMenu
*CurrentMenu
=GetMenu(i
);
628 for(j
=0;j
<CurrentMenu
->GetMenuItemCount();j
++)
630 wxMenuItem
*CurrentItem
=CurrentMenu
->FindItemByPosition(j
);
631 wxString ItemLabel
=CurrentItem
->GetLabel();
633 if(CurrentItem
->IsSeparator()==true)
635 char Separator
=MenuSeparatorChar
;
637 MenuAddItem(9000+i
,((i
*1000)+1000)+j
,0x00,&Separator
);
639 MenuAddItem(((i
*1000)+1000)+j
-1,((i
*1000)+1000)+j
,0x00,&Separator
);
644 MenuAddItem(9000+i
,((i
*1000)+1000)+j
,0x00,ItemLabel
);
646 MenuAddItem(((i
*1000)+1000)+j
-1,((i
*1000)+1000)+j
,0x00,ItemLabel
);
650 // Hide the dummy menu item, since we don't need it anymore.
651 MenuHideItem(9000+i
);
655 void wxMenuBar::Attach(wxFrame
*frame
)
657 wxMenuBarBase::Attach(frame
);
662 void wxMenuBar::Detach()
664 wxMenuBarBase::Detach();
667 #endif // wxUSE_MENUS