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 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 resizeable, 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::UpdateResizingMarker(int xPhysical
)
263 wxDCOverlay
dcover(m_overlay
, &dc
);
266 // unfortunately drawing the marker over the parent window doesn't work as
267 // it's usually covered by another window (the main control view) so just
268 // draw the marker over the header itself, even if it makes it not very
270 dc
.SetPen(*wxLIGHT_GREY_PEN
);
271 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
274 void wxHeaderCtrl::EndDragging()
280 // don't use the special dragging cursor any more
281 SetCursor(wxNullCursor
);
284 void wxHeaderCtrl::CancelDragging()
286 wxASSERT_MSG( IsDragging(),
287 "shouldn't be called if we're not dragging anything" );
291 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
293 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, GetId());
294 event
.SetEventObject(this);
295 event
.SetColumn(col
);
297 GetEventHandler()->ProcessEvent(event
);
302 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
304 const int xStart
= GetColStart(col
);
306 // notice that GetMinWidth() returns 0 if there is no minimal width so it
307 // still makes sense to use it even in this case
308 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
310 if ( xPhysical
< xMinEnd
)
313 return xPhysical
- xStart
;
316 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
318 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
319 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
321 event
.SetEventObject(this);
322 event
.SetColumn(col
);
324 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
326 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
333 //else: nothing to do -- we just don't start to resize
335 else // go ahead with resizing
339 m_colBeingResized
= col
;
340 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
343 //else: we had already done the above when we started
345 UpdateResizingMarker(xPhysical
);
349 void wxHeaderCtrl::EndResizing(int xPhysical
)
351 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
357 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_RESIZE
, GetId());
358 event
.SetEventObject(this);
359 event
.SetColumn(m_colBeingResized
);
360 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
362 GetEventHandler()->ProcessEvent(event
);
364 m_colBeingResized
= COL_NONE
;
367 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
371 wxDCOverlay
dcover(m_overlay
, &dc
);
375 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
377 // draw the phantom position of the column being dragged
378 int x
= xPhysical
- m_dragOffset
;
379 int y
= GetClientSize().y
;
380 dc
.DrawRectangle(x
, 0,
381 GetColumn(m_colBeingReordered
).GetWidth(), y
);
383 // and also a hint indicating where it is going to be inserted if it's
385 unsigned int col
= FindColumnAtPoint(xPhysical
);
386 if ( col
!= COL_NONE
)
388 static const int DROP_MARKER_WIDTH
= 4;
390 dc
.SetBrush(*wxBLUE
);
391 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
392 DROP_MARKER_WIDTH
, y
);
396 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
398 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_BEGIN_REORDER
, GetId());
399 event
.SetEventObject(this);
400 event
.SetColumn(col
);
402 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
404 // don't start dragging it, nothing to do otherwise
408 m_dragOffset
= xPhysical
- GetColStart(col
);
410 m_colBeingReordered
= col
;
411 SetCursor(wxCursor(wxCURSOR_HAND
));
414 // do not call UpdateReorderingMarker() here: we don't want to give
415 // feedback for reordering until the user starts to really move the mouse
416 // as he might want to just click on the column and not move it at all
419 bool wxHeaderCtrl::EndReordering(int xPhysical
)
421 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
427 const int colOld
= m_colBeingReordered
,
428 colNew
= FindColumnAtPoint(xPhysical
);
430 m_colBeingReordered
= COL_NONE
;
432 if ( xPhysical
- GetColStart(colOld
) == m_dragOffset
)
435 if ( colNew
!= colOld
)
437 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_REORDER
, GetId());
438 event
.SetEventObject(this);
439 event
.SetColumn(colOld
);
441 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
442 event
.SetNewOrder(pos
);
444 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
446 // do reorder the columns
447 DoMoveCol(colOld
, pos
);
451 // whether we moved the column or not, the user did move the mouse and so
452 // did try to do it so return true
456 // ----------------------------------------------------------------------------
457 // wxHeaderCtrl column reordering
458 // ----------------------------------------------------------------------------
460 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
462 m_colIndices
= order
;
466 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
471 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
473 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
478 // ----------------------------------------------------------------------------
479 // wxHeaderCtrl event handlers
480 // ----------------------------------------------------------------------------
482 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
483 EVT_PAINT(wxHeaderCtrl::OnPaint
)
485 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
487 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
489 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
492 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
495 GetClientSize(&w
, &h
);
497 wxAutoBufferedPaintDC
dc(this);
499 dc
.SetBackground(GetBackgroundColour());
502 // account for the horizontal scrollbar offset in the parent window
503 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
505 const unsigned int count
= m_numColumns
;
507 for ( unsigned int i
= 0; i
< count
; i
++ )
509 const unsigned idx
= m_colIndices
[i
];
510 const wxHeaderColumn
& col
= GetColumn(idx
);
511 if ( col
.IsHidden() )
514 const int colWidth
= col
.GetWidth();
516 wxHeaderSortIconType sortArrow
;
517 if ( col
.IsSortKey() )
519 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
520 : wxHDR_SORT_ICON_DOWN
;
522 else // not sorting by this column
524 sortArrow
= wxHDR_SORT_ICON_NONE
;
530 if ( idx
== m_hover
)
531 state
= wxCONTROL_CURRENT
;
535 state
= wxCONTROL_DISABLED
;
538 wxHeaderButtonParams params
;
539 params
.m_labelText
= col
.GetTitle();
540 params
.m_labelBitmap
= col
.GetBitmap();
541 params
.m_labelAlignment
= col
.GetAlignment();
543 wxRendererNative::Get().DrawHeaderButton
547 wxRect(xpos
, 0, colWidth
, h
),
557 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
563 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
565 if ( event
.GetKeyCode() == WXK_ESCAPE
)
579 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
581 // do this in advance to allow simply returning if we're not interested,
582 // we'll undo it if we do handle the event below
586 // account for the control displacement
587 const int xPhysical
= mevent
.GetX();
588 const int xLogical
= xPhysical
- m_scrollOffset
;
590 // first deal with the [continuation of any] dragging operations in
594 if ( mevent
.LeftUp() )
595 EndResizing(xPhysical
);
596 else // update the live separator position
597 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
602 if ( IsReordering() )
604 if ( !mevent
.LeftUp() )
606 // update the column position
607 UpdateReorderingMarker(xPhysical
);
612 // finish reordering and continue to generate a click event below if we
613 // didn't really reorder anything
614 if ( EndReordering(xPhysical
) )
619 // find if the event is over a column at all
621 const unsigned col
= mevent
.Leaving()
622 ? (onSeparator
= false, COL_NONE
)
623 : FindColumnAtPoint(xLogical
, &onSeparator
);
626 // update the highlighted column if it changed
627 if ( col
!= m_hover
)
629 const unsigned hoverOld
= m_hover
;
632 RefreshColIfNotNone(hoverOld
);
633 RefreshColIfNotNone(m_hover
);
636 // update mouse cursor as it moves around
637 if ( mevent
.Moving() )
639 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
643 // all the other events only make sense when they happen over a column
644 if ( col
== COL_NONE
)
648 // enter various dragging modes on left mouse press
649 if ( mevent
.LeftDown() )
653 // start resizing the column
654 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
655 StartOrContinueResizing(col
, xPhysical
);
657 else // on column itself
659 // start dragging the column
660 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
662 StartReordering(col
, xPhysical
);
668 // determine the type of header event corresponding to click events
669 wxEventType evtType
= wxEVT_NULL
;
670 const bool click
= mevent
.ButtonUp(),
671 dblclk
= mevent
.ButtonDClick();
672 if ( click
|| dblclk
)
674 switch ( mevent
.GetButton() )
676 case wxMOUSE_BTN_LEFT
:
677 // treat left double clicks on separator specially
678 if ( onSeparator
&& dblclk
)
680 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
682 else // not double click on separator
684 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
685 : wxEVT_COMMAND_HEADER_DCLICK
;
689 case wxMOUSE_BTN_RIGHT
:
690 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
691 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
694 case wxMOUSE_BTN_MIDDLE
:
695 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
696 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
700 // ignore clicks from other mouse buttons
705 if ( evtType
== wxEVT_NULL
)
708 wxHeaderCtrlEvent
event(evtType
, GetId());
709 event
.SetEventObject(this);
710 event
.SetColumn(col
);
712 if ( GetEventHandler()->ProcessEvent(event
) )
716 #endif // wxHAS_GENERIC_HEADERCTRL