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()
62 m_colBeingReordered
= COL_NONE
;
67 bool wxHeaderCtrl::Create(wxWindow
*parent
,
74 if ( !wxHeaderCtrlBase::Create(parent
, id
, pos
, size
,
75 style
, wxDefaultValidator
, name
) )
78 // tell the system to not paint the background at all to avoid flicker as
79 // we paint the entire window area in our OnPaint()
80 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
85 wxHeaderCtrl::~wxHeaderCtrl()
89 // ----------------------------------------------------------------------------
90 // wxHeaderCtrl columns manipulation
91 // ----------------------------------------------------------------------------
93 void wxHeaderCtrl::DoSetCount(unsigned int count
)
95 // update the column indices order array before changing m_numColumns
96 DoResizeColumnIndices(m_colIndices
, count
);
100 InvalidateBestSize();
104 unsigned int wxHeaderCtrl::DoGetCount() const
109 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
111 InvalidateBestSize();
113 // we need to refresh not only this column but also the ones after it in
114 // case it was shown or hidden or its width changed -- it would be nice to
115 // avoid doing this unnecessary by storing the old column width (TODO)
116 RefreshColsAfter(idx
);
119 // ----------------------------------------------------------------------------
120 // wxHeaderCtrl scrolling
121 // ----------------------------------------------------------------------------
123 void wxHeaderCtrl::DoScrollHorz(int dx
)
125 m_scrollOffset
+= dx
;
127 // don't call our own version which calls this function!
128 wxControl::ScrollWindow(dx
, 0);
131 // ----------------------------------------------------------------------------
132 // wxHeaderCtrl geometry
133 // ----------------------------------------------------------------------------
135 wxSize
wxHeaderCtrl::DoGetBestSize() const
137 // the vertical size is rather arbitrary but it looks better if we leave
138 // some space around the text
139 const wxSize
size(IsEmpty() ? wxHeaderCtrlBase::DoGetBestSize().x
140 : GetColEnd(GetColumnCount() - 1),
141 (7*GetCharHeight())/4);
146 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
148 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
150 int pos
= m_scrollOffset
;
151 for ( unsigned n
= 0; ; n
++ )
153 const unsigned i
= m_colIndices
[n
];
157 const wxHeaderColumn
& col
= self
->GetColumn(i
);
159 pos
+= col
.GetWidth();
165 int wxHeaderCtrl::GetColEnd(unsigned int idx
) const
167 int x
= GetColStart(idx
);
169 return x
+ const_cast<wxHeaderCtrl
*>(this)->GetColumn(idx
).GetWidth();
172 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x
, bool *onSeparator
) const
174 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
177 const unsigned count
= GetColumnCount();
178 for ( unsigned n
= 0; n
< count
; n
++ )
180 const unsigned idx
= m_colIndices
[n
];
181 const wxHeaderColumn
& col
= self
->GetColumn(idx
);
182 if ( col
.IsHidden() )
185 pos
+= col
.GetWidth();
187 // if the column is resizeable, check if we're approximatively over the
188 // line separating it from the next column
190 // TODO: don't hardcode sensitivity
191 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
198 // inside this column?
202 *onSeparator
= false;
208 *onSeparator
= false;
212 // ----------------------------------------------------------------------------
213 // wxHeaderCtrl repainting
214 // ----------------------------------------------------------------------------
216 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
218 wxRect rect
= GetClientRect();
219 rect
.x
+= GetColStart(idx
);
220 rect
.width
= GetColumn(idx
).GetWidth();
225 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx
)
227 if ( idx
!= COL_NONE
)
231 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
233 wxRect rect
= GetClientRect();
234 const int ofs
= GetColStart(idx
);
241 // ----------------------------------------------------------------------------
242 // wxHeaderCtrl dragging/resizing/reordering
243 // ----------------------------------------------------------------------------
245 bool wxHeaderCtrl::IsResizing() const
247 return m_colBeingResized
!= COL_NONE
;
250 bool wxHeaderCtrl::IsReordering() const
252 return m_colBeingReordered
!= COL_NONE
;
255 void wxHeaderCtrl::ClearMarkers()
259 wxDCOverlay
dcover(m_overlay
, &dc
);
263 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical
)
267 wxDCOverlay
dcover(m_overlay
, &dc
);
270 // unfortunately drawing the marker over the parent window doesn't work as
271 // it's usually covered by another window (the main control view) so just
272 // draw the marker over the header itself, even if it makes it not very
274 dc
.SetPen(*wxLIGHT_GREY_PEN
);
275 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
278 void wxHeaderCtrl::EndDragging()
284 // don't use the special dragging cursor any more
285 SetCursor(wxNullCursor
);
288 void wxHeaderCtrl::CancelDragging()
290 wxASSERT_MSG( IsDragging(),
291 "shouldn't be called if we're not dragging anything" );
295 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
297 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, GetId());
298 event
.SetEventObject(this);
299 event
.SetColumn(col
);
301 GetEventHandler()->ProcessEvent(event
);
306 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
308 const int xStart
= GetColStart(col
);
310 // notice that GetMinWidth() returns 0 if there is no minimal width so it
311 // still makes sense to use it even in this case
312 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
314 if ( xPhysical
< xMinEnd
)
317 return xPhysical
- xStart
;
320 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
322 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
323 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
325 event
.SetEventObject(this);
326 event
.SetColumn(col
);
328 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
330 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
337 //else: nothing to do -- we just don't start to resize
339 else // go ahead with resizing
343 m_colBeingResized
= col
;
344 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
347 //else: we had already done the above when we started
349 UpdateResizingMarker(xPhysical
);
353 void wxHeaderCtrl::EndResizing(int xPhysical
)
355 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
361 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_RESIZE
, GetId());
362 event
.SetEventObject(this);
363 event
.SetColumn(m_colBeingResized
);
364 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
366 GetEventHandler()->ProcessEvent(event
);
368 m_colBeingResized
= COL_NONE
;
371 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
375 wxDCOverlay
dcover(m_overlay
, &dc
);
379 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
381 // draw the phantom position of the column being dragged
382 int x
= xPhysical
- m_dragOffset
;
383 int y
= GetClientSize().y
;
384 dc
.DrawRectangle(x
, 0,
385 GetColumn(m_colBeingReordered
).GetWidth(), y
);
387 // and also a hint indicating where it is going to be inserted if it's
389 unsigned int col
= FindColumnAtPoint(xPhysical
);
390 if ( col
!= COL_NONE
)
392 static const int DROP_MARKER_WIDTH
= 4;
394 dc
.SetBrush(*wxBLUE
);
395 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
396 DROP_MARKER_WIDTH
, y
);
400 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
402 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_BEGIN_REORDER
, GetId());
403 event
.SetEventObject(this);
404 event
.SetColumn(col
);
406 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
408 // don't start dragging it, nothing to do otherwise
412 m_dragOffset
= xPhysical
- GetColStart(col
);
414 m_colBeingReordered
= col
;
415 SetCursor(wxCursor(wxCURSOR_HAND
));
418 // do not call UpdateReorderingMarker() here: we don't want to give
419 // feedback for reordering until the user starts to really move the mouse
420 // as he might want to just click on the column and not move it at all
423 bool wxHeaderCtrl::EndReordering(int xPhysical
)
425 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
431 const int colOld
= m_colBeingReordered
,
432 colNew
= FindColumnAtPoint(xPhysical
);
434 m_colBeingReordered
= COL_NONE
;
436 if ( xPhysical
- GetColStart(colOld
) == m_dragOffset
)
439 if ( colNew
!= colOld
)
441 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_REORDER
, GetId());
442 event
.SetEventObject(this);
443 event
.SetColumn(colOld
);
445 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
446 event
.SetNewOrder(pos
);
448 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
450 // do reorder the columns
451 DoMoveCol(colOld
, pos
);
455 // whether we moved the column or not, the user did move the mouse and so
456 // did try to do it so return true
460 // ----------------------------------------------------------------------------
461 // wxHeaderCtrl column reordering
462 // ----------------------------------------------------------------------------
464 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
466 m_colIndices
= order
;
470 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
475 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
477 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
482 // ----------------------------------------------------------------------------
483 // wxHeaderCtrl event handlers
484 // ----------------------------------------------------------------------------
486 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
487 EVT_PAINT(wxHeaderCtrl::OnPaint
)
489 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
491 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
493 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
496 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
499 GetClientSize(&w
, &h
);
501 wxAutoBufferedPaintDC
dc(this);
503 dc
.SetBackground(GetBackgroundColour());
506 // account for the horizontal scrollbar offset in the parent window
507 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
509 const unsigned int count
= m_numColumns
;
511 for ( unsigned int i
= 0; i
< count
; i
++ )
513 const unsigned idx
= m_colIndices
[i
];
514 const wxHeaderColumn
& col
= GetColumn(idx
);
515 if ( col
.IsHidden() )
518 const int colWidth
= col
.GetWidth();
520 wxHeaderSortIconType sortArrow
;
521 if ( col
.IsSortKey() )
523 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
524 : wxHDR_SORT_ICON_DOWN
;
526 else // not sorting by this column
528 sortArrow
= wxHDR_SORT_ICON_NONE
;
534 if ( idx
== m_hover
)
535 state
= wxCONTROL_CURRENT
;
539 state
= wxCONTROL_DISABLED
;
542 wxHeaderButtonParams params
;
543 params
.m_labelText
= col
.GetTitle();
544 params
.m_labelBitmap
= col
.GetBitmap();
545 params
.m_labelAlignment
= col
.GetAlignment();
547 wxRendererNative::Get().DrawHeaderButton
551 wxRect(xpos
, 0, colWidth
, h
),
561 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
567 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
569 if ( event
.GetKeyCode() == WXK_ESCAPE
)
583 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
585 // do this in advance to allow simply returning if we're not interested,
586 // we'll undo it if we do handle the event below
590 // account for the control displacement
591 const int xPhysical
= mevent
.GetX();
592 const int xLogical
= xPhysical
- m_scrollOffset
;
594 // first deal with the [continuation of any] dragging operations in
598 if ( mevent
.LeftUp() )
599 EndResizing(xPhysical
);
600 else // update the live separator position
601 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
606 if ( IsReordering() )
608 if ( !mevent
.LeftUp() )
610 // update the column position
611 UpdateReorderingMarker(xPhysical
);
616 // finish reordering and continue to generate a click event below if we
617 // didn't really reorder anything
618 if ( EndReordering(xPhysical
) )
623 // find if the event is over a column at all
625 const unsigned col
= mevent
.Leaving()
626 ? (onSeparator
= false, COL_NONE
)
627 : FindColumnAtPoint(xLogical
, &onSeparator
);
630 // update the highlighted column if it changed
631 if ( col
!= m_hover
)
633 const unsigned hoverOld
= m_hover
;
636 RefreshColIfNotNone(hoverOld
);
637 RefreshColIfNotNone(m_hover
);
640 // update mouse cursor as it moves around
641 if ( mevent
.Moving() )
643 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
647 // all the other events only make sense when they happen over a column
648 if ( col
== COL_NONE
)
652 // enter various dragging modes on left mouse press
653 if ( mevent
.LeftDown() )
657 // start resizing the column
658 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
659 StartOrContinueResizing(col
, xPhysical
);
661 else // on column itself
663 // start dragging the column
664 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
666 StartReordering(col
, xPhysical
);
672 // determine the type of header event corresponding to click events
673 wxEventType evtType
= wxEVT_NULL
;
674 const bool click
= mevent
.ButtonUp(),
675 dblclk
= mevent
.ButtonDClick();
676 if ( click
|| dblclk
)
678 switch ( mevent
.GetButton() )
680 case wxMOUSE_BTN_LEFT
:
681 // treat left double clicks on separator specially
682 if ( onSeparator
&& dblclk
)
684 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
686 else // not double click on separator
688 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
689 : wxEVT_COMMAND_HEADER_DCLICK
;
693 case wxMOUSE_BTN_RIGHT
:
694 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
695 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
698 case wxMOUSE_BTN_MIDDLE
:
699 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
700 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
704 // ignore clicks from other mouse buttons
709 if ( evtType
== wxEVT_NULL
)
712 wxHeaderCtrlEvent
event(evtType
, GetId());
713 event
.SetEventObject(this);
714 event
.SetColumn(col
);
716 if ( GetEventHandler()->ProcessEvent(event
) )
720 #endif // wxHAS_GENERIC_HEADERCTRL