// Menu
// ----------------------------------------------------------------------------
-class wxMenu : public wxEvtHandler
+class wxMenu : public wxMenuBase
{
- DECLARE_DYNAMIC_CLASS(wxMenu)
-
public:
- // ctor & dtor
-#ifdef WXWIN_COMPATIBILITY
- wxMenu( const wxString& title, const wxFunction func)
- {
- Init(title, 0, func);
- }
-#endif // WXWIN_COMPATIBILITY
- wxMenu( const wxString& title = wxEmptyString, long style = 0 )
+ // ctors & dtor
+ wxMenu(const wxString& title, long style = 0)
+ : wxMenuBase(title, style) { Init(); }
+
+ wxMenu(long style = 0) : wxMenuBase(style) { Init(); }
+
+ virtual ~wxMenu();
+
+ // implement base class virtuals
+ virtual bool DoAppend(wxMenuItem *item);
+ virtual bool DoInsert(size_t pos, wxMenuItem *item);
+ virtual wxMenuItem *DoRemove(wxMenuItem *item);
+
+ virtual void Break();
+
+ virtual void SetTitle(const wxString& title);
+
+ bool ProcessCommand(wxCommandEvent& event);
+
+#if WXWIN_COMPATIBILITY
+ wxMenu(const wxString& title, const wxFunction func)
+ : wxMenuBase(title)
{
- Init(title, style);
+ Callback(func);
}
- virtual ~wxMenu();
-
- // construct menu
- // append items to the menu
- // separator line
- void AppendSeparator();
- // normal item
- void Append(int id, const wxString& Label, const wxString& helpString = wxEmptyString,
- bool checkable = FALSE);
- // a submenu
- void Append(int id, const wxString& Label, wxMenu *SubMenu,
- const wxString& helpString = wxEmptyString);
- // the most generic form (create wxMenuItem first and use it's functions)
- void Append(wxMenuItem *pItem);
- // insert a break in the menu
- void Break();
- // delete an item
- void Delete(int id);
-
- // menu item control
- void Enable(int id, bool Flag);
- bool Enabled(int id) const;
- bool IsEnabled(int id) const { return Enabled(id); };
- void Check(int id, bool Flag);
- bool Checked(int id) const;
- bool IsChecked(int id) const { return IsChecked(id); };
-
- // Client data
- void SetClientData(void* clientData) { m_clientData = clientData; }
- void* GetClientData() const { return m_clientData; }
-
- // item properties
- // title
- void SetTitle(const wxString& label);
- const wxString GetTitle() const;
- // label
- void SetLabel(int id, const wxString& label);
- wxString GetLabel(int id) const;
- // help string
- virtual void SetHelpString(int id, const wxString& helpString);
- virtual wxString GetHelpString(int id) const ;
-
- // find item
- // Finds the item id matching the given string, -1 if not found.
- virtual int FindItem(const wxString& itemString) const ;
- // Find wxMenuItem by ID, and item's menu too if itemMenu is !NULL.
- wxMenuItem *FindItemForId(int itemId, wxMenu **itemMenu = NULL) const;
-
- // Updates the UI for a menu and all submenus recursively.
- // source is the object that has the update event handlers
- // defined for it. If NULL, the menu or associated window
- // will be used.
- void UpdateUI(wxEvtHandler* source = (wxEvtHandler*) NULL);
-
- void ProcessCommand(wxCommandEvent& event);
-
-#ifdef WXWIN_COMPATIBILITY
- void Callback(const wxFunction func) { m_callback = func; }
#endif // WXWIN_COMPATIBILITY
- void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
- wxEvtHandler *GetEventHandler() { return m_eventHandler; }
+ //// Motif-specific
+ WXWidget GetButtonWidget() const { return m_buttonWidget; }
+ void SetButtonWidget(WXWidget buttonWidget) { m_buttonWidget = buttonWidget; }
- wxList& GetItems() const { return (wxList&) m_menuItems; }
+ WXWidget GetMainWidget() const { return m_menuWidget; }
- void SetInvokingWindow(wxWindow *pWin) { m_pInvokingWindow = pWin; }
- wxWindow *GetInvokingWindow() const { return m_pInvokingWindow; }
+ int GetId() const { return m_menuId; }
+ void SetId(int id) { m_menuId = id; }
- //// Motif-specific
- WXWidget GetButtonWidget() const { return m_buttonWidget; }
- void SetButtonWidget(WXWidget buttonWidget) { m_buttonWidget = buttonWidget; }
- WXWidget GetMainWidget() const { return m_menuWidget; }
- wxMenu* GetParent() const { return m_menuParent; }
- int GetId() const { return m_menuId; }
- void SetId(int id) { m_menuId = id; }
- void SetMenuBar(wxMenuBar* menuBar) { m_menuBar = menuBar; }
- wxMenuBar* GetMenuBar() const { return m_menuBar; }
+ void SetMenuBar(wxMenuBar* menuBar) { m_menuBar = menuBar; }
+ wxMenuBar* GetMenuBar() const { return m_menuBar; }
- void CreatePopup (WXWidget logicalParent, int x, int y);
- void DestroyPopup (void);
- void ShowPopup (int x, int y);
- void HidePopup (void);
+ void CreatePopup(WXWidget logicalParent, int x, int y);
+ void DestroyPopup();
+ void ShowPopup(int x, int y);
+ void HidePopup();
- WXWidget CreateMenu(wxMenuBar *menuBar, WXWidget parent, wxMenu *topMenu,
- const wxString& title = "", bool isPulldown = FALSE);
+ WXWidget CreateMenu(wxMenuBar *menuBar, WXWidget parent, wxMenu *topMenu,
+ const wxString& title = wxEmptyString,
+ bool isPulldown = FALSE);
- // For popups, need to destroy, then recreate menu for a different (or
- // possibly same) window, since the parent may change.
- void DestroyMenu(bool full);
- WXWidget FindMenuItem(int id, wxMenuItem **it = NULL) const;
+ // For popups, need to destroy, then recreate menu for a different (or
+ // possibly same) window, since the parent may change.
+ void DestroyMenu(bool full);
+ WXWidget FindMenuItem(int id, wxMenuItem **it = NULL) const;
- const wxColour& GetBackgroundColour() const { return m_backgroundColour; }
- const wxColour& GetForegroundColour() const { return m_foregroundColour; }
- const wxFont& GetFont() const { return m_font; }
+ const wxColour& GetBackgroundColour() const { return m_backgroundColour; }
+ const wxColour& GetForegroundColour() const { return m_foregroundColour; }
+ const wxFont& GetFont() const { return m_font; }
- void SetBackgroundColour(const wxColour& colour);
- void SetForegroundColour(const wxColour& colour);
- void SetFont(const wxFont& colour);
- void ChangeFont(bool keepOriginalSize = FALSE);
+ void SetBackgroundColour(const wxColour& colour);
+ void SetForegroundColour(const wxColour& colour);
+ void SetFont(const wxFont& colour);
+ void ChangeFont(bool keepOriginalSize = FALSE);
- // implementation from now on
- WXWidget GetHandle() const { return m_menuWidget; }
- bool IsTearOff() const { return (m_style & wxMENU_TEAROFF) != 0; }
+ WXWidget GetHandle() const { return m_menuWidget; }
-public:
-#ifdef WXWIN_COMPATIBILITY
- wxFunction m_callback;
-#endif // WXWIN_COMPATIBILITY
+ bool IsTearOff() const { return (m_style & wxMENU_TEAROFF) != 0; }
- int m_noItems;
- wxString m_title;
- wxMenuBar * m_menuBar;
- wxList m_menuItems;
- wxEvtHandler * m_eventHandler;
- void* m_clientData;
- wxWindow* m_pInvokingWindow;
-
- long m_style;
-
- //// Motif-specific
- int m_numColumns;
- WXWidget m_menuWidget;
- WXWidget m_popupShell; // For holding the popup shell widget
- WXWidget m_buttonWidget; // The actual string, so we can grey it etc.
- int m_menuId;
- wxMenu* m_topLevelMenu ;
- wxMenu* m_menuParent;
- bool m_ownedByMenuBar;
- wxColour m_foregroundColour;
- wxColour m_backgroundColour;
- wxFont m_font;
+public:
+ // Motif-specific data
+ int m_numColumns;
+ WXWidget m_menuWidget;
+ WXWidget m_popupShell; // For holding the popup shell widget
+ WXWidget m_buttonWidget; // The actual string, so we can grey it etc.
+ int m_menuId;
+ wxMenu* m_topLevelMenu ;
+ bool m_ownedByMenuBar;
+ wxColour m_foregroundColour;
+ wxColour m_backgroundColour;
+ wxFont m_font;
private:
// common code for both constructors:
- void Init( const wxString& title,
- long style
-#ifdef WXWIN_COMPATIBILITY
- , const wxFunction func = (wxFunction) NULL
-#endif
- );
+ void Init();
+
+ DECLARE_DYNAMIC_CLASS(wxMenu)
};
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Construct a menu with optional title (then use append)
-void wxMenu::Init(const wxString& title,
- long style
-#ifdef WXWIN_COMPATIBILITY
- , const wxFunction func
-#endif
- )
+void wxMenu::Init()
{
- m_title = title;
- m_eventHandler = this;
- m_noItems = 0;
- m_menuBar = NULL;
- m_pInvokingWindow = NULL;
- m_style = style;
-
- //// Motif-specific members
+ // Motif-specific members
m_numColumns = 1;
m_menuWidget = (WXWidget) NULL;
m_popupShell = (WXWidget) NULL;
m_menuId = 0;
m_topLevelMenu = (wxMenu*) NULL;
m_ownedByMenuBar = FALSE;
- m_menuParent = (wxMenu*) NULL;
- m_clientData = (void*) NULL;
- if (m_title != "")
+ if ( !!m_title )
{
- Append(ID_SEPARATOR, m_title) ;
+ Append(wxID_SEPARATOR, m_title) ;
AppendSeparator() ;
}
+
m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU);
m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT);
m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
-
-#ifdef WXWIN_COMPATIBILITY
- Callback(func);
-#endif
}
// The wxWindow destructor will take care of deleting the submenus.
m_menuParent = NULL;
// m_menuBar = NULL;
}
-
- wxNode *node = m_menuItems.First();
- while (node)
- {
- wxMenuItem *item = (wxMenuItem *)node->Data();
-
- /*
- if (item->GetSubMenu())
- item->DeleteSubMenu();
- */
-
- wxNode *next = node->Next();
- delete item;
- delete node;
- node = next;
- }
}
void wxMenu::Break()
{
- m_numColumns ++;
+ m_numColumns++;
}
// function appends a new item or submenu to the menu
-void wxMenu::Append(wxMenuItem *pItem)
+bool wxMenu::DoAppend(wxMenuItem *pItem)
{
- wxCHECK_RET( pItem != NULL, "can't append NULL item to the menu" );
-
- m_menuItems.Append(pItem);
-
if (m_menuWidget)
- pItem->CreateItem (m_menuWidget, m_menuBar, m_topLevelMenu); // this is a dynamic Append
-
- m_noItems++;
-}
-
-void wxMenu::AppendSeparator()
-{
- Append(new wxMenuItem(this, ID_SEPARATOR));
-}
-
-// Pullright item
-// N.B.: difference between old and new code.
-// Old code stores subMenu in 'children' for later deletion,
-// as well as in m_menuItems, whereas we only store it in
-// m_menuItems here. What implications does this have?
-
-void wxMenu::Append(int id, const wxString& label, wxMenu *subMenu,
- const wxString& helpString)
-{
- Append(new wxMenuItem(this, id, label, helpString, FALSE, subMenu));
-
- subMenu->m_topLevelMenu = m_topLevelMenu;
-}
-
-// Ordinary menu item
-void wxMenu::Append(int id, const wxString& label,
- const wxString& helpString, bool checkable)
-{
- // 'checkable' parameter is useless for Windows.
- Append(new wxMenuItem(this, id, label, helpString, checkable));
-}
-
-void wxMenu::Delete(int id)
-{
- wxNode *node;
- wxMenuItem *item;
- int pos;
-
- for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
{
- item = (wxMenuItem *)node->Data();
- if (item->GetId() == id)
- break;
+ // this is a dynamic Append
+ pItem->CreateItem(m_menuWidget, m_menuBar, m_topLevelMenu);
}
- if (!node)
- return;
-
- item->DestroyItem(TRUE);
-
- // See also old code - don't know if this is needed (seems redundant).
- /*
- if (item->GetSubMenu()) {
- item->subMenu->top_level_menu = item->GetSubMenu();
- item->subMenu->window_parent = NULL;
- children->DeleteObject(item->GetSubMenu());
+ if ( pItem->IsSubMenu() )
+ {
+ pItem->GetSubMenu()->m_topLevelMenu = m_topLevelMenu;
}
- */
-
- m_menuItems.DeleteNode(node);
- delete item;
-}
-
-void wxMenu::Enable(int id, bool flag)
-{
- wxMenuItem *item = FindItemForId(id);
- wxCHECK_RET( item != NULL, "can't enable non-existing menu item" );
- item->Enable(flag);
+ return wxMenuBase::DoAppend(pItem);
}
-bool wxMenu::Enabled(int Id) const
+wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
{
- wxMenuItem *item = FindItemForId(Id);
- wxCHECK( item != NULL, FALSE );
+ item->DestroyItem(TRUE);
- return item->IsEnabled();
+ return wxMenuBase::DoRemove(item);
}
-void wxMenu::Check(int Id, bool Flag)
+bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
{
- wxMenuItem *item = FindItemForId(Id);
- wxCHECK_RET( item != NULL, "can't get status of non-existing menu item" );
-
- item->Check(Flag);
-}
+ if ( !wxMenuBase::DoInsert(pos, item) )
+ return FALSE;
-bool wxMenu::Checked(int id) const
-{
- wxMenuItem *item = FindItemForId(id);
- wxCHECK( item != NULL, FALSE );
+ wxFAIL_MSG(wxT("not implemented"));
- return item->IsChecked();
+ return FALSE;
}
void wxMenu::SetTitle(const wxString& label)
{
- m_title = label ;
+ m_title = label;
- wxNode *node = m_menuItems.First ();
- if (!node)
+ wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ if ( !node )
return;
- wxMenuItem *item = (wxMenuItem *) node->Data ();
+ wxMenuItem *item = node->GetData ();
Widget widget = (Widget) item->GetButtonWidget();
- if (!widget)
- return;
-
- XmString title_str = XmStringCreateSimple ((char*) (const char*) label);
- XtVaSetValues (widget,
- XmNlabelString, title_str,
- NULL);
- // TODO: should we delete title_str now?
-}
-
-const wxString wxMenu::GetTitle() const
-{
- return m_title;
-}
-
-void wxMenu::SetLabel(int id, const wxString& label)
-{
- wxMenuItem *item = FindItemForId(id);
- if (item == (wxMenuItem*) NULL)
+ if ( !widget )
return;
- item->SetText(label);
+ wxXmString title_str(label);
+ XtVaSetValues(widget,
+ XmNlabelString, title_str(),
+ NULL);
}
-wxString wxMenu::GetLabel(int id) const
-{
- wxMenuItem *it = NULL;
- WXWidget w = FindMenuItem (id, &it);
- if (w)
- {
- XmString text;
- char *s;
- XtVaGetValues ((Widget) w,
- XmNlabelString, &text,
- NULL);
-
- if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
- {
- wxString str(s);
- XtFree (s);
- return str;
- }
- else
- {
- XmStringFree (text);
- return wxEmptyString;
- }
- }
- else
- return wxEmptyString;
-}
-
-// Finds the item id matching the given string, -1 if not found.
-int wxMenu::FindItem (const wxString& itemString) const
-{
- char buf1[200];
- char buf2[200];
- wxStripMenuCodes ((char *)(const char *)itemString, buf1);
-
- for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
- {
- wxMenuItem *item = (wxMenuItem *) node->Data ();
- if (item->GetSubMenu())
- {
- int ans = item->GetSubMenu()->FindItem(itemString);
- if (ans > -1)
- return ans;
- }
- if ( !item->IsSeparator() )
- {
- wxStripMenuCodes((char *)item->GetName().c_str(), buf2);
- if (strcmp(buf1, buf2) == 0)
- return item->GetId();
- }
- }
-
- return -1;
-}
-
-wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
-{
- if (itemMenu)
- *itemMenu = NULL;
- for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
- {
- wxMenuItem *item = (wxMenuItem *) node->Data ();
-
- if (item->GetId() == itemId)
- {
- if (itemMenu)
- *itemMenu = (wxMenu *) this;
- return item;
- }
-
- if (item->GetSubMenu())
- {
- wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu);
- if (ans)
- return ans;
- }
- }
-
- if (itemMenu)
- *itemMenu = NULL;
- return NULL;
-}
-
-void wxMenu::SetHelpString(int itemId, const wxString& helpString)
-{
- wxMenuItem *item = FindItemForId (itemId);
- if (item)
- item->SetHelp(helpString);
-}
-
-wxString wxMenu::GetHelpString (int itemId) const
-{
- wxMenuItem *item = FindItemForId (itemId);
- wxString str("");
- return (item == NULL) ? str : item->GetHelp();
-}
-
-void wxMenu::ProcessCommand(wxCommandEvent & event)
+bool wxMenu::ProcessCommand(wxCommandEvent & event)
{
bool processed = FALSE;
+#if WXWIN_COMPATIBILITY
// Try a callback
if (m_callback)
{
(void) (*(m_callback)) (*this, event);
processed = TRUE;
}
+#endif // WXWIN_COMPATIBILITY
// Try the menu's event handler
if ( !processed && GetEventHandler())
// Try the window the menu was popped up from (and up
// through the hierarchy)
if ( !processed && GetInvokingWindow())
- processed = GetInvokingWindow()->ProcessEvent(event);
-}
-
-// Update a menu and all submenus recursively.
-// source is the object that has the update event handlers
-// defined for it. If NULL, the menu or associated window
-// will be used.
-void wxMenu::UpdateUI(wxEvtHandler* source)
-{
- if (!source && GetInvokingWindow())
- source = GetInvokingWindow()->GetEventHandler();
- if (!source)
- source = GetEventHandler();
- if (!source)
- source = this;
-
- wxNode* node = GetItems().First();
- while (node)
- {
- wxMenuItem* item = (wxMenuItem*) node->Data();
- if ( !item->IsSeparator() )
- {
- wxWindowID id = item->GetId();
- wxUpdateUIEvent event(id);
- event.SetEventObject( source );
-
- if (source->ProcessEvent(event))
- {
- if (event.GetSetText())
- SetLabel(id, event.GetText());
- if (event.GetSetChecked())
- Check(id, event.GetChecked());
- if (event.GetSetEnabled())
- Enable(id, event.GetEnabled());
- }
-
- if (item->GetSubMenu())
- item->GetSubMenu()->UpdateUI(source);
- }
- node = node->Next();
- }
+ processed = GetInvokingWindow()->ProcessEvent(event);
+
+ return processed;
}
// ----------------------------------------------------------------------------
wxMenuItem *item = NULL;
size_t menuCount = GetMenuCount();
for (size_t i = 0; i < menuCount; i++)
- if ((item = m_menus[i]->FindItemForId (id, itemMenu)))
+ if ((item = m_menus[i]->FindItem(id, itemMenu)))
return item;
return NULL;
}
XtRemoveWorkProc(WorkProcMenuId);
wxMenu *menu = (wxMenu *)clientData;
- if (menu->GetMainWidget()) {
- if (menu->GetParent())
+ if (menu->GetMainWidget())
+ {
+ wxMenu *menuParent = menu->GetParent();
+ if ( menuParent )
{
- wxList& list = menu->GetParent()->GetItems();
- list.DeleteObject(menu);
+ wxMenuItemList::Node *node = menuParent->GetMenuItems().GetFirst();
+ while ( node )
+ {
+ if ( node->GetData()->GetSubMenu() == menu )
+ {
+ menuParent->GetMenuItems().DeleteNode(node);
+
+ break;
+ }
+
+ node = node->GetNext();
+ }
}
+
menu->DestroyMenu(TRUE);
}
- /* Mark as no longer popped up */
+
+ // Mark as no longer popped up
menu->m_menuId = -1;
+
return TRUE;
}
m_menuBar = menuBar;
m_topLevelMenu = topMenu;
- for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
+ for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ node;
+ node = node->GetNext() )
{
- wxMenuItem *item = (wxMenuItem *) node->Data ();
- item->CreateItem (menu, menuBar, topMenu);
+ wxMenuItem *item = node->GetData();
+
+ item->CreateItem(menu, menuBar, topMenu);
}
SetBackgroundColour(m_backgroundColour);
// do a CreateMenu again.
void wxMenu::DestroyMenu (bool full)
{
- for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
+ for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ node;
+ node = node->GetNext() )
{
- wxMenuItem *item = (wxMenuItem *) node->Data ();
+ wxMenuItem *item = node->GetData();
item->SetMenuBar((wxMenuBar*) NULL);
item->DestroyItem(full);
- }// for()
+ }
if (m_buttonWidget)
{
return m_buttonWidget;
}
- for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
+ for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ node;
+ node = node->GetNext() )
{
- wxMenuItem *item = (wxMenuItem *) node->Data ();
+ wxMenuItem *item = node->GetData ();
if (item->GetId() == id)
{
if (it)
return w;
}
}
- }// for()
+ }
if (it)
*it = (wxMenuItem*) NULL;
if (m_buttonWidget)
wxDoChangeBackgroundColour(m_buttonWidget, (wxColour&) col, TRUE);
- wxNode* node = m_menuItems.First();
- while (node)
+ for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ node;
+ node = node->GetNext() )
{
- wxMenuItem* item = (wxMenuItem*) node->Data();
+ wxMenuItem* item = node->GetData();
if (item->GetButtonWidget())
{
// This crashes because it uses gadgets
}
if (item->GetSubMenu())
item->GetSubMenu()->SetBackgroundColour((wxColour&) col);
- node = node->Next();
}
}
if (m_buttonWidget)
wxDoChangeForegroundColour(m_buttonWidget, (wxColour&) col);
- wxNode* node = m_menuItems.First();
- while (node)
+ for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ node;
+ node = node->GetNext() )
{
- wxMenuItem* item = (wxMenuItem*) node->Data();
+ wxMenuItem* item = node->GetData();
if (item->GetButtonWidget())
{
// This crashes because it uses gadgets
}
if (item->GetSubMenu())
item->GetSubMenu()->SetForegroundColour((wxColour&) col);
- node = node->Next();
}
}
XmNfontList, fontList,
NULL);
}
- wxNode* node = m_menuItems.First();
- while (node)
+
+ for ( wxMenuItemList::Node *node = GetMenuItems().GetFirst();
+ node;
+ node = node->GetNext() )
{
- wxMenuItem* item = (wxMenuItem*) node->Data();
+ wxMenuItem* item = node->GetData();
if (m_menuWidget && item->GetButtonWidget() && m_font.Ok())
{
XtVaSetValues ((Widget) item->GetButtonWidget(),
}
if (item->GetSubMenu())
item->GetSubMenu()->ChangeFont(keepOriginalSize);
- node = node->Next();
}
#endif
}