wxMenuBarBase for MSW (untested)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4205 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-10-26 19:35:25 +00:00
parent 3dfac9707a
commit a8cfd0cbf1
4 changed files with 113 additions and 188 deletions

View File

@@ -197,10 +197,8 @@ private:
// Menu Bar (a la Windows) // Menu Bar (a la Windows)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class WXDLLEXPORT wxMenuBar : public wxEvtHandler class WXDLLEXPORT wxMenuBar : public wxMenuBarBase
{ {
DECLARE_DYNAMIC_CLASS(wxMenuBar)
public: public:
// ctors & dtor // ctors & dtor
// default constructor // default constructor
@@ -212,66 +210,38 @@ public:
virtual ~wxMenuBar(); virtual ~wxMenuBar();
// menubar construction // menubar construction
WXHMENU Create(); virtual bool Append( wxMenu *menu, const wxString &title );
void Append(wxMenu *menu, const wxString& title); virtual bool Insert(size_t pos, wxMenu *menu, const wxString& title);
void Insert(int pos, wxMenu * menu, const wxString& title); virtual wxMenu *Replace(size_t pos, wxMenu *menu, const wxString& title);
void ReplaceMenu(int pos, wxMenu * new_menu, const wxString& title); virtual wxMenu *Remove(size_t pos);
int FindMenu(const wxString& title);
void Detach();
virtual void Delete(wxMenu *menu, int index = 0); /* Menu not destroyed */
// state control virtual int FindMenuItem(const wxString& menuString,
// NB: must only be used AFTER menu has been attached to frame, const wxString& itemString) const;
// otherwise use individual menus to enable/disable items virtual wxMenuItem* FindItem( int id, wxMenu **menu = NULL ) const;
// enable the item
void Enable(int id, bool enable);
// TRUE if item enabled
bool IsEnabled(int id) const;
//
void EnableTop(int pos, bool enable);
// works only with checkable items virtual void EnableTop( size_t pos, bool flag );
void Check(int id, bool check); virtual void SetLabelTop( size_t pos, const wxString& label );
// TRUE if checked virtual wxString GetLabelTop( size_t pos ) const;
bool IsChecked(int id) const;
void SetLabel(int id, const wxString& label) ;
wxString GetLabel(int id) const ;
virtual void SetHelpString(int id, const wxString& helpString);
virtual wxString GetHelpString(int id) const ;
void SetLabelTop(int pos, const wxString& label) ;
wxString GetLabelTop(int pos) const ;
// notifications: return FALSE to prevent the menu from being // notifications: return FALSE to prevent the menu from being
// appended/deleted // appended/deleted
virtual bool OnAppend(wxMenu *menu, const wxChar *title); virtual bool OnAppend(wxMenu *menu, const wxChar *title);
virtual bool OnDelete(wxMenu *menu, int index); virtual bool OnDelete(wxMenu *menu, int index);
// item search // compatibility: these functions are deprecated
// by menu and item names, returns wxNOT_FOUND if not found #ifdef WXWIN_COMPATIBILITY
virtual int FindMenuItem(const wxString& menuString,
const wxString& itemString) const;
// returns NULL if not found
wxMenuItem* FindItem(int id) const { return FindItemForId(id); }
// returns NULL if not found, fills menuForItem if !NULL
wxMenuItem *FindItemForId(int itemId, wxMenu **menuForItem = NULL) const;
// submenus access
int GetMenuCount() const { return m_menuCount; }
wxMenu *GetMenu(int i) const { return m_menus[i]; }
void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; } void SetEventHandler(wxEvtHandler *handler) { m_eventHandler = handler; }
wxEvtHandler *GetEventHandler() { return m_eventHandler; } wxEvtHandler *GetEventHandler() { return m_eventHandler; }
#ifdef WXWIN_COMPATIBILITY
// compatibility: these functions are deprecated
bool Enabled(int id) const { return IsEnabled(id); } bool Enabled(int id) const { return IsEnabled(id); }
bool Checked(int id) const { return IsChecked(id); } bool Checked(int id) const { return IsChecked(id); }
#endif // WXWIN_COMPATIBILITY #endif // WXWIN_COMPATIBILITY
// IMPLEMENTATION // implementation from now on
WXHMENU Create();
int FindMenu(const wxString& title);
void Detach();
// returns TRUE if we're attached to a frame // returns TRUE if we're attached to a frame
bool IsAttached() const { return m_menuBarFrame != NULL; } bool IsAttached() const { return m_menuBarFrame != NULL; }
// get the frame we live in // get the frame we live in
@@ -295,10 +265,12 @@ protected:
// common part of all ctors // common part of all ctors
void Init(); void Init();
#ifdef WXWIN_COMPATIBILITY
wxEvtHandler *m_eventHandler; wxEvtHandler *m_eventHandler;
int m_menuCount; #endif // WXWIN_COMPATIBILITY
wxMenu **m_menus;
wxString *m_titles; wxArrayString m_titles;
wxFrame *m_menuBarFrame; wxFrame *m_menuBarFrame;
WXHMENU m_hMenu; WXHMENU m_hMenu;
@@ -306,6 +278,9 @@ protected:
// the accelerator table for all accelerators in all our menus // the accelerator table for all accelerators in all our menus
wxAcceleratorTable m_accelTable; wxAcceleratorTable m_accelTable;
#endif // wxUSE_ACCEL #endif // wxUSE_ACCEL
private:
DECLARE_DYNAMIC_CLASS(wxMenuBar)
}; };
#endif // _WX_MENU_H_ #endif // _WX_MENU_H_

View File

@@ -49,7 +49,7 @@ public:
virtual void Enable(bool bDoEnable = TRUE); virtual void Enable(bool bDoEnable = TRUE);
virtual void Check(bool bDoCheck = TRUE); virtual void Check(bool bDoCheck = TRUE);
virtual void IsChecked() const; virtual bool IsChecked() const;
// unfortunately needed to resolve ambiguity between // unfortunately needed to resolve ambiguity between
// wxMenuItemBase::IsCheckable() and wxOwnerDrawn::IsCheckable() // wxMenuItemBase::IsCheckable() and wxOwnerDrawn::IsCheckable()

View File

@@ -585,8 +585,6 @@ void wxMenu::Detach()
void wxMenuBar::Init() void wxMenuBar::Init()
{ {
m_eventHandler = this; m_eventHandler = this;
m_menuCount = 0;
m_menus = NULL;
m_titles = NULL; m_titles = NULL;
m_menuBarFrame = NULL; m_menuBarFrame = NULL;
m_hMenu = 0; m_hMenu = 0;
@@ -606,27 +604,19 @@ wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
{ {
Init(); Init();
m_menuCount = count; m_titles.Alloc(count);
m_menus = menus;
m_titles = new wxString[count];
int i; for ( int i = 0; i < count; i++ )
for ( i = 0; i < count; i++ ) {
m_titles[i] = titles[i]; m_menus.Append(menus[i]);
m_titles.Add(titles[i]);
for ( i = 0; i < count; i++ ) menus[i]->Attach(this);
m_menus[i]->Attach(this); }
} }
wxMenuBar::~wxMenuBar() wxMenuBar::~wxMenuBar()
{ {
for ( int i = 0; i < m_menuCount; i++ )
{
delete m_menus[i];
}
delete[] m_menus;
delete[] m_titles;
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -655,7 +645,8 @@ WXHMENU wxMenuBar::Create()
} }
else else
{ {
for ( int i = 0; i < m_menuCount; i++ ) size_t count = GetMenuCount();
for ( size_t i = 0; i < count; i++ )
{ {
if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
(UINT)m_menus[i]->GetHMenu(), (UINT)m_menus[i]->GetHMenu(),
@@ -676,7 +667,7 @@ WXHMENU wxMenuBar::Create()
// NB: we don't support owner drawn top level items for now, if we do these // NB: we don't support owner drawn top level items for now, if we do these
// functions would have to be changed to use wxMenuItem as well // functions would have to be changed to use wxMenuItem as well
void wxMenuBar::EnableTop(int pos, bool enable) void wxMenuBar::EnableTop(size_t pos, bool enable)
{ {
int flag = enable ? MF_ENABLED : MF_GRAYED;; int flag = enable ? MF_ENABLED : MF_GRAYED;;
@@ -685,7 +676,7 @@ void wxMenuBar::EnableTop(int pos, bool enable)
Refresh(); Refresh();
} }
void wxMenuBar::SetLabelTop(int pos, const wxString& label) void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
{ {
UINT id; UINT id;
UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
@@ -714,7 +705,7 @@ void wxMenuBar::SetLabelTop(int pos, const wxString& label)
} }
} }
wxString wxMenuBar::GetLabelTop(int pos) const wxString wxMenuBar::GetLabelTop(size_t pos) const
{ {
int len = ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND); int len = ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND);
@@ -780,10 +771,13 @@ bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// wxMenuBar construction // wxMenuBar construction
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
int wxMenuBar::FindMenu(const wxString& title) int wxMenuBar::FindMenu(const wxString& title)
{ {
wxString menuTitle = wxStripMenuCodes(title); wxString menuTitle = wxStripMenuCodes(title);
for ( int i = 0; i < m_menuCount; i++ )
size_t count = GetMenuCount();
for ( size_t i = 0; i < count; i++ )
{ {
wxString title = wxStripMenuCodes(m_titles[i]); wxString title = wxStripMenuCodes(m_titles[i]);
if ( menuTitle == title ) if ( menuTitle == title )
@@ -794,121 +788,75 @@ int wxMenuBar::FindMenu(const wxString& title)
} }
wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
void wxMenuBar::ReplaceMenu(int pos, wxMenu * new_menu, const wxString& title)
{ {
if (m_menuBarFrame) return; if ( m_menuBarFrame )
if ( pos >= 0 && pos < m_menuCount )
{ {
wxMenu *old_menu = m_menus[pos]; wxFAIL_MSG(wxT("not implemented"));
m_menus[pos] = new_menu;
delete old_menu; return NULL;
}
else
{
wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
if ( menuOld )
{
m_titles[pos] = title;
} }
return menuOld;
}
} }
bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
void wxMenuBar::Insert(int pos, wxMenu * menu, const wxString& title)
{ {
if (m_menuBarFrame) return; if ( m_menuBarFrame )
if ( pos < 0 && pos >= m_menuCount ) return;
m_menuCount ++;
wxMenu **new_menus = new wxMenu *[m_menuCount];
wxString *new_titles = new wxString[m_menuCount];
int i;
for (i = 0; i < pos; i++)
{ {
new_menus[i] = m_menus[i]; wxFAIL_MSG(wxT("not implemented"));
m_menus[i] = NULL;
new_titles[i] = m_titles[i]; return FALSE;
m_titles[i] = wxT(""); }
else
{
if ( !wxMenuBarBase::Insert(pos, menu, title) )
return FALSE;
m_titles.Insert(title, pos);
return TRUE;
}
} }
new_menus[pos] = (wxMenu *)menu; bool wxMenuBar::Append(wxMenu * menu, const wxString& title)
new_titles[i] = title; {
if ( !wxMenuBarBase::Append(menu, title) )
return FALSE;
for (i = pos+1; i < m_menuCount; i++) // menu is already appended, ignore errors
{ (void)OnAppend(menu, title);
new_menus[i] = m_menus[i-1];
m_menus[i-1] = NULL; m_titles.Add(title);
new_titles[i] = m_titles[i-1];
m_titles[i-1] = wxT("");
}
if (m_menus)
{
delete[]m_menus;
delete[]m_titles;
}
m_menus = new_menus;
m_titles = new_titles;
menu->SetParent(this); menu->SetParent(this);
return TRUE;
} }
wxMenu *wxMenuBar::Remove(size_t pos)
void wxMenuBar::Append (wxMenu * menu, const wxString& title)
{ {
if (!OnAppend(menu, title)) wxMenu *menu = wxMenuBarBase::Remove(pos);
return; if ( !menu )
return NULL;
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] = wxT("");
}
if (m_menus)
{
delete[]m_menus;
delete[]m_titles;
}
m_menus = new_menus;
m_titles = new_titles;
m_menus[m_menuCount - 1] = (wxMenu *)menu;
m_titles[m_menuCount - 1] = title;
menu->SetParent(this);
}
void wxMenuBar::Delete(wxMenu * menu, int i)
{
int j;
int ii = (int) i;
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 (!OnDelete(menu, ii))
return;
menu->SetParent(NULL); menu->SetParent(NULL);
-- m_menuCount; // the menu is deleted from the list anyhow, so we have to ignore all
for (j = ii; j < m_menuCount; j++) { // possible errors here
m_menus[j] = m_menus[j + 1]; (void)OnDelete(menu, pos);
m_titles[j] = m_titles[j + 1];
} m_titles.Remove(pos);
return menu;
} }
void wxMenuBar::Attach(wxFrame *frame) void wxMenuBar::Attach(wxFrame *frame)
@@ -921,8 +869,8 @@ void wxMenuBar::Attach(wxFrame *frame)
// create the accel table - we consider that the menubar construction is // create the accel table - we consider that the menubar construction is
// finished // finished
size_t nAccelCount = 0; size_t nAccelCount = 0;
int i; size_t i, count = GetMenuCount();
for ( i = 0; i < m_menuCount; i++ ) for ( i = 0; i < count; i++ )
{ {
nAccelCount += m_menus[i]->GetAccelCount(); nAccelCount += m_menus[i]->GetAccelCount();
} }
@@ -932,7 +880,7 @@ void wxMenuBar::Attach(wxFrame *frame)
wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
nAccelCount = 0; nAccelCount = 0;
for ( i = 0; i < m_menuCount; i++ ) for ( i = 0; i < count; i++ )
{ {
nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
} }
@@ -961,7 +909,8 @@ int wxMenuBar::FindMenuItem(const wxString& menuString,
const wxString& itemString) const const wxString& itemString) const
{ {
wxString menuLabel = wxStripMenuCodes(menuString); wxString menuLabel = wxStripMenuCodes(menuString);
for ( int i = 0; i < m_menuCount; i++ ) size_t count = GetMenuCount();
for ( size_t i = 0; i < count; i++ )
{ {
wxString title = wxStripMenuCodes(m_titles[i]); wxString title = wxStripMenuCodes(m_titles[i]);
if ( menuString == title ) if ( menuString == title )
@@ -971,13 +920,14 @@ int wxMenuBar::FindMenuItem(const wxString& menuString,
return wxNOT_FOUND; return wxNOT_FOUND;
} }
wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu **itemMenu) const wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const
{ {
if ( itemMenu ) if ( itemMenu )
*itemMenu = NULL; *itemMenu = NULL;
wxMenuItem *item = NULL; wxMenuItem *item = NULL;
for ( int i = 0; !item && (i < m_menuCount); i++ ) size_t count = GetMenuCount();
for ( size_t i = 0; !item && (i < count); i++ )
{ {
item = m_menus[i]->FindItemForId(id, itemMenu); item = m_menus[i]->FindItemForId(id, itemMenu);
} }

View File

@@ -140,9 +140,9 @@ void wxMenuItem::DeleteSubMenu()
// get item state // get item state
// -------------- // --------------
void wxMenuItem::IsChecked() const bool wxMenuItem::IsChecked() const
{ {
int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), id, MF_BYCOMMAND); int flag = ::GetMenuState(GetHMenuOf(m_parentMenu), GetId(), MF_BYCOMMAND);
// don't "and" with MF_ENABLED because its value is 0 // don't "and" with MF_ENABLED because its value is 0
return (flag & MF_DISABLED) == 0; return (flag & MF_DISABLED) == 0;