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_ALLOW_REORDER
= 0x0001,
35 // allow hiding (and showing back) the columns using the menu shown by
36 // right clicking the header
37 wxHD_ALLOW_HIDE
= 0x0002,
39 // style used by default when creating the control
40 wxHD_DEFAULT_STYLE
= wxHD_ALLOW_REORDER
43 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[];
45 // ----------------------------------------------------------------------------
46 // wxHeaderCtrlBase defines the interface of a header control
47 // ----------------------------------------------------------------------------
49 class WXDLLIMPEXP_CORE wxHeaderCtrlBase
: public wxControl
53 Derived classes must provide default ctor as well as a ctor and
54 Create() function with the following signatures:
56 wxHeaderCtrl(wxWindow *parent,
57 wxWindowID winid = wxID_ANY,
58 const wxPoint& pos = wxDefaultPosition,
59 const wxSize& size = wxDefaultSize,
60 long style = wxHD_DEFAULT_STYLE,
61 const wxString& name = wxHeaderCtrlNameStr);
63 bool Create(wxWindow *parent,
64 wxWindowID winid = wxID_ANY,
65 const wxPoint& pos = wxDefaultPosition,
66 const wxSize& size = wxDefaultSize,
67 long style = wxHD_DEFAULT_STYLE,
68 const wxString& name = wxHeaderCtrlNameStr);
71 // column-related methods
72 // ----------------------
74 // set the number of columns in the control
76 // this also calls UpdateColumn() for all columns
77 void SetColumnCount(unsigned int count
);
79 // return the number of columns in the control as set by SetColumnCount()
80 unsigned int GetColumnCount() const { return DoGetCount(); }
82 // return whether the control has any columns
83 bool IsEmpty() const { return DoGetCount() == 0; }
85 // update the column with the given index
86 void UpdateColumn(unsigned int idx
)
88 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
97 // set the columns order: the array defines the column index which appears
98 // the given position, it must have GetColumnCount() elements and contain
99 // all indices exactly once
100 void SetColumnsOrder(const wxArrayInt
& order
);
101 wxArrayInt
GetColumnsOrder() const;
103 // get the index of the column at the given display position
104 unsigned int GetColumnAt(unsigned int pos
) const;
106 // get the position at which this column is currently displayed
107 unsigned int GetColumnPos(unsigned int idx
) const;
109 // reset the columns order to the natural one
110 void ResetColumnsOrder();
112 // helper function used by the generic version of this control and also
113 // wxGrid: reshuffles the array of column indices indexed by positions
114 // (i.e. using the same convention as for SetColumnsOrder()) so that the
115 // column with the given index is found at the specified position
116 static void MoveColumnInOrderArray(wxArrayInt
& order
,
124 // show the popup menu containing all columns with check marks for the ones
125 // which are currently shown and return true if something was done using it
126 // (in this case UpdateColumnVisibility() will have been called) or false
127 // if the menu was cancelled
129 // this is called from the default right click handler for the controls
130 // with wxHD_ALLOW_HIDE style
131 bool ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
= wxString());
133 // show the columns customization dialog and return true if something was
134 // changed using it (in which case UpdateColumnVisibility() and/or
135 // UpdateColumnsOrder() will have been called)
137 // this is called by the control itself from ShowColumnsMenu() (which in
138 // turn is only called by the control if wxHD_ALLOW_HIDE style was
139 // specified) and if the control has wxHD_ALLOW_REORDER style as well
140 bool ShowCustomizeDialog();
143 // implementation only from now on
144 // -------------------------------
146 // the user doesn't need to TAB to this control
147 virtual bool AcceptsFocusFromKeyboard() const { return false; }
149 // this method is only overridden in order to synchronize the control with
150 // the main window when it is scrolled, the derived class must implement
152 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
155 // this method must be implemented by the derived classes to return the
156 // information for the given column
157 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const = 0;
159 // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK
160 // handler to update the fitting column width of the given column, it
161 // should return true if the width was really updated
162 virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx
),
163 int WXUNUSED(widthTitle
))
168 // this method is called from ShowColumnsMenu() and must be overridden to
169 // update the internal column visibility (there is no need to call
170 // UpdateColumn() from here, this will be done internally)
171 virtual void UpdateColumnVisibility(unsigned int WXUNUSED(idx
),
174 wxFAIL_MSG( "must be overridden if called" );
177 // this method is called from ShowCustomizeDialog() to reorder all columns
178 // at once and should be implemented for controls using wxHD_ALLOW_REORDER
179 // style (there is no need to call SetColumnsOrder() from here, this is
180 // done by the control itself)
181 virtual void UpdateColumnsOrder(const wxArrayInt
& WXUNUSED(order
))
183 wxFAIL_MSG( "must be overridden if called" );
186 // this method can be overridden in the derived classes to do something
187 // (e.g. update/resize some internal data structures) before the number of
188 // columns in the control changes
189 virtual void OnColumnCountChanging(unsigned int WXUNUSED(count
)) { }
192 // helper function for the derived classes: update the array of column
193 // indices after the number of columns changed
194 void DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
);
197 // methods implementing our public API and defined in platform-specific
199 virtual void DoSetCount(unsigned int count
) = 0;
200 virtual unsigned int DoGetCount() const = 0;
201 virtual void DoUpdate(unsigned int idx
) = 0;
203 virtual void DoScrollHorz(int dx
) = 0;
205 virtual void DoSetColumnsOrder(const wxArrayInt
& order
) = 0;
206 virtual wxArrayInt
DoGetColumnsOrder() const = 0;
208 // this window doesn't look nice with the border so don't use it by default
209 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
212 void OnSeparatorDClick(wxHeaderCtrlEvent
& event
);
213 void OnRClick(wxHeaderCtrlEvent
& event
);
215 DECLARE_EVENT_TABLE()
218 // ----------------------------------------------------------------------------
219 // wxHeaderCtrl: port-specific header control implementation, notice that this
220 // is still an ABC which is meant to be used as part of another
221 // control, see wxHeaderCtrlSimple for a standalone version
222 // ----------------------------------------------------------------------------
224 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
225 #include "wx/msw/headerctrl.h"
227 #define wxHAS_GENERIC_HEADERCTRL
228 #include "wx/generic/headerctrlg.h"
231 // ----------------------------------------------------------------------------
232 // wxHeaderCtrlSimple: concrete header control which can be used standalone
233 // ----------------------------------------------------------------------------
235 class WXDLLIMPEXP_CORE wxHeaderCtrlSimple
: public wxHeaderCtrl
241 wxHeaderCtrlSimple() { Init(); }
242 wxHeaderCtrlSimple(wxWindow
*parent
,
243 wxWindowID winid
= wxID_ANY
,
244 const wxPoint
& pos
= wxDefaultPosition
,
245 const wxSize
& size
= wxDefaultSize
,
246 long style
= wxHD_DEFAULT_STYLE
,
247 const wxString
& name
= wxHeaderCtrlNameStr
)
251 Create(parent
, winid
, pos
, size
, style
, name
);
254 // managing the columns
255 // --------------------
257 // insert the column at the given position, using GetColumnCount() as
258 // position appends it at the end
259 void InsertColumn(const wxHeaderColumnSimple
& col
, unsigned int idx
)
261 wxCHECK_RET( idx
<= GetColumnCount(), "invalid column index" );
266 // append the column to the end of the control
267 void AppendColumn(const wxHeaderColumnSimple
& col
)
269 DoInsert(col
, GetColumnCount());
272 // delete the column at the given index
273 void DeleteColumn(unsigned int idx
)
275 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
280 // delete all the existing columns
281 void DeleteAllColumns();
287 // show or hide the column, notice that even when a column is hidden we
288 // still account for it when using indices
289 void ShowColumn(unsigned int idx
, bool show
= true)
291 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
293 DoShowColumn(idx
, show
);
296 void HideColumn(unsigned int idx
)
298 ShowColumn(idx
, false);
301 // indicate that the column is used for sorting
302 void ShowSortIndicator(unsigned int idx
, bool ascending
= true)
304 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
306 DoShowSortIndicator(idx
, ascending
);
309 // remove the sort indicator completely
310 void RemoveSortIndicator();
313 // implement/override base class methods
314 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const;
315 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
);
317 // and define another one to be overridden in the derived classes: it
318 // should return the best width for the given column contents or -1 if not
319 // implemented, we use it to implement UpdateColumnWidthToFit()
320 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx
)) const
326 // functions implementing our public API
327 void DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
);
328 void DoDelete(unsigned int idx
);
329 void DoShowColumn(unsigned int idx
, bool show
);
330 void DoShowSortIndicator(unsigned int idx
, bool ascending
);
332 // common part of all ctors
335 // bring the column count in sync with the number of columns we store
336 void UpdateColumnCount() { SetColumnCount(m_cols
.size()); }
339 // all our current columns
340 typedef wxVector
<wxHeaderColumnSimple
> Columns
;
343 // the column currently used for sorting or -1 if none
344 unsigned int m_sortKey
;
347 DECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple
)
350 // ----------------------------------------------------------------------------
351 // wxHeaderCtrl events
352 // ----------------------------------------------------------------------------
354 class WXDLLIMPEXP_CORE wxHeaderCtrlEvent
: public wxNotifyEvent
357 wxHeaderCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
358 : wxNotifyEvent(commandType
, winid
),
361 m_order(static_cast<unsigned int>(-1))
365 wxHeaderCtrlEvent(const wxHeaderCtrlEvent
& event
)
366 : wxNotifyEvent(event
),
368 m_width(event
.m_width
),
369 m_order(event
.m_order
)
373 // the column which this event pertains to: valid for all header events
374 int GetColumn() const { return m_col
; }
375 void SetColumn(int col
) { m_col
= col
; }
377 // the width of the column: valid for column resizing/dragging events only
378 int GetWidth() const { return m_width
; }
379 void SetWidth(int width
) { m_width
= width
; }
381 // the new position of the column: for end reorder events only
382 unsigned int GetNewOrder() const { return m_order
; }
383 void SetNewOrder(unsigned int order
) { m_order
= order
; }
385 virtual wxEvent
*Clone() const { return new wxHeaderCtrlEvent(*this); }
388 // the column affected by the event
391 // the current width for the dragging events
394 // the new column position for end reorder event
395 unsigned int m_order
;
398 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent
)
402 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_CLICK
;
403 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
;
404 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
;
406 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_DCLICK
;
407 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
408 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
410 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
412 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
;
413 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RESIZING
;
414 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
;
416 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
;
417 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
;
419 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
;
421 typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction
)(wxHeaderCtrlEvent
&);
423 #define wxHeaderCtrlEventHandler(func) \
424 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent( \
425 wxHeaderCtrlEventFunction, &func)
427 #define wx__DECLARE_HEADER_EVT(evt, id, fn) \
428 wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
430 #define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
431 #define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
432 #define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
434 #define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
435 #define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
436 #define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
438 #define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
440 #define EVT_HEADER_BEGIN_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_RESIZE, id, fn)
441 #define EVT_HEADER_RESIZING(id, fn) wx__DECLARE_HEADER_EVT(RESIZING, id, fn)
442 #define EVT_HEADER_END_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(END_RESIZE, id, fn)
444 #define EVT_HEADER_BEGIN_REORDER(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_REORDER, id, fn)
445 #define EVT_HEADER_END_REORDER(id, fn) wx__DECLARE_HEADER_EVT(END_REORDER, id, fn)
447 #define EVT_HEADER_DRAGGING_CANCELLED(id, fn) wx__DECLARE_HEADER_EVT(DRAGGING_CANCELLED, id, fn)
449 #endif // _WX_HEADERCTRL_H_