1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/listctrl.h"
28 #if ((!defined(__WXMSW__) && !(defined(__WXMAC__) && wxOSX_USE_CARBON)) || defined(__WXUNIVERSAL__))
29 // if we have a native version, its implementation file does all this
30 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
32 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
34 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
38 #include "wx/scrolwin.h"
40 #include "wx/settings.h"
41 #include "wx/dynarray.h"
42 #include "wx/dcclient.h"
43 #include "wx/dcscreen.h"
45 #include "wx/settings.h"
49 #include "wx/imaglist.h"
50 #include "wx/renderer.h"
51 #include "wx/generic/private/listctrl.h"
54 #include "wx/osx/private.h"
55 // for themeing support
56 #include <Carbon/Carbon.h>
59 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
60 #define "wx/msw/wrapwin.h"
63 // NOTE: If using the wxListBox visual attributes works everywhere then this can
64 // be removed, as well as the #else case below.
65 #define _USE_VISATTR 0
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // // the height of the header window (FIXME: should depend on its font!)
73 // static const int HEADER_HEIGHT = 23;
75 static const int SCROLL_UNIT_X
= 15;
77 // the spacing between the lines (in report mode)
78 static const int LINE_SPACING
= 0;
80 // extra margins around the text label
82 static const int EXTRA_WIDTH
= 6;
84 static const int EXTRA_WIDTH
= 4;
88 static const int EXTRA_HEIGHT
= 6;
90 static const int EXTRA_HEIGHT
= 4;
93 // margin between the window and the items
94 static const int EXTRA_BORDER_X
= 2;
95 static const int EXTRA_BORDER_Y
= 2;
97 // offset for the header window
98 static const int HEADER_OFFSET_X
= 0;
99 static const int HEADER_OFFSET_Y
= 0;
101 // margin between rows of icons in [small] icon view
102 static const int MARGIN_BETWEEN_ROWS
= 6;
104 // when autosizing the columns, add some slack
105 static const int AUTOSIZE_COL_MARGIN
= 10;
107 // default width for the header columns
108 static const int WIDTH_COL_DEFAULT
= 80;
110 // the space between the image and the text in the report mode
111 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
113 // the space between the image and the text in the report mode in header
114 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
118 // ----------------------------------------------------------------------------
119 // arrays/list implementations
120 // ----------------------------------------------------------------------------
122 #include "wx/listimpl.cpp"
123 WX_DEFINE_LIST(wxListItemDataList
)
125 #include "wx/arrimpl.cpp"
126 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
128 #include "wx/listimpl.cpp"
129 WX_DEFINE_LIST(wxListHeaderDataList
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxListItemData::~wxListItemData()
138 // in the virtual list control the attributes are managed by the main
139 // program, so don't delete them
140 if ( !m_owner
->IsVirtual() )
146 void wxListItemData::Init()
154 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
160 if ( owner
->InReportView() )
166 void wxListItemData::SetItem( const wxListItem
&info
)
168 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
169 SetText(info
.m_text
);
170 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
171 m_image
= info
.m_image
;
172 if ( info
.m_mask
& wxLIST_MASK_DATA
)
173 m_data
= info
.m_data
;
175 if ( info
.HasAttributes() )
178 m_attr
->AssignFrom(*info
.GetAttributes());
180 m_attr
= new wxListItemAttr(*info
.GetAttributes());
188 m_rect
->width
= info
.m_width
;
192 void wxListItemData::SetPosition( int x
, int y
)
194 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
200 void wxListItemData::SetSize( int width
, int height
)
202 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
205 m_rect
->width
= width
;
207 m_rect
->height
= height
;
210 bool wxListItemData::IsHit( int x
, int y
) const
212 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
214 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
217 int wxListItemData::GetX() const
219 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
224 int wxListItemData::GetY() const
226 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
231 int wxListItemData::GetWidth() const
233 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
235 return m_rect
->width
;
238 int wxListItemData::GetHeight() const
240 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
242 return m_rect
->height
;
245 void wxListItemData::GetItem( wxListItem
&info
) const
247 long mask
= info
.m_mask
;
249 // by default, get everything for backwards compatibility
252 if ( mask
& wxLIST_MASK_TEXT
)
253 info
.m_text
= m_text
;
254 if ( mask
& wxLIST_MASK_IMAGE
)
255 info
.m_image
= m_image
;
256 if ( mask
& wxLIST_MASK_DATA
)
257 info
.m_data
= m_data
;
261 if ( m_attr
->HasTextColour() )
262 info
.SetTextColour(m_attr
->GetTextColour());
263 if ( m_attr
->HasBackgroundColour() )
264 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
265 if ( m_attr
->HasFont() )
266 info
.SetFont(m_attr
->GetFont());
270 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
274 void wxListHeaderData::Init()
286 wxListHeaderData::wxListHeaderData()
291 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
298 void wxListHeaderData::SetItem( const wxListItem
&item
)
300 m_mask
= item
.m_mask
;
302 if ( m_mask
& wxLIST_MASK_TEXT
)
303 m_text
= item
.m_text
;
305 if ( m_mask
& wxLIST_MASK_IMAGE
)
306 m_image
= item
.m_image
;
308 if ( m_mask
& wxLIST_MASK_FORMAT
)
309 m_format
= item
.m_format
;
311 if ( m_mask
& wxLIST_MASK_WIDTH
)
312 SetWidth(item
.m_width
);
314 if ( m_mask
& wxLIST_MASK_STATE
)
315 SetState(item
.m_state
);
318 void wxListHeaderData::SetPosition( int x
, int y
)
324 void wxListHeaderData::SetHeight( int h
)
329 void wxListHeaderData::SetWidth( int w
)
331 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
334 void wxListHeaderData::SetState( int flag
)
339 void wxListHeaderData::SetFormat( int format
)
344 bool wxListHeaderData::HasImage() const
346 return m_image
!= -1;
349 bool wxListHeaderData::IsHit( int x
, int y
) const
351 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
354 void wxListHeaderData::GetItem( wxListItem
& item
)
356 item
.m_mask
= m_mask
;
357 item
.m_text
= m_text
;
358 item
.m_image
= m_image
;
359 item
.m_format
= m_format
;
360 item
.m_width
= m_width
;
361 item
.m_state
= m_state
;
364 int wxListHeaderData::GetImage() const
369 int wxListHeaderData::GetWidth() const
374 int wxListHeaderData::GetFormat() const
379 int wxListHeaderData::GetState() const
384 //-----------------------------------------------------------------------------
386 //-----------------------------------------------------------------------------
388 inline int wxListLineData::GetMode() const
390 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
393 inline bool wxListLineData::InReportView() const
395 return m_owner
->HasFlag(wxLC_REPORT
);
398 inline bool wxListLineData::IsVirtual() const
400 return m_owner
->IsVirtual();
403 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
407 if ( InReportView() )
410 m_gi
= new GeometryInfo
;
412 m_highlighted
= false;
414 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
417 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
419 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
420 wxCHECK_RET( node
, _T("no subitems at all??") );
422 wxListItemData
*item
= node
->GetData();
430 case wxLC_SMALL_ICON
:
431 m_gi
->m_rectAll
.width
= spacing
;
438 m_gi
->m_rectLabel
.width
=
439 m_gi
->m_rectLabel
.height
= 0;
443 dc
->GetTextExtent( s
, &lw
, &lh
);
447 m_gi
->m_rectAll
.height
= spacing
+ lh
;
449 m_gi
->m_rectAll
.width
= lw
;
451 m_gi
->m_rectLabel
.width
= lw
;
452 m_gi
->m_rectLabel
.height
= lh
;
455 if (item
->HasImage())
458 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
459 m_gi
->m_rectIcon
.width
= w
+ 8;
460 m_gi
->m_rectIcon
.height
= h
+ 8;
462 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
463 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
464 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
465 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
468 if ( item
->HasText() )
470 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
471 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
473 else // no text, highlight the icon
475 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
476 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
481 s
= item
->GetTextForMeasuring();
483 dc
->GetTextExtent( s
, &lw
, &lh
);
487 m_gi
->m_rectLabel
.width
= lw
;
488 m_gi
->m_rectLabel
.height
= lh
;
490 m_gi
->m_rectAll
.width
= lw
;
491 m_gi
->m_rectAll
.height
= lh
;
493 if (item
->HasImage())
496 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
497 m_gi
->m_rectIcon
.width
= w
;
498 m_gi
->m_rectIcon
.height
= h
;
500 m_gi
->m_rectAll
.width
+= 4 + w
;
501 if (h
> m_gi
->m_rectAll
.height
)
502 m_gi
->m_rectAll
.height
= h
;
505 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
506 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
510 wxFAIL_MSG( _T("unexpected call to SetSize") );
514 wxFAIL_MSG( _T("unknown mode") );
519 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
521 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
522 wxCHECK_RET( node
, _T("no subitems at all??") );
524 wxListItemData
*item
= node
->GetData();
529 case wxLC_SMALL_ICON
:
530 m_gi
->m_rectAll
.x
= x
;
531 m_gi
->m_rectAll
.y
= y
;
533 if ( item
->HasImage() )
535 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
536 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
537 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
540 if ( item
->HasText() )
542 if (m_gi
->m_rectAll
.width
> spacing
)
543 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
545 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
546 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
547 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
548 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
550 else // no text, highlight the icon
552 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
553 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
558 m_gi
->m_rectAll
.x
= x
;
559 m_gi
->m_rectAll
.y
= y
;
561 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
562 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
563 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
565 if (item
->HasImage())
567 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
568 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
569 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
573 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
578 wxFAIL_MSG( _T("unexpected call to SetPosition") );
582 wxFAIL_MSG( _T("unknown mode") );
587 void wxListLineData::InitItems( int num
)
589 for (int i
= 0; i
< num
; i
++)
590 m_items
.Append( new wxListItemData(m_owner
) );
593 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
595 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
596 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
598 wxListItemData
*item
= node
->GetData();
599 item
->SetItem( info
);
602 void wxListLineData::GetItem( int index
, wxListItem
&info
)
604 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
607 wxListItemData
*item
= node
->GetData();
608 item
->GetItem( info
);
612 wxString
wxListLineData::GetText(int index
) const
616 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
619 wxListItemData
*item
= node
->GetData();
626 void wxListLineData::SetText( int index
, const wxString
& s
)
628 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
631 wxListItemData
*item
= node
->GetData();
636 void wxListLineData::SetImage( int index
, int image
)
638 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
639 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
641 wxListItemData
*item
= node
->GetData();
642 item
->SetImage(image
);
645 int wxListLineData::GetImage( int index
) const
647 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
648 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
650 wxListItemData
*item
= node
->GetData();
651 return item
->GetImage();
654 wxListItemAttr
*wxListLineData::GetAttr() const
656 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
657 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
659 wxListItemData
*item
= node
->GetData();
660 return item
->GetAttr();
663 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
665 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
666 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
668 wxListItemData
*item
= node
->GetData();
672 bool wxListLineData::SetAttributes(wxDC
*dc
,
673 const wxListItemAttr
*attr
,
676 wxWindow
*listctrl
= m_owner
->GetParent();
680 // don't use foreground colour for drawing highlighted items - this might
681 // make them completely invisible (and there is no way to do bit
682 // arithmetics on wxColour, unfortunately)
687 if (m_owner
->HasFocus()
688 #if !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
689 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
697 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
699 else if ( attr
&& attr
->HasTextColour() )
700 colText
= attr
->GetTextColour();
702 colText
= listctrl
->GetForegroundColour();
704 dc
->SetTextForeground(colText
);
708 if ( attr
&& attr
->HasFont() )
709 font
= attr
->GetFont();
711 font
= listctrl
->GetFont();
716 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
717 if ( highlighted
|| hasBgCol
)
720 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
722 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
724 dc
->SetPen( *wxTRANSPARENT_PEN
);
732 void wxListLineData::Draw( wxDC
*dc
)
734 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
735 wxCHECK_RET( node
, _T("no subitems at all??") );
737 bool highlighted
= IsHighlighted();
739 wxListItemAttr
*attr
= GetAttr();
741 if ( SetAttributes(dc
, attr
, highlighted
) )
742 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
744 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
750 int flags
= wxCONTROL_SELECTED
;
751 if (m_owner
->HasFocus()
752 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
753 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
756 flags
|= wxCONTROL_FOCUSED
;
757 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
762 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
767 // just for debugging to better see where the items are
769 dc
->SetPen(*wxRED_PEN
);
770 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
771 dc
->DrawRectangle( m_gi
->m_rectAll
);
772 dc
->SetPen(*wxGREEN_PEN
);
773 dc
->DrawRectangle( m_gi
->m_rectIcon
);
776 wxListItemData
*item
= node
->GetData();
777 if (item
->HasImage())
779 // centre the image inside our rectangle, this looks nicer when items
780 // ae aligned in a row
781 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
783 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
788 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
790 wxDCClipper
clipper(*dc
, rectLabel
);
791 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
795 void wxListLineData::DrawInReportMode( wxDC
*dc
,
797 const wxRect
& rectHL
,
801 // TODO: later we should support setting different attributes for
802 // different columns - to do it, just add "col" argument to
803 // GetAttr() and move these lines into the loop below
804 wxListItemAttr
*attr
= GetAttr();
805 if ( SetAttributes(dc
, attr
, highlighted
) )
806 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
808 dc
->DrawRectangle( rectHL
);
810 wxUnusedVar(current
);
816 int flags
= wxCONTROL_SELECTED
;
817 if (m_owner
->HasFocus())
818 flags
|= wxCONTROL_FOCUSED
;
820 flags
|= wxCONTROL_CURRENT
;
821 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
825 dc
->DrawRectangle( rectHL
);
830 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
831 yMid
= rect
.y
+ rect
.height
/2;
833 // This probably needs to be done
834 // on all platforms as the icons
835 // otherwise nearly touch the border
840 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
842 node
= node
->GetNext(), col
++ )
844 wxListItemData
*item
= node
->GetData();
846 int width
= m_owner
->GetColumnWidth(col
);
850 const int wText
= width
- 8;
851 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
853 if ( item
->HasImage() )
856 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
857 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
859 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
865 if ( item
->HasText() )
866 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
870 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
871 const wxString
& textOrig
,
877 // we don't support displaying multiple lines currently (and neither does
878 // wxMSW FWIW) so just merge all the lines
879 wxString
text(textOrig
);
880 text
.Replace(_T("\n"), _T(" "));
883 dc
->GetTextExtent(text
, &w
, &h
);
885 const wxCoord y
= yMid
- (h
+ 1)/2;
887 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
889 // determine if the string can fit inside the current width
892 // it can, draw it using the items alignment
894 m_owner
->GetColumn(col
, item
);
895 switch ( item
.GetAlign() )
897 case wxLIST_FORMAT_LEFT
:
901 case wxLIST_FORMAT_RIGHT
:
905 case wxLIST_FORMAT_CENTER
:
906 x
+= (width
- w
) / 2;
910 wxFAIL_MSG( _T("unknown list item format") );
914 dc
->DrawText(text
, x
, y
);
916 else // otherwise, truncate and add an ellipsis if possible
918 // determine the base width
919 wxString
ellipsis(wxT("..."));
921 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
923 // continue until we have enough space or only one character left
925 size_t len
= text
.length();
926 wxString drawntext
= text
.Left(len
);
929 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
930 drawntext
.RemoveLast();
933 if (w
+ base_w
<= width
)
937 // if still not enough space, remove ellipsis characters
938 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
940 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
941 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
945 dc
->DrawText(drawntext
, x
, y
);
946 dc
->DrawText(ellipsis
, x
+ w
, y
);
950 bool wxListLineData::Highlight( bool on
)
952 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
954 if ( on
== m_highlighted
)
962 void wxListLineData::ReverseHighlight( void )
964 Highlight(!IsHighlighted());
967 //-----------------------------------------------------------------------------
968 // wxListHeaderWindow
969 //-----------------------------------------------------------------------------
971 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
972 EVT_PAINT (wxListHeaderWindow::OnPaint
)
973 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
974 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
977 void wxListHeaderWindow::Init()
979 m_currentCursor
= NULL
;
980 m_isDragging
= false;
982 m_sendSetColumnWidth
= false;
985 wxListHeaderWindow::wxListHeaderWindow()
990 m_resizeCursor
= NULL
;
993 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
995 wxListMainWindow
*owner
,
999 const wxString
&name
)
1000 : wxWindow( win
, id
, pos
, size
, style
, name
)
1005 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1008 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1009 SetOwnForegroundColour( attr
.colFg
);
1010 SetOwnBackgroundColour( attr
.colBg
);
1012 SetOwnFont( attr
.font
);
1014 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1015 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1017 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1021 wxListHeaderWindow::~wxListHeaderWindow()
1023 delete m_resizeCursor
;
1026 #ifdef __WXUNIVERSAL__
1027 #include "wx/univ/renderer.h"
1028 #include "wx/univ/theme.h"
1031 // shift the DC origin to match the position of the main window horz
1032 // scrollbar: this allows us to always use logical coords
1033 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1035 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1038 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1041 parent
->GetViewStart( &view_start
, NULL
);
1046 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1048 // account for the horz scrollbar offset
1050 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1052 // Maybe we just have to check for m_signX
1053 // in the DC, but I leave the #ifdef __WXGTK__
1055 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1059 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1062 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1064 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1066 wxPaintDC
dc( this );
1070 dc
.SetFont( GetFont() );
1072 // width and height of the entire header window
1074 GetClientSize( &w
, &h
);
1075 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1077 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1078 dc
.SetTextForeground(GetForegroundColour());
1080 int x
= HEADER_OFFSET_X
;
1081 int numColumns
= m_owner
->GetColumnCount();
1083 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1085 m_owner
->GetColumn( i
, item
);
1086 int wCol
= item
.m_width
;
1092 if (!m_parent
->IsEnabled())
1093 flags
|= wxCONTROL_DISABLED
;
1095 // NB: The code below is not really Mac-specific, but since we are close
1096 // to 2.8 release and I don't have time to test on other platforms, I
1097 // defined this only for wxMac. If this behavior is desired on
1098 // other platforms, please go ahead and revise or remove the #ifdef.
1100 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1101 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1102 flags
|= wxCONTROL_SELECTED
;
1106 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1108 wxRendererNative::Get().DrawHeaderButton
1112 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1116 // see if we have enough space for the column label
1118 // for this we need the width of the text
1121 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1122 wLabel
+= 2 * EXTRA_WIDTH
;
1124 // and the width of the icon, if any
1125 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1126 const int image
= item
.m_image
;
1127 wxImageList
*imageList
;
1130 imageList
= m_owner
->GetSmallImageList();
1133 imageList
->GetSize(image
, ix
, iy
);
1134 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1142 // ignore alignment if there is not enough space anyhow
1144 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1147 wxFAIL_MSG( _T("unknown list item format") );
1150 case wxLIST_FORMAT_LEFT
:
1154 case wxLIST_FORMAT_RIGHT
:
1155 xAligned
= x
+ cw
- wLabel
;
1158 case wxLIST_FORMAT_CENTER
:
1159 xAligned
= x
+ (cw
- wLabel
) / 2;
1163 // draw the text and image clipping them so that they
1164 // don't overwrite the column boundary
1165 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1167 // if we have an image, draw it on the right of the label
1174 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1175 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1176 wxIMAGELIST_DRAW_TRANSPARENT
1180 dc
.DrawText( item
.GetText(),
1181 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1186 // Fill in what's missing to the right of the columns, otherwise we will
1187 // leave an unpainted area when columns are removed (and it looks better)
1190 wxRendererNative::Get().DrawHeaderButton
1194 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1195 wxCONTROL_DIRTY
// mark as last column
1200 void wxListHeaderWindow::OnInternalIdle()
1202 wxWindow::OnInternalIdle();
1204 if (m_sendSetColumnWidth
)
1206 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1207 m_sendSetColumnWidth
= false;
1211 void wxListHeaderWindow::DrawCurrent()
1214 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1215 m_sendSetColumnWidth
= true;
1216 m_colToSend
= m_column
;
1217 m_widthToSend
= m_currentX
- m_minX
;
1219 int x1
= m_currentX
;
1221 m_owner
->ClientToScreen( &x1
, &y1
);
1223 int x2
= m_currentX
;
1225 m_owner
->GetClientSize( NULL
, &y2
);
1226 m_owner
->ClientToScreen( &x2
, &y2
);
1229 dc
.SetLogicalFunction( wxINVERT
);
1230 dc
.SetPen( wxPen(*wxBLACK
, 2) );
1231 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1235 dc
.DrawLine( x1
, y1
, x2
, y2
);
1237 dc
.SetLogicalFunction( wxCOPY
);
1239 dc
.SetPen( wxNullPen
);
1240 dc
.SetBrush( wxNullBrush
);
1244 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1246 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1248 // we want to work with logical coords
1250 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1251 int y
= event
.GetY();
1255 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1257 // we don't draw the line beyond our window, but we allow dragging it
1260 GetClientSize( &w
, NULL
);
1261 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1264 // erase the line if it was drawn
1265 if ( m_currentX
< w
)
1268 if (event
.ButtonUp())
1271 m_isDragging
= false;
1273 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1274 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1281 m_currentX
= m_minX
+ 7;
1283 // draw in the new location
1284 if ( m_currentX
< w
)
1288 else // not dragging
1291 bool hit_border
= false;
1293 // end of the current column
1296 // find the column where this event occurred
1298 countCol
= m_owner
->GetColumnCount();
1299 for (col
= 0; col
< countCol
; col
++)
1301 xpos
+= m_owner
->GetColumnWidth( col
);
1304 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1306 // near the column border
1313 // inside the column
1320 if ( col
== countCol
)
1323 if (event
.LeftDown() || event
.RightUp())
1325 if (hit_border
&& event
.LeftDown())
1327 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1328 event
.GetPosition()) )
1330 m_isDragging
= true;
1335 //else: column resizing was vetoed by the user code
1337 else // click on a column
1339 // record the selected state of the columns
1340 if (event
.LeftDown())
1342 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
1345 m_owner
->GetColumn(i
, colItem
);
1346 long state
= colItem
.GetState();
1348 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
1350 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
1351 m_owner
->SetColumn(i
, colItem
);
1355 SendListEvent( event
.LeftDown()
1356 ? wxEVT_COMMAND_LIST_COL_CLICK
1357 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1358 event
.GetPosition());
1361 else if (event
.Moving())
1366 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1367 m_currentCursor
= m_resizeCursor
;
1371 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1372 m_currentCursor
= wxSTANDARD_CURSOR
;
1376 SetCursor(*m_currentCursor
);
1381 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1383 m_owner
->SetFocus();
1387 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1389 wxWindow
*parent
= GetParent();
1390 wxListEvent
le( type
, parent
->GetId() );
1391 le
.SetEventObject( parent
);
1392 le
.m_pointDrag
= pos
;
1394 // the position should be relative to the parent window, not
1395 // this one for compatibility with MSW and common sense: the
1396 // user code doesn't know anything at all about this header
1397 // window, so why should it get positions relative to it?
1398 le
.m_pointDrag
.y
-= GetSize().y
;
1400 le
.m_col
= m_column
;
1401 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1404 //-----------------------------------------------------------------------------
1405 // wxListRenameTimer (internal)
1406 //-----------------------------------------------------------------------------
1408 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1413 void wxListRenameTimer::Notify()
1415 m_owner
->OnRenameTimer();
1418 //-----------------------------------------------------------------------------
1419 // wxListTextCtrlWrapper (internal)
1420 //-----------------------------------------------------------------------------
1422 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
1423 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
1424 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
1425 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
1428 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
1431 : m_startValue(owner
->GetItemText(itemEdit
)),
1432 m_itemEdited(itemEdit
)
1436 m_aboutToFinish
= false;
1438 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1440 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1442 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1443 &rectLabel
.x
, &rectLabel
.y
);
1445 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
1446 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1447 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
1450 m_text
->PushEventHandler(this);
1453 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
1455 m_aboutToFinish
= true;
1457 if ( discardChanges
)
1459 m_owner
->OnRenameCancelled(m_itemEdited
);
1465 // Notify the owner about the changes
1468 // Even if vetoed, close the control (consistent with MSW)
1473 void wxListTextCtrlWrapper::Finish( bool setfocus
)
1475 m_text
->RemoveEventHandler(this);
1476 m_owner
->ResetTextControl( m_text
);
1478 wxPendingDelete
.Append( this );
1481 m_owner
->SetFocus();
1484 bool wxListTextCtrlWrapper::AcceptChanges()
1486 const wxString value
= m_text
->GetValue();
1488 // notice that we should always call OnRenameAccept() to generate the "end
1489 // label editing" event, even if the user hasn't really changed anything
1490 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
1492 // vetoed by the user
1496 // accepted, do rename the item (unless nothing changed)
1497 if ( value
!= m_startValue
)
1498 m_owner
->SetItemText(m_itemEdited
, value
);
1503 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
1505 switch ( event
.m_keyCode
)
1520 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
1522 if (m_aboutToFinish
)
1524 // auto-grow the textctrl:
1525 wxSize parentSize
= m_owner
->GetSize();
1526 wxPoint myPos
= m_text
->GetPosition();
1527 wxSize mySize
= m_text
->GetSize();
1529 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
1530 if (myPos
.x
+ sx
> parentSize
.x
)
1531 sx
= parentSize
.x
- myPos
.x
;
1534 m_text
->SetSize(sx
, wxDefaultCoord
);
1540 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
1542 if ( !m_aboutToFinish
)
1544 if ( !AcceptChanges() )
1545 m_owner
->OnRenameCancelled( m_itemEdited
);
1550 // We must let the native text control handle focus
1554 //-----------------------------------------------------------------------------
1556 //-----------------------------------------------------------------------------
1558 BEGIN_EVENT_TABLE(wxListMainWindow
, wxWindow
)
1559 EVT_PAINT (wxListMainWindow::OnPaint
)
1560 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1561 EVT_CHAR (wxListMainWindow::OnChar
)
1562 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1563 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
1564 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1565 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1566 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1567 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
1570 void wxListMainWindow::Init()
1575 m_lineTo
= (size_t)-1;
1581 m_small_image_list
= NULL
;
1582 m_normal_image_list
= NULL
;
1584 m_small_spacing
= 30;
1585 m_normal_spacing
= 40;
1589 m_isCreated
= false;
1591 m_lastOnSame
= false;
1592 m_renameTimer
= new wxListRenameTimer( this );
1593 m_textctrlWrapper
= NULL
;
1597 m_lineSelectSingleOnUp
=
1598 m_lineBeforeLastClicked
= (size_t)-1;
1601 wxListMainWindow::wxListMainWindow()
1606 m_highlightUnfocusedBrush
= NULL
;
1609 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1614 const wxString
&name
)
1615 : wxWindow( parent
, id
, pos
, size
, style
, name
)
1619 m_highlightBrush
= new wxBrush
1621 wxSystemSettings::GetColour
1623 wxSYS_COLOUR_HIGHLIGHT
1628 m_highlightUnfocusedBrush
= new wxBrush
1630 wxSystemSettings::GetColour
1632 wxSYS_COLOUR_BTNSHADOW
1637 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
1638 SetOwnForegroundColour( attr
.colFg
);
1639 SetOwnBackgroundColour( attr
.colBg
);
1641 SetOwnFont( attr
.font
);
1644 wxListMainWindow::~wxListMainWindow()
1647 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
1648 WX_CLEAR_ARRAY(m_aColWidths
);
1650 delete m_highlightBrush
;
1651 delete m_highlightUnfocusedBrush
;
1652 delete m_renameTimer
;
1655 void wxListMainWindow::CacheLineData(size_t line
)
1657 wxGenericListCtrl
*listctrl
= GetListCtrl();
1659 wxListLineData
*ld
= GetDummyLine();
1661 size_t countCol
= GetColumnCount();
1662 for ( size_t col
= 0; col
< countCol
; col
++ )
1664 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1665 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1668 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1671 wxListLineData
*wxListMainWindow::GetDummyLine() const
1673 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
1674 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
1676 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1678 // we need to recreate the dummy line if the number of columns in the
1679 // control changed as it would have the incorrect number of fields
1681 if ( !m_lines
.IsEmpty() &&
1682 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1684 self
->m_lines
.Clear();
1687 if ( m_lines
.IsEmpty() )
1689 wxListLineData
*line
= new wxListLineData(self
);
1690 self
->m_lines
.Add(line
);
1692 // don't waste extra memory -- there never going to be anything
1693 // else/more in this array
1694 self
->m_lines
.Shrink();
1700 // ----------------------------------------------------------------------------
1701 // line geometry (report mode only)
1702 // ----------------------------------------------------------------------------
1704 wxCoord
wxListMainWindow::GetLineHeight() const
1706 // we cache the line height as calling GetTextExtent() is slow
1707 if ( !m_lineHeight
)
1709 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1711 wxClientDC
dc( self
);
1712 dc
.SetFont( GetFont() );
1715 dc
.GetTextExtent(_T("H"), NULL
, &y
);
1717 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1720 m_small_image_list
->GetSize(0, iw
, ih
);
1725 self
->m_lineHeight
= y
+ LINE_SPACING
;
1728 return m_lineHeight
;
1731 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1733 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
1735 return LINE_SPACING
+ line
* GetLineHeight();
1738 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1740 if ( !InReportView() )
1741 return GetLine(line
)->m_gi
->m_rectAll
;
1744 rect
.x
= HEADER_OFFSET_X
;
1745 rect
.y
= GetLineY(line
);
1746 rect
.width
= GetHeaderWidth();
1747 rect
.height
= GetLineHeight();
1752 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1754 if ( !InReportView() )
1755 return GetLine(line
)->m_gi
->m_rectLabel
;
1758 wxListLineData
*data
= GetLine(line
);
1759 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1762 wxListItemData
*item
= node
->GetData();
1763 if ( item
->HasImage() )
1766 GetImageSize( item
->GetImage(), ix
, iy
);
1767 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1772 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1773 rect
.y
= GetLineY(line
);
1774 rect
.width
= GetColumnWidth(0) - image_x
;
1775 rect
.height
= GetLineHeight();
1780 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1782 if ( !InReportView() )
1783 return GetLine(line
)->m_gi
->m_rectIcon
;
1785 wxListLineData
*ld
= GetLine(line
);
1786 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
1789 rect
.x
= HEADER_OFFSET_X
;
1790 rect
.y
= GetLineY(line
);
1791 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1796 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1798 return InReportView() ? GetLineRect(line
)
1799 : GetLine(line
)->m_gi
->m_rectHighlight
;
1802 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1804 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
1806 wxListLineData
*ld
= GetLine(line
);
1808 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1809 return wxLIST_HITTEST_ONITEMICON
;
1811 // VS: Testing for "ld->HasText() || InReportView()" instead of
1812 // "ld->HasText()" is needed to make empty lines in report view
1814 if ( ld
->HasText() || InReportView() )
1816 wxRect rect
= InReportView() ? GetLineRect(line
)
1817 : GetLineLabelRect(line
);
1819 if ( rect
.Contains(x
, y
) )
1820 return wxLIST_HITTEST_ONITEMLABEL
;
1826 // ----------------------------------------------------------------------------
1827 // highlight (selection) handling
1828 // ----------------------------------------------------------------------------
1830 bool wxListMainWindow::IsHighlighted(size_t line
) const
1834 return m_selStore
.IsSelected(line
);
1838 wxListLineData
*ld
= GetLine(line
);
1839 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
1841 return ld
->IsHighlighted();
1845 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1851 wxArrayInt linesChanged
;
1852 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1855 // meny items changed state, refresh everything
1856 RefreshLines(lineFrom
, lineTo
);
1858 else // only a few items changed state, refresh only them
1860 size_t count
= linesChanged
.GetCount();
1861 for ( size_t n
= 0; n
< count
; n
++ )
1863 RefreshLine(linesChanged
[n
]);
1867 else // iterate over all items in non report view
1869 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1871 if ( HighlightLine(line
, highlight
) )
1877 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1883 changed
= m_selStore
.SelectItem(line
, highlight
);
1887 wxListLineData
*ld
= GetLine(line
);
1888 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
1890 changed
= ld
->Highlight(highlight
);
1895 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1896 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1902 void wxListMainWindow::RefreshLine( size_t line
)
1904 if ( InReportView() )
1906 size_t visibleFrom
, visibleTo
;
1907 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1909 if ( line
< visibleFrom
|| line
> visibleTo
)
1913 wxRect rect
= GetLineRect(line
);
1915 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1916 RefreshRect( rect
);
1919 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1921 // we suppose that they are ordered by caller
1922 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
1924 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
1926 if ( InReportView() )
1928 size_t visibleFrom
, visibleTo
;
1929 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1931 if ( lineFrom
< visibleFrom
)
1932 lineFrom
= visibleFrom
;
1933 if ( lineTo
> visibleTo
)
1938 rect
.y
= GetLineY(lineFrom
);
1939 rect
.width
= GetClientSize().x
;
1940 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1942 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1943 RefreshRect( rect
);
1947 // TODO: this should be optimized...
1948 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1955 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1957 if ( InReportView() )
1959 size_t visibleFrom
, visibleTo
;
1960 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1962 if ( lineFrom
< visibleFrom
)
1963 lineFrom
= visibleFrom
;
1964 else if ( lineFrom
> visibleTo
)
1969 rect
.y
= GetLineY(lineFrom
);
1970 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1972 wxSize size
= GetClientSize();
1973 rect
.width
= size
.x
;
1975 // refresh till the bottom of the window
1976 rect
.height
= size
.y
- rect
.y
;
1978 RefreshRect( rect
);
1982 // TODO: how to do it more efficiently?
1987 void wxListMainWindow::RefreshSelected()
1993 if ( InReportView() )
1995 GetVisibleLinesRange(&from
, &to
);
2000 to
= GetItemCount() - 1;
2003 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2004 RefreshLine(m_current
);
2006 for ( size_t line
= from
; line
<= to
; line
++ )
2008 // NB: the test works as expected even if m_current == -1
2009 if ( line
!= m_current
&& IsHighlighted(line
) )
2014 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2016 // Note: a wxPaintDC must be constructed even if no drawing is
2017 // done (a Windows requirement).
2018 wxPaintDC
dc( this );
2022 // nothing to draw or not the moment to draw it
2027 RecalculatePositions( false );
2029 GetListCtrl()->PrepareDC( dc
);
2032 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2034 dc
.SetFont( GetFont() );
2036 if ( InReportView() )
2038 int lineHeight
= GetLineHeight();
2040 size_t visibleFrom
, visibleTo
;
2041 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2044 int xOrig
= dc
.LogicalToDeviceX( 0 );
2045 int yOrig
= dc
.LogicalToDeviceY( 0 );
2047 // tell the caller cache to cache the data
2050 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2051 GetParent()->GetId());
2052 evCache
.SetEventObject( GetParent() );
2053 evCache
.m_oldItemIndex
= visibleFrom
;
2054 evCache
.m_itemIndex
= visibleTo
;
2055 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2058 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2060 rectLine
= GetLineRect(line
);
2063 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2064 rectLine
.width
, rectLine
.height
) )
2066 // don't redraw unaffected lines to avoid flicker
2070 GetLine(line
)->DrawInReportMode( &dc
,
2072 GetLineHighlightRect(line
),
2073 IsHighlighted(line
),
2074 line
== m_current
);
2077 if ( HasFlag(wxLC_HRULES
) )
2079 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2080 wxSize clientSize
= GetClientSize();
2082 size_t i
= visibleFrom
;
2083 if (i
== 0) i
= 1; // Don't draw the first one
2084 for ( ; i
<= visibleTo
; i
++ )
2087 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2088 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2089 clientSize
.x
- dev_x
, i
* lineHeight
);
2092 // Draw last horizontal rule
2093 if ( visibleTo
== GetItemCount() - 1 )
2096 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2097 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2098 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2102 // Draw vertical rules if required
2103 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2105 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2106 wxRect firstItemRect
, lastItemRect
;
2108 GetItemRect(visibleFrom
, firstItemRect
);
2109 GetItemRect(visibleTo
, lastItemRect
);
2110 int x
= firstItemRect
.GetX();
2112 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2114 for (int col
= 0; col
< GetColumnCount(); col
++)
2116 int colWidth
= GetColumnWidth(col
);
2118 int x_pos
= x
- dev_x
;
2119 if (col
< GetColumnCount()-1) x_pos
-= 2;
2120 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2121 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2127 size_t count
= GetItemCount();
2128 for ( size_t i
= 0; i
< count
; i
++ )
2130 GetLine(i
)->Draw( &dc
);
2134 #if !defined( __WXMAC__) && !defined(__WXGTK20__)
2135 // Don't draw rect outline under Mac at all.
2136 // Draw it elsewhere under GTK.
2141 wxRect
rect( GetLineHighlightRect( m_current
) );
2142 dc
.SetPen( *wxBLACK_PEN
);
2143 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2144 dc
.DrawRectangle( rect
);
2150 void wxListMainWindow::HighlightAll( bool on
)
2152 if ( IsSingleSel() )
2154 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2156 // we just have one item to turn off
2157 if ( HasCurrent() && IsHighlighted(m_current
) )
2159 HighlightLine(m_current
, false);
2160 RefreshLine(m_current
);
2163 else // multi selection
2166 HighlightLines(0, GetItemCount() - 1, on
);
2170 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2172 // Do nothing here. This prevents the default handler in wxScrolledWindow
2173 // from needlessly scrolling the window when the edit control is
2174 // dismissed. See ticket #9563.
2177 void wxListMainWindow::SendNotify( size_t line
,
2178 wxEventType command
,
2179 const wxPoint
& point
)
2181 wxListEvent
le( command
, GetParent()->GetId() );
2182 le
.SetEventObject( GetParent() );
2184 le
.m_itemIndex
= line
;
2186 // set only for events which have position
2187 if ( point
!= wxDefaultPosition
)
2188 le
.m_pointDrag
= point
;
2190 // don't try to get the line info for virtual list controls: the main
2191 // program has it anyhow and if we did it would result in accessing all
2192 // the lines, even those which are not visible now and this is precisely
2193 // what we're trying to avoid
2196 if ( line
!= (size_t)-1 )
2198 GetLine(line
)->GetItem( 0, le
.m_item
);
2200 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2202 //else: there may be no more such item
2204 GetParent()->GetEventHandler()->ProcessEvent( le
);
2207 void wxListMainWindow::ChangeCurrent(size_t current
)
2209 m_current
= current
;
2211 // as the current item changed, we shouldn't start editing it when the
2212 // "slow click" timer expires as the click happened on another item
2213 if ( m_renameTimer
->IsRunning() )
2214 m_renameTimer
->Stop();
2216 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2219 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2221 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2222 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2224 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2225 wxT("EditLabel() needs a text control") );
2227 size_t itemEdit
= (size_t)item
;
2229 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2230 le
.SetEventObject( GetParent() );
2231 le
.m_itemIndex
= item
;
2232 wxListLineData
*data
= GetLine(itemEdit
);
2233 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2234 data
->GetItem( 0, le
.m_item
);
2236 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2238 // vetoed by user code
2242 // We have to call this here because the label in question might just have
2243 // been added and no screen update taken place.
2246 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2247 // so that no pending events may change the item count (see below)
2248 // IMPORTANT: needs to be tested!
2251 // Pending events dispatched by wxSafeYield might have changed the item
2253 if ( (size_t)item
>= GetItemCount() )
2257 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2258 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2259 return m_textctrlWrapper
->GetText();
2262 void wxListMainWindow::OnRenameTimer()
2264 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2266 EditLabel( m_current
);
2269 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2271 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2272 le
.SetEventObject( GetParent() );
2273 le
.m_itemIndex
= itemEdit
;
2275 wxListLineData
*data
= GetLine(itemEdit
);
2277 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2279 data
->GetItem( 0, le
.m_item
);
2280 le
.m_item
.m_text
= value
;
2281 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2285 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2287 // let owner know that the edit was cancelled
2288 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2290 le
.SetEditCanceled(true);
2292 le
.SetEventObject( GetParent() );
2293 le
.m_itemIndex
= itemEdit
;
2295 wxListLineData
*data
= GetLine(itemEdit
);
2296 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2298 data
->GetItem( 0, le
.m_item
);
2299 GetEventHandler()->ProcessEvent( le
);
2302 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2305 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2306 // shutdown the edit control when the mouse is clicked elsewhere on the
2307 // listctrl because the order of events is different (or something like
2308 // that), so explicitly end the edit if it is active.
2309 if ( event
.LeftDown() && m_textctrlWrapper
)
2310 m_textctrlWrapper
->EndEdit( false );
2313 if ( event
.LeftDown() )
2316 event
.SetEventObject( GetParent() );
2317 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2320 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2322 // let the base class handle mouse wheel events.
2327 if ( !HasCurrent() || IsEmpty() )
2329 if (event
.RightDown())
2331 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2333 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2334 GetParent()->GetId(),
2335 ClientToScreen(event
.GetPosition()));
2336 evtCtx
.SetEventObject(GetParent());
2337 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2345 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2346 event
.ButtonDClick()) )
2349 int x
= event
.GetX();
2350 int y
= event
.GetY();
2351 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2353 // where did we hit it (if we did)?
2356 size_t count
= GetItemCount(),
2359 if ( InReportView() )
2361 current
= y
/ GetLineHeight();
2362 if ( current
< count
)
2363 hitResult
= HitTestLine(current
, x
, y
);
2367 // TODO: optimize it too! this is less simple than for report view but
2368 // enumerating all items is still not a way to do it!!
2369 for ( current
= 0; current
< count
; current
++ )
2371 hitResult
= HitTestLine(current
, x
, y
);
2377 if (event
.Dragging())
2379 if (m_dragCount
== 0)
2381 // we have to report the raw, physical coords as we want to be
2382 // able to call HitTest(event.m_pointDrag) from the user code to
2383 // get the item being dragged
2384 m_dragStart
= event
.GetPosition();
2389 if (m_dragCount
!= 3)
2392 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2393 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2395 wxListEvent
le( command
, GetParent()->GetId() );
2396 le
.SetEventObject( GetParent() );
2397 le
.m_itemIndex
= m_lineLastClicked
;
2398 le
.m_pointDrag
= m_dragStart
;
2399 GetParent()->GetEventHandler()->ProcessEvent( le
);
2410 // outside of any item
2411 if (event
.RightDown())
2413 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2415 wxContextMenuEvent
evtCtx(
2417 GetParent()->GetId(),
2418 ClientToScreen(event
.GetPosition()));
2419 evtCtx
.SetEventObject(GetParent());
2420 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2424 // reset the selection and bail out
2425 HighlightAll(false);
2431 bool forceClick
= false;
2432 if (event
.ButtonDClick())
2434 if ( m_renameTimer
->IsRunning() )
2435 m_renameTimer
->Stop();
2437 m_lastOnSame
= false;
2439 if ( current
== m_lineLastClicked
)
2441 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2447 // The first click was on another item, so don't interpret this as
2448 // a double click, but as a simple click instead
2455 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2457 // select single line
2458 HighlightAll( false );
2459 ReverseHighlight(m_lineSelectSingleOnUp
);
2464 if ((current
== m_current
) &&
2465 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2466 HasFlag(wxLC_EDIT_LABELS
) )
2468 if ( !InReportView() ||
2469 GetLineLabelRect(current
).Contains(x
, y
) )
2471 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2472 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2477 m_lastOnSame
= false;
2478 m_lineSelectSingleOnUp
= (size_t)-1;
2482 // This is necessary, because after a DnD operation in
2483 // from and to ourself, the up event is swallowed by the
2484 // DnD code. So on next non-up event (which means here and
2485 // now) m_lineSelectSingleOnUp should be reset.
2486 m_lineSelectSingleOnUp
= (size_t)-1;
2488 if (event
.RightDown())
2490 m_lineBeforeLastClicked
= m_lineLastClicked
;
2491 m_lineLastClicked
= current
;
2493 // If the item is already selected, do not update the selection.
2494 // Multi-selections should not be cleared if a selected item is clicked.
2495 if (!IsHighlighted(current
))
2497 HighlightAll(false);
2498 ChangeCurrent(current
);
2499 ReverseHighlight(m_current
);
2502 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2504 // Allow generation of context menu event
2507 else if (event
.MiddleDown())
2509 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2511 else if ( event
.LeftDown() || forceClick
)
2513 m_lineBeforeLastClicked
= m_lineLastClicked
;
2514 m_lineLastClicked
= current
;
2516 size_t oldCurrent
= m_current
;
2517 bool oldWasSelected
= IsHighlighted(m_current
);
2519 bool cmdModifierDown
= event
.CmdDown();
2520 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2522 if ( IsSingleSel() || !IsHighlighted(current
) )
2524 HighlightAll( false );
2526 ChangeCurrent(current
);
2528 ReverseHighlight(m_current
);
2530 else // multi sel & current is highlighted & no mod keys
2532 m_lineSelectSingleOnUp
= current
;
2533 ChangeCurrent(current
); // change focus
2536 else // multi sel & either ctrl or shift is down
2538 if (cmdModifierDown
)
2540 ChangeCurrent(current
);
2542 ReverseHighlight(m_current
);
2544 else if (event
.ShiftDown())
2546 ChangeCurrent(current
);
2548 size_t lineFrom
= oldCurrent
,
2551 if ( lineTo
< lineFrom
)
2554 lineFrom
= m_current
;
2557 HighlightLines(lineFrom
, lineTo
);
2559 else // !ctrl, !shift
2561 // test in the enclosing if should make it impossible
2562 wxFAIL_MSG( _T("how did we get here?") );
2566 if (m_current
!= oldCurrent
)
2567 RefreshLine( oldCurrent
);
2569 // forceClick is only set if the previous click was on another item
2570 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2574 void wxListMainWindow::MoveToItem(size_t item
)
2576 if ( item
== (size_t)-1 )
2579 wxRect rect
= GetLineRect(item
);
2581 int client_w
, client_h
;
2582 GetClientSize( &client_w
, &client_h
);
2584 const int hLine
= GetLineHeight();
2586 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2587 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2589 if ( InReportView() )
2591 // the next we need the range of lines shown it might be different,
2592 // so recalculate it
2593 ResetVisibleLinesRange();
2595 if (rect
.y
< view_y
)
2596 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2597 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2598 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2601 // At least on Mac the visible lines value will get reset inside of
2602 // Scroll *before* it actually scrolls the window because of the
2603 // Update() that happens there, so it will still have the wrong value.
2604 // So let's reset it again and wait for it to be recalculated in the
2605 // next paint event. I would expect this problem to show up in wxGTK
2606 // too but couldn't duplicate it there. Perhaps the order of events
2607 // is different... --Robin
2608 ResetVisibleLinesRange();
2616 if (rect
.x
-view_x
< 5)
2617 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2618 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2619 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2621 if (rect
.y
-view_y
< 5)
2622 sy
= (rect
.y
- 5) / hLine
;
2623 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2624 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2626 GetListCtrl()->Scroll(sx
, sy
);
2630 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2632 if ( !InReportView() )
2634 // TODO: this should work in all views but is not implemented now
2639 GetVisibleLinesRange(&top
, &bottom
);
2641 if ( bottom
== (size_t)-1 )
2644 ResetVisibleLinesRange();
2646 int hLine
= GetLineHeight();
2648 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2651 // see comment in MoveToItem() for why we do this
2652 ResetVisibleLinesRange();
2658 // ----------------------------------------------------------------------------
2659 // keyboard handling
2660 // ----------------------------------------------------------------------------
2662 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2664 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2665 _T("invalid item index in OnArrowChar()") );
2667 size_t oldCurrent
= m_current
;
2669 // in single selection we just ignore Shift as we can't select several
2671 if ( event
.ShiftDown() && !IsSingleSel() )
2673 ChangeCurrent(newCurrent
);
2675 // refresh the old focus to remove it
2676 RefreshLine( oldCurrent
);
2678 // select all the items between the old and the new one
2679 if ( oldCurrent
> newCurrent
)
2681 newCurrent
= oldCurrent
;
2682 oldCurrent
= m_current
;
2685 HighlightLines(oldCurrent
, newCurrent
);
2689 // all previously selected items are unselected unless ctrl is held
2690 // in a multiselection control
2691 if ( !event
.ControlDown() || IsSingleSel() )
2692 HighlightAll(false);
2694 ChangeCurrent(newCurrent
);
2696 // refresh the old focus to remove it
2697 RefreshLine( oldCurrent
);
2699 // in single selection mode we must always have a selected item
2700 if ( !event
.ControlDown() || IsSingleSel() )
2701 HighlightLine( m_current
, true );
2704 RefreshLine( m_current
);
2709 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2711 wxWindow
*parent
= GetParent();
2713 // propagate the key event upwards
2714 wxKeyEvent
ke(event
);
2715 ke
.SetEventObject( parent
);
2716 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2722 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2724 wxWindow
*parent
= GetParent();
2726 // propagate the key event upwards
2727 wxKeyEvent
ke(event
);
2728 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2734 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2736 wxWindow
*parent
= GetParent();
2738 // send a list_key event up
2741 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2742 le
.m_itemIndex
= m_current
;
2743 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2744 le
.m_code
= event
.GetKeyCode();
2745 le
.SetEventObject( parent
);
2746 parent
->GetEventHandler()->ProcessEvent( le
);
2749 if ( (event
.GetKeyCode() != WXK_UP
) &&
2750 (event
.GetKeyCode() != WXK_DOWN
) &&
2751 (event
.GetKeyCode() != WXK_RIGHT
) &&
2752 (event
.GetKeyCode() != WXK_LEFT
) &&
2753 (event
.GetKeyCode() != WXK_PAGEUP
) &&
2754 (event
.GetKeyCode() != WXK_PAGEDOWN
) &&
2755 (event
.GetKeyCode() != WXK_END
) &&
2756 (event
.GetKeyCode() != WXK_HOME
) )
2758 // propagate the char event upwards
2759 wxKeyEvent
ke(event
);
2760 ke
.SetEventObject( parent
);
2761 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2765 if ( HandleAsNavigationKey(event
) )
2768 // no item -> nothing to do
2775 // don't use m_linesPerPage directly as it might not be computed yet
2776 const int pageSize
= GetCountPerPage();
2777 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2779 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2781 if (event
.GetKeyCode() == WXK_RIGHT
)
2782 event
.m_keyCode
= WXK_LEFT
;
2783 else if (event
.GetKeyCode() == WXK_LEFT
)
2784 event
.m_keyCode
= WXK_RIGHT
;
2787 switch ( event
.GetKeyCode() )
2790 if ( m_current
> 0 )
2791 OnArrowChar( m_current
- 1, event
);
2795 if ( m_current
< (size_t)GetItemCount() - 1 )
2796 OnArrowChar( m_current
+ 1, event
);
2801 OnArrowChar( GetItemCount() - 1, event
);
2806 OnArrowChar( 0, event
);
2811 int steps
= InReportView() ? pageSize
- 1
2812 : m_current
% pageSize
;
2814 int index
= m_current
- steps
;
2818 OnArrowChar( index
, event
);
2824 int steps
= InReportView()
2826 : pageSize
- (m_current
% pageSize
) - 1;
2828 size_t index
= m_current
+ steps
;
2829 size_t count
= GetItemCount();
2830 if ( index
>= count
)
2833 OnArrowChar( index
, event
);
2838 if ( !InReportView() )
2840 int index
= m_current
- pageSize
;
2844 OnArrowChar( index
, event
);
2849 if ( !InReportView() )
2851 size_t index
= m_current
+ pageSize
;
2853 size_t count
= GetItemCount();
2854 if ( index
>= count
)
2857 OnArrowChar( index
, event
);
2862 if ( IsSingleSel() )
2864 if ( event
.ControlDown() )
2866 ReverseHighlight(m_current
);
2868 else // normal space press
2870 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2873 else // multiple selection
2875 ReverseHighlight(m_current
);
2881 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2889 // ----------------------------------------------------------------------------
2891 // ----------------------------------------------------------------------------
2893 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2897 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2898 event
.SetEventObject( GetParent() );
2899 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2903 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2904 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2905 // which are already drawn correctly resulting in horrible flicker - avoid
2915 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2919 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2920 event
.SetEventObject( GetParent() );
2921 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2929 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2931 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2933 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2935 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2937 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2939 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2941 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2943 else if ( InReportView() && (m_small_image_list
))
2945 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2949 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2951 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2953 m_normal_image_list
->GetSize( index
, width
, height
);
2955 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2957 m_small_image_list
->GetSize( index
, width
, height
);
2959 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2961 m_small_image_list
->GetSize( index
, width
, height
);
2963 else if ( InReportView() && m_small_image_list
)
2965 m_small_image_list
->GetSize( index
, width
, height
);
2974 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2976 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2977 dc
.SetFont( GetFont() );
2980 dc
.GetTextExtent( s
, &lw
, NULL
);
2982 return lw
+ AUTOSIZE_COL_MARGIN
;
2985 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2989 // calc the spacing from the icon size
2990 int width
= 0, height
= 0;
2992 if ((imageList
) && (imageList
->GetImageCount()) )
2993 imageList
->GetSize(0, width
, height
);
2995 if (which
== wxIMAGE_LIST_NORMAL
)
2997 m_normal_image_list
= imageList
;
2998 m_normal_spacing
= width
+ 8;
3001 if (which
== wxIMAGE_LIST_SMALL
)
3003 m_small_image_list
= imageList
;
3004 m_small_spacing
= width
+ 14;
3005 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3009 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3013 m_small_spacing
= spacing
;
3015 m_normal_spacing
= spacing
;
3018 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3020 return isSmall
? m_small_spacing
: m_normal_spacing
;
3023 // ----------------------------------------------------------------------------
3025 // ----------------------------------------------------------------------------
3027 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3029 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3031 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3033 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3034 item
.m_width
= GetTextLength( item
.m_text
);
3036 wxListHeaderData
*column
= node
->GetData();
3037 column
->SetItem( item
);
3039 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3041 headerWin
->m_dirty
= true;
3045 // invalidate it as it has to be recalculated
3049 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3051 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3052 _T("invalid column index") );
3054 wxCHECK_RET( InReportView(),
3055 _T("SetColumnWidth() can only be called in report mode.") );
3059 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3061 headerWin
->m_dirty
= true;
3063 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3064 wxCHECK_RET( node
, _T("no column?") );
3066 wxListHeaderData
*column
= node
->GetData();
3068 size_t count
= GetItemCount();
3070 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3072 width
= GetTextLength(column
->GetText());
3073 width
+= 2*EXTRA_WIDTH
;
3075 // check for column header's image availability
3076 const int image
= column
->GetImage();
3079 if ( m_small_image_list
)
3082 m_small_image_list
->GetSize(image
, ix
, iy
);
3083 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3087 else if ( width
== wxLIST_AUTOSIZE
)
3091 // TODO: determine the max width somehow...
3092 width
= WIDTH_COL_DEFAULT
;
3096 wxClientDC
dc(this);
3097 dc
.SetFont( GetFont() );
3099 int max
= AUTOSIZE_COL_MARGIN
;
3101 // if the cached column width isn't valid then recalculate it
3102 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3104 for (size_t i
= 0; i
< count
; i
++)
3106 wxListLineData
*line
= GetLine( i
);
3107 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3109 wxCHECK_RET( n
, _T("no subitem?") );
3111 wxListItemData
*itemData
= n
->GetData();
3114 itemData
->GetItem(item
);
3115 int itemWidth
= GetItemWidthWithImage(&item
);
3116 if (itemWidth
> max
)
3120 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3121 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3124 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3125 width
= max
+ AUTOSIZE_COL_MARGIN
;
3129 column
->SetWidth( width
);
3131 // invalidate it as it has to be recalculated
3135 int wxListMainWindow::GetHeaderWidth() const
3137 if ( !m_headerWidth
)
3139 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3141 size_t count
= GetColumnCount();
3142 for ( size_t col
= 0; col
< count
; col
++ )
3144 self
->m_headerWidth
+= GetColumnWidth(col
);
3148 return m_headerWidth
;
3151 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3153 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3154 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3156 wxListHeaderData
*column
= node
->GetData();
3157 column
->GetItem( item
);
3160 int wxListMainWindow::GetColumnWidth( int col
) const
3162 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3163 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3165 wxListHeaderData
*column
= node
->GetData();
3166 return column
->GetWidth();
3169 // ----------------------------------------------------------------------------
3171 // ----------------------------------------------------------------------------
3173 void wxListMainWindow::SetItem( wxListItem
&item
)
3175 long id
= item
.m_itemId
;
3176 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3177 _T("invalid item index in SetItem") );
3181 wxListLineData
*line
= GetLine((size_t)id
);
3182 line
->SetItem( item
.m_col
, item
);
3184 // Set item state if user wants
3185 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3186 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3190 // update the Max Width Cache if needed
3191 int width
= GetItemWidthWithImage(&item
);
3193 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3194 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3198 // update the item on screen
3200 GetItemRect(id
, rectItem
);
3201 RefreshRect(rectItem
);
3204 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3209 // first deal with selection
3210 if ( stateMask
& wxLIST_STATE_SELECTED
)
3212 // set/clear select state
3215 // optimized version for virtual listctrl.
3216 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3219 else if ( state
& wxLIST_STATE_SELECTED
)
3221 const long count
= GetItemCount();
3222 for( long i
= 0; i
< count
; i
++ )
3224 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3230 // clear for non virtual (somewhat optimized by using GetNextItem())
3232 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3234 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3239 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3241 // unfocus all: only one item can be focussed, so clearing focus for
3242 // all items is simply clearing focus of the focussed item.
3243 SetItemState(m_current
, state
, stateMask
);
3245 //(setting focus to all items makes no sense, so it is not handled here.)
3248 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3252 SetItemStateAll(state
, stateMask
);
3256 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3257 _T("invalid list ctrl item index in SetItem") );
3259 size_t oldCurrent
= m_current
;
3260 size_t item
= (size_t)litem
; // safe because of the check above
3262 // do we need to change the focus?
3263 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3265 if ( state
& wxLIST_STATE_FOCUSED
)
3267 // don't do anything if this item is already focused
3268 if ( item
!= m_current
)
3270 ChangeCurrent(item
);
3272 if ( oldCurrent
!= (size_t)-1 )
3274 if ( IsSingleSel() )
3276 HighlightLine(oldCurrent
, false);
3279 RefreshLine(oldCurrent
);
3282 RefreshLine( m_current
);
3287 // don't do anything if this item is not focused
3288 if ( item
== m_current
)
3292 if ( IsSingleSel() )
3294 // we must unselect the old current item as well or we
3295 // might end up with more than one selected item in a
3296 // single selection control
3297 HighlightLine(oldCurrent
, false);
3300 RefreshLine( oldCurrent
);
3305 // do we need to change the selection state?
3306 if ( stateMask
& wxLIST_STATE_SELECTED
)
3308 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3310 if ( IsSingleSel() )
3314 // selecting the item also makes it the focused one in the
3316 if ( m_current
!= item
)
3318 ChangeCurrent(item
);
3320 if ( oldCurrent
!= (size_t)-1 )
3322 HighlightLine( oldCurrent
, false );
3323 RefreshLine( oldCurrent
);
3329 // only the current item may be selected anyhow
3330 if ( item
!= m_current
)
3335 if ( HighlightLine(item
, on
) )
3342 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3344 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3345 _T("invalid list ctrl item index in GetItemState()") );
3347 int ret
= wxLIST_STATE_DONTCARE
;
3349 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3351 if ( (size_t)item
== m_current
)
3352 ret
|= wxLIST_STATE_FOCUSED
;
3355 if ( stateMask
& wxLIST_STATE_SELECTED
)
3357 if ( IsHighlighted(item
) )
3358 ret
|= wxLIST_STATE_SELECTED
;
3364 void wxListMainWindow::GetItem( wxListItem
&item
) const
3366 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3367 _T("invalid item index in GetItem") );
3369 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3370 line
->GetItem( item
.m_col
, item
);
3372 // Get item state if user wants it
3373 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3374 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3375 wxLIST_STATE_FOCUSED
);
3378 // ----------------------------------------------------------------------------
3380 // ----------------------------------------------------------------------------
3382 size_t wxListMainWindow::GetItemCount() const
3384 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3387 void wxListMainWindow::SetItemCount(long count
)
3389 m_selStore
.SetItemCount(count
);
3390 m_countVirt
= count
;
3392 ResetVisibleLinesRange();
3394 // scrollbars must be reset
3398 int wxListMainWindow::GetSelectedItemCount() const
3400 // deal with the quick case first
3401 if ( IsSingleSel() )
3402 return HasCurrent() ? IsHighlighted(m_current
) : false;
3404 // virtual controls remmebers all its selections itself
3406 return m_selStore
.GetSelectedCount();
3408 // TODO: we probably should maintain the number of items selected even for
3409 // non virtual controls as enumerating all lines is really slow...
3410 size_t countSel
= 0;
3411 size_t count
= GetItemCount();
3412 for ( size_t line
= 0; line
< count
; line
++ )
3414 if ( GetLine(line
)->IsHighlighted() )
3421 // ----------------------------------------------------------------------------
3422 // item position/size
3423 // ----------------------------------------------------------------------------
3425 wxRect
wxListMainWindow::GetViewRect() const
3427 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3429 // we need to find the longest/tallest label
3430 wxCoord xMax
= 0, yMax
= 0;
3431 const int count
= GetItemCount();
3434 for ( int i
= 0; i
< count
; i
++ )
3436 // we need logical, not physical, coordinates here, so use
3437 // GetLineRect() instead of GetItemRect()
3438 wxRect r
= GetLineRect(i
);
3440 wxCoord x
= r
.GetRight(),
3450 // some fudge needed to make it look prettier
3451 xMax
+= 2 * EXTRA_BORDER_X
;
3452 yMax
+= 2 * EXTRA_BORDER_Y
;
3454 // account for the scrollbars if necessary
3455 const wxSize sizeAll
= GetClientSize();
3456 if ( xMax
> sizeAll
.x
)
3457 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3458 if ( yMax
> sizeAll
.y
)
3459 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3461 return wxRect(0, 0, xMax
, yMax
);
3465 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3467 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3469 _T("GetSubItemRect only meaningful in report view") );
3470 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3471 _T("invalid item in GetSubItemRect") );
3473 // ensure that we're laid out, otherwise we could return nonsense
3476 wxConstCast(this, wxListMainWindow
)->
3477 RecalculatePositions(true /* no refresh */);
3480 rect
= GetLineRect((size_t)item
);
3482 // Adjust rect to specified column
3483 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3485 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3486 _T("invalid subItem in GetSubItemRect") );
3488 for (int i
= 0; i
< subItem
; i
++)
3490 rect
.x
+= GetColumnWidth(i
);
3492 rect
.width
= GetColumnWidth(subItem
);
3495 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3500 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3503 GetItemRect(item
, rect
);
3511 // ----------------------------------------------------------------------------
3512 // geometry calculation
3513 // ----------------------------------------------------------------------------
3515 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3517 const int lineHeight
= GetLineHeight();
3519 wxClientDC
dc( this );
3520 dc
.SetFont( GetFont() );
3522 const size_t count
= GetItemCount();
3525 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3526 iconSpacing
= m_normal_spacing
;
3527 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3528 iconSpacing
= m_small_spacing
;
3532 // Note that we do not call GetClientSize() here but
3533 // GetSize() and subtract the border size for sunken
3534 // borders manually. This is technically incorrect,
3535 // but we need to know the client area's size WITHOUT
3536 // scrollbars here. Since we don't know if there are
3537 // any scrollbars, we use GetSize() instead. Another
3538 // solution would be to call SetScrollbars() here to
3539 // remove the scrollbars and call GetClientSize() then,
3540 // but this might result in flicker and - worse - will
3541 // reset the scrollbars to 0 which is not good at all
3542 // if you resize a dialog/window, but don't want to
3543 // reset the window scrolling. RR.
3544 // Furthermore, we actually do NOT subtract the border
3545 // width as 2 pixels is just the extra space which we
3546 // need around the actual content in the window. Other-
3547 // wise the text would e.g. touch the upper border. RR.
3550 GetSize( &clientWidth
, &clientHeight
);
3552 if ( InReportView() )
3554 // all lines have the same height and we scroll one line per step
3555 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3557 m_linesPerPage
= clientHeight
/ lineHeight
;
3559 ResetVisibleLinesRange();
3561 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3562 GetHeaderWidth() / SCROLL_UNIT_X
,
3563 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3564 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3565 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3570 // we have 3 different layout strategies: either layout all items
3571 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3572 // to arrange them in top to bottom, left to right (don't ask me why
3573 // not the other way round...) order
3574 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3576 int x
= EXTRA_BORDER_X
;
3577 int y
= EXTRA_BORDER_Y
;
3579 wxCoord widthMax
= 0;
3582 for ( i
= 0; i
< count
; i
++ )
3584 wxListLineData
*line
= GetLine(i
);
3585 line
->CalculateSize( &dc
, iconSpacing
);
3586 line
->SetPosition( x
, y
, iconSpacing
);
3588 wxSize sizeLine
= GetLineSize(i
);
3590 if ( HasFlag(wxLC_ALIGN_TOP
) )
3592 if ( sizeLine
.x
> widthMax
)
3593 widthMax
= sizeLine
.x
;
3597 else // wxLC_ALIGN_LEFT
3599 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3603 if ( HasFlag(wxLC_ALIGN_TOP
) )
3605 // traverse the items again and tweak their sizes so that they are
3606 // all the same in a row
3607 for ( i
= 0; i
< count
; i
++ )
3609 wxListLineData
*line
= GetLine(i
);
3610 line
->m_gi
->ExtendWidth(widthMax
);
3614 GetListCtrl()->SetScrollbars
3618 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3619 (y
+ lineHeight
) / lineHeight
,
3620 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3621 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3625 else // "flowed" arrangement, the most complicated case
3627 // at first we try without any scrollbars, if the items don't fit into
3628 // the window, we recalculate after subtracting the space taken by the
3631 int entireWidth
= 0;
3633 for (int tries
= 0; tries
< 2; tries
++)
3635 entireWidth
= 2 * EXTRA_BORDER_X
;
3639 // Now we have decided that the items do not fit into the
3640 // client area, so we need a scrollbar
3641 entireWidth
+= SCROLL_UNIT_X
;
3644 int x
= EXTRA_BORDER_X
;
3645 int y
= EXTRA_BORDER_Y
;
3646 int maxWidthInThisRow
= 0;
3649 int currentlyVisibleLines
= 0;
3651 for (size_t i
= 0; i
< count
; i
++)
3653 currentlyVisibleLines
++;
3654 wxListLineData
*line
= GetLine( i
);
3655 line
->CalculateSize( &dc
, iconSpacing
);
3656 line
->SetPosition( x
, y
, iconSpacing
);
3658 wxSize sizeLine
= GetLineSize( i
);
3660 if ( maxWidthInThisRow
< sizeLine
.x
)
3661 maxWidthInThisRow
= sizeLine
.x
;
3664 if (currentlyVisibleLines
> m_linesPerPage
)
3665 m_linesPerPage
= currentlyVisibleLines
;
3667 if ( y
+ sizeLine
.y
>= clientHeight
)
3669 currentlyVisibleLines
= 0;
3671 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3672 x
+= maxWidthInThisRow
;
3673 entireWidth
+= maxWidthInThisRow
;
3674 maxWidthInThisRow
= 0;
3677 // We have reached the last item.
3678 if ( i
== count
- 1 )
3679 entireWidth
+= maxWidthInThisRow
;
3681 if ( (tries
== 0) &&
3682 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3684 clientHeight
-= wxSystemSettings::
3685 GetMetric(wxSYS_HSCROLL_Y
);
3690 if ( i
== count
- 1 )
3691 tries
= 1; // Everything fits, no second try required.
3695 GetListCtrl()->SetScrollbars
3699 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3701 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3710 // FIXME: why should we call it from here?
3717 void wxListMainWindow::RefreshAll()
3722 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3723 if ( headerWin
&& headerWin
->m_dirty
)
3725 headerWin
->m_dirty
= false;
3726 headerWin
->Refresh();
3730 void wxListMainWindow::UpdateCurrent()
3732 if ( !HasCurrent() && !IsEmpty() )
3736 long wxListMainWindow::GetNextItem( long item
,
3737 int WXUNUSED(geometry
),
3741 max
= GetItemCount();
3742 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3743 _T("invalid listctrl index in GetNextItem()") );
3745 // notice that we start with the next item (or the first one if item == -1)
3746 // and this is intentional to allow writing a simple loop to iterate over
3747 // all selected items
3750 // this is not an error because the index was OK initially,
3751 // just no such item
3758 size_t count
= GetItemCount();
3759 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3761 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3764 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3771 // ----------------------------------------------------------------------------
3773 // ----------------------------------------------------------------------------
3775 void wxListMainWindow::DeleteItem( long lindex
)
3777 size_t count
= GetItemCount();
3779 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3780 _T("invalid item index in DeleteItem") );
3782 size_t index
= (size_t)lindex
;
3784 // we don't need to adjust the index for the previous items
3785 if ( HasCurrent() && m_current
>= index
)
3787 // if the current item is being deleted, we want the next one to
3788 // become selected - unless there is no next one - so don't adjust
3789 // m_current in this case
3790 if ( m_current
!= index
|| m_current
== count
- 1 )
3794 if ( InReportView() )
3796 // mark the Column Max Width cache as dirty if the items in the line
3797 // we're deleting contain the Max Column Width
3798 wxListLineData
* const line
= GetLine(index
);
3799 wxListItemDataList::compatibility_iterator n
;
3800 wxListItemData
*itemData
;
3804 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3806 n
= line
->m_items
.Item( i
);
3807 itemData
= n
->GetData();
3808 itemData
->GetItem(item
);
3810 itemWidth
= GetItemWidthWithImage(&item
);
3812 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3813 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3816 ResetVisibleLinesRange();
3819 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3824 m_selStore
.OnItemDelete(index
);
3828 m_lines
.RemoveAt( index
);
3831 // we need to refresh the (vert) scrollbar as the number of items changed
3834 RefreshAfter(index
);
3837 void wxListMainWindow::DeleteColumn( int col
)
3839 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3841 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3844 delete node
->GetData();
3845 m_columns
.Erase( node
);
3849 // update all the items
3850 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3852 wxListLineData
* const line
= GetLine(i
);
3853 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3854 delete n
->GetData();
3855 line
->m_items
.Erase(n
);
3859 if ( InReportView() ) // we only cache max widths when in Report View
3861 delete m_aColWidths
.Item(col
);
3862 m_aColWidths
.RemoveAt(col
);
3865 // invalidate it as it has to be recalculated
3869 void wxListMainWindow::DoDeleteAllItems()
3872 // nothing to do - in particular, don't send the event
3877 // to make the deletion of all items faster, we don't send the
3878 // notifications for each item deletion in this case but only one event
3879 // for all of them: this is compatible with wxMSW and documented in
3880 // DeleteAllItems() description
3882 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3883 event
.SetEventObject( GetParent() );
3884 GetParent()->GetEventHandler()->ProcessEvent( event
);
3892 if ( InReportView() )
3894 ResetVisibleLinesRange();
3895 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3897 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3904 void wxListMainWindow::DeleteAllItems()
3908 RecalculatePositions();
3911 void wxListMainWindow::DeleteEverything()
3913 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3914 WX_CLEAR_ARRAY(m_aColWidths
);
3919 // ----------------------------------------------------------------------------
3920 // scanning for an item
3921 // ----------------------------------------------------------------------------
3923 void wxListMainWindow::EnsureVisible( long index
)
3925 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3926 _T("invalid index in EnsureVisible") );
3928 // We have to call this here because the label in question might just have
3929 // been added and its position is not known yet
3931 RecalculatePositions(true /* no refresh */);
3933 MoveToItem((size_t)index
);
3936 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3942 wxString str_upper
= str
.Upper();
3946 size_t count
= GetItemCount();
3947 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3949 wxListLineData
*line
= GetLine(i
);
3950 wxString line_upper
= line
->GetText(0).Upper();
3953 if (line_upper
== str_upper
)
3958 if (line_upper
.find(str_upper
) == 0)
3966 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3972 size_t count
= GetItemCount();
3973 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3975 wxListLineData
*line
= GetLine(i
);
3977 line
->GetItem( 0, item
);
3978 if (item
.m_data
== data
)
3985 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3988 GetVisibleLinesRange( &topItem
, NULL
);
3991 GetItemPosition( GetItemCount() - 1, p
);
3995 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3996 if ( id
>= 0 && id
< (long)GetItemCount() )
4002 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4004 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
4006 size_t count
= GetItemCount();
4008 if ( InReportView() )
4010 size_t current
= y
/ GetLineHeight();
4011 if ( current
< count
)
4013 flags
= HitTestLine(current
, x
, y
);
4020 // TODO: optimize it too! this is less simple than for report view but
4021 // enumerating all items is still not a way to do it!!
4022 for ( size_t current
= 0; current
< count
; current
++ )
4024 flags
= HitTestLine(current
, x
, y
);
4033 // ----------------------------------------------------------------------------
4035 // ----------------------------------------------------------------------------
4037 void wxListMainWindow::InsertItem( wxListItem
&item
)
4039 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4041 int count
= GetItemCount();
4042 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4044 if (item
.m_itemId
> count
)
4045 item
.m_itemId
= count
;
4047 size_t id
= item
.m_itemId
;
4051 if ( InReportView() )
4053 ResetVisibleLinesRange();
4055 // calculate the width of the item and adjust the max column width
4056 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4057 int width
= GetItemWidthWithImage(&item
);
4058 item
.SetWidth(width
);
4059 if (width
> pWidthInfo
->nMaxWidth
)
4060 pWidthInfo
->nMaxWidth
= width
;
4063 wxListLineData
*line
= new wxListLineData(this);
4065 line
->SetItem( item
.m_col
, item
);
4067 m_lines
.Insert( line
, id
);
4071 // If an item is selected at or below the point of insertion, we need to
4072 // increment the member variables because the current row's index has gone
4074 if ( HasCurrent() && m_current
>= id
)
4077 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4079 RefreshLines(id
, GetItemCount() - 1);
4082 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4085 if ( InReportView() )
4087 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4088 item
.m_width
= GetTextLength( item
.m_text
);
4090 wxListHeaderData
*column
= new wxListHeaderData( item
);
4091 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4093 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4096 wxListHeaderDataList::compatibility_iterator
4097 node
= m_columns
.Item( col
);
4098 m_columns
.Insert( node
, column
);
4099 m_aColWidths
.Insert( colWidthInfo
, col
);
4103 m_columns
.Append( column
);
4104 m_aColWidths
.Add( colWidthInfo
);
4109 // update all the items
4110 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4112 wxListLineData
* const line
= GetLine(i
);
4113 wxListItemData
* const data
= new wxListItemData(this);
4115 line
->m_items
.Insert(col
, data
);
4117 line
->m_items
.Append(data
);
4121 // invalidate it as it has to be recalculated
4126 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4129 wxClientDC
dc(this);
4131 dc
.SetFont( GetFont() );
4133 if (item
->GetImage() != -1)
4136 GetImageSize( item
->GetImage(), ix
, iy
);
4140 if (!item
->GetText().empty())
4143 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4150 // ----------------------------------------------------------------------------
4152 // ----------------------------------------------------------------------------
4154 static wxListCtrlCompare list_ctrl_compare_func_2
;
4155 static wxIntPtr list_ctrl_compare_data
;
4157 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4159 wxListLineData
*line1
= *arg1
;
4160 wxListLineData
*line2
= *arg2
;
4162 line1
->GetItem( 0, item
);
4163 wxUIntPtr data1
= item
.m_data
;
4164 line2
->GetItem( 0, item
);
4165 wxUIntPtr data2
= item
.m_data
;
4166 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4169 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4171 // selections won't make sense any more after sorting the items so reset
4173 HighlightAll(false);
4176 list_ctrl_compare_func_2
= fn
;
4177 list_ctrl_compare_data
= data
;
4178 m_lines
.Sort( list_ctrl_compare_func_1
);
4182 // ----------------------------------------------------------------------------
4184 // ----------------------------------------------------------------------------
4186 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4188 // update our idea of which lines are shown when we redraw the window the
4190 ResetVisibleLinesRange();
4192 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4194 wxGenericListCtrl
* lc
= GetListCtrl();
4195 wxCHECK_RET( lc
, _T("no listctrl window?") );
4197 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4199 lc
->m_headerWin
->Refresh();
4200 lc
->m_headerWin
->Update();
4205 int wxListMainWindow::GetCountPerPage() const
4207 if ( !m_linesPerPage
)
4209 wxConstCast(this, wxListMainWindow
)->
4210 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4213 return m_linesPerPage
;
4216 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4218 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4220 if ( m_lineFrom
== (size_t)-1 )
4222 size_t count
= GetItemCount();
4225 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4227 // this may happen if SetScrollbars() hadn't been called yet
4228 if ( m_lineFrom
>= count
)
4229 m_lineFrom
= count
- 1;
4231 // we redraw one extra line but this is needed to make the redrawing
4232 // logic work when there is a fractional number of lines on screen
4233 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4234 if ( m_lineTo
>= count
)
4235 m_lineTo
= count
- 1;
4237 else // empty control
4240 m_lineTo
= (size_t)-1;
4244 wxASSERT_MSG( IsEmpty() ||
4245 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4246 _T("GetVisibleLinesRange() returns incorrect result") );
4254 // -------------------------------------------------------------------------------------
4255 // wxGenericListCtrl
4256 // -------------------------------------------------------------------------------------
4258 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4260 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4261 EVT_SIZE(wxGenericListCtrl::OnSize
)
4262 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4265 void wxGenericListCtrl::Init()
4267 m_imageListNormal
= NULL
;
4268 m_imageListSmall
= NULL
;
4269 m_imageListState
= NULL
;
4271 m_ownsImageListNormal
=
4272 m_ownsImageListSmall
=
4273 m_ownsImageListState
= false;
4277 m_headerHeight
= wxRendererNative::Get().GetHeaderButtonHeight(this);
4280 wxGenericListCtrl::~wxGenericListCtrl()
4282 if (m_ownsImageListNormal
)
4283 delete m_imageListNormal
;
4284 if (m_ownsImageListSmall
)
4285 delete m_imageListSmall
;
4286 if (m_ownsImageListState
)
4287 delete m_imageListState
;
4290 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4292 bool needs_header
= HasHeader();
4293 bool has_header
= (m_headerWin
!= NULL
);
4295 if (needs_header
== has_header
)
4300 m_headerWin
= new wxListHeaderWindow
4302 this, wxID_ANY
, m_mainWin
,
4304 wxSize(GetClientSize().x
, m_headerHeight
),
4308 #if defined( __WXMAC__ )
4310 font
.CreateSystemFont( wxOSX_SYSTEM_FONT_SMALL
);
4311 m_headerWin
->SetFont( font
);
4314 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4318 GetSizer()->Detach( m_headerWin
);
4326 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4331 const wxValidator
&validator
,
4332 const wxString
&name
)
4336 // just like in other ports, an assert will fail if the user doesn't give any type style:
4337 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4338 _T("wxListCtrl style should have exactly one mode bit set") );
4340 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4344 style
&= ~wxBORDER_MASK
;
4345 style
|= wxBORDER_THEME
;
4348 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4350 SetTargetWindow( m_mainWin
);
4352 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4353 sizer
->Add( m_mainWin
, 1, wxGROW
);
4356 CreateOrDestroyHeaderWindowAsNeeded();
4358 SetInitialSize(size
);
4363 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4365 return wxBORDER_THEME
;
4368 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4369 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4373 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4375 // we need to process arrows ourselves for scrolling
4376 if ( nMsg
== WM_GETDLGCODE
)
4378 rc
|= DLGC_WANTARROWS
;
4385 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4387 wxSize newsize
= size
;
4389 newsize
.y
-= m_headerWin
->GetSize().y
;
4394 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4396 // update our idea of which lines are shown when we redraw
4397 // the window the next time
4398 m_mainWin
->ResetVisibleLinesRange();
4400 HandleOnScroll( event
);
4402 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4404 m_headerWin
->Refresh();
4405 m_headerWin
->Update();
4409 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4411 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4412 _T("wxLC_VIRTUAL can't be [un]set") );
4414 long flag
= GetWindowStyle();
4418 if (style
& wxLC_MASK_TYPE
)
4419 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4420 if (style
& wxLC_MASK_ALIGN
)
4421 flag
&= ~wxLC_MASK_ALIGN
;
4422 if (style
& wxLC_MASK_SORT
)
4423 flag
&= ~wxLC_MASK_SORT
;
4431 // some styles can be set without recreating everything (as happens in
4432 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4433 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4436 wxWindow::SetWindowStyleFlag(flag
);
4440 SetWindowStyleFlag( flag
);
4444 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4448 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4450 CreateOrDestroyHeaderWindowAsNeeded();
4452 GetSizer()->Layout();
4455 wxWindow::SetWindowStyleFlag( flag
);
4458 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4460 m_mainWin
->GetColumn( col
, item
);
4464 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4466 m_mainWin
->SetColumn( col
, item
);
4470 int wxGenericListCtrl::GetColumnWidth( int col
) const
4472 return m_mainWin
->GetColumnWidth( col
);
4475 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4477 m_mainWin
->SetColumnWidth( col
, width
);
4481 int wxGenericListCtrl::GetCountPerPage() const
4483 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4486 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4488 m_mainWin
->GetItem( info
);
4492 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4494 m_mainWin
->SetItem( info
);
4498 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4501 info
.m_text
= label
;
4502 info
.m_mask
= wxLIST_MASK_TEXT
;
4503 info
.m_itemId
= index
;
4507 info
.m_image
= imageId
;
4508 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4511 m_mainWin
->SetItem(info
);
4515 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4517 return m_mainWin
->GetItemState( item
, stateMask
);
4520 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4522 m_mainWin
->SetItemState( item
, state
, stateMask
);
4527 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4529 return SetItemColumnImage(item
, 0, image
);
4533 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4536 info
.m_image
= image
;
4537 info
.m_mask
= wxLIST_MASK_IMAGE
;
4538 info
.m_itemId
= item
;
4539 info
.m_col
= column
;
4540 m_mainWin
->SetItem( info
);
4544 wxString
wxGenericListCtrl::GetItemText( long item
) const
4546 return m_mainWin
->GetItemText(item
);
4549 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4551 m_mainWin
->SetItemText(item
, str
);
4554 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4557 info
.m_mask
= wxLIST_MASK_DATA
;
4558 info
.m_itemId
= item
;
4559 m_mainWin
->GetItem( info
);
4563 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4566 info
.m_mask
= wxLIST_MASK_DATA
;
4567 info
.m_itemId
= item
;
4569 m_mainWin
->SetItem( info
);
4573 wxRect
wxGenericListCtrl::GetViewRect() const
4575 return m_mainWin
->GetViewRect();
4578 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4580 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4583 bool wxGenericListCtrl::GetSubItemRect(long item
,
4586 int WXUNUSED(code
)) const
4588 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4591 if ( m_mainWin
->HasHeader() )
4592 rect
.y
+= m_headerHeight
+ 1;
4597 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4599 m_mainWin
->GetItemPosition( item
, pos
);
4603 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4608 int wxGenericListCtrl::GetItemCount() const
4610 return m_mainWin
->GetItemCount();
4613 int wxGenericListCtrl::GetColumnCount() const
4615 return m_mainWin
->GetColumnCount();
4618 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4620 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4623 wxSize
wxGenericListCtrl::GetItemSpacing() const
4625 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4627 return wxSize(spacing
, spacing
);
4630 #if WXWIN_COMPATIBILITY_2_6
4631 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4633 return m_mainWin
->GetItemSpacing( isSmall
);
4635 #endif // WXWIN_COMPATIBILITY_2_6
4637 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4640 info
.m_itemId
= item
;
4641 info
.SetTextColour( col
);
4642 m_mainWin
->SetItem( info
);
4645 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4648 info
.m_itemId
= item
;
4649 m_mainWin
->GetItem( info
);
4650 return info
.GetTextColour();
4653 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4656 info
.m_itemId
= item
;
4657 info
.SetBackgroundColour( col
);
4658 m_mainWin
->SetItem( info
);
4661 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4664 info
.m_itemId
= item
;
4665 m_mainWin
->GetItem( info
);
4666 return info
.GetBackgroundColour();
4669 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4672 info
.m_itemId
= item
;
4674 m_mainWin
->SetItem( info
);
4677 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4680 info
.m_itemId
= item
;
4681 m_mainWin
->GetItem( info
);
4682 return info
.GetFont();
4685 int wxGenericListCtrl::GetSelectedItemCount() const
4687 return m_mainWin
->GetSelectedItemCount();
4690 wxColour
wxGenericListCtrl::GetTextColour() const
4692 return GetForegroundColour();
4695 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4697 SetForegroundColour(col
);
4700 long wxGenericListCtrl::GetTopItem() const
4703 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4707 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4709 return m_mainWin
->GetNextItem( item
, geom
, state
);
4712 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4714 if (which
== wxIMAGE_LIST_NORMAL
)
4715 return m_imageListNormal
;
4716 else if (which
== wxIMAGE_LIST_SMALL
)
4717 return m_imageListSmall
;
4718 else if (which
== wxIMAGE_LIST_STATE
)
4719 return m_imageListState
;
4724 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4726 if ( which
== wxIMAGE_LIST_NORMAL
)
4728 if (m_ownsImageListNormal
)
4729 delete m_imageListNormal
;
4730 m_imageListNormal
= imageList
;
4731 m_ownsImageListNormal
= false;
4733 else if ( which
== wxIMAGE_LIST_SMALL
)
4735 if (m_ownsImageListSmall
)
4736 delete m_imageListSmall
;
4737 m_imageListSmall
= imageList
;
4738 m_ownsImageListSmall
= false;
4740 else if ( which
== wxIMAGE_LIST_STATE
)
4742 if (m_ownsImageListState
)
4743 delete m_imageListState
;
4744 m_imageListState
= imageList
;
4745 m_ownsImageListState
= false;
4748 m_mainWin
->SetImageList( imageList
, which
);
4751 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4753 SetImageList(imageList
, which
);
4754 if ( which
== wxIMAGE_LIST_NORMAL
)
4755 m_ownsImageListNormal
= true;
4756 else if ( which
== wxIMAGE_LIST_SMALL
)
4757 m_ownsImageListSmall
= true;
4758 else if ( which
== wxIMAGE_LIST_STATE
)
4759 m_ownsImageListState
= true;
4762 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4767 bool wxGenericListCtrl::DeleteItem( long item
)
4769 m_mainWin
->DeleteItem( item
);
4773 bool wxGenericListCtrl::DeleteAllItems()
4775 m_mainWin
->DeleteAllItems();
4779 bool wxGenericListCtrl::DeleteAllColumns()
4781 size_t count
= m_mainWin
->m_columns
.GetCount();
4782 for ( size_t n
= 0; n
< count
; n
++ )
4787 void wxGenericListCtrl::ClearAll()
4789 m_mainWin
->DeleteEverything();
4792 bool wxGenericListCtrl::DeleteColumn( int col
)
4794 m_mainWin
->DeleteColumn( col
);
4796 // if we don't have the header any longer, we need to relayout the window
4797 // if ( !GetColumnCount() )
4802 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4803 wxClassInfo
* textControlClass
)
4805 return m_mainWin
->EditLabel( item
, textControlClass
);
4808 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4810 return m_mainWin
->GetEditControl();
4813 bool wxGenericListCtrl::EnsureVisible( long item
)
4815 m_mainWin
->EnsureVisible( item
);
4819 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4821 return m_mainWin
->FindItem( start
, str
, partial
);
4824 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4826 return m_mainWin
->FindItem( start
, data
);
4829 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4830 int WXUNUSED(direction
))
4832 return m_mainWin
->FindItem( pt
);
4835 // TODO: sub item hit testing
4836 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4838 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4841 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4843 m_mainWin
->InsertItem( info
);
4844 return info
.m_itemId
;
4847 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4850 info
.m_text
= label
;
4851 info
.m_mask
= wxLIST_MASK_TEXT
;
4852 info
.m_itemId
= index
;
4853 return InsertItem( info
);
4856 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4859 info
.m_mask
= wxLIST_MASK_IMAGE
;
4860 info
.m_image
= imageIndex
;
4861 info
.m_itemId
= index
;
4862 return InsertItem( info
);
4865 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4868 info
.m_text
= label
;
4869 info
.m_image
= imageIndex
;
4870 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4871 info
.m_itemId
= index
;
4872 return InsertItem( info
);
4875 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4877 wxCHECK_MSG( InReportView(), -1, _T("can't add column in non report mode") );
4879 m_mainWin
->InsertColumn( col
, item
);
4881 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4882 // still have m_headerWin==NULL
4884 m_headerWin
->Refresh();
4889 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4890 int format
, int width
)
4893 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4894 item
.m_text
= heading
;
4897 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4898 item
.m_width
= width
;
4901 item
.m_format
= format
;
4903 return InsertColumn( col
, item
);
4906 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4908 return m_mainWin
->ScrollList(dx
, dy
);
4912 // fn is a function which takes 3 long arguments: item1, item2, data.
4913 // item1 is the long data associated with a first item (NOT the index).
4914 // item2 is the long data associated with a second item (NOT the index).
4915 // data is the same value as passed to SortItems.
4916 // The return value is a negative number if the first item should precede the second
4917 // item, a positive number of the second item should precede the first,
4918 // or zero if the two items are equivalent.
4919 // data is arbitrary data to be passed to the sort function.
4921 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4923 m_mainWin
->SortItems( fn
, data
);
4927 // ----------------------------------------------------------------------------
4929 // ----------------------------------------------------------------------------
4931 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4933 if (!m_mainWin
) return;
4935 // We need to override OnSize so that our scrolled
4936 // window a) does call Layout() to use sizers for
4937 // positioning the controls but b) does not query
4938 // the sizer for their size and use that for setting
4939 // the scrollable area as set that ourselves by
4940 // calling SetScrollbar() further down.
4944 m_mainWin
->RecalculatePositions();
4949 void wxGenericListCtrl::OnInternalIdle()
4951 wxWindow::OnInternalIdle();
4953 if (m_mainWin
->m_dirty
)
4954 m_mainWin
->RecalculatePositions();
4957 // ----------------------------------------------------------------------------
4959 // ----------------------------------------------------------------------------
4961 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4965 m_mainWin
->SetBackgroundColour( colour
);
4966 m_mainWin
->m_dirty
= true;
4972 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4974 if ( !wxWindow::SetForegroundColour( colour
) )
4979 m_mainWin
->SetForegroundColour( colour
);
4980 m_mainWin
->m_dirty
= true;
4984 m_headerWin
->SetForegroundColour( colour
);
4989 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4991 if ( !wxWindow::SetFont( font
) )
4996 m_mainWin
->SetFont( font
);
4997 m_mainWin
->m_dirty
= true;
5002 m_headerWin
->SetFont( font
);
5003 // CalculateAndSetHeaderHeight();
5013 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5016 // Use the same color scheme as wxListBox
5017 return wxListBox::GetClassDefaultAttributes(variant
);
5019 wxUnusedVar(variant
);
5020 wxVisualAttributes attr
;
5021 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5022 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5023 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5028 // ----------------------------------------------------------------------------
5029 // methods forwarded to m_mainWin
5030 // ----------------------------------------------------------------------------
5032 #if wxUSE_DRAG_AND_DROP
5034 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5036 m_mainWin
->SetDropTarget( dropTarget
);
5039 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5041 return m_mainWin
->GetDropTarget();
5046 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5048 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5051 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5053 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5056 wxColour
wxGenericListCtrl::GetForegroundColour() const
5058 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5061 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5064 return m_mainWin
->PopupMenu( menu
, x
, y
);
5070 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5072 m_mainWin
->DoClientToScreen(x
, y
);
5075 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5077 m_mainWin
->DoScreenToClient(x
, y
);
5080 void wxGenericListCtrl::SetFocus()
5082 // The test in window.cpp fails as we are a composite
5083 // window, so it checks against "this", but not m_mainWin.
5084 if ( DoFindFocus() != this )
5085 m_mainWin
->SetFocus();
5088 wxSize
wxGenericListCtrl::DoGetBestSize() const
5090 // Something is better than nothing...
5091 // 100x80 is what the MSW version will get from the default
5092 // wxControl::DoGetBestSize
5093 return wxSize(100, 80);
5096 // ----------------------------------------------------------------------------
5097 // virtual list control support
5098 // ----------------------------------------------------------------------------
5100 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5102 // this is a pure virtual function, in fact - which is not really pure
5103 // because the controls which are not virtual don't need to implement it
5104 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5106 return wxEmptyString
;
5109 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5111 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5113 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5117 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5120 return OnGetItemImage(item
);
5126 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5128 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5129 _T("invalid item index in OnGetItemAttr()") );
5131 // no attributes by default
5135 void wxGenericListCtrl::SetItemCount(long count
)
5137 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5139 m_mainWin
->SetItemCount(count
);
5142 void wxGenericListCtrl::RefreshItem(long item
)
5144 m_mainWin
->RefreshLine(item
);
5147 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5149 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5152 // Generic wxListCtrl is more or less a container for two other
5153 // windows which drawings are done upon. These are namely
5154 // 'm_headerWin' and 'm_mainWin'.
5155 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5156 // behaviour wxListCtrl has under wxMSW.
5158 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5162 // The easy case, no rectangle specified.
5164 m_headerWin
->Refresh(eraseBackground
);
5167 m_mainWin
->Refresh(eraseBackground
);
5171 // Refresh the header window
5174 wxRect rectHeader
= m_headerWin
->GetRect();
5175 rectHeader
.Intersect(*rect
);
5176 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5179 m_headerWin
->GetPosition(&x
, &y
);
5180 rectHeader
.Offset(-x
, -y
);
5181 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5185 // Refresh the main window
5188 wxRect rectMain
= m_mainWin
->GetRect();
5189 rectMain
.Intersect(*rect
);
5190 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5193 m_mainWin
->GetPosition(&x
, &y
);
5194 rectMain
.Offset(-x
, -y
);
5195 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5201 #endif // wxUSE_LISTCTRL