]> git.saurik.com Git - wxWidgets.git/blob - src/common/headerctrlcmn.cpp
Added wxSHOW_EFFECT_NONE.
[wxWidgets.git] / src / common / headerctrlcmn.cpp
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
26 #if wxUSE_HEADERCTRL
27
28 #ifndef WX_PRECOMP
29 #include "wx/menu.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/headerctrl.h"
33 #include "wx/rearrangectrl.h"
34
35 namespace
36 {
37
38 // ----------------------------------------------------------------------------
39 // constants
40 // ----------------------------------------------------------------------------
41
42 const unsigned int wxNO_COLUMN = static_cast<unsigned>(-1);
43
44 // ----------------------------------------------------------------------------
45 // wxHeaderColumnsRearrangeDialog: dialog for customizing our columns
46 // ----------------------------------------------------------------------------
47
48 #if wxUSE_REARRANGECTRL
49
50 class wxHeaderColumnsRearrangeDialog : public wxRearrangeDialog
51 {
52 public:
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
68 #endif // wxUSE_REARRANGECTRL
69
70 } // anonymous namespace
71
72 // ============================================================================
73 // wxHeaderCtrlBase implementation
74 // ============================================================================
75
76 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr[] = "wxHeaderCtrl";
77
78 BEGIN_EVENT_TABLE(wxHeaderCtrlBase, wxControl)
79 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY, wxHeaderCtrlBase::OnSeparatorDClick)
80 #if wxUSE_MENUS
81 EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxHeaderCtrlBase::OnRClick)
82 #endif // wxUSE_MENUS
83 END_EVENT_TABLE()
84
85 void 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
100 void wxHeaderCtrlBase::SetColumnCount(unsigned int count)
101 {
102 if ( count != GetColumnCount() )
103 OnColumnCountChanging(count);
104
105 // still call DoSetCount() even if the count didn't really change in order
106 // to update all the columns
107 DoSetCount(count);
108 }
109
110 // ----------------------------------------------------------------------------
111 // wxHeaderCtrlBase event handling
112 // ----------------------------------------------------------------------------
113
114 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent& event)
115 {
116 const unsigned col = event.GetColumn();
117
118 int w = wxWindowBase::GetTextExtent(GetColumn(col).GetTitle()).x;
119 w += 4*GetCharWidth(); // add some arbitrary margins around text
120
121 if ( !UpdateColumnWidthToFit(col, w) )
122 event.Skip();
123 else
124 UpdateColumn(col);
125 }
126
127 #if wxUSE_MENUS
128
129 void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent& event)
130 {
131 if ( !HasFlag(wxHD_ALLOW_HIDE) )
132 {
133 event.Skip();
134 return;
135 }
136
137 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
138 }
139
140 #endif // wxUSE_MENUS
141
142 // ----------------------------------------------------------------------------
143 // wxHeaderCtrlBase column reordering
144 // ----------------------------------------------------------------------------
145
146 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt& order)
147 {
148 const unsigned count = GetColumnCount();
149 wxCHECK_RET( order.size() == count, "wrong number of columns" );
150
151 // check the array validity
152 wxArrayInt seen(count, 0);
153 for ( unsigned n = 0; n < count; n++ )
154 {
155 const unsigned idx = order[n];
156 wxCHECK_RET( idx < count, "invalid column index" );
157 wxCHECK_RET( !seen[idx], "duplicate column index" );
158
159 seen[idx] = 1;
160 }
161
162 DoSetColumnsOrder(order);
163
164 // TODO-RTL: do we need to reverse the array?
165 }
166
167 void wxHeaderCtrlBase::ResetColumnsOrder()
168 {
169 const unsigned count = GetColumnCount();
170 wxArrayInt order(count);
171 for ( unsigned n = 0; n < count; n++ )
172 order[n] = n;
173
174 DoSetColumnsOrder(order);
175 }
176
177 wxArrayInt wxHeaderCtrlBase::GetColumnsOrder() const
178 {
179 const wxArrayInt order = DoGetColumnsOrder();
180
181 wxASSERT_MSG( order.size() == GetColumnCount(), "invalid order array" );
182
183 return order;
184 }
185
186 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos) const
187 {
188 wxCHECK_MSG( pos < GetColumnCount(), wxNO_COLUMN, "invalid position" );
189
190 return GetColumnsOrder()[pos];
191 }
192
193 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const
194 {
195 const unsigned count = GetColumnCount();
196
197 wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" );
198
199 const wxArrayInt order = GetColumnsOrder();
200 for ( unsigned n = 0; n < count; n++ )
201 {
202 if ( (unsigned)order[n] == idx )
203 return n;
204 }
205
206 wxFAIL_MSG( "column unexpectedly not displayed at all" );
207
208 return wxNO_COLUMN;
209 }
210
211 /* static */
212 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt& order,
213 unsigned int idx,
214 unsigned int pos)
215 {
216 const unsigned count = order.size();
217
218 wxArrayInt orderNew;
219 orderNew.reserve(count);
220 for ( unsigned n = 0; ; n++ )
221 {
222 // NB: order of checks is important for this to work when the new
223 // column position is the same as the old one
224
225 // insert the column at its new position
226 if ( orderNew.size() == pos )
227 orderNew.push_back(idx);
228
229 if ( n == count )
230 break;
231
232 // delete the column from its old position
233 const unsigned idxOld = order[n];
234 if ( idxOld == idx )
235 continue;
236
237 orderNew.push_back(idxOld);
238 }
239
240 order.swap(orderNew);
241 }
242
243 void
244 wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt& colIndices, unsigned int count)
245 {
246 // update the column indices array if necessary
247 const unsigned countOld = colIndices.size();
248 if ( count > countOld )
249 {
250 // all new columns have default positions equal to their indices
251 for ( unsigned n = countOld; n < count; n++ )
252 colIndices.push_back(n);
253 }
254 else if ( count < countOld )
255 {
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++ )
261 {
262 const unsigned idx = colIndices[n];
263 if ( idx < count )
264 colIndicesNew.push_back(idx);
265 }
266
267 colIndices.swap(colIndicesNew);
268 }
269 //else: count didn't really change, nothing to do
270
271 wxASSERT_MSG( colIndices.size() == count, "logic error" );
272 }
273
274 // ----------------------------------------------------------------------------
275 // wxHeaderCtrl extra UI
276 // ----------------------------------------------------------------------------
277
278 #if wxUSE_MENUS
279
280 void wxHeaderCtrlBase::AddColumnsItems(wxMenu& menu, int idColumnsBase)
281 {
282 const unsigned count = GetColumnCount();
283 for ( unsigned n = 0; n < count; n++ )
284 {
285 const wxHeaderColumn& col = GetColumn(n);
286 menu.AppendCheckItem(idColumnsBase + n, col.GetTitle());
287 if ( col.IsShown() )
288 menu.Check(n, true);
289 }
290 }
291
292 bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint& pt, const wxString& title)
293 {
294 // construct the menu with the entries for all columns
295 wxMenu menu;
296 if ( !title.empty() )
297 menu.SetTitle(title);
298
299 AddColumnsItems(menu);
300
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) )
305 {
306 menu.AppendSeparator();
307 menu.Append(count, _("&Customize..."));
308 }
309
310 // do show the menu and get the user selection
311 const int rc = GetPopupMenuSelectionFromUser(menu, pt);
312 if ( rc == wxID_NONE )
313 return false;
314
315 if ( static_cast<unsigned>(rc) == count )
316 {
317 return ShowCustomizeDialog();
318 }
319 else // a column selected from the menu
320 {
321 UpdateColumnVisibility(rc, !GetColumn(rc).IsShown());
322 }
323
324 return true;
325 }
326
327 #endif // wxUSE_MENUS
328
329 bool wxHeaderCtrlBase::ShowCustomizeDialog()
330 {
331 #if wxUSE_REARRANGECTRL
332 // prepare the data for showing the dialog
333 wxArrayInt order = GetColumnsOrder();
334
335 const unsigned count = GetColumnCount();
336
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());
343
344 // this loop is however over positions and not indices
345 unsigned pos;
346 for ( pos = 0; pos < count; pos++ )
347 {
348 int& idx = order[pos];
349 if ( GetColumn(idx).IsHidden() )
350 {
351 // indicate that this one is hidden
352 idx = ~idx;
353 }
354 }
355
356 // do show it
357 wxHeaderColumnsRearrangeDialog dlg(this, order, titles);
358 if ( dlg.ShowModal() == wxID_OK )
359 {
360 // and apply the changes
361 order = dlg.GetOrder();
362 for ( pos = 0; pos < count; pos++ )
363 {
364 int& idx = order[pos];
365 const bool show = idx >= 0;
366 if ( !show )
367 {
368 // make all indices positive for passing them to SetColumnsOrder()
369 idx = ~idx;
370 }
371
372 if ( show != GetColumn(idx).IsShown() )
373 UpdateColumnVisibility(idx, show);
374 }
375
376 UpdateColumnsOrder(order);
377 SetColumnsOrder(order);
378
379 return true;
380 }
381 #endif // wxUSE_REARRANGECTRL
382
383 return false;
384 }
385
386 // ============================================================================
387 // wxHeaderCtrlSimple implementation
388 // ============================================================================
389
390 void wxHeaderCtrlSimple::Init()
391 {
392 m_sortKey = wxNO_COLUMN;
393 }
394
395 const wxHeaderColumn& wxHeaderCtrlSimple::GetColumn(unsigned int idx) const
396 {
397 return m_cols[idx];
398 }
399
400 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple& col, unsigned int idx)
401 {
402 m_cols.insert(m_cols.begin() + idx, col);
403
404 UpdateColumnCount();
405 }
406
407 void wxHeaderCtrlSimple::DoDelete(unsigned int idx)
408 {
409 m_cols.erase(m_cols.begin() + idx);
410 if ( idx == m_sortKey )
411 m_sortKey = wxNO_COLUMN;
412
413 UpdateColumnCount();
414 }
415
416 void wxHeaderCtrlSimple::DeleteAllColumns()
417 {
418 m_cols.clear();
419 m_sortKey = wxNO_COLUMN;
420
421 UpdateColumnCount();
422 }
423
424
425 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx, bool show)
426 {
427 if ( show != m_cols[idx].IsShown() )
428 {
429 m_cols[idx].SetHidden(!show);
430
431 UpdateColumn(idx);
432 }
433 }
434
435 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx, bool ascending)
436 {
437 RemoveSortIndicator();
438
439 m_cols[idx].SetAsSortKey(ascending);
440 m_sortKey = idx;
441
442 UpdateColumn(idx);
443 }
444
445 void wxHeaderCtrlSimple::RemoveSortIndicator()
446 {
447 if ( m_sortKey != wxNO_COLUMN )
448 {
449 const unsigned sortOld = m_sortKey;
450 m_sortKey = wxNO_COLUMN;
451
452 m_cols[sortOld].UnsetAsSortKey();
453
454 UpdateColumn(sortOld);
455 }
456 }
457
458 bool
459 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
460 {
461 const int widthContents = GetBestFittingWidth(idx);
462 if ( widthContents == -1 )
463 return false;
464
465 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
466
467 return true;
468 }
469
470 // ============================================================================
471 // wxHeaderCtrlEvent implementation
472 // ============================================================================
473
474 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent, wxNotifyEvent)
475
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);
479
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);
483
484 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK, wxHeaderCtrlEvent);
485
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);
489
490 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_BEGIN_REORDER, wxHeaderCtrlEvent);
491 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_END_REORDER, wxHeaderCtrlEvent);
492
493 wxDEFINE_EVENT( wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED, wxHeaderCtrlEvent);
494
495 #endif // wxUSE_HEADERCTRL