Make column width calculations in wxDataViewCtrl and wxHeaderCtrl consistent.
[wxWidgets.git] / interface / wx / headerctrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/headerctrl.h
3 // Purpose: interface of wxHeaderCtrl
4 // Author: Vadim Zeitlin
5 // Created: 2008-12-01
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 /**
12 @class wxHeaderCtrl
13
14 wxHeaderCtrl is the control containing the column headings which is usually
15 used for display of tabular data.
16
17 It is used as part of wxGrid, in generic version wxDataViewCtrl and report
18 view of wxListCtrl but can be also used independently. In general this
19 class is meant to be used as part of another control which already stores
20 the column information somewhere as it can't be used directly: instead you
21 need to inherit from it and implement the GetColumn() method to provide
22 column information. See wxHeaderCtrlSimple for a concrete control class
23 which can be used directly.
24
25 In addition to labeling the columns, the control has the following
26 features:
27 - Column reordering support, either by explicitly configuring the
28 columns order and calling SetColumnsOrder() or by dragging the
29 columns interactively (if enabled).
30 - Display of the icons in the header: this is often used to display a
31 sort or reverse sort indicator when the column header is clicked.
32
33 Notice that this control itself doesn't do anything other than displaying
34 the column headers. In particular column reordering and sorting must still
35 be supported by the associated control displaying the real data under the
36 header. Also remember to call ScrollWindow() method of the control if the
37 associated data display window has a horizontal scrollbar, otherwise the
38 headers wouldn't align with the data when the window is scrolled.
39
40 This control is implemented using the native header control under MSW
41 systems and a generic implementation elsewhere.
42
43
44 @section headerctrl_improvements Future Improvements
45
46 Some features are supported by the native MSW control and so could be
47 easily implemented in this version of wxHeaderCtrl but need to be
48 implemented in the generic version as well to be really useful. Please let
49 us know if you need or, better, plan to work on implementing, any of them:
50 - Displaying bitmaps instead of or together with the text
51 - Custom drawn headers
52 - Filters associated with a column.
53
54
55 @beginStyleTable
56 @style{wxHD_ALLOW_REORDER}
57 If this style is specified (it is by default), the user can reorder
58 the control columns by dragging them.
59 @style{wxHD_ALLOW_HIDE}
60 If this style is specified, the control shows a popup menu allowing the
61 user to change the columns visibility on right mouse click. Notice that
62 the program can always hide or show the columns, this style only
63 affects the users capability to do it.
64 @style{wxHD_DEFAULT_STYLE}
65 Symbolic name for the default control style, currently equal to
66 @c wxHD_ALLOW_REORDER.
67 @endStyleTable
68
69 @beginEventEmissionTable{wxHeaderCtrlEvent}
70 @event{EVT_HEADER_CLICK(id, func)}
71 A column heading was clicked.
72 @event{EVT_HEADER_RIGHT_CLICK(id, func)}
73 A column heading was right clicked.
74 @event{EVT_HEADER_MIDDLE_CLICK(id, func)}
75 A column heading was clicked with the middle mouse button.
76 @event{EVT_HEADER_DCLICK(id, func)}
77 A column heading was double clicked.
78 @event{EVT_HEADER_RIGHT_DCLICK(id, func)}
79 A column heading was right double clicked.
80 @event{EVT_HEADER_MIDDLE_DCLICK(id, func)}
81 A column heading was double clicked with the middle mouse button.
82 @event{EVT_HEADER_SEPARATOR_DCLICK(id, func)}
83 Separator to the right of the specified column was double clicked
84 (this action is commonly used to resize the column to fit its
85 contents width and the control provides UpdateColumnWidthToFit() method
86 to make implementing this easier).
87 @event{EVT_HEADER_BEGIN_RESIZE(id, func)}
88 The user started to drag the separator to the right of the column
89 with the specified index (this can only happen for the columns for
90 which wxHeaderColumn::IsResizeable() returns true). The event can
91 be vetoed to prevent the column from being resized. If it isn't,
92 the resizing and end resize (or dragging cancelled) events will be
93 generated later.
94 @event{EVT_HEADER_RESIZING(id, func)}
95 The user is dragging the column with the specified index resizing
96 it and its current width is wxHeaderCtrlEvent::GetWidth().
97 The event can be vetoed to stop the dragging operation completely at
98 any time.
99 @event{EVT_HEADER_END_RESIZE(id, func)}
100 The user stopped dragging the column by releasing the mouse.
101 The column should normally be resized to the value of
102 wxHeaderCtrlEvent::GetWidth().
103 @event{EVT_HEADER_BEGIN_REORDER(id, func)}
104 The user started to drag the column with the specified index (this
105 can only happen for the controls with wxHD_ALLOW_REORDER style).
106 This event can be vetoed to prevent the column from being reordered,
107 otherwise the end reorder message will be generated later.
108 @event{EVT_HEADER_END_REORDER(id, func)}
109 The user dropped the column in its new location. The event can be
110 vetoed to prevent the column from being placed at the new position
111 or handled to update the display of the data in the associated
112 control to match the new column location (available from
113 wxHeaderCtrlEvent::GetNewOrder()).
114 @event{EVT_HEADER_DRAGGING_CANCELLED(id, func)}
115 The resizing or reordering operation currently in progress was
116 cancelled. This can happen if the user pressed Esc key while
117 dragging the mouse or the mouse capture was lost for some other
118 reason. You only need to handle this event if your application
119 entered into some modal mode when resizing or reordering began, in
120 which case it should handle this event in addition to the matching
121 end resizing or reordering ones.
122 @endEventTable
123
124 @library{wxcore}
125 @category{ctrl}
126
127 @see wxGrid, wxListCtrl, wxDataViewCtrl
128 */
129 class wxHeaderCtrl : public wxControl
130 {
131 public:
132 /**
133 Default constructor not creating the underlying window.
134
135 You must use Create() after creating the object using this constructor.
136 */
137 wxHeaderCtrl();
138
139 /**
140 Constructor creating the window.
141
142 Please see Create() for the parameters documentation.
143 */
144 wxHeaderCtrl(wxWindow *parent,
145 wxWindowID winid = wxID_ANY,
146 const wxPoint& pos = wxDefaultPosition,
147 const wxSize& size = wxDefaultSize,
148 long style = wxHD_DEFAULT_STYLE,
149 const wxString& name = wxHeaderCtrlNameStr);
150
151 /**
152 Create the header control window.
153
154 @param parent
155 The parent window. The header control should be typically
156 positioned along the top edge of this window.
157 @param winid
158 Id of the control or @c wxID_ANY if you don't care.
159 @param pos
160 The initial position of the control.
161 @param size
162 The initial size of the control (usually not very useful as this
163 control will typically be resized to have the same width as the
164 associated data display control).
165 @param style
166 The control style, @c wxHD_DEFAULT_STYLE by default. Notice that
167 the default style allows the user to reorder the columns by
168 dragging them and you need to explicitly turn this feature off by
169 using @code wxHD_DEFAULT_STYLE & ~wxHD_ALLOW_REORDER @endcode if
170 this is undesirable.
171 @param name
172 The name of the control.
173 */
174 bool Create(wxWindow *parent,
175 wxWindowID winid = wxID_ANY,
176 const wxPoint& pos = wxDefaultPosition,
177 const wxSize& size = wxDefaultSize,
178 long style = wxHD_DEFAULT_STYLE,
179 const wxString& name = wxHeaderCtrlNameStr);
180
181 /**
182 Set the number of columns in the control.
183
184 The control will use GetColumn() to get information about all the
185 new columns and refresh itself, i.e. this method also has the same
186 effect as calling UpdateColumn() for all columns but it should only be
187 used if the number of columns really changed.
188 */
189 void SetColumnCount(unsigned int count);
190
191 /**
192 Return the number of columns in the control.
193
194 @return
195 Number of columns as previously set by SetColumnCount().
196
197 @see IsEmpty()
198 */
199 unsigned int GetColumnCount() const;
200
201 /**
202 Return whether the control has any columns.
203
204 @see GetColumnCount()
205 */
206 bool IsEmpty() const;
207
208 /**
209 Update the column with the given index.
210
211 When the value returned by GetColumn() changes, this method must be
212 called to notify the control about the change and update the visual
213 display to match the new column data.
214
215 @param idx
216 The column index, must be less than GetColumnCount().
217 */
218 void UpdateColumn(unsigned int idx);
219
220 /**
221 Change the columns display order.
222
223 The display order defines the order in which the columns appear on the
224 screen and does @em not affect the interpretation of indices by all the
225 other class methods.
226
227 The @a order array specifies the column indices corresponding to the
228 display positions.
229
230 @param order
231 A permutation of all column indices, i.e. an array of size
232 GetColumnsOrder() containing all column indices exactly once. The
233 n-th element of this array defines the index of the column shown at
234 the n-th position from left (for the default left-to-right writing
235 direction).
236
237 @see wxListCtrl::SetColumnsOrder()
238 */
239 void SetColumnsOrder(const wxArrayInt& order);
240
241 /**
242 Return the array describing the columns display order.
243
244 For the controls without wxHD_ALLOW_REORDER style the returned array
245 will be the same as was passed to SetColumnsOrder() previously or
246 define the default order (with n-th element being n) if it hadn't been
247 called. But for the controls with wxHD_ALLOW_REORDER style, the columns
248 can be also reordered by user.
249 */
250 wxArrayInt GetColumnsOrder() const;
251
252 /**
253 Return the index of the column displayed at the given position.
254
255 @param pos
256 The display position, e.g. 0 for the left-most column, 1 for the
257 next one and so on until GetColumnCount() - 1.
258
259 @see GetColumnPos()
260 */
261 unsigned int GetColumnAt(unsigned int pos) const;
262
263 /**
264 Get the position at which this column is currently displayed.
265
266 Notice that a valid position is returned even for the hidden columns
267 currently.
268
269 @param idx
270 The column index, must be less than GetColumnCount().
271
272 @see GetColumnAt()
273 */
274 unsigned int GetColumnPos(unsigned int idx) const;
275
276 /**
277 Reset the columns order to the natural one.
278
279 After calling this function, the column with index @c idx appears at
280 position @c idx in the control.
281 */
282 void ResetColumnsOrder();
283
284 /**
285 Helper function to manipulate the array of column indices.
286
287 This function reshuffles the array of column indices indexed by
288 positions (i.e. using the same convention as for SetColumnsOrder()) so
289 that the column with the given index is found at the specified
290 position.
291
292 @param order
293 Array containing the indices of columns in order of their
294 positions.
295 @param idx
296 The index of the column to move.
297 @param pos
298 The new position for the column @a idx.
299 */
300 static void MoveColumnInOrderArray(wxArrayInt& order,
301 unsigned int idx,
302 unsigned int pos);
303
304 /**
305 Show the popup menu allowing the user to show or hide the columns.
306
307 This functions shows the popup menu containing all columns with check
308 marks for the ones which are currently shown and allows the user to
309 check or uncheck them to toggle their visibility. It is called from the
310 default EVT_HEADER_RIGHT_CLICK handler for the controls which have
311 wxHD_ALLOW_HIDE style. And if the column has wxHD_ALLOW_REORDER style
312 as well, the menu also contains an item to customize the columns shown
313 using which results in ShowCustomizeDialog() being called, please see
314 its description for more details.
315
316 If a column was toggled, UpdateColumnVisibility() virtual function is
317 called so it must be implemented for the controls with wxHD_ALLOW_HIDE
318 style or if you call this function explicitly.
319
320 @param pt
321 The position of the menu, in the header window coordinates.
322 @param title
323 The title for the menu if not empty.
324 @return
325 @true if a column was shown or hidden or @false if nothing was
326 done, e.g. because the menu was cancelled.
327 */
328 bool ShowColumnsMenu(const wxPoint& pt, const wxString& title = wxString());
329
330 /**
331 Helper function appending the checkable items corresponding to all the
332 columns to the given menu.
333
334 This function is used by ShowColumnsMenu() but can also be used if you
335 show your own custom columns menu and still want all the columns shown
336 in it. It appends menu items with column labels as their text and
337 consecutive ids starting from @a idColumnsBase to the menu and checks
338 the items corresponding to the currently visible columns.
339
340 Example of use:
341 @code
342 wxMenu menu;
343 menu.Append(100, "Some custom command");
344 menu.AppendSeparator();
345 AddColumnsItems(menu, 200);
346 const int rc = GetPopupMenuSelectionFromUser(menu, pt);
347 if ( rc >= 200 )
348 ... toggle visibility of the column rc-200 ...
349 @endcode
350
351 @param menu
352 The menu to append the items to. It may be currently empty or not.
353 @param idColumnsBase
354 The id for the menu item corresponding to the first column, the
355 other ones are consecutive starting from it. It should be positive.
356 */
357 void AddColumnsItems(wxMenu& menu, int idColumnsBase = 0);
358
359 /**
360 Show the column customization dialog.
361
362 This function displays a modal dialog containing the list of all
363 columns which the user can use to reorder them as well as show or hide
364 individual columns.
365
366 If the user accepts the changes done in the dialog, the virtual
367 methods UpdateColumnVisibility() and UpdateColumnsOrder() will be
368 called so they must be overridden in the derived class if this method
369 is ever called. Please notice that the user will be able to invoke it
370 interactively from the header popup menu if the control has both
371 wxHD_ALLOW_HIDE and wxHD_ALLOW_REORDER styles.
372
373 @see wxRearrangeDialog
374 */
375 bool ShowCustomizeDialog();
376
377 /**
378 Returns width needed for given column's title.
379
380 @since 2.9.4
381 */
382 int GetColumnTitleWidth(const wxHeaderColumn& col);
383
384 protected:
385 /**
386 Method to be implemented by the derived classes to return the
387 information for the given column.
388
389 @param idx
390 The column index, between 0 and the value last passed to
391 SetColumnCount().
392 */
393 virtual const wxHeaderColumn& GetColumn(unsigned int idx) const = 0;
394
395 /**
396 Method called when the column visibility is changed by the user.
397
398 This method is called from ShowColumnsMenu() or ShowCustomizeDialog()
399 when the user interactively hides or shows a column. A typical
400 implementation will simply update the internally stored column state.
401 Notice that there is no need to call UpdateColumn() from this method as
402 it is already done by wxHeaderCtrl itself.
403
404 The base class version doesn't do anything and must be overridden if
405 this method is called.
406
407 @param idx
408 The index of the column whose visibility was toggled.
409 @param show
410 The new visibility value, @true if the column is now shown or
411 @false if it is not hidden.
412 */
413 virtual void UpdateColumnVisibility(unsigned int idx, bool show);
414
415 /**
416 Method called when the columns order is changed in the customization
417 dialog.
418
419 This method is only called from ShowCustomizeDialog() when the user
420 changes the order of columns. In particular it is @em not called if a
421 single column changes place because the user dragged it to the new
422 location, the EVT_HEADER_END_REORDER event handler should be used to
423 react to this.
424
425 A typical implementation in a derived class will update the display
426 order of the columns in the associated control, if any. Notice that
427 there is no need to call SetColumnsOrder() from it as wxHeaderCtrl does
428 it itself.
429
430 The base class version doesn't do anything and must be overridden if
431 this method is called.
432
433 @param order
434 The new column order. This array uses the same convention as
435 SetColumnsOrder().
436 */
437 virtual void UpdateColumnsOrder(const wxArrayInt& order);
438
439 /**
440 Method which may be implemented by the derived classes to allow double
441 clicking the column separator to resize the column to fit its contents.
442
443 When a separator is double clicked, the default handler of
444 EVT_HEADER_SEPARATOR_DCLICK event calls this function and refreshes the
445 column if it returns @true so to implement the resizing of the column
446 to fit its width on header double click you need to implement this
447 method using logic similar to this example:
448 @code
449 class MyHeaderColumn : public wxHeaderColumn
450 {
451 public:
452 ...
453
454 void SetWidth(int width) { m_width = width; }
455 virtual int GetWidth() const { return m_width; }
456
457 private:
458 int m_width;
459 };
460
461 class MyHeaderCtrl : public wxHeaderCtrl
462 {
463 public:
464 protected:
465 virtual wxHeaderColumn& GetColumn(unsigned int idx) const
466 {
467 return m_cols[idx];
468 }
469
470 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
471 {
472 int widthContents = ... compute minimal width for column idx ...
473 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
474 return true;
475 }
476
477 wxVector<MyHeaderColumn> m_cols;
478 };
479 @endcode
480
481 Base class version simply returns @false.
482
483 @param idx
484 The zero-based index of the column to update.
485 @param widthTitle
486 Contains minimal width needed to display the column header itself
487 and will usually be used as a starting point for the fitting width
488 calculation.
489
490 @return
491 @true to indicate that the column was resized, i.e. GetColumn() now
492 returns the new width value, and so must be refreshed or @false
493 meaning that the control didn't reach to the separator double click.
494 */
495 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle);
496
497 /**
498 Can be overridden in the derived class to update internal data
499 structures when the number of the columns in the control changes.
500
501 This method is called by SetColumnCount() before effectively changing
502 the number of columns.
503
504 The base class version does nothing but it is good practice to still
505 call it from the overridden version in the derived class.
506 */
507 virtual void OnColumnCountChanging(unsigned int count);
508 };
509
510
511 /**
512 @class wxHeaderCtrlSimple
513
514 wxHeaderCtrlSimple is a concrete header control which can be used directly,
515 without inheriting from it as you need to do when using wxHeaderCtrl
516 itself.
517
518 When using it, you need to use simple AppendColumn(), InsertColumn() and
519 DeleteColumn() methods instead of setting the number of columns with
520 SetColumnCount() and returning the information about them from the
521 overridden GetColumn().
522
523 @library{wxcore}
524 @category{ctrl}
525
526 @see wxHeaderCtrl
527 */
528 class wxHeaderCtrlSimple : public wxHeaderCtrl
529 {
530 public:
531 /**
532 Default constructor not creating the underlying window.
533
534 You must use Create() after creating the object using this constructor.
535 */
536 wxHeaderCtrlSimple();
537
538 /**
539 Constructor creating the window.
540
541 Please see the base class wxHeaderCtrl::Create() method for the
542 parameters description.
543 */
544 wxHeaderCtrlSimple(wxWindow *parent,
545 wxWindowID winid = wxID_ANY,
546 const wxPoint& pos = wxDefaultPosition,
547 const wxSize& size = wxDefaultSize,
548 long style = wxHD_DEFAULT_STYLE,
549 const wxString& name = wxHeaderCtrlNameStr);
550
551 /**
552 Insert the column at the given position.
553
554 @param col
555 The column to insert. Notice that because of the existence of
556 implicit conversion from wxString to wxHeaderColumn a string
557 can be passed directly here.
558 @param idx
559 The position of the new column, from 0 to GetColumnCount(). Using
560 GetColumnCount() means to append the column to the end.
561
562 @see AppendColumn()
563 */
564 void InsertColumn(const wxHeaderColumnSimple& col, unsigned int idx);
565
566 /**
567 Append the column to the end of the control.
568
569 @see InsertColumn()
570 */
571 void AppendColumn(const wxHeaderColumnSimple& col);
572
573 /**
574 Delete the column at the given position.
575
576 @see InsertColumn(), AppendColumn()
577 */
578 void DeleteColumn(unsigned int idx);
579
580 /**
581 Show or hide the column.
582
583 Initially the column is shown by default or hidden if it was added with
584 wxCOL_HIDDEN flag set.
585
586 When a column is hidden, it doesn't appear at all on the screen but its
587 index is still taken into account when working with other columns. E.g.
588 if there are three columns 0, 1 and 2 and the column 1 is hidden you
589 still need to use index 2 to refer to the last visible column.
590
591 @param idx
592 The index of the column to show or hide, from 0 to GetColumnCount().
593 @param show
594 Indicates whether the column should be shown (default) or hidden.
595 */
596 void ShowColumn(unsigned int idx, bool show = true);
597
598 /**
599 Hide the column with the given index.
600
601 This is the same as calling @code ShowColumn(idx, false) @endcode.
602
603 @param idx
604 The index of the column to show or hide, from 0 to GetColumnCount().
605 */
606 void HideColumn(unsigned int idx);
607
608 /**
609 Update the column sort indicator.
610
611 The sort indicator, if shown, is typically an arrow pointing upwards or
612 downwards depending on whether the control contents is sorted in
613 ascending or descending order.
614
615 @param idx
616 The column to set the sort indicator for.
617 If @c -1 is given, then the currently shown sort indicator
618 will be removed.
619 @param sortOrder
620 If @true or @false show the sort indicator corresponding to
621 ascending or descending sort order respectively.
622 */
623 void ShowSortIndicator(unsigned int idx, bool sortOrder = true);
624
625 /**
626 Remove the sort indicator from the column being used as sort key.
627
628 @see ShowSortIndicator
629 */
630 void RemoveSortIndicator();
631
632 protected:
633 /**
634 This function can be overridden in the classes deriving from this
635 control instead of overriding UpdateColumnWidthToFit().
636
637 To implement automatic column resizing to fit its contents width when
638 the column divider is double clicked, you need to simply return the
639 fitting width for the given column @a idx from this method, the control
640 will automatically use the biggest value between the one returned from
641 here and the one needed for the display of the column title itself.
642
643 The base class version returns -1 indicating that this function is not
644 implemented.
645 */
646 virtual int GetBestFittingWidth(unsigned int idx) const;
647 };
648
649 /**
650 @class wxHeaderCtrlEvent
651
652 Event class representing the events generated by wxHeaderCtrl.
653
654 @library{wxcore}
655 @category{events}
656
657 @see wxHeaderCtrl
658 */
659 class wxHeaderCtrlEvent : public wxNotifyEvent
660 {
661 public:
662 /**
663 Return the index of the column affected by this event.
664
665 This method can be called for all header control events.
666 */
667 int GetColumn() const;
668
669 /**
670 Return the current width of the column.
671
672 This method can only be called for the dragging events.
673 */
674 int GetWidth() const;
675
676 /**
677 Return the new order of the column.
678
679 This method can only be called for end reorder event for which it
680 indicates the tentative new position for the column GetColumn()
681 selected by the user. If the event is not vetoed, this will become the
682 new column position in wxHeaderCtrl::GetColumnsOrder().
683 */
684 unsigned int GetNewOrder() const;
685 };