]> git.saurik.com Git - wxWidgets.git/blob - src/common/headerctrlcmn.cpp
Disable handling of wxEVT_MOUSEWHEEL in wxVarScrollHelperEvtHandler in wxGTK.
[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 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_HEADERCTRL
26
27 #ifndef WX_PRECOMP
28 #include "wx/menu.h"
29 #endif // WX_PRECOMP
30
31 #include "wx/headerctrl.h"
32 #include "wx/rearrangectrl.h"
33 #include "wx/renderer.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 int wxHeaderCtrlBase::GetColumnTitleWidth(const wxHeaderColumn& col)
111 {
112 int w = wxWindowBase::GetTextExtent(col.GetTitle()).x;
113
114 // add some margin:
115 w += wxRendererNative::Get().GetHeaderButtonMargin(this);
116
117 // if a bitmap is used, add space for it and 2px border:
118 wxBitmap bmp = col.GetBitmap();
119 if ( bmp.IsOk() )
120 w += bmp.GetWidth() + 2;
121
122 return w;
123 }
124
125 // ----------------------------------------------------------------------------
126 // wxHeaderCtrlBase event handling
127 // ----------------------------------------------------------------------------
128
129 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent& event)
130 {
131 const unsigned col = event.GetColumn();
132 const wxHeaderColumn& column = GetColumn(col);
133
134 if ( !column.IsResizeable() )
135 {
136 event.Skip();
137 return;
138 }
139
140 int w = GetColumnTitleWidth(column);
141
142 if ( !UpdateColumnWidthToFit(col, w) )
143 event.Skip();
144 else
145 UpdateColumn(col);
146 }
147
148 #if wxUSE_MENUS
149
150 void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent& event)
151 {
152 if ( !HasFlag(wxHD_ALLOW_HIDE) )
153 {
154 event.Skip();
155 return;
156 }
157
158 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
159 }
160
161 #endif // wxUSE_MENUS
162
163 // ----------------------------------------------------------------------------
164 // wxHeaderCtrlBase column reordering
165 // ----------------------------------------------------------------------------
166
167 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt& order)
168 {
169 const unsigned count = GetColumnCount();
170 wxCHECK_RET( order.size() == count, "wrong number of columns" );
171
172 // check the array validity
173 wxArrayInt seen(count, 0);
174 for ( unsigned n = 0; n < count; n++ )
175 {
176 const unsigned idx = order[n];
177 wxCHECK_RET( idx < count, "invalid column index" );
178 wxCHECK_RET( !seen[idx], "duplicate column index" );
179
180 seen[idx] = 1;
181 }
182
183 DoSetColumnsOrder(order);
184
185 // TODO-RTL: do we need to reverse the array?
186 }
187
188 void wxHeaderCtrlBase::ResetColumnsOrder()
189 {
190 const unsigned count = GetColumnCount();
191 wxArrayInt order(count);
192 for ( unsigned n = 0; n < count; n++ )
193 order[n] = n;
194
195 DoSetColumnsOrder(order);
196 }
197
198 wxArrayInt wxHeaderCtrlBase::GetColumnsOrder() const
199 {
200 const wxArrayInt order = DoGetColumnsOrder();
201
202 wxASSERT_MSG( order.size() == GetColumnCount(), "invalid order array" );
203
204 return order;
205 }
206
207 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos) const
208 {
209 wxCHECK_MSG( pos < GetColumnCount(), wxNO_COLUMN, "invalid position" );
210
211 return GetColumnsOrder()[pos];
212 }
213
214 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const
215 {
216 const unsigned count = GetColumnCount();
217
218 wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" );
219
220 const wxArrayInt order = GetColumnsOrder();
221 for ( unsigned n = 0; n < count; n++ )
222 {
223 if ( (unsigned)order[n] == idx )
224 return n;
225 }
226
227 wxFAIL_MSG( "column unexpectedly not displayed at all" );
228
229 return wxNO_COLUMN;
230 }
231
232 /* static */
233 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt& order,
234 unsigned int idx,
235 unsigned int pos)
236 {
237 const unsigned count = order.size();
238
239 wxArrayInt orderNew;
240 orderNew.reserve(count);
241 for ( unsigned n = 0; ; n++ )
242 {
243 // NB: order of checks is important for this to work when the new
244 // column position is the same as the old one
245
246 // insert the column at its new position
247 if ( orderNew.size() == pos )
248 orderNew.push_back(idx);
249
250 if ( n == count )
251 break;
252
253 // delete the column from its old position
254 const unsigned idxOld = order[n];
255 if ( idxOld == idx )
256 continue;
257
258 orderNew.push_back(idxOld);
259 }
260
261 order.swap(orderNew);
262 }
263
264 void
265 wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt& colIndices, unsigned int count)
266 {
267 // update the column indices array if necessary
268 const unsigned countOld = colIndices.size();
269 if ( count > countOld )
270 {
271 // all new columns have default positions equal to their indices
272 for ( unsigned n = countOld; n < count; n++ )
273 colIndices.push_back(n);
274 }
275 else if ( count < countOld )
276 {
277 // filter out all the positions which are invalid now while keeping the
278 // order of the remaining ones
279 wxArrayInt colIndicesNew;
280 colIndicesNew.reserve(count);
281 for ( unsigned n = 0; n < countOld; n++ )
282 {
283 const unsigned idx = colIndices[n];
284 if ( idx < count )
285 colIndicesNew.push_back(idx);
286 }
287
288 colIndices.swap(colIndicesNew);
289 }
290 //else: count didn't really change, nothing to do
291
292 wxASSERT_MSG( colIndices.size() == count, "logic error" );
293 }
294
295 // ----------------------------------------------------------------------------
296 // wxHeaderCtrl extra UI
297 // ----------------------------------------------------------------------------
298
299 #if wxUSE_MENUS
300
301 void wxHeaderCtrlBase::AddColumnsItems(wxMenu& menu, int idColumnsBase)
302 {
303 const unsigned count = GetColumnCount();
304 for ( unsigned n = 0; n < count; n++ )
305 {
306 const wxHeaderColumn& col = GetColumn(n);
307 menu.AppendCheckItem(idColumnsBase + n, col.GetTitle());
308 if ( col.IsShown() )
309 menu.Check(n, true);
310 }
311 }
312
313 bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint& pt, const wxString& title)
314 {
315 // construct the menu with the entries for all columns
316 wxMenu menu;
317 if ( !title.empty() )
318 menu.SetTitle(title);
319
320 AddColumnsItems(menu);
321
322 // ... and an extra one to show the customization dialog if the user is
323 // allowed to reorder the columns too
324 const unsigned count = GetColumnCount();
325 if ( HasFlag(wxHD_ALLOW_REORDER) )
326 {
327 menu.AppendSeparator();
328 menu.Append(count, _("&Customize..."));
329 }
330
331 // do show the menu and get the user selection
332 const int rc = GetPopupMenuSelectionFromUser(menu, pt);
333 if ( rc == wxID_NONE )
334 return false;
335
336 if ( static_cast<unsigned>(rc) == count )
337 {
338 return ShowCustomizeDialog();
339 }
340 else // a column selected from the menu
341 {
342 UpdateColumnVisibility(rc, !GetColumn(rc).IsShown());
343 }
344
345 return true;
346 }
347
348 #endif // wxUSE_MENUS
349
350 bool wxHeaderCtrlBase::ShowCustomizeDialog()
351 {
352 #if wxUSE_REARRANGECTRL
353 // prepare the data for showing the dialog
354 wxArrayInt order = GetColumnsOrder();
355
356 const unsigned count = GetColumnCount();
357
358 // notice that titles are always in the index order, they will be shown
359 // rearranged according to the display order in the dialog
360 wxArrayString titles;
361 titles.reserve(count);
362 for ( unsigned n = 0; n < count; n++ )
363 titles.push_back(GetColumn(n).GetTitle());
364
365 // this loop is however over positions and not indices
366 unsigned pos;
367 for ( pos = 0; pos < count; pos++ )
368 {
369 int& idx = order[pos];
370 if ( GetColumn(idx).IsHidden() )
371 {
372 // indicate that this one is hidden
373 idx = ~idx;
374 }
375 }
376
377 // do show it
378 wxHeaderColumnsRearrangeDialog dlg(this, order, titles);
379 if ( dlg.ShowModal() == wxID_OK )
380 {
381 // and apply the changes
382 order = dlg.GetOrder();
383 for ( pos = 0; pos < count; pos++ )
384 {
385 int& idx = order[pos];
386 const bool show = idx >= 0;
387 if ( !show )
388 {
389 // make all indices positive for passing them to SetColumnsOrder()
390 idx = ~idx;
391 }
392
393 if ( show != GetColumn(idx).IsShown() )
394 UpdateColumnVisibility(idx, show);
395 }
396
397 UpdateColumnsOrder(order);
398 SetColumnsOrder(order);
399
400 return true;
401 }
402 #endif // wxUSE_REARRANGECTRL
403
404 return false;
405 }
406
407 // ============================================================================
408 // wxHeaderCtrlSimple implementation
409 // ============================================================================
410
411 void wxHeaderCtrlSimple::Init()
412 {
413 m_sortKey = wxNO_COLUMN;
414 }
415
416 const wxHeaderColumn& wxHeaderCtrlSimple::GetColumn(unsigned int idx) const
417 {
418 return m_cols[idx];
419 }
420
421 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple& col, unsigned int idx)
422 {
423 m_cols.insert(m_cols.begin() + idx, col);
424
425 UpdateColumnCount();
426 }
427
428 void wxHeaderCtrlSimple::DoDelete(unsigned int idx)
429 {
430 m_cols.erase(m_cols.begin() + idx);
431 if ( idx == m_sortKey )
432 m_sortKey = wxNO_COLUMN;
433
434 UpdateColumnCount();
435 }
436
437 void wxHeaderCtrlSimple::DeleteAllColumns()
438 {
439 m_cols.clear();
440 m_sortKey = wxNO_COLUMN;
441
442 UpdateColumnCount();
443 }
444
445
446 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx, bool show)
447 {
448 if ( show != m_cols[idx].IsShown() )
449 {
450 m_cols[idx].SetHidden(!show);
451
452 UpdateColumn(idx);
453 }
454 }
455
456 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx, bool ascending)
457 {
458 RemoveSortIndicator();
459
460 m_cols[idx].SetSortOrder(ascending);
461 m_sortKey = idx;
462
463 UpdateColumn(idx);
464 }
465
466 void wxHeaderCtrlSimple::RemoveSortIndicator()
467 {
468 if ( m_sortKey != wxNO_COLUMN )
469 {
470 const unsigned sortOld = m_sortKey;
471 m_sortKey = wxNO_COLUMN;
472
473 m_cols[sortOld].UnsetAsSortKey();
474
475 UpdateColumn(sortOld);
476 }
477 }
478
479 bool
480 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
481 {
482 const int widthContents = GetBestFittingWidth(idx);
483 if ( widthContents == -1 )
484 return false;
485
486 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
487
488 return true;
489 }
490
491 // ============================================================================
492 // wxHeaderCtrlEvent implementation
493 // ============================================================================
494
495 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent, wxNotifyEvent)
496
497 wxDEFINE_EVENT( wxEVT_HEADER_CLICK, wxHeaderCtrlEvent);
498 wxDEFINE_EVENT( wxEVT_HEADER_RIGHT_CLICK, wxHeaderCtrlEvent);
499 wxDEFINE_EVENT( wxEVT_HEADER_MIDDLE_CLICK, wxHeaderCtrlEvent);
500
501 wxDEFINE_EVENT( wxEVT_HEADER_DCLICK, wxHeaderCtrlEvent);
502 wxDEFINE_EVENT( wxEVT_HEADER_RIGHT_DCLICK, wxHeaderCtrlEvent);
503 wxDEFINE_EVENT( wxEVT_HEADER_MIDDLE_DCLICK, wxHeaderCtrlEvent);
504
505 wxDEFINE_EVENT( wxEVT_HEADER_SEPARATOR_DCLICK, wxHeaderCtrlEvent);
506
507 wxDEFINE_EVENT( wxEVT_HEADER_BEGIN_RESIZE, wxHeaderCtrlEvent);
508 wxDEFINE_EVENT( wxEVT_HEADER_RESIZING, wxHeaderCtrlEvent);
509 wxDEFINE_EVENT( wxEVT_HEADER_END_RESIZE, wxHeaderCtrlEvent);
510
511 wxDEFINE_EVENT( wxEVT_HEADER_BEGIN_REORDER, wxHeaderCtrlEvent);
512 wxDEFINE_EVENT( wxEVT_HEADER_END_REORDER, wxHeaderCtrlEvent);
513
514 wxDEFINE_EVENT( wxEVT_HEADER_DRAGGING_CANCELLED, wxHeaderCtrlEvent);
515
516 #endif // wxUSE_HEADERCTRL