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()
61 m_colBeingResized
= COL_NONE
;
65 bool wxHeaderCtrl
::Create(wxWindow
*parent
,
72 if ( !wxHeaderCtrlBase
::Create(parent
, id
, pos
, size
,
73 style
, wxDefaultValidator
, name
) )
76 // tell the system to not paint the background at all to avoid flicker as
77 // we paint the entire window area in our OnPaint()
78 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
83 wxHeaderCtrl
::~wxHeaderCtrl()
87 // ----------------------------------------------------------------------------
88 // wxHeaderCtrl columns manipulation
89 // ----------------------------------------------------------------------------
91 void wxHeaderCtrl
::DoSetCount(unsigned int count
)
93 // update the column indices array if necessary
94 if ( count
> m_numColumns
)
96 // all new columns have default positions equal to their indices
97 for ( unsigned n
= m_numColumns
; n
< count
; n
++ )
98 m_colIndices
.push_back(n
);
100 else if ( count
< m_numColumns
)
102 // filter out all the positions which are invalid now while keeping the
103 // order of the remaining ones
104 wxArrayInt colIndices
;
105 for ( unsigned n
= 0; n
< m_numColumns
; n
++ )
107 const unsigned idx
= m_colIndices
[n
];
109 colIndices
.push_back(idx
);
112 wxASSERT_MSG( colIndices
.size() == count
, "logic error" );
114 m_colIndices
= colIndices
;
117 m_numColumns
= count
;
122 unsigned int wxHeaderCtrl
::DoGetCount() const
127 void wxHeaderCtrl
::DoUpdate(unsigned int idx
)
129 // we need to refresh not only this column but also the ones after it in
130 // case it was shown or hidden or its width changed -- it would be nice to
131 // avoid doing this unnecessary by storing the old column width (TODO)
132 RefreshColsAfter(idx
);
135 // ----------------------------------------------------------------------------
136 // wxHeaderCtrl scrolling
137 // ----------------------------------------------------------------------------
139 void wxHeaderCtrl
::DoScrollHorz(int dx
)
141 m_scrollOffset
+= dx
;
143 // don't call our own version which calls this function!
144 wxControl
::ScrollWindow(dx
, 0);
147 // ----------------------------------------------------------------------------
148 // wxHeaderCtrl geometry
149 // ----------------------------------------------------------------------------
151 wxSize wxHeaderCtrl
::DoGetBestSize() const
153 // the vertical size is rather arbitrary but it looks better if we leave
154 // some space around the text
155 return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4);
158 int wxHeaderCtrl
::GetColStart(unsigned int idx
) const
160 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
162 int pos
= m_scrollOffset
;
163 for ( unsigned n
= 0; n
< idx
; n
++ )
165 const wxHeaderColumnBase
& col
= self
->GetColumn(m_colIndices
[n
]);
167 pos
+= col
.GetWidth();
173 int wxHeaderCtrl
::FindColumnAtPoint(int x
, bool& onSeparator
) const
175 wxHeaderCtrl
* const self
= const_cast<wxHeaderCtrl
*>(this);
178 const unsigned count
= GetColumnCount();
179 for ( unsigned n
= 0; n
< count
; n
++ )
181 const wxHeaderColumnBase
& col
= self
->GetColumn(m_colIndices
[n
]);
182 if ( col
.IsHidden() )
185 pos
+= col
.GetWidth();
187 // if the column is resizeable, check if we're approximatively over the
188 // line separating it from the next column
190 // TODO: don't hardcode sensitivity
191 if ( col
.IsResizeable() && abs(x
- pos
) < 8 )
197 // inside this column?
201 return GetColumnAt(n
);
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
239 // ----------------------------------------------------------------------------
241 bool wxHeaderCtrl
::IsResizing() const
243 return m_colBeingResized
!= COL_NONE
;
246 void wxHeaderCtrl
::UpdateResizingMarker(int xPhysical
)
248 // unfortunately drawing the marker over the parent window doesn't work as
249 // it's usually covered by another window (the main control view) so just
250 // draw the marker over the header itself, even if it makes it not very
254 wxDCOverlay
dcover(m_overlay
, &dc
);
257 if ( xPhysical
!= -1 )
259 dc
.SetPen(*wxLIGHT_GREY_PEN
);
260 dc
.DrawLine(xPhysical
, 0, xPhysical
, GetClientSize().y
);
264 void wxHeaderCtrl
::EndDragging()
266 UpdateResizingMarker(-1);
270 // don't use the special dragging cursor any more
271 SetCursor(wxNullCursor
);
274 int wxHeaderCtrl
::ConstrainByMinWidth(unsigned int col
, int& xPhysical
)
276 const int xStart
= GetColStart(col
);
278 // notice that GetMinWidth() returns 0 if there is no minimal width so it
279 // still makes sense to use it even in this case
280 const int xMinEnd
= xStart
+ GetColumn(col
).GetMinWidth();
282 if ( xPhysical
< xMinEnd
)
285 return xPhysical
- xStart
;
288 void wxHeaderCtrl
::StartOrContinueResizing(unsigned int col
, int xPhysical
)
290 wxHeaderCtrlEvent
event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
291 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE
,
293 event
.SetEventObject(this);
294 event
.SetColumn(col
);
296 event
.SetWidth(ConstrainByMinWidth(col
, xPhysical
));
298 if ( GetEventHandler()->ProcessEvent(event
) && !event
.IsAllowed() )
305 //else: nothing to do -- we just don't start to resize
307 else // go ahead with resizing
311 m_colBeingResized
= col
;
312 SetCursor(wxCursor(wxCURSOR_SIZEWE
));
315 //else: we had already done the above when we started
317 UpdateResizingMarker(xPhysical
);
321 void wxHeaderCtrl
::EndResizing(int xPhysical
)
323 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
327 const bool cancelled
= xPhysical
== -1;
329 // if dragging was cancelled we must have already lost the mouse capture so
330 // don't try to release it
334 wxHeaderCtrlEvent
event(cancelled ? wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
335 : wxEVT_COMMAND_HEADER_END_RESIZE
,
337 event
.SetEventObject(this);
338 event
.SetColumn(m_colBeingResized
);
340 event
.SetWidth(ConstrainByMinWidth(m_colBeingResized
, xPhysical
));
342 GetEventHandler()->ProcessEvent(event
);
344 m_colBeingResized
= COL_NONE
;
347 // ----------------------------------------------------------------------------
348 // wxHeaderCtrl column reordering
349 // ----------------------------------------------------------------------------
351 void wxHeaderCtrl
::DoSetColumnsOrder(const wxArrayInt
& order
)
353 m_colIndices
= order
;
357 wxArrayInt wxHeaderCtrl
::DoGetColumnsOrder() const
362 // ----------------------------------------------------------------------------
363 // wxHeaderCtrl event handlers
364 // ----------------------------------------------------------------------------
366 BEGIN_EVENT_TABLE(wxHeaderCtrl
, wxHeaderCtrlBase
)
367 EVT_PAINT(wxHeaderCtrl
::OnPaint
)
369 EVT_MOUSE_EVENTS(wxHeaderCtrl
::OnMouse
)
371 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl
::OnCaptureLost
)
373 EVT_KEY_DOWN(wxHeaderCtrl
::OnKeyDown
)
376 void wxHeaderCtrl
::OnPaint(wxPaintEvent
& WXUNUSED(event
))
379 GetClientSize(&w
, &h
);
381 wxAutoBufferedPaintDC
dc(this);
383 dc
.SetBackground(GetBackgroundColour());
386 // account for the horizontal scrollbar offset in the parent window
387 dc
.SetDeviceOrigin(m_scrollOffset
, 0);
389 const unsigned int count
= m_numColumns
;
391 for ( unsigned int i
= 0; i
< count
; i
++ )
393 const wxHeaderColumnBase
& col
= GetColumn(m_colIndices
[i
]);
394 if ( col
.IsHidden() )
397 const int colWidth
= col
.GetWidth();
399 wxHeaderSortIconType sortArrow
;
400 if ( col
.IsSortKey() )
402 sortArrow
= col
.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
403 : wxHDR_SORT_ICON_DOWN
;
405 else // not sorting by this column
407 sortArrow
= wxHDR_SORT_ICON_NONE
;
414 state
= wxCONTROL_CURRENT
;
418 state
= wxCONTROL_DISABLED
;
421 wxHeaderButtonParams params
;
422 params
.m_labelText
= col
.GetTitle();
423 params
.m_labelBitmap
= col
.GetBitmap();
424 params
.m_labelAlignment
= col
.GetAlignment();
426 wxRendererNative
::Get().DrawHeaderButton
430 wxRect(xpos
, 0, colWidth
, h
),
440 void wxHeaderCtrl
::OnCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
446 void wxHeaderCtrl
::OnKeyDown(wxKeyEvent
& event
)
448 if ( IsResizing() && event
.GetKeyCode() == WXK_ESCAPE
)
459 void wxHeaderCtrl
::OnMouse(wxMouseEvent
& mevent
)
461 // do this in advance to allow simply returning if we're not interested,
462 // we'll undo it if we do handle the event below
466 // account for the control displacement
467 const int xPhysical
= mevent
.GetX();
468 const int xLogical
= xPhysical
- m_scrollOffset
;
470 // first deal with the [continuation of any] dragging operations in
474 if ( mevent
.LeftUp() )
475 EndResizing(xPhysical
);
476 else // update the live separator position
477 StartOrContinueResizing(m_colBeingResized
, xPhysical
);
483 // find if the event is over a column at all
485 const unsigned col
= mevent
.Leaving()
486 ?
(onSeparator
= false, COL_NONE
)
487 : FindColumnAtPoint(xLogical
, onSeparator
);
490 // update the highlighted column if it changed
491 if ( col
!= m_hover
)
493 const unsigned hoverOld
= m_hover
;
496 RefreshColIfNotNone(hoverOld
);
497 RefreshColIfNotNone(m_hover
);
500 // update mouse cursor as it moves around
501 if ( mevent
.Moving() )
503 SetCursor(onSeparator ?
wxCursor(wxCURSOR_SIZEWE
) : wxNullCursor
);
507 // all the other events only make sense when they happen over a column
508 if ( col
== COL_NONE
)
512 // enter various dragging modes on left mouse press
513 if ( mevent
.LeftDown() )
517 // start resizing the column
518 wxASSERT_MSG( !IsResizing(), "reentering resize mode?" );
519 StartOrContinueResizing(col
, xPhysical
);
521 else // on column itself
530 // determine the type of header event corresponding to click events
531 wxEventType evtType
= wxEVT_NULL
;
532 const bool click
= mevent
.ButtonUp(),
533 dblclk
= mevent
.ButtonDClick();
534 if ( click
|| dblclk
)
536 switch ( mevent
.GetButton() )
538 case wxMOUSE_BTN_LEFT
:
539 // treat left double clicks on separator specially
540 if ( onSeparator
&& dblclk
)
542 evtType
= wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
544 else // not double click on separator
546 evtType
= click ? wxEVT_COMMAND_HEADER_CLICK
547 : wxEVT_COMMAND_HEADER_DCLICK
;
551 case wxMOUSE_BTN_RIGHT
:
552 evtType
= click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK
553 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
556 case wxMOUSE_BTN_MIDDLE
:
557 evtType
= click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
558 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
562 // ignore clicks from other mouse buttons
567 if ( evtType
== wxEVT_NULL
)
570 wxHeaderCtrlEvent
event(evtType
, GetId());
571 event
.SetEventObject(this);
572 event
.SetColumn(col
);
574 if ( GetEventHandler()->ProcessEvent(event
) )
578 #endif // wxHAS_GENERIC_HEADERCTRL