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"
28 #include "wx/headerctrl.h"
30 #ifdef wxHAS_GENERIC_HEADERCTRL
32 #include "wx/dcbuffer.h"
33 #include "wx/renderer.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
42 const unsigned COL_NONE
= (unsigned)-1;
44 } // anonymous namespace
46 // ============================================================================
47 // wxHeaderCtrl implementation
48 // ============================================================================
50 // ----------------------------------------------------------------------------
51 // wxHeaderCtrl creation
52 // ----------------------------------------------------------------------------
54 void wxHeaderCtrl::Init()
59 m_colBeingReordered
= COL_NONE
;
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
)
92 // update the column indices order array before changing m_numColumns
93 DoResizeColumnIndices(m_colIndices
, count
);
101 unsigned int wxHeaderCtrl::DoGetCount() const
106 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
108 InvalidateBestSize();
110 // we need to refresh not only this column but also the ones after it in
111 // case it was shown or hidden or its width changed -- it would be nice to
112 // avoid doing this unnecessary by storing the old column width (TODO)
113 RefreshColsAfter(idx
);
116 // ----------------------------------------------------------------------------
117 // wxHeaderCtrl scrolling
118 // ----------------------------------------------------------------------------
120 void wxHeaderCtrl::DoScrollHorz(int dx
)
122 m_scrollOffset
+= dx
;
124 // don't call our own version which calls this function!
125 wxControl::ScrollWindow(dx
, 0);
128 // ----------------------------------------------------------------------------
129 // wxHeaderCtrl geometry
130 // ----------------------------------------------------------------------------
132 wxSize
wxHeaderCtrl::DoGetBestSize() const
134 wxWindow
*win
= GetParent();
135 int height
= wxRendererNative::Get().GetHeaderButtonHeight( win
);
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 height
); // (7*GetCharHeight())/4);
146 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
148 int pos
= m_scrollOffset
;
149 for ( unsigned n
= 0; ; n
++ )
151 const unsigned i
= m_colIndices
[n
];
155 const wxHeaderColumn
& col
= GetColumn(i
);
157 pos
+= col
.GetWidth();
163 int wxHeaderCtrl::GetColEnd(unsigned int idx
) const
165 int x
= GetColStart(idx
);
167 return x
+ GetColumn(idx
).GetWidth();
170 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x
, bool *onSeparator
) const
173 const unsigned count
= GetColumnCount();
174 for ( unsigned n
= 0; n
< count
; n
++ )
176 const unsigned idx
= m_colIndices
[n
];
177 const wxHeaderColumn
& col
= GetColumn(idx
);
178 if ( col
.IsHidden() )
181 pos
+= col
.GetWidth();
183 // if the column is resizable, check if we're approximatively over the
184 // line separating it from the next column
186 // TODO: don't hardcode sensitivity
187 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
194 // inside this column?
198 *onSeparator
= false;
204 *onSeparator
= false;
208 // ----------------------------------------------------------------------------
209 // wxHeaderCtrl repainting
210 // ----------------------------------------------------------------------------
212 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
214 wxRect rect
= GetClientRect();
215 rect
.x
+= GetColStart(idx
);
216 rect
.width
= GetColumn(idx
).GetWidth();
221 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx
)
223 if ( idx
!= COL_NONE
)
227 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
229 wxRect rect
= GetClientRect();
230 const int ofs
= GetColStart(idx
);
237 // ----------------------------------------------------------------------------
238 // wxHeaderCtrl dragging/resizing/reordering
239 // ----------------------------------------------------------------------------
241 bool wxHeaderCtrl::IsResizing() const
243 return m_colBeingResized
!= COL_NONE
;
246 bool wxHeaderCtrl::IsReordering() const
248 return m_colBeingReordered
!= COL_NONE
;
251 void wxHeaderCtrl::ClearMarkers()
255 wxDCOverlay
dcover(m_overlay
, &dc
);
259 void wxHeaderCtrl::EndDragging()
261 // We currently only use markers for reordering, not for resizing
268 // don't use the special dragging cursor any more
269 SetCursor(wxNullCursor
);
272 void wxHeaderCtrl::CancelDragging()
274 wxASSERT_MSG( IsDragging(),
275 "shouldn't be called if we're not dragging anything" );
279 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
281 wxHeaderCtrlEvent
event(wxEVT_HEADER_DRAGGING_CANCELLED
, GetId());
282 event
.SetEventObject(this);
283 event
.SetColumn(col
);
285 GetEventHandler()->ProcessEvent(event
);
290 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
292 const int xStart
= GetColStart(col
);
294 // notice that GetMinWidth() returns 0 if there is no minimal width so it
295 // still makes sense to use it even in this case
296 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
298 if ( xPhysical
< xMinEnd
)
301 return xPhysical
- xStart
;
304 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
306 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_HEADER_RESIZING
307 : wxEVT_HEADER_BEGIN_RESIZE
,
309 event
.SetEventObject(this);
310 event
.SetColumn(col
);
312 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
314 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
321 //else: nothing to do -- we just don't start to resize
323 else // go ahead with resizing
327 m_colBeingResized
= col
;
328 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
331 //else: we had already done the above when we started
336 void wxHeaderCtrl::EndResizing(int xPhysical
)
338 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
344 wxHeaderCtrlEvent
event(wxEVT_HEADER_END_RESIZE
, GetId());
345 event
.SetEventObject(this);
346 event
.SetColumn(m_colBeingResized
);
347 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
349 GetEventHandler()->ProcessEvent(event
);
351 m_colBeingResized
= COL_NONE
;
354 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
358 wxDCOverlay
dcover(m_overlay
, &dc
);
362 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
364 // draw the phantom position of the column being dragged
365 int x
= xPhysical
- m_dragOffset
;
366 int y
= GetClientSize().y
;
367 dc
.DrawRectangle(x
, 0,
368 GetColumn(m_colBeingReordered
).GetWidth(), y
);
370 // and also a hint indicating where it is going to be inserted if it's
372 unsigned int col
= FindColumnAtPoint(xPhysical
);
373 if ( col
!= COL_NONE
)
375 static const int DROP_MARKER_WIDTH
= 4;
377 dc
.SetBrush(*wxBLUE
);
378 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
379 DROP_MARKER_WIDTH
, y
);
383 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
385 wxHeaderCtrlEvent
event(wxEVT_HEADER_BEGIN_REORDER
, GetId());
386 event
.SetEventObject(this);
387 event
.SetColumn(col
);
389 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
391 // don't start dragging it, nothing to do otherwise
395 m_dragOffset
= xPhysical
- GetColStart(col
);
397 m_colBeingReordered
= col
;
398 SetCursor(wxCursor(wxCURSOR_HAND
));
401 // do not call UpdateReorderingMarker() here: we don't want to give
402 // feedback for reordering until the user starts to really move the mouse
403 // as he might want to just click on the column and not move it at all
406 bool wxHeaderCtrl::EndReordering(int xPhysical
)
408 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
414 const int colOld
= m_colBeingReordered
,
415 colNew
= FindColumnAtPoint(xPhysical
);
417 m_colBeingReordered
= COL_NONE
;
419 if ( xPhysical
- GetColStart(colOld
) == m_dragOffset
)
422 if ( colNew
!= colOld
)
424 wxHeaderCtrlEvent
event(wxEVT_HEADER_END_REORDER
, GetId());
425 event
.SetEventObject(this);
426 event
.SetColumn(colOld
);
428 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
429 event
.SetNewOrder(pos
);
431 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
433 // do reorder the columns
434 DoMoveCol(colOld
, pos
);
438 // whether we moved the column or not, the user did move the mouse and so
439 // did try to do it so return true
443 // ----------------------------------------------------------------------------
444 // wxHeaderCtrl column reordering
445 // ----------------------------------------------------------------------------
447 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
449 m_colIndices
= order
;
453 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
458 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
460 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
465 // ----------------------------------------------------------------------------
466 // wxHeaderCtrl event handlers
467 // ----------------------------------------------------------------------------
469 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
470 EVT_PAINT(wxHeaderCtrl::OnPaint
)
472 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
474 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
476 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
479 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
482 GetClientSize(&w
, &h
);
486 // GetVirtualSize(&vw, NULL);
489 wxAutoBufferedPaintDC
dc(this);
491 dc
.SetBackground(GetBackgroundColour());
494 // account for the horizontal scrollbar offset in the parent window
495 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
497 const unsigned int count
= m_numColumns
;
499 for ( unsigned int i
= 0; i
< count
; i
++ )
501 const unsigned idx
= m_colIndices
[i
];
502 const wxHeaderColumn
& col
= GetColumn(idx
);
503 if ( col
.IsHidden() )
506 int colWidth
= col
.GetWidth();
508 wxHeaderSortIconType sortArrow
;
509 if ( col
.IsSortKey() )
511 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
512 : wxHDR_SORT_ICON_DOWN
;
514 else // not sorting by this column
516 sortArrow
= wxHDR_SORT_ICON_NONE
;
522 if ( idx
== m_hover
)
523 state
= wxCONTROL_CURRENT
;
527 state
= wxCONTROL_DISABLED
;
531 state
|= wxCONTROL_SPECIAL
;
533 wxHeaderButtonParams params
;
534 params
.m_labelText
= col
.GetTitle();
535 params
.m_labelBitmap
= col
.GetBitmap();
536 params
.m_labelAlignment
= col
.GetAlignment();
541 // colWidth = wxMax( colWidth, vw - xpos );
542 state
|= wxCONTROL_DIRTY
;
546 wxRendererNative::Get().DrawHeaderButton
550 wxRect(xpos
, 0, colWidth
, h
),
560 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
566 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
568 if ( event
.GetKeyCode() == WXK_ESCAPE
)
582 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
584 // do this in advance to allow simply returning if we're not interested,
585 // we'll undo it if we do handle the event below
589 // account for the control displacement
590 const int xPhysical
= mevent
.GetX();
591 const int xLogical
= xPhysical
- m_scrollOffset
;
593 // first deal with the [continuation of any] dragging operations in
597 if ( mevent
.LeftUp() )
598 EndResizing(xPhysical
);
599 else // update the live separator position
600 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
605 if ( IsReordering() )
607 if ( !mevent
.LeftUp() )
609 // update the column position
610 UpdateReorderingMarker(xPhysical
);
615 // finish reordering and continue to generate a click event below if we
616 // didn't really reorder anything
617 if ( EndReordering(xPhysical
) )
622 // find if the event is over a column at all
624 const unsigned col
= mevent
.Leaving()
625 ? (onSeparator
= false, COL_NONE
)
626 : FindColumnAtPoint(xLogical
, &onSeparator
);
629 // update the highlighted column if it changed
630 if ( col
!= m_hover
)
632 const unsigned hoverOld
= m_hover
;
635 RefreshColIfNotNone(hoverOld
);
636 RefreshColIfNotNone(m_hover
);
639 // update mouse cursor as it moves around
640 if ( mevent
.Moving() )
642 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
646 // all the other events only make sense when they happen over a column
647 if ( col
== COL_NONE
)
651 // enter various dragging modes on left mouse press
652 if ( mevent
.LeftDown() )
656 // start resizing the column
657 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
658 StartOrContinueResizing(col
, xPhysical
);
660 else // on column itself
662 // start dragging the column
663 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
665 StartReordering(col
, xPhysical
);
671 // determine the type of header event corresponding to click events
672 wxEventType evtType
= wxEVT_NULL
;
673 const bool click
= mevent
.ButtonUp(),
674 dblclk
= mevent
.ButtonDClick();
675 if ( click
|| dblclk
)
677 switch ( mevent
.GetButton() )
679 case wxMOUSE_BTN_LEFT
:
680 // treat left double clicks on separator specially
681 if ( onSeparator
&& dblclk
)
683 evtType
= wxEVT_HEADER_SEPARATOR_DCLICK
;
685 else // not double click on separator
687 evtType
= click
? wxEVT_HEADER_CLICK
688 : wxEVT_HEADER_DCLICK
;
692 case wxMOUSE_BTN_RIGHT
:
693 evtType
= click
? wxEVT_HEADER_RIGHT_CLICK
694 : wxEVT_HEADER_RIGHT_DCLICK
;
697 case wxMOUSE_BTN_MIDDLE
:
698 evtType
= click
? wxEVT_HEADER_MIDDLE_CLICK
699 : wxEVT_HEADER_MIDDLE_DCLICK
;
703 // ignore clicks from other mouse buttons
708 if ( evtType
== wxEVT_NULL
)
711 wxHeaderCtrlEvent
event(evtType
, GetId());
712 event
.SetEventObject(this);
713 event
.SetColumn(col
);
715 if ( GetEventHandler()->ProcessEvent(event
) )
719 #endif // wxHAS_GENERIC_HEADERCTRL
721 #endif // wxUSE_HEADERCTRL