]> git.saurik.com Git - wxWidgets.git/blob - src/common/headerctrlcmn.cpp
Remove redundant lines from Inno Setup file.
[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 #include "wx/renderer.h"
35
36 namespace
37 {
38
39 // ----------------------------------------------------------------------------
40 // constants
41 // ----------------------------------------------------------------------------
42
43 const unsigned int wxNO_COLUMN = static_cast<unsigned>(-1);
44
45 // ----------------------------------------------------------------------------
46 // wxHeaderColumnsRearrangeDialog: dialog for customizing our columns
47 // ----------------------------------------------------------------------------
48
49 #if wxUSE_REARRANGECTRL
50
51 class wxHeaderColumnsRearrangeDialog : public wxRearrangeDialog
52 {
53 public:
54 wxHeaderColumnsRearrangeDialog(wxWindow *parent,
55 const wxArrayInt& order,
56 const wxArrayString& items)
57 : wxRearrangeDialog
58 (
59 parent,
60 _("Please select the columns to show and define their order:"),
61 _("Customize Columns"),
62 order,
63 items
64 )
65 {
66 }
67 };
68
69 #endif // wxUSE_REARRANGECTRL
70
71 } // anonymous namespace
72
73 // ============================================================================
74 // wxHeaderCtrlBase implementation
75 // ============================================================================
76
77 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr[] = "wxHeaderCtrl";
78
79 BEGIN_EVENT_TABLE(wxHeaderCtrlBase, wxControl)
80 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY, wxHeaderCtrlBase::OnSeparatorDClick)
81 #if wxUSE_MENUS
82 EVT_HEADER_RIGHT_CLICK(wxID_ANY, wxHeaderCtrlBase::OnRClick)
83 #endif // wxUSE_MENUS
84 END_EVENT_TABLE()
85
86 void wxHeaderCtrlBase::ScrollWindow(int dx,
87 int WXUNUSED_UNLESS_DEBUG(dy),
88 const wxRect * WXUNUSED_UNLESS_DEBUG(rect))
89
90 {
91 // this doesn't make sense at all
92 wxASSERT_MSG( !dy, "header window can't be scrolled vertically" );
93
94 // this would actually be nice to support for "frozen" headers but it isn't
95 // supported currently
96 wxASSERT_MSG( !rect, "header window can't be scrolled partially" );
97
98 DoScrollHorz(dx);
99 }
100
101 void wxHeaderCtrlBase::SetColumnCount(unsigned int count)
102 {
103 if ( count != GetColumnCount() )
104 OnColumnCountChanging(count);
105
106 // still call DoSetCount() even if the count didn't really change in order
107 // to update all the columns
108 DoSetCount(count);
109 }
110
111 int wxHeaderCtrlBase::GetColumnTitleWidth(const wxHeaderColumn& col)
112 {
113 int w = wxWindowBase::GetTextExtent(col.GetTitle()).x;
114
115 // add some margin:
116 w += wxRendererNative::Get().GetHeaderButtonMargin(this);
117
118 // if a bitmap is used, add space for it and 2px border:
119 wxBitmap bmp = col.GetBitmap();
120 if ( bmp.IsOk() )
121 w += bmp.GetWidth() + 2;
122
123 return w;
124 }
125
126 // ----------------------------------------------------------------------------
127 // wxHeaderCtrlBase event handling
128 // ----------------------------------------------------------------------------
129
130 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent& event)
131 {
132 const unsigned col = event.GetColumn();
133 const wxHeaderColumn& column = GetColumn(col);
134
135 if ( !column.IsResizeable() )
136 {
137 event.Skip();
138 return;
139 }
140
141 int w = GetColumnTitleWidth(column);
142
143 if ( !UpdateColumnWidthToFit(col, w) )
144 event.Skip();
145 else
146 UpdateColumn(col);
147 }
148
149 #if wxUSE_MENUS
150
151 void wxHeaderCtrlBase::OnRClick(wxHeaderCtrlEvent& event)
152 {
153 if ( !HasFlag(wxHD_ALLOW_HIDE) )
154 {
155 event.Skip();
156 return;
157 }
158
159 ShowColumnsMenu(ScreenToClient(wxGetMousePosition()));
160 }
161
162 #endif // wxUSE_MENUS
163
164 // ----------------------------------------------------------------------------
165 // wxHeaderCtrlBase column reordering
166 // ----------------------------------------------------------------------------
167
168 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt& order)
169 {
170 const unsigned count = GetColumnCount();
171 wxCHECK_RET( order.size() == count, "wrong number of columns" );
172
173 // check the array validity
174 wxArrayInt seen(count, 0);
175 for ( unsigned n = 0; n < count; n++ )
176 {
177 const unsigned idx = order[n];
178 wxCHECK_RET( idx < count, "invalid column index" );
179 wxCHECK_RET( !seen[idx], "duplicate column index" );
180
181 seen[idx] = 1;
182 }
183
184 DoSetColumnsOrder(order);
185
186 // TODO-RTL: do we need to reverse the array?
187 }
188
189 void wxHeaderCtrlBase::ResetColumnsOrder()
190 {
191 const unsigned count = GetColumnCount();
192 wxArrayInt order(count);
193 for ( unsigned n = 0; n < count; n++ )
194 order[n] = n;
195
196 DoSetColumnsOrder(order);
197 }
198
199 wxArrayInt wxHeaderCtrlBase::GetColumnsOrder() const
200 {
201 const wxArrayInt order = DoGetColumnsOrder();
202
203 wxASSERT_MSG( order.size() == GetColumnCount(), "invalid order array" );
204
205 return order;
206 }
207
208 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos) const
209 {
210 wxCHECK_MSG( pos < GetColumnCount(), wxNO_COLUMN, "invalid position" );
211
212 return GetColumnsOrder()[pos];
213 }
214
215 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const
216 {
217 const unsigned count = GetColumnCount();
218
219 wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" );
220
221 const wxArrayInt order = GetColumnsOrder();
222 for ( unsigned n = 0; n < count; n++ )
223 {
224 if ( (unsigned)order[n] == idx )
225 return n;
226 }
227
228 wxFAIL_MSG( "column unexpectedly not displayed at all" );
229
230 return wxNO_COLUMN;
231 }
232
233 /* static */
234 void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt& order,
235 unsigned int idx,
236 unsigned int pos)
237 {
238 const unsigned count = order.size();
239
240 wxArrayInt orderNew;
241 orderNew.reserve(count);
242 for ( unsigned n = 0; ; n++ )
243 {
244 // NB: order of checks is important for this to work when the new
245 // column position is the same as the old one
246
247 // insert the column at its new position
248 if ( orderNew.size() == pos )
249 orderNew.push_back(idx);
250
251 if ( n == count )
252 break;
253
254 // delete the column from its old position
255 const unsigned idxOld = order[n];
256 if ( idxOld == idx )
257 continue;
258
259 orderNew.push_back(idxOld);
260 }
261
262 order.swap(orderNew);
263 }
264
265 void
266 wxHeaderCtrlBase::DoResizeColumnIndices(wxArrayInt& colIndices, unsigned int count)
267 {
268 // update the column indices array if necessary
269 const unsigned countOld = colIndices.size();
270 if ( count > countOld )
271 {
272 // all new columns have default positions equal to their indices
273 for ( unsigned n = countOld; n < count; n++ )
274 colIndices.push_back(n);
275 }
276 else if ( count < countOld )
277 {
278 // filter out all the positions which are invalid now while keeping the
279 // order of the remaining ones
280 wxArrayInt colIndicesNew;
281 colIndicesNew.reserve(count);
282 for ( unsigned n = 0; n < countOld; n++ )
283 {
284 const unsigned idx = colIndices[n];
285 if ( idx < count )
286 colIndicesNew.push_back(idx);
287 }
288
289 colIndices.swap(colIndicesNew);
290 }
291 //else: count didn't really change, nothing to do
292
293 wxASSERT_MSG( colIndices.size() == count, "logic error" );
294 }
295
296 // ----------------------------------------------------------------------------
297 // wxHeaderCtrl extra UI
298 // ----------------------------------------------------------------------------
299
300 #if wxUSE_MENUS
301
302 void wxHeaderCtrlBase::AddColumnsItems(wxMenu& menu, int idColumnsBase)
303 {
304 const unsigned count = GetColumnCount();
305 for ( unsigned n = 0; n < count; n++ )
306 {
307 const wxHeaderColumn& col = GetColumn(n);
308 menu.AppendCheckItem(idColumnsBase + n, col.GetTitle());
309 if ( col.IsShown() )
310 menu.Check(n, true);
311 }
312 }
313
314 bool wxHeaderCtrlBase::ShowColumnsMenu(const wxPoint& pt, const wxString& title)
315 {
316 // construct the menu with the entries for all columns
317 wxMenu menu;
318 if ( !title.empty() )
319 menu.SetTitle(title);
320
321 AddColumnsItems(menu);
322
323 // ... and an extra one to show the customization dialog if the user is
324 // allowed to reorder the columns too
325 const unsigned count = GetColumnCount();
326 if ( HasFlag(wxHD_ALLOW_REORDER) )
327 {
328 menu.AppendSeparator();
329 menu.Append(count, _("&Customize..."));
330 }
331
332 // do show the menu and get the user selection
333 const int rc = GetPopupMenuSelectionFromUser(menu, pt);
334 if ( rc == wxID_NONE )
335 return false;
336
337 if ( static_cast<unsigned>(rc) == count )
338 {
339 return ShowCustomizeDialog();
340 }
341 else // a column selected from the menu
342 {
343 UpdateColumnVisibility(rc, !GetColumn(rc).IsShown());
344 }
345
346 return true;
347 }
348
349 #endif // wxUSE_MENUS
350
351 bool wxHeaderCtrlBase::ShowCustomizeDialog()
352 {
353 #if wxUSE_REARRANGECTRL
354 // prepare the data for showing the dialog
355 wxArrayInt order = GetColumnsOrder();
356
357 const unsigned count = GetColumnCount();
358
359 // notice that titles are always in the index order, they will be shown
360 // rearranged according to the display order in the dialog
361 wxArrayString titles;
362 titles.reserve(count);
363 for ( unsigned n = 0; n < count; n++ )
364 titles.push_back(GetColumn(n).GetTitle());
365
366 // this loop is however over positions and not indices
367 unsigned pos;
368 for ( pos = 0; pos < count; pos++ )
369 {
370 int& idx = order[pos];
371 if ( GetColumn(idx).IsHidden() )
372 {
373 // indicate that this one is hidden
374 idx = ~idx;
375 }
376 }
377
378 // do show it
379 wxHeaderColumnsRearrangeDialog dlg(this, order, titles);
380 if ( dlg.ShowModal() == wxID_OK )
381 {
382 // and apply the changes
383 order = dlg.GetOrder();
384 for ( pos = 0; pos < count; pos++ )
385 {
386 int& idx = order[pos];
387 const bool show = idx >= 0;
388 if ( !show )
389 {
390 // make all indices positive for passing them to SetColumnsOrder()
391 idx = ~idx;
392 }
393
394 if ( show != GetColumn(idx).IsShown() )
395 UpdateColumnVisibility(idx, show);
396 }
397
398 UpdateColumnsOrder(order);
399 SetColumnsOrder(order);
400
401 return true;
402 }
403 #endif // wxUSE_REARRANGECTRL
404
405 return false;
406 }
407
408 // ============================================================================
409 // wxHeaderCtrlSimple implementation
410 // ============================================================================
411
412 void wxHeaderCtrlSimple::Init()
413 {
414 m_sortKey = wxNO_COLUMN;
415 }
416
417 const wxHeaderColumn& wxHeaderCtrlSimple::GetColumn(unsigned int idx) const
418 {
419 return m_cols[idx];
420 }
421
422 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple& col, unsigned int idx)
423 {
424 m_cols.insert(m_cols.begin() + idx, col);
425
426 UpdateColumnCount();
427 }
428
429 void wxHeaderCtrlSimple::DoDelete(unsigned int idx)
430 {
431 m_cols.erase(m_cols.begin() + idx);
432 if ( idx == m_sortKey )
433 m_sortKey = wxNO_COLUMN;
434
435 UpdateColumnCount();
436 }
437
438 void wxHeaderCtrlSimple::DeleteAllColumns()
439 {
440 m_cols.clear();
441 m_sortKey = wxNO_COLUMN;
442
443 UpdateColumnCount();
444 }
445
446
447 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx, bool show)
448 {
449 if ( show != m_cols[idx].IsShown() )
450 {
451 m_cols[idx].SetHidden(!show);
452
453 UpdateColumn(idx);
454 }
455 }
456
457 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx, bool ascending)
458 {
459 RemoveSortIndicator();
460
461 m_cols[idx].SetSortOrder(ascending);
462 m_sortKey = idx;
463
464 UpdateColumn(idx);
465 }
466
467 void wxHeaderCtrlSimple::RemoveSortIndicator()
468 {
469 if ( m_sortKey != wxNO_COLUMN )
470 {
471 const unsigned sortOld = m_sortKey;
472 m_sortKey = wxNO_COLUMN;
473
474 m_cols[sortOld].UnsetAsSortKey();
475
476 UpdateColumn(sortOld);
477 }
478 }
479
480 bool
481 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
482 {
483 const int widthContents = GetBestFittingWidth(idx);
484 if ( widthContents == -1 )
485 return false;
486
487 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
488
489 return true;
490 }
491
492 // ============================================================================
493 // wxHeaderCtrlEvent implementation
494 // ============================================================================
495
496 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent, wxNotifyEvent)
497
498 wxDEFINE_EVENT( wxEVT_HEADER_CLICK, wxHeaderCtrlEvent);
499 wxDEFINE_EVENT( wxEVT_HEADER_RIGHT_CLICK, wxHeaderCtrlEvent);
500 wxDEFINE_EVENT( wxEVT_HEADER_MIDDLE_CLICK, wxHeaderCtrlEvent);
501
502 wxDEFINE_EVENT( wxEVT_HEADER_DCLICK, wxHeaderCtrlEvent);
503 wxDEFINE_EVENT( wxEVT_HEADER_RIGHT_DCLICK, wxHeaderCtrlEvent);
504 wxDEFINE_EVENT( wxEVT_HEADER_MIDDLE_DCLICK, wxHeaderCtrlEvent);
505
506 wxDEFINE_EVENT( wxEVT_HEADER_SEPARATOR_DCLICK, wxHeaderCtrlEvent);
507
508 wxDEFINE_EVENT( wxEVT_HEADER_BEGIN_RESIZE, wxHeaderCtrlEvent);
509 wxDEFINE_EVENT( wxEVT_HEADER_RESIZING, wxHeaderCtrlEvent);
510 wxDEFINE_EVENT( wxEVT_HEADER_END_RESIZE, wxHeaderCtrlEvent);
511
512 wxDEFINE_EVENT( wxEVT_HEADER_BEGIN_REORDER, wxHeaderCtrlEvent);
513 wxDEFINE_EVENT( wxEVT_HEADER_END_REORDER, wxHeaderCtrlEvent);
514
515 wxDEFINE_EVENT( wxEVT_HEADER_DRAGGING_CANCELLED, wxHeaderCtrlEvent);
516
517 #endif // wxUSE_HEADERCTRL