+// ----------------------------------------------------------------------------
+// wxHeaderCtrl dragging/resizing/reordering
+// ----------------------------------------------------------------------------
+
+bool wxHeaderCtrl::IsResizing() const
+{
+ return m_colBeingResized != COL_NONE;
+}
+
+bool wxHeaderCtrl::IsReordering() const
+{
+ return m_colBeingReordered != COL_NONE;
+}
+
+void wxHeaderCtrl::ClearMarkers()
+{
+ wxClientDC dc(this);
+
+ wxDCOverlay dcover(m_overlay, &dc);
+ dcover.Clear();
+}
+
+void wxHeaderCtrl::EndDragging()
+{
+ // We currently only use markers for reordering, not for resizing
+ if (IsReordering())
+ {
+ ClearMarkers();
+ m_overlay.Reset();
+ }
+
+ // don't use the special dragging cursor any more
+ SetCursor(wxNullCursor);
+}
+
+void wxHeaderCtrl::CancelDragging()
+{
+ wxASSERT_MSG( IsDragging(),
+ "shouldn't be called if we're not dragging anything" );
+
+ EndDragging();
+
+ unsigned int& col = IsResizing() ? m_colBeingResized : m_colBeingReordered;
+
+ wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED, GetId());
+ event.SetEventObject(this);
+ event.SetColumn(col);
+
+ GetEventHandler()->ProcessEvent(event);
+
+ col = COL_NONE;
+}
+
+int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col, int& xPhysical)
+{
+ const int xStart = GetColStart(col);
+
+ // notice that GetMinWidth() returns 0 if there is no minimal width so it
+ // still makes sense to use it even in this case
+ const int xMinEnd = xStart + GetColumn(col).GetMinWidth();
+
+ if ( xPhysical < xMinEnd )
+ xPhysical = xMinEnd;
+
+ return xPhysical - xStart;
+}
+
+void wxHeaderCtrl::StartOrContinueResizing(unsigned int col, int xPhysical)
+{
+ wxHeaderCtrlEvent event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
+ : wxEVT_COMMAND_HEADER_BEGIN_RESIZE,
+ GetId());
+ event.SetEventObject(this);
+ event.SetColumn(col);
+
+ event.SetWidth(ConstrainByMinWidth(col, xPhysical));
+
+ if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
+ {
+ if ( IsResizing() )
+ {
+ ReleaseMouse();
+ CancelDragging();
+ }
+ //else: nothing to do -- we just don't start to resize
+ }
+ else // go ahead with resizing
+ {
+ if ( !IsResizing() )
+ {
+ m_colBeingResized = col;
+ SetCursor(wxCursor(wxCURSOR_SIZEWE));
+ CaptureMouse();
+ }
+ //else: we had already done the above when we started
+
+ }
+}
+
+void wxHeaderCtrl::EndResizing(int xPhysical)
+{
+ wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
+
+ EndDragging();
+
+ ReleaseMouse();
+
+ wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_RESIZE, GetId());
+ event.SetEventObject(this);
+ event.SetColumn(m_colBeingResized);
+ event.SetWidth(ConstrainByMinWidth(m_colBeingResized, xPhysical));
+
+ GetEventHandler()->ProcessEvent(event);
+
+ m_colBeingResized = COL_NONE;
+}
+
+void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical)
+{
+ wxClientDC dc(this);
+
+ wxDCOverlay dcover(m_overlay, &dc);
+ dcover.Clear();
+
+ dc.SetPen(*wxBLUE);
+ dc.SetBrush(*wxTRANSPARENT_BRUSH);
+
+ // draw the phantom position of the column being dragged
+ int x = xPhysical - m_dragOffset;
+ int y = GetClientSize().y;
+ dc.DrawRectangle(x, 0,
+ GetColumn(m_colBeingReordered).GetWidth(), y);
+
+ // and also a hint indicating where it is going to be inserted if it's
+ // dropped now
+ unsigned int col = FindColumnAtPoint(xPhysical);
+ if ( col != COL_NONE )
+ {
+ static const int DROP_MARKER_WIDTH = 4;
+
+ dc.SetBrush(*wxBLUE);
+ dc.DrawRectangle(GetColEnd(col) - DROP_MARKER_WIDTH/2, 0,
+ DROP_MARKER_WIDTH, y);
+ }
+}
+
+void wxHeaderCtrl::StartReordering(unsigned int col, int xPhysical)
+{
+ wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_BEGIN_REORDER, GetId());
+ event.SetEventObject(this);
+ event.SetColumn(col);
+
+ if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
+ {
+ // don't start dragging it, nothing to do otherwise
+ return;
+ }
+
+ m_dragOffset = xPhysical - GetColStart(col);
+
+ m_colBeingReordered = col;
+ SetCursor(wxCursor(wxCURSOR_HAND));
+ CaptureMouse();
+
+ // do not call UpdateReorderingMarker() here: we don't want to give
+ // feedback for reordering until the user starts to really move the mouse
+ // as he might want to just click on the column and not move it at all
+}
+
+bool wxHeaderCtrl::EndReordering(int xPhysical)
+{
+ wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
+
+ EndDragging();
+
+ ReleaseMouse();
+
+ const int colOld = m_colBeingReordered,
+ colNew = FindColumnAtPoint(xPhysical);
+
+ m_colBeingReordered = COL_NONE;
+
+ if ( xPhysical - GetColStart(colOld) == m_dragOffset )
+ return false;
+
+ if ( colNew != colOld )
+ {
+ wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_REORDER, GetId());
+ event.SetEventObject(this);
+ event.SetColumn(colOld);
+
+ const unsigned pos = GetColumnPos(FindColumnAtPoint(xPhysical));
+ event.SetNewOrder(pos);
+
+ if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
+ {
+ // do reorder the columns
+ DoMoveCol(colOld, pos);
+ }
+ }
+
+ // whether we moved the column or not, the user did move the mouse and so
+ // did try to do it so return true
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// wxHeaderCtrl column reordering
+// ----------------------------------------------------------------------------
+
+void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt& order)
+{
+ m_colIndices = order;
+ Refresh();
+}
+
+wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const
+{
+ return m_colIndices;
+}
+
+void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos)
+{
+ MoveColumnInOrderArray(m_colIndices, idx, pos);
+
+ Refresh();
+}
+