From 9874b4ee8308b8989c5df7db4f3559625299d0a4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 27 Oct 1999 13:19:07 +0000 Subject: [PATCH] 1. wxMenu{Item|Bar} modifications for wxMotif 2. corrected a crash on startup if wxDataObject is used in Motif git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4212 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/menu.h | 4 +- include/wx/motif/menu.h | 122 +++++------- include/wx/motif/menuitem.h | 115 +++--------- src/motif/dataobj.cpp | 1 - src/motif/menu.cpp | 363 +++++++++++------------------------- src/motif/menuitem.cpp | 122 +++++++----- 6 files changed, 263 insertions(+), 464 deletions(-) diff --git a/include/wx/menu.h b/include/wx/menu.h index 0c8963a80e..fbaf8caedb 100644 --- a/include/wx/menu.h +++ b/include/wx/menu.h @@ -115,7 +115,9 @@ public: void SetHelpString(int id, const wxString& helpString); wxString GetHelpString(int id) const; - // need to override this one to avoid virtual function hiding + // need to override these ones to avoid virtual function hiding + virtual bool Enable(bool enable = TRUE) { return wxWindow::Enable(enable); } + virtual void SetLabel(const wxString& s) { wxWindow::SetLabel(s); } virtual wxString GetLabel() const { return wxWindow::GetLabel(); } // compatibility only: these functions are deprecated, use the new ones diff --git a/include/wx/motif/menu.h b/include/wx/motif/menu.h index d3d4705654..97c694329e 100644 --- a/include/wx/motif/menu.h +++ b/include/wx/motif/menu.h @@ -9,28 +9,23 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifndef _WX_MENU_H_ -#define _WX_MENU_H_ +#ifndef _WX_MOTIF_MENU_H_ +#define _WX_MOTIF_MENU_H_ #ifdef __GNUG__ -#pragma interface "menu.h" + #pragma interface "menu.h" #endif -#include "wx/defs.h" -#include "wx/event.h" +#include "wx/colour.h" #include "wx/font.h" -#include "wx/gdicmn.h" -class WXDLLEXPORT wxMenuItem; -class WXDLLEXPORT wxMenuBar; -class WXDLLEXPORT wxMenu; - -WXDLLEXPORT_DATA(extern const char*) wxEmptyString; +class wxFrame; // ---------------------------------------------------------------------------- // Menu // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxMenu: public wxEvtHandler + +class wxMenu : public wxEvtHandler { DECLARE_DYNAMIC_CLASS(wxMenu) @@ -106,7 +101,6 @@ public: void Callback(const wxFunction func) { m_callback = func; } #endif // WXWIN_COMPATIBILITY - virtual void SetParent(wxEvtHandler *parent) { m_parent = parent; } void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; } wxEvtHandler *GetEventHandler() { return m_eventHandler; } @@ -160,7 +154,6 @@ public: wxString m_title; wxMenuBar * m_menuBar; wxList m_menuItems; - wxEvtHandler * m_parent; wxEvtHandler * m_eventHandler; void* m_clientData; wxWindow* m_pInvokingWindow; @@ -191,54 +184,36 @@ private: }; // ---------------------------------------------------------------------------- -// Menu Bar (a la Windows) +// Menu Bar // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxFrame; -class WXDLLEXPORT wxMenuBar : public wxEvtHandler +class wxMenuBar : public wxMenuBarBase { -DECLARE_DYNAMIC_CLASS(wxMenuBar) - public: - wxMenuBar( long style ); - wxMenuBar(); + wxMenuBar() { Init(); } + wxMenuBar(long WXUNUSED(style)) { Init(); } wxMenuBar(int n, wxMenu *menus[], const wxString titles[]); - ~wxMenuBar(); - - void Append(wxMenu *menu, const wxString& title); - // Must only be used AFTER menu has been attached to frame, - // otherwise use individual menus to enable/disable items - void Enable(int Id, bool Flag); - bool Enabled(int Id) const ; - bool IsEnabled(int Id) const { return Enabled(Id); }; - void EnableTop(int pos, bool Flag); - void Check(int id, bool Flag); - bool Checked(int id) const ; - bool IsChecked(int Id) const { return Checked(Id); }; - void SetLabel(int id, const wxString& label) ; - wxString GetLabel(int id) const ; - void SetLabelTop(int pos, const wxString& label) ; - wxString GetLabelTop(int pos) const ; - virtual void Delete(wxMenu *menu, int index = 0); /* Menu not destroyed */ - virtual bool OnAppend(wxMenu *menu, const char *title); - virtual bool OnDelete(wxMenu *menu, int index); - - virtual void SetHelpString(int Id, const wxString& helpString); - virtual wxString GetHelpString(int Id) const ; - - virtual int FindMenuItem(const wxString& menuString, const wxString& itemString) const ; - - // Find wxMenuItem for item ID, and return item's - // menu too if itemMenu is non-NULL. - wxMenuItem *FindItemForId(int itemId, wxMenu **menuForItem = NULL) const ; - - void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; } - wxEvtHandler *GetEventHandler() { return m_eventHandler; } - - int GetMenuCount() const { return m_menuCount; } - wxMenu* GetMenu(int i) const { return m_menus[i]; } - - //// Motif-specific + virtual ~wxMenuBar(); + + // implement base class (pure) virtuals + // ------------------------------------ + + virtual bool Append( wxMenu *menu, const wxString &title ); + virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title); + virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title); + virtual wxMenu *Remove(size_t pos); + + virtual int FindMenuItem(const wxString& menuString, + const wxString& itemString) const; + virtual wxMenuItem* FindItem( int id, wxMenu **menu = NULL ) const; + + virtual void EnableTop( size_t pos, bool flag ); + virtual void SetLabelTop( size_t pos, const wxString& label ); + virtual wxString GetLabelTop( size_t pos ) const; + + // implementation only from now on + // ------------------------------- + wxFrame* GetMenuBarFrame() const { return m_menuBarFrame; } void SetMenuBarFrame(wxFrame* frame) { m_menuBarFrame = frame; } WXWidget GetMainWidget() const { return m_mainWidget; } @@ -254,24 +229,25 @@ public: 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); + virtual bool SetBackgroundColour(const wxColour& colour); + virtual bool SetForegroundColour(const wxColour& colour); + virtual bool SetFont(const wxFont& colour); void ChangeFont(bool keepOriginalSize = FALSE); public: - wxEvtHandler * m_eventHandler; - int m_menuCount; - wxMenu ** m_menus; - wxString * m_titles; - wxFrame * m_menuBarFrame; - - //// Motif-specific - WXWidget m_mainWidget; - - wxColour m_foregroundColour; - wxColour m_backgroundColour; - wxFont m_font; + // common part of all ctors + void Init(); + + wxArrayString m_titles; + wxFrame *m_menuBarFrame; + + WXWidget m_mainWidget; + + wxColour m_foregroundColour; + wxColour m_backgroundColour; + wxFont m_font; + + DECLARE_DYNAMIC_CLASS(wxMenuBar) }; -#endif // _WX_MENU_H_ +#endif // _WX_MOTIF_MENU_H_ diff --git a/include/wx/motif/menuitem.h b/include/wx/motif/menuitem.h index d4b9dc9e17..75649d9f30 100644 --- a/include/wx/motif/menuitem.h +++ b/include/wx/motif/menuitem.h @@ -9,107 +9,54 @@ // Licence: wxWindows license /////////////////////////////////////////////////////////////////////////////// -#ifndef _MENUITEM_H -#define _MENUITEM_H +#ifndef _WX_MOTIF_MENUITEM_H +#define _WX_MOTIF_MENUITEM_H #ifdef __GNUG__ -#pragma interface "menuitem.h" + #pragma interface "menuitem.h" #endif -// ---------------------------------------------------------------------------- -// headers -// ---------------------------------------------------------------------------- - -#include "wx/setup.h" - -// an exception to the general rule that a normal header doesn't include other -// headers - only because ownerdrw.h is not always included and I don't want -// to write #ifdef's everywhere... -#if wxUSE_OWNER_DRAWN -#include "wx/ownerdrw.h" -#endif - -// ---------------------------------------------------------------------------- -// constants -// ---------------------------------------------------------------------------- - -// id for a separator line in the menu (invalid for normal item) -// #define ID_SEPARATOR (-1) - // ---------------------------------------------------------------------------- // wxMenuItem: an item in the menu, optionally implements owner-drawn behaviour // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxMenuItem: public wxObject -#if wxUSE_OWNER_DRAWN - , public wxOwnerDrawn -#endif -{ -DECLARE_DYNAMIC_CLASS(wxMenuItem) +class WXDLLEXPORT wxMenuItem: public wxMenuItemBase +{ public: - // ctor & dtor - wxMenuItem(wxMenu *pParentMenu = NULL, int id = ID_SEPARATOR, - const wxString& strName = "", const wxString& wxHelp = "", - bool bCheckable = FALSE, wxMenu *pSubMenu = NULL); - virtual ~wxMenuItem(); - - // accessors (some more are inherited from wxOwnerDrawn or are below) - bool IsSeparator() const { return m_idItem == ID_SEPARATOR; } - bool IsEnabled() const { return m_bEnabled; } - bool IsChecked() const { return m_bChecked; } - - int GetId() const { return m_idItem; } - const wxString& GetHelp() const { return m_strHelp; } - wxMenu *GetSubMenu() const { return m_pSubMenu; } + // ctor & dtor + wxMenuItem(wxMenu *parentMenu = (wxMenu *)NULL, + int id = wxID_SEPARATOR, + const wxString& text = wxEmptyString, + const wxString& help = wxEmptyString, + bool isCheckable = FALSE, + wxMenu *subMenu = (wxMenu *)NULL); + ~wxMenuItem(); - // operations - void SetName(const wxString& strName) { m_strName = strName; } - void SetHelp(const wxString& strHelp) { m_strHelp = strHelp; } + // accessors (some more are inherited from wxOwnerDrawn or are below) + virtual void SetText(const wxString& label); + virtual void Enable(bool enable = TRUE); + virtual void Check(bool check = TRUE); - void Enable(bool bDoEnable = TRUE); - void Check(bool bDoCheck = TRUE); + void DeleteSubMenu(); - void DeleteSubMenu(); + // implementation from now on + void CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu); + void DestroyItem(bool full); - //// Motif-specific + WXWidget GetButtonWidget() const { return m_buttonWidget; } - // These two should probably exist for all ports - void SetLabel(const wxString& label); - wxString GetLabel() const { return m_strName; } - void CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMenu); - void DestroyItem(bool full); + wxMenuBar* GetMenuBar() const { return m_menuBar; } + void SetMenuBar(wxMenuBar* menuBar) { m_menuBar = menuBar; } - inline WXWidget GetButtonWidget() const { return m_buttonWidget; } - inline void SetChecked(bool check) { m_bChecked = check; } - inline wxMenuBar* GetMenuBar() const { return m_menuBar; } - inline void SetMenuBar(wxMenuBar* menuBar) { m_menuBar = menuBar; } - inline wxMenu* GetTopMenu() const { return m_topMenu; } - inline void SetTopMenu(wxMenu* menu) { m_topMenu = menu; } + wxMenu* GetTopMenu() const { return m_topMenu; } + void SetTopMenu(wxMenu* menu) { m_topMenu = menu; } private: - int m_idItem; // numeric id of the item - wxString m_strHelp; // associated help string - wxMenu *m_pSubMenu, // may be NULL - *m_pParentMenu; // menu this item is contained in - bool m_bEnabled, // enabled or greyed? - m_bChecked; // checked? (only if checkable) + WXWidget m_buttonWidget; + wxMenuBar* m_menuBar; + wxMenu* m_topMenu; // Top-level menu e.g. popup-menu - //// Motif-specific - WXWidget m_buttonWidget; - wxMenuBar* m_menuBar; - wxMenu* m_topMenu; // Top-level menu e.g. popup-menu - -#if wxUSE_OWNER_DRAWN - // wxOwnerDrawn base class already has these variables - nothing to do - -#else //!owner drawn - bool m_bCheckable; // can be checked? - wxString m_strName; // name or label of the item - -public: - const wxString& GetName() const { return m_strName; } - bool IsCheckable() const { return m_bCheckable; } -#endif //owner drawn + DECLARE_DYNAMIC_CLASS(wxMenuItem) }; -#endif //_MENUITEM_H +#endif // _WX_MOTIF_MENUITEM_H diff --git a/src/motif/dataobj.cpp b/src/motif/dataobj.cpp index 5564b4c24c..de8ab39666 100644 --- a/src/motif/dataobj.cpp +++ b/src/motif/dataobj.cpp @@ -33,7 +33,6 @@ Atom g_textAtom = 0; wxDataFormat::wxDataFormat() { - if (!g_textAtom) g_textAtom = XInternAtom( (Display*) wxGetDisplay(), "STRING", FALSE ); m_type = wxDF_INVALID; m_hasAtom = FALSE; m_atom = (Atom) 0; diff --git a/src/motif/menu.cpp b/src/motif/menu.cpp index 48dcafe35f..4e0b3af8eb 100644 --- a/src/motif/menu.cpp +++ b/src/motif/menu.cpp @@ -11,17 +11,17 @@ // ============================================================================ -// headers & declarations +// declarations // ============================================================================ -// wxWindows headers -// ----------------- - #ifdef __GNUG__ -#pragma implementation "menu.h" -#pragma implementation "menuitem.h" + #pragma implementation "menu.h" #endif +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + #include "wx/menu.h" #include "wx/menuitem.h" #include "wx/log.h" @@ -43,7 +43,6 @@ #include "wx/motif/private.h" // other standard headers -// ---------------------- #include #if !USE_SHARED_LIBRARY @@ -55,7 +54,9 @@ IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) // implementation // ============================================================================ +// ---------------------------------------------------------------------------- // Menus +// ---------------------------------------------------------------------------- // Construct a menu with optional title (then use append) void wxMenu::Init(const wxString& title, @@ -66,7 +67,6 @@ void wxMenu::Init(const wxString& title, ) { m_title = title; - m_parent = (wxEvtHandler*) NULL; m_eventHandler = this; m_noItems = 0; m_menuBar = NULL; @@ -272,7 +272,7 @@ void wxMenu::SetLabel(int id, const wxString& label) if (item == (wxMenuItem*) NULL) return; - item->SetLabel(label); + item->SetText(label); } wxString wxMenu::GetLabel(int id) const @@ -434,26 +434,13 @@ void wxMenu::UpdateUI(wxEvtHandler* source) } } +// ---------------------------------------------------------------------------- // Menu Bar -wxMenuBar::wxMenuBar() -{ - m_eventHandler = this; - m_menuCount = 0; - m_menus = NULL; - m_titles = NULL; - m_menuBarFrame = NULL; - m_mainWidget = (WXWidget) NULL; - m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU); - m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT); - m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT); -} +// ---------------------------------------------------------------------------- -wxMenuBar::wxMenuBar(long WXUNUSED(style)) +void wxMenuBar::Init() { m_eventHandler = this; - m_menuCount = 0; - m_menus = NULL; - m_titles = NULL; m_menuBarFrame = NULL; m_mainWidget = (WXWidget) NULL; m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU); @@ -463,241 +450,124 @@ wxMenuBar::wxMenuBar(long WXUNUSED(style)) wxMenuBar::wxMenuBar(int n, wxMenu *menus[], const wxString titles[]) { - m_eventHandler = this; - m_menuCount = n; - m_menus = menus; - m_titles = new wxString[n]; - int i; - for ( i = 0; i < n; i++ ) - m_titles[i] = titles[i]; - m_menuBarFrame = NULL; - m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENU); - m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_MENUTEXT); - m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT); -} + Init(); -wxMenuBar::~wxMenuBar() -{ - int i; - for (i = 0; i < m_menuCount; i++) + for ( int i = 0; i < n; i++ ) { - delete m_menus[i]; + m_menus.Append(menus[i]); + m_titles.Add(titles[i]); } - delete[] m_menus; - delete[] m_titles; } -// Must only be used AFTER menu has been attached to frame, -// otherwise use individual menus to enable/disable items -void wxMenuBar::Enable(int id, bool flag) +wxMenuBar::~wxMenuBar() { - wxMenu *itemMenu = NULL; - wxMenuItem *item = FindItemForId(id, &itemMenu) ; - if (!item) - return; - item->Enable(flag); + // nothing to do: wxMenuBarBase will delete the menus } -void wxMenuBar::EnableTop(int WXUNUSED(pos), bool WXUNUSED(flag)) +void wxMenuBar::EnableTop(size_t WXUNUSED(pos), bool WXUNUSED(flag)) { - // TODO + wxFAIL_MSG("TODO"); } -// Must only be used AFTER menu has been attached to frame, -// otherwise use individual menus -void wxMenuBar::Check(int id, bool flag) +void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) { - wxMenu *itemMenu = NULL; - wxMenuItem *item = FindItemForId(id, &itemMenu) ; - if (!item) + wxMenu *menu = GetMenu(pos); + if ( !menu ) return; - if (!item->IsCheckable()) - return ; - - item->Check(flag); -} - -bool wxMenuBar::Checked(int id) const -{ - wxMenu *itemMenu = NULL; - wxMenuItem *item = FindItemForId(id, &itemMenu) ; - if (!item) - return FALSE; - - return item->IsChecked(); -} - -bool wxMenuBar::Enabled(int id) const -{ - wxMenu *itemMenu = NULL; - wxMenuItem *item = FindItemForId(id, &itemMenu) ; - if (!item) - return FALSE; + Widget w = (Widget)menu->GetButtonWidget(); + if (w) + { + wxXmString label_str(label); - return item->IsEnabled(); + XtVaSetValues(w, + XmNlabelString, label_str(), + NULL); + } } -void wxMenuBar::SetLabel(int id, const wxString& label) +wxString wxMenuBar::GetLabelTop(size_t pos) const { - wxMenu *itemMenu = NULL; - wxMenuItem *item = FindItemForId(id, &itemMenu) ; + wxString str; - if (!item) - return; - - item->SetLabel(label); -} + wxMenu *menu = GetMenu(pos); + if ( menu ) + { + Widget w = (Widget)menu->GetButtonWidget(); + if (w) + { + XmString text; + XtVaGetValues(w, + XmNlabelString, &text, + NULL); -wxString wxMenuBar::GetLabel(int id) const -{ - wxMenu *itemMenu = NULL; - wxMenuItem *item = FindItemForId(id, &itemMenu) ; + char *s; + if ( XmStringGetLtoR(text, XmSTRING_DEFAULT_CHARSET, &s) ) + { + str = s; - if (!item) - return wxString(""); + XtFree(s); + } + } + } - return item->GetLabel(); + return str; } -void wxMenuBar::SetLabelTop(int pos, const wxString& label) +bool wxMenuBar::Append(wxMenu * menu, const wxString& title) { - wxASSERT( (pos < m_menuCount) ); + wxCHECK_MSG( menu, FALSE, wxT("invalid menu") ); + wxCHECK_MSG( !menu->GetParent() && !menu->GetButtonWidget(), FALSE, + wxT("menu already appended") ); - Widget w = (Widget) m_menus[pos]->GetButtonWidget(); - if (w) + if ( m_menuBarFrame ) { - XmString label_str = XmStringCreateSimple ((char*) (const char*) label); - XtVaSetValues (w, - XmNlabelString, label_str, - NULL); - XmStringFree (label_str); + WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE); + wxCHECK_MSG( w, FALSE, wxT("failed to create menu") ); + menu->SetButtonWidget(w); } -} - -wxString wxMenuBar::GetLabelTop(int pos) const -{ - wxASSERT( (pos < m_menuCount) ); - Widget w = (Widget) m_menus[pos]->GetButtonWidget(); - if (w) - { - XmString text; - char *s; - XtVaGetValues (w, - XmNlabelString, &text, - NULL); - - if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s)) - { - wxString str(s); - XtFree (s); - return str; - } - else - { - return wxEmptyString; - } - } - else - return wxEmptyString; - -} + menu->SetMenuBar(this); -bool wxMenuBar::OnDelete(wxMenu *menu, int WXUNUSED(pos)) -{ - // Only applies to dynamic deletion (when set in frame) - if (!m_menuBarFrame) - return TRUE; + m_titles.Add(title); - menu->DestroyMenu(TRUE); - return TRUE; + return wxMenuBarBase::Append(menu, title); } -bool wxMenuBar::OnAppend(wxMenu *menu, const char *title) +bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) { - // Only applies to dynamic append (when set in frame) - if (!m_menuBarFrame) - return TRUE; - - // Probably should be an assert here - if (menu->GetParent()) - return FALSE; - - // Has already been appended - if (menu->GetButtonWidget()) + if ( !wxMenuBarBase::Insert(pos, menu, title) ) return FALSE; - WXWidget w = menu->CreateMenu(this, GetMainWidget(), menu, title, TRUE); - menu->SetButtonWidget(w); + wxFAIL_MSG(wxT("TODO")); - return TRUE; + return FALSE; } -void wxMenuBar::Append (wxMenu * menu, const wxString& title) +wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) { - if (!OnAppend(menu, title)) - return; - - m_menuCount ++; - wxMenu **new_menus = new wxMenu *[m_menuCount]; - wxString *new_titles = new wxString[m_menuCount]; - int i; - - for (i = 0; i < m_menuCount - 1; i++) - { - new_menus[i] = m_menus[i]; - m_menus[i] = NULL; - new_titles[i] = m_titles[i]; - m_titles[i] = ""; - } - if (m_menus) - { - delete[]m_menus; - delete[]m_titles; - } - m_menus = new_menus; - m_titles = new_titles; + if ( !wxMenuBarBase::Replace(pos, menu, title) ) + return FALSE; - m_menus[m_menuCount - 1] = (wxMenu *)menu; - m_titles[m_menuCount - 1] = title; + wxFAIL_MSG(wxT("TODO")); - menu->SetMenuBar(this); - menu->SetParent(this); + return NULL; } -void wxMenuBar::Delete(wxMenu * menu, int i) +wxMenu *wxMenuBar::Remove(size_t pos) { - int j; - int ii = (int) i; + wxMenu *menu = wxMenuBarBase::Remove(pos); + if ( !menu ) + return NULL; - if (menu != 0) - { - for (ii = 0; ii < m_menuCount; ii++) - { - if (m_menus[ii] == menu) - break; - } - if (ii >= m_menuCount) - return; - } else - { - if (ii < 0 || ii >= m_menuCount) - return; - menu = m_menus[ii]; - } + if ( m_menuBarFrame ) + menu->DestroyMenu(TRUE); - if (!OnDelete(menu, ii)) - return; + menu->SetMenuBar(NULL); - menu->SetParent((wxEvtHandler*) NULL); + m_titles.Remove(pos); - -- m_menuCount; - for (j = ii; j < m_menuCount; j++) - { - m_menus[j] = m_menus[j + 1]; - m_titles[j] = m_titles[j + 1]; - } + return menu; } // Find the menu menuString, item itemString, and return the item id. @@ -707,8 +577,9 @@ int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemStr char buf1[200]; char buf2[200]; wxStripMenuCodes ((char *)(const char *)menuString, buf1); - int i; - for (i = 0; i < m_menuCount; i++) + + size_t menuCount = GetMenuCount(); + for (size_t i = 0; i < menuCount; i++) { wxStripMenuCodes ((char *)(const char *)m_titles[i], buf2); if (strcmp (buf1, buf2) == 0) @@ -717,43 +588,19 @@ int wxMenuBar::FindMenuItem (const wxString& menuString, const wxString& itemStr return -1; } -wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu ** itemMenu) const +wxMenuItem *wxMenuBar::FindItem(int id, wxMenu ** itemMenu) const { if (itemMenu) *itemMenu = NULL; wxMenuItem *item = NULL; - int i; - for (i = 0; i < m_menuCount; i++) + size_t menuCount = GetMenuCount(); + for (size_t i = 0; i < menuCount; i++) if ((item = m_menus[i]->FindItemForId (id, itemMenu))) return item; return NULL; } -void wxMenuBar::SetHelpString (int id, const wxString& helpString) -{ - int i; - for (i = 0; i < m_menuCount; i++) - { - if (m_menus[i]->FindItemForId (id)) - { - m_menus[i]->SetHelpString (id, helpString); - return; - } - } -} - -wxString wxMenuBar::GetHelpString (int id) const -{ - int i; - for (i = 0; i < m_menuCount; i++) - { - if (m_menus[i]->FindItemForId (id)) - return wxString(m_menus[i]->GetHelpString (id)); - } - return wxString(""); -} - // Create menubar bool wxMenuBar::CreateMenuBar(wxFrame* parent) { @@ -771,8 +618,8 @@ bool wxMenuBar::CreateMenuBar(wxFrame* parent) Widget menuBarW = XmCreateMenuBar ((Widget) parent->GetMainWindowWidget(), "MenuBar", NULL, 0); m_mainWidget = (WXWidget) menuBarW; - int i; - for (i = 0; i < GetMenuCount(); i++) + size_t menuCount = GetMenuCount(); + for (size_t i = 0; i < menuCount; i++) { wxMenu *menu = GetMenu(i); wxString title(m_titles[i]); @@ -816,8 +663,8 @@ bool wxMenuBar::DestroyMenuBar() XtUnmanageChild ((Widget) m_mainWidget); XtUnrealizeWidget ((Widget) m_mainWidget); - int i; - for (i = 0; i < GetMenuCount(); i++) + size_t menuCount = GetMenuCount(); + for (size_t i = 0; i < menuCount; i++) { wxMenu *menu = GetMenu(i); menu->DestroyMenu(TRUE); @@ -832,8 +679,6 @@ bool wxMenuBar::DestroyMenuBar() } //// Motif-specific - -extern wxApp *wxTheApp; static XtWorkProcId WorkProcMenuId; /* Since PopupMenu under Motif stills grab right mouse button events @@ -1092,26 +937,30 @@ void wxMenu::SetFont(const wxFont& font) ChangeFont(); } -void wxMenuBar::SetBackgroundColour(const wxColour& col) +bool wxMenuBar::SetBackgroundColour(const wxColour& col) { - m_backgroundColour = col; if (m_mainWidget) wxDoChangeBackgroundColour(m_mainWidget, (wxColour&) col); - int i; - for (i = 0; i < m_menuCount; i++) + + size_t menuCount = GetMenuCount(); + for (size_t i = 0; i < menuCount; i++) m_menus[i]->SetBackgroundColour((wxColour&) col); + + return TRUE; } -void wxMenuBar::SetForegroundColour(const wxColour& col) +bool wxMenuBar::SetForegroundColour(const wxColour& col) { m_foregroundColour = col; if (m_mainWidget) wxDoChangeForegroundColour(m_mainWidget, (wxColour&) col); - int i; - for (i = 0; i < m_menuCount; i++) + size_t menuCount = GetMenuCount(); + for (size_t i = 0; i < menuCount; i++) m_menus[i]->SetForegroundColour((wxColour&) col); + + return TRUE; } void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize)) @@ -1119,13 +968,15 @@ void wxMenuBar::ChangeFont(bool WXUNUSED(keepOriginalSize)) // Nothing to do for menubar, fonts are kept in wxMenus } -void wxMenuBar::SetFont(const wxFont& font) +bool wxMenuBar::SetFont(const wxFont& font) { m_font = font; ChangeFont(); - int i; - for (i = 0; i < m_menuCount; i++) + size_t menuCount = GetMenuCount(); + for (size_t i = 0; i < menuCount; i++) m_menus[i]->SetFont(font); + + return TRUE; } diff --git a/src/motif/menuitem.cpp b/src/motif/menuitem.cpp index 7471d9b0de..577cf2f7ab 100644 --- a/src/motif/menuitem.cpp +++ b/src/motif/menuitem.cpp @@ -10,9 +10,17 @@ /////////////////////////////////////////////////////////////////////////////// // ============================================================================ -// headers & declarations +// declarations // ============================================================================ +#ifdef __GNUG__ + #pragma implementation "menuitem.h" +#endif + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + #include "wx/menu.h" #include "wx/menuitem.h" #include "wx/utils.h" @@ -30,12 +38,13 @@ #include "wx/motif/private.h" -void wxMenuItemCallback (Widget w, XtPointer clientData, - XtPointer ptr); -void wxMenuItemArmCallback (Widget w, XtPointer clientData, - XtPointer ptr); -void wxMenuItemDisarmCallback (Widget w, XtPointer clientData, - XtPointer ptr); +// ---------------------------------------------------------------------------- +// functions prototypes +// ---------------------------------------------------------------------------- + +static void wxMenuItemCallback(Widget w, XtPointer clientData, XtPointer ptr); +static void wxMenuItemArmCallback(Widget w, XtPointer clientData, XtPointer ptr); +static void wxMenuItemDisarmCallback(Widget w, XtPointer clientData, XtPointer ptr); // ============================================================================ // implementation @@ -46,7 +55,7 @@ void wxMenuItemDisarmCallback (Widget w, XtPointer clientData, // ---------------------------------------------------------------------------- #if !USE_SHARED_LIBRARY -IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) + IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) #endif //USE_SHARED_LIBRARY // ---------------------------------------------------------------------------- @@ -59,23 +68,24 @@ IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id, const wxString& strName, const wxString& strHelp, bool bCheckable, - wxMenu *pSubMenu) : -m_strHelp(strHelp), -m_bCheckable(bCheckable), -m_strName(strName) + wxMenu *pSubMenu) { - wxASSERT( pParentMenu != NULL ); - - m_pParentMenu = pParentMenu; - m_pSubMenu = pSubMenu; - m_idItem = id; - m_bEnabled = TRUE; - m_bChecked = FALSE; - - //// Motif-specific - m_menuBar = NULL; + wxASSERT_MSG( pParentMenu != NULL, wxT("menuitem should have a menu") ); + + // common init + m_parentMenu = pParentMenu; + m_subMenu = pSubMenu; + m_id = id; + m_isEnabled = TRUE; + m_isChecked = FALSE; + m_help = strHelp; + m_isCheckable = bCheckable; + m_text = strName; + + // Motif-specific + m_menuBar = NULL; m_buttonWidget = (WXWidget) NULL; - m_topMenu = NULL; + m_topMenu = NULL; } wxMenuItem::~wxMenuItem() @@ -88,10 +98,10 @@ wxMenuItem::~wxMenuItem() // delete the sub menu void wxMenuItem::DeleteSubMenu() { - wxASSERT( m_pSubMenu != NULL ); + wxASSERT( m_subMenu != NULL ); - delete m_pSubMenu; - m_pSubMenu = NULL; + delete m_subMenu; + m_subMenu = NULL; } // change item state @@ -99,10 +109,11 @@ void wxMenuItem::DeleteSubMenu() void wxMenuItem::Enable(bool bDoEnable) { - if ( m_bEnabled != bDoEnable ) + if ( m_isChecked != bDoEnable ) { - if ( m_pSubMenu == NULL ) - { // normal menu item + if ( !IsSubMenu() ) + { + // normal menu item if (m_buttonWidget) XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable); } @@ -114,7 +125,7 @@ void wxMenuItem::Enable(bool bDoEnable) XtSetSensitive( (Widget) m_buttonWidget, (Boolean) bDoEnable); } - m_bEnabled = bDoEnable; + wxMenuItemBase::Enable(bDoEnable); } } @@ -122,13 +133,20 @@ void wxMenuItem::Check(bool bDoCheck) { wxCHECK_RET( IsCheckable(), "only checkable items may be checked" ); - if ( m_bChecked != bDoCheck ) + if ( m_isChecked != bDoCheck ) { - if (m_buttonWidget && XtIsSubclass ((Widget) m_buttonWidget, xmToggleButtonGadgetClass)) + if ( m_buttonWidget ) { - XtVaSetValues ( (Widget) m_buttonWidget, XmNset, (Boolean) bDoCheck, NULL); + wxASSERT_MSG( XtIsSubclass((Widget)m_buttonWidget, + xmToggleButtonGadgetClass), + wxT("checkable menu item must be a toggle button") ); + + XtVaSetValues((Widget)m_buttonWidget, + XmNset, (Boolean)bDoCheck, + NULL); } - m_bChecked = bDoCheck; + + wxMenuItemBase::Check(bDoCheck); } } @@ -143,12 +161,12 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen { // Id=-2 identifies a Title item. m_buttonWidget = (WXWidget) XtVaCreateManagedWidget - (wxStripMenuCodes(m_strName), + (wxStripMenuCodes(m_text), xmLabelGadgetClass, (Widget) menu, NULL); } - else if ((!m_strName.IsNull() && m_strName != "") && (!m_pSubMenu)) + else if ((!m_text.IsNull() && m_text != "") && (!m_subMenu)) { - wxString strName = wxStripMenuCodes(m_strName); + wxString strName = wxStripMenuCodes(m_text); if (IsCheckable()) { m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, @@ -160,13 +178,13 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen m_buttonWidget = (WXWidget) XtVaCreateManagedWidget (strName, xmPushButtonGadgetClass, (Widget) menu, NULL); - char mnem = wxFindMnemonic (m_strName); + char mnem = wxFindMnemonic (m_text); if (mnem != 0) XtVaSetValues ((Widget) m_buttonWidget, XmNmnemonic, mnem, NULL); //// TODO: proper accelerator treatment. What does wxFindAccelerator //// look for? - strName = m_strName; + strName = m_text; char *accel = wxFindAccelerator (strName); if (accel) XtVaSetValues ((Widget) m_buttonWidget, XmNaccelerator, accel, NULL); @@ -203,10 +221,10 @@ void wxMenuItem::CreateItem (WXWidget menu, wxMenuBar * menuBar, wxMenu * topMen m_buttonWidget = (WXWidget) XtVaCreateManagedWidget ("separator", xmSeparatorGadgetClass, (Widget) menu, NULL); } - else if (m_pSubMenu) + else if (m_subMenu) { - m_buttonWidget = m_pSubMenu->CreateMenu (menuBar, menu, topMenu, m_strName, TRUE); - m_pSubMenu->SetButtonWidget(m_buttonWidget); + m_buttonWidget = m_subMenu->CreateMenu (menuBar, menu, topMenu, m_text, TRUE); + m_subMenu->SetButtonWidget(m_buttonWidget); XtAddCallback ((Widget) m_buttonWidget, XmNcascadingCallback, (XtCallbackProc) wxMenuItemArmCallback, @@ -220,10 +238,10 @@ void wxMenuItem::DestroyItem(bool full) { if (GetId() == -2) { - ; // Nothing + ; // Nothing } - else if ((!m_strName.IsNull() && (m_strName != "")) && !m_pSubMenu) + else if ((!m_text.IsNull() && (m_text != "")) && !m_subMenu) { if (m_buttonWidget) { @@ -241,7 +259,7 @@ void wxMenuItem::DestroyItem(bool full) } else if (GetId() == -1) { - ; // Nothing + ; // Nothing } else if (GetSubMenu()) @@ -251,7 +269,7 @@ void wxMenuItem::DestroyItem(bool full) XtRemoveCallback ((Widget) m_buttonWidget, XmNcascadingCallback, wxMenuItemArmCallback, (XtPointer) this); } - m_pSubMenu->DestroyMenu(full); + m_subMenu->DestroyMenu(full); if (full) m_buttonWidget = NULL; } @@ -263,12 +281,12 @@ void wxMenuItem::DestroyItem(bool full) } } -void wxMenuItem::SetLabel(const wxString& label) +void wxMenuItem::SetText(const wxString& label) { char mnem = wxFindMnemonic (label); wxString label2 = wxStripMenuCodes(label); - m_strName = label; + m_text = label; if (m_buttonWidget) { @@ -291,6 +309,10 @@ void wxMenuItem::SetLabel(const wxString& label) } } +// ---------------------------------------------------------------------------- +// Motif callbacks +// ---------------------------------------------------------------------------- + void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData, XtPointer WXUNUSED(ptr)) { @@ -301,7 +323,9 @@ void wxMenuItemCallback (Widget WXUNUSED(w), XtPointer clientData, { Boolean isChecked = FALSE; XtVaGetValues ((Widget) item->GetButtonWidget(), XmNset, & isChecked, NULL); - item->SetChecked(isChecked); + + // only set the flag, don't actually check anything + item->wxMenuItemBase::Check(isChecked); } if (item->GetMenuBar() && item->GetMenuBar()->GetMenuBarFrame()) { -- 2.45.2