]> git.saurik.com Git - wxWidgets.git/blame - src/common/headerctrlcmn.cpp
Set the correct svn properties on the new files. Correct a minor date typo.
[wxWidgets.git] / src / common / headerctrlcmn.cpp
CommitLineData
56873923
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/headerctrlcmn.cpp
3// Purpose: implementation of wxHeaderCtrlBase
4// Author: Vadim Zeitlin
5// Created: 2008-12-02
6// RCS-ID: $Id$
7// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
e721a2a2
VZ
26#if wxUSE_HEADERCTRL
27
56873923 28#ifndef WX_PRECOMP
c4789bf6 29 #include "wx/menu.h"
56873923
VZ
30#endif // WX_PRECOMP
31
32#include "wx/headerctrl.h"
af67f39d
VZ
33#include "wx/rearrangectrl.h"
34
35namespace
36{
56873923 37
e2bfe673
VZ
38// ----------------------------------------------------------------------------
39// constants
40// ----------------------------------------------------------------------------
41
e2bfe673
VZ
42const unsigned int wxNO_COLUMN = static_cast<unsigned>(-1);
43
af67f39d
VZ
44// ----------------------------------------------------------------------------
45// wxHeaderColumnsRearrangeDialog: dialog for customizing our columns
46// ----------------------------------------------------------------------------
47
a632b93c
VZ
48#if wxUSE_REARRANGECTRL
49
af67f39d
VZ
50class wxHeaderColumnsRearrangeDialog : public wxRearrangeDialog
51{
52public:
53 wxHeaderColumnsRearrangeDialog(wxWindow *parent,
54 const wxArrayInt& order,
55 const wxArrayString& items)
56 : wxRearrangeDialog
57 (
58 parent,
59 _("Please select the columns to show and define their order:"),
60 _("Customize Columns"),
61 order,
62 items
63 )
64 {
65 }
66};
67
a632b93c
VZ
68#endif // wxUSE_REARRANGECTRL
69
e2bfe673
VZ
70} // anonymous namespace
71
56873923
VZ
72// ============================================================================
73// wxHeaderCtrlBase implementation
74// ============================================================================
75
9a382f1f 76extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr[] = "wxHeaderCtrl";
56873923 77
3bfaa5a7
VZ
78BEGIN_EVENT_TABLE(wxHeaderCtrlBase, wxControl)
79 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY, wxHeaderCtrlBase::OnSeparatorDClick)
8a2e3f80 80#if wxUSE_MENUS
613de0e8 81 EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxHeaderCtrlBase::OnRClick)
8a2e3f80 82#endif // wxUSE_MENUS
3bfaa5a7
VZ
83END_EVENT_TABLE()
84
d8fc3398
VZ
85void wxHeaderCtrlBase::ScrollWindow(int dx,
86 int WXUNUSED_UNLESS_DEBUG(dy),
87 const wxRect * WXUNUSED_UNLESS_DEBUG(rect))
88
89{
90 // this doesn't make sense at all
91 wxASSERT_MSG( !dy, "header window can't be scrolled vertically" );
92
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" );
96
97 DoScrollHorz(dx);
98}
99
4635abac
VZ
100void wxHeaderCtrlBase::SetColumnCount(unsigned int count)
101{
613de0e8
VZ
102 if ( count != GetColumnCount() )
103 OnColumnCountChanging(count);
4635abac 104
613de0e8
VZ
105 // still call DoSetCount() even if the count didn't really change in order
106 // to update all the columns
4635abac
VZ
107 DoSetCount(count);
108}
109
702f5349
VZ
110// ----------------------------------------------------------------------------
111// wxHeaderCtrlBase event handling
112// ----------------------------------------------------------------------------
113
3bfaa5a7
VZ
114void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent& event)
115{
116 const unsigned col = event.GetColumn();
1acf670a 117 const wxHeaderColumn& column = GetColumn(col);
3bfaa5a7 118
1acf670a
VS
119 if ( !column.IsResizeable() )
120 {
121 event.Skip();
122 return;
123 }
124
125 int w = wxWindowBase::GetTextExtent(column.GetTitle()).x;
f458d4dd 126 w += 4*GetCharWidth(); // add some arbitrary margins around text
3bfaa5a7
VZ
127
128 if ( !UpdateColumnWidthToFit(col, w) )
129 event.Skip();
130 else
131 UpdateColumn(col);
132}
133
8a2e3f80 134#if wxUSE_MENUS
faef508f 135
613de0e8
VZ
136void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent& event)
137{
138 if ( !HasFlag(wxHD_ALLOW_HIDE) )
139 {
140 event.Skip();
141 return;
142 }
143
144 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
145}
146
8a2e3f80 147#endif // wxUSE_MENUS
faef508f 148
702f5349
VZ
149// ----------------------------------------------------------------------------
150// wxHeaderCtrlBase column reordering
151// ----------------------------------------------------------------------------
152
153void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt& order)
154{
155 const unsigned count = GetColumnCount();
156 wxCHECK_RET( order.size() == count, "wrong number of columns" );
157
158 // check the array validity
159 wxArrayInt seen(count, 0);
160 for ( unsigned n = 0; n < count; n++ )
161 {
162 const unsigned idx = order[n];
163 wxCHECK_RET( idx < count, "invalid column index" );
164 wxCHECK_RET( !seen[idx], "duplicate column index" );
165
166 seen[idx] = 1;
167 }
168
169 DoSetColumnsOrder(order);
170
171 // TODO-RTL: do we need to reverse the array?
172}
173
da5a641f
VZ
174void wxHeaderCtrlBase::ResetColumnsOrder()
175{
176 const unsigned count = GetColumnCount();
177 wxArrayInt order(count);
178 for ( unsigned n = 0; n < count; n++ )
179 order[n] = n;
180
181 DoSetColumnsOrder(order);
182}
183
702f5349
VZ
184wxArrayInt wxHeaderCtrlBase::GetColumnsOrder() const
185{
186 const wxArrayInt order = DoGetColumnsOrder();
187
188 wxASSERT_MSG( order.size() == GetColumnCount(), "invalid order array" );
189
190 return order;
191}
192
193unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos) const
194{
195 wxCHECK_MSG( pos < GetColumnCount(), wxNO_COLUMN, "invalid position" );
196
197 return GetColumnsOrder()[pos];
198}
199
200unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const
201{
202 const unsigned count = GetColumnCount();
203
204 wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" );
205
206 const wxArrayInt order = GetColumnsOrder();
207 for ( unsigned n = 0; n < count; n++ )
208 {
209 if ( (unsigned)order[n] == idx )
210 return n;
211 }
212
213 wxFAIL_MSG( "column unexpectedly not displayed at all" );
214
215 return wxNO_COLUMN;
216}
217
1bb74626
VZ
218/* static */
219void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt& order,
220 unsigned int idx,
221 unsigned int pos)
222{
223 const unsigned count = order.size();
224
225 wxArrayInt orderNew;
226 orderNew.reserve(count);
227 for ( unsigned n = 0; ; n++ )
228 {
229 // NB: order of checks is important for this to work when the new
230 // column position is the same as the old one
231
232 // insert the column at its new position
233 if ( orderNew.size() == pos )
234 orderNew.push_back(idx);
235
236 if ( n == count )
237 break;
238
239 // delete the column from its old position
240 const unsigned idxOld = order[n];
241 if ( idxOld == idx )
242 continue;
243
244 orderNew.push_back(idxOld);
245 }
246
247 order.swap(orderNew);
248}
249
f6655391
VZ
250void
251wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt& colIndices, unsigned int count)
252{
253 // update the column indices array if necessary
254 const unsigned countOld = colIndices.size();
255 if ( count > countOld )
256 {
257 // all new columns have default positions equal to their indices
258 for ( unsigned n = countOld; n < count; n++ )
259 colIndices.push_back(n);
260 }
261 else if ( count < countOld )
262 {
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++ )
268 {
269 const unsigned idx = colIndices[n];
270 if ( idx < count )
271 colIndicesNew.push_back(idx);
272 }
273
274 colIndices.swap(colIndicesNew);
275 }
613de0e8 276 //else: count didn't really change, nothing to do
f6655391
VZ
277
278 wxASSERT_MSG( colIndices.size() == count, "logic error" );
279}
280
e8f25dbb
VZ
281// ----------------------------------------------------------------------------
282// wxHeaderCtrl extra UI
283// ----------------------------------------------------------------------------
284
8a2e3f80 285#if wxUSE_MENUS
faef508f 286
ddd0db96 287void wxHeaderCtrlBase::AddColumnsItems(wxMenu& menu, int idColumnsBase)
e8f25dbb 288{
e8f25dbb
VZ
289 const unsigned count = GetColumnCount();
290 for ( unsigned n = 0; n < count; n++ )
291 {
292 const wxHeaderColumn& col = GetColumn(n);
ddd0db96 293 menu.AppendCheckItem(idColumnsBase + n, col.GetTitle());
e8f25dbb
VZ
294 if ( col.IsShown() )
295 menu.Check(n, true);
296 }
ddd0db96
VZ
297}
298
299bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint& pt, const wxString& title)
300{
301 // construct the menu with the entries for all columns
302 wxMenu menu;
303 if ( !title.empty() )
304 menu.SetTitle(title);
305
306 AddColumnsItems(menu);
e8f25dbb 307
613de0e8
VZ
308 // ... and an extra one to show the customization dialog if the user is
309 // allowed to reorder the columns too
ddd0db96 310 const unsigned count = GetColumnCount();
613de0e8
VZ
311 if ( HasFlag(wxHD_ALLOW_REORDER) )
312 {
313 menu.AppendSeparator();
314 menu.Append(count, _("&Customize..."));
315 }
316
317 // do show the menu and get the user selection
318 const int rc = GetPopupMenuSelectionFromUser(menu, pt);
319 if ( rc == wxID_NONE )
320 return false;
321
322 if ( static_cast<unsigned>(rc) == count )
323 {
324 return ShowCustomizeDialog();
325 }
326 else // a column selected from the menu
327 {
328 UpdateColumnVisibility(rc, !GetColumn(rc).IsShown());
329 }
330
331 return true;
332}
333
8a2e3f80 334#endif // wxUSE_MENUS
faef508f 335
613de0e8
VZ
336bool wxHeaderCtrlBase::ShowCustomizeDialog()
337{
f0bb342f 338#if wxUSE_REARRANGECTRL
af67f39d
VZ
339 // prepare the data for showing the dialog
340 wxArrayInt order = GetColumnsOrder();
341
342 const unsigned count = GetColumnCount();
343
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());
350
351 // this loop is however over positions and not indices
352 unsigned pos;
353 for ( pos = 0; pos < count; pos++ )
354 {
355 int& idx = order[pos];
356 if ( GetColumn(idx).IsHidden() )
357 {
358 // indicate that this one is hidden
359 idx = ~idx;
360 }
361 }
362
363 // do show it
364 wxHeaderColumnsRearrangeDialog dlg(this, order, titles);
f0bb342f 365 if ( dlg.ShowModal() == wxID_OK )
af67f39d 366 {
f0bb342f
VZ
367 // and apply the changes
368 order = dlg.GetOrder();
369 for ( pos = 0; pos < count; pos++ )
af67f39d 370 {
f0bb342f
VZ
371 int& idx = order[pos];
372 const bool show = idx >= 0;
373 if ( !show )
374 {
375 // make all indices positive for passing them to SetColumnsOrder()
376 idx = ~idx;
377 }
378
379 if ( show != GetColumn(idx).IsShown() )
380 UpdateColumnVisibility(idx, show);
af67f39d
VZ
381 }
382
f0bb342f
VZ
383 UpdateColumnsOrder(order);
384 SetColumnsOrder(order);
af67f39d 385
f0bb342f
VZ
386 return true;
387 }
388#endif // wxUSE_REARRANGECTRL
af67f39d 389
f0bb342f 390 return false;
e8f25dbb
VZ
391}
392
e2bfe673
VZ
393// ============================================================================
394// wxHeaderCtrlSimple implementation
395// ============================================================================
396
397void wxHeaderCtrlSimple::Init()
398{
399 m_sortKey = wxNO_COLUMN;
400}
401
482d06f8 402const wxHeaderColumn& wxHeaderCtrlSimple::GetColumn(unsigned int idx) const
e2bfe673
VZ
403{
404 return m_cols[idx];
405}
406
407void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple& col, unsigned int idx)
408{
409 m_cols.insert(m_cols.begin() + idx, col);
410
411 UpdateColumnCount();
412}
413
414void wxHeaderCtrlSimple::DoDelete(unsigned int idx)
415{
416 m_cols.erase(m_cols.begin() + idx);
417 if ( idx == m_sortKey )
418 m_sortKey = wxNO_COLUMN;
419
420 UpdateColumnCount();
421}
422
423void wxHeaderCtrlSimple::DeleteAllColumns()
424{
425 m_cols.clear();
426 m_sortKey = wxNO_COLUMN;
427
428 UpdateColumnCount();
429}
430
431
432void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx, bool show)
433{
434 if ( show != m_cols[idx].IsShown() )
435 {
436 m_cols[idx].SetHidden(!show);
437
438 UpdateColumn(idx);
439 }
440}
441
442void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx, bool ascending)
443{
444 RemoveSortIndicator();
445
446 m_cols[idx].SetAsSortKey(ascending);
447 m_sortKey = idx;
448
449 UpdateColumn(idx);
450}
451
452void wxHeaderCtrlSimple::RemoveSortIndicator()
453{
454 if ( m_sortKey != wxNO_COLUMN )
455 {
456 const unsigned sortOld = m_sortKey;
457 m_sortKey = wxNO_COLUMN;
458
459 m_cols[sortOld].UnsetAsSortKey();
460
461 UpdateColumn(sortOld);
462 }
463}
464
e5a16353
VZ
465bool
466wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
467{
468 const int widthContents = GetBestFittingWidth(idx);
469 if ( widthContents == -1 )
470 return false;
471
472 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
e5a16353
VZ
473
474 return true;
475}
476
fa3d4aaf
VZ
477// ============================================================================
478// wxHeaderCtrlEvent implementation
479// ============================================================================
480
481IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent, wxNotifyEvent)
482
3c778901
VZ
483wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_CLICK, wxHeaderCtrlEvent);
484wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RIGHT_CLICK, wxHeaderCtrlEvent);
485wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_MIDDLE_CLICK, wxHeaderCtrlEvent);
fa3d4aaf 486
3c778901
VZ
487wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_DCLICK, wxHeaderCtrlEvent);
488wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RIGHT_DCLICK, wxHeaderCtrlEvent);
489wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_MIDDLE_DCLICK, wxHeaderCtrlEvent);
3bfaa5a7 490
3c778901 491wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK, wxHeaderCtrlEvent);
aef252d9 492
3c778901
VZ
493wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_BEGIN_RESIZE, wxHeaderCtrlEvent);
494wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_RESIZING, wxHeaderCtrlEvent);
495wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_END_RESIZE, wxHeaderCtrlEvent);
702f5349 496
3c778901
VZ
497wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_BEGIN_REORDER, wxHeaderCtrlEvent);
498wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_END_REORDER, wxHeaderCtrlEvent);
565804f2 499
3c778901 500wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED, wxHeaderCtrlEvent);
e721a2a2
VZ
501
502#endif // wxUSE_HEADERCTRL