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 array if necessary
96 if ( count
> m_numColumns
)
98 // all new columns have default positions equal to their indices
99 for ( unsigned n
= m_numColumns
; n
< count
; n
++ )
100 m_colIndices
.push_back(n
);
102 else if ( count
< m_numColumns
)
104 // filter out all the positions which are invalid now while keeping the
105 // order of the remaining ones
106 wxArrayInt colIndices
;
107 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
109 const unsigned idx
= m_colIndices
[n
];
111 colIndices
.push_back(idx
);
114 wxASSERT_MSG( colIndices
.size() == count
, "logic error" );
116 m_colIndices
= colIndices
;
118 else // count didn't really change
123 m_numColumns
= count
;
125 InvalidateBestSize();
129 unsigned int wxHeaderCtrl::DoGetCount() const
134 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
136 InvalidateBestSize();
138 // we need to refresh not only this column but also the ones after it in
139 // case it was shown or hidden or its width changed -- it would be nice to
140 // avoid doing this unnecessary by storing the old column width (TODO)
141 RefreshColsAfter(idx
);
144 // ----------------------------------------------------------------------------
145 // wxHeaderCtrl scrolling
146 // ----------------------------------------------------------------------------
148 void wxHeaderCtrl::DoScrollHorz(int dx
)
150 m_scrollOffset
+= dx
;
152 // don't call our own version which calls this function!
153 wxControl::ScrollWindow(dx
, 0);
156 // ----------------------------------------------------------------------------
157 // wxHeaderCtrl geometry
158 // ----------------------------------------------------------------------------
160 wxSize
wxHeaderCtrl::DoGetBestSize() const
162 // the vertical size is rather arbitrary but it looks better if we leave
163 // some space around the text
164 const wxSize
size(IsEmpty() ? wxHeaderCtrlBase::DoGetBestSize().x
165 : GetColEnd(GetColumnCount() - 1),
166 (7*GetCharHeight())/4);
171 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
173 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
175 int pos
= m_scrollOffset
;
176 for ( unsigned n
= 0; ; n
++ )
178 const unsigned i
= m_colIndices
[n
];
182 const wxHeaderColumn
& col
= self
->GetColumn(i
);
184 pos
+= col
.GetWidth();
190 int wxHeaderCtrl::GetColEnd(unsigned int idx
) const
192 int x
= GetColStart(idx
);
194 return x
+ const_cast<wxHeaderCtrl
*>(this)->GetColumn(idx
).GetWidth();
197 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x
, bool *onSeparator
) const
199 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
202 const unsigned count
= GetColumnCount();
203 for ( unsigned n
= 0; n
< count
; n
++ )
205 const unsigned idx
= m_colIndices
[n
];
206 const wxHeaderColumn
& col
= self
->GetColumn(idx
);
207 if ( col
.IsHidden() )
210 pos
+= col
.GetWidth();
212 // if the column is resizeable, check if we're approximatively over the
213 // line separating it from the next column
215 // TODO: don't hardcode sensitivity
216 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
223 // inside this column?
227 *onSeparator
= false;
235 // ----------------------------------------------------------------------------
236 // wxHeaderCtrl repainting
237 // ----------------------------------------------------------------------------
239 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
241 wxRect rect
= GetClientRect();
242 rect
.x
+= GetColStart(idx
);
243 rect
.width
= GetColumn(idx
).GetWidth();
248 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx
)
250 if ( idx
!= COL_NONE
)
254 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
256 wxRect rect
= GetClientRect();
257 const int ofs
= GetColStart(idx
);
264 // ----------------------------------------------------------------------------
265 // wxHeaderCtrl dragging/resizing/reordering
266 // ----------------------------------------------------------------------------
268 bool wxHeaderCtrl::IsResizing() const
270 return m_colBeingResized
!= COL_NONE
;
273 bool wxHeaderCtrl::IsReordering() const
275 return m_colBeingReordered
!= COL_NONE
;
278 void wxHeaderCtrl::ClearMarkers()
282 wxDCOverlay
dcover(m_overlay
, &dc
);
286 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical
)
290 wxDCOverlay
dcover(m_overlay
, &dc
);
293 // unfortunately drawing the marker over the parent window doesn't work as
294 // it's usually covered by another window (the main control view) so just
295 // draw the marker over the header itself, even if it makes it not very
297 dc
.SetPen(*wxLIGHT_GREY_PEN
);
298 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
301 void wxHeaderCtrl::EndDragging()
307 // don't use the special dragging cursor any more
308 SetCursor(wxNullCursor
);
311 void wxHeaderCtrl::CancelDragging()
313 wxASSERT_MSG( IsDragging(),
314 "shouldn't be called if we're not dragging anything" );
318 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
320 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, GetId());
321 event
.SetEventObject(this);
322 event
.SetColumn(col
);
324 GetEventHandler()->ProcessEvent(event
);
329 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
331 const int xStart
= GetColStart(col
);
333 // notice that GetMinWidth() returns 0 if there is no minimal width so it
334 // still makes sense to use it even in this case
335 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
337 if ( xPhysical
< xMinEnd
)
340 return xPhysical
- xStart
;
343 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
345 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
346 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
348 event
.SetEventObject(this);
349 event
.SetColumn(col
);
351 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
353 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
360 //else: nothing to do -- we just don't start to resize
362 else // go ahead with resizing
366 m_colBeingResized
= col
;
367 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
370 //else: we had already done the above when we started
372 UpdateResizingMarker(xPhysical
);
376 void wxHeaderCtrl::EndResizing(int xPhysical
)
378 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
384 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_RESIZE
, GetId());
385 event
.SetEventObject(this);
386 event
.SetColumn(m_colBeingResized
);
387 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
389 GetEventHandler()->ProcessEvent(event
);
391 m_colBeingResized
= COL_NONE
;
394 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
398 wxDCOverlay
dcover(m_overlay
, &dc
);
402 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
404 // draw the phantom position of the column being dragged
405 int x
= xPhysical
- m_dragOffset
;
406 int y
= GetClientSize().y
;
407 dc
.DrawRectangle(x
, 0,
408 GetColumn(m_colBeingReordered
).GetWidth(), y
);
410 // and also a hint indicating where it is going to be inserted if it's
412 unsigned int col
= FindColumnAtPoint(xPhysical
);
413 if ( col
!= COL_NONE
)
415 static const int DROP_MARKER_WIDTH
= 4;
417 dc
.SetBrush(*wxBLUE
);
418 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
419 DROP_MARKER_WIDTH
, y
);
423 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
425 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_BEGIN_REORDER
, GetId());
426 event
.SetEventObject(this);
427 event
.SetColumn(col
);
429 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
431 // don't start dragging it, nothing to do otherwise
435 m_dragOffset
= xPhysical
- GetColStart(col
);
437 m_colBeingReordered
= col
;
438 SetCursor(wxCursor(wxCURSOR_HAND
));
441 UpdateReorderingMarker(xPhysical
);
444 void wxHeaderCtrl::EndReordering(int xPhysical
)
446 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
452 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_REORDER
, GetId());
453 event
.SetEventObject(this);
454 event
.SetColumn(m_colBeingReordered
);
456 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
457 event
.SetNewOrder(pos
);
459 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
461 // do reorder the columns
462 DoMoveCol(m_colBeingReordered
, pos
);
465 m_colBeingReordered
= COL_NONE
;
468 // ----------------------------------------------------------------------------
469 // wxHeaderCtrl column reordering
470 // ----------------------------------------------------------------------------
472 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
474 m_colIndices
= order
;
478 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
483 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
485 MoveColumnInOrderArray(m_colIndices
, idx
, pos
);
490 // ----------------------------------------------------------------------------
491 // wxHeaderCtrl event handlers
492 // ----------------------------------------------------------------------------
494 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
495 EVT_PAINT(wxHeaderCtrl::OnPaint
)
497 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
499 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
501 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
504 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
507 GetClientSize(&w
, &h
);
509 wxAutoBufferedPaintDC
dc(this);
511 dc
.SetBackground(GetBackgroundColour());
514 // account for the horizontal scrollbar offset in the parent window
515 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
517 const unsigned int count
= m_numColumns
;
519 for ( unsigned int i
= 0; i
< count
; i
++ )
521 const unsigned idx
= m_colIndices
[i
];
522 const wxHeaderColumn
& col
= GetColumn(idx
);
523 if ( col
.IsHidden() )
526 const int colWidth
= col
.GetWidth();
528 wxHeaderSortIconType sortArrow
;
529 if ( col
.IsSortKey() )
531 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
532 : wxHDR_SORT_ICON_DOWN
;
534 else // not sorting by this column
536 sortArrow
= wxHDR_SORT_ICON_NONE
;
542 if ( idx
== m_hover
)
543 state
= wxCONTROL_CURRENT
;
547 state
= wxCONTROL_DISABLED
;
550 wxHeaderButtonParams params
;
551 params
.m_labelText
= col
.GetTitle();
552 params
.m_labelBitmap
= col
.GetBitmap();
553 params
.m_labelAlignment
= col
.GetAlignment();
555 wxRendererNative::Get().DrawHeaderButton
559 wxRect(xpos
, 0, colWidth
, h
),
569 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
575 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
577 if ( event
.GetKeyCode() == WXK_ESCAPE
)
591 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
593 // do this in advance to allow simply returning if we're not interested,
594 // we'll undo it if we do handle the event below
598 // account for the control displacement
599 const int xPhysical
= mevent
.GetX();
600 const int xLogical
= xPhysical
- m_scrollOffset
;
602 // first deal with the [continuation of any] dragging operations in
606 if ( mevent
.LeftUp() )
607 EndResizing(xPhysical
);
608 else // update the live separator position
609 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
614 if ( IsReordering() )
616 if ( mevent
.LeftUp() )
617 EndReordering(xPhysical
);
618 else // update the column position
619 UpdateReorderingMarker(xPhysical
);
625 // find if the event is over a column at all
627 const unsigned col
= mevent
.Leaving()
628 ? (onSeparator
= false, COL_NONE
)
629 : FindColumnAtPoint(xLogical
, &onSeparator
);
632 // update the highlighted column if it changed
633 if ( col
!= m_hover
)
635 const unsigned hoverOld
= m_hover
;
638 RefreshColIfNotNone(hoverOld
);
639 RefreshColIfNotNone(m_hover
);
642 // update mouse cursor as it moves around
643 if ( mevent
.Moving() )
645 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
649 // all the other events only make sense when they happen over a column
650 if ( col
== COL_NONE
)
654 // enter various dragging modes on left mouse press
655 if ( mevent
.LeftDown() )
659 // start resizing the column
660 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
661 StartOrContinueResizing(col
, xPhysical
);
663 else // on column itself
665 // start dragging the column
666 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
668 StartReordering(col
, xPhysical
);
674 // determine the type of header event corresponding to click events
675 wxEventType evtType
= wxEVT_NULL
;
676 const bool click
= mevent
.ButtonUp(),
677 dblclk
= mevent
.ButtonDClick();
678 if ( click
|| dblclk
)
680 switch ( mevent
.GetButton() )
682 case wxMOUSE_BTN_LEFT
:
683 // treat left double clicks on separator specially
684 if ( onSeparator
&& dblclk
)
686 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
688 else // not double click on separator
690 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
691 : wxEVT_COMMAND_HEADER_DCLICK
;
695 case wxMOUSE_BTN_RIGHT
:
696 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
697 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
700 case wxMOUSE_BTN_MIDDLE
:
701 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
702 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
706 // ignore clicks from other mouse buttons
711 if ( evtType
== wxEVT_NULL
)
714 wxHeaderCtrlEvent
event(evtType
, GetId());
715 event
.SetEventObject(this);
716 event
.SetColumn(col
);
718 if ( GetEventHandler()->ProcessEvent(event
) )
722 #endif // wxHAS_GENERIC_HEADERCTRL