1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/headerctrlcmn.cpp
3 // Purpose: implementation of wxHeaderCtrlBase
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 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
38 const unsigned int wxNO_COLUMN
= static_cast<unsigned>(-1);
40 } // anonymous namespace
42 // ============================================================================
43 // wxHeaderCtrlBase implementation
44 // ============================================================================
46 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[] = "wxHeaderCtrl";
48 BEGIN_EVENT_TABLE(wxHeaderCtrlBase
, wxControl
)
49 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY
, wxHeaderCtrlBase::OnSeparatorDClick
)
52 void wxHeaderCtrlBase::ScrollWindow(int dx
,
53 int WXUNUSED_UNLESS_DEBUG(dy
),
54 const wxRect
* WXUNUSED_UNLESS_DEBUG(rect
))
57 // this doesn't make sense at all
58 wxASSERT_MSG( !dy
, "header window can't be scrolled vertically" );
60 // this would actually be nice to support for "frozen" headers but it isn't
61 // supported currently
62 wxASSERT_MSG( !rect
, "header window can't be scrolled partially" );
67 void wxHeaderCtrlBase::SetColumnCount(unsigned int count
)
69 OnColumnCountChanging(count
);
74 // ----------------------------------------------------------------------------
75 // wxHeaderCtrlBase event handling
76 // ----------------------------------------------------------------------------
78 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent
& event
)
80 const unsigned col
= event
.GetColumn();
82 int w
= wxWindowBase::GetTextExtent(GetColumn(col
).GetTitle()).x
;
83 w
+= 4*GetCharWidth(); // add some arbitrary margins around text
85 if ( !UpdateColumnWidthToFit(col
, w
) )
91 // ----------------------------------------------------------------------------
92 // wxHeaderCtrlBase column reordering
93 // ----------------------------------------------------------------------------
95 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt
& order
)
97 const unsigned count
= GetColumnCount();
98 wxCHECK_RET( order
.size() == count
, "wrong number of columns" );
100 // check the array validity
101 wxArrayInt
seen(count
, 0);
102 for ( unsigned n
= 0; n
< count
; n
++ )
104 const unsigned idx
= order
[n
];
105 wxCHECK_RET( idx
< count
, "invalid column index" );
106 wxCHECK_RET( !seen
[idx
], "duplicate column index" );
111 DoSetColumnsOrder(order
);
113 // TODO-RTL: do we need to reverse the array?
116 wxArrayInt
wxHeaderCtrlBase::GetColumnsOrder() const
118 const wxArrayInt order
= DoGetColumnsOrder();
120 wxASSERT_MSG( order
.size() == GetColumnCount(), "invalid order array" );
125 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos
) const
127 wxCHECK_MSG( pos
< GetColumnCount(), wxNO_COLUMN
, "invalid position" );
129 return GetColumnsOrder()[pos
];
132 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx
) const
134 const unsigned count
= GetColumnCount();
136 wxCHECK_MSG( idx
< count
, wxNO_COLUMN
, "invalid index" );
138 const wxArrayInt order
= GetColumnsOrder();
139 for ( unsigned n
= 0; n
< count
; n
++ )
141 if ( (unsigned)order
[n
] == idx
)
145 wxFAIL_MSG( "column unexpectedly not displayed at all" );
151 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt
& order
,
155 const unsigned count
= order
.size();
158 orderNew
.reserve(count
);
159 for ( unsigned n
= 0; ; n
++ )
161 // NB: order of checks is important for this to work when the new
162 // column position is the same as the old one
164 // insert the column at its new position
165 if ( orderNew
.size() == pos
)
166 orderNew
.push_back(idx
);
171 // delete the column from its old position
172 const unsigned idxOld
= order
[n
];
176 orderNew
.push_back(idxOld
);
179 order
.swap(orderNew
);
182 // ============================================================================
183 // wxHeaderCtrlSimple implementation
184 // ============================================================================
186 void wxHeaderCtrlSimple::Init()
188 m_sortKey
= wxNO_COLUMN
;
191 wxHeaderColumn
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
)
196 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
198 m_cols
.insert(m_cols
.begin() + idx
, col
);
203 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
205 m_cols
.erase(m_cols
.begin() + idx
);
206 if ( idx
== m_sortKey
)
207 m_sortKey
= wxNO_COLUMN
;
212 void wxHeaderCtrlSimple::DeleteAllColumns()
215 m_sortKey
= wxNO_COLUMN
;
221 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
223 if ( show
!= m_cols
[idx
].IsShown() )
225 m_cols
[idx
].SetHidden(!show
);
231 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
233 RemoveSortIndicator();
235 m_cols
[idx
].SetAsSortKey(ascending
);
241 void wxHeaderCtrlSimple::RemoveSortIndicator()
243 if ( m_sortKey
!= wxNO_COLUMN
)
245 const unsigned sortOld
= m_sortKey
;
246 m_sortKey
= wxNO_COLUMN
;
248 m_cols
[sortOld
].UnsetAsSortKey();
250 UpdateColumn(sortOld
);
255 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
257 const int widthContents
= GetBestFittingWidth(idx
);
258 if ( widthContents
== -1 )
261 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
266 // ============================================================================
267 // wxHeaderCtrlEvent implementation
268 // ============================================================================
270 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
272 const wxEventType wxEVT_COMMAND_HEADER_CLICK
= wxNewEventType();
273 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
= wxNewEventType();
274 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
= wxNewEventType();
276 const wxEventType wxEVT_COMMAND_HEADER_DCLICK
= wxNewEventType();
277 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
= wxNewEventType();
278 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
= wxNewEventType();
280 const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
= wxNewEventType();
282 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
= wxNewEventType();
283 const wxEventType wxEVT_COMMAND_HEADER_RESIZING
= wxNewEventType();
284 const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
= wxNewEventType();
286 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
= wxNewEventType();
287 const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
= wxNewEventType();
289 const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
= wxNewEventType();