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"
30 #include "wx/headerctrl.h"
31 #include "wx/rearrangectrl.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 const unsigned int wxNO_COLUMN
= static_cast<unsigned>(-1);
42 // ----------------------------------------------------------------------------
43 // wxHeaderColumnsRearrangeDialog: dialog for customizing our columns
44 // ----------------------------------------------------------------------------
46 class wxHeaderColumnsRearrangeDialog
: public wxRearrangeDialog
49 wxHeaderColumnsRearrangeDialog(wxWindow
*parent
,
50 const wxArrayInt
& order
,
51 const wxArrayString
& items
)
55 _("Please select the columns to show and define their order:"),
56 _("Customize Columns"),
64 } // anonymous namespace
66 // ============================================================================
67 // wxHeaderCtrlBase implementation
68 // ============================================================================
70 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[] = "wxHeaderCtrl";
72 BEGIN_EVENT_TABLE(wxHeaderCtrlBase
, wxControl
)
73 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY
, wxHeaderCtrlBase::OnSeparatorDClick
)
75 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxHeaderCtrlBase::OnRClick
)
79 void wxHeaderCtrlBase::ScrollWindow(int dx
,
80 int WXUNUSED_UNLESS_DEBUG(dy
),
81 const wxRect
* WXUNUSED_UNLESS_DEBUG(rect
))
84 // this doesn't make sense at all
85 wxASSERT_MSG( !dy
, "header window can't be scrolled vertically" );
87 // this would actually be nice to support for "frozen" headers but it isn't
88 // supported currently
89 wxASSERT_MSG( !rect
, "header window can't be scrolled partially" );
94 void wxHeaderCtrlBase::SetColumnCount(unsigned int count
)
96 if ( count
!= GetColumnCount() )
97 OnColumnCountChanging(count
);
99 // still call DoSetCount() even if the count didn't really change in order
100 // to update all the columns
104 // ----------------------------------------------------------------------------
105 // wxHeaderCtrlBase event handling
106 // ----------------------------------------------------------------------------
108 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent
& event
)
110 const unsigned col
= event
.GetColumn();
112 int w
= wxWindowBase::GetTextExtent(GetColumn(col
).GetTitle()).x
;
113 w
+= 4*GetCharWidth(); // add some arbitrary margins around text
115 if ( !UpdateColumnWidthToFit(col
, w
) )
123 void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent
& event
)
125 if ( !HasFlag(wxHD_ALLOW_HIDE
) )
131 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
136 // ----------------------------------------------------------------------------
137 // wxHeaderCtrlBase column reordering
138 // ----------------------------------------------------------------------------
140 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt
& order
)
142 const unsigned count
= GetColumnCount();
143 wxCHECK_RET( order
.size() == count
, "wrong number of columns" );
145 // check the array validity
146 wxArrayInt
seen(count
, 0);
147 for ( unsigned n
= 0; n
< count
; n
++ )
149 const unsigned idx
= order
[n
];
150 wxCHECK_RET( idx
< count
, "invalid column index" );
151 wxCHECK_RET( !seen
[idx
], "duplicate column index" );
156 DoSetColumnsOrder(order
);
158 // TODO-RTL: do we need to reverse the array?
161 void wxHeaderCtrlBase::ResetColumnsOrder()
163 const unsigned count
= GetColumnCount();
164 wxArrayInt
order(count
);
165 for ( unsigned n
= 0; n
< count
; n
++ )
168 DoSetColumnsOrder(order
);
171 wxArrayInt
wxHeaderCtrlBase::GetColumnsOrder() const
173 const wxArrayInt order
= DoGetColumnsOrder();
175 wxASSERT_MSG( order
.size() == GetColumnCount(), "invalid order array" );
180 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos
) const
182 wxCHECK_MSG( pos
< GetColumnCount(), wxNO_COLUMN
, "invalid position" );
184 return GetColumnsOrder()[pos
];
187 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx
) const
189 const unsigned count
= GetColumnCount();
191 wxCHECK_MSG( idx
< count
, wxNO_COLUMN
, "invalid index" );
193 const wxArrayInt order
= GetColumnsOrder();
194 for ( unsigned n
= 0; n
< count
; n
++ )
196 if ( (unsigned)order
[n
] == idx
)
200 wxFAIL_MSG( "column unexpectedly not displayed at all" );
206 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt
& order
,
210 const unsigned count
= order
.size();
213 orderNew
.reserve(count
);
214 for ( unsigned n
= 0; ; n
++ )
216 // NB: order of checks is important for this to work when the new
217 // column position is the same as the old one
219 // insert the column at its new position
220 if ( orderNew
.size() == pos
)
221 orderNew
.push_back(idx
);
226 // delete the column from its old position
227 const unsigned idxOld
= order
[n
];
231 orderNew
.push_back(idxOld
);
234 order
.swap(orderNew
);
238 wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
)
240 // update the column indices array if necessary
241 const unsigned countOld
= colIndices
.size();
242 if ( count
> countOld
)
244 // all new columns have default positions equal to their indices
245 for ( unsigned n
= countOld
; n
< count
; n
++ )
246 colIndices
.push_back(n
);
248 else if ( count
< countOld
)
250 // filter out all the positions which are invalid now while keeping the
251 // order of the remaining ones
252 wxArrayInt colIndicesNew
;
253 colIndicesNew
.reserve(count
);
254 for ( unsigned n
= 0; n
< countOld
; n
++ )
256 const unsigned idx
= colIndices
[n
];
258 colIndicesNew
.push_back(idx
);
261 colIndices
.swap(colIndicesNew
);
263 //else: count didn't really change, nothing to do
265 wxASSERT_MSG( colIndices
.size() == count
, "logic error" );
268 // ----------------------------------------------------------------------------
269 // wxHeaderCtrl extra UI
270 // ----------------------------------------------------------------------------
274 void wxHeaderCtrlBase::AddColumnsItems(wxMenu
& menu
, int idColumnsBase
)
276 const unsigned count
= GetColumnCount();
277 for ( unsigned n
= 0; n
< count
; n
++ )
279 const wxHeaderColumn
& col
= GetColumn(n
);
280 menu
.AppendCheckItem(idColumnsBase
+ n
, col
.GetTitle());
286 bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
)
288 // construct the menu with the entries for all columns
290 if ( !title
.empty() )
291 menu
.SetTitle(title
);
293 AddColumnsItems(menu
);
295 // ... and an extra one to show the customization dialog if the user is
296 // allowed to reorder the columns too
297 const unsigned count
= GetColumnCount();
298 if ( HasFlag(wxHD_ALLOW_REORDER
) )
300 menu
.AppendSeparator();
301 menu
.Append(count
, _("&Customize..."));
304 // do show the menu and get the user selection
305 const int rc
= GetPopupMenuSelectionFromUser(menu
, pt
);
306 if ( rc
== wxID_NONE
)
309 if ( static_cast<unsigned>(rc
) == count
)
311 return ShowCustomizeDialog();
313 else // a column selected from the menu
315 UpdateColumnVisibility(rc
, !GetColumn(rc
).IsShown());
323 bool wxHeaderCtrlBase::ShowCustomizeDialog()
325 #if wxUSE_REARRANGECTRL
326 // prepare the data for showing the dialog
327 wxArrayInt order
= GetColumnsOrder();
329 const unsigned count
= GetColumnCount();
331 // notice that titles are always in the index order, they will be shown
332 // rearranged according to the display order in the dialog
333 wxArrayString titles
;
334 titles
.reserve(count
);
335 for ( unsigned n
= 0; n
< count
; n
++ )
336 titles
.push_back(GetColumn(n
).GetTitle());
338 // this loop is however over positions and not indices
340 for ( pos
= 0; pos
< count
; pos
++ )
342 int& idx
= order
[pos
];
343 if ( GetColumn(idx
).IsHidden() )
345 // indicate that this one is hidden
351 wxHeaderColumnsRearrangeDialog
dlg(this, order
, titles
);
352 if ( dlg
.ShowModal() == wxID_OK
)
354 // and apply the changes
355 order
= dlg
.GetOrder();
356 for ( pos
= 0; pos
< count
; pos
++ )
358 int& idx
= order
[pos
];
359 const bool show
= idx
>= 0;
362 // make all indices positive for passing them to SetColumnsOrder()
366 if ( show
!= GetColumn(idx
).IsShown() )
367 UpdateColumnVisibility(idx
, show
);
370 UpdateColumnsOrder(order
);
371 SetColumnsOrder(order
);
375 #endif // wxUSE_REARRANGECTRL
380 // ============================================================================
381 // wxHeaderCtrlSimple implementation
382 // ============================================================================
384 void wxHeaderCtrlSimple::Init()
386 m_sortKey
= wxNO_COLUMN
;
389 const wxHeaderColumn
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
) const
394 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
396 m_cols
.insert(m_cols
.begin() + idx
, col
);
401 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
403 m_cols
.erase(m_cols
.begin() + idx
);
404 if ( idx
== m_sortKey
)
405 m_sortKey
= wxNO_COLUMN
;
410 void wxHeaderCtrlSimple::DeleteAllColumns()
413 m_sortKey
= wxNO_COLUMN
;
419 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
421 if ( show
!= m_cols
[idx
].IsShown() )
423 m_cols
[idx
].SetHidden(!show
);
429 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
431 RemoveSortIndicator();
433 m_cols
[idx
].SetAsSortKey(ascending
);
439 void wxHeaderCtrlSimple::RemoveSortIndicator()
441 if ( m_sortKey
!= wxNO_COLUMN
)
443 const unsigned sortOld
= m_sortKey
;
444 m_sortKey
= wxNO_COLUMN
;
446 m_cols
[sortOld
].UnsetAsSortKey();
448 UpdateColumn(sortOld
);
453 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
455 const int widthContents
= GetBestFittingWidth(idx
);
456 if ( widthContents
== -1 )
459 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
464 // ============================================================================
465 // wxHeaderCtrlEvent implementation
466 // ============================================================================
468 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
470 const wxEventType wxEVT_COMMAND_HEADER_CLICK
= wxNewEventType();
471 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
= wxNewEventType();
472 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
= wxNewEventType();
474 const wxEventType wxEVT_COMMAND_HEADER_DCLICK
= wxNewEventType();
475 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
= wxNewEventType();
476 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
= wxNewEventType();
478 const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
= wxNewEventType();
480 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
= wxNewEventType();
481 const wxEventType wxEVT_COMMAND_HEADER_RESIZING
= wxNewEventType();
482 const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
= wxNewEventType();
484 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
= wxNewEventType();
485 const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
= wxNewEventType();
487 const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
= wxNewEventType();