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
,
125 // show the popup menu containing all columns with check marks for the ones
126 // which are currently shown and return true if something was done using it
127 // (in this case UpdateColumnVisibility() will have been called) or false
128 // if the menu was cancelled
130 // this is called from the default right click handler for the controls
131 // with wxHD_ALLOW_HIDE style
132 bool ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
= wxString());
134 // append the entries for all our columns to the given menu, with the
135 // currently visible columns being checked
137 // this is used by ShowColumnsMenu() but can also be used if you use your
138 // own custom columns menu but nevertheless want to show all the columns in
141 // the ids of the items corresponding to the columns are consecutive and
142 // start from idColumnsBase
143 void AddColumnsItems(wxMenu
& menu
, int idColumnsBase
= 0);
146 // show the columns customization dialog and return true if something was
147 // changed using it (in which case UpdateColumnVisibility() and/or
148 // UpdateColumnsOrder() will have been called)
150 // this is called by the control itself from ShowColumnsMenu() (which in
151 // turn is only called by the control if wxHD_ALLOW_HIDE style was
152 // specified) and if the control has wxHD_ALLOW_REORDER style as well
153 bool ShowCustomizeDialog();
156 // implementation only from now on
157 // -------------------------------
159 // the user doesn't need to TAB to this control
160 virtual bool AcceptsFocusFromKeyboard() const { return false; }
162 // this method is only overridden in order to synchronize the control with
163 // the main window when it is scrolled, the derived class must implement
165 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
168 // this method must be implemented by the derived classes to return the
169 // information for the given column
170 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const = 0;
172 // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK
173 // handler to update the fitting column width of the given column, it
174 // should return true if the width was really updated
175 virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx
),
176 int WXUNUSED(widthTitle
))
181 // this method is called from ShowColumnsMenu() and must be overridden to
182 // update the internal column visibility (there is no need to call
183 // UpdateColumn() from here, this will be done internally)
184 virtual void UpdateColumnVisibility(unsigned int WXUNUSED(idx
),
187 wxFAIL_MSG( "must be overridden if called" );
190 // this method is called from ShowCustomizeDialog() to reorder all columns
191 // at once and should be implemented for controls using wxHD_ALLOW_REORDER
192 // style (there is no need to call SetColumnsOrder() from here, this is
193 // done by the control itself)
194 virtual void UpdateColumnsOrder(const wxArrayInt
& WXUNUSED(order
))
196 wxFAIL_MSG( "must be overridden if called" );
199 // this method can be overridden in the derived classes to do something
200 // (e.g. update/resize some internal data structures) before the number of
201 // columns in the control changes
202 virtual void OnColumnCountChanging(unsigned int WXUNUSED(count
)) { }
205 // helper function for the derived classes: update the array of column
206 // indices after the number of columns changed
207 void DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
);
210 // methods implementing our public API and defined in platform-specific
212 virtual void DoSetCount(unsigned int count
) = 0;
213 virtual unsigned int DoGetCount() const = 0;
214 virtual void DoUpdate(unsigned int idx
) = 0;
216 virtual void DoScrollHorz(int dx
) = 0;
218 virtual void DoSetColumnsOrder(const wxArrayInt
& order
) = 0;
219 virtual wxArrayInt
DoGetColumnsOrder() const = 0;
221 // this window doesn't look nice with the border so don't use it by default
222 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
225 void OnSeparatorDClick(wxHeaderCtrlEvent
& event
);
227 void OnRClick(wxHeaderCtrlEvent
& event
);
230 DECLARE_EVENT_TABLE()
233 // ----------------------------------------------------------------------------
234 // wxHeaderCtrl: port-specific header control implementation, notice that this
235 // is still an ABC which is meant to be used as part of another
236 // control, see wxHeaderCtrlSimple for a standalone version
237 // ----------------------------------------------------------------------------
239 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
240 #include "wx/msw/headerctrl.h"
242 #define wxHAS_GENERIC_HEADERCTRL
243 #include "wx/generic/headerctrlg.h"
246 // ----------------------------------------------------------------------------
247 // wxHeaderCtrlSimple: concrete header control which can be used standalone
248 // ----------------------------------------------------------------------------
250 class WXDLLIMPEXP_CORE wxHeaderCtrlSimple
: public wxHeaderCtrl
256 wxHeaderCtrlSimple() { Init(); }
257 wxHeaderCtrlSimple(wxWindow
*parent
,
258 wxWindowID winid
= wxID_ANY
,
259 const wxPoint
& pos
= wxDefaultPosition
,
260 const wxSize
& size
= wxDefaultSize
,
261 long style
= wxHD_DEFAULT_STYLE
,
262 const wxString
& name
= wxHeaderCtrlNameStr
)
266 Create(parent
, winid
, pos
, size
, style
, name
);
269 // managing the columns
270 // --------------------
272 // insert the column at the given position, using GetColumnCount() as
273 // position appends it at the end
274 void InsertColumn(const wxHeaderColumnSimple
& col
, unsigned int idx
)
276 wxCHECK_RET( idx
<= GetColumnCount(), "invalid column index" );
281 // append the column to the end of the control
282 void AppendColumn(const wxHeaderColumnSimple
& col
)
284 DoInsert(col
, GetColumnCount());
287 // delete the column at the given index
288 void DeleteColumn(unsigned int idx
)
290 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
295 // delete all the existing columns
296 void DeleteAllColumns();
302 // show or hide the column, notice that even when a column is hidden we
303 // still account for it when using indices
304 void ShowColumn(unsigned int idx
, bool show
= true)
306 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
308 DoShowColumn(idx
, show
);
311 void HideColumn(unsigned int idx
)
313 ShowColumn(idx
, false);
316 // indicate that the column is used for sorting
317 void ShowSortIndicator(unsigned int idx
, bool ascending
= true)
319 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
321 DoShowSortIndicator(idx
, ascending
);
324 // remove the sort indicator completely
325 void RemoveSortIndicator();
328 // implement/override base class methods
329 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const;
330 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
);
332 // and define another one to be overridden in the derived classes: it
333 // should return the best width for the given column contents or -1 if not
334 // implemented, we use it to implement UpdateColumnWidthToFit()
335 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx
)) const
341 // functions implementing our public API
342 void DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
);
343 void DoDelete(unsigned int idx
);
344 void DoShowColumn(unsigned int idx
, bool show
);
345 void DoShowSortIndicator(unsigned int idx
, bool ascending
);
347 // common part of all ctors
350 // bring the column count in sync with the number of columns we store
351 void UpdateColumnCount() { SetColumnCount(m_cols
.size()); }
354 // all our current columns
355 typedef wxVector
<wxHeaderColumnSimple
> Columns
;
358 // the column currently used for sorting or -1 if none
359 unsigned int m_sortKey
;
362 DECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple
)
365 // ----------------------------------------------------------------------------
366 // wxHeaderCtrl events
367 // ----------------------------------------------------------------------------
369 class WXDLLIMPEXP_CORE wxHeaderCtrlEvent
: public wxNotifyEvent
372 wxHeaderCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
373 : wxNotifyEvent(commandType
, winid
),
376 m_order(static_cast<unsigned int>(-1))
380 wxHeaderCtrlEvent(const wxHeaderCtrlEvent
& event
)
381 : wxNotifyEvent(event
),
383 m_width(event
.m_width
),
384 m_order(event
.m_order
)
388 // the column which this event pertains to: valid for all header events
389 int GetColumn() const { return m_col
; }
390 void SetColumn(int col
) { m_col
= col
; }
392 // the width of the column: valid for column resizing/dragging events only
393 int GetWidth() const { return m_width
; }
394 void SetWidth(int width
) { m_width
= width
; }
396 // the new position of the column: for end reorder events only
397 unsigned int GetNewOrder() const { return m_order
; }
398 void SetNewOrder(unsigned int order
) { m_order
= order
; }
400 virtual wxEvent
*Clone() const { return new wxHeaderCtrlEvent(*this); }
403 // the column affected by the event
406 // the current width for the dragging events
409 // the new column position for end reorder event
410 unsigned int m_order
;
413 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent
)
417 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_CLICK
;
418 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
;
419 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
;
421 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_DCLICK
;
422 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
;
423 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
;
425 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
;
427 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
;
428 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_RESIZING
;
429 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
;
431 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
;
432 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
;
434 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
;
436 typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction
)(wxHeaderCtrlEvent
&);
438 #define wxHeaderCtrlEventHandler(func) \
439 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent( \
440 wxHeaderCtrlEventFunction, &func)
442 #define wx__DECLARE_HEADER_EVT(evt, id, fn) \
443 wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
445 #define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
446 #define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
447 #define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
449 #define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
450 #define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
451 #define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
453 #define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
455 #define EVT_HEADER_BEGIN_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_RESIZE, id, fn)
456 #define EVT_HEADER_RESIZING(id, fn) wx__DECLARE_HEADER_EVT(RESIZING, id, fn)
457 #define EVT_HEADER_END_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(END_RESIZE, id, fn)
459 #define EVT_HEADER_BEGIN_REORDER(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_REORDER, id, fn)
460 #define EVT_HEADER_END_REORDER(id, fn) wx__DECLARE_HEADER_EVT(END_REORDER, id, fn)
462 #define EVT_HEADER_DRAGGING_CANCELLED(id, fn) wx__DECLARE_HEADER_EVT(DRAGGING_CANCELLED, id, fn)
464 #endif // _WX_HEADERCTRL_H_