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 int wxHeaderCtrl::FindColumnAtPos(int x
, bool& onSeparator
) const
150 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
153 const unsigned count
= GetColumnCount();
154 for ( unsigned n
= 0; n
< count
; n
++ )
156 const wxHeaderColumnBase
& col
= self
->GetColumn(n
);
157 if ( col
.IsHidden() )
160 pos
+= col
.GetWidth();
162 // if the column is resizeable, check if we're approximatively over the
163 // line separating it from the next column
165 // TODO: don't hardcode sensitivity
166 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
172 // inside this column?
183 // ----------------------------------------------------------------------------
184 // wxHeaderCtrl repainting
185 // ----------------------------------------------------------------------------
187 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
189 wxRect rect
= GetClientRect();
190 rect
.x
+= GetColStart(idx
);
191 rect
.width
= GetColumn(idx
).GetWidth();
196 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
198 wxRect rect
= GetClientRect();
199 const int ofs
= GetColStart(idx
);
206 // ----------------------------------------------------------------------------
207 // wxHeaderCtrl event handlers
208 // ----------------------------------------------------------------------------
210 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxControl
)
211 EVT_PAINT(wxHeaderCtrl::OnPaint
)
213 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
216 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
219 GetClientSize(&w
, &h
);
221 wxAutoBufferedPaintDC
dc(this);
223 dc
.SetBackground(GetBackgroundColour());
226 // account for the horizontal scrollbar offset in the parent window
227 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
229 const unsigned int count
= m_numColumns
;
231 for ( unsigned int i
= 0; i
< count
; i
++ )
233 const wxHeaderColumnBase
& col
= GetColumn(i
);
234 if ( col
.IsHidden() )
237 const int colWidth
= col
.GetWidth();
239 wxHeaderSortIconType sortArrow
;
240 if ( col
.IsSortKey() )
242 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
243 : wxHDR_SORT_ICON_DOWN
;
245 else // not sorting by this column
247 sortArrow
= wxHDR_SORT_ICON_NONE
;
254 state
= wxCONTROL_CURRENT
;
258 state
= wxCONTROL_DISABLED
;
261 wxHeaderButtonParams params
;
262 params
.m_labelText
= col
.GetTitle();
263 params
.m_labelBitmap
= col
.GetBitmap();
264 params
.m_labelAlignment
= col
.GetAlignment();
266 wxRendererNative::Get().DrawHeaderButton
270 wxRect(xpos
, 0, colWidth
, h
),
280 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
284 // find if the event is over a column at all
286 const unsigned col
= FindColumnAtPos(mevent
.GetX(), onSeparator
);
287 if ( col
== COL_NONE
)
290 // update mouse cursor as it moves around
291 if ( mevent
.Moving() )
293 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
297 if ( mevent
.LeftDown() )
310 // determine the type of header event corresponding to this mouse event
312 const bool click
= mevent
.ButtonUp();
313 if ( click
|| mevent
.ButtonDClick() )
315 switch ( mevent
.GetButton() )
317 case wxMOUSE_BTN_LEFT
:
318 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
319 : wxEVT_COMMAND_HEADER_DCLICK
;
322 case wxMOUSE_BTN_RIGHT
:
323 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
324 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
327 case wxMOUSE_BTN_MIDDLE
:
328 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
329 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
333 // ignore clicks from other mouse buttons
337 wxHeaderCtrlEvent
event(evtType
, GetId());
338 event
.SetEventObject(this);
339 event
.SetColumn(col
);
341 if ( GetEventHandler()->ProcessEvent(event
) )
346 #endif // wxHAS_GENERIC_HEADERCTRL