1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/palmos/menu.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: William Osborne - minimal working wxPalmOS port
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"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 extern wxMenu
*wxCurrentPopupMenu
;
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // the (popup) menu title has this special id
60 static const int idMenuTitle
= -3;
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // ============================================================================
68 // ============================================================================
70 #include <wx/listimpl.cpp>
72 WX_DEFINE_LIST( wxMenuInfoList
) ;
74 #if wxUSE_EXTENDED_RTTI
76 WX_DEFINE_FLAGS( wxMenuStyle
)
78 wxBEGIN_FLAGS( wxMenuStyle
)
79 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
80 wxEND_FLAGS( wxMenuStyle
)
82 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
84 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
86 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
88 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
91 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
92 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
93 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
94 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
95 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
96 wxEND_PROPERTIES_TABLE()
98 wxBEGIN_HANDLERS_TABLE(wxMenu
)
99 wxEND_HANDLERS_TABLE()
101 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
103 WX_DEFINE_FLAGS( wxMenuBarStyle
)
105 wxBEGIN_FLAGS( wxMenuBarStyle
)
106 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
107 wxEND_FLAGS( wxMenuBarStyle
)
109 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
110 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
115 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
117 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
119 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
120 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
121 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
122 wxEND_PROPERTIES_TABLE()
124 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
125 wxEND_HANDLERS_TABLE()
127 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
129 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
131 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
133 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
136 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
137 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
138 wxEND_PROPERTIES_TABLE()
140 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
141 wxEND_HANDLERS_TABLE()
143 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
146 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
147 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
148 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
151 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
153 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
154 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
155 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
157 wxMenuInfo
* info
= new wxMenuInfo() ;
158 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
159 list
->Append( info
) ;
164 // ---------------------------------------------------------------------------
165 // wxMenu construction, adding and removing menu items
166 // ---------------------------------------------------------------------------
168 // Construct a menu with optional title (then use append)
173 // The wxWindow destructor will take care of deleting the submenus.
182 void wxMenu::Attach(wxMenuBarBase
*menubar
)
184 wxMenuBase::Attach(menubar
);
189 int wxMenu::FindAccel(int id
) const
194 void wxMenu::UpdateAccel(wxMenuItem
*item
)
198 #endif // wxUSE_ACCEL
200 // append a new item or submenu to the menu
201 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
203 if ( IsAttached() && GetMenuBar()->IsAttached() )
205 // Regenerate the menu resource
206 GetMenuBar()->Refresh();
212 void wxMenu::EndRadioGroup()
216 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
218 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
220 if(!wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
))
224 else if(IsAttached() && GetMenuBar()->IsAttached())
226 // Regenerate the menu resource
227 GetMenuBar()->Refresh();
233 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
235 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
241 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
243 // we need to find the items position in the child list
245 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
246 for ( pos
= 0; node
; pos
++ )
248 if ( node
->GetData() == item
)
251 node
= node
->GetNext();
254 // DoRemove() (unlike Remove) can only be called for existing item!
255 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
257 // remove the item from the menu
258 wxMenuItem
*ret
=wxMenuBase::DoRemove(item
);
260 if ( IsAttached() && GetMenuBar()->IsAttached() )
262 // Regenerate the menu resource
263 GetMenuBar()->Refresh();
269 // ---------------------------------------------------------------------------
270 // accelerator helpers
271 // ---------------------------------------------------------------------------
275 // create the wxAcceleratorEntries for our accels and put them into provided
276 // array - return the number of accels we have
277 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
279 size_t count
= GetAccelCount();
280 for ( size_t n
= 0; n
< count
; n
++ )
282 *accels
++ = *m_accels
[n
];
288 #endif // wxUSE_ACCEL
290 // ---------------------------------------------------------------------------
292 // ---------------------------------------------------------------------------
294 void wxMenu::SetTitle(const wxString
& label
)
298 if ( IsAttached() && GetMenuBar()->IsAttached() )
300 // Regenerate the menu resource
301 GetMenuBar()->Refresh();
305 // ---------------------------------------------------------------------------
307 // ---------------------------------------------------------------------------
309 bool wxMenu::PalmCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
314 // ---------------------------------------------------------------------------
316 // ---------------------------------------------------------------------------
318 wxWindow
*wxMenu::GetWindow() const
323 // ---------------------------------------------------------------------------
325 // ---------------------------------------------------------------------------
327 void wxMenuBar::Init()
331 wxMenuBar::wxMenuBar()
335 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
339 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
343 wxMenuBar::~wxMenuBar()
347 // ---------------------------------------------------------------------------
349 // ---------------------------------------------------------------------------
351 void wxMenuBar::Refresh()
353 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
355 // Regenerate the menu resource
359 WXHMENU
wxMenuBar::Create()
364 int wxMenuBar::PalmPositionForWxMenu(wxMenu
*menu
, int wxpos
)
369 // ---------------------------------------------------------------------------
370 // wxMenuBar functions to work with the top level submenus
371 // ---------------------------------------------------------------------------
373 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
375 // Palm OS does not have support for grayed or disabled items
378 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
380 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
382 m_titles
[pos
]=wxStripMenuCodes(label
);
389 // Regenerate the menu resource
393 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
395 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
396 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
398 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
401 // ---------------------------------------------------------------------------
402 // wxMenuBar construction
403 // ---------------------------------------------------------------------------
405 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
407 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
411 m_titles
[pos
]=wxStripMenuCodes(title
);
415 // Regenerate the menu resource
422 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
424 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
427 m_titles
.Insert(wxStripMenuCodes(title
), pos
);
431 // Regenerate the menu resource
438 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
440 if ( !wxMenuBarBase::Append(menu
, title
) )
443 m_titles
.Add(wxStripMenuCodes(title
));
447 // Regenerate the menu resource
454 wxMenu
*wxMenuBar::Remove(size_t pos
)
456 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
460 m_titles
.RemoveAt(pos
);
464 // Regenerate the menu resource
473 void wxMenuBar::RebuildAccelTable()
477 #endif // wxUSE_ACCEL
479 int wxMenuBar::ProcessCommand(int ItemID
)
484 int MenuNum
=(ItemID
/1000)-1;
485 int ItemNum
=(ItemID
-(1000*(MenuNum
+1)));
487 // Should never happen, but it doesn't hurt to check anyway.
488 if(MenuNum
>GetMenuCount())
492 wxMenu
*ActiveMenu
=GetMenu(MenuNum
);
494 // Make sure this is a valid item.
495 if(ItemNum
>ActiveMenu
->GetMenuItemCount())
499 wxMenuItem
*ActiveItem
=ActiveMenu
->FindItemByPosition(ItemNum
);
500 int ActiveID
=ActiveItem
->GetId();
505 /* Palm OS does not have good dynamic menu support. About all you can do with
506 * the standard API calls is to add new items to an existing drop-down menu and
507 * hide/show items in a drop-down menu. It is impossible to add, hide, or
508 * change the label on a drop-down menu.
510 * The easiest and simplest way around this limitation is to modify the Palm OS
511 * MenuBarType structure directly. This gives limited ability to change the
512 * label on a drop-down menu. I have not been able to find a safe way to add,
513 * delete, or resize drop-down menus in OS 6.
515 * The following routine attempt to work around these limitations present in the
516 * Palm OS API to provide limited dynamic menu support. This solution is far
517 * from perfect, but the only other option is to wait for PalmSource to add full
518 * dynamic menu support, or to recreate the Palm OS menu system from scratch.
520 * This system is limited in that no more than 4 drop-down menus are allowed per
521 * menu bar, and the label for each drop-down menu is limited to 8 characters of
522 * text. However, this menu system should work for most applications.
524 * Basically the menu routines select one of four menu bars, depending on
525 * whether or not the requested menu bar has one, two, three, or four drop-down
528 * These four "template" menu bars contain one, two, three, or four drop-down
529 * menus. Each menu has a dummy menu item attached to it to allow the Palm OS
530 * MenuAddItem function to add the real items.
532 * The labels on the drop-down menus are then replaced with the labels of the
535 * The menu is then attached to the active window and the MenuAddItem API
536 * function is called to add the items to each drop-down menu. Finally,
537 * MenuHideItem is called to remove the dummy items from each drop-down menu.
539 void wxMenuBar::LoadMenu()
544 // Handle to the currently running application database
547 // Get app database reference - needed for some Palm OS Menu API calls.
548 SysGetModuleDatabase(SysGetRefNum(), NULL
, &AppDB
);
550 // Get the number of menus
551 int NumMenus
=GetMenuCount();
553 // Set up the pointers and handles
554 char *PalmOSMenuBarPtr
;
555 MemHandle PalmOSMenuBar
;
557 // Load the menu template and set up the menu pointers
560 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',1000);
561 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
563 PalmOSMenuBarPtr
+=74;
567 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',2000);
568 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
570 PalmOSMenuBarPtr
+=116;
574 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',3000);
575 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
577 PalmOSMenuBarPtr
+=158;
581 // We support a maximum of 4 menus, so make sure that do not create
582 // more than we can handle.
585 PalmOSMenuBar
=DmGetResource(AppDB
,'MBAR',4000);
586 PalmOSMenuBarPtr
=(char *)MemHandleLock(PalmOSMenuBar
);
588 PalmOSMenuBarPtr
+=200;
591 // Set the proper names for the drop-down triggers.
592 for(i
=0;i
<NumMenus
;i
++)
594 // Clear out the old label
595 char buffer
[8]={' ',' ',' ',' ',' ',' ',' ',' '};
596 MemMove(PalmOSMenuBarPtr
,buffer
,8);
598 wxString MenuTitle
=m_titles
.Item(i
);
600 // Make sure we don't copy more than 8 bytes for the label
601 int LengthToCopy
=MenuTitle
.length();
605 MemMove(PalmOSMenuBarPtr
,MenuTitle
,LengthToCopy
);
606 PalmOSMenuBarPtr
+=11;
609 // We are done with the menu pointer.
610 MemHandleUnlock(PalmOSMenuBar
);
611 DmReleaseResource(PalmOSMenuBar
);
613 // We must make the menu active before we can add items to the drop-down
615 FrmSetMenu(FrmGetActiveForm(),AppDB
,NumMenus
*1000);
617 /* Add the menu items to the drop-down triggers. This must be done after
618 * setting the triggers, because setting the names of drop-down triggers
619 * that have a variable number of items requires carefull calculation of
620 * the offsets in the MenuBarType structure. Setting the triggers first
623 for(i
=0;i
<NumMenus
;i
++)
625 wxMenu
*CurrentMenu
=GetMenu(i
);
627 for(j
=0;j
<CurrentMenu
->GetMenuItemCount();j
++)
629 wxMenuItem
*CurrentItem
=CurrentMenu
->FindItemByPosition(j
);
630 wxString ItemLabel
=CurrentItem
->GetLabel();
632 if(CurrentItem
->IsSeparator()==true)
634 char Separator
=MenuSeparatorChar
;
636 MenuAddItem(9000+i
,((i
*1000)+1000)+j
,0x00,&Separator
);
638 MenuAddItem(((i
*1000)+1000)+j
-1,((i
*1000)+1000)+j
,0x00,&Separator
);
643 MenuAddItem(9000+i
,((i
*1000)+1000)+j
,0x00,ItemLabel
);
645 MenuAddItem(((i
*1000)+1000)+j
-1,((i
*1000)+1000)+j
,0x00,ItemLabel
);
649 // Hide the dummy menu item, since we don't need it anymore.
650 MenuHideItem(9000+i
);
654 void wxMenuBar::Attach(wxFrame
*frame
)
656 // before attaching preprocess menus to not include wxID_EXIT item
657 // as PalmOS guidelines suggest
663 while( item
= FindItem(wxID_EXIT
) )
665 menu
= item
->GetMenu();
666 if( !menu
) break; // something broken ?
668 size_t count
= menu
->GetMenuItemCount();
669 if( count
== 0 ) break; // something broken ?
671 // if EXIT is last item in menu
672 if( menu
->FindItemByPosition( count
- 1 ) == item
)
674 menu
->Destroy( item
);
676 // was more than one item?
677 // was previous separator ?
680 item
= menu
->FindItemByPosition( count
- 2 );
681 if(item
&& item
->IsSeparator())
682 menu
->Destroy( item
);
686 // if EXIT is first item in menu
687 else if( menu
->FindItemByPosition( 0 ) == item
)
689 menu
->Destroy( item
);
691 // was more than one item?
692 // was previous separator ?
695 item
= menu
->FindItemByPosition( 0 );
696 if(item
&& item
->IsSeparator())
697 menu
->Destroy( item
);
701 // if EXIT is in the middle but before and after are selectors
704 i
= 1; // 0 case already done
705 while ( (i
< count
) && (menu
->FindItemByPosition( 0 ) != item
) )
710 if (i
>= count
) break;
711 if (menu
->FindItemByPosition( i
) != item
) break;
712 menu
->Destroy( item
);
713 item
= menu
->FindItemByPosition( i
);
715 item
->IsSeparator() &&
716 menu
->FindItemByPosition( i
-1 )->IsSeparator() )
718 // noe need for two neighbouring separators
719 menu
->Destroy( item
);
724 // check if we received any empty menu!
726 while(i
< GetMenuCount())
730 if( menu
&& (menu
->GetMenuItemCount()==0) )
739 wxMenuBarBase::Attach(frame
);
744 void wxMenuBar::Detach()
746 wxMenuBarBase::Detach();
749 #endif // wxUSE_MENUS