From c97c99522358ed5791528a38ec2a727d079d2483 Mon Sep 17 00:00:00 2001
From: Robin Dunn <robin@alldunn.com>
Date: Thu, 30 Nov 2006 23:44:55 +0000
Subject: [PATCH] Return optimal label width from DrawHeaderButton

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43726 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/renderer.tex  |  9 ++++++---
 include/wx/renderer.h       | 15 ++++++++-------
 include/wx/univ/renderer.h  |  4 ++--
 src/generic/renderg.cpp     | 23 +++++++++++++++--------
 src/gtk/renderer.cpp        |  6 +++---
 src/mac/carbon/renderer.cpp |  6 +++---
 src/msw/renderer.cpp        |  9 ++++-----
 wxPython/src/_renderer.i    | 28 ++++++++++++++--------------
 8 files changed, 55 insertions(+), 45 deletions(-)

diff --git a/docs/latex/wx/renderer.tex b/docs/latex/wx/renderer.tex
index 6b1f12b56d..d0fa0b0ec9 100644
--- a/docs/latex/wx/renderer.tex
+++ b/docs/latex/wx/renderer.tex
@@ -131,16 +131,19 @@ rectangle of a drop down button which arrow matches the size you need.
 
 \membersection{wxRendererNative::DrawHeaderButton}\label{wxrenderernativedrawheaderbutton}
 
-\func{void}{DrawHeaderButton}{\param{wxWindow* }{win}, \param{wxDC\& }{dc}, \param{const wxRect\& }{rect}, \param{int }{flags = 0}, \param{wxHeaderSortIconType }{sortArrow = wxHDR\_SORT\_ICON\_NONE}, \param{wxHeaderButtonParams* }{params = NULL}}
+\func{int}{DrawHeaderButton}{\param{wxWindow* }{win}, \param{wxDC\& }{dc}, \param{const wxRect\& }{rect}, \param{int }{flags = 0}, \param{wxHeaderSortIconType }{sortArrow = wxHDR\_SORT\_ICON\_NONE}, \param{wxHeaderButtonParams* }{params = NULL}}
 
 Draw the header control button (used, for example, by
 \helpref{wxListCtrl}{wxlistctrl}).  Depending on platforms the
 \arg{flags} parameter may support the \texttt{wxCONTROL\_SELECTED}
 \texttt{wxCONTROL\_DISABLED} and \texttt{wxCONTROL\_CURRENT} bits.
 The \arg{sortArrow} parameter can be one of
-\texttt{wxHDR\_SORT\_ICON\_NONE}, \texttt{wxHDR\_SORT\_ICON\_UP}, or 
+\texttt{wxHDR\_SORT\_ICON\_NONE}, \texttt{wxHDR\_SORT\_ICON\_UP}, or
 \texttt{wxHDR\_SORT\_ICON\_DOWN}.  Additional values controlling the
-drawing of a text or bitmap label can be passed in \arg{params}.
+drawing of a text or bitmap label can be passed in \arg{params}.  The
+value returned is the optimal width to contain the the unabreviated
+label text or bitmap, the sort arrow if present, and internal margins.
+
 
 
 \membersection{wxRendererNative::DrawItemSelectionRect}\label{wxrenderernativedrawitemselectionrect}
diff --git a/include/wx/renderer.h b/include/wx/renderer.h
index 9345586fea..a270f0848e 100644
--- a/include/wx/renderer.h
+++ b/include/wx/renderer.h
@@ -157,8 +157,9 @@ public:
     // drawing functions
     // -----------------
 
-    // draw the header control button (used by wxListCtrl)
-    virtual void DrawHeaderButton(wxWindow *win,
+    // draw the header control button (used by wxListCtrl) Returns optimal
+    // width for the label contents.
+    virtual int  DrawHeaderButton(wxWindow *win,
                                   wxDC& dc,
                                   const wxRect& rect,
                                   int flags = 0,
@@ -168,7 +169,7 @@ public:
 
     // Draw the contents of a header control button (label, sort arrows, etc.)
     // Normally only called by DrawHeaderButton.
-    virtual void DrawHeaderButtonContents(wxWindow *win,
+    virtual int  DrawHeaderButtonContents(wxWindow *win,
                                           wxDC& dc,
                                           const wxRect& rect,
                                           int flags = 0,
@@ -308,21 +309,21 @@ public:
         : m_rendererNative(rendererNative) { }
 
 
-    virtual void DrawHeaderButton(wxWindow *win,
+    virtual int  DrawHeaderButton(wxWindow *win,
                                   wxDC& dc,
                                   const wxRect& rect,
                                   int flags = 0,
                                   wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
                                   wxHeaderButtonParams* params = NULL)
-        { m_rendererNative.DrawHeaderButton(win, dc, rect, flags, sortArrow, params); }
+        { return m_rendererNative.DrawHeaderButton(win, dc, rect, flags, sortArrow, params); }
 
-    virtual void DrawHeaderButtonContents(wxWindow *win,
+    virtual int  DrawHeaderButtonContents(wxWindow *win,
                                           wxDC& dc,
                                           const wxRect& rect,
                                           int flags = 0,
                                           wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
                                           wxHeaderButtonParams* params = NULL)
-        { m_rendererNative.DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params); }
+        { return m_rendererNative.DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params); }
 
     virtual int GetHeaderButtonHeight(wxWindow *win)
         { return m_rendererNative.GetHeaderButtonHeight(win); }
diff --git a/include/wx/univ/renderer.h b/include/wx/univ/renderer.h
index 446d11fd1e..9d66b616fe 100644
--- a/include/wx/univ/renderer.h
+++ b/include/wx/univ/renderer.h
@@ -847,13 +847,13 @@ public:
                              int flags) const
         { return m_renderer->HitTestFrame(rect, pt, flags); }
 
-    virtual void DrawHeaderButton(wxWindow *win,
+    virtual int  DrawHeaderButton(wxWindow *win,
                                   wxDC& dc,
                                   const wxRect& rect,
                                   int flags = 0,
                                   wxHeaderSortIconType sortIcon = wxHDR_SORT_ICON_NONE,
                                   wxHeaderButtonParams* params = NULL)
-        { m_renderer->DrawHeaderButton(win, dc, rect, flags, sortIcon, params); }
+        { return m_renderer->DrawHeaderButton(win, dc, rect, flags, sortIcon, params); }
     virtual void DrawTreeItemButton(wxWindow *win,
                                     wxDC& dc,
                                     const wxRect& rect,
diff --git a/src/generic/renderg.cpp b/src/generic/renderg.cpp
index 38713efef0..2991a11d95 100644
--- a/src/generic/renderg.cpp
+++ b/src/generic/renderg.cpp
@@ -46,14 +46,14 @@ class WXDLLEXPORT wxRendererGeneric : public wxRendererNative
 public:
     wxRendererGeneric();
 
-    virtual void DrawHeaderButton(wxWindow *win,
+    virtual int  DrawHeaderButton(wxWindow *win,
                                   wxDC& dc,
                                   const wxRect& rect,
                                   int flags = 0,
                                   wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
                                   wxHeaderButtonParams* params = NULL);
 
-    virtual void DrawHeaderButtonContents(wxWindow *win,
+    virtual int  DrawHeaderButtonContents(wxWindow *win,
                                           wxDC& dc,
                                           const wxRect& rect,
                                           int flags = 0,
@@ -204,7 +204,7 @@ wxRendererGeneric::DrawShadedRect(wxDC& dc,
 // tree/list ctrl drawing
 // ----------------------------------------------------------------------------
 
-void
+int
 wxRendererGeneric::DrawHeaderButton(wxWindow* win,
                                     wxDC& dc,
                                     const wxRect& rect,
@@ -239,11 +239,11 @@ wxRendererGeneric::DrawHeaderButton(wxWindow* win,
     dc.DrawLine( x, y+h-1, x+1, y+h-1 );
     dc.DrawLine( x+w-1, y, x+w-1, y+1 );
 
-    DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
+    return DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
 }
 
 
-void
+int
 wxRendererGeneric::DrawHeaderButtonContents(wxWindow *win,
                                             wxDC& dc,
                                             const wxRect& rect,
@@ -251,6 +251,8 @@ wxRendererGeneric::DrawHeaderButtonContents(wxWindow *win,
                                             wxHeaderSortIconType sortArrow,
                                             wxHeaderButtonParams* params)
 {
+    int labelWidth = 0;
+    
     // Mark this item as selected.  For the generic version we'll just draw an
     // underline
     if ( flags & wxCONTROL_SELECTED )
@@ -306,14 +308,17 @@ wxRendererGeneric::DrawHeaderButtonContents(wxWindow *win,
         dc.SetBrush(wxBrush(c));
         dc.DrawPolygon( 3, triPt, ar.x, ar.y);                  
     }
-
+    labelWidth += arrowSpace;
+    
     const int margin = 5;   // number of pixels to reserve on either side of the label
     int bmpWidth = 0;
     int txtEnd = 0;
     
     if ( params && params->m_labelBitmap.Ok() )
         bmpWidth = params->m_labelBitmap.GetWidth() + 2;
-        
+
+    labelWidth += bmpWidth + 2*margin;
+    
     // Draw a label if one is given
     if ( params && !params->m_labelText.empty() )
     {
@@ -330,8 +335,9 @@ wxRendererGeneric::DrawHeaderButtonContents(wxWindow *win,
 
         int tw, th, td, x, y;
         dc.GetTextExtent( label, &tw, &th, &td);
+        labelWidth += tw;
         y = rect.y + wxMax(0, (rect.height - (th+td)) / 2);
-
+        
         // truncate and add an ellipsis (...) if the text is too wide.
         int targetWidth = rect.width - arrowSpace - bmpWidth - 2*margin;
         if ( tw > targetWidth )        
@@ -397,6 +403,7 @@ wxRendererGeneric::DrawHeaderButtonContents(wxWindow *win,
         }
         dc.DrawBitmap(params->m_labelBitmap, x, y, true);
     }
+    return labelWidth;
 }
 
 
diff --git a/src/gtk/renderer.cpp b/src/gtk/renderer.cpp
index e968ace5e1..86ebfa8b4a 100644
--- a/src/gtk/renderer.cpp
+++ b/src/gtk/renderer.cpp
@@ -43,7 +43,7 @@ class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative
 {
 public:
     // draw the header control button (used by wxListCtrl)
-    virtual void DrawHeaderButton(wxWindow *win,
+    virtual int  DrawHeaderButton(wxWindow *win,
                                   wxDC& dc,
                                   const wxRect& rect,
                                   int flags = 0,
@@ -181,7 +181,7 @@ wxRendererGTK::GetTreeWidget()
 // list/tree controls drawing
 // ----------------------------------------------------------------------------
 
-void
+int
 wxRendererGTK::DrawHeaderButton(wxWindow *win,
                                 wxDC& dc,
                                 const wxRect& rect,
@@ -212,7 +212,7 @@ wxRendererGTK::DrawHeaderButton(wxWindow *win,
         dc.LogicalToDeviceX(rect.x) - x_diff, rect.y, rect.width, rect.height
     );
 
-    DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
+    return DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
 }
 
 // draw a ">" or "v" button
diff --git a/src/mac/carbon/renderer.cpp b/src/mac/carbon/renderer.cpp
index bf271a27bb..15d9ef20b1 100644
--- a/src/mac/carbon/renderer.cpp
+++ b/src/mac/carbon/renderer.cpp
@@ -34,7 +34,7 @@ class WXDLLEXPORT wxRendererMac : public wxDelegateRendererNative
 {
 public:
     // draw the header control button (used by wxListCtrl)
-    virtual void DrawHeaderButton( wxWindow *win,
+    virtual int DrawHeaderButton( wxWindow *win,
         wxDC& dc,
         const wxRect& rect,
         int flags = 0,
@@ -97,7 +97,7 @@ wxRendererNative& wxRendererNative::GetDefault()
     return s_rendererMac;
 }
 
-void wxRendererMac::DrawHeaderButton( wxWindow *win,
+int wxRendererMac::DrawHeaderButton( wxWindow *win,
     wxDC& dc,
     const wxRect& rect,
     int flags,
@@ -200,7 +200,7 @@ void wxRendererMac::DrawHeaderButton( wxWindow *win,
     }
     flags &= ~wxCONTROL_SELECTED;
 
-    DrawHeaderButtonContents(win, dc, newRect, flags, sortArrow, params);
+    return DrawHeaderButtonContents(win, dc, newRect, flags, sortArrow, params);
 }
 
 
diff --git a/src/msw/renderer.cpp b/src/msw/renderer.cpp
index e63c25ad8d..27c7cb7e6c 100644
--- a/src/msw/renderer.cpp
+++ b/src/msw/renderer.cpp
@@ -118,7 +118,7 @@ public:
 
     static wxRendererNative& Get();
 
-    virtual void DrawHeaderButton(wxWindow *win,
+    virtual int DrawHeaderButton(wxWindow *win,
                                   wxDC& dc,
                                   const wxRect& rect,
                                   int flags = 0,
@@ -290,7 +290,7 @@ wxRendererXP::DrawComboBoxDropButton(wxWindow * win,
 
 }
 
-void
+int
 wxRendererXP::DrawHeaderButton(wxWindow *win,
                                wxDC& dc,
                                const wxRect& rect,
@@ -301,8 +301,7 @@ wxRendererXP::DrawHeaderButton(wxWindow *win,
     wxUxThemeHandle hTheme(win, L"HEADER");
     if ( !hTheme )
     {
-        m_rendererNative.DrawHeaderButton(win, dc, rect, flags, sortArrow, params);
-        return;
+        return m_rendererNative.DrawHeaderButton(win, dc, rect, flags, sortArrow, params);
     }
 
     RECT r;
@@ -330,7 +329,7 @@ wxRendererXP::DrawHeaderButton(wxWindow *win,
     // and then clear those flags before calling DrawHeaderButtonContents.
     
     // Add any extras that are specified in flags and params
-    DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
+    return DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
 }
 
 
diff --git a/wxPython/src/_renderer.i b/wxPython/src/_renderer.i
index 85c9e57361..c7f100b84f 100644
--- a/wxPython/src/_renderer.i
+++ b/wxPython/src/_renderer.i
@@ -156,23 +156,23 @@ public:
 
      
     DocDeclStr(
-        virtual void , DrawHeaderButton(wxWindow *win,
-                                        wxDC& dc,
-                                        const wxRect& rect,
-                                        int flags = 0,
-                                        wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
-                                        wxHeaderButtonParams* params=NULL),
-        "Draw the header control button (such as what is used by `wx.ListCtrl`
-in report mode.)", "");
+        virtual int , DrawHeaderButton(wxWindow *win,
+                                       wxDC& dc,
+                                       const wxRect& rect,
+                                       int flags = 0,
+                                       wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
+                                       wxHeaderButtonParams* params=NULL),
+        "Draw a header control button (such as what is used by `wx.ListCtrl` in report
+mode.)  The optimal size of the label (text and icons) is returned.", "");
     
 
     DocDeclStr(
-        virtual void , DrawHeaderButtonContents(wxWindow *win,
-                                                wxDC& dc,
-                                                const wxRect& rect,
-                                                int flags = 0,
-                                                wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
-                                                wxHeaderButtonParams* params=NULL),
+        virtual int , DrawHeaderButtonContents(wxWindow *win,
+                                               wxDC& dc,
+                                               const wxRect& rect,
+                                               int flags = 0,
+                                               wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
+                                               wxHeaderButtonParams* params=NULL),
         "Draw the contents of a header control button, (label, sort
 arrows, etc.)  Normally this is only called by `DrawHeaderButton`.", "");
 
-- 
2.50.0

