document UpdateColumn()
[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 @endEventTable
90
91 @library{wxcore}
92 @category{ctrl}
93
94 @see wxGrid, wxListCtrl, wxDataViewCtrl
95
96
97 @section headerctrl_improvements Future Improvements
98
99 Some features are supported by the native MSW control and so could be
100 easily implemented in this version of wxHeaderCtrl but need to be
101 implemented in the generic version as well to be really useful. Please let
102 us know if you need or, better, plan to work on implementing, any of them:
103 - Displaying bitmaps instead of or together with the text
104 - Custom drawn headers
105 - Filters associated with a column.
106 */
107 class wxHeaderCtrl
108 {
109 public:
110 /**
111 Default constructor not creating the underlying window.
112
113 You must use Create() after creating the object using this constructor.
114 */
115 wxHeaderCtrl();
116
117 /**
118 Constructor creating the window.
119
120 Please see Create() for the parameters documentation.
121 */
122 wxHeaderCtrl(wxWindow *parent,
123 wxWindowID winid = wxID_ANY,
124 const wxPoint& pos = wxDefaultPosition,
125 const wxSize& size = wxDefaultSize,
126 long style = wxHD_DEFAULT_STYLE,
127 const wxString& name = wxHeaderCtrlNameStr);
128
129 /**
130 Create the header control window.
131
132 @param parent
133 The parent window. The header control should be typically
134 positioned along the top edge of this window.
135 @param winid
136 Id of the control or @c wxID_ANY if you don't care.
137 @param pos
138 The initial position of the control.
139 @param size
140 The initial size of the control (usually not very useful as this
141 control will typically be resized to have the same width as the
142 associated data display control).
143 @param style
144 The control style, @c wxHD_DEFAULT_STYLE by default. Notice that
145 the default style allows the user to reorder the columns by
146 dragging them and you need to explicitly turn this feature off by
147 using @code wxHD_DEFAULT_STYLE & ~wxHD_DRAGDROP @endcode if this is
148 undesirable.
149 @param name
150 The name of the control.
151 */
152 bool Create(wxWindow *parent,
153 wxWindowID winid = wxID_ANY,
154 const wxPoint& pos = wxDefaultPosition,
155 const wxSize& size = wxDefaultSize,
156 long style = wxHD_DEFAULT_STYLE,
157 const wxString& name = wxHeaderCtrlNameStr);
158
159 /**
160 Set the number of columns in the control.
161
162 The control will use GetColumn() to get information about all the
163 new columns and refresh itself, i.e. this method also has the same
164 effect as calling UpdateColumn() for all columns but it should only be
165 used if the number of columns really changed.
166 */
167 void SetColumnCount(unsigned int count);
168
169 /**
170 Return the number of columns in the control.
171
172 @return
173 Number of columns as previously set by SetColumnCount().
174
175 @see IsEmpty()
176 */
177 unsigned int GetColumnCount() const;
178
179 /**
180 Return whether the control has any columns.
181
182 @see GetColumnCount()
183 */
184 bool IsEmpty() const;
185
186 /**
187 Update the column with the given index.
188
189 When the value returned by GetColumn() changes, this method must be
190 called to notify the control about the change and update the visual
191 display to match the new column data.
192
193 @param idx
194 The column index, must be less than GetColumnCount().
195 */
196 void UpdateColumn(unsigned int idx);
197
198 protected:
199 /**
200 Method to be implemented by the derived classes to return the
201 information for the given column.
202
203 @param idx
204 The column index, between 0 and the value last passed to
205 SetColumnCount().
206 */
207 virtual wxHeaderColumnBase& GetColumn(unsigned int idx) = 0;
208
209 /**
210 Method which may be implemented by the derived classes to allow double
211 clicking the column separator to resize the column to fit its contents.
212
213 When a separator is double clicked, the default handler of
214 EVT_HEADER_SEPARATOR_DCLICK event calls this function and refreshes the
215 column if it returns @true so to implement the resizing of the column
216 to fit its width on header double click you need to implement this
217 method using logic similar to this example:
218 @code
219 class MyHeaderCtrl : public wxHeaderColumnBase
220 {
221 public:
222 ...
223
224 void SetWidth(int width) { m_width = width; }
225 virtual int GetWidth() const { return m_width; }
226
227 private:
228 int m_width;
229 };
230
231 class MyHeaderCtrl : public wxHeaderCtrl
232 {
233 public:
234 protected:
235 virtual wxHeaderColumnBase& GetColumn(unsigned int idx)
236 {
237 return m_cols[idx];
238 }
239
240 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
241 {
242 int widthContents = ... compute minimal width for column idx ...
243 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
244 return true;
245 }
246
247 wxVector<MyHeaderColumn> m_cols;
248 };
249 @endcode
250
251 Base class version simply returns @false.
252
253 @param width
254 Contains minimal width needed to display the column header itself
255 and will usually be used as a starting point for the fitting width
256 calculation.
257 @return
258 @true to indicate that the column was resized, i.e. GetColumn() now
259 returns the new width value, and so must be refreshed or @false
260 meaning that the control didn't reach to the separator double click.
261 */
262 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle);
263 };
264
265
266 /**
267 @class wxHeaderCtrlSimple
268
269 wxHeaderCtrlSimple is a concrete header control which can be used directly,
270 without inheriting from it as you need to do when using wxHeaderCtrl
271 itself.
272
273 When using it, you need to use simple AppendColumn(), InsertColumn() and
274 DeleteColumn() methods instead of setting the number of columns with
275 SetColumnCount() and returning the information about them from the
276 overridden GetColumn().
277
278 @library{wxcore}
279 @category{ctrl}
280
281 @see wxHeaderCtrl
282 */
283 class wxHeaderCtrlSimple : public wxHeaderCtrl
284 {
285 public:
286 /**
287 Default constructor not creating the underlying window.
288
289 You must use Create() after creating the object using this constructor.
290 */
291 wxHeaderCtrlSimple();
292
293 /**
294 Constructor creating the window.
295
296 Please see the base class wxHeaderCtrl::Create() method for the
297 parameters description.
298 */
299 wxHeaderCtrlSimple(wxWindow *parent,
300 wxWindowID winid = wxID_ANY,
301 const wxPoint& pos = wxDefaultPosition,
302 const wxSize& size = wxDefaultSize,
303 long style = wxHD_DEFAULT_STYLE,
304 const wxString& name = wxHeaderCtrlNameStr);
305
306 /**
307 Insert the column at the given position.
308
309 @param col
310 The column to insert. Notice that because of the existence of
311 implicit conversion from wxString to wxHeaderColumn a string
312 can be passed directly here.
313 @param idx
314 The position of the new column, from 0 to GetColumnCount(). Using
315 GetColumnCount() means to append the column to the end.
316
317 @see AppendColumn()
318 */
319 void InsertColumn(const wxHeaderColumn& col, unsigned int idx);
320
321 /**
322 Append the column to the end of the control.
323
324 @see InsertColumn()
325 */
326 void AppendColumn(const wxHeaderColumn& col);
327
328 /**
329 Delete the column at the given position.
330
331 @see InsertColumn(), AppendColumn()
332 */
333 void DeleteColumn(unsigned int idx);
334
335 /**
336 Show or hide the column.
337
338 Initially the column is shown by default or hidden if it was added with
339 wxCOL_HIDDEN flag set.
340
341 When a column is hidden, it doesn't appear at all on the screen but its
342 index is still taken into account when working with other columns. E.g.
343 if there are three columns 0, 1 and 2 and the column 1 is hidden you
344 still need to use index 2 to refer to the last visible column.
345
346 @param idx
347 The index of the column to show or hide, from 0 to GetColumnCount().
348 @param show
349 Indicates whether the column should be shown (default) or hidden.
350 */
351 void ShowColumn(unsigned int idx, bool show = true);
352
353 /**
354 Hide the column with the given index.
355
356 This is the same as calling @code ShowColumn(idx, false) @endcode.
357
358 @param idx
359 The index of the column to show or hide, from 0 to GetColumnCount().
360 */
361 void HideColumn(unsigned int idx);
362
363 /**
364 Update the column sort indicator.
365
366 The sort indicator, if shown, is typically an arrow pointing upwards or
367 downwards depending on whether the control contents is sorted in
368 ascending or descending order.
369
370 @param idx
371 The column to set the sort indicator for.
372 @param sortOrder
373 If @true or @false show the sort indicator corresponding to
374 ascending or descending sort order respectively, if @c -1 remove
375 the currently shown sort indicator.
376 */
377 virtual void ShowSortIndicator(unsigned int idx, int sortOrder);
378
379 /**
380 Remove the sort indicator from the given column.
381
382 This is the same as calling ShowSortIndicator() with @c -1 argument.
383
384 @param idx
385 The column to remove sort indicator for.
386 */
387 void RemoveSortIndicator(unsigned int idx);
388
389 protected:
390 /**
391 This function can be overridden in the classes deriving from this
392 control instead of overriding UpdateColumnWidthToFit().
393
394 To implement automatic column resizing to fit its contents width when
395 the column divider is double clicked, you need to simply return the
396 fitting width for the given column @a idx from this method, the control
397 will automatically use the biggest value between the one returned from
398 here and the one needed for the display of the column title itself.
399
400 The base class version returns -1 indicating that this function is not
401 implemented.
402 */
403 virtual int GetBestFittingWidth(unsigned int idx) const;
404 };
405
406 /**
407 @class wxHeaderCtrlEvent
408
409 Event class representing the events generated by wxHeaderCtrl.
410
411 @library{wxcore}
412 @category{ctrl}
413
414 @see wxHeaderCtrl
415 */
416 class wxHeaderCtrlEvent : public wxNotifyEvent
417 {
418 public:
419 /**
420 Return the index of the column affected by this event.
421
422 This method can be called for all header control events.
423 */
424 int GetColumn() const;
425
426 /**
427 Return the current width of the column.
428
429 This method can only be called for the dragging events.
430 */
431 int GetWidth() const;
432
433 /**
434 Return @true if the drag operation was cancelled.
435
436 This method can only be called for the end drag event.
437 */
438 bool IsCancelled() const;
439 };