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