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()
61 m_colBeingResized
= COL_NONE
;
65 bool wxHeaderCtrl::Create(wxWindow
*parent
,
72 if ( !wxHeaderCtrlBase::Create(parent
, id
, pos
, size
,
73 style
, wxDefaultValidator
, name
) )
76 // tell the system to not paint the background at all to avoid flicker as
77 // we paint the entire window area in our OnPaint()
78 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
83 wxHeaderCtrl::~wxHeaderCtrl()
87 // ----------------------------------------------------------------------------
88 // wxHeaderCtrl columns manipulation
89 // ----------------------------------------------------------------------------
91 void wxHeaderCtrl::DoSetCount(unsigned int count
)
94 m_colIndices
.resize(count
);
99 unsigned int wxHeaderCtrl::DoGetCount() const
104 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
106 // we need to refresh not only this column but also the ones after it in
107 // case it was shown or hidden or its width changed -- it would be nice to
108 // avoid doing this unnecessary by storing the old column width (TODO)
109 RefreshColsAfter(idx
);
112 // ----------------------------------------------------------------------------
113 // wxHeaderCtrl scrolling
114 // ----------------------------------------------------------------------------
116 void wxHeaderCtrl::DoScrollHorz(int dx
)
118 m_scrollOffset
+= dx
;
120 // don't call our own version which calls this function!
121 wxControl::ScrollWindow(dx
, 0);
124 // ----------------------------------------------------------------------------
125 // wxHeaderCtrl geometry
126 // ----------------------------------------------------------------------------
128 wxSize
wxHeaderCtrl::DoGetBestSize() const
130 // the vertical size is rather arbitrary but it looks better if we leave
131 // some space around the text
132 return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4);
135 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
137 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
139 int pos
= m_scrollOffset
;
140 for ( unsigned n
= 0; n
< idx
; n
++ )
142 const wxHeaderColumnBase
& col
= self
->GetColumn(n
);
144 pos
+= col
.GetWidth();
150 int wxHeaderCtrl::FindColumnAtPos(int x
, bool& onSeparator
) const
152 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
155 const unsigned count
= GetColumnCount();
156 for ( unsigned n
= 0; n
< count
; n
++ )
158 const wxHeaderColumnBase
& col
= self
->GetColumn(n
);
159 if ( col
.IsHidden() )
162 pos
+= col
.GetWidth();
164 // if the column is resizeable, check if we're approximatively over the
165 // line separating it from the next column
167 // TODO: don't hardcode sensitivity
168 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
174 // inside this column?
185 // ----------------------------------------------------------------------------
186 // wxHeaderCtrl repainting
187 // ----------------------------------------------------------------------------
189 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
191 wxRect rect
= GetClientRect();
192 rect
.x
+= GetColStart(idx
);
193 rect
.width
= GetColumn(idx
).GetWidth();
198 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx
)
200 if ( idx
!= COL_NONE
)
204 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
206 wxRect rect
= GetClientRect();
207 const int ofs
= GetColStart(idx
);
214 // ----------------------------------------------------------------------------
215 // wxHeaderCtrl dragging
216 // ----------------------------------------------------------------------------
218 bool wxHeaderCtrl::IsResizing() const
220 return m_colBeingResized
!= COL_NONE
;
223 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical
)
225 // unfortunately drawing the marker over the parent window doesn't work as
226 // it's usually covered by another window (the main control view) so just
227 // draw the marker over the header itself, even if it makes it not very
231 wxDCOverlay
dcover(m_overlay
, &dc
);
234 if ( xPhysical
!= -1 )
236 dc
.SetPen(*wxLIGHT_GREY_PEN
);
237 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
241 void wxHeaderCtrl::EndDragging()
243 UpdateResizingMarker(-1);
247 // don't use the special dragging cursor any more
248 SetCursor(wxNullCursor
);
251 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
253 const int xStart
= GetColStart(col
);
255 // notice that GetMinWidth() returns 0 if there is no minimal width so it
256 // still makes sense to use it even in this case
257 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
259 if ( xPhysical
< xMinEnd
)
262 return xPhysical
- xStart
;
265 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
267 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
268 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
270 event
.SetEventObject(this);
271 event
.SetColumn(col
);
273 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
275 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
282 //else: nothing to do -- we just don't start to resize
284 else // go ahead with resizing
288 m_colBeingResized
= col
;
289 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
292 //else: we had already done the above when we started
294 UpdateResizingMarker(xPhysical
);
298 void wxHeaderCtrl::EndResizing(int xPhysical
)
300 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
304 const bool cancelled
= xPhysical
== -1;
306 // if dragging was cancelled we must have already lost the mouse capture so
307 // don't try to release it
311 wxHeaderCtrlEvent
event(cancelled
? wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
312 : wxEVT_COMMAND_HEADER_END_RESIZE
,
314 event
.SetEventObject(this);
315 event
.SetColumn(m_colBeingResized
);
317 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
319 GetEventHandler()->ProcessEvent(event
);
321 m_colBeingResized
= COL_NONE
;
324 // ----------------------------------------------------------------------------
325 // wxHeaderCtrl column reordering
326 // ----------------------------------------------------------------------------
328 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
330 m_colIndices
= order
;
334 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
339 // ----------------------------------------------------------------------------
340 // wxHeaderCtrl event handlers
341 // ----------------------------------------------------------------------------
343 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
344 EVT_PAINT(wxHeaderCtrl::OnPaint
)
346 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
348 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
350 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
353 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
356 GetClientSize(&w
, &h
);
358 wxAutoBufferedPaintDC
dc(this);
360 dc
.SetBackground(GetBackgroundColour());
363 // account for the horizontal scrollbar offset in the parent window
364 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
366 const unsigned int count
= m_numColumns
;
368 for ( unsigned int i
= 0; i
< count
; i
++ )
370 const wxHeaderColumnBase
& col
= GetColumn(i
);
371 if ( col
.IsHidden() )
374 const int colWidth
= col
.GetWidth();
376 wxHeaderSortIconType sortArrow
;
377 if ( col
.IsSortKey() )
379 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
380 : wxHDR_SORT_ICON_DOWN
;
382 else // not sorting by this column
384 sortArrow
= wxHDR_SORT_ICON_NONE
;
391 state
= wxCONTROL_CURRENT
;
395 state
= wxCONTROL_DISABLED
;
398 wxHeaderButtonParams params
;
399 params
.m_labelText
= col
.GetTitle();
400 params
.m_labelBitmap
= col
.GetBitmap();
401 params
.m_labelAlignment
= col
.GetAlignment();
403 wxRendererNative::Get().DrawHeaderButton
407 wxRect(xpos
, 0, colWidth
, h
),
417 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
423 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
425 if ( IsResizing() && event
.GetKeyCode() == WXK_ESCAPE
)
436 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
438 // do this in advance to allow simply returning if we're not interested,
439 // we'll undo it if we do handle the event below
443 // account for the control displacement
444 const int xPhysical
= mevent
.GetX();
445 const int xLogical
= xPhysical
- m_scrollOffset
;
447 // first deal with the [continuation of any] dragging operations in
451 if ( mevent
.LeftUp() )
452 EndResizing(xPhysical
);
453 else // update the live separator position
454 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
460 // find if the event is over a column at all
462 const unsigned col
= mevent
.Leaving()
463 ? (onSeparator
= false, COL_NONE
)
464 : FindColumnAtPos(xLogical
, onSeparator
);
467 // update the highlighted column if it changed
468 if ( col
!= m_hover
)
470 const unsigned hoverOld
= m_hover
;
473 RefreshColIfNotNone(hoverOld
);
474 RefreshColIfNotNone(m_hover
);
477 // update mouse cursor as it moves around
478 if ( mevent
.Moving() )
480 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
484 // all the other events only make sense when they happen over a column
485 if ( col
== COL_NONE
)
489 // enter various dragging modes on left mouse press
490 if ( mevent
.LeftDown() )
494 // start resizing the column
495 wxASSERT_MSG( !IsResizing(), "reentering resize mode?" );
496 StartOrContinueResizing(col
, xPhysical
);
498 else // on column itself
507 // determine the type of header event corresponding to click events
508 wxEventType evtType
= wxEVT_NULL
;
509 const bool click
= mevent
.ButtonUp(),
510 dblclk
= mevent
.ButtonDClick();
511 if ( click
|| dblclk
)
513 switch ( mevent
.GetButton() )
515 case wxMOUSE_BTN_LEFT
:
516 // treat left double clicks on separator specially
517 if ( onSeparator
&& dblclk
)
519 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
521 else // not double click on separator
523 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
524 : wxEVT_COMMAND_HEADER_DCLICK
;
528 case wxMOUSE_BTN_RIGHT
:
529 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
530 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
533 case wxMOUSE_BTN_MIDDLE
:
534 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
535 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
539 // ignore clicks from other mouse buttons
544 if ( evtType
== wxEVT_NULL
)
547 wxHeaderCtrlEvent
event(evtType
, GetId());
548 event
.SetEventObject(this);
549 event
.SetColumn(col
);
551 if ( GetEventHandler()->ProcessEvent(event
) )
555 #endif // wxHAS_GENERIC_HEADERCTRL