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;
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::UpdateResizingMarker(int xPhysical
)
265 wxDCOverlay
dcover(m_overlay
, &dc
);
268 // unfortunately drawing the marker over the parent window doesn't work as
269 // it's usually covered by another window (the main control view) so just
270 // draw the marker over the header itself, even if it makes it not very
272 dc
.SetPen(*wxLIGHT_GREY_PEN
);
273 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
276 void wxHeaderCtrl::EndDragging()
282 // don't use the special dragging cursor any more
283 SetCursor(wxNullCursor
);
286 void wxHeaderCtrl::CancelDragging()
288 wxASSERT_MSG( IsDragging(),
289 "shouldn't be called if we're not dragging anything" );
293 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
295 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, GetId());
296 event
.SetEventObject(this);
297 event
.SetColumn(col
);
299 GetEventHandler()->ProcessEvent(event
);
304 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
306 const int xStart
= GetColStart(col
);
308 // notice that GetMinWidth() returns 0 if there is no minimal width so it
309 // still makes sense to use it even in this case
310 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
312 if ( xPhysical
< xMinEnd
)
315 return xPhysical
- xStart
;
318 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
320 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
321 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
323 event
.SetEventObject(this);
324 event
.SetColumn(col
);
326 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
328 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
335 //else: nothing to do -- we just don't start to resize
337 else // go ahead with resizing
341 m_colBeingResized
= col
;
342 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
345 //else: we had already done the above when we started
347 UpdateResizingMarker(xPhysical
);
351 void wxHeaderCtrl::EndResizing(int xPhysical
)
353 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
359 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_RESIZE
, GetId());
360 event
.SetEventObject(this);
361 event
.SetColumn(m_colBeingResized
);
362 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
364 GetEventHandler()->ProcessEvent(event
);
366 m_colBeingResized
= COL_NONE
;
369 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
373 wxDCOverlay
dcover(m_overlay
, &dc
);
377 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
379 // draw the phantom position of the column being dragged
380 int x
= xPhysical
- m_dragOffset
;
381 int y
= GetClientSize().y
;
382 dc
.DrawRectangle(x
, 0,
383 GetColumn(m_colBeingReordered
).GetWidth(), y
);
385 // and also a hint indicating where it is going to be inserted if it's
387 unsigned int col
= FindColumnAtPoint(xPhysical
);
388 if ( col
!= COL_NONE
)
390 static const int DROP_MARKER_WIDTH
= 4;
392 dc
.SetBrush(*wxBLUE
);
393 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
394 DROP_MARKER_WIDTH
, y
);
398 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
400 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_BEGIN_REORDER
, GetId());
401 event
.SetEventObject(this);
402 event
.SetColumn(col
);
404 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
406 // don't start dragging it, nothing to do otherwise
410 m_dragOffset
= xPhysical
- GetColStart(col
);
412 m_colBeingReordered
= col
;
413 SetCursor(wxCursor(wxCURSOR_HAND
));
416 // do not call UpdateReorderingMarker() here: we don't want to give
417 // feedback for reordering until the user starts to really move the mouse
418 // as he might want to just click on the column and not move it at all
421 bool wxHeaderCtrl::EndReordering(int xPhysical
)
423 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
429 const int colOld
= m_colBeingReordered
,
430 colNew
= FindColumnAtPoint(xPhysical
);
432 m_colBeingReordered
= COL_NONE
;
434 if ( xPhysical
- GetColStart(colOld
) == m_dragOffset
)
437 if ( colNew
!= colOld
)
439 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_REORDER
, GetId());
440 event
.SetEventObject(this);
441 event
.SetColumn(colOld
);
443 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
444 event
.SetNewOrder(pos
);
446 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
448 // do reorder the columns
449 DoMoveCol(colOld
, pos
);
453 // whether we moved the column or not, the user did move the mouse and so
454 // did try to do it so return true
458 // ----------------------------------------------------------------------------
459 // wxHeaderCtrl column reordering
460 // ----------------------------------------------------------------------------
462 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
464 m_colIndices
= order
;
468 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
473 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
475 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
480 // ----------------------------------------------------------------------------
481 // wxHeaderCtrl event handlers
482 // ----------------------------------------------------------------------------
484 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
485 EVT_PAINT(wxHeaderCtrl::OnPaint
)
487 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
489 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
491 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
494 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
497 GetClientSize(&w
, &h
);
499 wxAutoBufferedPaintDC
dc(this);
501 dc
.SetBackground(GetBackgroundColour());
504 // account for the horizontal scrollbar offset in the parent window
505 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
507 const unsigned int count
= m_numColumns
;
509 for ( unsigned int i
= 0; i
< count
; i
++ )
511 const unsigned idx
= m_colIndices
[i
];
512 const wxHeaderColumn
& col
= GetColumn(idx
);
513 if ( col
.IsHidden() )
516 const int colWidth
= col
.GetWidth();
518 wxHeaderSortIconType sortArrow
;
519 if ( col
.IsSortKey() )
521 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
522 : wxHDR_SORT_ICON_DOWN
;
524 else // not sorting by this column
526 sortArrow
= wxHDR_SORT_ICON_NONE
;
532 if ( idx
== m_hover
)
533 state
= wxCONTROL_CURRENT
;
537 state
= wxCONTROL_DISABLED
;
540 wxHeaderButtonParams params
;
541 params
.m_labelText
= col
.GetTitle();
542 params
.m_labelBitmap
= col
.GetBitmap();
543 params
.m_labelAlignment
= col
.GetAlignment();
545 wxRendererNative::Get().DrawHeaderButton
549 wxRect(xpos
, 0, colWidth
, h
),
559 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
565 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
567 if ( event
.GetKeyCode() == WXK_ESCAPE
)
581 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
583 // do this in advance to allow simply returning if we're not interested,
584 // we'll undo it if we do handle the event below
588 // account for the control displacement
589 const int xPhysical
= mevent
.GetX();
590 const int xLogical
= xPhysical
- m_scrollOffset
;
592 // first deal with the [continuation of any] dragging operations in
596 if ( mevent
.LeftUp() )
597 EndResizing(xPhysical
);
598 else // update the live separator position
599 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
604 if ( IsReordering() )
606 if ( !mevent
.LeftUp() )
608 // update the column position
609 UpdateReorderingMarker(xPhysical
);
614 // finish reordering and continue to generate a click event below if we
615 // didn't really reorder anything
616 if ( EndReordering(xPhysical
) )
621 // find if the event is over a column at all
623 const unsigned col
= mevent
.Leaving()
624 ? (onSeparator
= false, COL_NONE
)
625 : FindColumnAtPoint(xLogical
, &onSeparator
);
628 // update the highlighted column if it changed
629 if ( col
!= m_hover
)
631 const unsigned hoverOld
= m_hover
;
634 RefreshColIfNotNone(hoverOld
);
635 RefreshColIfNotNone(m_hover
);
638 // update mouse cursor as it moves around
639 if ( mevent
.Moving() )
641 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
645 // all the other events only make sense when they happen over a column
646 if ( col
== COL_NONE
)
650 // enter various dragging modes on left mouse press
651 if ( mevent
.LeftDown() )
655 // start resizing the column
656 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
657 StartOrContinueResizing(col
, xPhysical
);
659 else // on column itself
661 // start dragging the column
662 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
664 StartReordering(col
, xPhysical
);
670 // determine the type of header event corresponding to click events
671 wxEventType evtType
= wxEVT_NULL
;
672 const bool click
= mevent
.ButtonUp(),
673 dblclk
= mevent
.ButtonDClick();
674 if ( click
|| dblclk
)
676 switch ( mevent
.GetButton() )
678 case wxMOUSE_BTN_LEFT
:
679 // treat left double clicks on separator specially
680 if ( onSeparator
&& dblclk
)
682 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
684 else // not double click on separator
686 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
687 : wxEVT_COMMAND_HEADER_DCLICK
;
691 case wxMOUSE_BTN_RIGHT
:
692 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
693 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
696 case wxMOUSE_BTN_MIDDLE
:
697 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
698 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
702 // ignore clicks from other mouse buttons
707 if ( evtType
== wxEVT_NULL
)
710 wxHeaderCtrlEvent
event(evtType
, GetId());
711 event
.SetEventObject(this);
712 event
.SetColumn(col
);
714 if ( GetEventHandler()->ProcessEvent(event
) )
718 #endif // wxHAS_GENERIC_HEADERCTRL