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();
117 const wxHeaderColumn
& column
= GetColumn(col
);
119 if ( !column
.IsResizeable() )
125 int w
= wxWindowBase::GetTextExtent(column
.GetTitle()).x
;
126 w
+= 4*GetCharWidth(); // add some arbitrary margins around text
128 if ( !UpdateColumnWidthToFit(col
, w
) )
136 void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent
& event
)
138 if ( !HasFlag(wxHD_ALLOW_HIDE
) )
144 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
147 #endif // wxUSE_MENUS
149 // ----------------------------------------------------------------------------
150 // wxHeaderCtrlBase column reordering
151 // ----------------------------------------------------------------------------
153 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt
& order
)
155 const unsigned count
= GetColumnCount();
156 wxCHECK_RET( order
.size() == count
, "wrong number of columns" );
158 // check the array validity
159 wxArrayInt
seen(count
, 0);
160 for ( unsigned n
= 0; n
< count
; n
++ )
162 const unsigned idx
= order
[n
];
163 wxCHECK_RET( idx
< count
, "invalid column index" );
164 wxCHECK_RET( !seen
[idx
], "duplicate column index" );
169 DoSetColumnsOrder(order
);
171 // TODO-RTL: do we need to reverse the array?
174 void wxHeaderCtrlBase::ResetColumnsOrder()
176 const unsigned count
= GetColumnCount();
177 wxArrayInt
order(count
);
178 for ( unsigned n
= 0; n
< count
; n
++ )
181 DoSetColumnsOrder(order
);
184 wxArrayInt
wxHeaderCtrlBase::GetColumnsOrder() const
186 const wxArrayInt order
= DoGetColumnsOrder();
188 wxASSERT_MSG( order
.size() == GetColumnCount(), "invalid order array" );
193 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos
) const
195 wxCHECK_MSG( pos
< GetColumnCount(), wxNO_COLUMN
, "invalid position" );
197 return GetColumnsOrder()[pos
];
200 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx
) const
202 const unsigned count
= GetColumnCount();
204 wxCHECK_MSG( idx
< count
, wxNO_COLUMN
, "invalid index" );
206 const wxArrayInt order
= GetColumnsOrder();
207 for ( unsigned n
= 0; n
< count
; n
++ )
209 if ( (unsigned)order
[n
] == idx
)
213 wxFAIL_MSG( "column unexpectedly not displayed at all" );
219 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt
& order
,
223 const unsigned count
= order
.size();
226 orderNew
.reserve(count
);
227 for ( unsigned n
= 0; ; n
++ )
229 // NB: order of checks is important for this to work when the new
230 // column position is the same as the old one
232 // insert the column at its new position
233 if ( orderNew
.size() == pos
)
234 orderNew
.push_back(idx
);
239 // delete the column from its old position
240 const unsigned idxOld
= order
[n
];
244 orderNew
.push_back(idxOld
);
247 order
.swap(orderNew
);
251 wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
)
253 // update the column indices array if necessary
254 const unsigned countOld
= colIndices
.size();
255 if ( count
> countOld
)
257 // all new columns have default positions equal to their indices
258 for ( unsigned n
= countOld
; n
< count
; n
++ )
259 colIndices
.push_back(n
);
261 else if ( count
< countOld
)
263 // filter out all the positions which are invalid now while keeping the
264 // order of the remaining ones
265 wxArrayInt colIndicesNew
;
266 colIndicesNew
.reserve(count
);
267 for ( unsigned n
= 0; n
< countOld
; n
++ )
269 const unsigned idx
= colIndices
[n
];
271 colIndicesNew
.push_back(idx
);
274 colIndices
.swap(colIndicesNew
);
276 //else: count didn't really change, nothing to do
278 wxASSERT_MSG( colIndices
.size() == count
, "logic error" );
281 // ----------------------------------------------------------------------------
282 // wxHeaderCtrl extra UI
283 // ----------------------------------------------------------------------------
287 void wxHeaderCtrlBase::AddColumnsItems(wxMenu
& menu
, int idColumnsBase
)
289 const unsigned count
= GetColumnCount();
290 for ( unsigned n
= 0; n
< count
; n
++ )
292 const wxHeaderColumn
& col
= GetColumn(n
);
293 menu
.AppendCheckItem(idColumnsBase
+ n
, col
.GetTitle());
299 bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
)
301 // construct the menu with the entries for all columns
303 if ( !title
.empty() )
304 menu
.SetTitle(title
);
306 AddColumnsItems(menu
);
308 // ... and an extra one to show the customization dialog if the user is
309 // allowed to reorder the columns too
310 const unsigned count
= GetColumnCount();
311 if ( HasFlag(wxHD_ALLOW_REORDER
) )
313 menu
.AppendSeparator();
314 menu
.Append(count
, _("&Customize..."));
317 // do show the menu and get the user selection
318 const int rc
= GetPopupMenuSelectionFromUser(menu
, pt
);
319 if ( rc
== wxID_NONE
)
322 if ( static_cast<unsigned>(rc
) == count
)
324 return ShowCustomizeDialog();
326 else // a column selected from the menu
328 UpdateColumnVisibility(rc
, !GetColumn(rc
).IsShown());
334 #endif // wxUSE_MENUS
336 bool wxHeaderCtrlBase::ShowCustomizeDialog()
338 #if wxUSE_REARRANGECTRL
339 // prepare the data for showing the dialog
340 wxArrayInt order
= GetColumnsOrder();
342 const unsigned count
= GetColumnCount();
344 // notice that titles are always in the index order, they will be shown
345 // rearranged according to the display order in the dialog
346 wxArrayString titles
;
347 titles
.reserve(count
);
348 for ( unsigned n
= 0; n
< count
; n
++ )
349 titles
.push_back(GetColumn(n
).GetTitle());
351 // this loop is however over positions and not indices
353 for ( pos
= 0; pos
< count
; pos
++ )
355 int& idx
= order
[pos
];
356 if ( GetColumn(idx
).IsHidden() )
358 // indicate that this one is hidden
364 wxHeaderColumnsRearrangeDialog
dlg(this, order
, titles
);
365 if ( dlg
.ShowModal() == wxID_OK
)
367 // and apply the changes
368 order
= dlg
.GetOrder();
369 for ( pos
= 0; pos
< count
; pos
++ )
371 int& idx
= order
[pos
];
372 const bool show
= idx
>= 0;
375 // make all indices positive for passing them to SetColumnsOrder()
379 if ( show
!= GetColumn(idx
).IsShown() )
380 UpdateColumnVisibility(idx
, show
);
383 UpdateColumnsOrder(order
);
384 SetColumnsOrder(order
);
388 #endif // wxUSE_REARRANGECTRL
393 // ============================================================================
394 // wxHeaderCtrlSimple implementation
395 // ============================================================================
397 void wxHeaderCtrlSimple::Init()
399 m_sortKey
= wxNO_COLUMN
;
402 const wxHeaderColumn
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
) const
407 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
409 m_cols
.insert(m_cols
.begin() + idx
, col
);
414 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
416 m_cols
.erase(m_cols
.begin() + idx
);
417 if ( idx
== m_sortKey
)
418 m_sortKey
= wxNO_COLUMN
;
423 void wxHeaderCtrlSimple::DeleteAllColumns()
426 m_sortKey
= wxNO_COLUMN
;
432 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
434 if ( show
!= m_cols
[idx
].IsShown() )
436 m_cols
[idx
].SetHidden(!show
);
442 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
444 RemoveSortIndicator();
446 m_cols
[idx
].SetAsSortKey(ascending
);
452 void wxHeaderCtrlSimple::RemoveSortIndicator()
454 if ( m_sortKey
!= wxNO_COLUMN
)
456 const unsigned sortOld
= m_sortKey
;
457 m_sortKey
= wxNO_COLUMN
;
459 m_cols
[sortOld
].UnsetAsSortKey();
461 UpdateColumn(sortOld
);
466 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
468 const int widthContents
= GetBestFittingWidth(idx
);
469 if ( widthContents
== -1 )
472 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
477 // ============================================================================
478 // wxHeaderCtrlEvent implementation
479 // ============================================================================
481 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
483 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_CLICK
, wxHeaderCtrlEvent
);
484 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RIGHT_CLICK
, wxHeaderCtrlEvent
);
485 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_MIDDLE_CLICK
, wxHeaderCtrlEvent
);
487 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_DCLICK
, wxHeaderCtrlEvent
);
488 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RIGHT_DCLICK
, wxHeaderCtrlEvent
);
489 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
, wxHeaderCtrlEvent
);
491 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
, wxHeaderCtrlEvent
);
493 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_BEGIN_RESIZE
, wxHeaderCtrlEvent
);
494 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RESIZING
, wxHeaderCtrlEvent
);
495 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_END_RESIZE
, wxHeaderCtrlEvent
);
497 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_BEGIN_REORDER
, wxHeaderCtrlEvent
);
498 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_END_REORDER
, wxHeaderCtrlEvent
);
500 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, wxHeaderCtrlEvent
);
502 #endif // wxUSE_HEADERCTRL