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/vector.h"
18 #include "wx/headercol.h"
20 // notice that the classes in this header are defined in the core library even
21 // although currently they're only used by wxGrid which is in wxAdv because we
22 // plan to use it in wxListCtrl which is in core too in the future
23 class WXDLLIMPEXP_FWD_CORE wxHeaderCtrlEvent
;
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
31 // allow column drag and drop
32 wxHD_DRAGDROP
= 0x0001,
34 // style used by default when creating the control
35 wxHD_DEFAULT_STYLE
= wxHD_DRAGDROP
38 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[];
40 // ----------------------------------------------------------------------------
41 // wxHeaderCtrlBase defines the interface of a header control
42 // ----------------------------------------------------------------------------
44 class WXDLLIMPEXP_CORE wxHeaderCtrlBase
: public wxControl
48 Derived classes must provide default ctor as well as a ctor and
49 Create() function with the following signatures:
51 wxHeaderCtrl(wxWindow *parent,
52 wxWindowID winid = wxID_ANY,
53 const wxPoint& pos = wxDefaultPosition,
54 const wxSize& size = wxDefaultSize,
55 long style = wxHD_DEFAULT_STYLE,
56 const wxString& name = wxHeaderCtrlNameStr);
58 bool Create(wxWindow *parent,
59 wxWindowID winid = wxID_ANY,
60 const wxPoint& pos = wxDefaultPosition,
61 const wxSize& size = wxDefaultSize,
62 long style = wxHD_DEFAULT_STYLE,
63 const wxString& name = wxHeaderCtrlNameStr);
66 // column-related methods
67 // ----------------------
69 // set the number of columns in the control
71 // this also calls UpdateColumn() for all columns
72 void SetColumnCount(unsigned int count
) { DoSetCount(count
); }
74 // return the number of columns in the control as set by SetColumnCount()
75 unsigned int GetColumnCount() const { return DoGetCount(); }
77 // return whether the control has any columns
78 bool IsEmpty() const { return DoGetCount() == 0; }
80 // update the column with the given index
81 void UpdateColumn(unsigned int idx
)
83 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
89 // implementation only from now on
90 // -------------------------------
92 // the user doesn't need to TAB to this control
93 virtual bool AcceptsFocusFromKeyboard() const { return false; }
95 // this method is only overridden in order to synchronize the control with
96 // the main window when it is scrolled, the derived class must implement
98 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
101 // this method must be implemented by the derived classes to return the
102 // information for the given column
103 virtual wxHeaderColumnBase
& GetColumn(unsigned int idx
) = 0;
105 // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK
106 // handler to update the fitting column width of the given column, it
107 // should return true if the width was really updated
108 virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx
),
109 int WXUNUSED(widthTitle
))
115 // methods implementing our public API and defined in platform-specific
117 virtual void DoSetCount(unsigned int count
) = 0;
118 virtual unsigned int DoGetCount() const = 0;
119 virtual void DoUpdate(unsigned int idx
) = 0;
121 virtual void DoScrollHorz(int dx
) = 0;
123 // this window doesn't look nice with the border so don't use it by default
124 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
127 void OnSeparatorDClick(wxHeaderCtrlEvent
& event
);
129 DECLARE_EVENT_TABLE()
132 // ----------------------------------------------------------------------------
133 // wxHeaderCtrl: port-specific header control implementation, notice that this
134 // is still an ABC which is meant to be used as part of another
135 // control, see wxHeaderCtrlSimple for a standalone version
136 // ----------------------------------------------------------------------------
138 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
139 #include "wx/msw/headerctrl.h"
141 #define wxHAS_GENERIC_HEADERCTRL
142 #include "wx/generic/headerctrlg.h"
145 // ----------------------------------------------------------------------------
146 // wxHeaderCtrlSimple: concrete header control which can be used standalone
147 // ----------------------------------------------------------------------------
149 class WXDLLIMPEXP_CORE wxHeaderCtrlSimple
: public wxHeaderCtrl
155 wxHeaderCtrlSimple() { Init(); }
156 wxHeaderCtrlSimple(wxWindow
*parent
,
157 wxWindowID winid
= wxID_ANY
,
158 const wxPoint
& pos
= wxDefaultPosition
,
159 const wxSize
& size
= wxDefaultSize
,
160 long style
= wxHD_DEFAULT_STYLE
,
161 const wxString
& name
= wxHeaderCtrlNameStr
)
165 Create(parent
, winid
, pos
, size
, style
, name
);
168 // managing the columns
169 // --------------------
171 // insert the column at the given position, using GetColumnCount() as
172 // position appends it at the end
173 void InsertColumn(const wxHeaderColumnSimple
& col
, unsigned int idx
)
175 wxCHECK_RET( idx
<= GetColumnCount(), "invalid column index" );
180 // append the column to the end of the control
181 void AppendColumn(const wxHeaderColumnSimple
& col
)
183 DoInsert(col
, GetColumnCount());
186 // delete the column at the given index
187 void DeleteColumn(unsigned int idx
)
189 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
194 // delete all the existing columns
195 void DeleteAllColumns();
201 // show or hide the column, notice that even when a column is hidden we
202 // still account for it when using indices
203 void ShowColumn(unsigned int idx
, bool show
= true)
205 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
207 DoShowColumn(idx
, show
);
210 void HideColumn(unsigned int idx
)
212 ShowColumn(idx
, false);
215 // indicate that the column is used for sorting
216 void ShowSortIndicator(unsigned int idx
, bool ascending
= true)
218 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
220 DoShowSortIndicator(idx
, ascending
);
223 // remove the sort indicator completely
224 void RemoveSortIndicator();
227 // implement/override base class methods
228 virtual wxHeaderColumnBase
& GetColumn(unsigned int idx
);
229 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
);
231 // and define another one to be overridden in the derived classes: it
232 // should return the best width for the given column contents or -1 if not
233 // implemented, we use it to implement UpdateColumnWidthToFit()
234 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx
)) const
240 // functions implementing our public API
241 void DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
);
242 void DoDelete(unsigned int idx
);
243 void DoShowColumn(unsigned int idx
, bool show
);
244 void DoShowSortIndicator(unsigned int idx
, bool ascending
);
246 // common part of all ctors
249 // bring the column count in sync with the number of columns we store
250 void UpdateColumnCount() { SetColumnCount(m_cols
.size()); }
253 // all our current columns
254 typedef wxVector
<wxHeaderColumnSimple
> Columns
;
257 // the column currently used for sorting or -1 if none
258 unsigned int m_sortKey
;
261 DECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple
)
264 // ----------------------------------------------------------------------------
265 // wxHeaderCtrl events
266 // ----------------------------------------------------------------------------
268 class WXDLLIMPEXP_CORE wxHeaderCtrlEvent
: public wxNotifyEvent
271 wxHeaderCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
272 : wxNotifyEvent(commandType
, winid
)
276 wxHeaderCtrlEvent(const wxHeaderCtrlEvent
& event
)
277 : wxNotifyEvent(event
),
282 int GetColumn() const { return m_col
; }
283 void SetColumn(int col
) { m_col
= col
; }
285 virtual wxEvent
*Clone() const { return new wxHeaderCtrlEvent(*this); }
288 // the column affected by the event
292 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent
)
296 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_CLICK
;
297 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
;
298 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
;
300 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_DCLICK
;
301 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
302 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
304 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
306 typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction
)(wxHeaderCtrlEvent
&);
308 #define wxHeaderCtrlEventHandler(func) \
309 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent( \
310 wxHeaderCtrlEventFunction, &func)
312 #define wx__DECLARE_HEADER_EVT(evt, id, fn) \
313 wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
315 #define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
316 #define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
317 #define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
319 #define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
320 #define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
321 #define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
323 #define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
325 #endif // _WX_HEADERCTRL_H_