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 void wxHeaderCtrlBase::SetColumnCount(unsigned int count
)
69 OnColumnCountChanging(count
);
74 // ----------------------------------------------------------------------------
75 // wxHeaderCtrlBase event handling
76 // ----------------------------------------------------------------------------
78 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent
& event
)
80 const unsigned col
= event
.GetColumn();
82 int w
= wxWindowBase::GetTextExtent(GetColumn(col
).GetTitle()).x
;
83 w
+= 2*GetCharWidth(); // add some arbitrary margins around text
85 if ( !UpdateColumnWidthToFit(col
, w
) )
91 // ----------------------------------------------------------------------------
92 // wxHeaderCtrlBase column reordering
93 // ----------------------------------------------------------------------------
95 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt
& order
)
97 const unsigned count
= GetColumnCount();
98 wxCHECK_RET( order
.size() == count
, "wrong number of columns" );
100 // check the array validity
101 wxArrayInt
seen(count
, 0);
102 for ( unsigned n
= 0; n
< count
; n
++ )
104 const unsigned idx
= order
[n
];
105 wxCHECK_RET( idx
< count
, "invalid column index" );
106 wxCHECK_RET( !seen
[idx
], "duplicate column index" );
111 DoSetColumnsOrder(order
);
113 // TODO-RTL: do we need to reverse the array?
116 wxArrayInt
wxHeaderCtrlBase::GetColumnsOrder() const
118 const wxArrayInt order
= DoGetColumnsOrder();
120 wxASSERT_MSG( order
.size() == GetColumnCount(), "invalid order array" );
125 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos
) const
127 wxCHECK_MSG( pos
< GetColumnCount(), wxNO_COLUMN
, "invalid position" );
129 return GetColumnsOrder()[pos
];
132 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx
) const
134 const unsigned count
= GetColumnCount();
136 wxCHECK_MSG( idx
< count
, wxNO_COLUMN
, "invalid index" );
138 const wxArrayInt order
= GetColumnsOrder();
139 for ( unsigned n
= 0; n
< count
; n
++ )
141 if ( (unsigned)order
[n
] == idx
)
145 wxFAIL_MSG( "column unexpectedly not displayed at all" );
150 // ============================================================================
151 // wxHeaderCtrlSimple implementation
152 // ============================================================================
154 void wxHeaderCtrlSimple::Init()
156 m_sortKey
= wxNO_COLUMN
;
159 wxHeaderColumn
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
)
164 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
166 m_cols
.insert(m_cols
.begin() + idx
, col
);
171 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
173 m_cols
.erase(m_cols
.begin() + idx
);
174 if ( idx
== m_sortKey
)
175 m_sortKey
= wxNO_COLUMN
;
180 void wxHeaderCtrlSimple::DeleteAllColumns()
183 m_sortKey
= wxNO_COLUMN
;
189 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
191 if ( show
!= m_cols
[idx
].IsShown() )
193 m_cols
[idx
].SetHidden(!show
);
199 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
201 RemoveSortIndicator();
203 m_cols
[idx
].SetAsSortKey(ascending
);
209 void wxHeaderCtrlSimple::RemoveSortIndicator()
211 if ( m_sortKey
!= wxNO_COLUMN
)
213 const unsigned sortOld
= m_sortKey
;
214 m_sortKey
= wxNO_COLUMN
;
216 m_cols
[sortOld
].UnsetAsSortKey();
218 UpdateColumn(sortOld
);
223 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
225 const int widthContents
= GetBestFittingWidth(idx
);
226 if ( widthContents
== -1 )
229 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
234 // ============================================================================
235 // wxHeaderCtrlEvent implementation
236 // ============================================================================
238 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
240 const wxEventType wxEVT_COMMAND_HEADER_CLICK
= wxNewEventType();
241 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
= wxNewEventType();
242 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
= wxNewEventType();
244 const wxEventType wxEVT_COMMAND_HEADER_DCLICK
= wxNewEventType();
245 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
= wxNewEventType();
246 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
= wxNewEventType();
248 const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
= wxNewEventType();
250 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_RESIZE
= wxNewEventType();
251 const wxEventType wxEVT_COMMAND_HEADER_RESIZING
= wxNewEventType();
252 const wxEventType wxEVT_COMMAND_HEADER_END_RESIZE
= wxNewEventType();
254 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER
= wxNewEventType();
255 const wxEventType wxEVT_COMMAND_HEADER_END_REORDER
= wxNewEventType();
257 const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
= wxNewEventType();