]> git.saurik.com Git - wxWidgets.git/blob - src/generic/headerctrlg.cpp
Forgot header with OSX prefix
[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 #if wxUSE_HEADERCTRL
27
28 #include "wx/headerctrl.h"
29
30 #ifdef wxHAS_GENERIC_HEADERCTRL
31
32 #include "wx/dcbuffer.h"
33 #include "wx/renderer.h"
34
35 // ----------------------------------------------------------------------------
36 // constants
37 // ----------------------------------------------------------------------------
38
39 namespace
40 {
41
42 const unsigned NO_SORT = (unsigned)-1;
43
44 const unsigned COL_NONE = (unsigned)-1;
45
46 } // anonymous namespace
47
48 // ============================================================================
49 // wxHeaderCtrl implementation
50 // ============================================================================
51
52 // ----------------------------------------------------------------------------
53 // wxHeaderCtrl creation
54 // ----------------------------------------------------------------------------
55
56 void wxHeaderCtrl::Init()
57 {
58 m_numColumns = 0;
59 m_hover =
60 m_colBeingResized =
61 m_colBeingReordered = COL_NONE;
62 m_dragOffset = 0;
63 m_scrollOffset = 0;
64 }
65
66 bool wxHeaderCtrl::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
72 {
73 if ( !wxHeaderCtrlBase::Create(parent, id, pos, size,
74 style, wxDefaultValidator, name) )
75 return false;
76
77 // tell the system to not paint the background at all to avoid flicker as
78 // we paint the entire window area in our OnPaint()
79 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
80
81 return true;
82 }
83
84 wxHeaderCtrl::~wxHeaderCtrl()
85 {
86 }
87
88 // ----------------------------------------------------------------------------
89 // wxHeaderCtrl columns manipulation
90 // ----------------------------------------------------------------------------
91
92 void wxHeaderCtrl::DoSetCount(unsigned int count)
93 {
94 // update the column indices order array before changing m_numColumns
95 DoResizeColumnIndices(m_colIndices, count);
96
97 m_numColumns = count;
98
99 InvalidateBestSize();
100 Refresh();
101 }
102
103 unsigned int wxHeaderCtrl::DoGetCount() const
104 {
105 return m_numColumns;
106 }
107
108 void wxHeaderCtrl::DoUpdate(unsigned int idx)
109 {
110 InvalidateBestSize();
111
112 // we need to refresh not only this column but also the ones after it in
113 // case it was shown or hidden or its width changed -- it would be nice to
114 // avoid doing this unnecessary by storing the old column width (TODO)
115 RefreshColsAfter(idx);
116 }
117
118 // ----------------------------------------------------------------------------
119 // wxHeaderCtrl scrolling
120 // ----------------------------------------------------------------------------
121
122 void wxHeaderCtrl::DoScrollHorz(int dx)
123 {
124 m_scrollOffset += dx;
125
126 // don't call our own version which calls this function!
127 wxControl::ScrollWindow(dx, 0);
128 }
129
130 // ----------------------------------------------------------------------------
131 // wxHeaderCtrl geometry
132 // ----------------------------------------------------------------------------
133
134 wxSize wxHeaderCtrl::DoGetBestSize() const
135 {
136 wxWindow *win = GetParent();
137 int height = wxRendererNative::Get().GetHeaderButtonHeight( win );
138
139 // the vertical size is rather arbitrary but it looks better if we leave
140 // some space around the text
141 const wxSize size(IsEmpty() ? wxHeaderCtrlBase::DoGetBestSize().x
142 : GetColEnd(GetColumnCount() - 1),
143 height ); // (7*GetCharHeight())/4);
144 CacheBestSize(size);
145 return size;
146 }
147
148 int wxHeaderCtrl::GetColStart(unsigned int idx) const
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 = 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 + GetColumn(idx).GetWidth();
170 }
171
172 unsigned int wxHeaderCtrl::FindColumnAtPoint(int x, bool *onSeparator) const
173 {
174 int pos = 0;
175 const unsigned count = GetColumnCount();
176 for ( unsigned n = 0; n < count; n++ )
177 {
178 const unsigned idx = m_colIndices[n];
179 const wxHeaderColumn& col = GetColumn(idx);
180 if ( col.IsHidden() )
181 continue;
182
183 pos += col.GetWidth();
184
185 // if the column is resizeable, check if we're approximatively over the
186 // line separating it from the next column
187 //
188 // TODO: don't hardcode sensitivity
189 if ( col.IsResizeable() && abs(x - pos) < 8 )
190 {
191 if ( onSeparator )
192 *onSeparator = true;
193 return idx;
194 }
195
196 // inside this column?
197 if ( x < pos )
198 {
199 if ( onSeparator )
200 *onSeparator = false;
201 return idx;
202 }
203 }
204
205 if ( onSeparator )
206 *onSeparator = false;
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 // do not call UpdateReorderingMarker() here: we don't want to give
417 // feedback for reordering until the user starts to really move the mouse
418 // as he might want to just click on the column and not move it at all
419 }
420
421 bool wxHeaderCtrl::EndReordering(int xPhysical)
422 {
423 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
424
425 EndDragging();
426
427 ReleaseMouse();
428
429 const int colOld = m_colBeingReordered,
430 colNew = FindColumnAtPoint(xPhysical);
431
432 m_colBeingReordered = COL_NONE;
433
434 if ( xPhysical - GetColStart(colOld) == m_dragOffset )
435 return false;
436
437 if ( colNew != colOld )
438 {
439 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_REORDER, GetId());
440 event.SetEventObject(this);
441 event.SetColumn(colOld);
442
443 const unsigned pos = GetColumnPos(FindColumnAtPoint(xPhysical));
444 event.SetNewOrder(pos);
445
446 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
447 {
448 // do reorder the columns
449 DoMoveCol(colOld, pos);
450 }
451 }
452
453 // whether we moved the column or not, the user did move the mouse and so
454 // did try to do it so return true
455 return true;
456 }
457
458 // ----------------------------------------------------------------------------
459 // wxHeaderCtrl column reordering
460 // ----------------------------------------------------------------------------
461
462 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt& order)
463 {
464 m_colIndices = order;
465 Refresh();
466 }
467
468 wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const
469 {
470 return m_colIndices;
471 }
472
473 void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos)
474 {
475 MoveColumnInOrderArray(m_colIndices, idx, pos);
476
477 Refresh();
478 }
479
480 // ----------------------------------------------------------------------------
481 // wxHeaderCtrl event handlers
482 // ----------------------------------------------------------------------------
483
484 BEGIN_EVENT_TABLE(wxHeaderCtrl, wxHeaderCtrlBase)
485 EVT_PAINT(wxHeaderCtrl::OnPaint)
486
487 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse)
488
489 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost)
490
491 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown)
492 END_EVENT_TABLE()
493
494 void wxHeaderCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
495 {
496 int w, h;
497 GetClientSize(&w, &h);
498
499 #ifdef __WXGTK__
500 // int vw;
501 // GetVirtualSize(&vw, NULL);
502 #endif
503
504 wxAutoBufferedPaintDC dc(this);
505
506 dc.SetBackground(GetBackgroundColour());
507 dc.Clear();
508
509 // account for the horizontal scrollbar offset in the parent window
510 dc.SetDeviceOrigin(m_scrollOffset, 0);
511
512 const unsigned int count = m_numColumns;
513 int xpos = 0;
514 for ( unsigned int i = 0; i < count; i++ )
515 {
516 const unsigned idx = m_colIndices[i];
517 const wxHeaderColumn& col = GetColumn(idx);
518 if ( col.IsHidden() )
519 continue;
520
521 int colWidth = col.GetWidth();
522
523 wxHeaderSortIconType sortArrow;
524 if ( col.IsSortKey() )
525 {
526 sortArrow = col.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
527 : wxHDR_SORT_ICON_DOWN;
528 }
529 else // not sorting by this column
530 {
531 sortArrow = wxHDR_SORT_ICON_NONE;
532 }
533
534 int state = 0;
535 if ( IsEnabled() )
536 {
537 if ( idx == m_hover )
538 state = wxCONTROL_CURRENT;
539 }
540 else // disabled
541 {
542 state = wxCONTROL_DISABLED;
543 }
544
545 if (i == 0)
546 state |= wxCONTROL_SPECIAL;
547
548 wxHeaderButtonParams params;
549 params.m_labelText = col.GetTitle();
550 params.m_labelBitmap = col.GetBitmap();
551 params.m_labelAlignment = col.GetAlignment();
552
553 #ifdef __WXGTK__
554 if (i == count-1)
555 {
556 // colWidth = wxMax( colWidth, vw - xpos );
557 state |= wxCONTROL_DIRTY;
558 }
559 #endif
560
561 wxRendererNative::Get().DrawHeaderButton
562 (
563 this,
564 dc,
565 wxRect(xpos, 0, colWidth, h),
566 state,
567 sortArrow,
568 &params
569 );
570
571 xpos += colWidth;
572 }
573 }
574
575 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
576 {
577 if ( IsDragging() )
578 CancelDragging();
579 }
580
581 void wxHeaderCtrl::OnKeyDown(wxKeyEvent& event)
582 {
583 if ( event.GetKeyCode() == WXK_ESCAPE )
584 {
585 if ( IsDragging() )
586 {
587 ReleaseMouse();
588 CancelDragging();
589
590 return;
591 }
592 }
593
594 event.Skip();
595 }
596
597 void wxHeaderCtrl::OnMouse(wxMouseEvent& mevent)
598 {
599 // do this in advance to allow simply returning if we're not interested,
600 // we'll undo it if we do handle the event below
601 mevent.Skip();
602
603
604 // account for the control displacement
605 const int xPhysical = mevent.GetX();
606 const int xLogical = xPhysical - m_scrollOffset;
607
608 // first deal with the [continuation of any] dragging operations in
609 // progress
610 if ( IsResizing() )
611 {
612 if ( mevent.LeftUp() )
613 EndResizing(xPhysical);
614 else // update the live separator position
615 StartOrContinueResizing(m_colBeingResized, xPhysical);
616
617 return;
618 }
619
620 if ( IsReordering() )
621 {
622 if ( !mevent.LeftUp() )
623 {
624 // update the column position
625 UpdateReorderingMarker(xPhysical);
626
627 return;
628 }
629
630 // finish reordering and continue to generate a click event below if we
631 // didn't really reorder anything
632 if ( EndReordering(xPhysical) )
633 return;
634 }
635
636
637 // find if the event is over a column at all
638 bool onSeparator;
639 const unsigned col = mevent.Leaving()
640 ? (onSeparator = false, COL_NONE)
641 : FindColumnAtPoint(xLogical, &onSeparator);
642
643
644 // update the highlighted column if it changed
645 if ( col != m_hover )
646 {
647 const unsigned hoverOld = m_hover;
648 m_hover = col;
649
650 RefreshColIfNotNone(hoverOld);
651 RefreshColIfNotNone(m_hover);
652 }
653
654 // update mouse cursor as it moves around
655 if ( mevent.Moving() )
656 {
657 SetCursor(onSeparator ? wxCursor(wxCURSOR_SIZEWE) : wxNullCursor);
658 return;
659 }
660
661 // all the other events only make sense when they happen over a column
662 if ( col == COL_NONE )
663 return;
664
665
666 // enter various dragging modes on left mouse press
667 if ( mevent.LeftDown() )
668 {
669 if ( onSeparator )
670 {
671 // start resizing the column
672 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
673 StartOrContinueResizing(col, xPhysical);
674 }
675 else // on column itself
676 {
677 // start dragging the column
678 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
679
680 StartReordering(col, xPhysical);
681 }
682
683 return;
684 }
685
686 // determine the type of header event corresponding to click events
687 wxEventType evtType = wxEVT_NULL;
688 const bool click = mevent.ButtonUp(),
689 dblclk = mevent.ButtonDClick();
690 if ( click || dblclk )
691 {
692 switch ( mevent.GetButton() )
693 {
694 case wxMOUSE_BTN_LEFT:
695 // treat left double clicks on separator specially
696 if ( onSeparator && dblclk )
697 {
698 evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
699 }
700 else // not double click on separator
701 {
702 evtType = click ? wxEVT_COMMAND_HEADER_CLICK
703 : wxEVT_COMMAND_HEADER_DCLICK;
704 }
705 break;
706
707 case wxMOUSE_BTN_RIGHT:
708 evtType = click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK
709 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK;
710 break;
711
712 case wxMOUSE_BTN_MIDDLE:
713 evtType = click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
714 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK;
715 break;
716
717 default:
718 // ignore clicks from other mouse buttons
719 ;
720 }
721 }
722
723 if ( evtType == wxEVT_NULL )
724 return;
725
726 wxHeaderCtrlEvent event(evtType, GetId());
727 event.SetEventObject(this);
728 event.SetColumn(col);
729
730 if ( GetEventHandler()->ProcessEvent(event) )
731 mevent.Skip(false);
732 }
733
734 #endif // wxHAS_GENERIC_HEADERCTRL
735
736 #endif // wxUSE_HEADERCTRL