generic status bar now:

1. works correctly (handles negative widths according to the docs/MSW/Univ)
2. has shorter and simpler GetFieldRect() implementation
3. ... which is also more efficient (pixel widths are cached)


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16966 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-09-04 13:16:46 +00:00
parent 333e110dab
commit 390015c0ee
3 changed files with 64 additions and 154 deletions

View File

@@ -148,72 +148,6 @@ strings.
Saves the current field text in a per field stack, and sets the field text Saves the current field text in a per field stack, and sets the field text
to the string passed as argument. to the string passed as argument.
%% VZ: these functions are not in wxStatusBar API, these are just
%% implementation details of wxStatusBarGeneric
%%
%% \membersection{wxStatusBar::DrawField}\label{wxstatusbardrawfield}
%%
%% \func{virtual void}{DrawField}{\param{wxDC\& }{dc}, \param{int }{i}}
%%
%% Draws a field, including shaded borders and text.
%%
%% \wxheading{Parameters}
%%
%% \docparam{dc}{The device context to draw onto.}
%%
%% \docparam{i}{The field to be drawn.}
%%
%% \wxheading{See also}
%%
%% \helpref{wxStatusBar::DrawFieldText}{wxstatusbardrawfieldtext}
%%
%% \membersection{wxStatusBar::DrawFieldText}\label{wxstatusbardrawfieldtext}
%%
%% \func{virtual void}{DrawFieldText}{\param{wxDC\& }{dc}, \param{int }{i}}
%%
%% Draws a field's text.
%%
%% \wxheading{Parameters}
%%
%% \docparam{dc}{The device context to draw onto.}
%%
%% \docparam{i}{The field whose text is to be drawn.}
%%
%% \wxheading{See also}
%%
%% \helpref{wxStatusBar::DrawField}{wxstatusbardrawfield}
%%
%% \membersection{wxStatusBar::InitColours}\label{wxstatusbarinitcolours}
%%
%% \func{virtual void}{InitColours}{\void}
%%
%% Sets up the background colour and shading pens using suitable system colours (Windows) or tasteful shades
%% of grey (other platforms).
%%
%% \wxheading{Remarks}
%%
%% This function is called when the window is created, and also
%% from \helpref{wxStatusBar::OnSysColourChanged}{wxstatusbaronsyscolourchanged} on Windows.
%%
%% \wxheading{See also}
%%
%% \helpref{wxStatusBar::OnSysColourChanged}{wxstatusbaronsyscolourchanged}
%%
%% \membersection{wxStatusBar::OnSysColourChanged}\label{wxstatusbaronsyscolourchanged}
%%
%% \func{void}{OnSysColourChanged}{\param{wxSysColourChangedEvent\& }{event}}
%%
%% Handles a system colour change by calling \helpref{wxStatusBar::InitColours}{wxstatusbarinitcolours},
%% and refreshes the window.
%%
%% \wxheading{Parameters}
%%
%% \docparam{event}{The colour change event.}
%%
%% \wxheading{See also}
%%
%% \helpref{wxStatusBar::InitColours}{wxstatusbarinitcolours}
\membersection{wxStatusBar::SetFieldsCount}\label{wxstatusbarsetfieldscount} \membersection{wxStatusBar::SetFieldsCount}\label{wxstatusbarsetfieldscount}
\func{virtual void}{SetFieldsCount}{\param{int}{ number = 1}, \param{int* }{widths = NULL}} \func{virtual void}{SetFieldsCount}{\param{int}{ number = 1}, \param{int* }{widths = NULL}}
@@ -230,9 +164,8 @@ Use SetStatusWidths to set the field widths.}
\docparam{number}{The number of fields.} \docparam{number}{The number of fields.}
\docparam{widths}{An array of {\it n} integers, each of which is a status field width \docparam{widths}{An array of {\it n} integers interpreted in the same way as
in pixels. A value of -1 indicates that the field is variable width; at least one in \helpref{SetStatusWidths}{wxstatusbarsetstatuswidths}}
field must be -1.}
\membersection{wxStatusBar::SetMinHeight}\label{wxstatusbarsetminheight} \membersection{wxStatusBar::SetMinHeight}\label{wxstatusbarsetminheight}

View File

@@ -25,7 +25,7 @@ WXDLLEXPORT_DATA(extern const wxChar*) wxPanelNameStr;
class WXDLLEXPORT wxStatusBarGeneric : public wxStatusBarBase class WXDLLEXPORT wxStatusBarGeneric : public wxStatusBarBase
{ {
public: public:
wxStatusBarGeneric(); wxStatusBarGeneric() { Init(); }
wxStatusBarGeneric(wxWindow *parent, wxStatusBarGeneric(wxWindow *parent,
wxWindowID id, wxWindowID id,
const wxPoint& pos = wxDefaultPosition, const wxPoint& pos = wxDefaultPosition,
@@ -33,17 +33,21 @@ public:
long style = 0, long style = 0,
const wxString& name = wxPanelNameStr) const wxString& name = wxPanelNameStr)
{ {
Init();
Create(parent, id, pos, size, style, name); Create(parent, id, pos, size, style, name);
} }
wxStatusBarGeneric(wxWindow *parent, wxStatusBarGeneric(wxWindow *parent,
wxWindowID id, wxWindowID id,
long style, long style,
const wxString& name = wxPanelNameStr) const wxString& name = wxPanelNameStr)
{ {
Init();
Create(parent, id, style, name); Create(parent, id, style, name);
} }
~wxStatusBarGeneric(); virtual ~wxStatusBarGeneric();
bool Create(wxWindow *parent, wxWindowID id, bool Create(wxWindow *parent, wxWindowID id,
const wxPoint& WXUNUSED(pos) = wxDefaultPosition, const wxPoint& WXUNUSED(pos) = wxDefaultPosition,
@@ -61,7 +65,6 @@ public:
// Create status line // Create status line
virtual void SetFieldsCount(int number = 1, virtual void SetFieldsCount(int number = 1,
const int *widths = (const int *) NULL); const int *widths = (const int *) NULL);
int GetFieldsCount() const { return m_nFields; }
// Set status line text // Set status line text
virtual void SetStatusText(const wxString& text, int number = 0); virtual void SetStatusText(const wxString& text, int number = 0);
@@ -89,6 +92,7 @@ public:
void SetBorderY(int y); void SetBorderY(int y);
void OnPaint(wxPaintEvent& event); void OnPaint(wxPaintEvent& event);
void OnSize(wxSizeEvent& event);
virtual void InitColours(); virtual void InitColours();
@@ -96,7 +100,14 @@ public:
void OnSysColourChanged(wxSysColourChangedEvent& event); void OnSysColourChanged(wxSysColourChangedEvent& event);
protected: protected:
// common part of all ctors
void Init();
wxArrayString m_statusStrings; wxArrayString m_statusStrings;
// the widths of the status bar panes in pixels
wxArrayInt m_widthsAbs;
int m_borderX; int m_borderX;
int m_borderY; int m_borderY;
wxFont m_defaultStatusBarFont; wxFont m_defaultStatusBarFont;

View File

@@ -44,6 +44,8 @@
BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow) BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
EVT_PAINT(wxStatusBarGeneric::OnPaint) EVT_PAINT(wxStatusBarGeneric::OnPaint)
EVT_SIZE(wxStatusBarGeneric::OnSize)
EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged) EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
END_EVENT_TABLE() END_EVENT_TABLE()
@@ -51,7 +53,7 @@ END_EVENT_TABLE()
#define wxTHICK_LINE_BORDER 2 #define wxTHICK_LINE_BORDER 2
#define wxTHICK_LINE_WIDTH 1 #define wxTHICK_LINE_WIDTH 1
wxStatusBarGeneric::wxStatusBarGeneric() void wxStatusBarGeneric::Init()
{ {
m_borderX = wxTHICK_LINE_BORDER; m_borderX = wxTHICK_LINE_BORDER;
m_borderY = wxTHICK_LINE_BORDER; m_borderY = wxTHICK_LINE_BORDER;
@@ -59,9 +61,10 @@ wxStatusBarGeneric::wxStatusBarGeneric()
wxStatusBarGeneric::~wxStatusBarGeneric() wxStatusBarGeneric::~wxStatusBarGeneric()
{ {
# ifdef __WXMSW__ // VZ: what is this for? please comment...
SetFont(wxNullFont); #ifdef __WXMSW__
# endif // MSW SetFont(wxNullFont);
#endif // MSW
} }
bool wxStatusBarGeneric::Create(wxWindow *parent, bool wxStatusBarGeneric::Create(wxWindow *parent,
@@ -69,16 +72,10 @@ bool wxStatusBarGeneric::Create(wxWindow *parent,
long style, long style,
const wxString& name) const wxString& name)
{ {
// If create is ever meant to be re-entrant over the life of if ( !wxWindow::Create(parent, id,
// an object we should: wxDefaultPosition, wxDefaultSize,
// m_statusStrings.Empty(); style | wxTAB_TRAVERSAL, name) )
return FALSE;
m_borderX = wxTHICK_LINE_BORDER;
m_borderY = wxTHICK_LINE_BORDER;
bool success = wxWindow::Create(parent, id,
wxDefaultPosition, wxDefaultSize,
style | wxTAB_TRAVERSAL, name);
// The status bar should have a themed background // The status bar should have a themed background
SetThemeEnabled( TRUE ); SetThemeEnabled( TRUE );
@@ -102,24 +99,19 @@ bool wxStatusBarGeneric::Create(wxWindow *parent,
SetSize(-1, -1, -1, height); SetSize(-1, -1, -1, height);
return success; return TRUE;
} }
void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths) void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
{ {
wxASSERT_MSG( number >= 0, wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") );
_T("Yes, number should be a size_t and less than no fields is silly.") );
// if( number > m_nFields )
int i; int i;
for(i = m_nFields; i < number; ++i) for(i = m_nFields; i < number; ++i)
m_statusStrings.Add( wxEmptyString ); m_statusStrings.Add( wxEmptyString );
// if( number < m_nFields )
for (i = m_nFields - 1; i >= number; --i) for (i = m_nFields - 1; i >= number; --i)
m_statusStrings.Remove(i); m_statusStrings.RemoveAt(i);
m_nFields = number; m_nFields = number;
@@ -161,6 +153,9 @@ void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
// documented, but let's keep it for now // documented, but let's keep it for now
ReinitWidths(); ReinitWidths();
// forget the old cached pixel widths
m_widthsAbs.Empty();
if ( !widths_field ) if ( !widths_field )
{ {
// not an error, see the comment above // not an error, see the comment above
@@ -191,6 +186,7 @@ void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
for ( i = 0; i < m_nFields; i ++ ) for ( i = 0; i < m_nFields; i ++ )
DrawField(dc, i); DrawField(dc, i);
// VZ: again, what is this for?
#ifdef __WXMSW__ #ifdef __WXMSW__
dc.SetFont(wxNullFont); dc.SetFont(wxNullFont);
#endif // MSW #endif // MSW
@@ -273,73 +269,36 @@ void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
// Get the position and size of the field's internal bounding rectangle // Get the position and size of the field's internal bounding rectangle
bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
{ {
wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE, wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE,
_T("invalid status bar field index") ); _T("invalid status bar field index") );
int width, height; // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
int width, height;
#ifdef __WXPM__ #ifdef __WXPM__
GetSize(&width, &height); GetSize(&width, &height);
#else #else
GetClientSize(&width, &height); GetClientSize(&width, &height);
#endif #endif
int i; // we cache m_widthsAbs between calls normally but it's cleared when the
int sum_of_nonvar = 0; // status widths change so recompute it if needed
int num_of_var = 0; if ( m_widthsAbs.IsEmpty() )
bool do_same_width = FALSE;
int fieldWidth = 0;
int fieldPosition = 0;
if (m_statusWidths)
{
// if sum(not variable Windows) > c_width - (20 points per variable_window)
// then do_same_width = TRUE;
for (i = 0; i < m_nFields; i++)
{ {
if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i]; wxConstCast(this, wxStatusBarGeneric)->
else num_of_var++; m_widthsAbs = CalculateAbsWidths(width);
}
if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE;
}
else do_same_width = TRUE;
if (do_same_width)
{
for (i = 0; i < m_nFields; i++)
{
fieldWidth = (int)(width/m_nFields);
fieldPosition = i*fieldWidth;
if ( i == n )
break;
} }
}
else // no_same_width rect.x = 0;
{ for ( int i = 0; i < n; i++ )
int *tempwidth = new int[m_nFields];
int temppos = 0;
for (i = 0; i < m_nFields; i++)
{ {
if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i]; rect.x += m_widthsAbs[i];
else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
} }
for (i = 0; i < m_nFields; i++)
{
fieldWidth = tempwidth[i];
fieldPosition = temppos;
temppos += tempwidth[i]; rect.x += m_borderX;
rect.y = m_borderY;
if ( i == n ) rect.width = m_widthsAbs[n] - 2*m_borderX;
break; rect.height = height - 2*m_borderY;
}
delete [] tempwidth;
}
rect.x = fieldPosition + wxTHICK_LINE_BORDER;
rect.y = wxTHICK_LINE_BORDER;
rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
rect.height = height - 2 * wxTHICK_LINE_BORDER ;
return TRUE; return TRUE;
} }
@@ -400,6 +359,13 @@ void wxStatusBarGeneric::SetMinHeight(int height)
} }
} }
void wxStatusBarGeneric::OnSize(wxSizeEvent& event)
{
// have to recompute the widths in pixels
m_widthsAbs.Empty();
event.Skip();
}
#endif // wxUSE_STATUSBAR #endif // wxUSE_STATUSBAR
// vi:sts=4:sw=4:et