1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/headerctrlg.cpp
3 // Purpose: generic wxHeaderCtrl implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/headerctrl.h"
31 #ifdef wxHAS_GENERIC_HEADERCTRL
33 #include "wx/dcbuffer.h"
34 #include "wx/renderer.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
43 const unsigned NO_SORT
= (unsigned)-1;
45 const unsigned COL_NONE
= (unsigned)-1;
47 } // anonymous namespace
49 // ============================================================================
50 // wxHeaderCtrl implementation
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // wxHeaderCtrl creation
55 // ----------------------------------------------------------------------------
57 void wxHeaderCtrl::Init()
64 bool wxHeaderCtrl::Create(wxWindow
*parent
,
71 if ( !wxHeaderCtrlBase::Create(parent
, id
, pos
, size
,
72 style
, wxDefaultValidator
, name
) )
75 // tell the system to not paint the background at all to avoid flicker as
76 // we paint the entire window area in our OnPaint()
77 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
82 wxHeaderCtrl::~wxHeaderCtrl()
86 // ----------------------------------------------------------------------------
87 // wxHeaderCtrl columns manipulation
88 // ----------------------------------------------------------------------------
90 void wxHeaderCtrl::DoSetCount(unsigned int count
)
97 unsigned int wxHeaderCtrl::DoGetCount() const
102 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
104 // we need to refresh not only this column but also the ones after it in
105 // case it was shown or hidden or its width changed -- it would be nice to
106 // avoid doing this unnecessary by storing the old column width (TODO)
107 RefreshColsAfter(idx
);
110 // ----------------------------------------------------------------------------
111 // wxHeaderCtrl scrolling
112 // ----------------------------------------------------------------------------
114 void wxHeaderCtrl::DoScrollHorz(int dx
)
116 m_scrollOffset
+= dx
;
118 // don't call our own version which calls this function!
119 wxControl::ScrollWindow(dx
, 0);
122 // ----------------------------------------------------------------------------
123 // wxHeaderCtrl geometry
124 // ----------------------------------------------------------------------------
126 wxSize
wxHeaderCtrl::DoGetBestSize() const
128 // the vertical size is rather arbitrary but it looks better if we leave
129 // some space around the text
130 return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4);
133 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
135 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
138 for ( unsigned n
= 0; n
< idx
; n
++ )
140 const wxHeaderColumnBase
& col
= self
->GetColumn(n
);
142 pos
+= col
.GetWidth();
148 // ----------------------------------------------------------------------------
149 // wxHeaderCtrl repainting
150 // ----------------------------------------------------------------------------
152 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
154 wxRect rect
= GetClientRect();
155 rect
.x
+= GetColStart(idx
);
156 rect
.width
= GetColumn(idx
).GetWidth();
161 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
163 wxRect rect
= GetClientRect();
164 const int ofs
= GetColStart(idx
);
171 // ----------------------------------------------------------------------------
172 // wxHeaderCtrl event handlers
173 // ----------------------------------------------------------------------------
175 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxControl
)
176 EVT_PAINT(wxHeaderCtrl::OnPaint
)
178 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
181 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
184 GetClientSize(&w
, &h
);
186 wxAutoBufferedPaintDC
dc(this);
188 dc
.SetBackground(GetBackgroundColour());
191 // account for the horizontal scrollbar offset in the parent window
192 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
194 const unsigned int count
= m_numColumns
;
196 for ( unsigned int i
= 0; i
< count
; i
++ )
198 const wxHeaderColumnBase
& col
= GetColumn(i
);
199 if ( col
.IsHidden() )
202 const int colWidth
= col
.GetWidth();
204 wxHeaderSortIconType sortArrow
;
205 if ( col
.IsSortKey() )
207 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
208 : wxHDR_SORT_ICON_DOWN
;
210 else // not sorting by this column
212 sortArrow
= wxHDR_SORT_ICON_NONE
;
219 state
= wxCONTROL_CURRENT
;
223 state
= wxCONTROL_DISABLED
;
226 wxHeaderButtonParams params
;
227 params
.m_labelText
= col
.GetTitle();
228 params
.m_labelBitmap
= col
.GetBitmap();
229 params
.m_labelAlignment
= col
.GetAlignment();
231 wxRendererNative::Get().DrawHeaderButton
235 wxRect(xpos
, 0, colWidth
, h
),
245 void wxHeaderCtrl::OnMouse(wxMouseEvent
& event
)
250 #endif // wxHAS_GENERIC_HEADERCTRL