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