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::OnSeparatorDClick(wxHeaderCtrlEvent
& event
)
69 const unsigned col
= event
.GetColumn();
71 int w
= wxWindowBase::GetTextExtent(GetColumn(col
).GetTitle()).x
;
72 w
+= 2*GetCharWidth(); // add some arbitrary margins around text
74 if ( !UpdateColumnWidthToFit(col
, w
) )
80 // ============================================================================
81 // wxHeaderCtrlSimple implementation
82 // ============================================================================
84 void wxHeaderCtrlSimple::Init()
86 m_sortKey
= wxNO_COLUMN
;
89 wxHeaderColumnBase
& wxHeaderCtrlSimple::GetColumn(unsigned int idx
)
94 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple
& col
, unsigned int idx
)
96 m_cols
.insert(m_cols
.begin() + idx
, col
);
101 void wxHeaderCtrlSimple::DoDelete(unsigned int idx
)
103 m_cols
.erase(m_cols
.begin() + idx
);
104 if ( idx
== m_sortKey
)
105 m_sortKey
= wxNO_COLUMN
;
110 void wxHeaderCtrlSimple::DeleteAllColumns()
113 m_sortKey
= wxNO_COLUMN
;
119 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx
, bool show
)
121 if ( show
!= m_cols
[idx
].IsShown() )
123 m_cols
[idx
].SetHidden(!show
);
129 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx
, bool ascending
)
131 RemoveSortIndicator();
133 m_cols
[idx
].SetAsSortKey(ascending
);
139 void wxHeaderCtrlSimple::RemoveSortIndicator()
141 if ( m_sortKey
!= wxNO_COLUMN
)
143 const unsigned sortOld
= m_sortKey
;
144 m_sortKey
= wxNO_COLUMN
;
146 m_cols
[sortOld
].UnsetAsSortKey();
148 UpdateColumn(sortOld
);
153 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
155 const int widthContents
= GetBestFittingWidth(idx
);
156 if ( widthContents
== -1 )
159 m_cols
[idx
].SetWidth(wxMax(widthContents
, widthTitle
));
165 // ============================================================================
166 // wxHeaderCtrlEvent implementation
167 // ============================================================================
169 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent
, wxNotifyEvent
)
171 const wxEventType wxEVT_COMMAND_HEADER_CLICK
= wxNewEventType();
172 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_CLICK
= wxNewEventType();
173 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_CLICK
= wxNewEventType();
175 const wxEventType wxEVT_COMMAND_HEADER_DCLICK
= wxNewEventType();
176 const wxEventType wxEVT_COMMAND_HEADER_RIGHT_DCLICK
= wxNewEventType();
177 const wxEventType wxEVT_COMMAND_HEADER_MIDDLE_DCLICK
= wxNewEventType();
179 const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK
= wxNewEventType();