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