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