Simplify code dealing with toolbar position in associated frame

This commit is contained in:
PB 2020-05-26 23:43:54 +02:00 committed by Vadim Zeitlin
parent 46dfc54e8d
commit 13b15fecc0
5 changed files with 47 additions and 32 deletions

View File

@ -482,6 +482,10 @@ public:
// return true if this is a vertical toolbar, otherwise false // return true if this is a vertical toolbar, otherwise false
bool IsVertical() const; bool IsVertical() const;
// returns one of wxTB_TOP, wxTB_BOTTOM, wxTB_LEFT, wxTB_RIGHT
// indicating where the toolbar is placed in the associated frame
int GetDirection() const;
// these methods allow to access tools by their index in the toolbar // these methods allow to access tools by their index in the toolbar
size_t GetToolsCount() const { return m_tools.GetCount(); } size_t GetToolsCount() const { return m_tools.GetCount(); }
wxToolBarToolBase *GetToolByPos(int pos) { return m_tools[pos]; } wxToolBarToolBase *GetToolByPos(int pos) { return m_tools[pos]; }

View File

@ -628,6 +628,24 @@ bool wxToolBarBase::IsVertical() const
return HasFlag(wxTB_LEFT | wxTB_RIGHT); return HasFlag(wxTB_LEFT | wxTB_RIGHT);
} }
// wxTB_HORIZONTAL is same as wxTB_TOP and wxTB_VERTICAL is same as wxTB_LEFT,
// so a toolbar created with wxTB_HORIZONTAL | wxTB_BOTTOM style can have set both
// wxTB_TOP and wxTB_BOTTOM, similarly wxTB_VERTICAL | wxTB_RIGHT == wxTB_LEFT | wxTB_RIGHT.
// GetDirection() makes things less confusing and returns just one of wxTB_TOP, wxTB_BOTTOM,
// wxTB_LEFT, wxTB_RIGHT indicating where the toolbar is placed in the associated frame.
int wxToolBarBase::GetDirection() const
{
if ( HasFlag(wxTB_BOTTOM) )
return wxTB_BOTTOM;
if ( HasFlag(wxTB_RIGHT) )
return wxTB_RIGHT;
if ( HasFlag(wxTB_LEFT) )
return wxTB_LEFT;
return wxTB_TOP;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// event processing // event processing

View File

@ -318,15 +318,16 @@ void wxFrame::PositionStatusBar()
if ( toolbar ) if ( toolbar )
{ {
const wxSize sizeTB = toolbar->GetSize(); const wxSize sizeTB = toolbar->GetSize();
const int directionTB = toolbar->GetDirection();
if ( toolbar->HasFlag(wxTB_LEFT | wxTB_RIGHT) ) if ( toolbar->IsVertical() )
{ {
if ( !toolbar->HasFlag(wxTB_RIGHT) ) if ( directionTB == wxTB_LEFT )
x -= sizeTB.x; x -= sizeTB.x;
w += sizeTB.x; w += sizeTB.x;
} }
else if ( toolbar->HasFlag(wxTB_BOTTOM) ) else if ( directionTB == wxTB_BOTTOM )
{ {
// we need to position the status bar below the toolbar // we need to position the status bar below the toolbar
h += sizeTB.y; h += sizeTB.y;
@ -932,17 +933,13 @@ wxPoint wxFrame::GetClientAreaOrigin() const
if ( toolbar && toolbar->IsShown() ) if ( toolbar && toolbar->IsShown() )
{ {
const wxSize sizeTB = toolbar->GetSize(); const wxSize sizeTB = toolbar->GetSize();
const int directionTB = toolbar->GetDirection();
// wxTB_TOP has the same value as wxTB_HORIZONTAL so HasFlag(wxTB_TOP) if ( directionTB == wxTB_TOP )
// returns true for a toolbar created with wxTB_HORIZONTAL | wxTB_BOTTOM
// style despite the toolbar being on the frame bottom and not affecting
// the client area origin. We therefore need to check here not only for
// presence of wxTB_TOP but also for absence of wxTB_BOTTOM.
if ( toolbar->HasFlag(wxTB_TOP) && !toolbar->HasFlag(wxTB_BOTTOM) )
{ {
pt.y += sizeTB.y; pt.y += sizeTB.y;
} }
else if ( toolbar->HasFlag(wxTB_LEFT) && !toolbar->HasFlag(wxTB_RIGHT) ) else if ( directionTB == wxTB_LEFT )
{ {
pt.x += sizeTB.x; pt.x += sizeTB.x;
} }

View File

@ -59,14 +59,15 @@ wxPoint wxFrame::GetClientAreaOrigin() const
wxToolBar *toolbar = GetToolBar(); wxToolBar *toolbar = GetToolBar();
if ( toolbar && toolbar->IsShown() ) if ( toolbar && toolbar->IsShown() )
{ {
const int direction = toolbar->GetDirection();
int w, h; int w, h;
toolbar->GetSize(&w, &h); toolbar->GetSize(&w, &h);
if ( toolbar->HasFlag(wxTB_LEFT) && !toolbar->HasFlag(wxTB_RIGHT) ) if ( direction == wxTB_LEFT )
{ {
pt.x += w; pt.x += w;
} }
else if ( toolbar->HasFlag(wxTB_TOP) && !toolbar->HasFlag(wxTB_BOTTOM) ) else if ( direction == wxTB_TOP )
{ {
#if !wxOSX_USE_NATIVE_TOOLBAR #if !wxOSX_USE_NATIVE_TOOLBAR
pt.y += h; pt.y += h;
@ -338,16 +339,20 @@ void wxFrame::PositionToolBar()
if (GetToolBar()) if (GetToolBar())
{ {
const int direction = GetToolBar()->GetDirection();
int tx, ty, tw, th; int tx, ty, tw, th;
tx = ty = 0 ; tx = ty = 0 ;
GetToolBar()->GetSize(&tw, &th); GetToolBar()->GetSize(&tw, &th);
// Note: we need to test for wxTB_RIGHT before wxTB_LEFT, as the latter if (direction == wxTB_LEFT)
// is the same as wxTB_VERTICAL and it's possible to combine wxTB_RIGHT {
// with wxTB_VERTICAL, so testing for wxTB_LEFT could be true for a // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
// right-aligned toolbar (while the reverse is, luckily, impossible). // means, pretend we don't have toolbar/status bar, so we
if (GetToolBar()->HasFlag(wxTB_RIGHT)) // have the original client size.
GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
}
else if (direction == wxTB_RIGHT)
{ {
// Use the 'real' position. wxSIZE_NO_ADJUSTMENTS // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
// means, pretend we don't have toolbar/status bar, so we // means, pretend we don't have toolbar/status bar, so we
@ -355,14 +360,7 @@ void wxFrame::PositionToolBar()
tx = cw - tw; tx = cw - tw;
GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS ); GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
} }
else if (GetToolBar()->HasFlag(wxTB_LEFT)) else if (direction == wxTB_BOTTOM)
{
// Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
// means, pretend we don't have toolbar/status bar, so we
// have the original client size.
GetToolBar()->SetSize(tx , ty , tw, ch , wxSIZE_NO_ADJUSTMENTS );
}
else if (GetToolBar()->HasFlag(wxTB_BOTTOM))
{ {
tx = 0; tx = 0;
ty = ch - th; ty = ch - th;

View File

@ -1670,6 +1670,7 @@ void wxToolBar::OnPaint(wxPaintEvent& event)
else else
#endif #endif
{ {
const int direction = GetDirection();
int w, h; int w, h;
GetSize( &w, &h ); GetSize( &w, &h );
@ -1689,16 +1690,13 @@ void wxToolBar::OnPaint(wxPaintEvent& event)
dc.SetPen( wxPen( wxColour( 0x51,0x51,0x51 ) ) ); dc.SetPen( wxPen( wxColour( 0x51,0x51,0x51 ) ) );
// Note that testing this is in the "reverse" order, i.e. right before if ( direction == wxTB_RIGHT )
// left and bottom before top because these flags can actually be
// combined because left/top are the same as vertical/horizontal
if ( HasFlag(wxTB_RIGHT) )
dc.DrawLine(0, 0, 0, h); dc.DrawLine(0, 0, 0, h);
else if ( HasFlag(wxTB_LEFT) ) else if ( direction == wxTB_LEFT )
dc.DrawLine(w-1, 0, w-1, h); dc.DrawLine(w-1, 0, w-1, h);
else if ( HasFlag(wxTB_BOTTOM) ) else if ( direction == wxTB_BOTTOM )
dc.DrawLine(0, 0, w, 0); dc.DrawLine(0, 0, w, 0);
else if ( HasFlag(wxTB_TOP) ) else if ( direction == wxTB_TOP )
dc.DrawLine(0, h-1, w, h-1); dc.DrawLine(0, h-1, w, h-1);
} }
event.Skip(); event.Skip();