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
- 4 );
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
- 4 - iy
)/2,
1174 wxIMAGELIST_DRAW_TRANSPARENT
1178 dc
.DrawText( item
.GetText(),
1179 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
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::CacheLineData(size_t line
)
1655 wxGenericListCtrl
*listctrl
= GetListCtrl();
1657 wxListLineData
*ld
= GetDummyLine();
1659 size_t countCol
= GetColumnCount();
1660 for ( size_t col
= 0; col
< countCol
; col
++ )
1662 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1663 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1666 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1669 wxListLineData
*wxListMainWindow::GetDummyLine() const
1671 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1672 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1674 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1676 // we need to recreate the dummy line if the number of columns in the
1677 // control changed as it would have the incorrect number of fields
1679 if ( !m_lines
.IsEmpty() &&
1680 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1682 self
->m_lines
.Clear();
1685 if ( m_lines
.IsEmpty() )
1687 wxListLineData
*line
= new wxListLineData(self
);
1688 self
->m_lines
.Add(line
);
1690 // don't waste extra memory -- there never going to be anything
1691 // else/more in this array
1692 self
->m_lines
.Shrink();
1698 // ----------------------------------------------------------------------------
1699 // line geometry (report mode only)
1700 // ----------------------------------------------------------------------------
1702 wxCoord
wxListMainWindow::GetLineHeight() const
1704 // we cache the line height as calling GetTextExtent() is slow
1705 if ( !m_lineHeight
)
1707 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1709 wxClientDC
dc( self
);
1710 dc
.SetFont( GetFont() );
1713 dc
.GetTextExtent(wxT("H"), NULL
, &y
);
1715 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1718 m_small_image_list
->GetSize(0, iw
, ih
);
1723 self
->m_lineHeight
= y
+ LINE_SPACING
;
1726 return m_lineHeight
;
1729 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1731 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1733 return LINE_SPACING
+ line
* GetLineHeight();
1736 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1738 if ( !InReportView() )
1739 return GetLine(line
)->m_gi
->m_rectAll
;
1742 rect
.x
= HEADER_OFFSET_X
;
1743 rect
.y
= GetLineY(line
);
1744 rect
.width
= GetHeaderWidth();
1745 rect
.height
= GetLineHeight();
1750 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1752 if ( !InReportView() )
1753 return GetLine(line
)->m_gi
->m_rectLabel
;
1756 wxListLineData
*data
= GetLine(line
);
1757 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1760 wxListItemData
*item
= node
->GetData();
1761 if ( item
->HasImage() )
1764 GetImageSize( item
->GetImage(), ix
, iy
);
1765 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1770 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1771 rect
.y
= GetLineY(line
);
1772 rect
.width
= GetColumnWidth(0) - image_x
;
1773 rect
.height
= GetLineHeight();
1778 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1780 if ( !InReportView() )
1781 return GetLine(line
)->m_gi
->m_rectIcon
;
1783 wxListLineData
*ld
= GetLine(line
);
1784 wxASSERT_MSG( ld
->HasImage(), wxT("should have an image") );
1787 rect
.x
= HEADER_OFFSET_X
;
1788 rect
.y
= GetLineY(line
);
1789 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1794 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1796 return InReportView() ? GetLineRect(line
)
1797 : GetLine(line
)->m_gi
->m_rectHighlight
;
1800 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1802 wxASSERT_MSG( line
< GetItemCount(), wxT("invalid line in HitTestLine") );
1804 wxListLineData
*ld
= GetLine(line
);
1806 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1807 return wxLIST_HITTEST_ONITEMICON
;
1809 // VS: Testing for "ld->HasText() || InReportView()" instead of
1810 // "ld->HasText()" is needed to make empty lines in report view
1812 if ( ld
->HasText() || InReportView() )
1814 wxRect rect
= InReportView() ? GetLineRect(line
)
1815 : GetLineLabelRect(line
);
1817 if ( rect
.Contains(x
, y
) )
1818 return wxLIST_HITTEST_ONITEMLABEL
;
1824 // ----------------------------------------------------------------------------
1825 // highlight (selection) handling
1826 // ----------------------------------------------------------------------------
1828 bool wxListMainWindow::IsHighlighted(size_t line
) const
1832 return m_selStore
.IsSelected(line
);
1836 wxListLineData
*ld
= GetLine(line
);
1837 wxCHECK_MSG( ld
, false, wxT("invalid index in IsHighlighted") );
1839 return ld
->IsHighlighted();
1843 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1849 wxArrayInt linesChanged
;
1850 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1853 // meny items changed state, refresh everything
1854 RefreshLines(lineFrom
, lineTo
);
1856 else // only a few items changed state, refresh only them
1858 size_t count
= linesChanged
.GetCount();
1859 for ( size_t n
= 0; n
< count
; n
++ )
1861 RefreshLine(linesChanged
[n
]);
1865 else // iterate over all items in non report view
1867 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1869 if ( HighlightLine(line
, highlight
) )
1875 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1881 changed
= m_selStore
.SelectItem(line
, highlight
);
1885 wxListLineData
*ld
= GetLine(line
);
1886 wxCHECK_MSG( ld
, false, wxT("invalid index in HighlightLine") );
1888 changed
= ld
->Highlight(highlight
);
1893 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1894 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1900 void wxListMainWindow::RefreshLine( size_t line
)
1902 if ( InReportView() )
1904 size_t visibleFrom
, visibleTo
;
1905 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1907 if ( line
< visibleFrom
|| line
> visibleTo
)
1911 wxRect rect
= GetLineRect(line
);
1913 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1914 RefreshRect( rect
);
1917 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1919 // we suppose that they are ordered by caller
1920 wxASSERT_MSG( lineFrom
<= lineTo
, wxT("indices in disorder") );
1922 wxASSERT_MSG( lineTo
< GetItemCount(), wxT("invalid line range") );
1924 if ( InReportView() )
1926 size_t visibleFrom
, visibleTo
;
1927 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1929 if ( lineFrom
< visibleFrom
)
1930 lineFrom
= visibleFrom
;
1931 if ( lineTo
> visibleTo
)
1936 rect
.y
= GetLineY(lineFrom
);
1937 rect
.width
= GetClientSize().x
;
1938 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1940 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1941 RefreshRect( rect
);
1945 // TODO: this should be optimized...
1946 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1953 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1955 if ( InReportView() )
1957 size_t visibleFrom
, visibleTo
;
1958 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1960 if ( lineFrom
< visibleFrom
)
1961 lineFrom
= visibleFrom
;
1962 else if ( lineFrom
> visibleTo
)
1967 rect
.y
= GetLineY(lineFrom
);
1968 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1970 wxSize size
= GetClientSize();
1971 rect
.width
= size
.x
;
1973 // refresh till the bottom of the window
1974 rect
.height
= size
.y
- rect
.y
;
1976 RefreshRect( rect
);
1980 // TODO: how to do it more efficiently?
1985 void wxListMainWindow::RefreshSelected()
1991 if ( InReportView() )
1993 GetVisibleLinesRange(&from
, &to
);
1998 to
= GetItemCount() - 1;
2001 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2002 RefreshLine(m_current
);
2004 for ( size_t line
= from
; line
<= to
; line
++ )
2006 // NB: the test works as expected even if m_current == -1
2007 if ( line
!= m_current
&& IsHighlighted(line
) )
2012 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2014 // Note: a wxPaintDC must be constructed even if no drawing is
2015 // done (a Windows requirement).
2016 wxPaintDC
dc( this );
2020 // nothing to draw or not the moment to draw it
2025 RecalculatePositions( false );
2027 GetListCtrl()->PrepareDC( dc
);
2030 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2032 dc
.SetFont( GetFont() );
2034 if ( InReportView() )
2036 int lineHeight
= GetLineHeight();
2038 size_t visibleFrom
, visibleTo
;
2039 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2042 int xOrig
= dc
.LogicalToDeviceX( 0 );
2043 int yOrig
= dc
.LogicalToDeviceY( 0 );
2045 // tell the caller cache to cache the data
2048 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2049 GetParent()->GetId());
2050 evCache
.SetEventObject( GetParent() );
2051 evCache
.m_oldItemIndex
= visibleFrom
;
2052 evCache
.m_itemIndex
= visibleTo
;
2053 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2056 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2058 rectLine
= GetLineRect(line
);
2061 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2062 rectLine
.width
, rectLine
.height
) )
2064 // don't redraw unaffected lines to avoid flicker
2068 GetLine(line
)->DrawInReportMode( &dc
,
2070 GetLineHighlightRect(line
),
2071 IsHighlighted(line
),
2072 line
== m_current
);
2075 if ( HasFlag(wxLC_HRULES
) )
2077 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2078 wxSize clientSize
= GetClientSize();
2080 size_t i
= visibleFrom
;
2081 if (i
== 0) i
= 1; // Don't draw the first one
2082 for ( ; i
<= visibleTo
; i
++ )
2085 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2086 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2087 clientSize
.x
- dev_x
, i
* lineHeight
);
2090 // Draw last horizontal rule
2091 if ( visibleTo
== GetItemCount() - 1 )
2094 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2095 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2096 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2100 // Draw vertical rules if required
2101 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2103 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2104 wxRect firstItemRect
, lastItemRect
;
2106 GetItemRect(visibleFrom
, firstItemRect
);
2107 GetItemRect(visibleTo
, lastItemRect
);
2108 int x
= firstItemRect
.GetX();
2110 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2112 for (int col
= 0; col
< GetColumnCount(); col
++)
2114 int colWidth
= GetColumnWidth(col
);
2116 int x_pos
= x
- dev_x
;
2117 if (col
< GetColumnCount()-1) x_pos
-= 2;
2118 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2119 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2125 size_t count
= GetItemCount();
2126 for ( size_t i
= 0; i
< count
; i
++ )
2128 GetLine(i
)->Draw( &dc
);
2132 #if !defined( __WXMAC__) && !defined(__WXGTK20__)
2133 // Don't draw rect outline under Mac at all.
2134 // Draw it elsewhere under GTK.
2139 wxRect
rect( GetLineHighlightRect( m_current
) );
2140 dc
.SetPen( *wxBLACK_PEN
);
2141 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2142 dc
.DrawRectangle( rect
);
2148 void wxListMainWindow::HighlightAll( bool on
)
2150 if ( IsSingleSel() )
2152 wxASSERT_MSG( !on
, wxT("can't do this in a single selection control") );
2154 // we just have one item to turn off
2155 if ( HasCurrent() && IsHighlighted(m_current
) )
2157 HighlightLine(m_current
, false);
2158 RefreshLine(m_current
);
2161 else // multi selection
2164 HighlightLines(0, GetItemCount() - 1, on
);
2168 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2170 // Do nothing here. This prevents the default handler in wxScrolledWindow
2171 // from needlessly scrolling the window when the edit control is
2172 // dismissed. See ticket #9563.
2175 void wxListMainWindow::SendNotify( size_t line
,
2176 wxEventType command
,
2177 const wxPoint
& point
)
2179 wxListEvent
le( command
, GetParent()->GetId() );
2180 le
.SetEventObject( GetParent() );
2182 le
.m_itemIndex
= line
;
2184 // set only for events which have position
2185 if ( point
!= wxDefaultPosition
)
2186 le
.m_pointDrag
= point
;
2188 // don't try to get the line info for virtual list controls: the main
2189 // program has it anyhow and if we did it would result in accessing all
2190 // the lines, even those which are not visible now and this is precisely
2191 // what we're trying to avoid
2194 if ( line
!= (size_t)-1 )
2196 GetLine(line
)->GetItem( 0, le
.m_item
);
2198 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2200 //else: there may be no more such item
2202 GetParent()->GetEventHandler()->ProcessEvent( le
);
2205 void wxListMainWindow::ChangeCurrent(size_t current
)
2207 m_current
= current
;
2209 // as the current item changed, we shouldn't start editing it when the
2210 // "slow click" timer expires as the click happened on another item
2211 if ( m_renameTimer
->IsRunning() )
2212 m_renameTimer
->Stop();
2214 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2217 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2219 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2220 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2222 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2223 wxT("EditLabel() needs a text control") );
2225 size_t itemEdit
= (size_t)item
;
2227 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2228 le
.SetEventObject( GetParent() );
2229 le
.m_itemIndex
= item
;
2230 wxListLineData
*data
= GetLine(itemEdit
);
2231 wxCHECK_MSG( data
, NULL
, wxT("invalid index in EditLabel()") );
2232 data
->GetItem( 0, le
.m_item
);
2234 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2236 // vetoed by user code
2240 // We have to call this here because the label in question might just have
2241 // been added and no screen update taken place.
2244 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2245 // so that no pending events may change the item count (see below)
2246 // IMPORTANT: needs to be tested!
2249 // Pending events dispatched by wxSafeYield might have changed the item
2251 if ( (size_t)item
>= GetItemCount() )
2255 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2256 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2257 return m_textctrlWrapper
->GetText();
2260 void wxListMainWindow::OnRenameTimer()
2262 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2264 EditLabel( m_current
);
2267 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2269 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2270 le
.SetEventObject( GetParent() );
2271 le
.m_itemIndex
= itemEdit
;
2273 wxListLineData
*data
= GetLine(itemEdit
);
2275 wxCHECK_MSG( data
, false, wxT("invalid index in OnRenameAccept()") );
2277 data
->GetItem( 0, le
.m_item
);
2278 le
.m_item
.m_text
= value
;
2279 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2283 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2285 // let owner know that the edit was cancelled
2286 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2288 le
.SetEditCanceled(true);
2290 le
.SetEventObject( GetParent() );
2291 le
.m_itemIndex
= itemEdit
;
2293 wxListLineData
*data
= GetLine(itemEdit
);
2294 wxCHECK_RET( data
, wxT("invalid index in OnRenameCancelled()") );
2296 data
->GetItem( 0, le
.m_item
);
2297 GetEventHandler()->ProcessEvent( le
);
2300 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2303 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2304 // shutdown the edit control when the mouse is clicked elsewhere on the
2305 // listctrl because the order of events is different (or something like
2306 // that), so explicitly end the edit if it is active.
2307 if ( event
.LeftDown() && m_textctrlWrapper
)
2308 m_textctrlWrapper
->EndEdit( false );
2311 if ( event
.LeftDown() )
2314 event
.SetEventObject( GetParent() );
2315 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2318 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2320 // let the base class handle mouse wheel events.
2325 if ( !HasCurrent() || IsEmpty() )
2327 if (event
.RightDown())
2329 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2331 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2332 GetParent()->GetId(),
2333 ClientToScreen(event
.GetPosition()));
2334 evtCtx
.SetEventObject(GetParent());
2335 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2343 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2344 event
.ButtonDClick()) )
2347 int x
= event
.GetX();
2348 int y
= event
.GetY();
2349 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2351 // where did we hit it (if we did)?
2354 size_t count
= GetItemCount(),
2357 if ( InReportView() )
2359 current
= y
/ GetLineHeight();
2360 if ( current
< count
)
2361 hitResult
= HitTestLine(current
, x
, y
);
2365 // TODO: optimize it too! this is less simple than for report view but
2366 // enumerating all items is still not a way to do it!!
2367 for ( current
= 0; current
< count
; current
++ )
2369 hitResult
= HitTestLine(current
, x
, y
);
2375 if (event
.Dragging())
2377 if (m_dragCount
== 0)
2379 // we have to report the raw, physical coords as we want to be
2380 // able to call HitTest(event.m_pointDrag) from the user code to
2381 // get the item being dragged
2382 m_dragStart
= event
.GetPosition();
2387 if (m_dragCount
!= 3)
2390 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2391 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2393 wxListEvent
le( command
, GetParent()->GetId() );
2394 le
.SetEventObject( GetParent() );
2395 le
.m_itemIndex
= m_lineLastClicked
;
2396 le
.m_pointDrag
= m_dragStart
;
2397 GetParent()->GetEventHandler()->ProcessEvent( le
);
2408 // outside of any item
2409 if (event
.RightDown())
2411 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2413 wxContextMenuEvent
evtCtx(
2415 GetParent()->GetId(),
2416 ClientToScreen(event
.GetPosition()));
2417 evtCtx
.SetEventObject(GetParent());
2418 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2422 // reset the selection and bail out
2423 HighlightAll(false);
2429 bool forceClick
= false;
2430 if (event
.ButtonDClick())
2432 if ( m_renameTimer
->IsRunning() )
2433 m_renameTimer
->Stop();
2435 m_lastOnSame
= false;
2437 if ( current
== m_lineLastClicked
)
2439 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2445 // The first click was on another item, so don't interpret this as
2446 // a double click, but as a simple click instead
2453 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2455 // select single line
2456 HighlightAll( false );
2457 ReverseHighlight(m_lineSelectSingleOnUp
);
2462 if ((current
== m_current
) &&
2463 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2464 HasFlag(wxLC_EDIT_LABELS
) )
2466 if ( !InReportView() ||
2467 GetLineLabelRect(current
).Contains(x
, y
) )
2469 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2470 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2475 m_lastOnSame
= false;
2476 m_lineSelectSingleOnUp
= (size_t)-1;
2480 // This is necessary, because after a DnD operation in
2481 // from and to ourself, the up event is swallowed by the
2482 // DnD code. So on next non-up event (which means here and
2483 // now) m_lineSelectSingleOnUp should be reset.
2484 m_lineSelectSingleOnUp
= (size_t)-1;
2486 if (event
.RightDown())
2488 m_lineBeforeLastClicked
= m_lineLastClicked
;
2489 m_lineLastClicked
= current
;
2491 // If the item is already selected, do not update the selection.
2492 // Multi-selections should not be cleared if a selected item is clicked.
2493 if (!IsHighlighted(current
))
2495 HighlightAll(false);
2496 ChangeCurrent(current
);
2497 ReverseHighlight(m_current
);
2500 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2502 // Allow generation of context menu event
2505 else if (event
.MiddleDown())
2507 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2509 else if ( event
.LeftDown() || forceClick
)
2511 m_lineBeforeLastClicked
= m_lineLastClicked
;
2512 m_lineLastClicked
= current
;
2514 size_t oldCurrent
= m_current
;
2515 bool oldWasSelected
= IsHighlighted(m_current
);
2517 bool cmdModifierDown
= event
.CmdDown();
2518 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2520 if ( IsSingleSel() || !IsHighlighted(current
) )
2522 HighlightAll( false );
2524 ChangeCurrent(current
);
2526 ReverseHighlight(m_current
);
2528 else // multi sel & current is highlighted & no mod keys
2530 m_lineSelectSingleOnUp
= current
;
2531 ChangeCurrent(current
); // change focus
2534 else // multi sel & either ctrl or shift is down
2536 if (cmdModifierDown
)
2538 ChangeCurrent(current
);
2540 ReverseHighlight(m_current
);
2542 else if (event
.ShiftDown())
2544 ChangeCurrent(current
);
2546 size_t lineFrom
= oldCurrent
,
2549 if ( lineTo
< lineFrom
)
2552 lineFrom
= m_current
;
2555 HighlightLines(lineFrom
, lineTo
);
2557 else // !ctrl, !shift
2559 // test in the enclosing if should make it impossible
2560 wxFAIL_MSG( wxT("how did we get here?") );
2564 if (m_current
!= oldCurrent
)
2565 RefreshLine( oldCurrent
);
2567 // forceClick is only set if the previous click was on another item
2568 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2572 void wxListMainWindow::MoveToItem(size_t item
)
2574 if ( item
== (size_t)-1 )
2577 wxRect rect
= GetLineRect(item
);
2579 int client_w
, client_h
;
2580 GetClientSize( &client_w
, &client_h
);
2582 const int hLine
= GetLineHeight();
2584 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2585 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2587 if ( InReportView() )
2589 // the next we need the range of lines shown it might be different,
2590 // so recalculate it
2591 ResetVisibleLinesRange();
2593 if (rect
.y
< view_y
)
2594 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2595 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2596 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2599 // At least on Mac the visible lines value will get reset inside of
2600 // Scroll *before* it actually scrolls the window because of the
2601 // Update() that happens there, so it will still have the wrong value.
2602 // So let's reset it again and wait for it to be recalculated in the
2603 // next paint event. I would expect this problem to show up in wxGTK
2604 // too but couldn't duplicate it there. Perhaps the order of events
2605 // is different... --Robin
2606 ResetVisibleLinesRange();
2614 if (rect
.x
-view_x
< 5)
2615 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2616 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2617 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2619 if (rect
.y
-view_y
< 5)
2620 sy
= (rect
.y
- 5) / hLine
;
2621 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2622 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2624 GetListCtrl()->Scroll(sx
, sy
);
2628 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2630 if ( !InReportView() )
2632 // TODO: this should work in all views but is not implemented now
2637 GetVisibleLinesRange(&top
, &bottom
);
2639 if ( bottom
== (size_t)-1 )
2642 ResetVisibleLinesRange();
2644 int hLine
= GetLineHeight();
2646 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2649 // see comment in MoveToItem() for why we do this
2650 ResetVisibleLinesRange();
2656 // ----------------------------------------------------------------------------
2657 // keyboard handling
2658 // ----------------------------------------------------------------------------
2660 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2662 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2663 wxT("invalid item index in OnArrowChar()") );
2665 size_t oldCurrent
= m_current
;
2667 // in single selection we just ignore Shift as we can't select several
2669 if ( event
.ShiftDown() && !IsSingleSel() )
2671 ChangeCurrent(newCurrent
);
2673 // refresh the old focus to remove it
2674 RefreshLine( oldCurrent
);
2676 // select all the items between the old and the new one
2677 if ( oldCurrent
> newCurrent
)
2679 newCurrent
= oldCurrent
;
2680 oldCurrent
= m_current
;
2683 HighlightLines(oldCurrent
, newCurrent
);
2687 // all previously selected items are unselected unless ctrl is held
2688 // in a multiselection control
2689 if ( !event
.ControlDown() || IsSingleSel() )
2690 HighlightAll(false);
2692 ChangeCurrent(newCurrent
);
2694 // refresh the old focus to remove it
2695 RefreshLine( oldCurrent
);
2697 // in single selection mode we must always have a selected item
2698 if ( !event
.ControlDown() || IsSingleSel() )
2699 HighlightLine( m_current
, true );
2702 RefreshLine( m_current
);
2707 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2709 wxWindow
*parent
= GetParent();
2711 // propagate the key event upwards
2712 wxKeyEvent
ke(event
);
2713 ke
.SetEventObject( parent
);
2714 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2720 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2722 wxWindow
*parent
= GetParent();
2724 // propagate the key event upwards
2725 wxKeyEvent
ke(event
);
2726 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2732 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2734 wxWindow
*parent
= GetParent();
2736 // send a list_key event up
2739 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2740 le
.m_itemIndex
= m_current
;
2741 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2742 le
.m_code
= event
.GetKeyCode();
2743 le
.SetEventObject( parent
);
2744 parent
->GetEventHandler()->ProcessEvent( le
);
2747 if ( (event
.GetKeyCode() != WXK_UP
) &&
2748 (event
.GetKeyCode() != WXK_DOWN
) &&
2749 (event
.GetKeyCode() != WXK_RIGHT
) &&
2750 (event
.GetKeyCode() != WXK_LEFT
) &&
2751 (event
.GetKeyCode() != WXK_PAGEUP
) &&
2752 (event
.GetKeyCode() != WXK_PAGEDOWN
) &&
2753 (event
.GetKeyCode() != WXK_END
) &&
2754 (event
.GetKeyCode() != WXK_HOME
) )
2756 // propagate the char event upwards
2757 wxKeyEvent
ke(event
);
2758 ke
.SetEventObject( parent
);
2759 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2763 if ( HandleAsNavigationKey(event
) )
2766 // no item -> nothing to do
2773 // don't use m_linesPerPage directly as it might not be computed yet
2774 const int pageSize
= GetCountPerPage();
2775 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
2777 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2779 if (event
.GetKeyCode() == WXK_RIGHT
)
2780 event
.m_keyCode
= WXK_LEFT
;
2781 else if (event
.GetKeyCode() == WXK_LEFT
)
2782 event
.m_keyCode
= WXK_RIGHT
;
2785 switch ( event
.GetKeyCode() )
2788 if ( m_current
> 0 )
2789 OnArrowChar( m_current
- 1, event
);
2793 if ( m_current
< (size_t)GetItemCount() - 1 )
2794 OnArrowChar( m_current
+ 1, event
);
2799 OnArrowChar( GetItemCount() - 1, event
);
2804 OnArrowChar( 0, event
);
2809 int steps
= InReportView() ? pageSize
- 1
2810 : m_current
% pageSize
;
2812 int index
= m_current
- steps
;
2816 OnArrowChar( index
, event
);
2822 int steps
= InReportView()
2824 : pageSize
- (m_current
% pageSize
) - 1;
2826 size_t index
= m_current
+ steps
;
2827 size_t count
= GetItemCount();
2828 if ( index
>= count
)
2831 OnArrowChar( index
, event
);
2836 if ( !InReportView() )
2838 int index
= m_current
- pageSize
;
2842 OnArrowChar( index
, event
);
2847 if ( !InReportView() )
2849 size_t index
= m_current
+ pageSize
;
2851 size_t count
= GetItemCount();
2852 if ( index
>= count
)
2855 OnArrowChar( index
, event
);
2860 if ( IsSingleSel() )
2862 if ( event
.ControlDown() )
2864 ReverseHighlight(m_current
);
2866 else // normal space press
2868 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2871 else // multiple selection
2873 ReverseHighlight(m_current
);
2879 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2887 // ----------------------------------------------------------------------------
2889 // ----------------------------------------------------------------------------
2891 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2895 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2896 event
.SetEventObject( GetParent() );
2897 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2901 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2902 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2903 // which are already drawn correctly resulting in horrible flicker - avoid
2913 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2917 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2918 event
.SetEventObject( GetParent() );
2919 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2927 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2929 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2931 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2933 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2935 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2937 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2939 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2941 else if ( InReportView() && (m_small_image_list
))
2943 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2947 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2949 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2951 m_normal_image_list
->GetSize( index
, width
, height
);
2953 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2955 m_small_image_list
->GetSize( index
, width
, height
);
2957 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2959 m_small_image_list
->GetSize( index
, width
, height
);
2961 else if ( InReportView() && m_small_image_list
)
2963 m_small_image_list
->GetSize( index
, width
, height
);
2972 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2974 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2975 dc
.SetFont( GetFont() );
2978 dc
.GetTextExtent( s
, &lw
, NULL
);
2980 return lw
+ AUTOSIZE_COL_MARGIN
;
2983 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2987 // calc the spacing from the icon size
2988 int width
= 0, height
= 0;
2990 if ((imageList
) && (imageList
->GetImageCount()) )
2991 imageList
->GetSize(0, width
, height
);
2993 if (which
== wxIMAGE_LIST_NORMAL
)
2995 m_normal_image_list
= imageList
;
2996 m_normal_spacing
= width
+ 8;
2999 if (which
== wxIMAGE_LIST_SMALL
)
3001 m_small_image_list
= imageList
;
3002 m_small_spacing
= width
+ 14;
3003 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3007 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3011 m_small_spacing
= spacing
;
3013 m_normal_spacing
= spacing
;
3016 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3018 return isSmall
? m_small_spacing
: m_normal_spacing
;
3021 // ----------------------------------------------------------------------------
3023 // ----------------------------------------------------------------------------
3025 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3027 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3029 wxCHECK_RET( node
, wxT("invalid column index in SetColumn") );
3031 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3032 item
.m_width
= GetTextLength( item
.m_text
);
3034 wxListHeaderData
*column
= node
->GetData();
3035 column
->SetItem( item
);
3037 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3039 headerWin
->m_dirty
= true;
3043 // invalidate it as it has to be recalculated
3047 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3049 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3050 wxT("invalid column index") );
3052 wxCHECK_RET( InReportView(),
3053 wxT("SetColumnWidth() can only be called in report mode.") );
3057 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3059 headerWin
->m_dirty
= true;
3061 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3062 wxCHECK_RET( node
, wxT("no column?") );
3064 wxListHeaderData
*column
= node
->GetData();
3066 size_t count
= GetItemCount();
3068 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3070 width
= GetTextLength(column
->GetText());
3071 width
+= 2*EXTRA_WIDTH
;
3073 // check for column header's image availability
3074 const int image
= column
->GetImage();
3077 if ( m_small_image_list
)
3080 m_small_image_list
->GetSize(image
, ix
, iy
);
3081 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3085 else if ( width
== wxLIST_AUTOSIZE
)
3089 // TODO: determine the max width somehow...
3090 width
= WIDTH_COL_DEFAULT
;
3094 wxClientDC
dc(this);
3095 dc
.SetFont( GetFont() );
3097 int max
= AUTOSIZE_COL_MARGIN
;
3099 // if the cached column width isn't valid then recalculate it
3100 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3102 for (size_t i
= 0; i
< count
; i
++)
3104 wxListLineData
*line
= GetLine( i
);
3105 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3107 wxCHECK_RET( n
, wxT("no subitem?") );
3109 wxListItemData
*itemData
= n
->GetData();
3112 itemData
->GetItem(item
);
3113 int itemWidth
= GetItemWidthWithImage(&item
);
3114 if (itemWidth
> max
)
3118 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3119 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3122 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3123 width
= max
+ AUTOSIZE_COL_MARGIN
;
3127 column
->SetWidth( width
);
3129 // invalidate it as it has to be recalculated
3133 int wxListMainWindow::GetHeaderWidth() const
3135 if ( !m_headerWidth
)
3137 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3139 size_t count
= GetColumnCount();
3140 for ( size_t col
= 0; col
< count
; col
++ )
3142 self
->m_headerWidth
+= GetColumnWidth(col
);
3146 return m_headerWidth
;
3149 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3151 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3152 wxCHECK_RET( node
, wxT("invalid column index in GetColumn") );
3154 wxListHeaderData
*column
= node
->GetData();
3155 column
->GetItem( item
);
3158 int wxListMainWindow::GetColumnWidth( int col
) const
3160 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3161 wxCHECK_MSG( node
, 0, wxT("invalid column index") );
3163 wxListHeaderData
*column
= node
->GetData();
3164 return column
->GetWidth();
3167 // ----------------------------------------------------------------------------
3169 // ----------------------------------------------------------------------------
3171 void wxListMainWindow::SetItem( wxListItem
&item
)
3173 long id
= item
.m_itemId
;
3174 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3175 wxT("invalid item index in SetItem") );
3179 wxListLineData
*line
= GetLine((size_t)id
);
3180 line
->SetItem( item
.m_col
, item
);
3182 // Set item state if user wants
3183 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3184 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3188 // update the Max Width Cache if needed
3189 int width
= GetItemWidthWithImage(&item
);
3191 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3192 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3196 // update the item on screen
3198 GetItemRect(id
, rectItem
);
3199 RefreshRect(rectItem
);
3202 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3207 // first deal with selection
3208 if ( stateMask
& wxLIST_STATE_SELECTED
)
3210 // set/clear select state
3213 // optimized version for virtual listctrl.
3214 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3217 else if ( state
& wxLIST_STATE_SELECTED
)
3219 const long count
= GetItemCount();
3220 for( long i
= 0; i
< count
; i
++ )
3222 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3228 // clear for non virtual (somewhat optimized by using GetNextItem())
3230 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3232 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3237 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3239 // unfocus all: only one item can be focussed, so clearing focus for
3240 // all items is simply clearing focus of the focussed item.
3241 SetItemState(m_current
, state
, stateMask
);
3243 //(setting focus to all items makes no sense, so it is not handled here.)
3246 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3250 SetItemStateAll(state
, stateMask
);
3254 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3255 wxT("invalid list ctrl item index in SetItem") );
3257 size_t oldCurrent
= m_current
;
3258 size_t item
= (size_t)litem
; // safe because of the check above
3260 // do we need to change the focus?
3261 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3263 if ( state
& wxLIST_STATE_FOCUSED
)
3265 // don't do anything if this item is already focused
3266 if ( item
!= m_current
)
3268 ChangeCurrent(item
);
3270 if ( oldCurrent
!= (size_t)-1 )
3272 if ( IsSingleSel() )
3274 HighlightLine(oldCurrent
, false);
3277 RefreshLine(oldCurrent
);
3280 RefreshLine( m_current
);
3285 // don't do anything if this item is not focused
3286 if ( item
== m_current
)
3290 if ( IsSingleSel() )
3292 // we must unselect the old current item as well or we
3293 // might end up with more than one selected item in a
3294 // single selection control
3295 HighlightLine(oldCurrent
, false);
3298 RefreshLine( oldCurrent
);
3303 // do we need to change the selection state?
3304 if ( stateMask
& wxLIST_STATE_SELECTED
)
3306 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3308 if ( IsSingleSel() )
3312 // selecting the item also makes it the focused one in the
3314 if ( m_current
!= item
)
3316 ChangeCurrent(item
);
3318 if ( oldCurrent
!= (size_t)-1 )
3320 HighlightLine( oldCurrent
, false );
3321 RefreshLine( oldCurrent
);
3327 // only the current item may be selected anyhow
3328 if ( item
!= m_current
)
3333 if ( HighlightLine(item
, on
) )
3340 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3342 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3343 wxT("invalid list ctrl item index in GetItemState()") );
3345 int ret
= wxLIST_STATE_DONTCARE
;
3347 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3349 if ( (size_t)item
== m_current
)
3350 ret
|= wxLIST_STATE_FOCUSED
;
3353 if ( stateMask
& wxLIST_STATE_SELECTED
)
3355 if ( IsHighlighted(item
) )
3356 ret
|= wxLIST_STATE_SELECTED
;
3362 void wxListMainWindow::GetItem( wxListItem
&item
) const
3364 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3365 wxT("invalid item index in GetItem") );
3367 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3368 line
->GetItem( item
.m_col
, item
);
3370 // Get item state if user wants it
3371 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3372 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3373 wxLIST_STATE_FOCUSED
);
3376 // ----------------------------------------------------------------------------
3378 // ----------------------------------------------------------------------------
3380 size_t wxListMainWindow::GetItemCount() const
3382 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3385 void wxListMainWindow::SetItemCount(long count
)
3387 m_selStore
.SetItemCount(count
);
3388 m_countVirt
= count
;
3390 ResetVisibleLinesRange();
3392 // scrollbars must be reset
3396 int wxListMainWindow::GetSelectedItemCount() const
3398 // deal with the quick case first
3399 if ( IsSingleSel() )
3400 return HasCurrent() ? IsHighlighted(m_current
) : false;
3402 // virtual controls remmebers all its selections itself
3404 return m_selStore
.GetSelectedCount();
3406 // TODO: we probably should maintain the number of items selected even for
3407 // non virtual controls as enumerating all lines is really slow...
3408 size_t countSel
= 0;
3409 size_t count
= GetItemCount();
3410 for ( size_t line
= 0; line
< count
; line
++ )
3412 if ( GetLine(line
)->IsHighlighted() )
3419 // ----------------------------------------------------------------------------
3420 // item position/size
3421 // ----------------------------------------------------------------------------
3423 wxRect
wxListMainWindow::GetViewRect() const
3425 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3427 // we need to find the longest/tallest label
3428 wxCoord xMax
= 0, yMax
= 0;
3429 const int count
= GetItemCount();
3432 for ( int i
= 0; i
< count
; i
++ )
3434 // we need logical, not physical, coordinates here, so use
3435 // GetLineRect() instead of GetItemRect()
3436 wxRect r
= GetLineRect(i
);
3438 wxCoord x
= r
.GetRight(),
3448 // some fudge needed to make it look prettier
3449 xMax
+= 2 * EXTRA_BORDER_X
;
3450 yMax
+= 2 * EXTRA_BORDER_Y
;
3452 // account for the scrollbars if necessary
3453 const wxSize sizeAll
= GetClientSize();
3454 if ( xMax
> sizeAll
.x
)
3455 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3456 if ( yMax
> sizeAll
.y
)
3457 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3459 return wxRect(0, 0, xMax
, yMax
);
3463 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3465 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3467 wxT("GetSubItemRect only meaningful in report view") );
3468 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3469 wxT("invalid item in GetSubItemRect") );
3471 // ensure that we're laid out, otherwise we could return nonsense
3474 wxConstCast(this, wxListMainWindow
)->
3475 RecalculatePositions(true /* no refresh */);
3478 rect
= GetLineRect((size_t)item
);
3480 // Adjust rect to specified column
3481 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3483 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3484 wxT("invalid subItem in GetSubItemRect") );
3486 for (int i
= 0; i
< subItem
; i
++)
3488 rect
.x
+= GetColumnWidth(i
);
3490 rect
.width
= GetColumnWidth(subItem
);
3493 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3498 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3501 GetItemRect(item
, rect
);
3509 // ----------------------------------------------------------------------------
3510 // geometry calculation
3511 // ----------------------------------------------------------------------------
3513 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3515 const int lineHeight
= GetLineHeight();
3517 wxClientDC
dc( this );
3518 dc
.SetFont( GetFont() );
3520 const size_t count
= GetItemCount();
3523 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3524 iconSpacing
= m_normal_spacing
;
3525 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3526 iconSpacing
= m_small_spacing
;
3530 // Note that we do not call GetClientSize() here but
3531 // GetSize() and subtract the border size for sunken
3532 // borders manually. This is technically incorrect,
3533 // but we need to know the client area's size WITHOUT
3534 // scrollbars here. Since we don't know if there are
3535 // any scrollbars, we use GetSize() instead. Another
3536 // solution would be to call SetScrollbars() here to
3537 // remove the scrollbars and call GetClientSize() then,
3538 // but this might result in flicker and - worse - will
3539 // reset the scrollbars to 0 which is not good at all
3540 // if you resize a dialog/window, but don't want to
3541 // reset the window scrolling. RR.
3542 // Furthermore, we actually do NOT subtract the border
3543 // width as 2 pixels is just the extra space which we
3544 // need around the actual content in the window. Other-
3545 // wise the text would e.g. touch the upper border. RR.
3548 GetSize( &clientWidth
, &clientHeight
);
3550 if ( InReportView() )
3552 // all lines have the same height and we scroll one line per step
3553 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3555 m_linesPerPage
= clientHeight
/ lineHeight
;
3557 ResetVisibleLinesRange();
3559 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3560 GetHeaderWidth() / SCROLL_UNIT_X
,
3561 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3562 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3563 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3568 // we have 3 different layout strategies: either layout all items
3569 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3570 // to arrange them in top to bottom, left to right (don't ask me why
3571 // not the other way round...) order
3572 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3574 int x
= EXTRA_BORDER_X
;
3575 int y
= EXTRA_BORDER_Y
;
3577 wxCoord widthMax
= 0;
3580 for ( i
= 0; i
< count
; i
++ )
3582 wxListLineData
*line
= GetLine(i
);
3583 line
->CalculateSize( &dc
, iconSpacing
);
3584 line
->SetPosition( x
, y
, iconSpacing
);
3586 wxSize sizeLine
= GetLineSize(i
);
3588 if ( HasFlag(wxLC_ALIGN_TOP
) )
3590 if ( sizeLine
.x
> widthMax
)
3591 widthMax
= sizeLine
.x
;
3595 else // wxLC_ALIGN_LEFT
3597 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3601 if ( HasFlag(wxLC_ALIGN_TOP
) )
3603 // traverse the items again and tweak their sizes so that they are
3604 // all the same in a row
3605 for ( i
= 0; i
< count
; i
++ )
3607 wxListLineData
*line
= GetLine(i
);
3608 line
->m_gi
->ExtendWidth(widthMax
);
3612 GetListCtrl()->SetScrollbars
3616 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3617 (y
+ lineHeight
) / lineHeight
,
3618 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3619 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3623 else // "flowed" arrangement, the most complicated case
3625 // at first we try without any scrollbars, if the items don't fit into
3626 // the window, we recalculate after subtracting the space taken by the
3629 int entireWidth
= 0;
3631 for (int tries
= 0; tries
< 2; tries
++)
3633 entireWidth
= 2 * EXTRA_BORDER_X
;
3637 // Now we have decided that the items do not fit into the
3638 // client area, so we need a scrollbar
3639 entireWidth
+= SCROLL_UNIT_X
;
3642 int x
= EXTRA_BORDER_X
;
3643 int y
= EXTRA_BORDER_Y
;
3644 int maxWidthInThisRow
= 0;
3647 int currentlyVisibleLines
= 0;
3649 for (size_t i
= 0; i
< count
; i
++)
3651 currentlyVisibleLines
++;
3652 wxListLineData
*line
= GetLine( i
);
3653 line
->CalculateSize( &dc
, iconSpacing
);
3654 line
->SetPosition( x
, y
, iconSpacing
);
3656 wxSize sizeLine
= GetLineSize( i
);
3658 if ( maxWidthInThisRow
< sizeLine
.x
)
3659 maxWidthInThisRow
= sizeLine
.x
;
3662 if (currentlyVisibleLines
> m_linesPerPage
)
3663 m_linesPerPage
= currentlyVisibleLines
;
3665 if ( y
+ sizeLine
.y
>= clientHeight
)
3667 currentlyVisibleLines
= 0;
3669 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3670 x
+= maxWidthInThisRow
;
3671 entireWidth
+= maxWidthInThisRow
;
3672 maxWidthInThisRow
= 0;
3675 // We have reached the last item.
3676 if ( i
== count
- 1 )
3677 entireWidth
+= maxWidthInThisRow
;
3679 if ( (tries
== 0) &&
3680 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3682 clientHeight
-= wxSystemSettings::
3683 GetMetric(wxSYS_HSCROLL_Y
);
3688 if ( i
== count
- 1 )
3689 tries
= 1; // Everything fits, no second try required.
3693 GetListCtrl()->SetScrollbars
3697 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3699 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3708 // FIXME: why should we call it from here?
3715 void wxListMainWindow::RefreshAll()
3720 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3721 if ( headerWin
&& headerWin
->m_dirty
)
3723 headerWin
->m_dirty
= false;
3724 headerWin
->Refresh();
3728 void wxListMainWindow::UpdateCurrent()
3730 if ( !HasCurrent() && !IsEmpty() )
3734 long wxListMainWindow::GetNextItem( long item
,
3735 int WXUNUSED(geometry
),
3739 max
= GetItemCount();
3740 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3741 wxT("invalid listctrl index in GetNextItem()") );
3743 // notice that we start with the next item (or the first one if item == -1)
3744 // and this is intentional to allow writing a simple loop to iterate over
3745 // all selected items
3748 // this is not an error because the index was OK initially,
3749 // just no such item
3756 size_t count
= GetItemCount();
3757 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3759 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3762 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3769 // ----------------------------------------------------------------------------
3771 // ----------------------------------------------------------------------------
3773 void wxListMainWindow::DeleteItem( long lindex
)
3775 size_t count
= GetItemCount();
3777 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3778 wxT("invalid item index in DeleteItem") );
3780 size_t index
= (size_t)lindex
;
3782 // we don't need to adjust the index for the previous items
3783 if ( HasCurrent() && m_current
>= index
)
3785 // if the current item is being deleted, we want the next one to
3786 // become selected - unless there is no next one - so don't adjust
3787 // m_current in this case
3788 if ( m_current
!= index
|| m_current
== count
- 1 )
3792 if ( InReportView() )
3794 // mark the Column Max Width cache as dirty if the items in the line
3795 // we're deleting contain the Max Column Width
3796 wxListLineData
* const line
= GetLine(index
);
3797 wxListItemDataList::compatibility_iterator n
;
3798 wxListItemData
*itemData
;
3802 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3804 n
= line
->m_items
.Item( i
);
3805 itemData
= n
->GetData();
3806 itemData
->GetItem(item
);
3808 itemWidth
= GetItemWidthWithImage(&item
);
3810 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3811 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3814 ResetVisibleLinesRange();
3817 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3822 m_selStore
.OnItemDelete(index
);
3826 m_lines
.RemoveAt( index
);
3829 // we need to refresh the (vert) scrollbar as the number of items changed
3832 RefreshAfter(index
);
3835 void wxListMainWindow::DeleteColumn( int col
)
3837 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3839 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3842 delete node
->GetData();
3843 m_columns
.Erase( node
);
3847 // update all the items
3848 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3850 wxListLineData
* const line
= GetLine(i
);
3851 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3852 delete n
->GetData();
3853 line
->m_items
.Erase(n
);
3857 if ( InReportView() ) // we only cache max widths when in Report View
3859 delete m_aColWidths
.Item(col
);
3860 m_aColWidths
.RemoveAt(col
);
3863 // invalidate it as it has to be recalculated
3867 void wxListMainWindow::DoDeleteAllItems()
3870 // nothing to do - in particular, don't send the event
3875 // to make the deletion of all items faster, we don't send the
3876 // notifications for each item deletion in this case but only one event
3877 // for all of them: this is compatible with wxMSW and documented in
3878 // DeleteAllItems() description
3880 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3881 event
.SetEventObject( GetParent() );
3882 GetParent()->GetEventHandler()->ProcessEvent( event
);
3890 if ( InReportView() )
3892 ResetVisibleLinesRange();
3893 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3895 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3902 void wxListMainWindow::DeleteAllItems()
3906 RecalculatePositions();
3909 void wxListMainWindow::DeleteEverything()
3911 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3912 WX_CLEAR_ARRAY(m_aColWidths
);
3917 // ----------------------------------------------------------------------------
3918 // scanning for an item
3919 // ----------------------------------------------------------------------------
3921 void wxListMainWindow::EnsureVisible( long index
)
3923 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3924 wxT("invalid index in EnsureVisible") );
3926 // We have to call this here because the label in question might just have
3927 // been added and its position is not known yet
3929 RecalculatePositions(true /* no refresh */);
3931 MoveToItem((size_t)index
);
3934 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3940 wxString str_upper
= str
.Upper();
3944 size_t count
= GetItemCount();
3945 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3947 wxListLineData
*line
= GetLine(i
);
3948 wxString line_upper
= line
->GetText(0).Upper();
3951 if (line_upper
== str_upper
)
3956 if (line_upper
.find(str_upper
) == 0)
3964 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3970 size_t count
= GetItemCount();
3971 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3973 wxListLineData
*line
= GetLine(i
);
3975 line
->GetItem( 0, item
);
3976 if (item
.m_data
== data
)
3983 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3986 GetVisibleLinesRange( &topItem
, NULL
);
3989 GetItemPosition( GetItemCount() - 1, p
);
3993 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3994 if ( id
>= 0 && id
< (long)GetItemCount() )
4000 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4002 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
4004 size_t count
= GetItemCount();
4006 if ( InReportView() )
4008 size_t current
= y
/ GetLineHeight();
4009 if ( current
< count
)
4011 flags
= HitTestLine(current
, x
, y
);
4018 // TODO: optimize it too! this is less simple than for report view but
4019 // enumerating all items is still not a way to do it!!
4020 for ( size_t current
= 0; current
< count
; current
++ )
4022 flags
= HitTestLine(current
, x
, y
);
4031 // ----------------------------------------------------------------------------
4033 // ----------------------------------------------------------------------------
4035 void wxListMainWindow::InsertItem( wxListItem
&item
)
4037 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4039 int count
= GetItemCount();
4040 wxCHECK_RET( item
.m_itemId
>= 0, wxT("invalid item index") );
4042 if (item
.m_itemId
> count
)
4043 item
.m_itemId
= count
;
4045 size_t id
= item
.m_itemId
;
4049 if ( InReportView() )
4051 ResetVisibleLinesRange();
4053 // calculate the width of the item and adjust the max column width
4054 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4055 int width
= GetItemWidthWithImage(&item
);
4056 item
.SetWidth(width
);
4057 if (width
> pWidthInfo
->nMaxWidth
)
4058 pWidthInfo
->nMaxWidth
= width
;
4061 wxListLineData
*line
= new wxListLineData(this);
4063 line
->SetItem( item
.m_col
, item
);
4065 m_lines
.Insert( line
, id
);
4069 // If an item is selected at or below the point of insertion, we need to
4070 // increment the member variables because the current row's index has gone
4072 if ( HasCurrent() && m_current
>= id
)
4075 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4077 RefreshLines(id
, GetItemCount() - 1);
4080 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4083 if ( InReportView() )
4085 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4086 item
.m_width
= GetTextLength( item
.m_text
);
4088 wxListHeaderData
*column
= new wxListHeaderData( item
);
4089 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4091 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4094 wxListHeaderDataList::compatibility_iterator
4095 node
= m_columns
.Item( col
);
4096 m_columns
.Insert( node
, column
);
4097 m_aColWidths
.Insert( colWidthInfo
, col
);
4101 m_columns
.Append( column
);
4102 m_aColWidths
.Add( colWidthInfo
);
4107 // update all the items
4108 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4110 wxListLineData
* const line
= GetLine(i
);
4111 wxListItemData
* const data
= new wxListItemData(this);
4113 line
->m_items
.Insert(col
, data
);
4115 line
->m_items
.Append(data
);
4119 // invalidate it as it has to be recalculated
4124 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4127 wxClientDC
dc(this);
4129 dc
.SetFont( GetFont() );
4131 if (item
->GetImage() != -1)
4134 GetImageSize( item
->GetImage(), ix
, iy
);
4138 if (!item
->GetText().empty())
4141 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4148 // ----------------------------------------------------------------------------
4150 // ----------------------------------------------------------------------------
4152 static wxListCtrlCompare list_ctrl_compare_func_2
;
4153 static wxIntPtr list_ctrl_compare_data
;
4155 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4157 wxListLineData
*line1
= *arg1
;
4158 wxListLineData
*line2
= *arg2
;
4160 line1
->GetItem( 0, item
);
4161 wxUIntPtr data1
= item
.m_data
;
4162 line2
->GetItem( 0, item
);
4163 wxUIntPtr data2
= item
.m_data
;
4164 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4167 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4169 // selections won't make sense any more after sorting the items so reset
4171 HighlightAll(false);
4174 list_ctrl_compare_func_2
= fn
;
4175 list_ctrl_compare_data
= data
;
4176 m_lines
.Sort( list_ctrl_compare_func_1
);
4180 // ----------------------------------------------------------------------------
4182 // ----------------------------------------------------------------------------
4184 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4186 // update our idea of which lines are shown when we redraw the window the
4188 ResetVisibleLinesRange();
4190 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4192 wxGenericListCtrl
* lc
= GetListCtrl();
4193 wxCHECK_RET( lc
, wxT("no listctrl window?") );
4195 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4197 lc
->m_headerWin
->Refresh();
4198 lc
->m_headerWin
->Update();
4203 int wxListMainWindow::GetCountPerPage() const
4205 if ( !m_linesPerPage
)
4207 wxConstCast(this, wxListMainWindow
)->
4208 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4211 return m_linesPerPage
;
4214 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4216 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4218 if ( m_lineFrom
== (size_t)-1 )
4220 size_t count
= GetItemCount();
4223 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4225 // this may happen if SetScrollbars() hadn't been called yet
4226 if ( m_lineFrom
>= count
)
4227 m_lineFrom
= count
- 1;
4229 // we redraw one extra line but this is needed to make the redrawing
4230 // logic work when there is a fractional number of lines on screen
4231 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4232 if ( m_lineTo
>= count
)
4233 m_lineTo
= count
- 1;
4235 else // empty control
4238 m_lineTo
= (size_t)-1;
4242 wxASSERT_MSG( IsEmpty() ||
4243 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4244 wxT("GetVisibleLinesRange() returns incorrect result") );
4252 // -------------------------------------------------------------------------------------
4253 // wxGenericListCtrl
4254 // -------------------------------------------------------------------------------------
4256 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4258 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4259 EVT_SIZE(wxGenericListCtrl::OnSize
)
4260 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4263 void wxGenericListCtrl::Init()
4265 m_imageListNormal
= NULL
;
4266 m_imageListSmall
= NULL
;
4267 m_imageListState
= NULL
;
4269 m_ownsImageListNormal
=
4270 m_ownsImageListSmall
=
4271 m_ownsImageListState
= false;
4277 wxGenericListCtrl::~wxGenericListCtrl()
4279 if (m_ownsImageListNormal
)
4280 delete m_imageListNormal
;
4281 if (m_ownsImageListSmall
)
4282 delete m_imageListSmall
;
4283 if (m_ownsImageListState
)
4284 delete m_imageListState
;
4287 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4289 bool needs_header
= HasHeader();
4290 bool has_header
= (m_headerWin
!= NULL
);
4292 if (needs_header
== has_header
)
4297 m_headerWin
= new wxListHeaderWindow
4299 this, wxID_ANY
, m_mainWin
,
4304 wxRendererNative::Get().GetHeaderButtonHeight(this)
4309 #if defined( __WXMAC__ )
4311 font
.CreateSystemFont( wxOSX_SYSTEM_FONT_SMALL
);
4312 m_headerWin
->SetFont( font
);
4315 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4319 GetSizer()->Detach( m_headerWin
);
4327 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4332 const wxValidator
&validator
,
4333 const wxString
&name
)
4337 // just like in other ports, an assert will fail if the user doesn't give any type style:
4338 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4339 wxT("wxListCtrl style should have exactly one mode bit set") );
4341 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4345 style
&= ~wxBORDER_MASK
;
4346 style
|= wxBORDER_THEME
;
4349 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4351 SetTargetWindow( m_mainWin
);
4353 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4354 sizer
->Add( m_mainWin
, 1, wxGROW
);
4357 CreateOrDestroyHeaderWindowAsNeeded();
4359 SetInitialSize(size
);
4364 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4366 return wxBORDER_THEME
;
4369 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4370 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4374 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4376 // we need to process arrows ourselves for scrolling
4377 if ( nMsg
== WM_GETDLGCODE
)
4379 rc
|= DLGC_WANTARROWS
;
4386 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4388 wxSize newsize
= size
;
4390 newsize
.y
-= m_headerWin
->GetSize().y
;
4395 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4397 // update our idea of which lines are shown when we redraw
4398 // the window the next time
4399 m_mainWin
->ResetVisibleLinesRange();
4401 HandleOnScroll( event
);
4403 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4405 m_headerWin
->Refresh();
4406 m_headerWin
->Update();
4410 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4412 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4413 wxT("wxLC_VIRTUAL can't be [un]set") );
4415 long flag
= GetWindowStyle();
4419 if (style
& wxLC_MASK_TYPE
)
4420 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4421 if (style
& wxLC_MASK_ALIGN
)
4422 flag
&= ~wxLC_MASK_ALIGN
;
4423 if (style
& wxLC_MASK_SORT
)
4424 flag
&= ~wxLC_MASK_SORT
;
4432 // some styles can be set without recreating everything (as happens in
4433 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4434 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4437 wxWindow::SetWindowStyleFlag(flag
);
4441 SetWindowStyleFlag( flag
);
4445 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4447 // update the window style first so that the header is created or destroyed
4448 // corresponding to the new style
4449 wxWindow::SetWindowStyleFlag( flag
);
4453 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4455 CreateOrDestroyHeaderWindowAsNeeded();
4457 GetSizer()->Layout();
4461 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4463 m_mainWin
->GetColumn( col
, item
);
4467 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4469 m_mainWin
->SetColumn( col
, item
);
4473 int wxGenericListCtrl::GetColumnWidth( int col
) const
4475 return m_mainWin
->GetColumnWidth( col
);
4478 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4480 m_mainWin
->SetColumnWidth( col
, width
);
4484 int wxGenericListCtrl::GetCountPerPage() const
4486 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4489 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4491 m_mainWin
->GetItem( info
);
4495 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4497 m_mainWin
->SetItem( info
);
4501 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4504 info
.m_text
= label
;
4505 info
.m_mask
= wxLIST_MASK_TEXT
;
4506 info
.m_itemId
= index
;
4510 info
.m_image
= imageId
;
4511 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4514 m_mainWin
->SetItem(info
);
4518 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4520 return m_mainWin
->GetItemState( item
, stateMask
);
4523 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4525 m_mainWin
->SetItemState( item
, state
, stateMask
);
4530 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4532 return SetItemColumnImage(item
, 0, image
);
4536 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4539 info
.m_image
= image
;
4540 info
.m_mask
= wxLIST_MASK_IMAGE
;
4541 info
.m_itemId
= item
;
4542 info
.m_col
= column
;
4543 m_mainWin
->SetItem( info
);
4547 wxString
wxGenericListCtrl::GetItemText( long item
) const
4549 return m_mainWin
->GetItemText(item
);
4552 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4554 m_mainWin
->SetItemText(item
, str
);
4557 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4560 info
.m_mask
= wxLIST_MASK_DATA
;
4561 info
.m_itemId
= item
;
4562 m_mainWin
->GetItem( info
);
4566 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4569 info
.m_mask
= wxLIST_MASK_DATA
;
4570 info
.m_itemId
= item
;
4572 m_mainWin
->SetItem( info
);
4576 wxRect
wxGenericListCtrl::GetViewRect() const
4578 return m_mainWin
->GetViewRect();
4581 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4583 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4586 bool wxGenericListCtrl::GetSubItemRect(long item
,
4589 int WXUNUSED(code
)) const
4591 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4594 if ( m_mainWin
->HasHeader() )
4595 rect
.y
+= m_headerWin
->GetSize().y
+ 1;
4600 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4602 m_mainWin
->GetItemPosition( item
, pos
);
4606 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4611 int wxGenericListCtrl::GetItemCount() const
4613 return m_mainWin
->GetItemCount();
4616 int wxGenericListCtrl::GetColumnCount() const
4618 return m_mainWin
->GetColumnCount();
4621 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4623 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4626 wxSize
wxGenericListCtrl::GetItemSpacing() const
4628 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4630 return wxSize(spacing
, spacing
);
4633 #if WXWIN_COMPATIBILITY_2_6
4634 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4636 return m_mainWin
->GetItemSpacing( isSmall
);
4638 #endif // WXWIN_COMPATIBILITY_2_6
4640 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4643 info
.m_itemId
= item
;
4644 info
.SetTextColour( col
);
4645 m_mainWin
->SetItem( info
);
4648 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4651 info
.m_itemId
= item
;
4652 m_mainWin
->GetItem( info
);
4653 return info
.GetTextColour();
4656 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4659 info
.m_itemId
= item
;
4660 info
.SetBackgroundColour( col
);
4661 m_mainWin
->SetItem( info
);
4664 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4667 info
.m_itemId
= item
;
4668 m_mainWin
->GetItem( info
);
4669 return info
.GetBackgroundColour();
4672 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4675 info
.m_itemId
= item
;
4677 m_mainWin
->SetItem( info
);
4680 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4683 info
.m_itemId
= item
;
4684 m_mainWin
->GetItem( info
);
4685 return info
.GetFont();
4688 int wxGenericListCtrl::GetSelectedItemCount() const
4690 return m_mainWin
->GetSelectedItemCount();
4693 wxColour
wxGenericListCtrl::GetTextColour() const
4695 return GetForegroundColour();
4698 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4700 SetForegroundColour(col
);
4703 long wxGenericListCtrl::GetTopItem() const
4706 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4710 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4712 return m_mainWin
->GetNextItem( item
, geom
, state
);
4715 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4717 if (which
== wxIMAGE_LIST_NORMAL
)
4718 return m_imageListNormal
;
4719 else if (which
== wxIMAGE_LIST_SMALL
)
4720 return m_imageListSmall
;
4721 else if (which
== wxIMAGE_LIST_STATE
)
4722 return m_imageListState
;
4727 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4729 if ( which
== wxIMAGE_LIST_NORMAL
)
4731 if (m_ownsImageListNormal
)
4732 delete m_imageListNormal
;
4733 m_imageListNormal
= imageList
;
4734 m_ownsImageListNormal
= false;
4736 else if ( which
== wxIMAGE_LIST_SMALL
)
4738 if (m_ownsImageListSmall
)
4739 delete m_imageListSmall
;
4740 m_imageListSmall
= imageList
;
4741 m_ownsImageListSmall
= false;
4743 else if ( which
== wxIMAGE_LIST_STATE
)
4745 if (m_ownsImageListState
)
4746 delete m_imageListState
;
4747 m_imageListState
= imageList
;
4748 m_ownsImageListState
= false;
4751 m_mainWin
->SetImageList( imageList
, which
);
4754 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4756 SetImageList(imageList
, which
);
4757 if ( which
== wxIMAGE_LIST_NORMAL
)
4758 m_ownsImageListNormal
= true;
4759 else if ( which
== wxIMAGE_LIST_SMALL
)
4760 m_ownsImageListSmall
= true;
4761 else if ( which
== wxIMAGE_LIST_STATE
)
4762 m_ownsImageListState
= true;
4765 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4770 bool wxGenericListCtrl::DeleteItem( long item
)
4772 m_mainWin
->DeleteItem( item
);
4776 bool wxGenericListCtrl::DeleteAllItems()
4778 m_mainWin
->DeleteAllItems();
4782 bool wxGenericListCtrl::DeleteAllColumns()
4784 size_t count
= m_mainWin
->m_columns
.GetCount();
4785 for ( size_t n
= 0; n
< count
; n
++ )
4790 void wxGenericListCtrl::ClearAll()
4792 m_mainWin
->DeleteEverything();
4795 bool wxGenericListCtrl::DeleteColumn( int col
)
4797 m_mainWin
->DeleteColumn( col
);
4799 // if we don't have the header any longer, we need to relayout the window
4800 // if ( !GetColumnCount() )
4805 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4806 wxClassInfo
* textControlClass
)
4808 return m_mainWin
->EditLabel( item
, textControlClass
);
4811 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4813 return m_mainWin
->GetEditControl();
4816 bool wxGenericListCtrl::EnsureVisible( long item
)
4818 m_mainWin
->EnsureVisible( item
);
4822 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4824 return m_mainWin
->FindItem( start
, str
, partial
);
4827 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4829 return m_mainWin
->FindItem( start
, data
);
4832 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4833 int WXUNUSED(direction
))
4835 return m_mainWin
->FindItem( pt
);
4838 // TODO: sub item hit testing
4839 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4841 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4844 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4846 m_mainWin
->InsertItem( info
);
4847 return info
.m_itemId
;
4850 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4853 info
.m_text
= label
;
4854 info
.m_mask
= wxLIST_MASK_TEXT
;
4855 info
.m_itemId
= index
;
4856 return InsertItem( info
);
4859 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4862 info
.m_mask
= wxLIST_MASK_IMAGE
;
4863 info
.m_image
= imageIndex
;
4864 info
.m_itemId
= index
;
4865 return InsertItem( info
);
4868 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4871 info
.m_text
= label
;
4872 info
.m_image
= imageIndex
;
4873 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4874 info
.m_itemId
= index
;
4875 return InsertItem( info
);
4878 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4880 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4882 m_mainWin
->InsertColumn( col
, item
);
4884 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4885 // still have m_headerWin==NULL
4887 m_headerWin
->Refresh();
4892 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4893 int format
, int width
)
4896 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4897 item
.m_text
= heading
;
4900 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4901 item
.m_width
= width
;
4904 item
.m_format
= format
;
4906 return InsertColumn( col
, item
);
4909 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4911 return m_mainWin
->ScrollList(dx
, dy
);
4915 // fn is a function which takes 3 long arguments: item1, item2, data.
4916 // item1 is the long data associated with a first item (NOT the index).
4917 // item2 is the long data associated with a second item (NOT the index).
4918 // data is the same value as passed to SortItems.
4919 // The return value is a negative number if the first item should precede the second
4920 // item, a positive number of the second item should precede the first,
4921 // or zero if the two items are equivalent.
4922 // data is arbitrary data to be passed to the sort function.
4924 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4926 m_mainWin
->SortItems( fn
, data
);
4930 // ----------------------------------------------------------------------------
4932 // ----------------------------------------------------------------------------
4934 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4936 if (!m_mainWin
) return;
4938 // We need to override OnSize so that our scrolled
4939 // window a) does call Layout() to use sizers for
4940 // positioning the controls but b) does not query
4941 // the sizer for their size and use that for setting
4942 // the scrollable area as set that ourselves by
4943 // calling SetScrollbar() further down.
4947 m_mainWin
->RecalculatePositions();
4952 void wxGenericListCtrl::OnInternalIdle()
4954 wxWindow::OnInternalIdle();
4956 if (m_mainWin
->m_dirty
)
4957 m_mainWin
->RecalculatePositions();
4960 // ----------------------------------------------------------------------------
4962 // ----------------------------------------------------------------------------
4964 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4968 m_mainWin
->SetBackgroundColour( colour
);
4969 m_mainWin
->m_dirty
= true;
4975 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4977 if ( !wxWindow::SetForegroundColour( colour
) )
4982 m_mainWin
->SetForegroundColour( colour
);
4983 m_mainWin
->m_dirty
= true;
4987 m_headerWin
->SetForegroundColour( colour
);
4992 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4994 if ( !wxWindow::SetFont( font
) )
4999 m_mainWin
->SetFont( font
);
5000 m_mainWin
->m_dirty
= true;
5005 m_headerWin
->SetFont( font
);
5006 // CalculateAndSetHeaderHeight();
5016 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5019 // Use the same color scheme as wxListBox
5020 return wxListBox::GetClassDefaultAttributes(variant
);
5022 wxUnusedVar(variant
);
5023 wxVisualAttributes attr
;
5024 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5025 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5026 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5031 // ----------------------------------------------------------------------------
5032 // methods forwarded to m_mainWin
5033 // ----------------------------------------------------------------------------
5035 #if wxUSE_DRAG_AND_DROP
5037 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5039 m_mainWin
->SetDropTarget( dropTarget
);
5042 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5044 return m_mainWin
->GetDropTarget();
5049 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5051 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5054 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5056 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5059 wxColour
wxGenericListCtrl::GetForegroundColour() const
5061 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5064 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5067 return m_mainWin
->PopupMenu( menu
, x
, y
);
5073 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5075 m_mainWin
->DoClientToScreen(x
, y
);
5078 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5080 m_mainWin
->DoScreenToClient(x
, y
);
5083 void wxGenericListCtrl::SetFocus()
5085 // The test in window.cpp fails as we are a composite
5086 // window, so it checks against "this", but not m_mainWin.
5087 if ( DoFindFocus() != this )
5088 m_mainWin
->SetFocus();
5091 wxSize
wxGenericListCtrl::DoGetBestSize() const
5093 // Something is better than nothing...
5094 // 100x80 is what the MSW version will get from the default
5095 // wxControl::DoGetBestSize
5096 return wxSize(100, 80);
5099 // ----------------------------------------------------------------------------
5100 // virtual list control support
5101 // ----------------------------------------------------------------------------
5103 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5105 // this is a pure virtual function, in fact - which is not really pure
5106 // because the controls which are not virtual don't need to implement it
5107 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5109 return wxEmptyString
;
5112 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5114 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5116 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5120 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5123 return OnGetItemImage(item
);
5129 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5131 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5132 wxT("invalid item index in OnGetItemAttr()") );
5134 // no attributes by default
5138 void wxGenericListCtrl::SetItemCount(long count
)
5140 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5142 m_mainWin
->SetItemCount(count
);
5145 void wxGenericListCtrl::RefreshItem(long item
)
5147 m_mainWin
->RefreshLine(item
);
5150 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5152 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5155 // Generic wxListCtrl is more or less a container for two other
5156 // windows which drawings are done upon. These are namely
5157 // 'm_headerWin' and 'm_mainWin'.
5158 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5159 // behaviour wxListCtrl has under wxMSW.
5161 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5165 // The easy case, no rectangle specified.
5167 m_headerWin
->Refresh(eraseBackground
);
5170 m_mainWin
->Refresh(eraseBackground
);
5174 // Refresh the header window
5177 wxRect rectHeader
= m_headerWin
->GetRect();
5178 rectHeader
.Intersect(*rect
);
5179 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5182 m_headerWin
->GetPosition(&x
, &y
);
5183 rectHeader
.Offset(-x
, -y
);
5184 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5188 // Refresh the main window
5191 wxRect rectMain
= m_mainWin
->GetRect();
5192 rectMain
.Intersect(*rect
);
5193 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5196 m_mainWin
->GetPosition(&x
, &y
);
5197 rectMain
.Offset(-x
, -y
);
5198 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5204 void wxGenericListCtrl::Update()
5208 if ( m_mainWin
->m_dirty
)
5209 m_mainWin
->RecalculatePositions();
5211 m_mainWin
->Update();
5215 m_headerWin
->Update();
5218 #endif // wxUSE_LISTCTRL