1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/headerctrl.h
3 // Purpose: wxHeaderCtrlBase class: interface of wxHeaderCtrl
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_HEADERCTRL_H_
11 #define _WX_HEADERCTRL_H_
13 #include "wx/control.h"
17 #include "wx/dynarray.h"
18 #include "wx/vector.h"
20 #include "wx/headercol.h"
22 // notice that the classes in this header are defined in the core library even
23 // although currently they're only used by wxGrid which is in wxAdv because we
24 // plan to use it in wxListCtrl which is in core too in the future
25 class WXDLLIMPEXP_FWD_CORE wxHeaderCtrlEvent
;
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
33 // allow column drag and drop
34 wxHD_ALLOW_REORDER
= 0x0001,
36 // allow hiding (and showing back) the columns using the menu shown by
37 // right clicking the header
38 wxHD_ALLOW_HIDE
= 0x0002,
40 // style used by default when creating the control
41 wxHD_DEFAULT_STYLE
= wxHD_ALLOW_REORDER
44 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[];
46 // ----------------------------------------------------------------------------
47 // wxHeaderCtrlBase defines the interface of a header control
48 // ----------------------------------------------------------------------------
50 class WXDLLIMPEXP_CORE wxHeaderCtrlBase
: public wxControl
54 Derived classes must provide default ctor as well as a ctor and
55 Create() function with the following signatures:
57 wxHeaderCtrl(wxWindow *parent,
58 wxWindowID winid = wxID_ANY,
59 const wxPoint& pos = wxDefaultPosition,
60 const wxSize& size = wxDefaultSize,
61 long style = wxHD_DEFAULT_STYLE,
62 const wxString& name = wxHeaderCtrlNameStr);
64 bool Create(wxWindow *parent,
65 wxWindowID winid = wxID_ANY,
66 const wxPoint& pos = wxDefaultPosition,
67 const wxSize& size = wxDefaultSize,
68 long style = wxHD_DEFAULT_STYLE,
69 const wxString& name = wxHeaderCtrlNameStr);
72 // column-related methods
73 // ----------------------
75 // set the number of columns in the control
77 // this also calls UpdateColumn() for all columns
78 void SetColumnCount(unsigned int count
);
80 // return the number of columns in the control as set by SetColumnCount()
81 unsigned int GetColumnCount() const { return DoGetCount(); }
83 // return whether the control has any columns
84 bool IsEmpty() const { return DoGetCount() == 0; }
86 // update the column with the given index
87 void UpdateColumn(unsigned int idx
)
89 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
98 // set the columns order: the array defines the column index which appears
99 // the given position, it must have GetColumnCount() elements and contain
100 // all indices exactly once
101 void SetColumnsOrder(const wxArrayInt
& order
);
102 wxArrayInt
GetColumnsOrder() const;
104 // get the index of the column at the given display position
105 unsigned int GetColumnAt(unsigned int pos
) const;
107 // get the position at which this column is currently displayed
108 unsigned int GetColumnPos(unsigned int idx
) const;
110 // reset the columns order to the natural one
111 void ResetColumnsOrder();
113 // helper function used by the generic version of this control and also
114 // wxGrid: reshuffles the array of column indices indexed by positions
115 // (i.e. using the same convention as for SetColumnsOrder()) so that the
116 // column with the given index is found at the specified position
117 static void MoveColumnInOrderArray(wxArrayInt
& order
,
126 // show the popup menu containing all columns with check marks for the ones
127 // which are currently shown and return true if something was done using it
128 // (in this case UpdateColumnVisibility() will have been called) or false
129 // if the menu was cancelled
131 // this is called from the default right click handler for the controls
132 // with wxHD_ALLOW_HIDE style
133 bool ShowColumnsMenu(const wxPoint
& pt
, const wxString
& title
= wxString());
135 // append the entries for all our columns to the given menu, with the
136 // currently visible columns being checked
138 // this is used by ShowColumnsMenu() but can also be used if you use your
139 // own custom columns menu but nevertheless want to show all the columns in
142 // the ids of the items corresponding to the columns are consecutive and
143 // start from idColumnsBase
144 void AddColumnsItems(wxMenu
& menu
, int idColumnsBase
= 0);
145 #endif // wxUSE_MENUS
147 // show the columns customization dialog and return true if something was
148 // changed using it (in which case UpdateColumnVisibility() and/or
149 // UpdateColumnsOrder() will have been called)
151 // this is called by the control itself from ShowColumnsMenu() (which in
152 // turn is only called by the control if wxHD_ALLOW_HIDE style was
153 // specified) and if the control has wxHD_ALLOW_REORDER style as well
154 bool ShowCustomizeDialog();
156 // compute column title width
157 int GetColumnTitleWidth(const wxHeaderColumn
& col
);
159 // implementation only from now on
160 // -------------------------------
162 // the user doesn't need to TAB to this control
163 virtual bool AcceptsFocusFromKeyboard() const { return false; }
165 // this method is only overridden in order to synchronize the control with
166 // the main window when it is scrolled, the derived class must implement
168 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
171 // this method must be implemented by the derived classes to return the
172 // information for the given column
173 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const = 0;
175 // this method is called from the default EVT_HEADER_SEPARATOR_DCLICK
176 // handler to update the fitting column width of the given column, it
177 // should return true if the width was really updated
178 virtual bool UpdateColumnWidthToFit(unsigned int WXUNUSED(idx
),
179 int WXUNUSED(widthTitle
))
184 // this method is called from ShowColumnsMenu() and must be overridden to
185 // update the internal column visibility (there is no need to call
186 // UpdateColumn() from here, this will be done internally)
187 virtual void UpdateColumnVisibility(unsigned int WXUNUSED(idx
),
190 wxFAIL_MSG( "must be overridden if called" );
193 // this method is called from ShowCustomizeDialog() to reorder all columns
194 // at once and should be implemented for controls using wxHD_ALLOW_REORDER
195 // style (there is no need to call SetColumnsOrder() from here, this is
196 // done by the control itself)
197 virtual void UpdateColumnsOrder(const wxArrayInt
& WXUNUSED(order
))
199 wxFAIL_MSG( "must be overridden if called" );
202 // this method can be overridden in the derived classes to do something
203 // (e.g. update/resize some internal data structures) before the number of
204 // columns in the control changes
205 virtual void OnColumnCountChanging(unsigned int WXUNUSED(count
)) { }
208 // helper function for the derived classes: update the array of column
209 // indices after the number of columns changed
210 void DoResizeColumnIndices(wxArrayInt
& colIndices
, unsigned int count
);
213 // this window doesn't look nice with the border so don't use it by default
214 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
217 // methods implementing our public API and defined in platform-specific
219 virtual void DoSetCount(unsigned int count
) = 0;
220 virtual unsigned int DoGetCount() const = 0;
221 virtual void DoUpdate(unsigned int idx
) = 0;
223 virtual void DoScrollHorz(int dx
) = 0;
225 virtual void DoSetColumnsOrder(const wxArrayInt
& order
) = 0;
226 virtual wxArrayInt
DoGetColumnsOrder() const = 0;
230 void OnSeparatorDClick(wxHeaderCtrlEvent
& event
);
232 void OnRClick(wxHeaderCtrlEvent
& event
);
233 #endif // wxUSE_MENUS
235 DECLARE_EVENT_TABLE()
238 // ----------------------------------------------------------------------------
239 // wxHeaderCtrl: port-specific header control implementation, notice that this
240 // is still an ABC which is meant to be used as part of another
241 // control, see wxHeaderCtrlSimple for a standalone version
242 // ----------------------------------------------------------------------------
244 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
245 #include "wx/msw/headerctrl.h"
247 #define wxHAS_GENERIC_HEADERCTRL
248 #include "wx/generic/headerctrlg.h"
251 // ----------------------------------------------------------------------------
252 // wxHeaderCtrlSimple: concrete header control which can be used standalone
253 // ----------------------------------------------------------------------------
255 class WXDLLIMPEXP_CORE wxHeaderCtrlSimple
: public wxHeaderCtrl
261 wxHeaderCtrlSimple() { Init(); }
262 wxHeaderCtrlSimple(wxWindow
*parent
,
263 wxWindowID winid
= wxID_ANY
,
264 const wxPoint
& pos
= wxDefaultPosition
,
265 const wxSize
& size
= wxDefaultSize
,
266 long style
= wxHD_DEFAULT_STYLE
,
267 const wxString
& name
= wxHeaderCtrlNameStr
)
271 Create(parent
, winid
, pos
, size
, style
, name
);
274 // managing the columns
275 // --------------------
277 // insert the column at the given position, using GetColumnCount() as
278 // position appends it at the end
279 void InsertColumn(const wxHeaderColumnSimple
& col
, unsigned int idx
)
281 wxCHECK_RET( idx
<= GetColumnCount(), "invalid column index" );
286 // append the column to the end of the control
287 void AppendColumn(const wxHeaderColumnSimple
& col
)
289 DoInsert(col
, GetColumnCount());
292 // delete the column at the given index
293 void DeleteColumn(unsigned int idx
)
295 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
300 // delete all the existing columns
301 void DeleteAllColumns();
307 // show or hide the column, notice that even when a column is hidden we
308 // still account for it when using indices
309 void ShowColumn(unsigned int idx
, bool show
= true)
311 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
313 DoShowColumn(idx
, show
);
316 void HideColumn(unsigned int idx
)
318 ShowColumn(idx
, false);
321 // indicate that the column is used for sorting
322 void ShowSortIndicator(unsigned int idx
, bool ascending
= true)
324 wxCHECK_RET( idx
< GetColumnCount(), "invalid column index" );
326 DoShowSortIndicator(idx
, ascending
);
329 // remove the sort indicator completely
330 void RemoveSortIndicator();
333 // implement/override base class methods
334 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const;
335 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
);
337 // and define another one to be overridden in the derived classes: it
338 // should return the best width for the given column contents or -1 if not
339 // implemented, we use it to implement UpdateColumnWidthToFit()
340 virtual int GetBestFittingWidth(unsigned int WXUNUSED(idx
)) const
346 // functions implementing our public API
347 void DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
);
348 void DoDelete(unsigned int idx
);
349 void DoShowColumn(unsigned int idx
, bool show
);
350 void DoShowSortIndicator(unsigned int idx
, bool ascending
);
352 // common part of all ctors
355 // bring the column count in sync with the number of columns we store
356 void UpdateColumnCount()
358 SetColumnCount(static_cast<int>(m_cols
.size()));
362 // all our current columns
363 typedef wxVector
<wxHeaderColumnSimple
> Columns
;
366 // the column currently used for sorting or -1 if none
367 unsigned int m_sortKey
;
370 wxDECLARE_NO_COPY_CLASS(wxHeaderCtrlSimple
);
373 // ----------------------------------------------------------------------------
374 // wxHeaderCtrl events
375 // ----------------------------------------------------------------------------
377 class WXDLLIMPEXP_CORE wxHeaderCtrlEvent
: public wxNotifyEvent
380 wxHeaderCtrlEvent(wxEventType commandType
= wxEVT_NULL
, int winid
= 0)
381 : wxNotifyEvent(commandType
, winid
),
384 m_order(static_cast<unsigned int>(-1))
388 wxHeaderCtrlEvent(const wxHeaderCtrlEvent
& event
)
389 : wxNotifyEvent(event
),
391 m_width(event
.m_width
),
392 m_order(event
.m_order
)
396 // the column which this event pertains to: valid for all header events
397 int GetColumn() const { return m_col
; }
398 void SetColumn(int col
) { m_col
= col
; }
400 // the width of the column: valid for column resizing/dragging events only
401 int GetWidth() const { return m_width
; }
402 void SetWidth(int width
) { m_width
= width
; }
404 // the new position of the column: for end reorder events only
405 unsigned int GetNewOrder() const { return m_order
; }
406 void SetNewOrder(unsigned int order
) { m_order
= order
; }
408 virtual wxEvent
*Clone() const { return new wxHeaderCtrlEvent(*this); }
411 // the column affected by the event
414 // the current width for the dragging events
417 // the new column position for end reorder event
418 unsigned int m_order
;
421 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHeaderCtrlEvent
)
425 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_CLICK
, wxHeaderCtrlEvent
);
426 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_RIGHT_CLICK
, wxHeaderCtrlEvent
);
427 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_MIDDLE_CLICK
, wxHeaderCtrlEvent
);
429 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_DCLICK
, wxHeaderCtrlEvent
);
430 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_RIGHT_DCLICK
, wxHeaderCtrlEvent
);
431 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_MIDDLE_DCLICK
, wxHeaderCtrlEvent
);
433 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_SEPARATOR_DCLICK
, wxHeaderCtrlEvent
);
435 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_BEGIN_RESIZE
, wxHeaderCtrlEvent
);
436 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_RESIZING
, wxHeaderCtrlEvent
);
437 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_END_RESIZE
, wxHeaderCtrlEvent
);
439 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_BEGIN_REORDER
, wxHeaderCtrlEvent
);
440 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_END_REORDER
, wxHeaderCtrlEvent
);
442 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_HEADER_DRAGGING_CANCELLED
, wxHeaderCtrlEvent
);
444 typedef void (wxEvtHandler::*wxHeaderCtrlEventFunction
)(wxHeaderCtrlEvent
&);
446 #define wxHeaderCtrlEventHandler(func) \
447 wxEVENT_HANDLER_CAST(wxHeaderCtrlEventFunction, func)
449 #define wx__DECLARE_HEADER_EVT(evt, id, fn) \
450 wx__DECLARE_EVT1(wxEVT_HEADER_ ## evt, id, wxHeaderCtrlEventHandler(fn))
452 #define EVT_HEADER_CLICK(id, fn) wx__DECLARE_HEADER_EVT(CLICK, id, fn)
453 #define EVT_HEADER_RIGHT_CLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_CLICK, id, fn)
454 #define EVT_HEADER_MIDDLE_CLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_CLICK, id, fn)
456 #define EVT_HEADER_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(DCLICK, id, fn)
457 #define EVT_HEADER_RIGHT_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(RIGHT_DCLICK, id, fn)
458 #define EVT_HEADER_MIDDLE_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(MIDDLE_DCLICK, id, fn)
460 #define EVT_HEADER_SEPARATOR_DCLICK(id, fn) wx__DECLARE_HEADER_EVT(SEPARATOR_DCLICK, id, fn)
462 #define EVT_HEADER_BEGIN_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_RESIZE, id, fn)
463 #define EVT_HEADER_RESIZING(id, fn) wx__DECLARE_HEADER_EVT(RESIZING, id, fn)
464 #define EVT_HEADER_END_RESIZE(id, fn) wx__DECLARE_HEADER_EVT(END_RESIZE, id, fn)
466 #define EVT_HEADER_BEGIN_REORDER(id, fn) wx__DECLARE_HEADER_EVT(BEGIN_REORDER, id, fn)
467 #define EVT_HEADER_END_REORDER(id, fn) wx__DECLARE_HEADER_EVT(END_REORDER, id, fn)
469 #define EVT_HEADER_DRAGGING_CANCELLED(id, fn) wx__DECLARE_HEADER_EVT(DRAGGING_CANCELLED, id, fn)
471 // old wxEVT_COMMAND_* constants
472 #define wxEVT_COMMAND_HEADER_CLICK wxEVT_HEADER_CLICK
473 #define wxEVT_COMMAND_HEADER_RIGHT_CLICK wxEVT_HEADER_RIGHT_CLICK
474 #define wxEVT_COMMAND_HEADER_MIDDLE_CLICK wxEVT_HEADER_MIDDLE_CLICK
475 #define wxEVT_COMMAND_HEADER_DCLICK wxEVT_HEADER_DCLICK
476 #define wxEVT_COMMAND_HEADER_RIGHT_DCLICK wxEVT_HEADER_RIGHT_DCLICK
477 #define wxEVT_COMMAND_HEADER_MIDDLE_DCLICK wxEVT_HEADER_MIDDLE_DCLICK
478 #define wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK wxEVT_HEADER_SEPARATOR_DCLICK
479 #define wxEVT_COMMAND_HEADER_BEGIN_RESIZE wxEVT_HEADER_BEGIN_RESIZE
480 #define wxEVT_COMMAND_HEADER_RESIZING wxEVT_HEADER_RESIZING
481 #define wxEVT_COMMAND_HEADER_END_RESIZE wxEVT_HEADER_END_RESIZE
482 #define wxEVT_COMMAND_HEADER_BEGIN_REORDER wxEVT_HEADER_BEGIN_REORDER
483 #define wxEVT_COMMAND_HEADER_END_REORDER wxEVT_HEADER_END_REORDER
484 #define wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED wxEVT_HEADER_DRAGGING_CANCELLED
486 #endif // wxUSE_HEADERCTRL
488 #endif // _WX_HEADERCTRL_H_