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
;
119 m_numColumns
= count
;
124 unsigned int wxHeaderCtrl::DoGetCount() const
129 void wxHeaderCtrl::DoUpdate(unsigned int idx
)
131 // we need to refresh not only this column but also the ones after it in
132 // case it was shown or hidden or its width changed -- it would be nice to
133 // avoid doing this unnecessary by storing the old column width (TODO)
134 RefreshColsAfter(idx
);
137 // ----------------------------------------------------------------------------
138 // wxHeaderCtrl scrolling
139 // ----------------------------------------------------------------------------
141 void wxHeaderCtrl::DoScrollHorz(int dx
)
143 m_scrollOffset
+= dx
;
145 // don't call our own version which calls this function!
146 wxControl::ScrollWindow(dx
, 0);
149 // ----------------------------------------------------------------------------
150 // wxHeaderCtrl geometry
151 // ----------------------------------------------------------------------------
153 wxSize
wxHeaderCtrl::DoGetBestSize() const
155 // the vertical size is rather arbitrary but it looks better if we leave
156 // some space around the text
157 return wxSize(IsEmpty() ? DEFAULT_ITEM_WIDTH
158 : GetColEnd(GetColumnCount() - 1),
159 (7*GetCharHeight())/4);
162 int wxHeaderCtrl::GetColStart(unsigned int idx
) const
164 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
166 int pos
= m_scrollOffset
;
167 for ( unsigned n
= 0; ; n
++ )
169 const unsigned i
= m_colIndices
[n
];
173 const wxHeaderColumnBase
& col
= self
->GetColumn(i
);
175 pos
+= col
.GetWidth();
181 int wxHeaderCtrl::GetColEnd(unsigned int idx
) const
183 int x
= GetColStart(idx
);
185 return x
+ const_cast<wxHeaderCtrl
*>(this)->GetColumn(idx
).GetWidth();
188 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x
, bool *onSeparator
) const
190 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
193 const unsigned count
= GetColumnCount();
194 for ( unsigned n
= 0; n
< count
; n
++ )
196 const unsigned idx
= m_colIndices
[n
];
197 const wxHeaderColumnBase
& col
= self
->GetColumn(idx
);
198 if ( col
.IsHidden() )
201 pos
+= col
.GetWidth();
203 // if the column is resizeable, check if we're approximatively over the
204 // line separating it from the next column
206 // TODO: don't hardcode sensitivity
207 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
214 // inside this column?
218 *onSeparator
= false;
226 // ----------------------------------------------------------------------------
227 // wxHeaderCtrl repainting
228 // ----------------------------------------------------------------------------
230 void wxHeaderCtrl::RefreshCol(unsigned int idx
)
232 wxRect rect
= GetClientRect();
233 rect
.x
+= GetColStart(idx
);
234 rect
.width
= GetColumn(idx
).GetWidth();
239 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx
)
241 if ( idx
!= COL_NONE
)
245 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx
)
247 wxRect rect
= GetClientRect();
248 const int ofs
= GetColStart(idx
);
255 // ----------------------------------------------------------------------------
256 // wxHeaderCtrl dragging/resizing/reordering
257 // ----------------------------------------------------------------------------
259 bool wxHeaderCtrl::IsResizing() const
261 return m_colBeingResized
!= COL_NONE
;
264 bool wxHeaderCtrl::IsReordering() const
266 return m_colBeingReordered
!= COL_NONE
;
269 void wxHeaderCtrl::ClearMarkers()
273 wxDCOverlay
dcover(m_overlay
, &dc
);
277 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical
)
281 wxDCOverlay
dcover(m_overlay
, &dc
);
284 // unfortunately drawing the marker over the parent window doesn't work as
285 // it's usually covered by another window (the main control view) so just
286 // draw the marker over the header itself, even if it makes it not very
288 dc
.SetPen(*wxLIGHT_GREY_PEN
);
289 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
292 void wxHeaderCtrl::EndDragging()
298 // don't use the special dragging cursor any more
299 SetCursor(wxNullCursor
);
302 void wxHeaderCtrl::CancelDragging()
304 wxASSERT_MSG( IsDragging(),
305 "shouldn't be called if we're not dragging anything" );
309 unsigned int& col
= IsResizing() ? m_colBeingResized
: m_colBeingReordered
;
311 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, GetId());
312 event
.SetEventObject(this);
313 event
.SetColumn(col
);
315 GetEventHandler()->ProcessEvent(event
);
320 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
322 const int xStart
= GetColStart(col
);
324 // notice that GetMinWidth() returns 0 if there is no minimal width so it
325 // still makes sense to use it even in this case
326 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
328 if ( xPhysical
< xMinEnd
)
331 return xPhysical
- xStart
;
334 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col
, int xPhysical
)
336 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
337 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
339 event
.SetEventObject(this);
340 event
.SetColumn(col
);
342 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
344 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
351 //else: nothing to do -- we just don't start to resize
353 else // go ahead with resizing
357 m_colBeingResized
= col
;
358 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
361 //else: we had already done the above when we started
363 UpdateResizingMarker(xPhysical
);
367 void wxHeaderCtrl::EndResizing(int xPhysical
)
369 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
375 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_RESIZE
, GetId());
376 event
.SetEventObject(this);
377 event
.SetColumn(m_colBeingResized
);
378 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
380 GetEventHandler()->ProcessEvent(event
);
382 m_colBeingResized
= COL_NONE
;
385 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical
)
389 wxDCOverlay
dcover(m_overlay
, &dc
);
393 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
395 // draw the phantom position of the column being dragged
396 int x
= xPhysical
- m_dragOffset
;
397 int y
= GetClientSize().y
;
398 dc
.DrawRectangle(x
, 0,
399 GetColumn(m_colBeingReordered
).GetWidth(), y
);
401 // and also a hint indicating where it is going to be inserted if it's
403 unsigned int col
= FindColumnAtPoint(xPhysical
);
404 if ( col
!= COL_NONE
)
406 static const int DROP_MARKER_WIDTH
= 4;
408 dc
.SetBrush(*wxBLUE
);
409 dc
.DrawRectangle(GetColEnd(col
) - DROP_MARKER_WIDTH
/2, 0,
410 DROP_MARKER_WIDTH
, y
);
414 void wxHeaderCtrl::StartReordering(unsigned int col
, int xPhysical
)
416 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_BEGIN_REORDER
, GetId());
417 event
.SetEventObject(this);
418 event
.SetColumn(col
);
420 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
422 // don't start dragging it, nothing to do otherwise
426 m_dragOffset
= xPhysical
- GetColStart(col
);
428 m_colBeingReordered
= col
;
429 SetCursor(wxCursor(wxCURSOR_HAND
));
432 UpdateReorderingMarker(xPhysical
);
435 void wxHeaderCtrl::EndReordering(int xPhysical
)
437 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
443 wxHeaderCtrlEvent
event(wxEVT_COMMAND_HEADER_END_REORDER
, GetId());
444 event
.SetEventObject(this);
445 event
.SetColumn(m_colBeingReordered
);
447 const unsigned pos
= GetColumnPos(FindColumnAtPoint(xPhysical
));
448 event
.SetNewOrder(pos
);
450 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
452 // do reorder the columns
453 DoMoveCol(m_colBeingReordered
, pos
);
456 m_colBeingReordered
= COL_NONE
;
459 // ----------------------------------------------------------------------------
460 // wxHeaderCtrl column reordering
461 // ----------------------------------------------------------------------------
463 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt
& order
)
465 m_colIndices
= order
;
469 wxArrayInt
wxHeaderCtrl::DoGetColumnsOrder() const
474 void wxHeaderCtrl::DoMoveCol(unsigned int idx
, unsigned int pos
)
476 const unsigned count
= m_colIndices
.size();
478 wxArrayInt colIndices
;
479 colIndices
.reserve(count
);
480 for ( unsigned n
= 0; n
< count
; n
++ )
482 // NB: order of checks is important for this to work when the new
483 // column position is the same as the old one
485 // insert the column at its new position
486 if ( colIndices
.size() == pos
)
487 colIndices
.push_back(idx
);
489 // delete the column from its old position
490 const unsigned idxOld
= m_colIndices
[n
];
494 colIndices
.push_back(idxOld
);
497 m_colIndices
= colIndices
;
502 // ----------------------------------------------------------------------------
503 // wxHeaderCtrl event handlers
504 // ----------------------------------------------------------------------------
506 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
507 EVT_PAINT(wxHeaderCtrl::OnPaint
)
509 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse
)
511 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost
)
513 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown
)
516 void wxHeaderCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
519 GetClientSize(&w
, &h
);
521 wxAutoBufferedPaintDC
dc(this);
523 dc
.SetBackground(GetBackgroundColour());
526 // account for the horizontal scrollbar offset in the parent window
527 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
529 const unsigned int count
= m_numColumns
;
531 for ( unsigned int i
= 0; i
< count
; i
++ )
533 const unsigned idx
= m_colIndices
[i
];
534 const wxHeaderColumnBase
& col
= GetColumn(idx
);
535 if ( col
.IsHidden() )
538 const int colWidth
= col
.GetWidth();
540 wxHeaderSortIconType sortArrow
;
541 if ( col
.IsSortKey() )
543 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
544 : wxHDR_SORT_ICON_DOWN
;
546 else // not sorting by this column
548 sortArrow
= wxHDR_SORT_ICON_NONE
;
554 if ( idx
== m_hover
)
555 state
= wxCONTROL_CURRENT
;
559 state
= wxCONTROL_DISABLED
;
562 wxHeaderButtonParams params
;
563 params
.m_labelText
= col
.GetTitle();
564 params
.m_labelBitmap
= col
.GetBitmap();
565 params
.m_labelAlignment
= col
.GetAlignment();
567 wxRendererNative::Get().DrawHeaderButton
571 wxRect(xpos
, 0, colWidth
, h
),
581 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
587 void wxHeaderCtrl::OnKeyDown(wxKeyEvent
& event
)
589 if ( event
.GetKeyCode() == WXK_ESCAPE
)
603 void wxHeaderCtrl::OnMouse(wxMouseEvent
& mevent
)
605 // do this in advance to allow simply returning if we're not interested,
606 // we'll undo it if we do handle the event below
610 // account for the control displacement
611 const int xPhysical
= mevent
.GetX();
612 const int xLogical
= xPhysical
- m_scrollOffset
;
614 // first deal with the [continuation of any] dragging operations in
618 if ( mevent
.LeftUp() )
619 EndResizing(xPhysical
);
620 else // update the live separator position
621 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
626 if ( IsReordering() )
628 if ( mevent
.LeftUp() )
629 EndReordering(xPhysical
);
630 else // update the column position
631 UpdateReorderingMarker(xPhysical
);
637 // find if the event is over a column at all
639 const unsigned col
= mevent
.Leaving()
640 ? (onSeparator
= false, COL_NONE
)
641 : FindColumnAtPoint(xLogical
, &onSeparator
);
644 // update the highlighted column if it changed
645 if ( col
!= m_hover
)
647 const unsigned hoverOld
= m_hover
;
650 RefreshColIfNotNone(hoverOld
);
651 RefreshColIfNotNone(m_hover
);
654 // update mouse cursor as it moves around
655 if ( mevent
.Moving() )
657 SetCursor(onSeparator
? wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
661 // all the other events only make sense when they happen over a column
662 if ( col
== COL_NONE
)
666 // enter various dragging modes on left mouse press
667 if ( mevent
.LeftDown() )
671 // start resizing the column
672 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
673 StartOrContinueResizing(col
, xPhysical
);
675 else // on column itself
677 // start dragging the column
678 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
680 StartReordering(col
, xPhysical
);
686 // determine the type of header event corresponding to click events
687 wxEventType evtType
= wxEVT_NULL
;
688 const bool click
= mevent
.ButtonUp(),
689 dblclk
= mevent
.ButtonDClick();
690 if ( click
|| dblclk
)
692 switch ( mevent
.GetButton() )
694 case wxMOUSE_BTN_LEFT
:
695 // treat left double clicks on separator specially
696 if ( onSeparator
&& dblclk
)
698 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
700 else // not double click on separator
702 evtType
= click
? wxEVT_COMMAND_HEADER_CLICK
703 : wxEVT_COMMAND_HEADER_DCLICK
;
707 case wxMOUSE_BTN_RIGHT
:
708 evtType
= click
? wxEVT_COMMAND_HEADER_RIGHT_CLICK
709 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
712 case wxMOUSE_BTN_MIDDLE
:
713 evtType
= click
? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
714 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
718 // ignore clicks from other mouse buttons
723 if ( evtType
== wxEVT_NULL
)
726 wxHeaderCtrlEvent
event(evtType
, GetId());
727 event
.SetEventObject(this);
728 event
.SetColumn(col
);
730 if ( GetEventHandler()->ProcessEvent(event
) )
734 #endif // wxHAS_GENERIC_HEADERCTRL