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