there is no need to call UpdateColumn() in the derived class UpdateColumnWidthToFit...
[wxWidgets.git] / src / common / headerctrlcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/headerctrlcmn.cpp
3 // Purpose: implementation of wxHeaderCtrlBase
4 // Author: Vadim Zeitlin
5 // Created: 2008-12-02
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #endif // WX_PRECOMP
28
29 #include "wx/headerctrl.h"
30
31 // ----------------------------------------------------------------------------
32 // constants
33 // ----------------------------------------------------------------------------
34
35 namespace
36 {
37
38 const unsigned int wxNO_COLUMN = static_cast<unsigned>(-1);
39
40 } // anonymous namespace
41
42 // ============================================================================
43 // wxHeaderCtrlBase implementation
44 // ============================================================================
45
46 extern WXDLLIMPEXP_DATA_CORE(const char) wxHeaderCtrlNameStr[] = "wxHeaderCtrl";
47
48 BEGIN_EVENT_TABLE(wxHeaderCtrlBase, wxControl)
49 EVT_HEADER_SEPARATOR_DCLICK(wxID_ANY, wxHeaderCtrlBase::OnSeparatorDClick)
50 END_EVENT_TABLE()
51
52 void wxHeaderCtrlBase::ScrollWindow(int dx,
53 int WXUNUSED_UNLESS_DEBUG(dy),
54 const wxRect * WXUNUSED_UNLESS_DEBUG(rect))
55
56 {
57 // this doesn't make sense at all
58 wxASSERT_MSG( !dy, "header window can't be scrolled vertically" );
59
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" );
63
64 DoScrollHorz(dx);
65 }
66
67 void wxHeaderCtrlBase::SetColumnCount(unsigned int count)
68 {
69 OnColumnCountChanging(count);
70
71 DoSetCount(count);
72 }
73
74 // ----------------------------------------------------------------------------
75 // wxHeaderCtrlBase event handling
76 // ----------------------------------------------------------------------------
77
78 void wxHeaderCtrlBase::OnSeparatorDClick(wxHeaderCtrlEvent& event)
79 {
80 const unsigned col = event.GetColumn();
81
82 int w = wxWindowBase::GetTextExtent(GetColumn(col).GetTitle()).x;
83 w += 2*GetCharWidth(); // add some arbitrary margins around text
84
85 if ( !UpdateColumnWidthToFit(col, w) )
86 event.Skip();
87 else
88 UpdateColumn(col);
89 }
90
91 // ----------------------------------------------------------------------------
92 // wxHeaderCtrlBase column reordering
93 // ----------------------------------------------------------------------------
94
95 void wxHeaderCtrlBase::SetColumnsOrder(const wxArrayInt& order)
96 {
97 const unsigned count = GetColumnCount();
98 wxCHECK_RET( order.size() == count, "wrong number of columns" );
99
100 // check the array validity
101 wxArrayInt seen(count, 0);
102 for ( unsigned n = 0; n < count; n++ )
103 {
104 const unsigned idx = order[n];
105 wxCHECK_RET( idx < count, "invalid column index" );
106 wxCHECK_RET( !seen[idx], "duplicate column index" );
107
108 seen[idx] = 1;
109 }
110
111 DoSetColumnsOrder(order);
112
113 // TODO-RTL: do we need to reverse the array?
114 }
115
116 wxArrayInt wxHeaderCtrlBase::GetColumnsOrder() const
117 {
118 const wxArrayInt order = DoGetColumnsOrder();
119
120 wxASSERT_MSG( order.size() == GetColumnCount(), "invalid order array" );
121
122 return order;
123 }
124
125 unsigned int wxHeaderCtrlBase::GetColumnAt(unsigned int pos) const
126 {
127 wxCHECK_MSG( pos < GetColumnCount(), wxNO_COLUMN, "invalid position" );
128
129 return GetColumnsOrder()[pos];
130 }
131
132 unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const
133 {
134 const unsigned count = GetColumnCount();
135
136 wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" );
137
138 const wxArrayInt order = GetColumnsOrder();
139 for ( unsigned n = 0; n < count; n++ )
140 {
141 if ( (unsigned)order[n] == idx )
142 return n;
143 }
144
145 wxFAIL_MSG( "column unexpectedly not displayed at all" );
146
147 return wxNO_COLUMN;
148 }
149
150 // ============================================================================
151 // wxHeaderCtrlSimple implementation
152 // ============================================================================
153
154 void wxHeaderCtrlSimple::Init()
155 {
156 m_sortKey = wxNO_COLUMN;
157 }
158
159 wxHeaderColumn& wxHeaderCtrlSimple::GetColumn(unsigned int idx)
160 {
161 return m_cols[idx];
162 }
163
164 void wxHeaderCtrlSimple::DoInsert(const wxHeaderColumnSimple& col, unsigned int idx)
165 {
166 m_cols.insert(m_cols.begin() + idx, col);
167
168 UpdateColumnCount();
169 }
170
171 void wxHeaderCtrlSimple::DoDelete(unsigned int idx)
172 {
173 m_cols.erase(m_cols.begin() + idx);
174 if ( idx == m_sortKey )
175 m_sortKey = wxNO_COLUMN;
176
177 UpdateColumnCount();
178 }
179
180 void wxHeaderCtrlSimple::DeleteAllColumns()
181 {
182 m_cols.clear();
183 m_sortKey = wxNO_COLUMN;
184
185 UpdateColumnCount();
186 }
187
188
189 void wxHeaderCtrlSimple::DoShowColumn(unsigned int idx, bool show)
190 {
191 if ( show != m_cols[idx].IsShown() )
192 {
193 m_cols[idx].SetHidden(!show);
194
195 UpdateColumn(idx);
196 }
197 }
198
199 void wxHeaderCtrlSimple::DoShowSortIndicator(unsigned int idx, bool ascending)
200 {
201 RemoveSortIndicator();
202
203 m_cols[idx].SetAsSortKey(ascending);
204 m_sortKey = idx;
205
206 UpdateColumn(idx);
207 }
208
209 void wxHeaderCtrlSimple::RemoveSortIndicator()
210 {
211 if ( m_sortKey != wxNO_COLUMN )
212 {
213 const unsigned sortOld = m_sortKey;
214 m_sortKey = wxNO_COLUMN;
215
216 m_cols[sortOld].UnsetAsSortKey();
217
218 UpdateColumn(sortOld);
219 }
220 }
221
222 bool
223 wxHeaderCtrlSimple::UpdateColumnWidthToFit(unsigned int idx, int widthTitle)
224 {
225 const int widthContents = GetBestFittingWidth(idx);
226 if ( widthContents == -1 )
227 return false;
228
229 m_cols[idx].SetWidth(wxMax(widthContents, widthTitle));
230
231 return true;
232 }
233
234 // ============================================================================
235 // wxHeaderCtrlEvent implementation
236 // ============================================================================
237
238 IMPLEMENT_DYNAMIC_CLASS(wxHeaderCtrlEvent, wxNotifyEvent)
239
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();
243
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();
247
248 const wxEventType wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK = wxNewEventType();
249
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();
253
254 const wxEventType wxEVT_COMMAND_HEADER_BEGIN_REORDER = wxNewEventType();
255 const wxEventType wxEVT_COMMAND_HEADER_END_REORDER = wxNewEventType();
256
257 const wxEventType wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED = wxNewEventType();