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"
18 #include "wx/dynarray.h"
19 #include "wx/vector.h"
21 #include "wx/headercol.h"
23 // notice that the classes in this header are defined in the core library even
24 // although currently they're only used by wxGrid which is in wxAdv because we
25 // plan to use it in wxListCtrl which is in core too in the future
26 class WXDLLIMPEXP_FWD_CORE wxHeaderCtrlEvent
;
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
34 // allow column drag and drop
35 wxHD_ALLOW_REORDER
= 0x0001,
37 // allow hiding (and showing back) the columns using the menu shown by
38 // right clicking the header
39 wxHD_ALLOW_HIDE
= 0x0002,
41 // style used by default when creating the control
42 wxHD_DEFAULT_STYLE
= wxHD_ALLOW_REORDER
45 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[];
47 // ----------------------------------------------------------------------------
48 // wxHeaderCtrlBase defines the interface of a header control
49 // ----------------------------------------------------------------------------
51 class WXDLLIMPEXP_CORE wxHeaderCtrlBase
: public wxControl
55 Derived classes must provide default ctor as well as a ctor and
56 Create() function with the following signatures:
58 wxHeaderCtrl(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);
65 bool Create(wxWindow *parent,
66 wxWindowID winid = wxID_ANY,
67 const wxPoint& pos = wxDefaultPosition,
68 const wxSize& size = wxDefaultSize,
69 long style = wxHD_DEFAULT_STYLE,
70 const wxString& name = wxHeaderCtrlNameStr);
73 // column-related methods
74 // ----------------------
76 // set the number of columns in the control
78 // this also calls UpdateColumn() for all columns
79 void SetColumnCount(unsigned int count
);
81 // return the number of columns in the control as set by SetColumnCount()
82 unsigned int GetColumnCount() const { return DoGetCount(); }
84 // return whether the control has any columns
85 bool IsEmpty() const { return DoGetCount() == 0; }
87 // update the column with the given index
88 void UpdateColumn(unsigned int idx
)
90 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
99 // set the columns order: the array defines the column index which appears
100 // the given position, it must have GetColumnCount() elements and contain
101 // all indices exactly once
102 void SetColumnsOrder(const wxArrayInt
& order
);
103 wxArrayInt
GetColumnsOrder() const;
105 // get the index of the column at the given display position
106 unsigned int GetColumnAt(unsigned int pos
) const;
108 // get the position at which this column is currently displayed
109 unsigned int GetColumnPos(unsigned int idx
) const;
111 // reset the columns order to the natural one
112 void ResetColumnsOrder();
114 // helper function used by the generic version of this control and also
115 // wxGrid: reshuffles the array of column indices indexed by positions
116 // (i.e. using the same convention as for SetColumnsOrder()) so that the
117 // column with the given index is found at the specified position
118 static void MoveColumnInOrderArray(wxArrayInt
& order
,
127 // show the popup menu containing all columns with check marks for the ones
128 // which are currently shown and return true if something was done using it
129 // (in this case UpdateColumnVisibility() will have been called) or false
130 // if the menu was cancelled
132 // this is called from the default right click handler for the controls
133 // with wxHD_ALLOW_HIDE style
134 bool ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
= wxString());
136 // append the entries for all our columns to the given menu, with the
137 // currently visible columns being checked
139 // this is used by ShowColumnsMenu() but can also be used if you use your
140 // own custom columns menu but nevertheless want to show all the columns in
143 // the ids of the items corresponding to the columns are consecutive and
144 // start from idColumnsBase
145 void AddColumnsItems(wxMenu
& menu
, int idColumnsBase
= 0);
146 #endif // wxUSE_MENUS
148 // show the columns customization dialog and return true if something was
149 // changed using it (in which case UpdateColumnVisibility() and/or
150 // UpdateColumnsOrder() will have been called)
152 // this is called by the control itself from ShowColumnsMenu() (which in
153 // turn is only called by the control if wxHD_ALLOW_HIDE style was
154 // specified) and if the control has wxHD_ALLOW_REORDER style as well
155 bool ShowCustomizeDialog();
157 // compute column title width
158 int GetColumnTitleWidth(const wxHeaderColumn
& col
);
160 // implementation only from now on
161 // -------------------------------
163 // the user doesn't need to TAB to this control
164 virtual bool AcceptsFocusFromKeyboard() const { return false; }
166 // this method is only overridden in order to synchronize the control with
167 // the main window when it is scrolled, the derived class must implement
169 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
172 // this method must be implemented by the derived classes to return the
173 // information for the given column
174 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const = 0;
176 // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK
177 // handler to update the fitting column width of the given column, it
178 // should return true if the width was really updated
179 virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx
),
180 int WXUNUSED(widthTitle
))
185 // this method is called from ShowColumnsMenu() and must be overridden to
186 // update the internal column visibility (there is no need to call
187 // UpdateColumn() from here, this will be done internally)
188 virtual void UpdateColumnVisibility(unsigned int WXUNUSED(idx
),
191 wxFAIL_MSG( "must be overridden if called" );
194 // this method is called from ShowCustomizeDialog() to reorder all columns
195 // at once and should be implemented for controls using wxHD_ALLOW_REORDER
196 // style (there is no need to call SetColumnsOrder() from here, this is
197 // done by the control itself)
198 virtual void UpdateColumnsOrder(const wxArrayInt
& WXUNUSED(order
))
200 wxFAIL_MSG( "must be overridden if called" );
203 // this method can be overridden in the derived classes to do something
204 // (e.g. update/resize some internal data structures) before the number of
205 // columns in the control changes
206 virtual void OnColumnCountChanging(unsigned int WXUNUSED(count
)) { }
209 // helper function for the derived classes: update the array of column
210 // indices after the number of columns changed
211 void DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
);
214 // methods implementing our public API and defined in platform-specific
216 virtual void DoSetCount(unsigned int count
) = 0;
217 virtual unsigned int DoGetCount() const = 0;
218 virtual void DoUpdate(unsigned int idx
) = 0;
220 virtual void DoScrollHorz(int dx
) = 0;
222 virtual void DoSetColumnsOrder(const wxArrayInt
& order
) = 0;
223 virtual wxArrayInt
DoGetColumnsOrder() const = 0;
225 // this window doesn't look nice with the border so don't use it by default
226 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
229 void OnSeparatorDClick(wxHeaderCtrlEvent
& event
);
231 void OnRClick(wxHeaderCtrlEvent
& event
);
232 #endif // wxUSE_MENUS
234 DECLARE_EVENT_TABLE()
237 // ----------------------------------------------------------------------------
238 // wxHeaderCtrl: port-specific header control implementation, notice that this
239 // is still an ABC which is meant to be used as part of another
240 // control, see wxHeaderCtrlSimple for a standalone version
241 // ----------------------------------------------------------------------------
243 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
244 #include "wx/msw/headerctrl.h"
246 #define wxHAS_GENERIC_HEADERCTRL
247 #include "wx/generic/headerctrlg.h"
250 // ----------------------------------------------------------------------------
251 // wxHeaderCtrlSimple: concrete header control which can be used standalone
252 // ----------------------------------------------------------------------------
254 class WXDLLIMPEXP_CORE wxHeaderCtrlSimple
: public wxHeaderCtrl
260 wxHeaderCtrlSimple() { Init(); }
261 wxHeaderCtrlSimple(wxWindow
*parent
,
262 wxWindowID winid
= wxID_ANY
,
263 const wxPoint
& pos
= wxDefaultPosition
,
264 const wxSize
& size
= wxDefaultSize
,
265 long style
= wxHD_DEFAULT_STYLE
,
266 const wxString
& name
= wxHeaderCtrlNameStr
)
270 Create(parent
, winid
, pos
, size
, style
, name
);
273 // managing the columns
274 // --------------------
276 // insert the column at the given position, using GetColumnCount() as
277 // position appends it at the end
278 void InsertColumn(const wxHeaderColumnSimple
& col
, unsigned int idx
)
280 wxCHECK_RET( idx
<= GetColumnCount(), "invalid column index" );
285 // append the column to the end of the control
286 void AppendColumn(const wxHeaderColumnSimple
& col
)
288 DoInsert(col
, GetColumnCount());
291 // delete the column at the given index
292 void DeleteColumn(unsigned int idx
)
294 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
299 // delete all the existing columns
300 void DeleteAllColumns();
306 // show or hide the column, notice that even when a column is hidden we
307 // still account for it when using indices
308 void ShowColumn(unsigned int idx
, bool show
= true)
310 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
312 DoShowColumn(idx
, show
);
315 void HideColumn(unsigned int idx
)
317 ShowColumn(idx
, false);
320 // indicate that the column is used for sorting
321 void ShowSortIndicator(unsigned int idx
, bool ascending
= true)
323 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
325 DoShowSortIndicator(idx
, ascending
);
328 // remove the sort indicator completely
329 void RemoveSortIndicator();
332 // implement/override base class methods
333 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const;
334 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
);
336 // and define another one to be overridden in the derived classes: it
337 // should return the best width for the given column contents or -1 if not
338 // implemented, we use it to implement UpdateColumnWidthToFit()
339 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx
)) const
345 // functions implementing our public API
346 void DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
);
347 void DoDelete(unsigned int idx
);
348 void DoShowColumn(unsigned int idx
, bool show
);
349 void DoShowSortIndicator(unsigned int idx
, bool ascending
);
351 // common part of all ctors
354 // bring the column count in sync with the number of columns we store
355 void UpdateColumnCount() { SetColumnCount(m_cols
.size()); }
358 // all our current columns
359 typedef wxVector
<wxHeaderColumnSimple
> Columns
;
362 // the column currently used for sorting or -1 if none
363 unsigned int m_sortKey
;
366 wxDECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple
);
369 // ----------------------------------------------------------------------------
370 // wxHeaderCtrl events
371 // ----------------------------------------------------------------------------
373 class WXDLLIMPEXP_CORE wxHeaderCtrlEvent
: public wxNotifyEvent
376 wxHeaderCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
377 : wxNotifyEvent(commandType
, winid
),
380 m_order(static_cast<unsigned int>(-1))
384 wxHeaderCtrlEvent(const wxHeaderCtrlEvent
& event
)
385 : wxNotifyEvent(event
),
387 m_width(event
.m_width
),
388 m_order(event
.m_order
)
392 // the column which this event pertains to: valid for all header events
393 int GetColumn() const { return m_col
; }
394 void SetColumn(int col
) { m_col
= col
; }
396 // the width of the column: valid for column resizing/dragging events only
397 int GetWidth() const { return m_width
; }
398 void SetWidth(int width
) { m_width
= width
; }
400 // the new position of the column: for end reorder events only
401 unsigned int GetNewOrder() const { return m_order
; }
402 void SetNewOrder(unsigned int order
) { m_order
= order
; }
404 virtual wxEvent
*Clone() const { return new wxHeaderCtrlEvent(*this); }
407 // the column affected by the event
410 // the current width for the dragging events
413 // the new column position for end reorder event
414 unsigned int m_order
;
417 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent
)
421 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_CLICK
, wxHeaderCtrlEvent
);
422 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_RIGHT_CLICK
, wxHeaderCtrlEvent
);
423 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_MIDDLE_CLICK
, wxHeaderCtrlEvent
);
425 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_DCLICK
, wxHeaderCtrlEvent
);
426 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_RIGHT_DCLICK
, wxHeaderCtrlEvent
);
427 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
, wxHeaderCtrlEvent
);
429 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
, wxHeaderCtrlEvent
);
431 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_BEGIN_RESIZE
, wxHeaderCtrlEvent
);
432 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_RESIZING
, wxHeaderCtrlEvent
);
433 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_END_RESIZE
, wxHeaderCtrlEvent
);
435 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_BEGIN_REORDER
, wxHeaderCtrlEvent
);
436 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_END_REORDER
, wxHeaderCtrlEvent
);
438 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
, wxHeaderCtrlEvent
);
440 typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction
)(wxHeaderCtrlEvent
&);
442 #define wxHeaderCtrlEventHandler(func) \
443 wxEVENT_HANDLER_CAST(wxHeaderCtrlEventFunction, func)
445 #define wx__DECLARE_HEADER_EVT(evt, id, fn) \
446 wx__DECLARE_EVT1(wxEVT_COMMAND_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
448 #define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
449 #define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
450 #define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
452 #define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
453 #define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
454 #define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
456 #define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
458 #define EVT_HEADER_BEGIN_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_RESIZE, id, fn)
459 #define EVT_HEADER_RESIZING(id, fn) wx__DECLARE_HEADER_EVT(RESIZING, id, fn)
460 #define EVT_HEADER_END_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(END_RESIZE, id, fn)
462 #define EVT_HEADER_BEGIN_REORDER(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_REORDER, id, fn)
463 #define EVT_HEADER_END_REORDER(id, fn) wx__DECLARE_HEADER_EVT(END_REORDER, id, fn)
465 #define EVT_HEADER_DRAGGING_CANCELLED(id, fn) wx__DECLARE_HEADER_EVT(DRAGGING_CANCELLED, id, fn)
467 #endif // wxUSE_HEADERCTRL
469 #endif // _WX_HEADERCTRL_H_