implement column reordering support in wxMSW wxHeaderCtrl; use it in wxDataViewCtrl...
[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 license
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 and (will be used in the near future) in
18 in wxDataViewCtrl and report view of wxListCtrl but can be also used
19 independently. In general this class is meant to be used as part of another
20 control which already stores the column information somewhere as it can't
21 be used directly: instead you need to inherit from it and implement the
22 GetColumn() method to provide column information. See wxHeaderCtrlSimple
23 for a concrete control class 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 @beginStyleTable
44 @style{wxHD_DRAGDROP}
45 If this style is specified (it is by default), the user can reorder
46 the control columns by dragging them.
47 @style{wxHD_DEFAULT_STYLE}
48 Symbolic name for the default control style, currently equal to @c
49 wxHD_DRAGDROP.
50 @endStyleTable
51
52 @beginEventTable{wxHeaderCtrlEvent}
53 @event{EVT_HEADER_CLICK(id, func)}
54 A column heading was clicked.
55 @event{EVT_HEADER_RIGHT_CLICK(id, func)}
56 A column heading was right clicked.
57 @event{EVT_HEADER_MIDDLE_CLICK(id, func)}
58 A column heading was clicked with the middle mouse button.
59
60 @event{EVT_HEADER_DCLICK(id, func)}
61 A column heading was double clicked.
62 @event{EVT_HEADER_RIGHT_DCLICK(id, func)}
63 A column heading was right double clicked.
64 @event{EVT_HEADER_MIDDLE_DCLICK(id, func)}
65 A column heading was double clicked with the middle mouse button.
66
67 @event{EVT_HEADER_SEPARATOR_DCLICK(id, func)}
68 Separator to the right of the specified column was double clicked
69 (this action is commonly used to resize the column to fit its
70 contents width and the control provides UpdateColumnWidthToFit() method
71 to make implementing this easier).
72
73 @event{EVT_HEADER_BEGIN_RESIZE(id, func)}
74 The user started to drag the separator to the right of the column
75 with the specified index (this can only happen for the columns for
76 which wxHeaderColumn::IsResizeable() returns true). The event can
77 be vetoed to prevent the column from being resized. If it isn't,
78 the resizing and end resize events will be generated later.
79 @event{EVT_HEADER_RESIZING(id, func)}
80 The user is dragging the column with the specified index resizing
81 it and its current width is wxHeaderCtrlEvent::GetWidth(). The
82 event can be vetoed to stop the dragging operation completely at
83 any time.
84 @event{EVT_HEADER_END_RESIZE(id, func)}
85 Either the user stopped dragging the column by releasing the mouse
86 or the resizing was cancelled. If wxHeaderCtrlEvent::IsCancelled()
87 returns @true, nothing should be done, otherwise the column should
88 normally be resized to the value of wxHeaderCtrlEvent::GetWidth().
89
90 @event{EVT_HEADER_BEGIN_REORDER(id, func)}
91 The user started to drag the column with the specified index (this
92 can only happen for the controls with wxHD_DRAGDROP style). This
93 event can be vetoed to prevent the column from being reordered,
94 otherwise the end reorder message will be generated later.
95 @event{EVT_HEADER_END_REORDER(id, func)}
96 Either the user dropped the column in its new location or the
97 drag operation was cancelled. If wxHeaderCtrlEvent::IsCancelled()
98 returns @true, nothing should be done, otherwise the event can be
99 vetoed to prevent the column from being placed at the new position
100 or handled to update the display of the data in the associated
101 control to match the new column location (available from
102 wxHeaderCtrlEvent::GetNewOrder()).
103 @endEventTable
104
105 @library{wxcore}
106 @category{ctrl}
107
108 @see wxGrid, wxListCtrl, wxDataViewCtrl
109
110
111 @section headerctrl_improvements Future Improvements
112
113 Some features are supported by the native MSW control and so could be
114 easily implemented in this version of wxHeaderCtrl but need to be
115 implemented in the generic version as well to be really useful. Please let
116 us know if you need or, better, plan to work on implementing, any of them:
117 - Displaying bitmaps instead of or together with the text
118 - Custom drawn headers
119 - Filters associated with a column.
120 */
121 class wxHeaderCtrl
122 {
123 public:
124 /**
125 Default constructor not creating the underlying window.
126
127 You must use Create() after creating the object using this constructor.
128 */
129 wxHeaderCtrl();
130
131 /**
132 Constructor creating the window.
133
134 Please see Create() for the parameters documentation.
135 */
136 wxHeaderCtrl(wxWindow *parent,
137 wxWindowID winid = wxID_ANY,
138 const wxPoint& pos = wxDefaultPosition,
139 const wxSize& size = wxDefaultSize,
140 long style = wxHD_DEFAULT_STYLE,
141 const wxString& name = wxHeaderCtrlNameStr);
142
143 /**
144 Create the header control window.
145
146 @param parent
147 The parent window. The header control should be typically
148 positioned along the top edge of this window.
149 @param winid
150 Id of the control or @c wxID_ANY if you don't care.
151 @param pos
152 The initial position of the control.
153 @param size
154 The initial size of the control (usually not very useful as this
155 control will typically be resized to have the same width as the
156 associated data display control).
157 @param style
158 The control style, @c wxHD_DEFAULT_STYLE by default. Notice that
159 the default style allows the user to reorder the columns by
160 dragging them and you need to explicitly turn this feature off by
161 using @code wxHD_DEFAULT_STYLE & ~wxHD_DRAGDROP @endcode if this is
162 undesirable.
163 @param name
164 The name of the control.
165 */
166 bool Create(wxWindow *parent,
167 wxWindowID winid = wxID_ANY,
168 const wxPoint& pos = wxDefaultPosition,
169 const wxSize& size = wxDefaultSize,
170 long style = wxHD_DEFAULT_STYLE,
171 const wxString& name = wxHeaderCtrlNameStr);
172
173 /**
174 Set the number of columns in the control.
175
176 The control will use GetColumn() to get information about all the
177 new columns and refresh itself, i.e. this method also has the same
178 effect as calling UpdateColumn() for all columns but it should only be
179 used if the number of columns really changed.
180 */
181 void SetColumnCount(unsigned int count);
182
183 /**
184 Return the number of columns in the control.
185
186 @return
187 Number of columns as previously set by SetColumnCount().
188
189 @see IsEmpty()
190 */
191 unsigned int GetColumnCount() const;
192
193 /**
194 Return whether the control has any columns.
195
196 @see GetColumnCount()
197 */
198 bool IsEmpty() const;
199
200 /**
201 Update the column with the given index.
202
203 When the value returned by GetColumn() changes, this method must be
204 called to notify the control about the change and update the visual
205 display to match the new column data.
206
207 @param idx
208 The column index, must be less than GetColumnCount().
209 */
210 void UpdateColumn(unsigned int idx);
211
212 /**
213 Change the columns display order.
214
215 The display order defines the order in which the columns appear on the
216 screen and does @em not affect the interpretation of indices by all the
217 other class methods.
218
219 The @a order array specifies the column indices corresponding to the
220 display positions.
221
222 @param order
223 A permutation of all column indices, i.e. an array of size
224 GetColumnsOrder() containing all column indices exactly once. The
225 n-th element of this array defines the index of the column shown at
226 the n-th position from left (for the default left-to-right writing
227 direction).
228
229 @see wxListCtrl::SetColumnsOrder()
230 */
231 void SetColumnsOrder(const wxArrayInt& order);
232
233 /**
234 Return the array describing the columns display order.
235
236 For the controls without wxHD_DRAGDROP style the returned array will be
237 the same as was passed to SetColumnsOrder() previously or define the
238 default order (with n-th element being n) if it hadn't been called. But
239 for the controls with wxHD_DRAGDROP style, the columns can be also
240 reordered by user.
241 */
242 wxArrayInt GetColumnsOrder() const;
243
244 /**
245 Return the index of the column displayed at the given position.
246
247 @param pos
248 The display position, e.g. 0 for the left-most column, 1 for the
249 next one and so on until GetColumnCount() - 1.
250
251 @see GetColumnPos()
252 */
253 unsigned int GetColumnAt(unsigned int pos) const;
254
255 /**
256 Get the position at which this column is currently displayed.
257
258 Notice that a valid position is returned even for the hidden columns
259 currently.
260
261 @param idx
262 The column index, must be less than GetColumnCount().
263
264 @see GetColumnAt()
265 */
266 unsigned int GetColumnPos(unsigned int idx) const;
267
268 protected:
269 /**
270 Method to be implemented by the derived classes to return the
271 information for the given column.
272
273 @param idx
274 The column index, between 0 and the value last passed to
275 SetColumnCount().
276 */
277 virtual wxHeaderColumnBase& GetColumn(unsigned int idx) = 0;
278
279 /**
280 Method which may be implemented by the derived classes to allow double
281 clicking the column separator to resize the column to fit its contents.
282
283 When a separator is double clicked, the default handler of
284 EVT_HEADER_SEPARATOR_DCLICK event calls this function and refreshes the
285 column if it returns @true so to implement the resizing of the column
286 to fit its width on header double click you need to implement this
287 method using logic similar to this example:
288 @code
289 class MyHeaderCtrl : public wxHeaderColumnBase
290 {
291 public:
292 ...
293
294 void SetWidth(int width) { m_width = width; }
295 virtual int GetWidth() const { return m_width; }
296
297 private:
298 int m_width;
299 };
300
301 class MyHeaderCtrl : public wxHeaderCtrl
302 {
303 public:
304 protected:
305 virtual wxHeaderColumnBase& GetColumn(unsigned int idx)
306 {
307 return m_cols[idx];
308 }
309
310 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
311 {
312 int widthContents = ... compute minimal width for column idx ...
313 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
314 return true;
315 }
316
317 wxVector<MyHeaderColumn> m_cols;
318 };
319 @endcode
320
321 Base class version simply returns @false.
322
323 @param width
324 Contains minimal width needed to display the column header itself
325 and will usually be used as a starting point for the fitting width
326 calculation.
327 @return
328 @true to indicate that the column was resized, i.e. GetColumn() now
329 returns the new width value, and so must be refreshed or @false
330 meaning that the control didn't reach to the separator double click.
331 */
332 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle);
333 };
334
335
336 /**
337 @class wxHeaderCtrlSimple
338
339 wxHeaderCtrlSimple is a concrete header control which can be used directly,
340 without inheriting from it as you need to do when using wxHeaderCtrl
341 itself.
342
343 When using it, you need to use simple AppendColumn(), InsertColumn() and
344 DeleteColumn() methods instead of setting the number of columns with
345 SetColumnCount() and returning the information about them from the
346 overridden GetColumn().
347
348 @library{wxcore}
349 @category{ctrl}
350
351 @see wxHeaderCtrl
352 */
353 class wxHeaderCtrlSimple : public wxHeaderCtrl
354 {
355 public:
356 /**
357 Default constructor not creating the underlying window.
358
359 You must use Create() after creating the object using this constructor.
360 */
361 wxHeaderCtrlSimple();
362
363 /**
364 Constructor creating the window.
365
366 Please see the base class wxHeaderCtrl::Create() method for the
367 parameters description.
368 */
369 wxHeaderCtrlSimple(wxWindow *parent,
370 wxWindowID winid = wxID_ANY,
371 const wxPoint& pos = wxDefaultPosition,
372 const wxSize& size = wxDefaultSize,
373 long style = wxHD_DEFAULT_STYLE,
374 const wxString& name = wxHeaderCtrlNameStr);
375
376 /**
377 Insert the column at the given position.
378
379 @param col
380 The column to insert. Notice that because of the existence of
381 implicit conversion from wxString to wxHeaderColumn a string
382 can be passed directly here.
383 @param idx
384 The position of the new column, from 0 to GetColumnCount(). Using
385 GetColumnCount() means to append the column to the end.
386
387 @see AppendColumn()
388 */
389 void InsertColumn(const wxHeaderColumn& col, unsigned int idx);
390
391 /**
392 Append the column to the end of the control.
393
394 @see InsertColumn()
395 */
396 void AppendColumn(const wxHeaderColumn& col);
397
398 /**
399 Delete the column at the given position.
400
401 @see InsertColumn(), AppendColumn()
402 */
403 void DeleteColumn(unsigned int idx);
404
405 /**
406 Show or hide the column.
407
408 Initially the column is shown by default or hidden if it was added with
409 wxCOL_HIDDEN flag set.
410
411 When a column is hidden, it doesn't appear at all on the screen but its
412 index is still taken into account when working with other columns. E.g.
413 if there are three columns 0, 1 and 2 and the column 1 is hidden you
414 still need to use index 2 to refer to the last visible column.
415
416 @param idx
417 The index of the column to show or hide, from 0 to GetColumnCount().
418 @param show
419 Indicates whether the column should be shown (default) or hidden.
420 */
421 void ShowColumn(unsigned int idx, bool show = true);
422
423 /**
424 Hide the column with the given index.
425
426 This is the same as calling @code ShowColumn(idx, false) @endcode.
427
428 @param idx
429 The index of the column to show or hide, from 0 to GetColumnCount().
430 */
431 void HideColumn(unsigned int idx);
432
433 /**
434 Update the column sort indicator.
435
436 The sort indicator, if shown, is typically an arrow pointing upwards or
437 downwards depending on whether the control contents is sorted in
438 ascending or descending order.
439
440 @param idx
441 The column to set the sort indicator for.
442 @param sortOrder
443 If @true or @false show the sort indicator corresponding to
444 ascending or descending sort order respectively, if @c -1 remove
445 the currently shown sort indicator.
446 */
447 virtual void ShowSortIndicator(unsigned int idx, int sortOrder);
448
449 /**
450 Remove the sort indicator from the given column.
451
452 This is the same as calling ShowSortIndicator() with @c -1 argument.
453
454 @param idx
455 The column to remove sort indicator for.
456 */
457 void RemoveSortIndicator(unsigned int idx);
458
459 protected:
460 /**
461 This function can be overridden in the classes deriving from this
462 control instead of overriding UpdateColumnWidthToFit().
463
464 To implement automatic column resizing to fit its contents width when
465 the column divider is double clicked, you need to simply return the
466 fitting width for the given column @a idx from this method, the control
467 will automatically use the biggest value between the one returned from
468 here and the one needed for the display of the column title itself.
469
470 The base class version returns -1 indicating that this function is not
471 implemented.
472 */
473 virtual int GetBestFittingWidth(unsigned int idx) const;
474 };
475
476 /**
477 @class wxHeaderCtrlEvent
478
479 Event class representing the events generated by wxHeaderCtrl.
480
481 @library{wxcore}
482 @category{ctrl}
483
484 @see wxHeaderCtrl
485 */
486 class wxHeaderCtrlEvent : public wxNotifyEvent
487 {
488 public:
489 /**
490 Return the index of the column affected by this event.
491
492 This method can be called for all header control events.
493 */
494 int GetColumn() const;
495
496 /**
497 Return the current width of the column.
498
499 This method can only be called for the dragging events.
500 */
501 int GetWidth() const;
502
503 /**
504 Return the new order of the column.
505
506 This method can only be called for end reorder event for which it
507 indicates the tentative new position for the column GetColumn()
508 selected by the user. If the event is not vetoed, this will become the
509 new column position in wxHeaderCtrl::GetColumnsOrder().
510 */
511 unsigned int GetNewOrder() const;
512
513 /**
514 Return @true if the drag operation was cancelled.
515
516 This method can only be called for the end drag event.
517 */
518 bool IsCancelled() const;
519 };