implement column resizing events in wxHeaderCtrl
[wxWidgets.git] / src / generic / headerctrlg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/headerctrlg.cpp
3 // Purpose: generic wxHeaderCtrl implementation
4 // Author: Vadim Zeitlin
5 // Created: 2008-12-03
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 #ifdef wxHAS_GENERIC_HEADERCTRL
32
33 #include "wx/dcbuffer.h"
34 #include "wx/renderer.h"
35
36 // ----------------------------------------------------------------------------
37 // constants
38 // ----------------------------------------------------------------------------
39
40 namespace
41 {
42
43 const unsigned NO_SORT = (unsigned)-1;
44
45 const unsigned COL_NONE = (unsigned)-1;
46
47 } // anonymous namespace
48
49 // ============================================================================
50 // wxHeaderCtrl implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxHeaderCtrl creation
55 // ----------------------------------------------------------------------------
56
57 void wxHeaderCtrl::Init()
58 {
59 m_numColumns = 0;
60 m_hover =
61 m_colBeingResized = COL_NONE;
62 m_scrollOffset = 0;
63 }
64
65 bool wxHeaderCtrl::Create(wxWindow *parent,
66 wxWindowID id,
67 const wxPoint& pos,
68 const wxSize& size,
69 long style,
70 const wxString& name)
71 {
72 if ( !wxHeaderCtrlBase::Create(parent, id, pos, size,
73 style, wxDefaultValidator, name) )
74 return false;
75
76 // tell the system to not paint the background at all to avoid flicker as
77 // we paint the entire window area in our OnPaint()
78 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
79
80 return true;
81 }
82
83 wxHeaderCtrl::~wxHeaderCtrl()
84 {
85 }
86
87 // ----------------------------------------------------------------------------
88 // wxHeaderCtrl columns manipulation
89 // ----------------------------------------------------------------------------
90
91 void wxHeaderCtrl::DoSetCount(unsigned int count)
92 {
93 m_numColumns = count;
94
95 Refresh();
96 }
97
98 unsigned int wxHeaderCtrl::DoGetCount() const
99 {
100 return m_numColumns;
101 }
102
103 void wxHeaderCtrl::DoUpdate(unsigned int idx)
104 {
105 // we need to refresh not only this column but also the ones after it in
106 // case it was shown or hidden or its width changed -- it would be nice to
107 // avoid doing this unnecessary by storing the old column width (TODO)
108 RefreshColsAfter(idx);
109 }
110
111 // ----------------------------------------------------------------------------
112 // wxHeaderCtrl scrolling
113 // ----------------------------------------------------------------------------
114
115 void wxHeaderCtrl::DoScrollHorz(int dx)
116 {
117 m_scrollOffset += dx;
118
119 // don't call our own version which calls this function!
120 wxControl::ScrollWindow(dx, 0);
121 }
122
123 // ----------------------------------------------------------------------------
124 // wxHeaderCtrl geometry
125 // ----------------------------------------------------------------------------
126
127 wxSize wxHeaderCtrl::DoGetBestSize() const
128 {
129 // the vertical size is rather arbitrary but it looks better if we leave
130 // some space around the text
131 return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4);
132 }
133
134 int wxHeaderCtrl::GetColStart(unsigned int idx) const
135 {
136 wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this);
137
138 int pos = m_scrollOffset;
139 for ( unsigned n = 0; n < idx; n++ )
140 {
141 const wxHeaderColumnBase& col = self->GetColumn(n);
142 if ( col.IsShown() )
143 pos += col.GetWidth();
144 }
145
146 return pos;
147 }
148
149 int wxHeaderCtrl::FindColumnAtPos(int x, bool& onSeparator) const
150 {
151 wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this);
152
153 int pos = 0;
154 const unsigned count = GetColumnCount();
155 for ( unsigned n = 0; n < count; n++ )
156 {
157 const wxHeaderColumnBase& col = self->GetColumn(n);
158 if ( col.IsHidden() )
159 continue;
160
161 pos += col.GetWidth();
162
163 // if the column is resizeable, check if we're approximatively over the
164 // line separating it from the next column
165 //
166 // TODO: don't hardcode sensitivity
167 if ( col.IsResizeable() && abs(x - pos) < 8 )
168 {
169 onSeparator = true;
170 return n;
171 }
172
173 // inside this column?
174 if ( x < pos )
175 {
176 onSeparator = false;
177 return n;
178 }
179 }
180
181 return COL_NONE;
182 }
183
184 // ----------------------------------------------------------------------------
185 // wxHeaderCtrl repainting
186 // ----------------------------------------------------------------------------
187
188 void wxHeaderCtrl::RefreshCol(unsigned int idx)
189 {
190 wxRect rect = GetClientRect();
191 rect.x += GetColStart(idx);
192 rect.width = GetColumn(idx).GetWidth();
193
194 RefreshRect(rect);
195 }
196
197 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx)
198 {
199 if ( idx != COL_NONE )
200 RefreshCol(idx);
201 }
202
203 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx)
204 {
205 wxRect rect = GetClientRect();
206 const int ofs = GetColStart(idx);
207 rect.x += ofs;
208 rect.width -= ofs;
209
210 RefreshRect(rect);
211 }
212
213 // ----------------------------------------------------------------------------
214 // wxHeaderCtrl dragging
215 // ----------------------------------------------------------------------------
216
217 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical)
218 {
219 // unfortunately drawing the marker over the parent window doesn't work as
220 // it's usually covered by another window (the main control view) so just
221 // draw the marker over the header itself, even if it makes it not very
222 // useful
223 wxClientDC dc(this);
224
225 wxDCOverlay dcover(m_overlay, &dc);
226 dcover.Clear();
227
228 if ( xPhysical != -1 )
229 {
230 dc.SetPen(*wxLIGHT_GREY_PEN);
231 dc.DrawLine(xPhysical, 0, xPhysical, GetClientSize().y);
232 }
233 }
234
235 void wxHeaderCtrl::EndDragging()
236 {
237 UpdateResizingMarker(-1);
238
239 m_overlay.Reset();
240 }
241
242 void wxHeaderCtrl::EndResizing(int width)
243 {
244 wxASSERT_MSG( m_colBeingResized != COL_NONE,
245 "shouldn't be called if we're not resizing" );
246
247 EndDragging();
248
249 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_DRAG, GetId());
250 event.SetEventObject(this);
251 event.SetColumn(m_colBeingResized);
252 if ( width == -1 )
253 event.SetCancelled();
254 else
255 event.SetWidth(width);
256
257 GetEventHandler()->ProcessEvent(event);
258
259 m_colBeingResized = COL_NONE;
260 }
261
262 // ----------------------------------------------------------------------------
263 // wxHeaderCtrl event handlers
264 // ----------------------------------------------------------------------------
265
266 BEGIN_EVENT_TABLE(wxHeaderCtrl, wxHeaderCtrlBase)
267 EVT_PAINT(wxHeaderCtrl::OnPaint)
268
269 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse)
270
271 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost)
272 END_EVENT_TABLE()
273
274 void wxHeaderCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
275 {
276 int w, h;
277 GetClientSize(&w, &h);
278
279 wxAutoBufferedPaintDC dc(this);
280
281 dc.SetBackground(GetBackgroundColour());
282 dc.Clear();
283
284 // account for the horizontal scrollbar offset in the parent window
285 dc.SetDeviceOrigin(m_scrollOffset, 0);
286
287 const unsigned int count = m_numColumns;
288 int xpos = 0;
289 for ( unsigned int i = 0; i < count; i++ )
290 {
291 const wxHeaderColumnBase& col = GetColumn(i);
292 if ( col.IsHidden() )
293 continue;
294
295 const int colWidth = col.GetWidth();
296
297 wxHeaderSortIconType sortArrow;
298 if ( col.IsSortKey() )
299 {
300 sortArrow = col.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
301 : wxHDR_SORT_ICON_DOWN;
302 }
303 else // not sorting by this column
304 {
305 sortArrow = wxHDR_SORT_ICON_NONE;
306 }
307
308 int state = 0;
309 if ( IsEnabled() )
310 {
311 if ( i == m_hover )
312 state = wxCONTROL_CURRENT;
313 }
314 else // disabled
315 {
316 state = wxCONTROL_DISABLED;
317 }
318
319 wxHeaderButtonParams params;
320 params.m_labelText = col.GetTitle();
321 params.m_labelBitmap = col.GetBitmap();
322 params.m_labelAlignment = col.GetAlignment();
323
324 wxRendererNative::Get().DrawHeaderButton
325 (
326 this,
327 dc,
328 wxRect(xpos, 0, colWidth, h),
329 state,
330 sortArrow,
331 &params
332 );
333
334 xpos += colWidth;
335 }
336 }
337
338 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
339 {
340 if ( m_colBeingResized != COL_NONE )
341 EndResizing(-1);
342 }
343
344 void wxHeaderCtrl::OnMouse(wxMouseEvent& mevent)
345 {
346 // do this in advance to allow simply returning if we're not interested,
347 // we'll undo it if we do handle the event below
348 mevent.Skip();
349
350
351 // account for the control displacement
352 const int xPhysical = mevent.GetX();
353 const int xLogical = xPhysical - m_scrollOffset;
354
355 // first deal with the [continuation of any] dragging operations in
356 // progress
357 if ( m_colBeingResized != COL_NONE )
358 {
359 if ( mevent.LeftUp() )
360 EndResizing(xLogical - GetColStart(m_colBeingResized));
361 else // update the live separator position
362 UpdateResizingMarker(xPhysical);
363
364 return;
365 }
366
367
368 // find if the event is over a column at all
369 bool onSeparator;
370 const unsigned col = mevent.Leaving()
371 ? (onSeparator = false, COL_NONE)
372 : FindColumnAtPos(xLogical, onSeparator);
373
374
375 // update the highlighted column if it changed
376 if ( col != m_hover )
377 {
378 const unsigned hoverOld = m_hover;
379 m_hover = col;
380
381 RefreshColIfNotNone(hoverOld);
382 RefreshColIfNotNone(m_hover);
383 }
384
385 // update mouse cursor as it moves around
386 if ( mevent.Moving() )
387 {
388 SetCursor(onSeparator ? wxCursor(wxCURSOR_SIZEWE) : wxNullCursor);
389 return;
390 }
391
392 // all the other events only make sense when they happen over a column
393 if ( col == COL_NONE )
394 return;
395
396
397 // enter various dragging modes on left mouse press
398 if ( mevent.LeftDown() )
399 {
400 if ( onSeparator )
401 {
402 // start resizing the column
403 m_colBeingResized = col;
404 UpdateResizingMarker(xPhysical);
405 }
406 else // on column itself
407 {
408 // TODO: drag column
409 ;
410 }
411
412 return;
413 }
414
415 // determine the type of header event corresponding to click events
416 wxEventType evtType = wxEVT_NULL;
417 const bool click = mevent.ButtonUp(),
418 dblclk = mevent.ButtonDClick();
419 if ( click || dblclk )
420 {
421 switch ( mevent.GetButton() )
422 {
423 case wxMOUSE_BTN_LEFT:
424 // treat left double clicks on separator specially
425 if ( onSeparator && dblclk )
426 {
427 evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
428 }
429 else // not double click on separator
430 {
431 evtType = click ? wxEVT_COMMAND_HEADER_CLICK
432 : wxEVT_COMMAND_HEADER_DCLICK;
433 }
434 break;
435
436 case wxMOUSE_BTN_RIGHT:
437 evtType = click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK
438 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK;
439 break;
440
441 case wxMOUSE_BTN_MIDDLE:
442 evtType = click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
443 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK;
444 break;
445
446 default:
447 // ignore clicks from other mouse buttons
448 ;
449 }
450 }
451
452 if ( evtType == wxEVT_NULL )
453 return;
454
455 wxHeaderCtrlEvent event(evtType, GetId());
456 event.SetEventObject(this);
457 event.SetColumn(col);
458
459 if ( GetEventHandler()->ProcessEvent(event) )
460 mevent.Skip(false);
461 }
462
463 #endif // wxHAS_GENERIC_HEADERCTRL