1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/headerctrlcmn.cpp
3 // Purpose: implementation of wxHeaderCtrlBase
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/headerctrl.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
38 const unsigned int wxNO_COLUMN
= static_cast<unsigned>(-1);
40 } // anonymous namespace
42 // ============================================================================
43 // wxHeaderCtrlBase implementation
44 // ============================================================================
46 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr
[] = "wxHeaderCtrl";
48 BEGIN_EVENT_TABLE(wxHeaderCtrlBase
, wxControl
)
49 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY
, wxHeaderCtrlBase::OnSeparatorDClick
)
52 void wxHeaderCtrlBase::ScrollWindow(int dx
,
53 int WXUNUSED_UNLESS_DEBUG(dy
),
54 const wxRect
* WXUNUSED_UNLESS_DEBUG(rect
))
57 // this doesn't make sense at all
58 wxASSERT_MSG( !dy
, "header window can't be scrolled vertically" );
60 // this would actually be nice to support for "frozen" headers but it isn't
61 // supported currently
62 wxASSERT_MSG( !rect
, "header window can't be scrolled partially" );
67 // ----------------------------------------------------------------------------
68 // wxHeaderCtrlBase event handling
69 // ----------------------------------------------------------------------------
71 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent
& event
)
73 const unsigned col
= event
.GetColumn();
75 int w
= wxWindowBase::GetTextExtent(GetColumn(col
).GetTitle()).x
;
76 w
+= 2*GetCharWidth(); // add some arbitrary margins around text
78 if ( !UpdateColumnWidthToFit(col
, w
) )
84 // ----------------------------------------------------------------------------
85 // wxHeaderCtrlBase column reordering
86 // ----------------------------------------------------------------------------
88 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt
& order
)
90 const unsigned count
= GetColumnCount();
91 wxCHECK_RET( order
.size() == count
, "wrong number of columns" );
93 // check the array validity
94 wxArrayInt
seen(count
, 0);
95 for ( unsigned n
= 0; n
< count
; n
++ )
97 const unsigned idx
= order
[n
];
98 wxCHECK_RET( idx
< count
, "invalid column index" );
99 wxCHECK_RET( !seen
[idx
], "duplicate column index" );
104 DoSetColumnsOrder(order
);
106 // TODO-RTL: do we need to reverse the array?
109 wxArrayInt
wxHeaderCtrlBase::GetColumnsOrder() const
111 const wxArrayInt order
= DoGetColumnsOrder();
113 wxASSERT_MSG( order
.size() == GetColumnCount(), "invalid order array" );
118 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos
) const
120 wxCHECK_MSG( pos
< GetColumnCount(), wxNO_COLUMN
, "invalid position" );
122 return GetColumnsOrder()[pos
];
125 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx
) const
127 const unsigned count
= GetColumnCount();
129 wxCHECK_MSG( idx
< count
, wxNO_COLUMN
, "invalid index" );
131 const wxArrayInt order
= GetColumnsOrder();
132 for ( unsigned n
= 0; n
< count
; n
++ )
134 if ( (unsigned)order
[n
] == idx
)
138 wxFAIL_MSG( "column unexpectedly not displayed at all" );
143 // ============================================================================
144 // wxHeaderCtrlSimple implementation
145 // ============================================================================
147 void wxHeaderCtrlSimple::Init()
149 m_sortKey
= wxNO_COLUMN
;
152 wxHeaderColumn
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
)
157 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
159 m_cols
.insert(m_cols
.begin() + idx
, col
);
164 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
166 m_cols
.erase(m_cols
.begin() + idx
);
167 if ( idx
== m_sortKey
)
168 m_sortKey
= wxNO_COLUMN
;
173 void wxHeaderCtrlSimple::DeleteAllColumns()
176 m_sortKey
= wxNO_COLUMN
;
182 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
184 if ( show
!= m_cols
[idx
].IsShown() )
186 m_cols
[idx
].SetHidden(!show
);
192 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
194 RemoveSortIndicator();
196 m_cols
[idx
].SetAsSortKey(ascending
);
202 void wxHeaderCtrlSimple::RemoveSortIndicator()
204 if ( m_sortKey
!= wxNO_COLUMN
)
206 const unsigned sortOld
= m_sortKey
;
207 m_sortKey
= wxNO_COLUMN
;
209 m_cols
[sortOld
].UnsetAsSortKey();
211 UpdateColumn(sortOld
);
216 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
218 const int widthContents
= GetBestFittingWidth(idx
);
219 if ( widthContents
== -1 )
222 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
228 // ============================================================================
229 // wxHeaderCtrlEvent implementation
230 // ============================================================================
232 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
234 const wxEventType wxEVT_COMMAND_HEADER_CLICK
= wxNewEventType();
235 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
= wxNewEventType();
236 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
= wxNewEventType();
238 const wxEventType wxEVT_COMMAND_HEADER_DCLICK
= wxNewEventType();
239 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
= wxNewEventType();
240 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
= wxNewEventType();
242 const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
= wxNewEventType();
244 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
= wxNewEventType();
245 const wxEventType wxEVT_COMMAND_HEADER_RESIZING
= wxNewEventType();
246 const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
= wxNewEventType();
248 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
= wxNewEventType();
249 const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
= wxNewEventType();
251 const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
= wxNewEventType();