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()
63 bool wxHeaderCtrl::Create(wxWindow
*parent
,
70 if ( !wxHeaderCtrlBase::Create(parent
, id
, pos
, size
,
71 style
, wxDefaultValidator
, name
) )
74 // tell the system to not paint the background at all to avoid flicker as
75 // we paint the entire window area in our OnPaint()
76 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
81 wxHeaderCtrl::~wxHeaderCtrl()
85 // ----------------------------------------------------------------------------
86 // wxHeaderCtrl columns manipulation
87 // ----------------------------------------------------------------------------
89 unsigned int wxHeaderCtrl::DoGetCount() const
94 void wxHeaderCtrl::DoInsert(const wxHeaderColumn
& col
, unsigned int idx
)
96 m_cols
.insert(m_cols
.begin() + idx
, col
);
97 m_sortOrders
.insert(m_sortOrders
.begin() + idx
, -1);
99 if ( m_cols
[idx
].IsShown() )
100 RefreshColsAfter(idx
);
103 void wxHeaderCtrl::DoDelete(unsigned int idx
)
105 m_cols
.erase(m_cols
.begin() + idx
);
106 m_sortOrders
.erase(m_sortOrders
.begin() + idx
);
108 RefreshColsAfter(idx
);
111 void wxHeaderCtrl::DoShowColumn(unsigned int idx
, bool show
)
113 if ( show
!= m_cols
[idx
].IsShown() )
115 m_cols
[idx
].SetHidden(!show
);
117 RefreshColsAfter(idx
);
121 void wxHeaderCtrl::DoShowSortIndicator(unsigned int idx
, int sortOrder
)
123 if ( sortOrder
!= m_sortOrders
[idx
] )
125 m_sortOrders
[idx
] = sortOrder
;
131 // ----------------------------------------------------------------------------
132 // wxHeaderCtrl scrolling
133 // ----------------------------------------------------------------------------
135 void wxHeaderCtrl::DoScrollHorz(int dx
)
137 m_scrollOffset
+= dx
;
139 // don't call our own version which calls this function!
140 wxControl::ScrollWindow(dx
, 0);
143 // ----------------------------------------------------------------------------
144 // wxHeaderCtrl geometry
145 // ----------------------------------------------------------------------------
147 wxSize
wxHeaderCtrl::DoGetBestSize() const
149 // the vertical size is rather arbitrary but it looks better if we leave
150 // some space around the text
151 return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4);
154 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
157 for ( unsigned n
= 0; n
< idx
; n
++ )
159 const wxHeaderColumn
& col
= m_cols
[n
];
161 pos
+= col
.GetWidth();
167 // ----------------------------------------------------------------------------
168 // wxHeaderCtrl repainting
169 // ----------------------------------------------------------------------------
171 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
173 wxRect rect
= GetClientRect();
174 rect
.x
+= GetColStart(idx
);
175 rect
.width
= m_cols
[idx
].GetWidth();
180 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
182 wxRect rect
= GetClientRect();
183 const int ofs
= GetColStart(idx
);
190 // ----------------------------------------------------------------------------
191 // wxHeaderCtrl event handlers
192 // ----------------------------------------------------------------------------
194 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxControl
)
195 EVT_PAINT(wxHeaderCtrl::OnPaint
)
197 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
200 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
203 GetClientSize(&w
, &h
);
205 wxAutoBufferedPaintDC
dc(this);
207 dc
.SetBackground(GetBackgroundColour());
210 // account for the horizontal scrollbar offset in the parent window
211 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
213 const unsigned int count
= m_cols
.size();
215 for ( unsigned int i
= 0; i
< count
; i
++ )
217 const wxHeaderColumn
& col
= m_cols
[i
];
218 if ( col
.IsHidden() )
221 const int colWidth
= col
.GetWidth();
223 wxHeaderSortIconType sortArrow
;
224 switch ( m_sortOrders
[i
] )
227 wxFAIL_MSG( "wrong sort order value" );
231 sortArrow
= wxHDR_SORT_ICON_NONE
;
235 sortArrow
= wxHDR_SORT_ICON_DOWN
;
239 sortArrow
= wxHDR_SORT_ICON_UP
;
247 state
= wxCONTROL_CURRENT
;
251 state
= wxCONTROL_DISABLED
;
254 wxHeaderButtonParams params
;
255 params
.m_labelText
= col
.GetTitle();
256 params
.m_labelBitmap
= col
.GetBitmap();
257 params
.m_labelAlignment
= col
.GetAlignment();
259 wxRendererNative::Get().DrawHeaderButton
263 wxRect(xpos
, 0, colWidth
, h
),
273 void wxHeaderCtrl::OnMouse(wxMouseEvent
& event
)
278 #endif // wxHAS_GENERIC_HEADERCTRL