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