1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/headerctrlg.cpp
3 // Purpose: generic wxHeaderCtrl implementation
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
27 #include "wx/headerctrl.h"
29 #ifdef wxHAS_GENERIC_HEADERCTRL
31 #include "wx/dcbuffer.h"
32 #include "wx/renderer.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
41 const unsigned COL_NONE
= (unsigned)-1;
43 } // anonymous namespace
45 // ============================================================================
46 // wxHeaderCtrl implementation
47 // ============================================================================
49 // ----------------------------------------------------------------------------
50 // wxHeaderCtrl creation
51 // ----------------------------------------------------------------------------
53 void wxHeaderCtrl::Init()
58 m_colBeingReordered
= COL_NONE
;
63 bool wxHeaderCtrl::Create(wxWindow
*parent
,
70 if ( !wxHeaderCtrlBase::Create(parent
, id
, pos
, size
,
71 style
, wxDefaultValidator
, name
) )
74 // tell the system to not paint the background at all to avoid flicker as
75 // we paint the entire window area in our OnPaint()
76 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
81 wxHeaderCtrl::~wxHeaderCtrl()
85 // ----------------------------------------------------------------------------
86 // wxHeaderCtrl columns manipulation
87 // ----------------------------------------------------------------------------
89 void wxHeaderCtrl::DoSetCount(unsigned int count
)
91 // update the column indices order array before changing m_numColumns
92 DoResizeColumnIndices(m_colIndices
, count
);
100 unsigned int wxHeaderCtrl::DoGetCount() const
105 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
107 InvalidateBestSize();
109 // we need to refresh not only this column but also the ones after it in
110 // case it was shown or hidden or its width changed -- it would be nice to
111 // avoid doing this unnecessary by storing the old column width (TODO)
112 RefreshColsAfter(idx
);
115 // ----------------------------------------------------------------------------
116 // wxHeaderCtrl scrolling
117 // ----------------------------------------------------------------------------
119 void wxHeaderCtrl::DoScrollHorz(int dx
)
121 m_scrollOffset
+= dx
;
123 // don't call our own version which calls this function!
124 wxControl::ScrollWindow(dx
, 0);
127 // ----------------------------------------------------------------------------
128 // wxHeaderCtrl geometry
129 // ----------------------------------------------------------------------------
131 wxSize
wxHeaderCtrl::DoGetBestSize() const
133 wxWindow
*win
= GetParent();
134 int height
= wxRendererNative::Get().GetHeaderButtonHeight( win
);
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 height
); // (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 resizable, 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::EndDragging()
260 // We currently only use markers for reordering, not for resizing
267 // don't use the special dragging cursor any more
268 SetCursor(wxNullCursor
);
271 void wxHeaderCtrl::CancelDragging()
273 wxASSERT_MSG( IsDragging(),
274 "shouldn't be called if we're not dragging anything" );
278 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
280 wxHeaderCtrlEvent
event(wxEVT_HEADER_DRAGGING_CANCELLED
, GetId());
281 event
.SetEventObject(this);
282 event
.SetColumn(col
);
284 GetEventHandler()->ProcessEvent(event
);
289 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
291 const int xStart
= GetColStart(col
);
293 // notice that GetMinWidth() returns 0 if there is no minimal width so it
294 // still makes sense to use it even in this case
295 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
297 if ( xPhysical
< xMinEnd
)
300 return xPhysical
- xStart
;
303 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
305 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_HEADER_RESIZING
306 : wxEVT_HEADER_BEGIN_RESIZE
,
308 event
.SetEventObject(this);
309 event
.SetColumn(col
);
311 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
313 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
320 //else: nothing to do -- we just don't start to resize
322 else // go ahead with resizing
326 m_colBeingResized
= col
;
327 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
330 //else: we had already done the above when we started
335 void wxHeaderCtrl::EndResizing(int xPhysical
)
337 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
343 wxHeaderCtrlEvent
event(wxEVT_HEADER_END_RESIZE
, GetId());
344 event
.SetEventObject(this);
345 event
.SetColumn(m_colBeingResized
);
346 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
348 GetEventHandler()->ProcessEvent(event
);
350 m_colBeingResized
= COL_NONE
;
353 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
357 wxDCOverlay
dcover(m_overlay
, &dc
);
361 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
363 // draw the phantom position of the column being dragged
364 int x
= xPhysical
- m_dragOffset
;
365 int y
= GetClientSize().y
;
366 dc
.DrawRectangle(x
, 0,
367 GetColumn(m_colBeingReordered
).GetWidth(), y
);
369 // and also a hint indicating where it is going to be inserted if it's
371 unsigned int col
= FindColumnAtPoint(xPhysical
);
372 if ( col
!= COL_NONE
)
374 static const int DROP_MARKER_WIDTH
= 4;
376 dc
.SetBrush(*wxBLUE
);
377 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
378 DROP_MARKER_WIDTH
, y
);
382 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
384 wxHeaderCtrlEvent
event(wxEVT_HEADER_BEGIN_REORDER
, GetId());
385 event
.SetEventObject(this);
386 event
.SetColumn(col
);
388 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
390 // don't start dragging it, nothing to do otherwise
394 m_dragOffset
= xPhysical
- GetColStart(col
);
396 m_colBeingReordered
= col
;
397 SetCursor(wxCursor(wxCURSOR_HAND
));
400 // do not call UpdateReorderingMarker() here: we don't want to give
401 // feedback for reordering until the user starts to really move the mouse
402 // as he might want to just click on the column and not move it at all
405 bool wxHeaderCtrl::EndReordering(int xPhysical
)
407 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
413 const int colOld
= m_colBeingReordered
,
414 colNew
= FindColumnAtPoint(xPhysical
);
416 m_colBeingReordered
= COL_NONE
;
418 if ( xPhysical
- GetColStart(colOld
) == m_dragOffset
)
421 if ( colNew
!= colOld
)
423 wxHeaderCtrlEvent
event(wxEVT_HEADER_END_REORDER
, GetId());
424 event
.SetEventObject(this);
425 event
.SetColumn(colOld
);
427 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
428 event
.SetNewOrder(pos
);
430 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
432 // do reorder the columns
433 DoMoveCol(colOld
, pos
);
437 // whether we moved the column or not, the user did move the mouse and so
438 // did try to do it so return true
442 // ----------------------------------------------------------------------------
443 // wxHeaderCtrl column reordering
444 // ----------------------------------------------------------------------------
446 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
448 m_colIndices
= order
;
452 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
457 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
459 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
464 // ----------------------------------------------------------------------------
465 // wxHeaderCtrl event handlers
466 // ----------------------------------------------------------------------------
468 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
469 EVT_PAINT(wxHeaderCtrl::OnPaint
)
471 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
473 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
475 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
478 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
481 GetClientSize(&w
, &h
);
485 // GetVirtualSize(&vw, NULL);
488 wxAutoBufferedPaintDC
dc(this);
490 dc
.SetBackground(GetBackgroundColour());
493 // account for the horizontal scrollbar offset in the parent window
494 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
496 const unsigned int count
= m_numColumns
;
498 for ( unsigned int i
= 0; i
< count
; i
++ )
500 const unsigned idx
= m_colIndices
[i
];
501 const wxHeaderColumn
& col
= GetColumn(idx
);
502 if ( col
.IsHidden() )
505 int colWidth
= col
.GetWidth();
507 wxHeaderSortIconType sortArrow
;
508 if ( col
.IsSortKey() )
510 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
511 : wxHDR_SORT_ICON_DOWN
;
513 else // not sorting by this column
515 sortArrow
= wxHDR_SORT_ICON_NONE
;
521 if ( idx
== m_hover
)
522 state
= wxCONTROL_CURRENT
;
526 state
= wxCONTROL_DISABLED
;
530 state
|= wxCONTROL_SPECIAL
;
532 wxHeaderButtonParams params
;
533 params
.m_labelText
= col
.GetTitle();
534 params
.m_labelBitmap
= col
.GetBitmap();
535 params
.m_labelAlignment
= col
.GetAlignment();
540 // colWidth = wxMax( colWidth, vw - xpos );
541 state
|= wxCONTROL_DIRTY
;
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_HEADER_SEPARATOR_DCLICK
;
684 else // not double click on separator
686 evtType
= click
? wxEVT_HEADER_CLICK
687 : wxEVT_HEADER_DCLICK
;
691 case wxMOUSE_BTN_RIGHT
:
692 evtType
= click
? wxEVT_HEADER_RIGHT_CLICK
693 : wxEVT_HEADER_RIGHT_DCLICK
;
696 case wxMOUSE_BTN_MIDDLE
:
697 evtType
= click
? wxEVT_HEADER_MIDDLE_CLICK
698 : wxEVT_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
720 #endif // wxUSE_HEADERCTRL