]> git.saurik.com Git - wxWidgets.git/blame - include/wx/headerctrl.h
add support for sorting to grid columns
[wxWidgets.git] / include / wx / headerctrl.h
CommitLineData
56873923
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/headerctrl.h
3// Purpose: wxHeaderCtrlBase class: 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#ifndef _WX_HEADERCTRL_H_
12#define _WX_HEADERCTRL_H_
13
14#include "wx/control.h"
15
702f5349 16#include "wx/dynarray.h"
e2bfe673
VZ
17#include "wx/vector.h"
18
56873923
VZ
19#include "wx/headercol.h"
20
21// notice that the classes in this header are defined in the core library even
22// although currently they're only used by wxGrid which is in wxAdv because we
23// plan to use it in wxListCtrl which is in core too in the future
3bfaa5a7 24class WXDLLIMPEXP_FWD_CORE wxHeaderCtrlEvent;
56873923
VZ
25
26// ----------------------------------------------------------------------------
27// constants
28// ----------------------------------------------------------------------------
29
30enum
31{
32 // allow column drag and drop
33 wxHD_DRAGDROP = 0x0001,
34
35 // style used by default when creating the control
36 wxHD_DEFAULT_STYLE = wxHD_DRAGDROP
37};
38
39extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr[];
40
56873923
VZ
41// ----------------------------------------------------------------------------
42// wxHeaderCtrlBase defines the interface of a header control
43// ----------------------------------------------------------------------------
44
45class WXDLLIMPEXP_CORE wxHeaderCtrlBase : public wxControl
46{
47public:
48 /*
49 Derived classes must provide default ctor as well as a ctor and
50 Create() function with the following signatures:
51
52 wxHeaderCtrl(wxWindow *parent,
53 wxWindowID winid = wxID_ANY,
54 const wxPoint& pos = wxDefaultPosition,
55 const wxSize& size = wxDefaultSize,
e2bfe673 56 long style = wxHD_DEFAULT_STYLE,
56873923
VZ
57 const wxString& name = wxHeaderCtrlNameStr);
58
59 bool Create(wxWindow *parent,
60 wxWindowID winid = wxID_ANY,
61 const wxPoint& pos = wxDefaultPosition,
62 const wxSize& size = wxDefaultSize,
e2bfe673 63 long style = wxHD_DEFAULT_STYLE,
56873923
VZ
64 const wxString& name = wxHeaderCtrlNameStr);
65 */
66
e2bfe673
VZ
67 // column-related methods
68 // ----------------------
69
70 // set the number of columns in the control
71 //
72 // this also calls UpdateColumn() for all columns
4635abac 73 void SetColumnCount(unsigned int count);
56873923 74
e2bfe673 75 // return the number of columns in the control as set by SetColumnCount()
56873923
VZ
76 unsigned int GetColumnCount() const { return DoGetCount(); }
77
78 // return whether the control has any columns
e2bfe673
VZ
79 bool IsEmpty() const { return DoGetCount() == 0; }
80
81 // update the column with the given index
82 void UpdateColumn(unsigned int idx)
83 {
84 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
85
86 DoUpdate(idx);
87 }
88
702f5349
VZ
89 // set the columns order: the array defines the column index which appears
90 // the given position, it must have GetColumnCount() elements and contain
91 // all indices exactly once
92 void SetColumnsOrder(const wxArrayInt& order);
93 wxArrayInt GetColumnsOrder() const;
94
95 // get the index of the column at the given display position
96 unsigned int GetColumnAt(unsigned int pos) const;
97
98 // get the position at which this column is currently displayed
99 unsigned int GetColumnPos(unsigned int idx) const;
100
1bb74626
VZ
101 // helper function used by the generic version of this control and also
102 // wxGrid: reshuffles the array of column indices indexed by positions
103 // (i.e. using the same convention as for SetColumnsOrder()) so that the
104 // column with the given index is found at the specified position
105 static void MoveColumnInOrderArray(wxArrayInt& order,
106 unsigned int idx,
107 unsigned int pos);
108
e2bfe673
VZ
109
110 // implementation only from now on
111 // -------------------------------
112
113 // the user doesn't need to TAB to this control
114 virtual bool AcceptsFocusFromKeyboard() const { return false; }
115
116 // this method is only overridden in order to synchronize the control with
117 // the main window when it is scrolled, the derived class must implement
118 // DoScrollHorz()
119 virtual void ScrollWindow(int dx, int dy, const wxRect *rect = NULL);
120
121protected:
122 // this method must be implemented by the derived classes to return the
123 // information for the given column
dcb6cbec 124 virtual wxHeaderColumn& GetColumn(unsigned int idx) = 0;
e2bfe673 125
3bfaa5a7
VZ
126 // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK
127 // handler to update the fitting column width of the given column, it
128 // should return true if the width was really updated
129 virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx),
130 int WXUNUSED(widthTitle))
131 {
132 return false;
133 }
134
4635abac
VZ
135 // this method can be overridden in the derived classes to do something
136 // (e.g. update/resize some internal data structures) before the number of
137 // columns in the control changes
138 virtual void OnColumnCountChanging(unsigned int WXUNUSED(count)) { }
139
e2bfe673
VZ
140private:
141 // methods implementing our public API and defined in platform-specific
142 // implementations
143 virtual void DoSetCount(unsigned int count) = 0;
144 virtual unsigned int DoGetCount() const = 0;
145 virtual void DoUpdate(unsigned int idx) = 0;
146
147 virtual void DoScrollHorz(int dx) = 0;
148
702f5349
VZ
149 virtual void DoSetColumnsOrder(const wxArrayInt& order) = 0;
150 virtual wxArrayInt DoGetColumnsOrder() const = 0;
151
e2bfe673
VZ
152 // this window doesn't look nice with the border so don't use it by default
153 virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
3bfaa5a7
VZ
154
155 // event handlers
156 void OnSeparatorDClick(wxHeaderCtrlEvent& event);
157
158 DECLARE_EVENT_TABLE()
e2bfe673
VZ
159};
160
161// ----------------------------------------------------------------------------
162// wxHeaderCtrl: port-specific header control implementation, notice that this
163// is still an ABC which is meant to be used as part of another
164// control, see wxHeaderCtrlSimple for a standalone version
165// ----------------------------------------------------------------------------
166
70405f7e 167#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
e2bfe673
VZ
168 #include "wx/msw/headerctrl.h"
169#else
170 #define wxHAS_GENERIC_HEADERCTRL
171 #include "wx/generic/headerctrlg.h"
172#endif // platform
173
174// ----------------------------------------------------------------------------
175// wxHeaderCtrlSimple: concrete header control which can be used standalone
176// ----------------------------------------------------------------------------
177
178class WXDLLIMPEXP_CORE wxHeaderCtrlSimple : public wxHeaderCtrl
179{
180public:
181 // control creation
182 // ----------------
183
184 wxHeaderCtrlSimple() { Init(); }
185 wxHeaderCtrlSimple(wxWindow *parent,
186 wxWindowID winid = wxID_ANY,
187 const wxPoint& pos = wxDefaultPosition,
188 const wxSize& size = wxDefaultSize,
189 long style = wxHD_DEFAULT_STYLE,
190 const wxString& name = wxHeaderCtrlNameStr)
191 {
192 Init();
193
194 Create(parent, winid, pos, size, style, name);
195 }
196
197 // managing the columns
198 // --------------------
56873923
VZ
199
200 // insert the column at the given position, using GetColumnCount() as
201 // position appends it at the end
e2bfe673 202 void InsertColumn(const wxHeaderColumnSimple& col, unsigned int idx)
56873923
VZ
203 {
204 wxCHECK_RET( idx <= GetColumnCount(), "invalid column index" );
205
206 DoInsert(col, idx);
207 }
208
209 // append the column to the end of the control
e2bfe673 210 void AppendColumn(const wxHeaderColumnSimple& col)
56873923
VZ
211 {
212 DoInsert(col, GetColumnCount());
213 }
214
215 // delete the column at the given index
216 void DeleteColumn(unsigned int idx)
217 {
218 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
219
220 DoDelete(idx);
221 }
222
223 // delete all the existing columns
224 void DeleteAllColumns();
225
226
227 // modifying columns
228 // -----------------
229
a0009205
VZ
230 // show or hide the column, notice that even when a column is hidden we
231 // still account for it when using indices
232 void ShowColumn(unsigned int idx, bool show = true)
233 {
234 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
235
236 DoShowColumn(idx, show);
237 }
238
239 void HideColumn(unsigned int idx)
240 {
241 ShowColumn(idx, false);
242 }
243
e2bfe673
VZ
244 // indicate that the column is used for sorting
245 void ShowSortIndicator(unsigned int idx, bool ascending = true)
56873923 246 {
a0009205
VZ
247 wxCHECK_RET( idx < GetColumnCount(), "invalid column index" );
248
e2bfe673 249 DoShowSortIndicator(idx, ascending);
56873923
VZ
250 }
251
e2bfe673
VZ
252 // remove the sort indicator completely
253 void RemoveSortIndicator();
56873923 254
e2bfe673 255protected:
e5a16353 256 // implement/override base class methods
dcb6cbec 257 virtual wxHeaderColumn& GetColumn(unsigned int idx);
e5a16353
VZ
258 virtual bool UpdateColumnWidthToFit(unsigned int idx, int widthTitle);
259
260 // and define another one to be overridden in the derived classes: it
261 // should return the best width for the given column contents or -1 if not
262 // implemented, we use it to implement UpdateColumnWidthToFit()
263 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx)) const
264 {
265 return -1;
266 }
56873923 267
e2bfe673
VZ
268private:
269 // functions implementing our public API
270 void DoInsert(const wxHeaderColumnSimple& col, unsigned int idx);
271 void DoDelete(unsigned int idx);
272 void DoShowColumn(unsigned int idx, bool show);
273 void DoShowSortIndicator(unsigned int idx, bool ascending);
56873923 274
e2bfe673
VZ
275 // common part of all ctors
276 void Init();
56873923 277
e2bfe673
VZ
278 // bring the column count in sync with the number of columns we store
279 void UpdateColumnCount() { SetColumnCount(m_cols.size()); }
d8fc3398 280
a3e0efb6 281
e2bfe673
VZ
282 // all our current columns
283 typedef wxVector<wxHeaderColumnSimple> Columns;
284 Columns m_cols;
56873923 285
e2bfe673
VZ
286 // the column currently used for sorting or -1 if none
287 unsigned int m_sortKey;
288
289
290 DECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple)
291};
56873923 292
fa3d4aaf
VZ
293// ----------------------------------------------------------------------------
294// wxHeaderCtrl events
295// ----------------------------------------------------------------------------
296
297class WXDLLIMPEXP_CORE wxHeaderCtrlEvent : public wxNotifyEvent
298{
299public:
300 wxHeaderCtrlEvent(wxEventType commandType = wxEVT_NULL, int winid = 0)
aef252d9
VZ
301 : wxNotifyEvent(commandType, winid),
302 m_col(-1),
303 m_width(0),
565804f2 304 m_order(static_cast<unsigned int>(-1))
fa3d4aaf
VZ
305 {
306 }
307
308 wxHeaderCtrlEvent(const wxHeaderCtrlEvent& event)
309 : wxNotifyEvent(event),
aef252d9
VZ
310 m_col(event.m_col),
311 m_width(event.m_width),
565804f2 312 m_order(event.m_order)
fa3d4aaf
VZ
313 {
314 }
315
aef252d9 316 // the column which this event pertains to: valid for all header events
fa3d4aaf
VZ
317 int GetColumn() const { return m_col; }
318 void SetColumn(int col) { m_col = col; }
319
aef252d9
VZ
320 // the width of the column: valid for column resizing/dragging events only
321 int GetWidth() const { return m_width; }
322 void SetWidth(int width) { m_width = width; }
323
702f5349
VZ
324 // the new position of the column: for end reorder events only
325 unsigned int GetNewOrder() const { return m_order; }
326 void SetNewOrder(unsigned int order) { m_order = order; }
327
fa3d4aaf
VZ
328 virtual wxEvent *Clone() const { return new wxHeaderCtrlEvent(*this); }
329
330protected:
331 // the column affected by the event
332 int m_col;
333
aef252d9
VZ
334 // the current width for the dragging events
335 int m_width;
336
702f5349
VZ
337 // the new column position for end reorder event
338 unsigned int m_order;
339
fa3d4aaf
VZ
340private:
341 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent)
342};
343
344
056d5a89
VZ
345extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_CLICK;
346extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK;
347extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK;
fa3d4aaf 348
056d5a89
VZ
349extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_DCLICK;
350extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK;
351extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK;
fa3d4aaf 352
3bfaa5a7
VZ
353extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
354
396825dc
VZ
355extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE;
356extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_RESIZING;
357extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE;
aef252d9 358
702f5349
VZ
359extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER;
360extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_END_REORDER;
361
565804f2
VZ
362extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED;
363
fa3d4aaf
VZ
364typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction)(wxHeaderCtrlEvent&);
365
366#define wxHeaderCtrlEventHandler(func) \
367 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent( \
368 wxHeaderCtrlEventFunction, &func)
369
370#define wx__DECLARE_HEADER_EVT(evt, id, fn) \
371 wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
372
373#define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
374#define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
375#define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
376
377#define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
378#define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
379#define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
380
3bfaa5a7
VZ
381#define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
382
396825dc
VZ
383#define EVT_HEADER_BEGIN_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_RESIZE, id, fn)
384#define EVT_HEADER_RESIZING(id, fn) wx__DECLARE_HEADER_EVT(RESIZING, id, fn)
385#define EVT_HEADER_END_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(END_RESIZE, id, fn)
aef252d9 386
702f5349
VZ
387#define EVT_HEADER_BEGIN_REORDER(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_REORDER, id, fn)
388#define EVT_HEADER_END_REORDER(id, fn) wx__DECLARE_HEADER_EVT(END_REORDER, id, fn)
389
565804f2
VZ
390#define EVT_HEADER_DRAGGING_CANCELLED(id, fn) wx__DECLARE_HEADER_EVT(DRAGGING_CANCELLED, id, fn)
391
56873923 392#endif // _WX_HEADERCTRL_H_