From 390015c0ee28d25b0b7e23c42a52733adcac334f Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Wed, 4 Sep 2002 13:16:46 +0000
Subject: [PATCH] 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
---
 docs/latex/wx/statusbr.tex    |  71 +------------------
 include/wx/generic/statusbr.h |  23 +++++--
 src/generic/statusbr.cpp      | 124 ++++++++++++----------------------
 3 files changed, 64 insertions(+), 154 deletions(-)

diff --git a/docs/latex/wx/statusbr.tex b/docs/latex/wx/statusbr.tex
index 10000e952b..2ebf4e4d97 100644
--- a/docs/latex/wx/statusbr.tex
+++ b/docs/latex/wx/statusbr.tex
@@ -148,72 +148,6 @@ strings.
 Saves the current field text in a per field stack, and sets the field text
 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}
 
 \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{widths}{An array of {\it n} integers, each of which is a status field width
-in pixels. A value of -1 indicates that the field is variable width; at least one
-field must be -1.}
+\docparam{widths}{An array of {\it n} integers interpreted in the same way as
+in \helpref{SetStatusWidths}{wxstatusbarsetstatuswidths}}
 
 \membersection{wxStatusBar::SetMinHeight}\label{wxstatusbarsetminheight}
 
diff --git a/include/wx/generic/statusbr.h b/include/wx/generic/statusbr.h
index 9145f7dbaf..fc08061abf 100644
--- a/include/wx/generic/statusbr.h
+++ b/include/wx/generic/statusbr.h
@@ -25,7 +25,7 @@ WXDLLEXPORT_DATA(extern const wxChar*) wxPanelNameStr;
 class WXDLLEXPORT wxStatusBarGeneric : public wxStatusBarBase
 {
 public:
-  wxStatusBarGeneric();
+  wxStatusBarGeneric() { Init(); }
   wxStatusBarGeneric(wxWindow *parent,
               wxWindowID id,
               const wxPoint& pos = wxDefaultPosition,
@@ -33,17 +33,21 @@ public:
               long style = 0,
               const wxString& name = wxPanelNameStr)
   {
+      Init();
+
       Create(parent, id, pos, size, style, name);
   }
   wxStatusBarGeneric(wxWindow *parent,
-              wxWindowID id,
-              long style,
-              const wxString& name = wxPanelNameStr)
+                     wxWindowID id,
+                     long style,
+                     const wxString& name = wxPanelNameStr)
   {
+      Init();
+
       Create(parent, id, style, name);
   }
 
-  ~wxStatusBarGeneric();
+  virtual ~wxStatusBarGeneric();
 
   bool Create(wxWindow *parent, wxWindowID id,
               const wxPoint& WXUNUSED(pos) = wxDefaultPosition,
@@ -61,7 +65,6 @@ public:
   // Create status line
   virtual void SetFieldsCount(int number = 1,
                               const int *widths = (const int *) NULL);
-  int GetFieldsCount() const { return m_nFields; }
 
   // Set status line text
   virtual void SetStatusText(const wxString& text, int number = 0);
@@ -89,6 +92,7 @@ public:
   void SetBorderY(int y);
 
   void OnPaint(wxPaintEvent& event);
+  void OnSize(wxSizeEvent& event);
 
   virtual void InitColours();
 
@@ -96,7 +100,14 @@ public:
   void OnSysColourChanged(wxSysColourChangedEvent& event);
 
 protected:
+  // common part of all ctors
+  void Init();
+
   wxArrayString     m_statusStrings;
+
+  // the widths of the status bar panes in pixels
+  wxArrayInt        m_widthsAbs;
+
   int               m_borderX;
   int               m_borderY;
   wxFont            m_defaultStatusBarFont;
diff --git a/src/generic/statusbr.cpp b/src/generic/statusbr.cpp
index 24f273375d..c5128995c8 100644
--- a/src/generic/statusbr.cpp
+++ b/src/generic/statusbr.cpp
@@ -44,6 +44,8 @@
 
 BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
     EVT_PAINT(wxStatusBarGeneric::OnPaint)
+    EVT_SIZE(wxStatusBarGeneric::OnSize)
+
     EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
 END_EVENT_TABLE()
 
@@ -51,7 +53,7 @@ END_EVENT_TABLE()
 #define         wxTHICK_LINE_BORDER 2
 #define         wxTHICK_LINE_WIDTH  1
 
-wxStatusBarGeneric::wxStatusBarGeneric()
+void wxStatusBarGeneric::Init()
 {
   m_borderX = wxTHICK_LINE_BORDER;
   m_borderY = wxTHICK_LINE_BORDER;
@@ -59,9 +61,10 @@ wxStatusBarGeneric::wxStatusBarGeneric()
 
 wxStatusBarGeneric::~wxStatusBarGeneric()
 {
-#   ifdef __WXMSW__
-        SetFont(wxNullFont);
-#   endif // MSW
+    // VZ: what is this for? please comment...
+#ifdef __WXMSW__
+    SetFont(wxNullFont);
+#endif // MSW
 }
 
 bool wxStatusBarGeneric::Create(wxWindow *parent,
@@ -69,16 +72,10 @@ bool wxStatusBarGeneric::Create(wxWindow *parent,
                                 long style,
                                 const wxString& name)
 {
-    // If create is ever meant to be re-entrant over the life of
-    // an object we should:
-    // m_statusStrings.Empty();
-
-  m_borderX = wxTHICK_LINE_BORDER;
-  m_borderY = wxTHICK_LINE_BORDER;
-
-  bool success = wxWindow::Create(parent, id,
-                                  wxDefaultPosition, wxDefaultSize,
-                                  style | wxTAB_TRAVERSAL, name);
+  if ( !wxWindow::Create(parent, id,
+                         wxDefaultPosition, wxDefaultSize,
+                         style | wxTAB_TRAVERSAL, name) )
+      return FALSE;
 
   // The status bar should have a themed background
   SetThemeEnabled( TRUE );
@@ -102,24 +99,19 @@ bool wxStatusBarGeneric::Create(wxWindow *parent,
 
   SetSize(-1, -1, -1, height);
 
-  return success;
+  return TRUE;
 }
 
 void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
 {
-    wxASSERT_MSG( number >= 0,
-                  _T("Yes, number should be a size_t and less than no fields is silly.") );
-
-    // if( number > m_nFields )
+    wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") );
 
     int i;
     for(i = m_nFields; i < number; ++i)
         m_statusStrings.Add( wxEmptyString );
 
-    // if( number < m_nFields )
-
     for (i = m_nFields - 1; i >= number; --i)
-        m_statusStrings.Remove(i);
+        m_statusStrings.RemoveAt(i);
 
     m_nFields = number;
 
@@ -161,6 +153,9 @@ void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
     //      documented, but let's keep it for now
     ReinitWidths();
 
+    // forget the old cached pixel widths
+    m_widthsAbs.Empty();
+
     if ( !widths_field )
     {
         // not an error, see the comment above
@@ -191,6 +186,7 @@ void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
   for ( i = 0; i < m_nFields; i ++ )
     DrawField(dc, i);
 
+  // VZ: again, what is this for?
 #ifdef __WXMSW__
   dc.SetFont(wxNullFont);
 #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
 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
 {
-  wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE,
-               _T("invalid status bar field index") );
+    wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE,
+                 _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__
-  GetSize(&width, &height);
+    GetSize(&width, &height);
 #else
-  GetClientSize(&width, &height);
+    GetClientSize(&width, &height);
 #endif
 
-  int i;
-  int sum_of_nonvar = 0;
-  int num_of_var = 0;
-  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];
-        else num_of_var++;
-     }
-     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
-  {
-    int *tempwidth = new int[m_nFields];
-    int temppos = 0;
-    for (i = 0; i < m_nFields; i++)
+    // we cache m_widthsAbs between calls normally but it's cleared when the
+    // status widths change so recompute it if needed
+    if ( m_widthsAbs.IsEmpty() )
     {
-      if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i];
-      else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
+        wxConstCast(this, wxStatusBarGeneric)->
+            m_widthsAbs = CalculateAbsWidths(width);
     }
-    for (i = 0; i < m_nFields; i++)
-    {
-        fieldWidth = tempwidth[i];
-        fieldPosition = temppos;
-
-          temppos += tempwidth[i];
 
-        if ( i == n )
-            break;
+    rect.x = 0;
+    for ( int i = 0; i < n; i++ )
+    {
+        rect.x += m_widthsAbs[i];
     }
-    delete [] tempwidth;
-  }
 
-    rect.x = fieldPosition + wxTHICK_LINE_BORDER;
-    rect.y = wxTHICK_LINE_BORDER;
+    rect.x += m_borderX;
+    rect.y = m_borderY;
 
-    rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
-    rect.height = height - 2 * wxTHICK_LINE_BORDER ;
+    rect.width = m_widthsAbs[n] - 2*m_borderX;
+    rect.height = height - 2*m_borderY;
 
     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
 
-// vi:sts=4:sw=4:et
-- 
2.45.2

