provide Do[GS]etColumnsOrder() stubs for the generic 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 m_colIndices.resize(count);
95
96 Refresh();
97 }
98
99 unsigned int wxHeaderCtrl::DoGetCount() const
100 {
101 return m_numColumns;
102 }
103
104 void wxHeaderCtrl::DoUpdate(unsigned int idx)
105 {
106 // we need to refresh not only this column but also the ones after it in
107 // case it was shown or hidden or its width changed -- it would be nice to
108 // avoid doing this unnecessary by storing the old column width (TODO)
109 RefreshColsAfter(idx);
110 }
111
112 // ----------------------------------------------------------------------------
113 // wxHeaderCtrl scrolling
114 // ----------------------------------------------------------------------------
115
116 void wxHeaderCtrl::DoScrollHorz(int dx)
117 {
118 m_scrollOffset += dx;
119
120 // don't call our own version which calls this function!
121 wxControl::ScrollWindow(dx, 0);
122 }
123
124 // ----------------------------------------------------------------------------
125 // wxHeaderCtrl geometry
126 // ----------------------------------------------------------------------------
127
128 wxSize wxHeaderCtrl::DoGetBestSize() const
129 {
130 // the vertical size is rather arbitrary but it looks better if we leave
131 // some space around the text
132 return wxSize(GetColStart(GetColumnCount()), (7*GetCharHeight())/4);
133 }
134
135 int wxHeaderCtrl::GetColStart(unsigned int idx) const
136 {
137 wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this);
138
139 int pos = m_scrollOffset;
140 for ( unsigned n = 0; n < idx; n++ )
141 {
142 const wxHeaderColumnBase& col = self->GetColumn(n);
143 if ( col.IsShown() )
144 pos += col.GetWidth();
145 }
146
147 return pos;
148 }
149
150 int wxHeaderCtrl::FindColumnAtPos(int x, bool& onSeparator) const
151 {
152 wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this);
153
154 int pos = 0;
155 const unsigned count = GetColumnCount();
156 for ( unsigned n = 0; n < count; n++ )
157 {
158 const wxHeaderColumnBase& col = self->GetColumn(n);
159 if ( col.IsHidden() )
160 continue;
161
162 pos += col.GetWidth();
163
164 // if the column is resizeable, check if we're approximatively over the
165 // line separating it from the next column
166 //
167 // TODO: don't hardcode sensitivity
168 if ( col.IsResizeable() && abs(x - pos) < 8 )
169 {
170 onSeparator = true;
171 return n;
172 }
173
174 // inside this column?
175 if ( x < pos )
176 {
177 onSeparator = false;
178 return n;
179 }
180 }
181
182 return COL_NONE;
183 }
184
185 // ----------------------------------------------------------------------------
186 // wxHeaderCtrl repainting
187 // ----------------------------------------------------------------------------
188
189 void wxHeaderCtrl::RefreshCol(unsigned int idx)
190 {
191 wxRect rect = GetClientRect();
192 rect.x += GetColStart(idx);
193 rect.width = GetColumn(idx).GetWidth();
194
195 RefreshRect(rect);
196 }
197
198 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx)
199 {
200 if ( idx != COL_NONE )
201 RefreshCol(idx);
202 }
203
204 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx)
205 {
206 wxRect rect = GetClientRect();
207 const int ofs = GetColStart(idx);
208 rect.x += ofs;
209 rect.width -= ofs;
210
211 RefreshRect(rect);
212 }
213
214 // ----------------------------------------------------------------------------
215 // wxHeaderCtrl dragging
216 // ----------------------------------------------------------------------------
217
218 bool wxHeaderCtrl::IsResizing() const
219 {
220 return m_colBeingResized != COL_NONE;
221 }
222
223 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical)
224 {
225 // unfortunately drawing the marker over the parent window doesn't work as
226 // it's usually covered by another window (the main control view) so just
227 // draw the marker over the header itself, even if it makes it not very
228 // useful
229 wxClientDC dc(this);
230
231 wxDCOverlay dcover(m_overlay, &dc);
232 dcover.Clear();
233
234 if ( xPhysical != -1 )
235 {
236 dc.SetPen(*wxLIGHT_GREY_PEN);
237 dc.DrawLine(xPhysical, 0, xPhysical, GetClientSize().y);
238 }
239 }
240
241 void wxHeaderCtrl::EndDragging()
242 {
243 UpdateResizingMarker(-1);
244
245 m_overlay.Reset();
246
247 // don't use the special dragging cursor any more
248 SetCursor(wxNullCursor);
249 }
250
251 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col, int& xPhysical)
252 {
253 const int xStart = GetColStart(col);
254
255 // notice that GetMinWidth() returns 0 if there is no minimal width so it
256 // still makes sense to use it even in this case
257 const int xMinEnd = xStart + GetColumn(col).GetMinWidth();
258
259 if ( xPhysical < xMinEnd )
260 xPhysical = xMinEnd;
261
262 return xPhysical - xStart;
263 }
264
265 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col, int xPhysical)
266 {
267 wxHeaderCtrlEvent event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
268 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE,
269 GetId());
270 event.SetEventObject(this);
271 event.SetColumn(col);
272
273 event.SetWidth(ConstrainByMinWidth(col, xPhysical));
274
275 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
276 {
277 if ( IsResizing() )
278 {
279 ReleaseMouse();
280 EndResizing(-1);
281 }
282 //else: nothing to do -- we just don't start to resize
283 }
284 else // go ahead with resizing
285 {
286 if ( !IsResizing() )
287 {
288 m_colBeingResized = col;
289 SetCursor(wxCursor(wxCURSOR_SIZEWE));
290 CaptureMouse();
291 }
292 //else: we had already done the above when we started
293
294 UpdateResizingMarker(xPhysical);
295 }
296 }
297
298 void wxHeaderCtrl::EndResizing(int xPhysical)
299 {
300 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
301
302 EndDragging();
303
304 const bool cancelled = xPhysical == -1;
305
306 // if dragging was cancelled we must have already lost the mouse capture so
307 // don't try to release it
308 if ( !cancelled )
309 ReleaseMouse();
310
311 wxHeaderCtrlEvent event(cancelled ? wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED
312 : wxEVT_COMMAND_HEADER_END_RESIZE,
313 GetId());
314 event.SetEventObject(this);
315 event.SetColumn(m_colBeingResized);
316 if ( !cancelled )
317 event.SetWidth(ConstrainByMinWidth(m_colBeingResized, xPhysical));
318
319 GetEventHandler()->ProcessEvent(event);
320
321 m_colBeingResized = COL_NONE;
322 }
323
324 // ----------------------------------------------------------------------------
325 // wxHeaderCtrl column reordering
326 // ----------------------------------------------------------------------------
327
328 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt& order)
329 {
330 m_colIndices = order;
331 Refresh();
332 }
333
334 wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const
335 {
336 return m_colIndices;
337 }
338
339 // ----------------------------------------------------------------------------
340 // wxHeaderCtrl event handlers
341 // ----------------------------------------------------------------------------
342
343 BEGIN_EVENT_TABLE(wxHeaderCtrl, wxHeaderCtrlBase)
344 EVT_PAINT(wxHeaderCtrl::OnPaint)
345
346 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse)
347
348 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost)
349
350 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown)
351 END_EVENT_TABLE()
352
353 void wxHeaderCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
354 {
355 int w, h;
356 GetClientSize(&w, &h);
357
358 wxAutoBufferedPaintDC dc(this);
359
360 dc.SetBackground(GetBackgroundColour());
361 dc.Clear();
362
363 // account for the horizontal scrollbar offset in the parent window
364 dc.SetDeviceOrigin(m_scrollOffset, 0);
365
366 const unsigned int count = m_numColumns;
367 int xpos = 0;
368 for ( unsigned int i = 0; i < count; i++ )
369 {
370 const wxHeaderColumnBase& col = GetColumn(i);
371 if ( col.IsHidden() )
372 continue;
373
374 const int colWidth = col.GetWidth();
375
376 wxHeaderSortIconType sortArrow;
377 if ( col.IsSortKey() )
378 {
379 sortArrow = col.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
380 : wxHDR_SORT_ICON_DOWN;
381 }
382 else // not sorting by this column
383 {
384 sortArrow = wxHDR_SORT_ICON_NONE;
385 }
386
387 int state = 0;
388 if ( IsEnabled() )
389 {
390 if ( i == m_hover )
391 state = wxCONTROL_CURRENT;
392 }
393 else // disabled
394 {
395 state = wxCONTROL_DISABLED;
396 }
397
398 wxHeaderButtonParams params;
399 params.m_labelText = col.GetTitle();
400 params.m_labelBitmap = col.GetBitmap();
401 params.m_labelAlignment = col.GetAlignment();
402
403 wxRendererNative::Get().DrawHeaderButton
404 (
405 this,
406 dc,
407 wxRect(xpos, 0, colWidth, h),
408 state,
409 sortArrow,
410 &params
411 );
412
413 xpos += colWidth;
414 }
415 }
416
417 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
418 {
419 if ( IsResizing() )
420 EndResizing(-1);
421 }
422
423 void wxHeaderCtrl::OnKeyDown(wxKeyEvent& event)
424 {
425 if ( IsResizing() && event.GetKeyCode() == WXK_ESCAPE )
426 {
427 ReleaseMouse();
428 EndResizing(-1);
429 }
430 else
431 {
432 event.Skip();
433 }
434 }
435
436 void wxHeaderCtrl::OnMouse(wxMouseEvent& mevent)
437 {
438 // do this in advance to allow simply returning if we're not interested,
439 // we'll undo it if we do handle the event below
440 mevent.Skip();
441
442
443 // account for the control displacement
444 const int xPhysical = mevent.GetX();
445 const int xLogical = xPhysical - m_scrollOffset;
446
447 // first deal with the [continuation of any] dragging operations in
448 // progress
449 if ( IsResizing() )
450 {
451 if ( mevent.LeftUp() )
452 EndResizing(xPhysical);
453 else // update the live separator position
454 StartOrContinueResizing(m_colBeingResized, xPhysical);
455
456 return;
457 }
458
459
460 // find if the event is over a column at all
461 bool onSeparator;
462 const unsigned col = mevent.Leaving()
463 ? (onSeparator = false, COL_NONE)
464 : FindColumnAtPos(xLogical, onSeparator);
465
466
467 // update the highlighted column if it changed
468 if ( col != m_hover )
469 {
470 const unsigned hoverOld = m_hover;
471 m_hover = col;
472
473 RefreshColIfNotNone(hoverOld);
474 RefreshColIfNotNone(m_hover);
475 }
476
477 // update mouse cursor as it moves around
478 if ( mevent.Moving() )
479 {
480 SetCursor(onSeparator ? wxCursor(wxCURSOR_SIZEWE) : wxNullCursor);
481 return;
482 }
483
484 // all the other events only make sense when they happen over a column
485 if ( col == COL_NONE )
486 return;
487
488
489 // enter various dragging modes on left mouse press
490 if ( mevent.LeftDown() )
491 {
492 if ( onSeparator )
493 {
494 // start resizing the column
495 wxASSERT_MSG( !IsResizing(), "reentering resize mode?" );
496 StartOrContinueResizing(col, xPhysical);
497 }
498 else // on column itself
499 {
500 // TODO: drag column
501 ;
502 }
503
504 return;
505 }
506
507 // determine the type of header event corresponding to click events
508 wxEventType evtType = wxEVT_NULL;
509 const bool click = mevent.ButtonUp(),
510 dblclk = mevent.ButtonDClick();
511 if ( click || dblclk )
512 {
513 switch ( mevent.GetButton() )
514 {
515 case wxMOUSE_BTN_LEFT:
516 // treat left double clicks on separator specially
517 if ( onSeparator && dblclk )
518 {
519 evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
520 }
521 else // not double click on separator
522 {
523 evtType = click ? wxEVT_COMMAND_HEADER_CLICK
524 : wxEVT_COMMAND_HEADER_DCLICK;
525 }
526 break;
527
528 case wxMOUSE_BTN_RIGHT:
529 evtType = click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK
530 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK;
531 break;
532
533 case wxMOUSE_BTN_MIDDLE:
534 evtType = click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
535 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK;
536 break;
537
538 default:
539 // ignore clicks from other mouse buttons
540 ;
541 }
542 }
543
544 if ( evtType == wxEVT_NULL )
545 return;
546
547 wxHeaderCtrlEvent event(evtType, GetId());
548 event.SetEventObject(this);
549 event.SetColumn(col);
550
551 if ( GetEventHandler()->ProcessEvent(event) )
552 mevent.Skip(false);
553 }
554
555 #endif // wxHAS_GENERIC_HEADERCTRL