]> git.saurik.com Git - wxWidgets.git/blob - src/generic/headerctrlg.cpp
no changes, just refactoring: extracted the code to update the column indices array...
[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 =
62 m_colBeingReordered = COL_NONE;
63 m_dragOffset = 0;
64 m_scrollOffset = 0;
65 }
66
67 bool wxHeaderCtrl::Create(wxWindow *parent,
68 wxWindowID id,
69 const wxPoint& pos,
70 const wxSize& size,
71 long style,
72 const wxString& name)
73 {
74 if ( !wxHeaderCtrlBase::Create(parent, id, pos, size,
75 style, wxDefaultValidator, name) )
76 return false;
77
78 // tell the system to not paint the background at all to avoid flicker as
79 // we paint the entire window area in our OnPaint()
80 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
81
82 return true;
83 }
84
85 wxHeaderCtrl::~wxHeaderCtrl()
86 {
87 }
88
89 // ----------------------------------------------------------------------------
90 // wxHeaderCtrl columns manipulation
91 // ----------------------------------------------------------------------------
92
93 void wxHeaderCtrl::DoSetCount(unsigned int count)
94 {
95 // update the column indices order array before changing m_numColumns
96 DoResizeColumnIndices(m_colIndices, count);
97
98 m_numColumns = count;
99
100 InvalidateBestSize();
101 Refresh();
102 }
103
104 unsigned int wxHeaderCtrl::DoGetCount() const
105 {
106 return m_numColumns;
107 }
108
109 void wxHeaderCtrl::DoUpdate(unsigned int idx)
110 {
111 InvalidateBestSize();
112
113 // we need to refresh not only this column but also the ones after it in
114 // case it was shown or hidden or its width changed -- it would be nice to
115 // avoid doing this unnecessary by storing the old column width (TODO)
116 RefreshColsAfter(idx);
117 }
118
119 // ----------------------------------------------------------------------------
120 // wxHeaderCtrl scrolling
121 // ----------------------------------------------------------------------------
122
123 void wxHeaderCtrl::DoScrollHorz(int dx)
124 {
125 m_scrollOffset += dx;
126
127 // don't call our own version which calls this function!
128 wxControl::ScrollWindow(dx, 0);
129 }
130
131 // ----------------------------------------------------------------------------
132 // wxHeaderCtrl geometry
133 // ----------------------------------------------------------------------------
134
135 wxSize wxHeaderCtrl::DoGetBestSize() const
136 {
137 // the vertical size is rather arbitrary but it looks better if we leave
138 // some space around the text
139 const wxSize size(IsEmpty() ? wxHeaderCtrlBase::DoGetBestSize().x
140 : GetColEnd(GetColumnCount() - 1),
141 (7*GetCharHeight())/4);
142 CacheBestSize(size);
143 return size;
144 }
145
146 int wxHeaderCtrl::GetColStart(unsigned int idx) const
147 {
148 wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this);
149
150 int pos = m_scrollOffset;
151 for ( unsigned n = 0; ; n++ )
152 {
153 const unsigned i = m_colIndices[n];
154 if ( i == idx )
155 break;
156
157 const wxHeaderColumn& col = self->GetColumn(i);
158 if ( col.IsShown() )
159 pos += col.GetWidth();
160 }
161
162 return pos;
163 }
164
165 int wxHeaderCtrl::GetColEnd(unsigned int idx) const
166 {
167 int x = GetColStart(idx);
168
169 return x + const_cast<wxHeaderCtrl *>(this)->GetColumn(idx).GetWidth();
170 }
171
172 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x, bool *onSeparator) const
173 {
174 wxHeaderCtrl * const self = const_cast<wxHeaderCtrl *>(this);
175
176 int pos = 0;
177 const unsigned count = GetColumnCount();
178 for ( unsigned n = 0; n < count; n++ )
179 {
180 const unsigned idx = m_colIndices[n];
181 const wxHeaderColumn& col = self->GetColumn(idx);
182 if ( col.IsHidden() )
183 continue;
184
185 pos += col.GetWidth();
186
187 // if the column is resizeable, check if we're approximatively over the
188 // line separating it from the next column
189 //
190 // TODO: don't hardcode sensitivity
191 if ( col.IsResizeable() && abs(x - pos) < 8 )
192 {
193 if ( onSeparator )
194 *onSeparator = true;
195 return idx;
196 }
197
198 // inside this column?
199 if ( x < pos )
200 {
201 if ( onSeparator )
202 *onSeparator = false;
203 return idx;
204 }
205 }
206
207 return COL_NONE;
208 }
209
210 // ----------------------------------------------------------------------------
211 // wxHeaderCtrl repainting
212 // ----------------------------------------------------------------------------
213
214 void wxHeaderCtrl::RefreshCol(unsigned int idx)
215 {
216 wxRect rect = GetClientRect();
217 rect.x += GetColStart(idx);
218 rect.width = GetColumn(idx).GetWidth();
219
220 RefreshRect(rect);
221 }
222
223 void wxHeaderCtrl::RefreshColIfNotNone(unsigned int idx)
224 {
225 if ( idx != COL_NONE )
226 RefreshCol(idx);
227 }
228
229 void wxHeaderCtrl::RefreshColsAfter(unsigned int idx)
230 {
231 wxRect rect = GetClientRect();
232 const int ofs = GetColStart(idx);
233 rect.x += ofs;
234 rect.width -= ofs;
235
236 RefreshRect(rect);
237 }
238
239 // ----------------------------------------------------------------------------
240 // wxHeaderCtrl dragging/resizing/reordering
241 // ----------------------------------------------------------------------------
242
243 bool wxHeaderCtrl::IsResizing() const
244 {
245 return m_colBeingResized != COL_NONE;
246 }
247
248 bool wxHeaderCtrl::IsReordering() const
249 {
250 return m_colBeingReordered != COL_NONE;
251 }
252
253 void wxHeaderCtrl::ClearMarkers()
254 {
255 wxClientDC dc(this);
256
257 wxDCOverlay dcover(m_overlay, &dc);
258 dcover.Clear();
259 }
260
261 void wxHeaderCtrl::UpdateResizingMarker(int xPhysical)
262 {
263 wxClientDC dc(this);
264
265 wxDCOverlay dcover(m_overlay, &dc);
266 dcover.Clear();
267
268 // unfortunately drawing the marker over the parent window doesn't work as
269 // it's usually covered by another window (the main control view) so just
270 // draw the marker over the header itself, even if it makes it not very
271 // useful
272 dc.SetPen(*wxLIGHT_GREY_PEN);
273 dc.DrawLine(xPhysical, 0, xPhysical, GetClientSize().y);
274 }
275
276 void wxHeaderCtrl::EndDragging()
277 {
278 ClearMarkers();
279
280 m_overlay.Reset();
281
282 // don't use the special dragging cursor any more
283 SetCursor(wxNullCursor);
284 }
285
286 void wxHeaderCtrl::CancelDragging()
287 {
288 wxASSERT_MSG( IsDragging(),
289 "shouldn't be called if we're not dragging anything" );
290
291 EndDragging();
292
293 unsigned int& col = IsResizing() ? m_colBeingResized : m_colBeingReordered;
294
295 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED, GetId());
296 event.SetEventObject(this);
297 event.SetColumn(col);
298
299 GetEventHandler()->ProcessEvent(event);
300
301 col = COL_NONE;
302 }
303
304 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col, int& xPhysical)
305 {
306 const int xStart = GetColStart(col);
307
308 // notice that GetMinWidth() returns 0 if there is no minimal width so it
309 // still makes sense to use it even in this case
310 const int xMinEnd = xStart + GetColumn(col).GetMinWidth();
311
312 if ( xPhysical < xMinEnd )
313 xPhysical = xMinEnd;
314
315 return xPhysical - xStart;
316 }
317
318 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col, int xPhysical)
319 {
320 wxHeaderCtrlEvent event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
321 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE,
322 GetId());
323 event.SetEventObject(this);
324 event.SetColumn(col);
325
326 event.SetWidth(ConstrainByMinWidth(col, xPhysical));
327
328 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
329 {
330 if ( IsResizing() )
331 {
332 ReleaseMouse();
333 CancelDragging();
334 }
335 //else: nothing to do -- we just don't start to resize
336 }
337 else // go ahead with resizing
338 {
339 if ( !IsResizing() )
340 {
341 m_colBeingResized = col;
342 SetCursor(wxCursor(wxCURSOR_SIZEWE));
343 CaptureMouse();
344 }
345 //else: we had already done the above when we started
346
347 UpdateResizingMarker(xPhysical);
348 }
349 }
350
351 void wxHeaderCtrl::EndResizing(int xPhysical)
352 {
353 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
354
355 EndDragging();
356
357 ReleaseMouse();
358
359 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_RESIZE, GetId());
360 event.SetEventObject(this);
361 event.SetColumn(m_colBeingResized);
362 event.SetWidth(ConstrainByMinWidth(m_colBeingResized, xPhysical));
363
364 GetEventHandler()->ProcessEvent(event);
365
366 m_colBeingResized = COL_NONE;
367 }
368
369 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical)
370 {
371 wxClientDC dc(this);
372
373 wxDCOverlay dcover(m_overlay, &dc);
374 dcover.Clear();
375
376 dc.SetPen(*wxBLUE);
377 dc.SetBrush(*wxTRANSPARENT_BRUSH);
378
379 // draw the phantom position of the column being dragged
380 int x = xPhysical - m_dragOffset;
381 int y = GetClientSize().y;
382 dc.DrawRectangle(x, 0,
383 GetColumn(m_colBeingReordered).GetWidth(), y);
384
385 // and also a hint indicating where it is going to be inserted if it's
386 // dropped now
387 unsigned int col = FindColumnAtPoint(xPhysical);
388 if ( col != COL_NONE )
389 {
390 static const int DROP_MARKER_WIDTH = 4;
391
392 dc.SetBrush(*wxBLUE);
393 dc.DrawRectangle(GetColEnd(col) - DROP_MARKER_WIDTH/2, 0,
394 DROP_MARKER_WIDTH, y);
395 }
396 }
397
398 void wxHeaderCtrl::StartReordering(unsigned int col, int xPhysical)
399 {
400 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_BEGIN_REORDER, GetId());
401 event.SetEventObject(this);
402 event.SetColumn(col);
403
404 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
405 {
406 // don't start dragging it, nothing to do otherwise
407 return;
408 }
409
410 m_dragOffset = xPhysical - GetColStart(col);
411
412 m_colBeingReordered = col;
413 SetCursor(wxCursor(wxCURSOR_HAND));
414 CaptureMouse();
415
416 UpdateReorderingMarker(xPhysical);
417 }
418
419 void wxHeaderCtrl::EndReordering(int xPhysical)
420 {
421 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
422
423 EndDragging();
424
425 ReleaseMouse();
426
427 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_REORDER, GetId());
428 event.SetEventObject(this);
429 event.SetColumn(m_colBeingReordered);
430
431 const unsigned pos = GetColumnPos(FindColumnAtPoint(xPhysical));
432 event.SetNewOrder(pos);
433
434 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
435 {
436 // do reorder the columns
437 DoMoveCol(m_colBeingReordered, pos);
438 }
439
440 m_colBeingReordered = COL_NONE;
441 }
442
443 // ----------------------------------------------------------------------------
444 // wxHeaderCtrl column reordering
445 // ----------------------------------------------------------------------------
446
447 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt& order)
448 {
449 m_colIndices = order;
450 Refresh();
451 }
452
453 wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const
454 {
455 return m_colIndices;
456 }
457
458 void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos)
459 {
460 MoveColumnInOrderArray(m_colIndices, idx, pos);
461
462 Refresh();
463 }
464
465 // ----------------------------------------------------------------------------
466 // wxHeaderCtrl event handlers
467 // ----------------------------------------------------------------------------
468
469 BEGIN_EVENT_TABLE(wxHeaderCtrl, wxHeaderCtrlBase)
470 EVT_PAINT(wxHeaderCtrl::OnPaint)
471
472 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse)
473
474 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost)
475
476 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown)
477 END_EVENT_TABLE()
478
479 void wxHeaderCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
480 {
481 int w, h;
482 GetClientSize(&w, &h);
483
484 wxAutoBufferedPaintDC dc(this);
485
486 dc.SetBackground(GetBackgroundColour());
487 dc.Clear();
488
489 // account for the horizontal scrollbar offset in the parent window
490 dc.SetDeviceOrigin(m_scrollOffset, 0);
491
492 const unsigned int count = m_numColumns;
493 int xpos = 0;
494 for ( unsigned int i = 0; i < count; i++ )
495 {
496 const unsigned idx = m_colIndices[i];
497 const wxHeaderColumn& col = GetColumn(idx);
498 if ( col.IsHidden() )
499 continue;
500
501 const int colWidth = col.GetWidth();
502
503 wxHeaderSortIconType sortArrow;
504 if ( col.IsSortKey() )
505 {
506 sortArrow = col.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
507 : wxHDR_SORT_ICON_DOWN;
508 }
509 else // not sorting by this column
510 {
511 sortArrow = wxHDR_SORT_ICON_NONE;
512 }
513
514 int state = 0;
515 if ( IsEnabled() )
516 {
517 if ( idx == m_hover )
518 state = wxCONTROL_CURRENT;
519 }
520 else // disabled
521 {
522 state = wxCONTROL_DISABLED;
523 }
524
525 wxHeaderButtonParams params;
526 params.m_labelText = col.GetTitle();
527 params.m_labelBitmap = col.GetBitmap();
528 params.m_labelAlignment = col.GetAlignment();
529
530 wxRendererNative::Get().DrawHeaderButton
531 (
532 this,
533 dc,
534 wxRect(xpos, 0, colWidth, h),
535 state,
536 sortArrow,
537 &params
538 );
539
540 xpos += colWidth;
541 }
542 }
543
544 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
545 {
546 if ( IsDragging() )
547 CancelDragging();
548 }
549
550 void wxHeaderCtrl::OnKeyDown(wxKeyEvent& event)
551 {
552 if ( event.GetKeyCode() == WXK_ESCAPE )
553 {
554 if ( IsDragging() )
555 {
556 ReleaseMouse();
557 CancelDragging();
558
559 return;
560 }
561 }
562
563 event.Skip();
564 }
565
566 void wxHeaderCtrl::OnMouse(wxMouseEvent& mevent)
567 {
568 // do this in advance to allow simply returning if we're not interested,
569 // we'll undo it if we do handle the event below
570 mevent.Skip();
571
572
573 // account for the control displacement
574 const int xPhysical = mevent.GetX();
575 const int xLogical = xPhysical - m_scrollOffset;
576
577 // first deal with the [continuation of any] dragging operations in
578 // progress
579 if ( IsResizing() )
580 {
581 if ( mevent.LeftUp() )
582 EndResizing(xPhysical);
583 else // update the live separator position
584 StartOrContinueResizing(m_colBeingResized, xPhysical);
585
586 return;
587 }
588
589 if ( IsReordering() )
590 {
591 if ( mevent.LeftUp() )
592 EndReordering(xPhysical);
593 else // update the column position
594 UpdateReorderingMarker(xPhysical);
595
596 return;
597 }
598
599
600 // find if the event is over a column at all
601 bool onSeparator;
602 const unsigned col = mevent.Leaving()
603 ? (onSeparator = false, COL_NONE)
604 : FindColumnAtPoint(xLogical, &onSeparator);
605
606
607 // update the highlighted column if it changed
608 if ( col != m_hover )
609 {
610 const unsigned hoverOld = m_hover;
611 m_hover = col;
612
613 RefreshColIfNotNone(hoverOld);
614 RefreshColIfNotNone(m_hover);
615 }
616
617 // update mouse cursor as it moves around
618 if ( mevent.Moving() )
619 {
620 SetCursor(onSeparator ? wxCursor(wxCURSOR_SIZEWE) : wxNullCursor);
621 return;
622 }
623
624 // all the other events only make sense when they happen over a column
625 if ( col == COL_NONE )
626 return;
627
628
629 // enter various dragging modes on left mouse press
630 if ( mevent.LeftDown() )
631 {
632 if ( onSeparator )
633 {
634 // start resizing the column
635 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
636 StartOrContinueResizing(col, xPhysical);
637 }
638 else // on column itself
639 {
640 // start dragging the column
641 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
642
643 StartReordering(col, xPhysical);
644 }
645
646 return;
647 }
648
649 // determine the type of header event corresponding to click events
650 wxEventType evtType = wxEVT_NULL;
651 const bool click = mevent.ButtonUp(),
652 dblclk = mevent.ButtonDClick();
653 if ( click || dblclk )
654 {
655 switch ( mevent.GetButton() )
656 {
657 case wxMOUSE_BTN_LEFT:
658 // treat left double clicks on separator specially
659 if ( onSeparator && dblclk )
660 {
661 evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
662 }
663 else // not double click on separator
664 {
665 evtType = click ? wxEVT_COMMAND_HEADER_CLICK
666 : wxEVT_COMMAND_HEADER_DCLICK;
667 }
668 break;
669
670 case wxMOUSE_BTN_RIGHT:
671 evtType = click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK
672 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK;
673 break;
674
675 case wxMOUSE_BTN_MIDDLE:
676 evtType = click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
677 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK;
678 break;
679
680 default:
681 // ignore clicks from other mouse buttons
682 ;
683 }
684 }
685
686 if ( evtType == wxEVT_NULL )
687 return;
688
689 wxHeaderCtrlEvent event(evtType, GetId());
690 event.SetEventObject(this);
691 event.SetColumn(col);
692
693 if ( GetEventHandler()->ProcessEvent(event) )
694 mevent.Skip(false);
695 }
696
697 #endif // wxHAS_GENERIC_HEADERCTRL