1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/headerctrl.h
3 // Purpose: wxHeaderCtrlBase class: interface of wxHeaderCtrl
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_HEADERCTRL_H_
12 #define _WX_HEADERCTRL_H_
14 #include "wx/control.h"
16 #include "wx/dynarray.h"
17 #include "wx/vector.h"
19 #include "wx/headercol.h"
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
;
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
32 // allow column drag and drop
33 wxHD_DRAGDROP
= 0x0001,
35 // style used by default when creating the control
36 wxHD_DEFAULT_STYLE
= wxHD_DRAGDROP
39 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[];
41 // ----------------------------------------------------------------------------
42 // wxHeaderCtrlBase defines the interface of a header control
43 // ----------------------------------------------------------------------------
45 class WXDLLIMPEXP_CORE wxHeaderCtrlBase
: public wxControl
49 Derived classes must provide default ctor as well as a ctor and
50 Create() function with the following signatures:
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);
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);
67 // column-related methods
68 // ----------------------
70 // set the number of columns in the control
72 // this also calls UpdateColumn() for all columns
73 void SetColumnCount(unsigned int count
);
75 // return the number of columns in the control as set by SetColumnCount()
76 unsigned int GetColumnCount() const { return DoGetCount(); }
78 // return whether the control has any columns
79 bool IsEmpty() const { return DoGetCount() == 0; }
81 // update the column with the given index
82 void UpdateColumn(unsigned int idx
)
84 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
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;
95 // get the index of the column at the given display position
96 unsigned int GetColumnAt(unsigned int pos
) const;
98 // get the position at which this column is currently displayed
99 unsigned int GetColumnPos(unsigned int idx
) const;
101 // reset the columns order to the natural one
102 void ResetColumnsOrder();
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
,
113 // implementation only from now on
114 // -------------------------------
116 // the user doesn't need to TAB to this control
117 virtual bool AcceptsFocusFromKeyboard() const { return false; }
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
122 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
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;
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
))
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
)) { }
144 // helper function for the derived classes: update the array of column
145 // indices after the number of columns changed
146 void DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
);
149 // methods implementing our public API and defined in platform-specific
151 virtual void DoSetCount(unsigned int count
) = 0;
152 virtual unsigned int DoGetCount() const = 0;
153 virtual void DoUpdate(unsigned int idx
) = 0;
155 virtual void DoScrollHorz(int dx
) = 0;
157 virtual void DoSetColumnsOrder(const wxArrayInt
& order
) = 0;
158 virtual wxArrayInt
DoGetColumnsOrder() const = 0;
160 // this window doesn't look nice with the border so don't use it by default
161 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
164 void OnSeparatorDClick(wxHeaderCtrlEvent
& event
);
166 DECLARE_EVENT_TABLE()
169 // ----------------------------------------------------------------------------
170 // wxHeaderCtrl: port-specific header control implementation, notice that this
171 // is still an ABC which is meant to be used as part of another
172 // control, see wxHeaderCtrlSimple for a standalone version
173 // ----------------------------------------------------------------------------
175 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
176 #include "wx/msw/headerctrl.h"
178 #define wxHAS_GENERIC_HEADERCTRL
179 #include "wx/generic/headerctrlg.h"
182 // ----------------------------------------------------------------------------
183 // wxHeaderCtrlSimple: concrete header control which can be used standalone
184 // ----------------------------------------------------------------------------
186 class WXDLLIMPEXP_CORE wxHeaderCtrlSimple
: public wxHeaderCtrl
192 wxHeaderCtrlSimple() { Init(); }
193 wxHeaderCtrlSimple(wxWindow
*parent
,
194 wxWindowID winid
= wxID_ANY
,
195 const wxPoint
& pos
= wxDefaultPosition
,
196 const wxSize
& size
= wxDefaultSize
,
197 long style
= wxHD_DEFAULT_STYLE
,
198 const wxString
& name
= wxHeaderCtrlNameStr
)
202 Create(parent
, winid
, pos
, size
, style
, name
);
205 // managing the columns
206 // --------------------
208 // insert the column at the given position, using GetColumnCount() as
209 // position appends it at the end
210 void InsertColumn(const wxHeaderColumnSimple
& col
, unsigned int idx
)
212 wxCHECK_RET( idx
<= GetColumnCount(), "invalid column index" );
217 // append the column to the end of the control
218 void AppendColumn(const wxHeaderColumnSimple
& col
)
220 DoInsert(col
, GetColumnCount());
223 // delete the column at the given index
224 void DeleteColumn(unsigned int idx
)
226 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
231 // delete all the existing columns
232 void DeleteAllColumns();
238 // show or hide the column, notice that even when a column is hidden we
239 // still account for it when using indices
240 void ShowColumn(unsigned int idx
, bool show
= true)
242 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
244 DoShowColumn(idx
, show
);
247 void HideColumn(unsigned int idx
)
249 ShowColumn(idx
, false);
252 // indicate that the column is used for sorting
253 void ShowSortIndicator(unsigned int idx
, bool ascending
= true)
255 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
257 DoShowSortIndicator(idx
, ascending
);
260 // remove the sort indicator completely
261 void RemoveSortIndicator();
264 // implement/override base class methods
265 virtual wxHeaderColumn
& GetColumn(unsigned int idx
);
266 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
);
268 // and define another one to be overridden in the derived classes: it
269 // should return the best width for the given column contents or -1 if not
270 // implemented, we use it to implement UpdateColumnWidthToFit()
271 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx
)) const
277 // functions implementing our public API
278 void DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
);
279 void DoDelete(unsigned int idx
);
280 void DoShowColumn(unsigned int idx
, bool show
);
281 void DoShowSortIndicator(unsigned int idx
, bool ascending
);
283 // common part of all ctors
286 // bring the column count in sync with the number of columns we store
287 void UpdateColumnCount() { SetColumnCount(m_cols
.size()); }
290 // all our current columns
291 typedef wxVector
<wxHeaderColumnSimple
> Columns
;
294 // the column currently used for sorting or -1 if none
295 unsigned int m_sortKey
;
298 DECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple
)
301 // ----------------------------------------------------------------------------
302 // wxHeaderCtrl events
303 // ----------------------------------------------------------------------------
305 class WXDLLIMPEXP_CORE wxHeaderCtrlEvent
: public wxNotifyEvent
308 wxHeaderCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
309 : wxNotifyEvent(commandType
, winid
),
312 m_order(static_cast<unsigned int>(-1))
316 wxHeaderCtrlEvent(const wxHeaderCtrlEvent
& event
)
317 : wxNotifyEvent(event
),
319 m_width(event
.m_width
),
320 m_order(event
.m_order
)
324 // the column which this event pertains to: valid for all header events
325 int GetColumn() const { return m_col
; }
326 void SetColumn(int col
) { m_col
= col
; }
328 // the width of the column: valid for column resizing/dragging events only
329 int GetWidth() const { return m_width
; }
330 void SetWidth(int width
) { m_width
= width
; }
332 // the new position of the column: for end reorder events only
333 unsigned int GetNewOrder() const { return m_order
; }
334 void SetNewOrder(unsigned int order
) { m_order
= order
; }
336 virtual wxEvent
*Clone() const { return new wxHeaderCtrlEvent(*this); }
339 // the column affected by the event
342 // the current width for the dragging events
345 // the new column position for end reorder event
346 unsigned int m_order
;
349 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent
)
353 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_CLICK
;
354 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
;
355 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
;
357 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_DCLICK
;
358 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
359 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
361 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
363 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
;
364 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RESIZING
;
365 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
;
367 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
;
368 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
;
370 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
;
372 typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction
)(wxHeaderCtrlEvent
&);
374 #define wxHeaderCtrlEventHandler(func) \
375 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent( \
376 wxHeaderCtrlEventFunction, &func)
378 #define wx__DECLARE_HEADER_EVT(evt, id, fn) \
379 wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
381 #define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
382 #define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
383 #define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
385 #define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
386 #define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
387 #define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
389 #define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
391 #define EVT_HEADER_BEGIN_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_RESIZE, id, fn)
392 #define EVT_HEADER_RESIZING(id, fn) wx__DECLARE_HEADER_EVT(RESIZING, id, fn)
393 #define EVT_HEADER_END_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(END_RESIZE, id, fn)
395 #define EVT_HEADER_BEGIN_REORDER(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_REORDER, id, fn)
396 #define EVT_HEADER_END_REORDER(id, fn) wx__DECLARE_HEADER_EVT(END_REORDER, id, fn)
398 #define EVT_HEADER_DRAGGING_CANCELLED(id, fn) wx__DECLARE_HEADER_EVT(DRAGGING_CANCELLED, id, fn)
400 #endif // _WX_HEADERCTRL_H_