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 // the vertical size is rather arbitrary but it looks better if we leave
137 // some space around the text
138 const wxSize
size(IsEmpty() ? wxHeaderCtrlBase::DoGetBestSize().x
139 : GetColEnd(GetColumnCount() - 1),
140 (7*GetCharHeight())/4);
145 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
147 int pos
= m_scrollOffset
;
148 for ( unsigned n
= 0; ; n
++ )
150 const unsigned i
= m_colIndices
[n
];
154 const wxHeaderColumn
& col
= GetColumn(i
);
156 pos
+= col
.GetWidth();
162 int wxHeaderCtrl::GetColEnd(unsigned int idx
) const
164 int x
= GetColStart(idx
);
166 return x
+ GetColumn(idx
).GetWidth();
169 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x
, bool *onSeparator
) const
172 const unsigned count
= GetColumnCount();
173 for ( unsigned n
= 0; n
< count
; n
++ )
175 const unsigned idx
= m_colIndices
[n
];
176 const wxHeaderColumn
& col
= GetColumn(idx
);
177 if ( col
.IsHidden() )
180 pos
+= col
.GetWidth();
182 // if the column is resizeable, check if we're approximatively over the
183 // line separating it from the next column
185 // TODO: don't hardcode sensitivity
186 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
193 // inside this column?
197 *onSeparator
= false;
203 *onSeparator
= false;
207 // ----------------------------------------------------------------------------
208 // wxHeaderCtrl repainting
209 // ----------------------------------------------------------------------------
211 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
213 wxRect rect
= GetClientRect();
214 rect
.x
+= GetColStart(idx
);
215 rect
.width
= GetColumn(idx
).GetWidth();
220 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx
)
222 if ( idx
!= COL_NONE
)
226 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
228 wxRect rect
= GetClientRect();
229 const int ofs
= GetColStart(idx
);
236 // ----------------------------------------------------------------------------
237 // wxHeaderCtrl dragging/resizing/reordering
238 // ----------------------------------------------------------------------------
240 bool wxHeaderCtrl::IsResizing() const
242 return m_colBeingResized
!= COL_NONE
;
245 bool wxHeaderCtrl::IsReordering() const
247 return m_colBeingReordered
!= COL_NONE
;
250 void wxHeaderCtrl::ClearMarkers()
254 wxDCOverlay
dcover(m_overlay
, &dc
);
258 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical
)
262 wxDCOverlay
dcover(m_overlay
, &dc
);
265 // unfortunately drawing the marker over the parent window doesn't work as
266 // it's usually covered by another window (the main control view) so just
267 // draw the marker over the header itself, even if it makes it not very
269 dc
.SetPen(*wxLIGHT_GREY_PEN
);
270 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
273 void wxHeaderCtrl::EndDragging()
279 // don't use the special dragging cursor any more
280 SetCursor(wxNullCursor
);
283 void wxHeaderCtrl::CancelDragging()
285 wxASSERT_MSG( IsDragging(),
286 "shouldn't be called if we're not dragging anything" );
290 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
292 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, GetId());
293 event
.SetEventObject(this);
294 event
.SetColumn(col
);
296 GetEventHandler()->ProcessEvent(event
);
301 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
303 const int xStart
= GetColStart(col
);
305 // notice that GetMinWidth() returns 0 if there is no minimal width so it
306 // still makes sense to use it even in this case
307 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
309 if ( xPhysical
< xMinEnd
)
312 return xPhysical
- xStart
;
315 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
317 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
318 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
320 event
.SetEventObject(this);
321 event
.SetColumn(col
);
323 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
325 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
332 //else: nothing to do -- we just don't start to resize
334 else // go ahead with resizing
338 m_colBeingResized
= col
;
339 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
342 //else: we had already done the above when we started
344 UpdateResizingMarker(xPhysical
);
348 void wxHeaderCtrl::EndResizing(int xPhysical
)
350 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
356 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_RESIZE
, GetId());
357 event
.SetEventObject(this);
358 event
.SetColumn(m_colBeingResized
);
359 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
361 GetEventHandler()->ProcessEvent(event
);
363 m_colBeingResized
= COL_NONE
;
366 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
370 wxDCOverlay
dcover(m_overlay
, &dc
);
374 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
376 // draw the phantom position of the column being dragged
377 int x
= xPhysical
- m_dragOffset
;
378 int y
= GetClientSize().y
;
379 dc
.DrawRectangle(x
, 0,
380 GetColumn(m_colBeingReordered
).GetWidth(), y
);
382 // and also a hint indicating where it is going to be inserted if it's
384 unsigned int col
= FindColumnAtPoint(xPhysical
);
385 if ( col
!= COL_NONE
)
387 static const int DROP_MARKER_WIDTH
= 4;
389 dc
.SetBrush(*wxBLUE
);
390 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
391 DROP_MARKER_WIDTH
, y
);
395 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
397 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_BEGIN_REORDER
, GetId());
398 event
.SetEventObject(this);
399 event
.SetColumn(col
);
401 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
403 // don't start dragging it, nothing to do otherwise
407 m_dragOffset
= xPhysical
- GetColStart(col
);
409 m_colBeingReordered
= col
;
410 SetCursor(wxCursor(wxCURSOR_HAND
));
413 // do not call UpdateReorderingMarker() here: we don't want to give
414 // feedback for reordering until the user starts to really move the mouse
415 // as he might want to just click on the column and not move it at all
418 bool wxHeaderCtrl::EndReordering(int xPhysical
)
420 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
426 const int colOld
= m_colBeingReordered
,
427 colNew
= FindColumnAtPoint(xPhysical
);
429 m_colBeingReordered
= COL_NONE
;
431 if ( xPhysical
- GetColStart(colOld
) == m_dragOffset
)
434 if ( colNew
!= colOld
)
436 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_REORDER
, GetId());
437 event
.SetEventObject(this);
438 event
.SetColumn(colOld
);
440 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
441 event
.SetNewOrder(pos
);
443 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
445 // do reorder the columns
446 DoMoveCol(colOld
, pos
);
450 // whether we moved the column or not, the user did move the mouse and so
451 // did try to do it so return true
455 // ----------------------------------------------------------------------------
456 // wxHeaderCtrl column reordering
457 // ----------------------------------------------------------------------------
459 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
461 m_colIndices
= order
;
465 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
470 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
472 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
477 // ----------------------------------------------------------------------------
478 // wxHeaderCtrl event handlers
479 // ----------------------------------------------------------------------------
481 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
482 EVT_PAINT(wxHeaderCtrl::OnPaint
)
484 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
486 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
488 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
491 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
494 GetClientSize(&w
, &h
);
496 wxAutoBufferedPaintDC
dc(this);
498 dc
.SetBackground(GetBackgroundColour());
501 // account for the horizontal scrollbar offset in the parent window
502 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
504 const unsigned int count
= m_numColumns
;
506 for ( unsigned int i
= 0; i
< count
; i
++ )
508 const unsigned idx
= m_colIndices
[i
];
509 const wxHeaderColumn
& col
= GetColumn(idx
);
510 if ( col
.IsHidden() )
513 const int colWidth
= col
.GetWidth();
515 wxHeaderSortIconType sortArrow
;
516 if ( col
.IsSortKey() )
518 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
519 : wxHDR_SORT_ICON_DOWN
;
521 else // not sorting by this column
523 sortArrow
= wxHDR_SORT_ICON_NONE
;
529 if ( idx
== m_hover
)
530 state
= wxCONTROL_CURRENT
;
534 state
= wxCONTROL_DISABLED
;
537 wxHeaderButtonParams params
;
538 params
.m_labelText
= col
.GetTitle();
539 params
.m_labelBitmap
= col
.GetBitmap();
540 params
.m_labelAlignment
= col
.GetAlignment();
542 wxRendererNative::Get().DrawHeaderButton
546 wxRect(xpos
, 0, colWidth
, h
),
556 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
562 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
564 if ( event
.GetKeyCode() == WXK_ESCAPE
)
578 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
580 // do this in advance to allow simply returning if we're not interested,
581 // we'll undo it if we do handle the event below
585 // account for the control displacement
586 const int xPhysical
= mevent
.GetX();
587 const int xLogical
= xPhysical
- m_scrollOffset
;
589 // first deal with the [continuation of any] dragging operations in
593 if ( mevent
.LeftUp() )
594 EndResizing(xPhysical
);
595 else // update the live separator position
596 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
601 if ( IsReordering() )
603 if ( !mevent
.LeftUp() )
605 // update the column position
606 UpdateReorderingMarker(xPhysical
);
611 // finish reordering and continue to generate a click event below if we
612 // didn't really reorder anything
613 if ( EndReordering(xPhysical
) )
618 // find if the event is over a column at all
620 const unsigned col
= mevent
.Leaving()
621 ? (onSeparator
= false, COL_NONE
)
622 : FindColumnAtPoint(xLogical
, &onSeparator
);
625 // update the highlighted column if it changed
626 if ( col
!= m_hover
)
628 const unsigned hoverOld
= m_hover
;
631 RefreshColIfNotNone(hoverOld
);
632 RefreshColIfNotNone(m_hover
);
635 // update mouse cursor as it moves around
636 if ( mevent
.Moving() )
638 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
642 // all the other events only make sense when they happen over a column
643 if ( col
== COL_NONE
)
647 // enter various dragging modes on left mouse press
648 if ( mevent
.LeftDown() )
652 // start resizing the column
653 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
654 StartOrContinueResizing(col
, xPhysical
);
656 else // on column itself
658 // start dragging the column
659 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
661 StartReordering(col
, xPhysical
);
667 // determine the type of header event corresponding to click events
668 wxEventType evtType
= wxEVT_NULL
;
669 const bool click
= mevent
.ButtonUp(),
670 dblclk
= mevent
.ButtonDClick();
671 if ( click
|| dblclk
)
673 switch ( mevent
.GetButton() )
675 case wxMOUSE_BTN_LEFT
:
676 // treat left double clicks on separator specially
677 if ( onSeparator
&& dblclk
)
679 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
681 else // not double click on separator
683 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
684 : wxEVT_COMMAND_HEADER_DCLICK
;
688 case wxMOUSE_BTN_RIGHT
:
689 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
690 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
693 case wxMOUSE_BTN_MIDDLE
:
694 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
695 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
699 // ignore clicks from other mouse buttons
704 if ( evtType
== wxEVT_NULL
)
707 wxHeaderCtrlEvent
event(evtType
, GetId());
708 event
.SetEventObject(this);
709 event
.SetColumn(col
);
711 if ( GetEventHandler()->ProcessEvent(event
) )
715 #endif // wxHAS_GENERIC_HEADERCTRL
717 #endif // wxUSE_HEADERCTRL