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 NO_SORT
= (unsigned)-1;
44 const unsigned COL_NONE
= (unsigned)-1;
46 } // anonymous namespace
48 // ============================================================================
49 // wxHeaderCtrl implementation
50 // ============================================================================
52 // ----------------------------------------------------------------------------
53 // wxHeaderCtrl creation
54 // ----------------------------------------------------------------------------
56 void wxHeaderCtrl::Init()
61 m_colBeingReordered
= COL_NONE
;
66 bool wxHeaderCtrl::Create(wxWindow
*parent
,
73 if ( !wxHeaderCtrlBase::Create(parent
, id
, pos
, size
,
74 style
, wxDefaultValidator
, name
) )
77 // tell the system to not paint the background at all to avoid flicker as
78 // we paint the entire window area in our OnPaint()
79 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
84 wxHeaderCtrl::~wxHeaderCtrl()
88 // ----------------------------------------------------------------------------
89 // wxHeaderCtrl columns manipulation
90 // ----------------------------------------------------------------------------
92 void wxHeaderCtrl::DoSetCount(unsigned int count
)
94 // update the column indices order array before changing m_numColumns
95 DoResizeColumnIndices(m_colIndices
, count
);
103 unsigned int wxHeaderCtrl::DoGetCount() const
108 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
110 InvalidateBestSize();
112 // we need to refresh not only this column but also the ones after it in
113 // case it was shown or hidden or its width changed -- it would be nice to
114 // avoid doing this unnecessary by storing the old column width (TODO)
115 RefreshColsAfter(idx
);
118 // ----------------------------------------------------------------------------
119 // wxHeaderCtrl scrolling
120 // ----------------------------------------------------------------------------
122 void wxHeaderCtrl::DoScrollHorz(int dx
)
124 m_scrollOffset
+= dx
;
126 // don't call our own version which calls this function!
127 wxControl::ScrollWindow(dx
, 0);
130 // ----------------------------------------------------------------------------
131 // wxHeaderCtrl geometry
132 // ----------------------------------------------------------------------------
134 wxSize
wxHeaderCtrl::DoGetBestSize() const
136 wxWindow
*win
= GetParent();
137 int height
= wxRendererNative::Get().GetHeaderButtonHeight( win
);
139 // the vertical size is rather arbitrary but it looks better if we leave
140 // some space around the text
141 const wxSize
size(IsEmpty() ? wxHeaderCtrlBase::DoGetBestSize().x
142 : GetColEnd(GetColumnCount() - 1),
143 height
); // (7*GetCharHeight())/4);
148 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
150 int pos
= m_scrollOffset
;
151 for ( unsigned n
= 0; ; n
++ )
153 const unsigned i
= m_colIndices
[n
];
157 const wxHeaderColumn
& col
= GetColumn(i
);
159 pos
+= col
.GetWidth();
165 int wxHeaderCtrl::GetColEnd(unsigned int idx
) const
167 int x
= GetColStart(idx
);
169 return x
+ GetColumn(idx
).GetWidth();
172 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x
, bool *onSeparator
) const
175 const unsigned count
= GetColumnCount();
176 for ( unsigned n
= 0; n
< count
; n
++ )
178 const unsigned idx
= m_colIndices
[n
];
179 const wxHeaderColumn
& col
= GetColumn(idx
);
180 if ( col
.IsHidden() )
183 pos
+= col
.GetWidth();
185 // if the column is resizeable, check if we're approximatively over the
186 // line separating it from the next column
188 // TODO: don't hardcode sensitivity
189 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
196 // inside this column?
200 *onSeparator
= false;
206 *onSeparator
= false;
210 // ----------------------------------------------------------------------------
211 // wxHeaderCtrl repainting
212 // ----------------------------------------------------------------------------
214 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
216 wxRect rect
= GetClientRect();
217 rect
.x
+= GetColStart(idx
);
218 rect
.width
= GetColumn(idx
).GetWidth();
223 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx
)
225 if ( idx
!= COL_NONE
)
229 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
231 wxRect rect
= GetClientRect();
232 const int ofs
= GetColStart(idx
);
239 // ----------------------------------------------------------------------------
240 // wxHeaderCtrl dragging/resizing/reordering
241 // ----------------------------------------------------------------------------
243 bool wxHeaderCtrl::IsResizing() const
245 return m_colBeingResized
!= COL_NONE
;
248 bool wxHeaderCtrl::IsReordering() const
250 return m_colBeingReordered
!= COL_NONE
;
253 void wxHeaderCtrl::ClearMarkers()
257 wxDCOverlay
dcover(m_overlay
, &dc
);
261 void wxHeaderCtrl::EndDragging()
263 // We currently only use markers for reordering, not for resizing
270 // don't use the special dragging cursor any more
271 SetCursor(wxNullCursor
);
274 void wxHeaderCtrl::CancelDragging()
276 wxASSERT_MSG( IsDragging(),
277 "shouldn't be called if we're not dragging anything" );
281 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
283 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, GetId());
284 event
.SetEventObject(this);
285 event
.SetColumn(col
);
287 GetEventHandler()->ProcessEvent(event
);
292 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
294 const int xStart
= GetColStart(col
);
296 // notice that GetMinWidth() returns 0 if there is no minimal width so it
297 // still makes sense to use it even in this case
298 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
300 if ( xPhysical
< xMinEnd
)
303 return xPhysical
- xStart
;
306 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
308 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
309 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
311 event
.SetEventObject(this);
312 event
.SetColumn(col
);
314 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
316 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
323 //else: nothing to do -- we just don't start to resize
325 else // go ahead with resizing
329 m_colBeingResized
= col
;
330 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
333 //else: we had already done the above when we started
338 void wxHeaderCtrl::EndResizing(int xPhysical
)
340 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
346 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_RESIZE
, GetId());
347 event
.SetEventObject(this);
348 event
.SetColumn(m_colBeingResized
);
349 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
351 GetEventHandler()->ProcessEvent(event
);
353 m_colBeingResized
= COL_NONE
;
356 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
360 wxDCOverlay
dcover(m_overlay
, &dc
);
364 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
366 // draw the phantom position of the column being dragged
367 int x
= xPhysical
- m_dragOffset
;
368 int y
= GetClientSize().y
;
369 dc
.DrawRectangle(x
, 0,
370 GetColumn(m_colBeingReordered
).GetWidth(), y
);
372 // and also a hint indicating where it is going to be inserted if it's
374 unsigned int col
= FindColumnAtPoint(xPhysical
);
375 if ( col
!= COL_NONE
)
377 static const int DROP_MARKER_WIDTH
= 4;
379 dc
.SetBrush(*wxBLUE
);
380 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
381 DROP_MARKER_WIDTH
, y
);
385 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
387 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_BEGIN_REORDER
, GetId());
388 event
.SetEventObject(this);
389 event
.SetColumn(col
);
391 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
393 // don't start dragging it, nothing to do otherwise
397 m_dragOffset
= xPhysical
- GetColStart(col
);
399 m_colBeingReordered
= col
;
400 SetCursor(wxCursor(wxCURSOR_HAND
));
403 // do not call UpdateReorderingMarker() here: we don't want to give
404 // feedback for reordering until the user starts to really move the mouse
405 // as he might want to just click on the column and not move it at all
408 bool wxHeaderCtrl::EndReordering(int xPhysical
)
410 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
416 const int colOld
= m_colBeingReordered
,
417 colNew
= FindColumnAtPoint(xPhysical
);
419 m_colBeingReordered
= COL_NONE
;
421 if ( xPhysical
- GetColStart(colOld
) == m_dragOffset
)
424 if ( colNew
!= colOld
)
426 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_REORDER
, GetId());
427 event
.SetEventObject(this);
428 event
.SetColumn(colOld
);
430 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
431 event
.SetNewOrder(pos
);
433 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
435 // do reorder the columns
436 DoMoveCol(colOld
, pos
);
440 // whether we moved the column or not, the user did move the mouse and so
441 // did try to do it so return true
445 // ----------------------------------------------------------------------------
446 // wxHeaderCtrl column reordering
447 // ----------------------------------------------------------------------------
449 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
451 m_colIndices
= order
;
455 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
460 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
462 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
467 // ----------------------------------------------------------------------------
468 // wxHeaderCtrl event handlers
469 // ----------------------------------------------------------------------------
471 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
472 EVT_PAINT(wxHeaderCtrl::OnPaint
)
474 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
476 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
478 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
481 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
484 GetClientSize(&w
, &h
);
488 // GetVirtualSize(&vw, NULL);
491 wxAutoBufferedPaintDC
dc(this);
493 dc
.SetBackground(GetBackgroundColour());
496 // account for the horizontal scrollbar offset in the parent window
497 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
499 const unsigned int count
= m_numColumns
;
501 for ( unsigned int i
= 0; i
< count
; i
++ )
503 const unsigned idx
= m_colIndices
[i
];
504 const wxHeaderColumn
& col
= GetColumn(idx
);
505 if ( col
.IsHidden() )
508 int colWidth
= col
.GetWidth();
510 wxHeaderSortIconType sortArrow
;
511 if ( col
.IsSortKey() )
513 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
514 : wxHDR_SORT_ICON_DOWN
;
516 else // not sorting by this column
518 sortArrow
= wxHDR_SORT_ICON_NONE
;
524 if ( idx
== m_hover
)
525 state
= wxCONTROL_CURRENT
;
529 state
= wxCONTROL_DISABLED
;
533 state
|= wxCONTROL_SPECIAL
;
535 wxHeaderButtonParams params
;
536 params
.m_labelText
= col
.GetTitle();
537 params
.m_labelBitmap
= col
.GetBitmap();
538 params
.m_labelAlignment
= col
.GetAlignment();
543 // colWidth = wxMax( colWidth, vw - xpos );
544 state
|= wxCONTROL_DIRTY
;
548 wxRendererNative::Get().DrawHeaderButton
552 wxRect(xpos
, 0, colWidth
, h
),
562 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
568 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
570 if ( event
.GetKeyCode() == WXK_ESCAPE
)
584 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
586 // do this in advance to allow simply returning if we're not interested,
587 // we'll undo it if we do handle the event below
591 // account for the control displacement
592 const int xPhysical
= mevent
.GetX();
593 const int xLogical
= xPhysical
- m_scrollOffset
;
595 // first deal with the [continuation of any] dragging operations in
599 if ( mevent
.LeftUp() )
600 EndResizing(xPhysical
);
601 else // update the live separator position
602 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
607 if ( IsReordering() )
609 if ( !mevent
.LeftUp() )
611 // update the column position
612 UpdateReorderingMarker(xPhysical
);
617 // finish reordering and continue to generate a click event below if we
618 // didn't really reorder anything
619 if ( EndReordering(xPhysical
) )
624 // find if the event is over a column at all
626 const unsigned col
= mevent
.Leaving()
627 ? (onSeparator
= false, COL_NONE
)
628 : FindColumnAtPoint(xLogical
, &onSeparator
);
631 // update the highlighted column if it changed
632 if ( col
!= m_hover
)
634 const unsigned hoverOld
= m_hover
;
637 RefreshColIfNotNone(hoverOld
);
638 RefreshColIfNotNone(m_hover
);
641 // update mouse cursor as it moves around
642 if ( mevent
.Moving() )
644 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
648 // all the other events only make sense when they happen over a column
649 if ( col
== COL_NONE
)
653 // enter various dragging modes on left mouse press
654 if ( mevent
.LeftDown() )
658 // start resizing the column
659 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
660 StartOrContinueResizing(col
, xPhysical
);
662 else // on column itself
664 // start dragging the column
665 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
667 StartReordering(col
, xPhysical
);
673 // determine the type of header event corresponding to click events
674 wxEventType evtType
= wxEVT_NULL
;
675 const bool click
= mevent
.ButtonUp(),
676 dblclk
= mevent
.ButtonDClick();
677 if ( click
|| dblclk
)
679 switch ( mevent
.GetButton() )
681 case wxMOUSE_BTN_LEFT
:
682 // treat left double clicks on separator specially
683 if ( onSeparator
&& dblclk
)
685 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
687 else // not double click on separator
689 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
690 : wxEVT_COMMAND_HEADER_DCLICK
;
694 case wxMOUSE_BTN_RIGHT
:
695 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
696 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
699 case wxMOUSE_BTN_MIDDLE
:
700 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
701 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
705 // ignore clicks from other mouse buttons
710 if ( evtType
== wxEVT_NULL
)
713 wxHeaderCtrlEvent
event(evtType
, GetId());
714 event
.SetEventObject(this);
715 event
.SetColumn(col
);
717 if ( GetEventHandler()->ProcessEvent(event
) )
721 #endif // wxHAS_GENERIC_HEADERCTRL
723 #endif // wxUSE_HEADERCTRL