]> git.saurik.com Git - wxWidgets.git/blob - src/generic/headerctrlg.cpp
destroy the abort dialog when done with it
[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 // 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 (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 resizeable, 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::UpdateResizingMarker(int xPhysical)
259 {
260 wxClientDC dc(this);
261
262 wxDCOverlay dcover(m_overlay, &dc);
263 dcover.Clear();
264
265 // unfortunately drawing the marker over the parent window doesn't work as
266 // it's usually covered by another window (the main control view) so just
267 // draw the marker over the header itself, even if it makes it not very
268 // useful
269 dc.SetPen(*wxLIGHT_GREY_PEN);
270 dc.DrawLine(xPhysical, 0, xPhysical, GetClientSize().y);
271 }
272
273 void wxHeaderCtrl::EndDragging()
274 {
275 ClearMarkers();
276
277 m_overlay.Reset();
278
279 // don't use the special dragging cursor any more
280 SetCursor(wxNullCursor);
281 }
282
283 void wxHeaderCtrl::CancelDragging()
284 {
285 wxASSERT_MSG( IsDragging(),
286 "shouldn't be called if we're not dragging anything" );
287
288 EndDragging();
289
290 unsigned int& col = IsResizing() ? m_colBeingResized : m_colBeingReordered;
291
292 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_DRAGGING_CANCELLED, GetId());
293 event.SetEventObject(this);
294 event.SetColumn(col);
295
296 GetEventHandler()->ProcessEvent(event);
297
298 col = COL_NONE;
299 }
300
301 int wxHeaderCtrl::ConstrainByMinWidth(unsigned int col, int& xPhysical)
302 {
303 const int xStart = GetColStart(col);
304
305 // notice that GetMinWidth() returns 0 if there is no minimal width so it
306 // still makes sense to use it even in this case
307 const int xMinEnd = xStart + GetColumn(col).GetMinWidth();
308
309 if ( xPhysical < xMinEnd )
310 xPhysical = xMinEnd;
311
312 return xPhysical - xStart;
313 }
314
315 void wxHeaderCtrl::StartOrContinueResizing(unsigned int col, int xPhysical)
316 {
317 wxHeaderCtrlEvent event(IsResizing() ? wxEVT_COMMAND_HEADER_RESIZING
318 : wxEVT_COMMAND_HEADER_BEGIN_RESIZE,
319 GetId());
320 event.SetEventObject(this);
321 event.SetColumn(col);
322
323 event.SetWidth(ConstrainByMinWidth(col, xPhysical));
324
325 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
326 {
327 if ( IsResizing() )
328 {
329 ReleaseMouse();
330 CancelDragging();
331 }
332 //else: nothing to do -- we just don't start to resize
333 }
334 else // go ahead with resizing
335 {
336 if ( !IsResizing() )
337 {
338 m_colBeingResized = col;
339 SetCursor(wxCursor(wxCURSOR_SIZEWE));
340 CaptureMouse();
341 }
342 //else: we had already done the above when we started
343
344 UpdateResizingMarker(xPhysical);
345 }
346 }
347
348 void wxHeaderCtrl::EndResizing(int xPhysical)
349 {
350 wxASSERT_MSG( IsResizing(), "shouldn't be called if we're not resizing" );
351
352 EndDragging();
353
354 ReleaseMouse();
355
356 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_RESIZE, GetId());
357 event.SetEventObject(this);
358 event.SetColumn(m_colBeingResized);
359 event.SetWidth(ConstrainByMinWidth(m_colBeingResized, xPhysical));
360
361 GetEventHandler()->ProcessEvent(event);
362
363 m_colBeingResized = COL_NONE;
364 }
365
366 void wxHeaderCtrl::UpdateReorderingMarker(int xPhysical)
367 {
368 wxClientDC dc(this);
369
370 wxDCOverlay dcover(m_overlay, &dc);
371 dcover.Clear();
372
373 dc.SetPen(*wxBLUE);
374 dc.SetBrush(*wxTRANSPARENT_BRUSH);
375
376 // draw the phantom position of the column being dragged
377 int x = xPhysical - m_dragOffset;
378 int y = GetClientSize().y;
379 dc.DrawRectangle(x, 0,
380 GetColumn(m_colBeingReordered).GetWidth(), y);
381
382 // and also a hint indicating where it is going to be inserted if it's
383 // dropped now
384 unsigned int col = FindColumnAtPoint(xPhysical);
385 if ( col != COL_NONE )
386 {
387 static const int DROP_MARKER_WIDTH = 4;
388
389 dc.SetBrush(*wxBLUE);
390 dc.DrawRectangle(GetColEnd(col) - DROP_MARKER_WIDTH/2, 0,
391 DROP_MARKER_WIDTH, y);
392 }
393 }
394
395 void wxHeaderCtrl::StartReordering(unsigned int col, int xPhysical)
396 {
397 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_BEGIN_REORDER, GetId());
398 event.SetEventObject(this);
399 event.SetColumn(col);
400
401 if ( GetEventHandler()->ProcessEvent(event) && !event.IsAllowed() )
402 {
403 // don't start dragging it, nothing to do otherwise
404 return;
405 }
406
407 m_dragOffset = xPhysical - GetColStart(col);
408
409 m_colBeingReordered = col;
410 SetCursor(wxCursor(wxCURSOR_HAND));
411 CaptureMouse();
412
413 // do not call UpdateReorderingMarker() here: we don't want to give
414 // feedback for reordering until the user starts to really move the mouse
415 // as he might want to just click on the column and not move it at all
416 }
417
418 bool wxHeaderCtrl::EndReordering(int xPhysical)
419 {
420 wxASSERT_MSG( IsReordering(), "shouldn't be called if we're not reordering" );
421
422 EndDragging();
423
424 ReleaseMouse();
425
426 const int colOld = m_colBeingReordered,
427 colNew = FindColumnAtPoint(xPhysical);
428
429 m_colBeingReordered = COL_NONE;
430
431 if ( xPhysical - GetColStart(colOld) == m_dragOffset )
432 return false;
433
434 if ( colNew != colOld )
435 {
436 wxHeaderCtrlEvent event(wxEVT_COMMAND_HEADER_END_REORDER, GetId());
437 event.SetEventObject(this);
438 event.SetColumn(colOld);
439
440 const unsigned pos = GetColumnPos(FindColumnAtPoint(xPhysical));
441 event.SetNewOrder(pos);
442
443 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
444 {
445 // do reorder the columns
446 DoMoveCol(colOld, pos);
447 }
448 }
449
450 // whether we moved the column or not, the user did move the mouse and so
451 // did try to do it so return true
452 return true;
453 }
454
455 // ----------------------------------------------------------------------------
456 // wxHeaderCtrl column reordering
457 // ----------------------------------------------------------------------------
458
459 void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt& order)
460 {
461 m_colIndices = order;
462 Refresh();
463 }
464
465 wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const
466 {
467 return m_colIndices;
468 }
469
470 void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos)
471 {
472 MoveColumnInOrderArray(m_colIndices, idx, pos);
473
474 Refresh();
475 }
476
477 // ----------------------------------------------------------------------------
478 // wxHeaderCtrl event handlers
479 // ----------------------------------------------------------------------------
480
481 BEGIN_EVENT_TABLE(wxHeaderCtrl, wxHeaderCtrlBase)
482 EVT_PAINT(wxHeaderCtrl::OnPaint)
483
484 EVT_MOUSE_EVENTS(wxHeaderCtrl::OnMouse)
485
486 EVT_MOUSE_CAPTURE_LOST(wxHeaderCtrl::OnCaptureLost)
487
488 EVT_KEY_DOWN(wxHeaderCtrl::OnKeyDown)
489 END_EVENT_TABLE()
490
491 void wxHeaderCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
492 {
493 int w, h;
494 GetClientSize(&w, &h);
495
496 wxAutoBufferedPaintDC dc(this);
497
498 dc.SetBackground(GetBackgroundColour());
499 dc.Clear();
500
501 // account for the horizontal scrollbar offset in the parent window
502 dc.SetDeviceOrigin(m_scrollOffset, 0);
503
504 const unsigned int count = m_numColumns;
505 int xpos = 0;
506 for ( unsigned int i = 0; i < count; i++ )
507 {
508 const unsigned idx = m_colIndices[i];
509 const wxHeaderColumn& col = GetColumn(idx);
510 if ( col.IsHidden() )
511 continue;
512
513 const int colWidth = col.GetWidth();
514
515 wxHeaderSortIconType sortArrow;
516 if ( col.IsSortKey() )
517 {
518 sortArrow = col.IsSortOrderAscending() ? wxHDR_SORT_ICON_UP
519 : wxHDR_SORT_ICON_DOWN;
520 }
521 else // not sorting by this column
522 {
523 sortArrow = wxHDR_SORT_ICON_NONE;
524 }
525
526 int state = 0;
527 if ( IsEnabled() )
528 {
529 if ( idx == m_hover )
530 state = wxCONTROL_CURRENT;
531 }
532 else // disabled
533 {
534 state = wxCONTROL_DISABLED;
535 }
536
537 wxHeaderButtonParams params;
538 params.m_labelText = col.GetTitle();
539 params.m_labelBitmap = col.GetBitmap();
540 params.m_labelAlignment = col.GetAlignment();
541
542 wxRendererNative::Get().DrawHeaderButton
543 (
544 this,
545 dc,
546 wxRect(xpos, 0, colWidth, h),
547 state,
548 sortArrow,
549 &params
550 );
551
552 xpos += colWidth;
553 }
554 }
555
556 void wxHeaderCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
557 {
558 if ( IsDragging() )
559 CancelDragging();
560 }
561
562 void wxHeaderCtrl::OnKeyDown(wxKeyEvent& event)
563 {
564 if ( event.GetKeyCode() == WXK_ESCAPE )
565 {
566 if ( IsDragging() )
567 {
568 ReleaseMouse();
569 CancelDragging();
570
571 return;
572 }
573 }
574
575 event.Skip();
576 }
577
578 void wxHeaderCtrl::OnMouse(wxMouseEvent& mevent)
579 {
580 // do this in advance to allow simply returning if we're not interested,
581 // we'll undo it if we do handle the event below
582 mevent.Skip();
583
584
585 // account for the control displacement
586 const int xPhysical = mevent.GetX();
587 const int xLogical = xPhysical - m_scrollOffset;
588
589 // first deal with the [continuation of any] dragging operations in
590 // progress
591 if ( IsResizing() )
592 {
593 if ( mevent.LeftUp() )
594 EndResizing(xPhysical);
595 else // update the live separator position
596 StartOrContinueResizing(m_colBeingResized, xPhysical);
597
598 return;
599 }
600
601 if ( IsReordering() )
602 {
603 if ( !mevent.LeftUp() )
604 {
605 // update the column position
606 UpdateReorderingMarker(xPhysical);
607
608 return;
609 }
610
611 // finish reordering and continue to generate a click event below if we
612 // didn't really reorder anything
613 if ( EndReordering(xPhysical) )
614 return;
615 }
616
617
618 // find if the event is over a column at all
619 bool onSeparator;
620 const unsigned col = mevent.Leaving()
621 ? (onSeparator = false, COL_NONE)
622 : FindColumnAtPoint(xLogical, &onSeparator);
623
624
625 // update the highlighted column if it changed
626 if ( col != m_hover )
627 {
628 const unsigned hoverOld = m_hover;
629 m_hover = col;
630
631 RefreshColIfNotNone(hoverOld);
632 RefreshColIfNotNone(m_hover);
633 }
634
635 // update mouse cursor as it moves around
636 if ( mevent.Moving() )
637 {
638 SetCursor(onSeparator ? wxCursor(wxCURSOR_SIZEWE) : wxNullCursor);
639 return;
640 }
641
642 // all the other events only make sense when they happen over a column
643 if ( col == COL_NONE )
644 return;
645
646
647 // enter various dragging modes on left mouse press
648 if ( mevent.LeftDown() )
649 {
650 if ( onSeparator )
651 {
652 // start resizing the column
653 wxASSERT_MSG( !IsResizing(), "reentering column resize mode?" );
654 StartOrContinueResizing(col, xPhysical);
655 }
656 else // on column itself
657 {
658 // start dragging the column
659 wxASSERT_MSG( !IsReordering(), "reentering column move mode?" );
660
661 StartReordering(col, xPhysical);
662 }
663
664 return;
665 }
666
667 // determine the type of header event corresponding to click events
668 wxEventType evtType = wxEVT_NULL;
669 const bool click = mevent.ButtonUp(),
670 dblclk = mevent.ButtonDClick();
671 if ( click || dblclk )
672 {
673 switch ( mevent.GetButton() )
674 {
675 case wxMOUSE_BTN_LEFT:
676 // treat left double clicks on separator specially
677 if ( onSeparator && dblclk )
678 {
679 evtType = wxEVT_COMMAND_HEADER_SEPARATOR_DCLICK;
680 }
681 else // not double click on separator
682 {
683 evtType = click ? wxEVT_COMMAND_HEADER_CLICK
684 : wxEVT_COMMAND_HEADER_DCLICK;
685 }
686 break;
687
688 case wxMOUSE_BTN_RIGHT:
689 evtType = click ? wxEVT_COMMAND_HEADER_RIGHT_CLICK
690 : wxEVT_COMMAND_HEADER_RIGHT_DCLICK;
691 break;
692
693 case wxMOUSE_BTN_MIDDLE:
694 evtType = click ? wxEVT_COMMAND_HEADER_MIDDLE_CLICK
695 : wxEVT_COMMAND_HEADER_MIDDLE_DCLICK;
696 break;
697
698 default:
699 // ignore clicks from other mouse buttons
700 ;
701 }
702 }
703
704 if ( evtType == wxEVT_NULL )
705 return;
706
707 wxHeaderCtrlEvent event(evtType, GetId());
708 event.SetEventObject(this);
709 event.SetColumn(col);
710
711 if ( GetEventHandler()->ProcessEvent(event) )
712 mevent.Skip(false);
713 }
714
715 #endif // wxHAS_GENERIC_HEADERCTRL
716
717 #endif // wxUSE_HEADERCTRL