]> git.saurik.com Git - wxWidgets.git/blame - src/common/headerctrlcmn.cpp
Somehow, setting a tint color makes gauge work :/.
[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
56873923
VZ
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
e721a2a2
VZ
25#if wxUSE_HEADERCTRL
26
56873923 27#ifndef WX_PRECOMP
c4789bf6 28 #include "wx/menu.h"
56873923
VZ
29#endif // WX_PRECOMP
30
31#include "wx/headerctrl.h"
af67f39d 32#include "wx/rearrangectrl.h"
53137278 33#include "wx/renderer.h"
af67f39d
VZ
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
53137278
VS
110int 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
702f5349
VZ
125// ----------------------------------------------------------------------------
126// wxHeaderCtrlBase event handling
127// ----------------------------------------------------------------------------
128
3bfaa5a7
VZ
129void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent& event)
130{
131 const unsigned col = event.GetColumn();
1acf670a 132 const wxHeaderColumn& column = GetColumn(col);
3bfaa5a7 133
1acf670a
VS
134 if ( !column.IsResizeable() )
135 {
136 event.Skip();
137 return;
138 }
139
53137278 140 int w = GetColumnTitleWidth(column);
3bfaa5a7
VZ
141
142 if ( !UpdateColumnWidthToFit(col, w) )
143 event.Skip();
144 else
145 UpdateColumn(col);
146}
147
8a2e3f80 148#if wxUSE_MENUS
faef508f 149
613de0e8
VZ
150void 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
8a2e3f80 161#endif // wxUSE_MENUS
faef508f 162
702f5349
VZ
163// ----------------------------------------------------------------------------
164// wxHeaderCtrlBase column reordering
165// ----------------------------------------------------------------------------
166
167void 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
da5a641f
VZ
188void 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
702f5349
VZ
198wxArrayInt 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
207unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos) const
208{
209 wxCHECK_MSG( pos < GetColumnCount(), wxNO_COLUMN, "invalid position" );
210
211 return GetColumnsOrder()[pos];
212}
213
214unsigned 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
1bb74626
VZ
232/* static */
233void 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
f6655391
VZ
264void
265wxHeaderCtrlBase::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 }
613de0e8 290 //else: count didn't really change, nothing to do
f6655391
VZ
291
292 wxASSERT_MSG( colIndices.size() == count, "logic error" );
293}
294
e8f25dbb
VZ
295// ----------------------------------------------------------------------------
296// wxHeaderCtrl extra UI
297// ----------------------------------------------------------------------------
298
8a2e3f80 299#if wxUSE_MENUS
faef508f 300
ddd0db96 301void wxHeaderCtrlBase::AddColumnsItems(wxMenu& menu, int idColumnsBase)
e8f25dbb 302{
e8f25dbb
VZ
303 const unsigned count = GetColumnCount();
304 for ( unsigned n = 0; n < count; n++ )
305 {
306 const wxHeaderColumn& col = GetColumn(n);
ddd0db96 307 menu.AppendCheckItem(idColumnsBase + n, col.GetTitle());
e8f25dbb
VZ
308 if ( col.IsShown() )
309 menu.Check(n, true);
310 }
ddd0db96
VZ
311}
312
313bool 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);
e8f25dbb 321
613de0e8
VZ
322 // ... and an extra one to show the customization dialog if the user is
323 // allowed to reorder the columns too
ddd0db96 324 const unsigned count = GetColumnCount();
613de0e8
VZ
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
8a2e3f80 348#endif // wxUSE_MENUS
faef508f 349
613de0e8
VZ
350bool wxHeaderCtrlBase::ShowCustomizeDialog()
351{
f0bb342f 352#if wxUSE_REARRANGECTRL
af67f39d
VZ
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);
f0bb342f 379 if ( dlg.ShowModal() == wxID_OK )
af67f39d 380 {
f0bb342f
VZ
381 // and apply the changes
382 order = dlg.GetOrder();
383 for ( pos = 0; pos < count; pos++ )
af67f39d 384 {
f0bb342f
VZ
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);
af67f39d
VZ
395 }
396
f0bb342f
VZ
397 UpdateColumnsOrder(order);
398 SetColumnsOrder(order);
af67f39d 399
f0bb342f
VZ
400 return true;
401 }
402#endif // wxUSE_REARRANGECTRL
af67f39d 403
f0bb342f 404 return false;
e8f25dbb
VZ
405}
406
e2bfe673
VZ
407// ============================================================================
408// wxHeaderCtrlSimple implementation
409// ============================================================================
410
411void wxHeaderCtrlSimple::Init()
412{
413 m_sortKey = wxNO_COLUMN;
414}
415
482d06f8 416const wxHeaderColumn& wxHeaderCtrlSimple::GetColumn(unsigned int idx) const
e2bfe673
VZ
417{
418 return m_cols[idx];
419}
420
421void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple& col, unsigned int idx)
422{
423 m_cols.insert(m_cols.begin() + idx, col);
424
425 UpdateColumnCount();
426}
427
428void 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
437void wxHeaderCtrlSimple::DeleteAllColumns()
438{
439 m_cols.clear();
440 m_sortKey = wxNO_COLUMN;
441
442 UpdateColumnCount();
443}
444
445
446void 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
456void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx, bool ascending)
457{
458 RemoveSortIndicator();
459
aadbdd16 460 m_cols[idx].SetSortOrder(ascending);
e2bfe673
VZ
461 m_sortKey = idx;
462
463 UpdateColumn(idx);
464}
465
466void 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
e5a16353
VZ
479bool
480wxHeaderCtrlSimple::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));
e5a16353
VZ
487
488 return true;
489}
490
fa3d4aaf
VZ
491// ============================================================================
492// wxHeaderCtrlEvent implementation
493// ============================================================================
494
495IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent, wxNotifyEvent)
496
ce7fe42e
VZ
497wxDEFINE_EVENT( wxEVT_HEADER_CLICK, wxHeaderCtrlEvent);
498wxDEFINE_EVENT( wxEVT_HEADER_RIGHT_CLICK, wxHeaderCtrlEvent);
499wxDEFINE_EVENT( wxEVT_HEADER_MIDDLE_CLICK, wxHeaderCtrlEvent);
fa3d4aaf 500
ce7fe42e
VZ
501wxDEFINE_EVENT( wxEVT_HEADER_DCLICK, wxHeaderCtrlEvent);
502wxDEFINE_EVENT( wxEVT_HEADER_RIGHT_DCLICK, wxHeaderCtrlEvent);
503wxDEFINE_EVENT( wxEVT_HEADER_MIDDLE_DCLICK, wxHeaderCtrlEvent);
3bfaa5a7 504
ce7fe42e 505wxDEFINE_EVENT( wxEVT_HEADER_SEPARATOR_DCLICK, wxHeaderCtrlEvent);
aef252d9 506
ce7fe42e
VZ
507wxDEFINE_EVENT( wxEVT_HEADER_BEGIN_RESIZE, wxHeaderCtrlEvent);
508wxDEFINE_EVENT( wxEVT_HEADER_RESIZING, wxHeaderCtrlEvent);
509wxDEFINE_EVENT( wxEVT_HEADER_END_RESIZE, wxHeaderCtrlEvent);
702f5349 510
ce7fe42e
VZ
511wxDEFINE_EVENT( wxEVT_HEADER_BEGIN_REORDER, wxHeaderCtrlEvent);
512wxDEFINE_EVENT( wxEVT_HEADER_END_REORDER, wxHeaderCtrlEvent);
565804f2 513
ce7fe42e 514wxDEFINE_EVENT( wxEVT_HEADER_DRAGGING_CANCELLED, wxHeaderCtrlEvent);
e721a2a2
VZ
515
516#endif // wxUSE_HEADERCTRL