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
)
74 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxHeaderCtrlBase::OnRClick
)
77 void wxHeaderCtrlBase::ScrollWindow(int dx
,
78 int WXUNUSED_UNLESS_DEBUG(dy
),
79 const wxRect
* WXUNUSED_UNLESS_DEBUG(rect
))
82 // this doesn't make sense at all
83 wxASSERT_MSG( !dy
, "header window can't be scrolled vertically" );
85 // this would actually be nice to support for "frozen" headers but it isn't
86 // supported currently
87 wxASSERT_MSG( !rect
, "header window can't be scrolled partially" );
92 void wxHeaderCtrlBase::SetColumnCount(unsigned int count
)
94 if ( count
!= GetColumnCount() )
95 OnColumnCountChanging(count
);
97 // still call DoSetCount() even if the count didn't really change in order
98 // to update all the columns
102 // ----------------------------------------------------------------------------
103 // wxHeaderCtrlBase event handling
104 // ----------------------------------------------------------------------------
106 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent
& event
)
108 const unsigned col
= event
.GetColumn();
110 int w
= wxWindowBase::GetTextExtent(GetColumn(col
).GetTitle()).x
;
111 w
+= 4*GetCharWidth(); // add some arbitrary margins around text
113 if ( !UpdateColumnWidthToFit(col
, w
) )
119 void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent
& event
)
121 if ( !HasFlag(wxHD_ALLOW_HIDE
) )
127 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
130 // ----------------------------------------------------------------------------
131 // wxHeaderCtrlBase column reordering
132 // ----------------------------------------------------------------------------
134 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt
& order
)
136 const unsigned count
= GetColumnCount();
137 wxCHECK_RET( order
.size() == count
, "wrong number of columns" );
139 // check the array validity
140 wxArrayInt
seen(count
, 0);
141 for ( unsigned n
= 0; n
< count
; n
++ )
143 const unsigned idx
= order
[n
];
144 wxCHECK_RET( idx
< count
, "invalid column index" );
145 wxCHECK_RET( !seen
[idx
], "duplicate column index" );
150 DoSetColumnsOrder(order
);
152 // TODO-RTL: do we need to reverse the array?
155 void wxHeaderCtrlBase::ResetColumnsOrder()
157 const unsigned count
= GetColumnCount();
158 wxArrayInt
order(count
);
159 for ( unsigned n
= 0; n
< count
; n
++ )
162 DoSetColumnsOrder(order
);
165 wxArrayInt
wxHeaderCtrlBase::GetColumnsOrder() const
167 const wxArrayInt order
= DoGetColumnsOrder();
169 wxASSERT_MSG( order
.size() == GetColumnCount(), "invalid order array" );
174 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos
) const
176 wxCHECK_MSG( pos
< GetColumnCount(), wxNO_COLUMN
, "invalid position" );
178 return GetColumnsOrder()[pos
];
181 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx
) const
183 const unsigned count
= GetColumnCount();
185 wxCHECK_MSG( idx
< count
, wxNO_COLUMN
, "invalid index" );
187 const wxArrayInt order
= GetColumnsOrder();
188 for ( unsigned n
= 0; n
< count
; n
++ )
190 if ( (unsigned)order
[n
] == idx
)
194 wxFAIL_MSG( "column unexpectedly not displayed at all" );
200 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt
& order
,
204 const unsigned count
= order
.size();
207 orderNew
.reserve(count
);
208 for ( unsigned n
= 0; ; n
++ )
210 // NB: order of checks is important for this to work when the new
211 // column position is the same as the old one
213 // insert the column at its new position
214 if ( orderNew
.size() == pos
)
215 orderNew
.push_back(idx
);
220 // delete the column from its old position
221 const unsigned idxOld
= order
[n
];
225 orderNew
.push_back(idxOld
);
228 order
.swap(orderNew
);
232 wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
)
234 // update the column indices array if necessary
235 const unsigned countOld
= colIndices
.size();
236 if ( count
> countOld
)
238 // all new columns have default positions equal to their indices
239 for ( unsigned n
= countOld
; n
< count
; n
++ )
240 colIndices
.push_back(n
);
242 else if ( count
< countOld
)
244 // filter out all the positions which are invalid now while keeping the
245 // order of the remaining ones
246 wxArrayInt colIndicesNew
;
247 colIndicesNew
.reserve(count
);
248 for ( unsigned n
= 0; n
< countOld
; n
++ )
250 const unsigned idx
= colIndices
[n
];
252 colIndicesNew
.push_back(idx
);
255 colIndices
.swap(colIndicesNew
);
257 //else: count didn't really change, nothing to do
259 wxASSERT_MSG( colIndices
.size() == count
, "logic error" );
262 // ----------------------------------------------------------------------------
263 // wxHeaderCtrl extra UI
264 // ----------------------------------------------------------------------------
266 void wxHeaderCtrlBase::AddColumnsItems(wxMenu
& menu
, int idColumnsBase
)
268 const unsigned count
= GetColumnCount();
269 for ( unsigned n
= 0; n
< count
; n
++ )
271 const wxHeaderColumn
& col
= GetColumn(n
);
272 menu
.AppendCheckItem(idColumnsBase
+ n
, col
.GetTitle());
278 bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
)
280 // construct the menu with the entries for all columns
282 if ( !title
.empty() )
283 menu
.SetTitle(title
);
285 AddColumnsItems(menu
);
287 // ... and an extra one to show the customization dialog if the user is
288 // allowed to reorder the columns too
289 const unsigned count
= GetColumnCount();
290 if ( HasFlag(wxHD_ALLOW_REORDER
) )
292 menu
.AppendSeparator();
293 menu
.Append(count
, _("&Customize..."));
296 // do show the menu and get the user selection
297 const int rc
= GetPopupMenuSelectionFromUser(menu
, pt
);
298 if ( rc
== wxID_NONE
)
301 if ( static_cast<unsigned>(rc
) == count
)
303 return ShowCustomizeDialog();
305 else // a column selected from the menu
307 UpdateColumnVisibility(rc
, !GetColumn(rc
).IsShown());
313 bool wxHeaderCtrlBase::ShowCustomizeDialog()
315 // prepare the data for showing the dialog
316 wxArrayInt order
= GetColumnsOrder();
318 const unsigned count
= GetColumnCount();
320 // notice that titles are always in the index order, they will be shown
321 // rearranged according to the display order in the dialog
322 wxArrayString titles
;
323 titles
.reserve(count
);
324 for ( unsigned n
= 0; n
< count
; n
++ )
325 titles
.push_back(GetColumn(n
).GetTitle());
327 // this loop is however over positions and not indices
329 for ( pos
= 0; pos
< count
; pos
++ )
331 int& idx
= order
[pos
];
332 if ( GetColumn(idx
).IsHidden() )
334 // indicate that this one is hidden
340 wxHeaderColumnsRearrangeDialog
dlg(this, order
, titles
);
341 if ( dlg
.ShowModal() != wxID_OK
)
344 // and apply the changes
345 order
= dlg
.GetOrder();
346 for ( pos
= 0; pos
< count
; pos
++ )
348 int& idx
= order
[pos
];
349 const bool show
= idx
>= 0;
352 // make all indices positive for passing them to SetColumnsOrder()
356 if ( show
!= GetColumn(idx
).IsShown() )
357 UpdateColumnVisibility(idx
, show
);
360 UpdateColumnsOrder(order
);
361 SetColumnsOrder(order
);
366 // ============================================================================
367 // wxHeaderCtrlSimple implementation
368 // ============================================================================
370 void wxHeaderCtrlSimple::Init()
372 m_sortKey
= wxNO_COLUMN
;
375 const wxHeaderColumn
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
) const
380 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
382 m_cols
.insert(m_cols
.begin() + idx
, col
);
387 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
389 m_cols
.erase(m_cols
.begin() + idx
);
390 if ( idx
== m_sortKey
)
391 m_sortKey
= wxNO_COLUMN
;
396 void wxHeaderCtrlSimple::DeleteAllColumns()
399 m_sortKey
= wxNO_COLUMN
;
405 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
407 if ( show
!= m_cols
[idx
].IsShown() )
409 m_cols
[idx
].SetHidden(!show
);
415 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
417 RemoveSortIndicator();
419 m_cols
[idx
].SetAsSortKey(ascending
);
425 void wxHeaderCtrlSimple::RemoveSortIndicator()
427 if ( m_sortKey
!= wxNO_COLUMN
)
429 const unsigned sortOld
= m_sortKey
;
430 m_sortKey
= wxNO_COLUMN
;
432 m_cols
[sortOld
].UnsetAsSortKey();
434 UpdateColumn(sortOld
);
439 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
441 const int widthContents
= GetBestFittingWidth(idx
);
442 if ( widthContents
== -1 )
445 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
450 // ============================================================================
451 // wxHeaderCtrlEvent implementation
452 // ============================================================================
454 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
456 const wxEventType wxEVT_COMMAND_HEADER_CLICK
= wxNewEventType();
457 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
= wxNewEventType();
458 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
= wxNewEventType();
460 const wxEventType wxEVT_COMMAND_HEADER_DCLICK
= wxNewEventType();
461 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
= wxNewEventType();
462 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
= wxNewEventType();
464 const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
= wxNewEventType();
466 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
= wxNewEventType();
467 const wxEventType wxEVT_COMMAND_HEADER_RESIZING
= wxNewEventType();
468 const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
= wxNewEventType();
470 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
= wxNewEventType();
471 const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
= wxNewEventType();
473 const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
= wxNewEventType();