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"
32 #include "wx/headerctrl.h"
33 #include "wx/rearrangectrl.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 const unsigned int wxNO_COLUMN
= static_cast<unsigned>(-1);
44 // ----------------------------------------------------------------------------
45 // wxHeaderColumnsRearrangeDialog: dialog for customizing our columns
46 // ----------------------------------------------------------------------------
48 #if wxUSE_REARRANGECTRL
50 class wxHeaderColumnsRearrangeDialog
: public wxRearrangeDialog
53 wxHeaderColumnsRearrangeDialog(wxWindow
*parent
,
54 const wxArrayInt
& order
,
55 const wxArrayString
& items
)
59 _("Please select the columns to show and define their order:"),
60 _("Customize Columns"),
68 #endif // wxUSE_REARRANGECTRL
70 } // anonymous namespace
72 // ============================================================================
73 // wxHeaderCtrlBase implementation
74 // ============================================================================
76 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[] = "wxHeaderCtrl";
78 BEGIN_EVENT_TABLE(wxHeaderCtrlBase
, wxControl
)
79 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY
, wxHeaderCtrlBase::OnSeparatorDClick
)
81 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxHeaderCtrlBase::OnRClick
)
85 void wxHeaderCtrlBase::ScrollWindow(int dx
,
86 int WXUNUSED_UNLESS_DEBUG(dy
),
87 const wxRect
* WXUNUSED_UNLESS_DEBUG(rect
))
90 // this doesn't make sense at all
91 wxASSERT_MSG( !dy
, "header window can't be scrolled vertically" );
93 // this would actually be nice to support for "frozen" headers but it isn't
94 // supported currently
95 wxASSERT_MSG( !rect
, "header window can't be scrolled partially" );
100 void wxHeaderCtrlBase::SetColumnCount(unsigned int count
)
102 if ( count
!= GetColumnCount() )
103 OnColumnCountChanging(count
);
105 // still call DoSetCount() even if the count didn't really change in order
106 // to update all the columns
110 // ----------------------------------------------------------------------------
111 // wxHeaderCtrlBase event handling
112 // ----------------------------------------------------------------------------
114 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent
& event
)
116 const unsigned col
= event
.GetColumn();
118 int w
= wxWindowBase::GetTextExtent(GetColumn(col
).GetTitle()).x
;
119 w
+= 4*GetCharWidth(); // add some arbitrary margins around text
121 if ( !UpdateColumnWidthToFit(col
, w
) )
129 void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent
& event
)
131 if ( !HasFlag(wxHD_ALLOW_HIDE
) )
137 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
140 #endif // wxUSE_MENUS
142 // ----------------------------------------------------------------------------
143 // wxHeaderCtrlBase column reordering
144 // ----------------------------------------------------------------------------
146 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt
& order
)
148 const unsigned count
= GetColumnCount();
149 wxCHECK_RET( order
.size() == count
, "wrong number of columns" );
151 // check the array validity
152 wxArrayInt
seen(count
, 0);
153 for ( unsigned n
= 0; n
< count
; n
++ )
155 const unsigned idx
= order
[n
];
156 wxCHECK_RET( idx
< count
, "invalid column index" );
157 wxCHECK_RET( !seen
[idx
], "duplicate column index" );
162 DoSetColumnsOrder(order
);
164 // TODO-RTL: do we need to reverse the array?
167 void wxHeaderCtrlBase::ResetColumnsOrder()
169 const unsigned count
= GetColumnCount();
170 wxArrayInt
order(count
);
171 for ( unsigned n
= 0; n
< count
; n
++ )
174 DoSetColumnsOrder(order
);
177 wxArrayInt
wxHeaderCtrlBase::GetColumnsOrder() const
179 const wxArrayInt order
= DoGetColumnsOrder();
181 wxASSERT_MSG( order
.size() == GetColumnCount(), "invalid order array" );
186 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos
) const
188 wxCHECK_MSG( pos
< GetColumnCount(), wxNO_COLUMN
, "invalid position" );
190 return GetColumnsOrder()[pos
];
193 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx
) const
195 const unsigned count
= GetColumnCount();
197 wxCHECK_MSG( idx
< count
, wxNO_COLUMN
, "invalid index" );
199 const wxArrayInt order
= GetColumnsOrder();
200 for ( unsigned n
= 0; n
< count
; n
++ )
202 if ( (unsigned)order
[n
] == idx
)
206 wxFAIL_MSG( "column unexpectedly not displayed at all" );
212 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt
& order
,
216 const unsigned count
= order
.size();
219 orderNew
.reserve(count
);
220 for ( unsigned n
= 0; ; n
++ )
222 // NB: order of checks is important for this to work when the new
223 // column position is the same as the old one
225 // insert the column at its new position
226 if ( orderNew
.size() == pos
)
227 orderNew
.push_back(idx
);
232 // delete the column from its old position
233 const unsigned idxOld
= order
[n
];
237 orderNew
.push_back(idxOld
);
240 order
.swap(orderNew
);
244 wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
)
246 // update the column indices array if necessary
247 const unsigned countOld
= colIndices
.size();
248 if ( count
> countOld
)
250 // all new columns have default positions equal to their indices
251 for ( unsigned n
= countOld
; n
< count
; n
++ )
252 colIndices
.push_back(n
);
254 else if ( count
< countOld
)
256 // filter out all the positions which are invalid now while keeping the
257 // order of the remaining ones
258 wxArrayInt colIndicesNew
;
259 colIndicesNew
.reserve(count
);
260 for ( unsigned n
= 0; n
< countOld
; n
++ )
262 const unsigned idx
= colIndices
[n
];
264 colIndicesNew
.push_back(idx
);
267 colIndices
.swap(colIndicesNew
);
269 //else: count didn't really change, nothing to do
271 wxASSERT_MSG( colIndices
.size() == count
, "logic error" );
274 // ----------------------------------------------------------------------------
275 // wxHeaderCtrl extra UI
276 // ----------------------------------------------------------------------------
280 void wxHeaderCtrlBase::AddColumnsItems(wxMenu
& menu
, int idColumnsBase
)
282 const unsigned count
= GetColumnCount();
283 for ( unsigned n
= 0; n
< count
; n
++ )
285 const wxHeaderColumn
& col
= GetColumn(n
);
286 menu
.AppendCheckItem(idColumnsBase
+ n
, col
.GetTitle());
292 bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
)
294 // construct the menu with the entries for all columns
296 if ( !title
.empty() )
297 menu
.SetTitle(title
);
299 AddColumnsItems(menu
);
301 // ... and an extra one to show the customization dialog if the user is
302 // allowed to reorder the columns too
303 const unsigned count
= GetColumnCount();
304 if ( HasFlag(wxHD_ALLOW_REORDER
) )
306 menu
.AppendSeparator();
307 menu
.Append(count
, _("&Customize..."));
310 // do show the menu and get the user selection
311 const int rc
= GetPopupMenuSelectionFromUser(menu
, pt
);
312 if ( rc
== wxID_NONE
)
315 if ( static_cast<unsigned>(rc
) == count
)
317 return ShowCustomizeDialog();
319 else // a column selected from the menu
321 UpdateColumnVisibility(rc
, !GetColumn(rc
).IsShown());
327 #endif // wxUSE_MENUS
329 bool wxHeaderCtrlBase::ShowCustomizeDialog()
331 #if wxUSE_REARRANGECTRL
332 // prepare the data for showing the dialog
333 wxArrayInt order
= GetColumnsOrder();
335 const unsigned count
= GetColumnCount();
337 // notice that titles are always in the index order, they will be shown
338 // rearranged according to the display order in the dialog
339 wxArrayString titles
;
340 titles
.reserve(count
);
341 for ( unsigned n
= 0; n
< count
; n
++ )
342 titles
.push_back(GetColumn(n
).GetTitle());
344 // this loop is however over positions and not indices
346 for ( pos
= 0; pos
< count
; pos
++ )
348 int& idx
= order
[pos
];
349 if ( GetColumn(idx
).IsHidden() )
351 // indicate that this one is hidden
357 wxHeaderColumnsRearrangeDialog
dlg(this, order
, titles
);
358 if ( dlg
.ShowModal() == wxID_OK
)
360 // and apply the changes
361 order
= dlg
.GetOrder();
362 for ( pos
= 0; pos
< count
; pos
++ )
364 int& idx
= order
[pos
];
365 const bool show
= idx
>= 0;
368 // make all indices positive for passing them to SetColumnsOrder()
372 if ( show
!= GetColumn(idx
).IsShown() )
373 UpdateColumnVisibility(idx
, show
);
376 UpdateColumnsOrder(order
);
377 SetColumnsOrder(order
);
381 #endif // wxUSE_REARRANGECTRL
386 // ============================================================================
387 // wxHeaderCtrlSimple implementation
388 // ============================================================================
390 void wxHeaderCtrlSimple::Init()
392 m_sortKey
= wxNO_COLUMN
;
395 const wxHeaderColumn
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
) const
400 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
402 m_cols
.insert(m_cols
.begin() + idx
, col
);
407 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
409 m_cols
.erase(m_cols
.begin() + idx
);
410 if ( idx
== m_sortKey
)
411 m_sortKey
= wxNO_COLUMN
;
416 void wxHeaderCtrlSimple::DeleteAllColumns()
419 m_sortKey
= wxNO_COLUMN
;
425 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
427 if ( show
!= m_cols
[idx
].IsShown() )
429 m_cols
[idx
].SetHidden(!show
);
435 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
437 RemoveSortIndicator();
439 m_cols
[idx
].SetAsSortKey(ascending
);
445 void wxHeaderCtrlSimple::RemoveSortIndicator()
447 if ( m_sortKey
!= wxNO_COLUMN
)
449 const unsigned sortOld
= m_sortKey
;
450 m_sortKey
= wxNO_COLUMN
;
452 m_cols
[sortOld
].UnsetAsSortKey();
454 UpdateColumn(sortOld
);
459 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
461 const int widthContents
= GetBestFittingWidth(idx
);
462 if ( widthContents
== -1 )
465 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
470 // ============================================================================
471 // wxHeaderCtrlEvent implementation
472 // ============================================================================
474 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
476 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_CLICK
, wxHeaderCtrlEvent
);
477 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RIGHT_CLICK
, wxHeaderCtrlEvent
);
478 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_MIDDLE_CLICK
, wxHeaderCtrlEvent
);
480 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_DCLICK
, wxHeaderCtrlEvent
);
481 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RIGHT_DCLICK
, wxHeaderCtrlEvent
);
482 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
, wxHeaderCtrlEvent
);
484 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
, wxHeaderCtrlEvent
);
486 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_BEGIN_RESIZE
, wxHeaderCtrlEvent
);
487 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RESIZING
, wxHeaderCtrlEvent
);
488 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_END_RESIZE
, wxHeaderCtrlEvent
);
490 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_BEGIN_REORDER
, wxHeaderCtrlEvent
);
491 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_END_REORDER
, wxHeaderCtrlEvent
);
493 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, wxHeaderCtrlEvent
);
495 #endif // wxUSE_HEADERCTRL