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