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"
57 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
58 #define "wx/msw/wrapwin.h"
61 // NOTE: If using the wxListBox visual attributes works everywhere then this can
62 // be removed, as well as the #else case below.
63 #define _USE_VISATTR 0
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // // the height of the header window (FIXME: should depend on its font!)
71 // static const int HEADER_HEIGHT = 23;
73 static const int SCROLL_UNIT_X
= 15;
75 // the spacing between the lines (in report mode)
76 static const int LINE_SPACING
= 0;
78 // extra margins around the text label
80 static const int EXTRA_WIDTH
= 6;
82 static const int EXTRA_WIDTH
= 4;
86 static const int EXTRA_HEIGHT
= 6;
88 static const int EXTRA_HEIGHT
= 4;
91 // margin between the window and the items
92 static const int EXTRA_BORDER_X
= 2;
93 static const int EXTRA_BORDER_Y
= 2;
95 // offset for the header window
96 static const int HEADER_OFFSET_X
= 0;
97 static const int HEADER_OFFSET_Y
= 0;
99 // margin between rows of icons in [small] icon view
100 static const int MARGIN_BETWEEN_ROWS
= 6;
102 // when autosizing the columns, add some slack
103 static const int AUTOSIZE_COL_MARGIN
= 10;
105 // default width for the header columns
106 static const int WIDTH_COL_DEFAULT
= 80;
108 // the space between the image and the text in the report mode
109 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
111 // the space between the image and the text in the report mode in header
112 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
116 // ----------------------------------------------------------------------------
117 // arrays/list implementations
118 // ----------------------------------------------------------------------------
120 #include "wx/listimpl.cpp"
121 WX_DEFINE_LIST(wxListItemDataList
)
123 #include "wx/arrimpl.cpp"
124 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
126 #include "wx/listimpl.cpp"
127 WX_DEFINE_LIST(wxListHeaderDataList
)
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 wxListItemData::~wxListItemData()
136 // in the virtual list control the attributes are managed by the main
137 // program, so don't delete them
138 if ( !m_owner
->IsVirtual() )
144 void wxListItemData::Init()
152 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
158 if ( owner
->InReportView() )
164 void wxListItemData::SetItem( const wxListItem
&info
)
166 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
167 SetText(info
.m_text
);
168 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
169 m_image
= info
.m_image
;
170 if ( info
.m_mask
& wxLIST_MASK_DATA
)
171 m_data
= info
.m_data
;
173 if ( info
.HasAttributes() )
176 m_attr
->AssignFrom(*info
.GetAttributes());
178 m_attr
= new wxListItemAttr(*info
.GetAttributes());
186 m_rect
->width
= info
.m_width
;
190 void wxListItemData::SetPosition( int x
, int y
)
192 wxCHECK_RET( m_rect
, wxT("unexpected SetPosition() call") );
198 void wxListItemData::SetSize( int width
, int height
)
200 wxCHECK_RET( m_rect
, wxT("unexpected SetSize() call") );
203 m_rect
->width
= width
;
205 m_rect
->height
= height
;
208 bool wxListItemData::IsHit( int x
, int y
) const
210 wxCHECK_MSG( m_rect
, false, wxT("can't be called in this mode") );
212 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
215 int wxListItemData::GetX() const
217 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
222 int wxListItemData::GetY() const
224 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
229 int wxListItemData::GetWidth() const
231 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
233 return m_rect
->width
;
236 int wxListItemData::GetHeight() const
238 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
240 return m_rect
->height
;
243 void wxListItemData::GetItem( wxListItem
&info
) const
245 long mask
= info
.m_mask
;
247 // by default, get everything for backwards compatibility
250 if ( mask
& wxLIST_MASK_TEXT
)
251 info
.m_text
= m_text
;
252 if ( mask
& wxLIST_MASK_IMAGE
)
253 info
.m_image
= m_image
;
254 if ( mask
& wxLIST_MASK_DATA
)
255 info
.m_data
= m_data
;
259 if ( m_attr
->HasTextColour() )
260 info
.SetTextColour(m_attr
->GetTextColour());
261 if ( m_attr
->HasBackgroundColour() )
262 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
263 if ( m_attr
->HasFont() )
264 info
.SetFont(m_attr
->GetFont());
268 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
272 void wxListHeaderData::Init()
284 wxListHeaderData::wxListHeaderData()
289 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
296 void wxListHeaderData::SetItem( const wxListItem
&item
)
298 m_mask
= item
.m_mask
;
300 if ( m_mask
& wxLIST_MASK_TEXT
)
301 m_text
= item
.m_text
;
303 if ( m_mask
& wxLIST_MASK_IMAGE
)
304 m_image
= item
.m_image
;
306 if ( m_mask
& wxLIST_MASK_FORMAT
)
307 m_format
= item
.m_format
;
309 if ( m_mask
& wxLIST_MASK_WIDTH
)
310 SetWidth(item
.m_width
);
312 if ( m_mask
& wxLIST_MASK_STATE
)
313 SetState(item
.m_state
);
316 void wxListHeaderData::SetPosition( int x
, int y
)
322 void wxListHeaderData::SetHeight( int h
)
327 void wxListHeaderData::SetWidth( int w
)
329 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
332 void wxListHeaderData::SetState( int flag
)
337 void wxListHeaderData::SetFormat( int format
)
342 bool wxListHeaderData::HasImage() const
344 return m_image
!= -1;
347 bool wxListHeaderData::IsHit( int x
, int y
) const
349 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
352 void wxListHeaderData::GetItem( wxListItem
& item
)
354 item
.m_mask
= m_mask
;
355 item
.m_text
= m_text
;
356 item
.m_image
= m_image
;
357 item
.m_format
= m_format
;
358 item
.m_width
= m_width
;
359 item
.m_state
= m_state
;
362 int wxListHeaderData::GetImage() const
367 int wxListHeaderData::GetWidth() const
372 int wxListHeaderData::GetFormat() const
377 int wxListHeaderData::GetState() const
382 //-----------------------------------------------------------------------------
384 //-----------------------------------------------------------------------------
386 inline int wxListLineData::GetMode() const
388 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
391 inline bool wxListLineData::InReportView() const
393 return m_owner
->HasFlag(wxLC_REPORT
);
396 inline bool wxListLineData::IsVirtual() const
398 return m_owner
->IsVirtual();
401 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
405 if ( InReportView() )
408 m_gi
= new GeometryInfo
;
410 m_highlighted
= false;
412 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
415 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
417 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
418 wxCHECK_RET( node
, wxT("no subitems at all??") );
420 wxListItemData
*item
= node
->GetData();
428 case wxLC_SMALL_ICON
:
429 m_gi
->m_rectAll
.width
= spacing
;
436 m_gi
->m_rectLabel
.width
=
437 m_gi
->m_rectLabel
.height
= 0;
441 dc
->GetTextExtent( s
, &lw
, &lh
);
445 m_gi
->m_rectAll
.height
= spacing
+ lh
;
447 m_gi
->m_rectAll
.width
= lw
;
449 m_gi
->m_rectLabel
.width
= lw
;
450 m_gi
->m_rectLabel
.height
= lh
;
453 if (item
->HasImage())
456 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
457 m_gi
->m_rectIcon
.width
= w
+ 8;
458 m_gi
->m_rectIcon
.height
= h
+ 8;
460 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
461 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
462 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
463 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
466 if ( item
->HasText() )
468 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
469 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
471 else // no text, highlight the icon
473 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
474 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
479 s
= item
->GetTextForMeasuring();
481 dc
->GetTextExtent( s
, &lw
, &lh
);
485 m_gi
->m_rectLabel
.width
= lw
;
486 m_gi
->m_rectLabel
.height
= lh
;
488 m_gi
->m_rectAll
.width
= lw
;
489 m_gi
->m_rectAll
.height
= lh
;
491 if (item
->HasImage())
494 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
495 m_gi
->m_rectIcon
.width
= w
;
496 m_gi
->m_rectIcon
.height
= h
;
498 m_gi
->m_rectAll
.width
+= 4 + w
;
499 if (h
> m_gi
->m_rectAll
.height
)
500 m_gi
->m_rectAll
.height
= h
;
503 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
504 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
508 wxFAIL_MSG( wxT("unexpected call to SetSize") );
512 wxFAIL_MSG( wxT("unknown mode") );
517 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
519 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
520 wxCHECK_RET( node
, wxT("no subitems at all??") );
522 wxListItemData
*item
= node
->GetData();
527 case wxLC_SMALL_ICON
:
528 m_gi
->m_rectAll
.x
= x
;
529 m_gi
->m_rectAll
.y
= y
;
531 if ( item
->HasImage() )
533 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
534 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
535 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
538 if ( item
->HasText() )
540 if (m_gi
->m_rectAll
.width
> spacing
)
541 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
543 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
544 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
545 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
546 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
548 else // no text, highlight the icon
550 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
551 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
556 m_gi
->m_rectAll
.x
= x
;
557 m_gi
->m_rectAll
.y
= y
;
559 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
560 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
561 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
563 if (item
->HasImage())
565 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
566 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
567 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
571 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
576 wxFAIL_MSG( wxT("unexpected call to SetPosition") );
580 wxFAIL_MSG( wxT("unknown mode") );
585 void wxListLineData::InitItems( int num
)
587 for (int i
= 0; i
< num
; i
++)
588 m_items
.Append( new wxListItemData(m_owner
) );
591 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
593 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
594 wxCHECK_RET( node
, wxT("invalid column index in SetItem") );
596 wxListItemData
*item
= node
->GetData();
597 item
->SetItem( info
);
600 void wxListLineData::GetItem( int index
, wxListItem
&info
)
602 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
605 wxListItemData
*item
= node
->GetData();
606 item
->GetItem( info
);
610 wxString
wxListLineData::GetText(int index
) const
614 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
617 wxListItemData
*item
= node
->GetData();
624 void wxListLineData::SetText( int index
, const wxString
& s
)
626 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
629 wxListItemData
*item
= node
->GetData();
634 void wxListLineData::SetImage( int index
, int image
)
636 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
637 wxCHECK_RET( node
, wxT("invalid column index in SetImage()") );
639 wxListItemData
*item
= node
->GetData();
640 item
->SetImage(image
);
643 int wxListLineData::GetImage( int index
) const
645 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
646 wxCHECK_MSG( node
, -1, wxT("invalid column index in GetImage()") );
648 wxListItemData
*item
= node
->GetData();
649 return item
->GetImage();
652 wxListItemAttr
*wxListLineData::GetAttr() const
654 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
655 wxCHECK_MSG( node
, NULL
, wxT("invalid column index in GetAttr()") );
657 wxListItemData
*item
= node
->GetData();
658 return item
->GetAttr();
661 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
663 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
664 wxCHECK_RET( node
, wxT("invalid column index in SetAttr()") );
666 wxListItemData
*item
= node
->GetData();
670 bool wxListLineData::SetAttributes(wxDC
*dc
,
671 const wxListItemAttr
*attr
,
674 wxWindow
*listctrl
= m_owner
->GetParent();
678 // don't use foreground colour for drawing highlighted items - this might
679 // make them completely invisible (and there is no way to do bit
680 // arithmetics on wxColour, unfortunately)
685 if (m_owner
->HasFocus()
686 #if !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
687 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
695 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
697 else if ( attr
&& attr
->HasTextColour() )
698 colText
= attr
->GetTextColour();
700 colText
= listctrl
->GetForegroundColour();
702 dc
->SetTextForeground(colText
);
706 if ( attr
&& attr
->HasFont() )
707 font
= attr
->GetFont();
709 font
= listctrl
->GetFont();
714 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
715 if ( highlighted
|| hasBgCol
)
718 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
720 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
722 dc
->SetPen( *wxTRANSPARENT_PEN
);
730 void wxListLineData::Draw( wxDC
*dc
)
732 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
733 wxCHECK_RET( node
, wxT("no subitems at all??") );
735 bool highlighted
= IsHighlighted();
737 wxListItemAttr
*attr
= GetAttr();
739 if ( SetAttributes(dc
, attr
, highlighted
) )
740 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
742 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
748 int flags
= wxCONTROL_SELECTED
;
749 if (m_owner
->HasFocus()
750 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
751 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
754 flags
|= wxCONTROL_FOCUSED
;
755 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
760 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
765 // just for debugging to better see where the items are
767 dc
->SetPen(*wxRED_PEN
);
768 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
769 dc
->DrawRectangle( m_gi
->m_rectAll
);
770 dc
->SetPen(*wxGREEN_PEN
);
771 dc
->DrawRectangle( m_gi
->m_rectIcon
);
774 wxListItemData
*item
= node
->GetData();
775 if (item
->HasImage())
777 // centre the image inside our rectangle, this looks nicer when items
778 // ae aligned in a row
779 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
781 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
786 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
788 wxDCClipper
clipper(*dc
, rectLabel
);
789 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
793 void wxListLineData::DrawInReportMode( wxDC
*dc
,
795 const wxRect
& rectHL
,
799 // TODO: later we should support setting different attributes for
800 // different columns - to do it, just add "col" argument to
801 // GetAttr() and move these lines into the loop below
802 wxListItemAttr
*attr
= GetAttr();
803 if ( SetAttributes(dc
, attr
, highlighted
) )
804 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
806 dc
->DrawRectangle( rectHL
);
808 wxUnusedVar(current
);
814 int flags
= wxCONTROL_SELECTED
;
815 if (m_owner
->HasFocus())
816 flags
|= wxCONTROL_FOCUSED
;
818 flags
|= wxCONTROL_CURRENT
;
819 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
823 dc
->DrawRectangle( rectHL
);
828 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
829 yMid
= rect
.y
+ rect
.height
/2;
831 // This probably needs to be done
832 // on all platforms as the icons
833 // otherwise nearly touch the border
838 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
840 node
= node
->GetNext(), col
++ )
842 wxListItemData
*item
= node
->GetData();
844 int width
= m_owner
->GetColumnWidth(col
);
848 const int wText
= width
- 8;
849 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
851 if ( item
->HasImage() )
854 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
855 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
857 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
863 if ( item
->HasText() )
864 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
868 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
869 const wxString
& textOrig
,
875 // we don't support displaying multiple lines currently (and neither does
876 // wxMSW FWIW) so just merge all the lines
877 wxString
text(textOrig
);
878 text
.Replace(wxT("\n"), wxT(" "));
881 dc
->GetTextExtent(text
, &w
, &h
);
883 const wxCoord y
= yMid
- (h
+ 1)/2;
885 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
887 // determine if the string can fit inside the current width
890 // it can, draw it using the items alignment
892 m_owner
->GetColumn(col
, item
);
893 switch ( item
.GetAlign() )
895 case wxLIST_FORMAT_LEFT
:
899 case wxLIST_FORMAT_RIGHT
:
903 case wxLIST_FORMAT_CENTER
:
904 x
+= (width
- w
) / 2;
908 wxFAIL_MSG( wxT("unknown list item format") );
912 dc
->DrawText(text
, x
, y
);
914 else // otherwise, truncate and add an ellipsis if possible
916 // determine the base width
917 wxString
ellipsis(wxT("..."));
919 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
921 // continue until we have enough space or only one character left
923 size_t len
= text
.length();
924 wxString drawntext
= text
.Left(len
);
927 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
928 drawntext
.RemoveLast();
931 if (w
+ base_w
<= width
)
935 // if still not enough space, remove ellipsis characters
936 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
938 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
939 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
943 dc
->DrawText(drawntext
, x
, y
);
944 dc
->DrawText(ellipsis
, x
+ w
, y
);
948 bool wxListLineData::Highlight( bool on
)
950 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
952 if ( on
== m_highlighted
)
960 void wxListLineData::ReverseHighlight( void )
962 Highlight(!IsHighlighted());
965 //-----------------------------------------------------------------------------
966 // wxListHeaderWindow
967 //-----------------------------------------------------------------------------
969 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
970 EVT_PAINT (wxListHeaderWindow::OnPaint
)
971 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
972 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
975 void wxListHeaderWindow::Init()
977 m_currentCursor
= NULL
;
978 m_isDragging
= false;
980 m_sendSetColumnWidth
= false;
983 wxListHeaderWindow::wxListHeaderWindow()
988 m_resizeCursor
= NULL
;
991 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
993 wxListMainWindow
*owner
,
997 const wxString
&name
)
998 : wxWindow( win
, id
, pos
, size
, style
, name
)
1003 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1006 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1007 SetOwnForegroundColour( attr
.colFg
);
1008 SetOwnBackgroundColour( attr
.colBg
);
1010 SetOwnFont( attr
.font
);
1012 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1013 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1015 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1019 wxListHeaderWindow::~wxListHeaderWindow()
1021 delete m_resizeCursor
;
1024 #ifdef __WXUNIVERSAL__
1025 #include "wx/univ/renderer.h"
1026 #include "wx/univ/theme.h"
1029 // shift the DC origin to match the position of the main window horz
1030 // scrollbar: this allows us to always use logical coords
1031 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1033 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1036 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1039 parent
->GetViewStart( &view_start
, NULL
);
1044 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1046 // account for the horz scrollbar offset
1048 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1050 // Maybe we just have to check for m_signX
1051 // in the DC, but I leave the #ifdef __WXGTK__
1053 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1057 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1060 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1062 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1064 wxPaintDC
dc( this );
1068 dc
.SetFont( GetFont() );
1070 // width and height of the entire header window
1072 GetClientSize( &w
, &h
);
1073 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1075 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1076 dc
.SetTextForeground(GetForegroundColour());
1078 int x
= HEADER_OFFSET_X
;
1079 int numColumns
= m_owner
->GetColumnCount();
1081 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1083 m_owner
->GetColumn( i
, item
);
1084 int wCol
= item
.m_width
;
1090 if (!m_parent
->IsEnabled())
1091 flags
|= wxCONTROL_DISABLED
;
1093 // NB: The code below is not really Mac-specific, but since we are close
1094 // to 2.8 release and I don't have time to test on other platforms, I
1095 // defined this only for wxMac. If this behavior is desired on
1096 // other platforms, please go ahead and revise or remove the #ifdef.
1098 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1099 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1100 flags
|= wxCONTROL_SELECTED
;
1104 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1106 wxRendererNative::Get().DrawHeaderButton
1110 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1114 // see if we have enough space for the column label
1116 // for this we need the width of the text
1119 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1120 wLabel
+= 2 * EXTRA_WIDTH
;
1122 // and the width of the icon, if any
1123 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1124 const int image
= item
.m_image
;
1125 wxImageList
*imageList
;
1128 imageList
= m_owner
->GetSmallImageList();
1131 imageList
->GetSize(image
, ix
, iy
);
1132 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1140 // ignore alignment if there is not enough space anyhow
1142 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1145 wxFAIL_MSG( wxT("unknown list item format") );
1148 case wxLIST_FORMAT_LEFT
:
1152 case wxLIST_FORMAT_RIGHT
:
1153 xAligned
= x
+ cw
- wLabel
;
1156 case wxLIST_FORMAT_CENTER
:
1157 xAligned
= x
+ (cw
- wLabel
) / 2;
1161 // draw the text and image clipping them so that they
1162 // don't overwrite the column boundary
1163 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
);
1165 // if we have an image, draw it on the right of the label
1172 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1173 HEADER_OFFSET_Y
+ (h
- iy
)/2,
1174 wxIMAGELIST_DRAW_TRANSPARENT
1178 dc
.DrawText( item
.GetText(),
1179 xAligned
+ EXTRA_WIDTH
, (h
- hLabel
) / 2 );
1184 // Fill in what's missing to the right of the columns, otherwise we will
1185 // leave an unpainted area when columns are removed (and it looks better)
1188 wxRendererNative::Get().DrawHeaderButton
1192 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1193 wxCONTROL_DIRTY
// mark as last column
1198 void wxListHeaderWindow::OnInternalIdle()
1200 wxWindow::OnInternalIdle();
1202 if (m_sendSetColumnWidth
)
1204 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1205 m_sendSetColumnWidth
= false;
1209 void wxListHeaderWindow::DrawCurrent()
1212 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1213 m_sendSetColumnWidth
= true;
1214 m_colToSend
= m_column
;
1215 m_widthToSend
= m_currentX
- m_minX
;
1217 int x1
= m_currentX
;
1219 m_owner
->ClientToScreen( &x1
, &y1
);
1221 int x2
= m_currentX
;
1223 m_owner
->GetClientSize( NULL
, &y2
);
1224 m_owner
->ClientToScreen( &x2
, &y2
);
1227 dc
.SetLogicalFunction( wxINVERT
);
1228 dc
.SetPen( wxPen(*wxBLACK
, 2) );
1229 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1233 dc
.DrawLine( x1
, y1
, x2
, y2
);
1235 dc
.SetLogicalFunction( wxCOPY
);
1237 dc
.SetPen( wxNullPen
);
1238 dc
.SetBrush( wxNullBrush
);
1242 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1244 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1246 // we want to work with logical coords
1248 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1249 int y
= event
.GetY();
1253 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1255 // we don't draw the line beyond our window, but we allow dragging it
1258 GetClientSize( &w
, NULL
);
1259 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1262 // erase the line if it was drawn
1263 if ( m_currentX
< w
)
1266 if (event
.ButtonUp())
1269 m_isDragging
= false;
1271 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1272 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1279 m_currentX
= m_minX
+ 7;
1281 // draw in the new location
1282 if ( m_currentX
< w
)
1286 else // not dragging
1289 bool hit_border
= false;
1291 // end of the current column
1294 // find the column where this event occurred
1296 countCol
= m_owner
->GetColumnCount();
1297 for (col
= 0; col
< countCol
; col
++)
1299 xpos
+= m_owner
->GetColumnWidth( col
);
1302 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1304 // near the column border
1311 // inside the column
1318 if ( col
== countCol
)
1321 if (event
.LeftDown() || event
.RightUp())
1323 if (hit_border
&& event
.LeftDown())
1325 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1326 event
.GetPosition()) )
1328 m_isDragging
= true;
1333 //else: column resizing was vetoed by the user code
1335 else // click on a column
1337 // record the selected state of the columns
1338 if (event
.LeftDown())
1340 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
1343 m_owner
->GetColumn(i
, colItem
);
1344 long state
= colItem
.GetState();
1346 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
1348 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
1349 m_owner
->SetColumn(i
, colItem
);
1353 SendListEvent( event
.LeftDown()
1354 ? wxEVT_COMMAND_LIST_COL_CLICK
1355 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1356 event
.GetPosition());
1359 else if (event
.Moving())
1364 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1365 m_currentCursor
= m_resizeCursor
;
1369 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1370 m_currentCursor
= wxSTANDARD_CURSOR
;
1374 SetCursor(*m_currentCursor
);
1379 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1381 m_owner
->SetFocus();
1385 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1387 wxWindow
*parent
= GetParent();
1388 wxListEvent
le( type
, parent
->GetId() );
1389 le
.SetEventObject( parent
);
1390 le
.m_pointDrag
= pos
;
1392 // the position should be relative to the parent window, not
1393 // this one for compatibility with MSW and common sense: the
1394 // user code doesn't know anything at all about this header
1395 // window, so why should it get positions relative to it?
1396 le
.m_pointDrag
.y
-= GetSize().y
;
1398 le
.m_col
= m_column
;
1399 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1402 //-----------------------------------------------------------------------------
1403 // wxListRenameTimer (internal)
1404 //-----------------------------------------------------------------------------
1406 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1411 void wxListRenameTimer::Notify()
1413 m_owner
->OnRenameTimer();
1416 //-----------------------------------------------------------------------------
1417 // wxListTextCtrlWrapper (internal)
1418 //-----------------------------------------------------------------------------
1420 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
1421 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
1422 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
1423 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
1426 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
1429 : m_startValue(owner
->GetItemText(itemEdit
)),
1430 m_itemEdited(itemEdit
)
1434 m_aboutToFinish
= false;
1436 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1438 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1440 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1441 &rectLabel
.x
, &rectLabel
.y
);
1443 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
1444 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1445 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
1448 m_text
->PushEventHandler(this);
1451 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
1453 m_aboutToFinish
= true;
1455 if ( discardChanges
)
1457 m_owner
->OnRenameCancelled(m_itemEdited
);
1463 // Notify the owner about the changes
1466 // Even if vetoed, close the control (consistent with MSW)
1471 void wxListTextCtrlWrapper::Finish( bool setfocus
)
1473 m_text
->RemoveEventHandler(this);
1474 m_owner
->ResetTextControl( m_text
);
1476 wxPendingDelete
.Append( this );
1479 m_owner
->SetFocus();
1482 bool wxListTextCtrlWrapper::AcceptChanges()
1484 const wxString value
= m_text
->GetValue();
1486 // notice that we should always call OnRenameAccept() to generate the "end
1487 // label editing" event, even if the user hasn't really changed anything
1488 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
1490 // vetoed by the user
1494 // accepted, do rename the item (unless nothing changed)
1495 if ( value
!= m_startValue
)
1496 m_owner
->SetItemText(m_itemEdited
, value
);
1501 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
1503 switch ( event
.m_keyCode
)
1518 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
1520 if (m_aboutToFinish
)
1522 // auto-grow the textctrl:
1523 wxSize parentSize
= m_owner
->GetSize();
1524 wxPoint myPos
= m_text
->GetPosition();
1525 wxSize mySize
= m_text
->GetSize();
1527 m_text
->GetTextExtent(m_text
->GetValue() + wxT("MM"), &sx
, &sy
);
1528 if (myPos
.x
+ sx
> parentSize
.x
)
1529 sx
= parentSize
.x
- myPos
.x
;
1532 m_text
->SetSize(sx
, wxDefaultCoord
);
1538 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
1540 if ( !m_aboutToFinish
)
1542 if ( !AcceptChanges() )
1543 m_owner
->OnRenameCancelled( m_itemEdited
);
1548 // We must let the native text control handle focus
1552 //-----------------------------------------------------------------------------
1554 //-----------------------------------------------------------------------------
1556 BEGIN_EVENT_TABLE(wxListMainWindow
, wxWindow
)
1557 EVT_PAINT (wxListMainWindow::OnPaint
)
1558 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1559 EVT_CHAR (wxListMainWindow::OnChar
)
1560 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1561 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
1562 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1563 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1564 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1565 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
1568 void wxListMainWindow::Init()
1573 m_lineTo
= (size_t)-1;
1579 m_small_image_list
= NULL
;
1580 m_normal_image_list
= NULL
;
1582 m_small_spacing
= 30;
1583 m_normal_spacing
= 40;
1587 m_isCreated
= false;
1589 m_lastOnSame
= false;
1590 m_renameTimer
= new wxListRenameTimer( this );
1591 m_textctrlWrapper
= NULL
;
1595 m_lineSelectSingleOnUp
=
1596 m_lineBeforeLastClicked
= (size_t)-1;
1599 wxListMainWindow::wxListMainWindow()
1604 m_highlightUnfocusedBrush
= NULL
;
1607 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1612 const wxString
&name
)
1613 : wxWindow( parent
, id
, pos
, size
, style
, name
)
1617 m_highlightBrush
= new wxBrush
1619 wxSystemSettings::GetColour
1621 wxSYS_COLOUR_HIGHLIGHT
1626 m_highlightUnfocusedBrush
= new wxBrush
1628 wxSystemSettings::GetColour
1630 wxSYS_COLOUR_BTNSHADOW
1635 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
1636 SetOwnForegroundColour( attr
.colFg
);
1637 SetOwnBackgroundColour( attr
.colBg
);
1639 SetOwnFont( attr
.font
);
1642 wxListMainWindow::~wxListMainWindow()
1645 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
1646 WX_CLEAR_ARRAY(m_aColWidths
);
1648 delete m_highlightBrush
;
1649 delete m_highlightUnfocusedBrush
;
1650 delete m_renameTimer
;
1653 void wxListMainWindow::SetReportView(bool inReportView
)
1655 const size_t count
= m_lines
.size();
1656 for ( size_t n
= 0; n
< count
; n
++ )
1658 m_lines
[n
].SetReportView(inReportView
);
1662 void wxListMainWindow::CacheLineData(size_t line
)
1664 wxGenericListCtrl
*listctrl
= GetListCtrl();
1666 wxListLineData
*ld
= GetDummyLine();
1668 size_t countCol
= GetColumnCount();
1669 for ( size_t col
= 0; col
< countCol
; col
++ )
1671 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1672 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1675 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1678 wxListLineData
*wxListMainWindow::GetDummyLine() const
1680 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1681 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1683 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1685 // we need to recreate the dummy line if the number of columns in the
1686 // control changed as it would have the incorrect number of fields
1688 if ( !m_lines
.IsEmpty() &&
1689 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1691 self
->m_lines
.Clear();
1694 if ( m_lines
.IsEmpty() )
1696 wxListLineData
*line
= new wxListLineData(self
);
1697 self
->m_lines
.Add(line
);
1699 // don't waste extra memory -- there never going to be anything
1700 // else/more in this array
1701 self
->m_lines
.Shrink();
1707 // ----------------------------------------------------------------------------
1708 // line geometry (report mode only)
1709 // ----------------------------------------------------------------------------
1711 wxCoord
wxListMainWindow::GetLineHeight() const
1713 // we cache the line height as calling GetTextExtent() is slow
1714 if ( !m_lineHeight
)
1716 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1718 wxClientDC
dc( self
);
1719 dc
.SetFont( GetFont() );
1722 dc
.GetTextExtent(wxT("H"), NULL
, &y
);
1724 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1727 m_small_image_list
->GetSize(0, iw
, ih
);
1732 self
->m_lineHeight
= y
+ LINE_SPACING
;
1735 return m_lineHeight
;
1738 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1740 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1742 return LINE_SPACING
+ line
* GetLineHeight();
1745 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1747 if ( !InReportView() )
1748 return GetLine(line
)->m_gi
->m_rectAll
;
1751 rect
.x
= HEADER_OFFSET_X
;
1752 rect
.y
= GetLineY(line
);
1753 rect
.width
= GetHeaderWidth();
1754 rect
.height
= GetLineHeight();
1759 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1761 if ( !InReportView() )
1762 return GetLine(line
)->m_gi
->m_rectLabel
;
1765 wxListLineData
*data
= GetLine(line
);
1766 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1769 wxListItemData
*item
= node
->GetData();
1770 if ( item
->HasImage() )
1773 GetImageSize( item
->GetImage(), ix
, iy
);
1774 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1779 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1780 rect
.y
= GetLineY(line
);
1781 rect
.width
= GetColumnWidth(0) - image_x
;
1782 rect
.height
= GetLineHeight();
1787 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1789 if ( !InReportView() )
1790 return GetLine(line
)->m_gi
->m_rectIcon
;
1792 wxListLineData
*ld
= GetLine(line
);
1793 wxASSERT_MSG( ld
->HasImage(), wxT("should have an image") );
1796 rect
.x
= HEADER_OFFSET_X
;
1797 rect
.y
= GetLineY(line
);
1798 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1803 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1805 return InReportView() ? GetLineRect(line
)
1806 : GetLine(line
)->m_gi
->m_rectHighlight
;
1809 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1811 wxASSERT_MSG( line
< GetItemCount(), wxT("invalid line in HitTestLine") );
1813 wxListLineData
*ld
= GetLine(line
);
1815 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1816 return wxLIST_HITTEST_ONITEMICON
;
1818 // VS: Testing for "ld->HasText() || InReportView()" instead of
1819 // "ld->HasText()" is needed to make empty lines in report view
1821 if ( ld
->HasText() || InReportView() )
1823 wxRect rect
= InReportView() ? GetLineRect(line
)
1824 : GetLineLabelRect(line
);
1826 if ( rect
.Contains(x
, y
) )
1827 return wxLIST_HITTEST_ONITEMLABEL
;
1833 // ----------------------------------------------------------------------------
1834 // highlight (selection) handling
1835 // ----------------------------------------------------------------------------
1837 bool wxListMainWindow::IsHighlighted(size_t line
) const
1841 return m_selStore
.IsSelected(line
);
1845 wxListLineData
*ld
= GetLine(line
);
1846 wxCHECK_MSG( ld
, false, wxT("invalid index in IsHighlighted") );
1848 return ld
->IsHighlighted();
1852 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1858 wxArrayInt linesChanged
;
1859 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1862 // meny items changed state, refresh everything
1863 RefreshLines(lineFrom
, lineTo
);
1865 else // only a few items changed state, refresh only them
1867 size_t count
= linesChanged
.GetCount();
1868 for ( size_t n
= 0; n
< count
; n
++ )
1870 RefreshLine(linesChanged
[n
]);
1874 else // iterate over all items in non report view
1876 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1878 if ( HighlightLine(line
, highlight
) )
1884 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1890 changed
= m_selStore
.SelectItem(line
, highlight
);
1894 wxListLineData
*ld
= GetLine(line
);
1895 wxCHECK_MSG( ld
, false, wxT("invalid index in HighlightLine") );
1897 changed
= ld
->Highlight(highlight
);
1902 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1903 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1909 void wxListMainWindow::RefreshLine( size_t line
)
1911 if ( InReportView() )
1913 size_t visibleFrom
, visibleTo
;
1914 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1916 if ( line
< visibleFrom
|| line
> visibleTo
)
1920 wxRect rect
= GetLineRect(line
);
1922 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1923 RefreshRect( rect
);
1926 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1928 // we suppose that they are ordered by caller
1929 wxASSERT_MSG( lineFrom
<= lineTo
, wxT("indices in disorder") );
1931 wxASSERT_MSG( lineTo
< GetItemCount(), wxT("invalid line range") );
1933 if ( InReportView() )
1935 size_t visibleFrom
, visibleTo
;
1936 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1938 if ( lineFrom
< visibleFrom
)
1939 lineFrom
= visibleFrom
;
1940 if ( lineTo
> visibleTo
)
1945 rect
.y
= GetLineY(lineFrom
);
1946 rect
.width
= GetClientSize().x
;
1947 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1949 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1950 RefreshRect( rect
);
1954 // TODO: this should be optimized...
1955 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1962 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1964 if ( InReportView() )
1966 size_t visibleFrom
, visibleTo
;
1967 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1969 if ( lineFrom
< visibleFrom
)
1970 lineFrom
= visibleFrom
;
1971 else if ( lineFrom
> visibleTo
)
1976 rect
.y
= GetLineY(lineFrom
);
1977 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1979 wxSize size
= GetClientSize();
1980 rect
.width
= size
.x
;
1982 // refresh till the bottom of the window
1983 rect
.height
= size
.y
- rect
.y
;
1985 RefreshRect( rect
);
1989 // TODO: how to do it more efficiently?
1994 void wxListMainWindow::RefreshSelected()
2000 if ( InReportView() )
2002 GetVisibleLinesRange(&from
, &to
);
2007 to
= GetItemCount() - 1;
2010 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2011 RefreshLine(m_current
);
2013 for ( size_t line
= from
; line
<= to
; line
++ )
2015 // NB: the test works as expected even if m_current == -1
2016 if ( line
!= m_current
&& IsHighlighted(line
) )
2021 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2023 // Note: a wxPaintDC must be constructed even if no drawing is
2024 // done (a Windows requirement).
2025 wxPaintDC
dc( this );
2029 // nothing to draw or not the moment to draw it
2034 RecalculatePositions( false );
2036 GetListCtrl()->PrepareDC( dc
);
2039 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2041 dc
.SetFont( GetFont() );
2043 if ( InReportView() )
2045 int lineHeight
= GetLineHeight();
2047 size_t visibleFrom
, visibleTo
;
2048 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2051 int xOrig
= dc
.LogicalToDeviceX( 0 );
2052 int yOrig
= dc
.LogicalToDeviceY( 0 );
2054 // tell the caller cache to cache the data
2057 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2058 GetParent()->GetId());
2059 evCache
.SetEventObject( GetParent() );
2060 evCache
.m_oldItemIndex
= visibleFrom
;
2061 evCache
.m_itemIndex
= visibleTo
;
2062 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2065 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2067 rectLine
= GetLineRect(line
);
2070 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2071 rectLine
.width
, rectLine
.height
) )
2073 // don't redraw unaffected lines to avoid flicker
2077 GetLine(line
)->DrawInReportMode( &dc
,
2079 GetLineHighlightRect(line
),
2080 IsHighlighted(line
),
2081 line
== m_current
);
2084 if ( HasFlag(wxLC_HRULES
) )
2086 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2087 wxSize clientSize
= GetClientSize();
2089 size_t i
= visibleFrom
;
2090 if (i
== 0) i
= 1; // Don't draw the first one
2091 for ( ; i
<= visibleTo
; i
++ )
2094 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2095 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2096 clientSize
.x
- dev_x
, i
* lineHeight
);
2099 // Draw last horizontal rule
2100 if ( visibleTo
== GetItemCount() - 1 )
2103 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2104 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2105 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2109 // Draw vertical rules if required
2110 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2112 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2113 wxRect firstItemRect
, lastItemRect
;
2115 GetItemRect(visibleFrom
, firstItemRect
);
2116 GetItemRect(visibleTo
, lastItemRect
);
2117 int x
= firstItemRect
.GetX();
2119 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2121 for (int col
= 0; col
< GetColumnCount(); col
++)
2123 int colWidth
= GetColumnWidth(col
);
2125 int x_pos
= x
- dev_x
;
2126 if (col
< GetColumnCount()-1) x_pos
-= 2;
2127 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2128 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2134 size_t count
= GetItemCount();
2135 for ( size_t i
= 0; i
< count
; i
++ )
2137 GetLine(i
)->Draw( &dc
);
2141 #if !defined( __WXMAC__) && !defined(__WXGTK20__)
2142 // Don't draw rect outline under Mac at all.
2143 // Draw it elsewhere under GTK.
2148 wxRect
rect( GetLineHighlightRect( m_current
) );
2149 dc
.SetPen( *wxBLACK_PEN
);
2150 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2151 dc
.DrawRectangle( rect
);
2157 void wxListMainWindow::HighlightAll( bool on
)
2159 if ( IsSingleSel() )
2161 wxASSERT_MSG( !on
, wxT("can't do this in a single selection control") );
2163 // we just have one item to turn off
2164 if ( HasCurrent() && IsHighlighted(m_current
) )
2166 HighlightLine(m_current
, false);
2167 RefreshLine(m_current
);
2170 else // multi selection
2173 HighlightLines(0, GetItemCount() - 1, on
);
2177 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2179 // Do nothing here. This prevents the default handler in wxScrolledWindow
2180 // from needlessly scrolling the window when the edit control is
2181 // dismissed. See ticket #9563.
2184 void wxListMainWindow::SendNotify( size_t line
,
2185 wxEventType command
,
2186 const wxPoint
& point
)
2188 wxListEvent
le( command
, GetParent()->GetId() );
2189 le
.SetEventObject( GetParent() );
2191 le
.m_itemIndex
= line
;
2193 // set only for events which have position
2194 if ( point
!= wxDefaultPosition
)
2195 le
.m_pointDrag
= point
;
2197 // don't try to get the line info for virtual list controls: the main
2198 // program has it anyhow and if we did it would result in accessing all
2199 // the lines, even those which are not visible now and this is precisely
2200 // what we're trying to avoid
2203 if ( line
!= (size_t)-1 )
2205 GetLine(line
)->GetItem( 0, le
.m_item
);
2207 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2209 //else: there may be no more such item
2211 GetParent()->GetEventHandler()->ProcessEvent( le
);
2214 void wxListMainWindow::ChangeCurrent(size_t current
)
2216 m_current
= current
;
2218 // as the current item changed, we shouldn't start editing it when the
2219 // "slow click" timer expires as the click happened on another item
2220 if ( m_renameTimer
->IsRunning() )
2221 m_renameTimer
->Stop();
2223 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2226 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2228 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2229 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2231 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2232 wxT("EditLabel() needs a text control") );
2234 size_t itemEdit
= (size_t)item
;
2236 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2237 le
.SetEventObject( GetParent() );
2238 le
.m_itemIndex
= item
;
2239 wxListLineData
*data
= GetLine(itemEdit
);
2240 wxCHECK_MSG( data
, NULL
, wxT("invalid index in EditLabel()") );
2241 data
->GetItem( 0, le
.m_item
);
2243 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2245 // vetoed by user code
2249 // We have to call this here because the label in question might just have
2250 // been added and no screen update taken place.
2253 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2254 // so that no pending events may change the item count (see below)
2255 // IMPORTANT: needs to be tested!
2258 // Pending events dispatched by wxSafeYield might have changed the item
2260 if ( (size_t)item
>= GetItemCount() )
2264 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2265 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2266 return m_textctrlWrapper
->GetText();
2269 void wxListMainWindow::OnRenameTimer()
2271 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2273 EditLabel( m_current
);
2276 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2278 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2279 le
.SetEventObject( GetParent() );
2280 le
.m_itemIndex
= itemEdit
;
2282 wxListLineData
*data
= GetLine(itemEdit
);
2284 wxCHECK_MSG( data
, false, wxT("invalid index in OnRenameAccept()") );
2286 data
->GetItem( 0, le
.m_item
);
2287 le
.m_item
.m_text
= value
;
2288 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2292 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2294 // let owner know that the edit was cancelled
2295 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2297 le
.SetEditCanceled(true);
2299 le
.SetEventObject( GetParent() );
2300 le
.m_itemIndex
= itemEdit
;
2302 wxListLineData
*data
= GetLine(itemEdit
);
2303 wxCHECK_RET( data
, wxT("invalid index in OnRenameCancelled()") );
2305 data
->GetItem( 0, le
.m_item
);
2306 GetEventHandler()->ProcessEvent( le
);
2309 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2312 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2313 // shutdown the edit control when the mouse is clicked elsewhere on the
2314 // listctrl because the order of events is different (or something like
2315 // that), so explicitly end the edit if it is active.
2316 if ( event
.LeftDown() && m_textctrlWrapper
)
2317 m_textctrlWrapper
->EndEdit( false );
2320 if ( event
.LeftDown() )
2323 event
.SetEventObject( GetParent() );
2324 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2327 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2329 // let the base class handle mouse wheel events.
2334 if ( !HasCurrent() || IsEmpty() )
2336 if (event
.RightDown())
2338 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2340 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2341 GetParent()->GetId(),
2342 ClientToScreen(event
.GetPosition()));
2343 evtCtx
.SetEventObject(GetParent());
2344 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2352 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2353 event
.ButtonDClick()) )
2356 int x
= event
.GetX();
2357 int y
= event
.GetY();
2358 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2360 // where did we hit it (if we did)?
2363 size_t count
= GetItemCount(),
2366 if ( InReportView() )
2368 current
= y
/ GetLineHeight();
2369 if ( current
< count
)
2370 hitResult
= HitTestLine(current
, x
, y
);
2374 // TODO: optimize it too! this is less simple than for report view but
2375 // enumerating all items is still not a way to do it!!
2376 for ( current
= 0; current
< count
; current
++ )
2378 hitResult
= HitTestLine(current
, x
, y
);
2384 if (event
.Dragging())
2386 if (m_dragCount
== 0)
2388 // we have to report the raw, physical coords as we want to be
2389 // able to call HitTest(event.m_pointDrag) from the user code to
2390 // get the item being dragged
2391 m_dragStart
= event
.GetPosition();
2396 if (m_dragCount
!= 3)
2399 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2400 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2402 wxListEvent
le( command
, GetParent()->GetId() );
2403 le
.SetEventObject( GetParent() );
2404 le
.m_itemIndex
= m_lineLastClicked
;
2405 le
.m_pointDrag
= m_dragStart
;
2406 GetParent()->GetEventHandler()->ProcessEvent( le
);
2417 // outside of any item
2418 if (event
.RightDown())
2420 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2422 wxContextMenuEvent
evtCtx(
2424 GetParent()->GetId(),
2425 ClientToScreen(event
.GetPosition()));
2426 evtCtx
.SetEventObject(GetParent());
2427 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2431 // reset the selection and bail out
2432 HighlightAll(false);
2438 bool forceClick
= false;
2439 if (event
.ButtonDClick())
2441 if ( m_renameTimer
->IsRunning() )
2442 m_renameTimer
->Stop();
2444 m_lastOnSame
= false;
2446 if ( current
== m_lineLastClicked
)
2448 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2454 // The first click was on another item, so don't interpret this as
2455 // a double click, but as a simple click instead
2462 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2464 // select single line
2465 HighlightAll( false );
2466 ReverseHighlight(m_lineSelectSingleOnUp
);
2471 if ((current
== m_current
) &&
2472 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2473 HasFlag(wxLC_EDIT_LABELS
) )
2475 if ( !InReportView() ||
2476 GetLineLabelRect(current
).Contains(x
, y
) )
2478 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2479 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2484 m_lastOnSame
= false;
2485 m_lineSelectSingleOnUp
= (size_t)-1;
2489 // This is necessary, because after a DnD operation in
2490 // from and to ourself, the up event is swallowed by the
2491 // DnD code. So on next non-up event (which means here and
2492 // now) m_lineSelectSingleOnUp should be reset.
2493 m_lineSelectSingleOnUp
= (size_t)-1;
2495 if (event
.RightDown())
2497 m_lineBeforeLastClicked
= m_lineLastClicked
;
2498 m_lineLastClicked
= current
;
2500 // If the item is already selected, do not update the selection.
2501 // Multi-selections should not be cleared if a selected item is clicked.
2502 if (!IsHighlighted(current
))
2504 HighlightAll(false);
2505 ChangeCurrent(current
);
2506 ReverseHighlight(m_current
);
2509 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2511 // Allow generation of context menu event
2514 else if (event
.MiddleDown())
2516 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2518 else if ( event
.LeftDown() || forceClick
)
2520 m_lineBeforeLastClicked
= m_lineLastClicked
;
2521 m_lineLastClicked
= current
;
2523 size_t oldCurrent
= m_current
;
2524 bool oldWasSelected
= IsHighlighted(m_current
);
2526 bool cmdModifierDown
= event
.CmdDown();
2527 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2529 if ( IsSingleSel() || !IsHighlighted(current
) )
2531 HighlightAll( false );
2533 ChangeCurrent(current
);
2535 ReverseHighlight(m_current
);
2537 else // multi sel & current is highlighted & no mod keys
2539 m_lineSelectSingleOnUp
= current
;
2540 ChangeCurrent(current
); // change focus
2543 else // multi sel & either ctrl or shift is down
2545 if (cmdModifierDown
)
2547 ChangeCurrent(current
);
2549 ReverseHighlight(m_current
);
2551 else if (event
.ShiftDown())
2553 ChangeCurrent(current
);
2555 size_t lineFrom
= oldCurrent
,
2558 if ( lineTo
< lineFrom
)
2561 lineFrom
= m_current
;
2564 HighlightLines(lineFrom
, lineTo
);
2566 else // !ctrl, !shift
2568 // test in the enclosing if should make it impossible
2569 wxFAIL_MSG( wxT("how did we get here?") );
2573 if (m_current
!= oldCurrent
)
2574 RefreshLine( oldCurrent
);
2576 // forceClick is only set if the previous click was on another item
2577 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2581 void wxListMainWindow::MoveToItem(size_t item
)
2583 if ( item
== (size_t)-1 )
2586 wxRect rect
= GetLineRect(item
);
2588 int client_w
, client_h
;
2589 GetClientSize( &client_w
, &client_h
);
2591 const int hLine
= GetLineHeight();
2593 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2594 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2596 if ( InReportView() )
2598 // the next we need the range of lines shown it might be different,
2599 // so recalculate it
2600 ResetVisibleLinesRange();
2602 if (rect
.y
< view_y
)
2603 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2604 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2605 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2608 // At least on Mac the visible lines value will get reset inside of
2609 // Scroll *before* it actually scrolls the window because of the
2610 // Update() that happens there, so it will still have the wrong value.
2611 // So let's reset it again and wait for it to be recalculated in the
2612 // next paint event. I would expect this problem to show up in wxGTK
2613 // too but couldn't duplicate it there. Perhaps the order of events
2614 // is different... --Robin
2615 ResetVisibleLinesRange();
2623 if (rect
.x
-view_x
< 5)
2624 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2625 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2626 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2628 if (rect
.y
-view_y
< 5)
2629 sy
= (rect
.y
- 5) / hLine
;
2630 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2631 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2633 GetListCtrl()->Scroll(sx
, sy
);
2637 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2639 if ( !InReportView() )
2641 // TODO: this should work in all views but is not implemented now
2646 GetVisibleLinesRange(&top
, &bottom
);
2648 if ( bottom
== (size_t)-1 )
2651 ResetVisibleLinesRange();
2653 int hLine
= GetLineHeight();
2655 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2658 // see comment in MoveToItem() for why we do this
2659 ResetVisibleLinesRange();
2665 // ----------------------------------------------------------------------------
2666 // keyboard handling
2667 // ----------------------------------------------------------------------------
2669 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2671 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2672 wxT("invalid item index in OnArrowChar()") );
2674 size_t oldCurrent
= m_current
;
2676 // in single selection we just ignore Shift as we can't select several
2678 if ( event
.ShiftDown() && !IsSingleSel() )
2680 ChangeCurrent(newCurrent
);
2682 // refresh the old focus to remove it
2683 RefreshLine( oldCurrent
);
2685 // select all the items between the old and the new one
2686 if ( oldCurrent
> newCurrent
)
2688 newCurrent
= oldCurrent
;
2689 oldCurrent
= m_current
;
2692 HighlightLines(oldCurrent
, newCurrent
);
2696 // all previously selected items are unselected unless ctrl is held
2697 // in a multiselection control
2698 if ( !event
.ControlDown() || IsSingleSel() )
2699 HighlightAll(false);
2701 ChangeCurrent(newCurrent
);
2703 // refresh the old focus to remove it
2704 RefreshLine( oldCurrent
);
2706 // in single selection mode we must always have a selected item
2707 if ( !event
.ControlDown() || IsSingleSel() )
2708 HighlightLine( m_current
, true );
2711 RefreshLine( m_current
);
2716 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2718 wxWindow
*parent
= GetParent();
2720 // propagate the key event upwards
2721 wxKeyEvent
ke(event
);
2722 ke
.SetEventObject( parent
);
2723 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2729 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2731 wxWindow
*parent
= GetParent();
2733 // propagate the key event upwards
2734 wxKeyEvent
ke(event
);
2735 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2741 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2743 wxWindow
*parent
= GetParent();
2745 // send a list_key event up
2748 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2749 le
.m_itemIndex
= m_current
;
2750 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2751 le
.m_code
= event
.GetKeyCode();
2752 le
.SetEventObject( parent
);
2753 parent
->GetEventHandler()->ProcessEvent( le
);
2756 if ( (event
.GetKeyCode() != WXK_UP
) &&
2757 (event
.GetKeyCode() != WXK_DOWN
) &&
2758 (event
.GetKeyCode() != WXK_RIGHT
) &&
2759 (event
.GetKeyCode() != WXK_LEFT
) &&
2760 (event
.GetKeyCode() != WXK_PAGEUP
) &&
2761 (event
.GetKeyCode() != WXK_PAGEDOWN
) &&
2762 (event
.GetKeyCode() != WXK_END
) &&
2763 (event
.GetKeyCode() != WXK_HOME
) )
2765 // propagate the char event upwards
2766 wxKeyEvent
ke(event
);
2767 ke
.SetEventObject( parent
);
2768 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2772 if ( HandleAsNavigationKey(event
) )
2775 // no item -> nothing to do
2782 // don't use m_linesPerPage directly as it might not be computed yet
2783 const int pageSize
= GetCountPerPage();
2784 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
2786 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2788 if (event
.GetKeyCode() == WXK_RIGHT
)
2789 event
.m_keyCode
= WXK_LEFT
;
2790 else if (event
.GetKeyCode() == WXK_LEFT
)
2791 event
.m_keyCode
= WXK_RIGHT
;
2794 switch ( event
.GetKeyCode() )
2797 if ( m_current
> 0 )
2798 OnArrowChar( m_current
- 1, event
);
2802 if ( m_current
< (size_t)GetItemCount() - 1 )
2803 OnArrowChar( m_current
+ 1, event
);
2808 OnArrowChar( GetItemCount() - 1, event
);
2813 OnArrowChar( 0, event
);
2818 int steps
= InReportView() ? pageSize
- 1
2819 : m_current
% pageSize
;
2821 int index
= m_current
- steps
;
2825 OnArrowChar( index
, event
);
2831 int steps
= InReportView()
2833 : pageSize
- (m_current
% pageSize
) - 1;
2835 size_t index
= m_current
+ steps
;
2836 size_t count
= GetItemCount();
2837 if ( index
>= count
)
2840 OnArrowChar( index
, event
);
2845 if ( !InReportView() )
2847 int index
= m_current
- pageSize
;
2851 OnArrowChar( index
, event
);
2856 if ( !InReportView() )
2858 size_t index
= m_current
+ pageSize
;
2860 size_t count
= GetItemCount();
2861 if ( index
>= count
)
2864 OnArrowChar( index
, event
);
2869 if ( IsSingleSel() )
2871 if ( event
.ControlDown() )
2873 ReverseHighlight(m_current
);
2875 else // normal space press
2877 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2880 else // multiple selection
2882 ReverseHighlight(m_current
);
2888 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2896 // ----------------------------------------------------------------------------
2898 // ----------------------------------------------------------------------------
2900 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2904 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2905 event
.SetEventObject( GetParent() );
2906 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2910 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2911 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2912 // which are already drawn correctly resulting in horrible flicker - avoid
2922 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2926 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2927 event
.SetEventObject( GetParent() );
2928 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2936 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2938 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2940 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2942 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2944 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2946 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2948 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2950 else if ( InReportView() && (m_small_image_list
))
2952 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2956 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2958 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2960 m_normal_image_list
->GetSize( index
, width
, height
);
2962 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2964 m_small_image_list
->GetSize( index
, width
, height
);
2966 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2968 m_small_image_list
->GetSize( index
, width
, height
);
2970 else if ( InReportView() && m_small_image_list
)
2972 m_small_image_list
->GetSize( index
, width
, height
);
2981 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2983 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2984 dc
.SetFont( GetFont() );
2987 dc
.GetTextExtent( s
, &lw
, NULL
);
2989 return lw
+ AUTOSIZE_COL_MARGIN
;
2992 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2996 // calc the spacing from the icon size
2997 int width
= 0, height
= 0;
2999 if ((imageList
) && (imageList
->GetImageCount()) )
3000 imageList
->GetSize(0, width
, height
);
3002 if (which
== wxIMAGE_LIST_NORMAL
)
3004 m_normal_image_list
= imageList
;
3005 m_normal_spacing
= width
+ 8;
3008 if (which
== wxIMAGE_LIST_SMALL
)
3010 m_small_image_list
= imageList
;
3011 m_small_spacing
= width
+ 14;
3012 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3016 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3020 m_small_spacing
= spacing
;
3022 m_normal_spacing
= spacing
;
3025 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3027 return isSmall
? m_small_spacing
: m_normal_spacing
;
3030 // ----------------------------------------------------------------------------
3032 // ----------------------------------------------------------------------------
3034 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3036 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3038 wxCHECK_RET( node
, wxT("invalid column index in SetColumn") );
3040 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3041 item
.m_width
= GetTextLength( item
.m_text
);
3043 wxListHeaderData
*column
= node
->GetData();
3044 column
->SetItem( item
);
3046 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3048 headerWin
->m_dirty
= true;
3052 // invalidate it as it has to be recalculated
3056 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3058 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3059 wxT("invalid column index") );
3061 wxCHECK_RET( InReportView(),
3062 wxT("SetColumnWidth() can only be called in report mode.") );
3066 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3068 headerWin
->m_dirty
= true;
3070 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3071 wxCHECK_RET( node
, wxT("no column?") );
3073 wxListHeaderData
*column
= node
->GetData();
3075 size_t count
= GetItemCount();
3077 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3079 width
= GetTextLength(column
->GetText());
3080 width
+= 2*EXTRA_WIDTH
;
3082 // check for column header's image availability
3083 const int image
= column
->GetImage();
3086 if ( m_small_image_list
)
3089 m_small_image_list
->GetSize(image
, ix
, iy
);
3090 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3094 else if ( width
== wxLIST_AUTOSIZE
)
3098 // TODO: determine the max width somehow...
3099 width
= WIDTH_COL_DEFAULT
;
3103 wxClientDC
dc(this);
3104 dc
.SetFont( GetFont() );
3106 int max
= AUTOSIZE_COL_MARGIN
;
3108 // if the cached column width isn't valid then recalculate it
3109 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3111 for (size_t i
= 0; i
< count
; i
++)
3113 wxListLineData
*line
= GetLine( i
);
3114 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3116 wxCHECK_RET( n
, wxT("no subitem?") );
3118 wxListItemData
*itemData
= n
->GetData();
3121 itemData
->GetItem(item
);
3122 int itemWidth
= GetItemWidthWithImage(&item
);
3123 if (itemWidth
> max
)
3127 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3128 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3131 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3132 width
= max
+ AUTOSIZE_COL_MARGIN
;
3136 column
->SetWidth( width
);
3138 // invalidate it as it has to be recalculated
3142 int wxListMainWindow::GetHeaderWidth() const
3144 if ( !m_headerWidth
)
3146 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3148 size_t count
= GetColumnCount();
3149 for ( size_t col
= 0; col
< count
; col
++ )
3151 self
->m_headerWidth
+= GetColumnWidth(col
);
3155 return m_headerWidth
;
3158 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3160 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3161 wxCHECK_RET( node
, wxT("invalid column index in GetColumn") );
3163 wxListHeaderData
*column
= node
->GetData();
3164 column
->GetItem( item
);
3167 int wxListMainWindow::GetColumnWidth( int col
) const
3169 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3170 wxCHECK_MSG( node
, 0, wxT("invalid column index") );
3172 wxListHeaderData
*column
= node
->GetData();
3173 return column
->GetWidth();
3176 // ----------------------------------------------------------------------------
3178 // ----------------------------------------------------------------------------
3180 void wxListMainWindow::SetItem( wxListItem
&item
)
3182 long id
= item
.m_itemId
;
3183 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3184 wxT("invalid item index in SetItem") );
3188 wxListLineData
*line
= GetLine((size_t)id
);
3189 line
->SetItem( item
.m_col
, item
);
3191 // Set item state if user wants
3192 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3193 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3197 // update the Max Width Cache if needed
3198 int width
= GetItemWidthWithImage(&item
);
3200 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3201 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3205 // update the item on screen
3207 GetItemRect(id
, rectItem
);
3208 RefreshRect(rectItem
);
3211 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3216 // first deal with selection
3217 if ( stateMask
& wxLIST_STATE_SELECTED
)
3219 // set/clear select state
3222 // optimized version for virtual listctrl.
3223 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3226 else if ( state
& wxLIST_STATE_SELECTED
)
3228 const long count
= GetItemCount();
3229 for( long i
= 0; i
< count
; i
++ )
3231 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3237 // clear for non virtual (somewhat optimized by using GetNextItem())
3239 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3241 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3246 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3248 // unfocus all: only one item can be focussed, so clearing focus for
3249 // all items is simply clearing focus of the focussed item.
3250 SetItemState(m_current
, state
, stateMask
);
3252 //(setting focus to all items makes no sense, so it is not handled here.)
3255 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3259 SetItemStateAll(state
, stateMask
);
3263 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3264 wxT("invalid list ctrl item index in SetItem") );
3266 size_t oldCurrent
= m_current
;
3267 size_t item
= (size_t)litem
; // safe because of the check above
3269 // do we need to change the focus?
3270 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3272 if ( state
& wxLIST_STATE_FOCUSED
)
3274 // don't do anything if this item is already focused
3275 if ( item
!= m_current
)
3277 ChangeCurrent(item
);
3279 if ( oldCurrent
!= (size_t)-1 )
3281 if ( IsSingleSel() )
3283 HighlightLine(oldCurrent
, false);
3286 RefreshLine(oldCurrent
);
3289 RefreshLine( m_current
);
3294 // don't do anything if this item is not focused
3295 if ( item
== m_current
)
3299 if ( IsSingleSel() )
3301 // we must unselect the old current item as well or we
3302 // might end up with more than one selected item in a
3303 // single selection control
3304 HighlightLine(oldCurrent
, false);
3307 RefreshLine( oldCurrent
);
3312 // do we need to change the selection state?
3313 if ( stateMask
& wxLIST_STATE_SELECTED
)
3315 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3317 if ( IsSingleSel() )
3321 // selecting the item also makes it the focused one in the
3323 if ( m_current
!= item
)
3325 ChangeCurrent(item
);
3327 if ( oldCurrent
!= (size_t)-1 )
3329 HighlightLine( oldCurrent
, false );
3330 RefreshLine( oldCurrent
);
3336 // only the current item may be selected anyhow
3337 if ( item
!= m_current
)
3342 if ( HighlightLine(item
, on
) )
3349 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3351 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3352 wxT("invalid list ctrl item index in GetItemState()") );
3354 int ret
= wxLIST_STATE_DONTCARE
;
3356 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3358 if ( (size_t)item
== m_current
)
3359 ret
|= wxLIST_STATE_FOCUSED
;
3362 if ( stateMask
& wxLIST_STATE_SELECTED
)
3364 if ( IsHighlighted(item
) )
3365 ret
|= wxLIST_STATE_SELECTED
;
3371 void wxListMainWindow::GetItem( wxListItem
&item
) const
3373 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3374 wxT("invalid item index in GetItem") );
3376 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3377 line
->GetItem( item
.m_col
, item
);
3379 // Get item state if user wants it
3380 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3381 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3382 wxLIST_STATE_FOCUSED
);
3385 // ----------------------------------------------------------------------------
3387 // ----------------------------------------------------------------------------
3389 size_t wxListMainWindow::GetItemCount() const
3391 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3394 void wxListMainWindow::SetItemCount(long count
)
3396 m_selStore
.SetItemCount(count
);
3397 m_countVirt
= count
;
3399 ResetVisibleLinesRange();
3401 // scrollbars must be reset
3405 int wxListMainWindow::GetSelectedItemCount() const
3407 // deal with the quick case first
3408 if ( IsSingleSel() )
3409 return HasCurrent() ? IsHighlighted(m_current
) : false;
3411 // virtual controls remmebers all its selections itself
3413 return m_selStore
.GetSelectedCount();
3415 // TODO: we probably should maintain the number of items selected even for
3416 // non virtual controls as enumerating all lines is really slow...
3417 size_t countSel
= 0;
3418 size_t count
= GetItemCount();
3419 for ( size_t line
= 0; line
< count
; line
++ )
3421 if ( GetLine(line
)->IsHighlighted() )
3428 // ----------------------------------------------------------------------------
3429 // item position/size
3430 // ----------------------------------------------------------------------------
3432 wxRect
wxListMainWindow::GetViewRect() const
3434 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3436 // we need to find the longest/tallest label
3437 wxCoord xMax
= 0, yMax
= 0;
3438 const int count
= GetItemCount();
3441 for ( int i
= 0; i
< count
; i
++ )
3443 // we need logical, not physical, coordinates here, so use
3444 // GetLineRect() instead of GetItemRect()
3445 wxRect r
= GetLineRect(i
);
3447 wxCoord x
= r
.GetRight(),
3457 // some fudge needed to make it look prettier
3458 xMax
+= 2 * EXTRA_BORDER_X
;
3459 yMax
+= 2 * EXTRA_BORDER_Y
;
3461 // account for the scrollbars if necessary
3462 const wxSize sizeAll
= GetClientSize();
3463 if ( xMax
> sizeAll
.x
)
3464 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3465 if ( yMax
> sizeAll
.y
)
3466 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3468 return wxRect(0, 0, xMax
, yMax
);
3472 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3474 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3476 wxT("GetSubItemRect only meaningful in report view") );
3477 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3478 wxT("invalid item in GetSubItemRect") );
3480 // ensure that we're laid out, otherwise we could return nonsense
3483 wxConstCast(this, wxListMainWindow
)->
3484 RecalculatePositions(true /* no refresh */);
3487 rect
= GetLineRect((size_t)item
);
3489 // Adjust rect to specified column
3490 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3492 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3493 wxT("invalid subItem in GetSubItemRect") );
3495 for (int i
= 0; i
< subItem
; i
++)
3497 rect
.x
+= GetColumnWidth(i
);
3499 rect
.width
= GetColumnWidth(subItem
);
3502 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3507 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3510 GetItemRect(item
, rect
);
3518 // ----------------------------------------------------------------------------
3519 // geometry calculation
3520 // ----------------------------------------------------------------------------
3522 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3524 const int lineHeight
= GetLineHeight();
3526 wxClientDC
dc( this );
3527 dc
.SetFont( GetFont() );
3529 const size_t count
= GetItemCount();
3532 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3533 iconSpacing
= m_normal_spacing
;
3534 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3535 iconSpacing
= m_small_spacing
;
3539 // Note that we do not call GetClientSize() here but
3540 // GetSize() and subtract the border size for sunken
3541 // borders manually. This is technically incorrect,
3542 // but we need to know the client area's size WITHOUT
3543 // scrollbars here. Since we don't know if there are
3544 // any scrollbars, we use GetSize() instead. Another
3545 // solution would be to call SetScrollbars() here to
3546 // remove the scrollbars and call GetClientSize() then,
3547 // but this might result in flicker and - worse - will
3548 // reset the scrollbars to 0 which is not good at all
3549 // if you resize a dialog/window, but don't want to
3550 // reset the window scrolling. RR.
3551 // Furthermore, we actually do NOT subtract the border
3552 // width as 2 pixels is just the extra space which we
3553 // need around the actual content in the window. Other-
3554 // wise the text would e.g. touch the upper border. RR.
3557 GetSize( &clientWidth
, &clientHeight
);
3559 if ( InReportView() )
3561 // all lines have the same height and we scroll one line per step
3562 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3564 m_linesPerPage
= clientHeight
/ lineHeight
;
3566 ResetVisibleLinesRange();
3568 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3569 GetHeaderWidth() / SCROLL_UNIT_X
,
3570 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3571 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3572 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3577 // we have 3 different layout strategies: either layout all items
3578 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3579 // to arrange them in top to bottom, left to right (don't ask me why
3580 // not the other way round...) order
3581 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3583 int x
= EXTRA_BORDER_X
;
3584 int y
= EXTRA_BORDER_Y
;
3586 wxCoord widthMax
= 0;
3589 for ( i
= 0; i
< count
; i
++ )
3591 wxListLineData
*line
= GetLine(i
);
3592 line
->CalculateSize( &dc
, iconSpacing
);
3593 line
->SetPosition( x
, y
, iconSpacing
);
3595 wxSize sizeLine
= GetLineSize(i
);
3597 if ( HasFlag(wxLC_ALIGN_TOP
) )
3599 if ( sizeLine
.x
> widthMax
)
3600 widthMax
= sizeLine
.x
;
3604 else // wxLC_ALIGN_LEFT
3606 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3610 if ( HasFlag(wxLC_ALIGN_TOP
) )
3612 // traverse the items again and tweak their sizes so that they are
3613 // all the same in a row
3614 for ( i
= 0; i
< count
; i
++ )
3616 wxListLineData
*line
= GetLine(i
);
3617 line
->m_gi
->ExtendWidth(widthMax
);
3621 GetListCtrl()->SetScrollbars
3625 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3626 (y
+ lineHeight
) / lineHeight
,
3627 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3628 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3632 else // "flowed" arrangement, the most complicated case
3634 // at first we try without any scrollbars, if the items don't fit into
3635 // the window, we recalculate after subtracting the space taken by the
3638 int entireWidth
= 0;
3640 for (int tries
= 0; tries
< 2; tries
++)
3642 entireWidth
= 2 * EXTRA_BORDER_X
;
3646 // Now we have decided that the items do not fit into the
3647 // client area, so we need a scrollbar
3648 entireWidth
+= SCROLL_UNIT_X
;
3651 int x
= EXTRA_BORDER_X
;
3652 int y
= EXTRA_BORDER_Y
;
3653 int maxWidthInThisRow
= 0;
3656 int currentlyVisibleLines
= 0;
3658 for (size_t i
= 0; i
< count
; i
++)
3660 currentlyVisibleLines
++;
3661 wxListLineData
*line
= GetLine( i
);
3662 line
->CalculateSize( &dc
, iconSpacing
);
3663 line
->SetPosition( x
, y
, iconSpacing
);
3665 wxSize sizeLine
= GetLineSize( i
);
3667 if ( maxWidthInThisRow
< sizeLine
.x
)
3668 maxWidthInThisRow
= sizeLine
.x
;
3671 if (currentlyVisibleLines
> m_linesPerPage
)
3672 m_linesPerPage
= currentlyVisibleLines
;
3674 if ( y
+ sizeLine
.y
>= clientHeight
)
3676 currentlyVisibleLines
= 0;
3678 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3679 x
+= maxWidthInThisRow
;
3680 entireWidth
+= maxWidthInThisRow
;
3681 maxWidthInThisRow
= 0;
3684 // We have reached the last item.
3685 if ( i
== count
- 1 )
3686 entireWidth
+= maxWidthInThisRow
;
3688 if ( (tries
== 0) &&
3689 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3691 clientHeight
-= wxSystemSettings::
3692 GetMetric(wxSYS_HSCROLL_Y
);
3697 if ( i
== count
- 1 )
3698 tries
= 1; // Everything fits, no second try required.
3702 GetListCtrl()->SetScrollbars
3706 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3708 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3717 // FIXME: why should we call it from here?
3724 void wxListMainWindow::RefreshAll()
3729 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3730 if ( headerWin
&& headerWin
->m_dirty
)
3732 headerWin
->m_dirty
= false;
3733 headerWin
->Refresh();
3737 void wxListMainWindow::UpdateCurrent()
3739 if ( !HasCurrent() && !IsEmpty() )
3743 long wxListMainWindow::GetNextItem( long item
,
3744 int WXUNUSED(geometry
),
3748 max
= GetItemCount();
3749 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3750 wxT("invalid listctrl index in GetNextItem()") );
3752 // notice that we start with the next item (or the first one if item == -1)
3753 // and this is intentional to allow writing a simple loop to iterate over
3754 // all selected items
3757 // this is not an error because the index was OK initially,
3758 // just no such item
3765 size_t count
= GetItemCount();
3766 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3768 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3771 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3778 // ----------------------------------------------------------------------------
3780 // ----------------------------------------------------------------------------
3782 void wxListMainWindow::DeleteItem( long lindex
)
3784 size_t count
= GetItemCount();
3786 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3787 wxT("invalid item index in DeleteItem") );
3789 size_t index
= (size_t)lindex
;
3791 // we don't need to adjust the index for the previous items
3792 if ( HasCurrent() && m_current
>= index
)
3794 // if the current item is being deleted, we want the next one to
3795 // become selected - unless there is no next one - so don't adjust
3796 // m_current in this case
3797 if ( m_current
!= index
|| m_current
== count
- 1 )
3801 if ( InReportView() )
3803 // mark the Column Max Width cache as dirty if the items in the line
3804 // we're deleting contain the Max Column Width
3805 wxListLineData
* const line
= GetLine(index
);
3806 wxListItemDataList::compatibility_iterator n
;
3807 wxListItemData
*itemData
;
3811 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3813 n
= line
->m_items
.Item( i
);
3814 itemData
= n
->GetData();
3815 itemData
->GetItem(item
);
3817 itemWidth
= GetItemWidthWithImage(&item
);
3819 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3820 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3823 ResetVisibleLinesRange();
3826 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3831 m_selStore
.OnItemDelete(index
);
3835 m_lines
.RemoveAt( index
);
3838 // we need to refresh the (vert) scrollbar as the number of items changed
3841 RefreshAfter(index
);
3844 void wxListMainWindow::DeleteColumn( int col
)
3846 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3848 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3851 delete node
->GetData();
3852 m_columns
.Erase( node
);
3856 // update all the items
3857 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3859 wxListLineData
* const line
= GetLine(i
);
3860 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3861 delete n
->GetData();
3862 line
->m_items
.Erase(n
);
3866 if ( InReportView() ) // we only cache max widths when in Report View
3868 delete m_aColWidths
.Item(col
);
3869 m_aColWidths
.RemoveAt(col
);
3872 // invalidate it as it has to be recalculated
3876 void wxListMainWindow::DoDeleteAllItems()
3879 // nothing to do - in particular, don't send the event
3884 // to make the deletion of all items faster, we don't send the
3885 // notifications for each item deletion in this case but only one event
3886 // for all of them: this is compatible with wxMSW and documented in
3887 // DeleteAllItems() description
3889 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3890 event
.SetEventObject( GetParent() );
3891 GetParent()->GetEventHandler()->ProcessEvent( event
);
3899 if ( InReportView() )
3901 ResetVisibleLinesRange();
3902 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3904 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3911 void wxListMainWindow::DeleteAllItems()
3915 RecalculatePositions();
3918 void wxListMainWindow::DeleteEverything()
3920 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3921 WX_CLEAR_ARRAY(m_aColWidths
);
3926 // ----------------------------------------------------------------------------
3927 // scanning for an item
3928 // ----------------------------------------------------------------------------
3930 void wxListMainWindow::EnsureVisible( long index
)
3932 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3933 wxT("invalid index in EnsureVisible") );
3935 // We have to call this here because the label in question might just have
3936 // been added and its position is not known yet
3938 RecalculatePositions(true /* no refresh */);
3940 MoveToItem((size_t)index
);
3943 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3949 wxString str_upper
= str
.Upper();
3953 size_t count
= GetItemCount();
3954 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3956 wxListLineData
*line
= GetLine(i
);
3957 wxString line_upper
= line
->GetText(0).Upper();
3960 if (line_upper
== str_upper
)
3965 if (line_upper
.find(str_upper
) == 0)
3973 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3979 size_t count
= GetItemCount();
3980 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3982 wxListLineData
*line
= GetLine(i
);
3984 line
->GetItem( 0, item
);
3985 if (item
.m_data
== data
)
3992 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3995 GetVisibleLinesRange( &topItem
, NULL
);
3998 GetItemPosition( GetItemCount() - 1, p
);
4002 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4003 if ( id
>= 0 && id
< (long)GetItemCount() )
4009 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4011 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
4013 size_t count
= GetItemCount();
4015 if ( InReportView() )
4017 size_t current
= y
/ GetLineHeight();
4018 if ( current
< count
)
4020 flags
= HitTestLine(current
, x
, y
);
4027 // TODO: optimize it too! this is less simple than for report view but
4028 // enumerating all items is still not a way to do it!!
4029 for ( size_t current
= 0; current
< count
; current
++ )
4031 flags
= HitTestLine(current
, x
, y
);
4040 // ----------------------------------------------------------------------------
4042 // ----------------------------------------------------------------------------
4044 void wxListMainWindow::InsertItem( wxListItem
&item
)
4046 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4048 int count
= GetItemCount();
4049 wxCHECK_RET( item
.m_itemId
>= 0, wxT("invalid item index") );
4051 if (item
.m_itemId
> count
)
4052 item
.m_itemId
= count
;
4054 size_t id
= item
.m_itemId
;
4058 if ( InReportView() )
4060 ResetVisibleLinesRange();
4062 // calculate the width of the item and adjust the max column width
4063 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4064 int width
= GetItemWidthWithImage(&item
);
4065 item
.SetWidth(width
);
4066 if (width
> pWidthInfo
->nMaxWidth
)
4067 pWidthInfo
->nMaxWidth
= width
;
4070 wxListLineData
*line
= new wxListLineData(this);
4072 line
->SetItem( item
.m_col
, item
);
4074 m_lines
.Insert( line
, id
);
4078 // If an item is selected at or below the point of insertion, we need to
4079 // increment the member variables because the current row's index has gone
4081 if ( HasCurrent() && m_current
>= id
)
4084 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4086 RefreshLines(id
, GetItemCount() - 1);
4089 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4092 if ( InReportView() )
4094 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4095 item
.m_width
= GetTextLength( item
.m_text
);
4097 wxListHeaderData
*column
= new wxListHeaderData( item
);
4098 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4100 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4103 wxListHeaderDataList::compatibility_iterator
4104 node
= m_columns
.Item( col
);
4105 m_columns
.Insert( node
, column
);
4106 m_aColWidths
.Insert( colWidthInfo
, col
);
4110 m_columns
.Append( column
);
4111 m_aColWidths
.Add( colWidthInfo
);
4116 // update all the items
4117 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4119 wxListLineData
* const line
= GetLine(i
);
4120 wxListItemData
* const data
= new wxListItemData(this);
4122 line
->m_items
.Insert(col
, data
);
4124 line
->m_items
.Append(data
);
4128 // invalidate it as it has to be recalculated
4133 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4136 wxClientDC
dc(this);
4138 dc
.SetFont( GetFont() );
4140 if (item
->GetImage() != -1)
4143 GetImageSize( item
->GetImage(), ix
, iy
);
4147 if (!item
->GetText().empty())
4150 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4157 // ----------------------------------------------------------------------------
4159 // ----------------------------------------------------------------------------
4161 static wxListCtrlCompare list_ctrl_compare_func_2
;
4162 static wxIntPtr list_ctrl_compare_data
;
4164 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4166 wxListLineData
*line1
= *arg1
;
4167 wxListLineData
*line2
= *arg2
;
4169 line1
->GetItem( 0, item
);
4170 wxUIntPtr data1
= item
.m_data
;
4171 line2
->GetItem( 0, item
);
4172 wxUIntPtr data2
= item
.m_data
;
4173 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4176 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4178 // selections won't make sense any more after sorting the items so reset
4180 HighlightAll(false);
4183 list_ctrl_compare_func_2
= fn
;
4184 list_ctrl_compare_data
= data
;
4185 m_lines
.Sort( list_ctrl_compare_func_1
);
4189 // ----------------------------------------------------------------------------
4191 // ----------------------------------------------------------------------------
4193 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4195 // update our idea of which lines are shown when we redraw the window the
4197 ResetVisibleLinesRange();
4199 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4201 wxGenericListCtrl
* lc
= GetListCtrl();
4202 wxCHECK_RET( lc
, wxT("no listctrl window?") );
4204 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4206 lc
->m_headerWin
->Refresh();
4207 lc
->m_headerWin
->Update();
4212 int wxListMainWindow::GetCountPerPage() const
4214 if ( !m_linesPerPage
)
4216 wxConstCast(this, wxListMainWindow
)->
4217 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4220 return m_linesPerPage
;
4223 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4225 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4227 if ( m_lineFrom
== (size_t)-1 )
4229 size_t count
= GetItemCount();
4232 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4234 // this may happen if SetScrollbars() hadn't been called yet
4235 if ( m_lineFrom
>= count
)
4236 m_lineFrom
= count
- 1;
4238 // we redraw one extra line but this is needed to make the redrawing
4239 // logic work when there is a fractional number of lines on screen
4240 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4241 if ( m_lineTo
>= count
)
4242 m_lineTo
= count
- 1;
4244 else // empty control
4247 m_lineTo
= (size_t)-1;
4251 wxASSERT_MSG( IsEmpty() ||
4252 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4253 wxT("GetVisibleLinesRange() returns incorrect result") );
4261 // -------------------------------------------------------------------------------------
4262 // wxGenericListCtrl
4263 // -------------------------------------------------------------------------------------
4265 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4267 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4268 EVT_SIZE(wxGenericListCtrl::OnSize
)
4269 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4272 void wxGenericListCtrl::Init()
4274 m_imageListNormal
= NULL
;
4275 m_imageListSmall
= NULL
;
4276 m_imageListState
= NULL
;
4278 m_ownsImageListNormal
=
4279 m_ownsImageListSmall
=
4280 m_ownsImageListState
= false;
4286 wxGenericListCtrl::~wxGenericListCtrl()
4288 if (m_ownsImageListNormal
)
4289 delete m_imageListNormal
;
4290 if (m_ownsImageListSmall
)
4291 delete m_imageListSmall
;
4292 if (m_ownsImageListState
)
4293 delete m_imageListState
;
4296 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4298 bool needs_header
= HasHeader();
4299 bool has_header
= (m_headerWin
!= NULL
);
4301 if (needs_header
== has_header
)
4306 m_headerWin
= new wxListHeaderWindow
4308 this, wxID_ANY
, m_mainWin
,
4313 wxRendererNative::Get().GetHeaderButtonHeight(this)
4318 #if defined( __WXMAC__ )
4319 static wxFont
font( wxOSX_SYSTEM_FONT_SMALL
);
4320 m_headerWin
->SetFont( font
);
4323 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4327 GetSizer()->Detach( m_headerWin
);
4335 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4340 const wxValidator
&validator
,
4341 const wxString
&name
)
4345 // just like in other ports, an assert will fail if the user doesn't give any type style:
4346 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4347 wxT("wxListCtrl style should have exactly one mode bit set") );
4349 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4353 style
&= ~wxBORDER_MASK
;
4354 style
|= wxBORDER_THEME
;
4357 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4359 SetTargetWindow( m_mainWin
);
4361 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4362 sizer
->Add( m_mainWin
, 1, wxGROW
);
4365 CreateOrDestroyHeaderWindowAsNeeded();
4367 SetInitialSize(size
);
4372 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4374 return wxBORDER_THEME
;
4377 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4378 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4382 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4384 // we need to process arrows ourselves for scrolling
4385 if ( nMsg
== WM_GETDLGCODE
)
4387 rc
|= DLGC_WANTARROWS
;
4394 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4396 wxSize newsize
= size
;
4398 newsize
.y
-= m_headerWin
->GetSize().y
;
4403 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4405 // update our idea of which lines are shown when we redraw
4406 // the window the next time
4407 m_mainWin
->ResetVisibleLinesRange();
4409 HandleOnScroll( event
);
4411 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4413 m_headerWin
->Refresh();
4414 m_headerWin
->Update();
4418 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4420 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4421 wxT("wxLC_VIRTUAL can't be [un]set") );
4423 long flag
= GetWindowStyle();
4427 if (style
& wxLC_MASK_TYPE
)
4428 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4429 if (style
& wxLC_MASK_ALIGN
)
4430 flag
&= ~wxLC_MASK_ALIGN
;
4431 if (style
& wxLC_MASK_SORT
)
4432 flag
&= ~wxLC_MASK_SORT
;
4440 // some styles can be set without recreating everything (as happens in
4441 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4442 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4445 wxWindow::SetWindowStyleFlag(flag
);
4449 SetWindowStyleFlag( flag
);
4453 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4455 const bool wasInReportView
= HasFlag(wxLC_REPORT
);
4457 // update the window style first so that the header is created or destroyed
4458 // corresponding to the new style
4459 wxWindow::SetWindowStyleFlag( flag
);
4463 const bool inReportView
= (flag
& wxLC_REPORT
) != 0;
4464 if ( inReportView
!= wasInReportView
)
4466 // we need to notify the main window about this change as it must
4467 // update its data structures
4468 m_mainWin
->SetReportView(inReportView
);
4471 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4473 CreateOrDestroyHeaderWindowAsNeeded();
4475 GetSizer()->Layout();
4479 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4481 m_mainWin
->GetColumn( col
, item
);
4485 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4487 m_mainWin
->SetColumn( col
, item
);
4491 int wxGenericListCtrl::GetColumnWidth( int col
) const
4493 return m_mainWin
->GetColumnWidth( col
);
4496 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4498 m_mainWin
->SetColumnWidth( col
, width
);
4502 int wxGenericListCtrl::GetCountPerPage() const
4504 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4507 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4509 m_mainWin
->GetItem( info
);
4513 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4515 m_mainWin
->SetItem( info
);
4519 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4522 info
.m_text
= label
;
4523 info
.m_mask
= wxLIST_MASK_TEXT
;
4524 info
.m_itemId
= index
;
4528 info
.m_image
= imageId
;
4529 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4532 m_mainWin
->SetItem(info
);
4536 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4538 return m_mainWin
->GetItemState( item
, stateMask
);
4541 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4543 m_mainWin
->SetItemState( item
, state
, stateMask
);
4548 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4550 return SetItemColumnImage(item
, 0, image
);
4554 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4557 info
.m_image
= image
;
4558 info
.m_mask
= wxLIST_MASK_IMAGE
;
4559 info
.m_itemId
= item
;
4560 info
.m_col
= column
;
4561 m_mainWin
->SetItem( info
);
4565 wxString
wxGenericListCtrl::GetItemText( long item
) const
4567 return m_mainWin
->GetItemText(item
);
4570 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4572 m_mainWin
->SetItemText(item
, str
);
4575 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4578 info
.m_mask
= wxLIST_MASK_DATA
;
4579 info
.m_itemId
= item
;
4580 m_mainWin
->GetItem( info
);
4584 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4587 info
.m_mask
= wxLIST_MASK_DATA
;
4588 info
.m_itemId
= item
;
4590 m_mainWin
->SetItem( info
);
4594 wxRect
wxGenericListCtrl::GetViewRect() const
4596 return m_mainWin
->GetViewRect();
4599 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4601 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4604 bool wxGenericListCtrl::GetSubItemRect(long item
,
4607 int WXUNUSED(code
)) const
4609 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4612 if ( m_mainWin
->HasHeader() )
4613 rect
.y
+= m_headerWin
->GetSize().y
+ 1;
4618 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4620 m_mainWin
->GetItemPosition( item
, pos
);
4624 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4629 int wxGenericListCtrl::GetItemCount() const
4631 return m_mainWin
->GetItemCount();
4634 int wxGenericListCtrl::GetColumnCount() const
4636 return m_mainWin
->GetColumnCount();
4639 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4641 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4644 wxSize
wxGenericListCtrl::GetItemSpacing() const
4646 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4648 return wxSize(spacing
, spacing
);
4651 #if WXWIN_COMPATIBILITY_2_6
4652 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4654 return m_mainWin
->GetItemSpacing( isSmall
);
4656 #endif // WXWIN_COMPATIBILITY_2_6
4658 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4661 info
.m_itemId
= item
;
4662 info
.SetTextColour( col
);
4663 m_mainWin
->SetItem( info
);
4666 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4669 info
.m_itemId
= item
;
4670 m_mainWin
->GetItem( info
);
4671 return info
.GetTextColour();
4674 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4677 info
.m_itemId
= item
;
4678 info
.SetBackgroundColour( col
);
4679 m_mainWin
->SetItem( info
);
4682 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4685 info
.m_itemId
= item
;
4686 m_mainWin
->GetItem( info
);
4687 return info
.GetBackgroundColour();
4690 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4693 info
.m_itemId
= item
;
4695 m_mainWin
->SetItem( info
);
4698 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4701 info
.m_itemId
= item
;
4702 m_mainWin
->GetItem( info
);
4703 return info
.GetFont();
4706 int wxGenericListCtrl::GetSelectedItemCount() const
4708 return m_mainWin
->GetSelectedItemCount();
4711 wxColour
wxGenericListCtrl::GetTextColour() const
4713 return GetForegroundColour();
4716 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4718 SetForegroundColour(col
);
4721 long wxGenericListCtrl::GetTopItem() const
4724 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4728 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4730 return m_mainWin
->GetNextItem( item
, geom
, state
);
4733 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4735 if (which
== wxIMAGE_LIST_NORMAL
)
4736 return m_imageListNormal
;
4737 else if (which
== wxIMAGE_LIST_SMALL
)
4738 return m_imageListSmall
;
4739 else if (which
== wxIMAGE_LIST_STATE
)
4740 return m_imageListState
;
4745 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4747 if ( which
== wxIMAGE_LIST_NORMAL
)
4749 if (m_ownsImageListNormal
)
4750 delete m_imageListNormal
;
4751 m_imageListNormal
= imageList
;
4752 m_ownsImageListNormal
= false;
4754 else if ( which
== wxIMAGE_LIST_SMALL
)
4756 if (m_ownsImageListSmall
)
4757 delete m_imageListSmall
;
4758 m_imageListSmall
= imageList
;
4759 m_ownsImageListSmall
= false;
4761 else if ( which
== wxIMAGE_LIST_STATE
)
4763 if (m_ownsImageListState
)
4764 delete m_imageListState
;
4765 m_imageListState
= imageList
;
4766 m_ownsImageListState
= false;
4769 m_mainWin
->SetImageList( imageList
, which
);
4772 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4774 SetImageList(imageList
, which
);
4775 if ( which
== wxIMAGE_LIST_NORMAL
)
4776 m_ownsImageListNormal
= true;
4777 else if ( which
== wxIMAGE_LIST_SMALL
)
4778 m_ownsImageListSmall
= true;
4779 else if ( which
== wxIMAGE_LIST_STATE
)
4780 m_ownsImageListState
= true;
4783 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4788 bool wxGenericListCtrl::DeleteItem( long item
)
4790 m_mainWin
->DeleteItem( item
);
4794 bool wxGenericListCtrl::DeleteAllItems()
4796 m_mainWin
->DeleteAllItems();
4800 bool wxGenericListCtrl::DeleteAllColumns()
4802 size_t count
= m_mainWin
->m_columns
.GetCount();
4803 for ( size_t n
= 0; n
< count
; n
++ )
4808 void wxGenericListCtrl::ClearAll()
4810 m_mainWin
->DeleteEverything();
4813 bool wxGenericListCtrl::DeleteColumn( int col
)
4815 m_mainWin
->DeleteColumn( col
);
4817 // if we don't have the header any longer, we need to relayout the window
4818 // if ( !GetColumnCount() )
4823 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4824 wxClassInfo
* textControlClass
)
4826 return m_mainWin
->EditLabel( item
, textControlClass
);
4829 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4831 return m_mainWin
->GetEditControl();
4834 bool wxGenericListCtrl::EnsureVisible( long item
)
4836 m_mainWin
->EnsureVisible( item
);
4840 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4842 return m_mainWin
->FindItem( start
, str
, partial
);
4845 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4847 return m_mainWin
->FindItem( start
, data
);
4850 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4851 int WXUNUSED(direction
))
4853 return m_mainWin
->FindItem( pt
);
4856 // TODO: sub item hit testing
4857 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4859 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4862 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4864 m_mainWin
->InsertItem( info
);
4865 return info
.m_itemId
;
4868 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4871 info
.m_text
= label
;
4872 info
.m_mask
= wxLIST_MASK_TEXT
;
4873 info
.m_itemId
= index
;
4874 return InsertItem( info
);
4877 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4880 info
.m_mask
= wxLIST_MASK_IMAGE
;
4881 info
.m_image
= imageIndex
;
4882 info
.m_itemId
= index
;
4883 return InsertItem( info
);
4886 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4889 info
.m_text
= label
;
4890 info
.m_image
= imageIndex
;
4891 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4892 info
.m_itemId
= index
;
4893 return InsertItem( info
);
4896 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4898 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4900 m_mainWin
->InsertColumn( col
, item
);
4902 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4903 // still have m_headerWin==NULL
4905 m_headerWin
->Refresh();
4910 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4911 int format
, int width
)
4914 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4915 item
.m_text
= heading
;
4918 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4919 item
.m_width
= width
;
4922 item
.m_format
= format
;
4924 return InsertColumn( col
, item
);
4927 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4929 return m_mainWin
->ScrollList(dx
, dy
);
4933 // fn is a function which takes 3 long arguments: item1, item2, data.
4934 // item1 is the long data associated with a first item (NOT the index).
4935 // item2 is the long data associated with a second item (NOT the index).
4936 // data is the same value as passed to SortItems.
4937 // The return value is a negative number if the first item should precede the second
4938 // item, a positive number of the second item should precede the first,
4939 // or zero if the two items are equivalent.
4940 // data is arbitrary data to be passed to the sort function.
4942 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4944 m_mainWin
->SortItems( fn
, data
);
4948 // ----------------------------------------------------------------------------
4950 // ----------------------------------------------------------------------------
4952 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4954 if (!m_mainWin
) return;
4956 // We need to override OnSize so that our scrolled
4957 // window a) does call Layout() to use sizers for
4958 // positioning the controls but b) does not query
4959 // the sizer for their size and use that for setting
4960 // the scrollable area as set that ourselves by
4961 // calling SetScrollbar() further down.
4965 m_mainWin
->RecalculatePositions();
4970 void wxGenericListCtrl::OnInternalIdle()
4972 wxWindow::OnInternalIdle();
4974 if (m_mainWin
->m_dirty
)
4975 m_mainWin
->RecalculatePositions();
4978 // ----------------------------------------------------------------------------
4980 // ----------------------------------------------------------------------------
4982 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4986 m_mainWin
->SetBackgroundColour( colour
);
4987 m_mainWin
->m_dirty
= true;
4993 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4995 if ( !wxWindow::SetForegroundColour( colour
) )
5000 m_mainWin
->SetForegroundColour( colour
);
5001 m_mainWin
->m_dirty
= true;
5005 m_headerWin
->SetForegroundColour( colour
);
5010 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5012 if ( !wxWindow::SetFont( font
) )
5017 m_mainWin
->SetFont( font
);
5018 m_mainWin
->m_dirty
= true;
5023 m_headerWin
->SetFont( font
);
5024 // CalculateAndSetHeaderHeight();
5034 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5037 // Use the same color scheme as wxListBox
5038 return wxListBox::GetClassDefaultAttributes(variant
);
5040 wxUnusedVar(variant
);
5041 wxVisualAttributes attr
;
5042 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5043 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5044 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5049 // ----------------------------------------------------------------------------
5050 // methods forwarded to m_mainWin
5051 // ----------------------------------------------------------------------------
5053 #if wxUSE_DRAG_AND_DROP
5055 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5057 m_mainWin
->SetDropTarget( dropTarget
);
5060 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5062 return m_mainWin
->GetDropTarget();
5067 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5069 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5072 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5074 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5077 wxColour
wxGenericListCtrl::GetForegroundColour() const
5079 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5082 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5085 return m_mainWin
->PopupMenu( menu
, x
, y
);
5091 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5093 m_mainWin
->DoClientToScreen(x
, y
);
5096 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5098 m_mainWin
->DoScreenToClient(x
, y
);
5101 void wxGenericListCtrl::SetFocus()
5103 // The test in window.cpp fails as we are a composite
5104 // window, so it checks against "this", but not m_mainWin.
5105 if ( DoFindFocus() != this )
5106 m_mainWin
->SetFocus();
5109 wxSize
wxGenericListCtrl::DoGetBestSize() const
5111 // Something is better than nothing...
5112 // 100x80 is what the MSW version will get from the default
5113 // wxControl::DoGetBestSize
5114 return wxSize(100, 80);
5117 // ----------------------------------------------------------------------------
5118 // virtual list control support
5119 // ----------------------------------------------------------------------------
5121 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5123 // this is a pure virtual function, in fact - which is not really pure
5124 // because the controls which are not virtual don't need to implement it
5125 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5127 return wxEmptyString
;
5130 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5132 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5134 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5138 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5141 return OnGetItemImage(item
);
5147 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5149 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5150 wxT("invalid item index in OnGetItemAttr()") );
5152 // no attributes by default
5156 void wxGenericListCtrl::SetItemCount(long count
)
5158 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5160 m_mainWin
->SetItemCount(count
);
5163 void wxGenericListCtrl::RefreshItem(long item
)
5165 m_mainWin
->RefreshLine(item
);
5168 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5170 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5173 // Generic wxListCtrl is more or less a container for two other
5174 // windows which drawings are done upon. These are namely
5175 // 'm_headerWin' and 'm_mainWin'.
5176 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5177 // behaviour wxListCtrl has under wxMSW.
5179 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5183 // The easy case, no rectangle specified.
5185 m_headerWin
->Refresh(eraseBackground
);
5188 m_mainWin
->Refresh(eraseBackground
);
5192 // Refresh the header window
5195 wxRect rectHeader
= m_headerWin
->GetRect();
5196 rectHeader
.Intersect(*rect
);
5197 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5200 m_headerWin
->GetPosition(&x
, &y
);
5201 rectHeader
.Offset(-x
, -y
);
5202 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5206 // Refresh the main window
5209 wxRect rectMain
= m_mainWin
->GetRect();
5210 rectMain
.Intersect(*rect
);
5211 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5214 m_mainWin
->GetPosition(&x
, &y
);
5215 rectMain
.Offset(-x
, -y
);
5216 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5222 void wxGenericListCtrl::Update()
5226 if ( m_mainWin
->m_dirty
)
5227 m_mainWin
->RecalculatePositions();
5229 m_mainWin
->Update();
5233 m_headerWin
->Update();
5236 #endif // wxUSE_LISTCTRL