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
) )
743 flags
|= wxCONTROL_SELECTED
;
744 if (m_owner
->HasFocus()
745 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
746 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
749 flags
|= wxCONTROL_FOCUSED
;
750 wxRendererNative::Get().
751 DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
754 // just for debugging to better see where the items are
756 dc
->SetPen(*wxRED_PEN
);
757 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
758 dc
->DrawRectangle( m_gi
->m_rectAll
);
759 dc
->SetPen(*wxGREEN_PEN
);
760 dc
->DrawRectangle( m_gi
->m_rectIcon
);
763 wxListItemData
*item
= node
->GetData();
764 if (item
->HasImage())
766 // centre the image inside our rectangle, this looks nicer when items
767 // ae aligned in a row
768 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
770 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
775 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
777 wxDCClipper
clipper(*dc
, rectLabel
);
778 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
782 void wxListLineData::DrawInReportMode( wxDC
*dc
,
784 const wxRect
& rectHL
,
788 // TODO: later we should support setting different attributes for
789 // different columns - to do it, just add "col" argument to
790 // GetAttr() and move these lines into the loop below
791 wxListItemAttr
*attr
= GetAttr();
792 if ( SetAttributes(dc
, attr
, highlighted
) )
796 flags
|= wxCONTROL_SELECTED
;
797 if (m_owner
->HasFocus())
798 flags
|= wxCONTROL_FOCUSED
;
800 flags
|= wxCONTROL_CURRENT
;
801 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
804 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
805 yMid
= rect
.y
+ rect
.height
/2;
807 // This probably needs to be done
808 // on all platforms as the icons
809 // otherwise nearly touch the border
814 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
816 node
= node
->GetNext(), col
++ )
818 wxListItemData
*item
= node
->GetData();
820 int width
= m_owner
->GetColumnWidth(col
);
824 const int wText
= width
- 8;
825 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
827 if ( item
->HasImage() )
830 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
831 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
833 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
839 if ( item
->HasText() )
840 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
844 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
845 const wxString
& textOrig
,
851 // we don't support displaying multiple lines currently (and neither does
852 // wxMSW FWIW) so just merge all the lines
853 wxString
text(textOrig
);
854 text
.Replace(wxT("\n"), wxT(" "));
857 dc
->GetTextExtent(text
, &w
, &h
);
859 const wxCoord y
= yMid
- (h
+ 1)/2;
861 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
863 // determine if the string can fit inside the current width
866 // it can, draw it using the items alignment
868 m_owner
->GetColumn(col
, item
);
869 switch ( item
.GetAlign() )
871 case wxLIST_FORMAT_LEFT
:
875 case wxLIST_FORMAT_RIGHT
:
879 case wxLIST_FORMAT_CENTER
:
880 x
+= (width
- w
) / 2;
884 wxFAIL_MSG( wxT("unknown list item format") );
888 dc
->DrawText(text
, x
, y
);
890 else // otherwise, truncate and add an ellipsis if possible
892 // determine the base width
893 wxString
ellipsis(wxT("..."));
895 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
897 // continue until we have enough space or only one character left
899 size_t len
= text
.length();
900 wxString drawntext
= text
.Left(len
);
903 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
904 drawntext
.RemoveLast();
907 if (w
+ base_w
<= width
)
911 // if still not enough space, remove ellipsis characters
912 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
914 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
915 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
919 dc
->DrawText(drawntext
, x
, y
);
920 dc
->DrawText(ellipsis
, x
+ w
, y
);
924 bool wxListLineData::Highlight( bool on
)
926 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
928 if ( on
== m_highlighted
)
936 void wxListLineData::ReverseHighlight( void )
938 Highlight(!IsHighlighted());
941 //-----------------------------------------------------------------------------
942 // wxListHeaderWindow
943 //-----------------------------------------------------------------------------
945 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
946 EVT_PAINT (wxListHeaderWindow::OnPaint
)
947 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
948 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
951 void wxListHeaderWindow::Init()
953 m_currentCursor
= NULL
;
954 m_isDragging
= false;
956 m_sendSetColumnWidth
= false;
959 wxListHeaderWindow::wxListHeaderWindow()
964 m_resizeCursor
= NULL
;
967 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
969 wxListMainWindow
*owner
,
973 const wxString
&name
)
974 : wxWindow( win
, id
, pos
, size
, style
, name
)
979 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
982 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
983 SetOwnForegroundColour( attr
.colFg
);
984 SetOwnBackgroundColour( attr
.colBg
);
986 SetOwnFont( attr
.font
);
988 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
989 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
991 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
995 wxListHeaderWindow::~wxListHeaderWindow()
997 delete m_resizeCursor
;
1000 #ifdef __WXUNIVERSAL__
1001 #include "wx/univ/renderer.h"
1002 #include "wx/univ/theme.h"
1005 // shift the DC origin to match the position of the main window horz
1006 // scrollbar: this allows us to always use logical coords
1007 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1009 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1012 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1015 parent
->GetViewStart( &view_start
, NULL
);
1020 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1022 // account for the horz scrollbar offset
1024 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1026 // Maybe we just have to check for m_signX
1027 // in the DC, but I leave the #ifdef __WXGTK__
1029 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1033 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1036 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1038 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1040 wxPaintDC
dc( this );
1044 dc
.SetFont( GetFont() );
1046 // width and height of the entire header window
1048 GetClientSize( &w
, &h
);
1049 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1051 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1052 dc
.SetTextForeground(GetForegroundColour());
1054 int x
= HEADER_OFFSET_X
;
1055 int numColumns
= m_owner
->GetColumnCount();
1057 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1059 m_owner
->GetColumn( i
, item
);
1060 int wCol
= item
.m_width
;
1066 if (!m_parent
->IsEnabled())
1067 flags
|= wxCONTROL_DISABLED
;
1069 // NB: The code below is not really Mac-specific, but since we are close
1070 // to 2.8 release and I don't have time to test on other platforms, I
1071 // defined this only for wxMac. If this behavior is desired on
1072 // other platforms, please go ahead and revise or remove the #ifdef.
1074 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1075 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1076 flags
|= wxCONTROL_SELECTED
;
1080 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1082 wxRendererNative::Get().DrawHeaderButton
1086 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1090 // see if we have enough space for the column label
1092 // for this we need the width of the text
1095 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1096 wLabel
+= 2 * EXTRA_WIDTH
;
1098 // and the width of the icon, if any
1099 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1100 const int image
= item
.m_image
;
1101 wxImageList
*imageList
;
1104 imageList
= m_owner
->GetSmallImageList();
1107 imageList
->GetSize(image
, ix
, iy
);
1108 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1116 // ignore alignment if there is not enough space anyhow
1118 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1121 wxFAIL_MSG( wxT("unknown list item format") );
1124 case wxLIST_FORMAT_LEFT
:
1128 case wxLIST_FORMAT_RIGHT
:
1129 xAligned
= x
+ cw
- wLabel
;
1132 case wxLIST_FORMAT_CENTER
:
1133 xAligned
= x
+ (cw
- wLabel
) / 2;
1137 // draw the text and image clipping them so that they
1138 // don't overwrite the column boundary
1139 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
);
1141 // if we have an image, draw it on the right of the label
1148 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1149 HEADER_OFFSET_Y
+ (h
- iy
)/2,
1150 wxIMAGELIST_DRAW_TRANSPARENT
1154 dc
.DrawText( item
.GetText(),
1155 xAligned
+ EXTRA_WIDTH
, (h
- hLabel
) / 2 );
1160 // Fill in what's missing to the right of the columns, otherwise we will
1161 // leave an unpainted area when columns are removed (and it looks better)
1164 wxRendererNative::Get().DrawHeaderButton
1168 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1169 wxCONTROL_DIRTY
// mark as last column
1174 void wxListHeaderWindow::OnInternalIdle()
1176 wxWindow::OnInternalIdle();
1178 if (m_sendSetColumnWidth
)
1180 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1181 m_sendSetColumnWidth
= false;
1185 void wxListHeaderWindow::DrawCurrent()
1188 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1189 m_sendSetColumnWidth
= true;
1190 m_colToSend
= m_column
;
1191 m_widthToSend
= m_currentX
- m_minX
;
1193 int x1
= m_currentX
;
1195 m_owner
->ClientToScreen( &x1
, &y1
);
1197 int x2
= m_currentX
;
1199 m_owner
->GetClientSize( NULL
, &y2
);
1200 m_owner
->ClientToScreen( &x2
, &y2
);
1203 dc
.SetLogicalFunction( wxINVERT
);
1204 dc
.SetPen( wxPen(*wxBLACK
, 2) );
1205 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1209 dc
.DrawLine( x1
, y1
, x2
, y2
);
1211 dc
.SetLogicalFunction( wxCOPY
);
1213 dc
.SetPen( wxNullPen
);
1214 dc
.SetBrush( wxNullBrush
);
1218 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1220 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1222 // we want to work with logical coords
1224 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1225 int y
= event
.GetY();
1229 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1231 // we don't draw the line beyond our window, but we allow dragging it
1234 GetClientSize( &w
, NULL
);
1235 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1238 // erase the line if it was drawn
1239 if ( m_currentX
< w
)
1242 if (event
.ButtonUp())
1245 m_isDragging
= false;
1247 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1248 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1255 m_currentX
= m_minX
+ 7;
1257 // draw in the new location
1258 if ( m_currentX
< w
)
1262 else // not dragging
1265 bool hit_border
= false;
1267 // end of the current column
1270 // find the column where this event occurred
1272 countCol
= m_owner
->GetColumnCount();
1273 for (col
= 0; col
< countCol
; col
++)
1275 xpos
+= m_owner
->GetColumnWidth( col
);
1278 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1280 // near the column border
1287 // inside the column
1294 if ( col
== countCol
)
1297 if (event
.LeftDown() || event
.RightUp())
1299 if (hit_border
&& event
.LeftDown())
1301 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1302 event
.GetPosition()) )
1304 m_isDragging
= true;
1309 //else: column resizing was vetoed by the user code
1311 else // click on a column
1313 // record the selected state of the columns
1314 if (event
.LeftDown())
1316 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
1319 m_owner
->GetColumn(i
, colItem
);
1320 long state
= colItem
.GetState();
1322 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
1324 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
1325 m_owner
->SetColumn(i
, colItem
);
1329 SendListEvent( event
.LeftDown()
1330 ? wxEVT_COMMAND_LIST_COL_CLICK
1331 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1332 event
.GetPosition());
1335 else if (event
.Moving())
1340 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1341 m_currentCursor
= m_resizeCursor
;
1345 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1346 m_currentCursor
= wxSTANDARD_CURSOR
;
1350 SetCursor(*m_currentCursor
);
1355 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1357 m_owner
->SetFocus();
1361 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1363 wxWindow
*parent
= GetParent();
1364 wxListEvent
le( type
, parent
->GetId() );
1365 le
.SetEventObject( parent
);
1366 le
.m_pointDrag
= pos
;
1368 // the position should be relative to the parent window, not
1369 // this one for compatibility with MSW and common sense: the
1370 // user code doesn't know anything at all about this header
1371 // window, so why should it get positions relative to it?
1372 le
.m_pointDrag
.y
-= GetSize().y
;
1374 le
.m_col
= m_column
;
1375 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1378 //-----------------------------------------------------------------------------
1379 // wxListRenameTimer (internal)
1380 //-----------------------------------------------------------------------------
1382 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1387 void wxListRenameTimer::Notify()
1389 m_owner
->OnRenameTimer();
1392 //-----------------------------------------------------------------------------
1393 // wxListTextCtrlWrapper (internal)
1394 //-----------------------------------------------------------------------------
1396 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
1397 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
1398 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
1399 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
1402 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
1405 : m_startValue(owner
->GetItemText(itemEdit
)),
1406 m_itemEdited(itemEdit
)
1410 m_aboutToFinish
= false;
1412 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1414 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1416 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1417 &rectLabel
.x
, &rectLabel
.y
);
1419 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
1420 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1421 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
1424 m_text
->PushEventHandler(this);
1427 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
1429 m_aboutToFinish
= true;
1431 if ( discardChanges
)
1433 m_owner
->OnRenameCancelled(m_itemEdited
);
1439 // Notify the owner about the changes
1442 // Even if vetoed, close the control (consistent with MSW)
1447 void wxListTextCtrlWrapper::Finish( bool setfocus
)
1449 m_text
->RemoveEventHandler(this);
1450 m_owner
->ResetTextControl( m_text
);
1452 wxPendingDelete
.Append( this );
1455 m_owner
->SetFocus();
1458 bool wxListTextCtrlWrapper::AcceptChanges()
1460 const wxString value
= m_text
->GetValue();
1462 // notice that we should always call OnRenameAccept() to generate the "end
1463 // label editing" event, even if the user hasn't really changed anything
1464 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
1466 // vetoed by the user
1470 // accepted, do rename the item (unless nothing changed)
1471 if ( value
!= m_startValue
)
1472 m_owner
->SetItemText(m_itemEdited
, value
);
1477 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
1479 switch ( event
.m_keyCode
)
1494 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
1496 if (m_aboutToFinish
)
1498 // auto-grow the textctrl:
1499 wxSize parentSize
= m_owner
->GetSize();
1500 wxPoint myPos
= m_text
->GetPosition();
1501 wxSize mySize
= m_text
->GetSize();
1503 m_text
->GetTextExtent(m_text
->GetValue() + wxT("MM"), &sx
, &sy
);
1504 if (myPos
.x
+ sx
> parentSize
.x
)
1505 sx
= parentSize
.x
- myPos
.x
;
1508 m_text
->SetSize(sx
, wxDefaultCoord
);
1514 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
1516 if ( !m_aboutToFinish
)
1518 if ( !AcceptChanges() )
1519 m_owner
->OnRenameCancelled( m_itemEdited
);
1524 // We must let the native text control handle focus
1528 //-----------------------------------------------------------------------------
1530 //-----------------------------------------------------------------------------
1532 BEGIN_EVENT_TABLE(wxListMainWindow
, wxWindow
)
1533 EVT_PAINT (wxListMainWindow::OnPaint
)
1534 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1535 EVT_CHAR (wxListMainWindow::OnChar
)
1536 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1537 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
1538 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1539 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1540 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1541 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
1544 void wxListMainWindow::Init()
1549 m_lineTo
= (size_t)-1;
1555 m_small_image_list
= NULL
;
1556 m_normal_image_list
= NULL
;
1558 m_small_spacing
= 30;
1559 m_normal_spacing
= 40;
1563 m_isCreated
= false;
1565 m_lastOnSame
= false;
1566 m_renameTimer
= new wxListRenameTimer( this );
1567 m_textctrlWrapper
= NULL
;
1571 m_lineSelectSingleOnUp
=
1572 m_lineBeforeLastClicked
= (size_t)-1;
1575 wxListMainWindow::wxListMainWindow()
1580 m_highlightUnfocusedBrush
= NULL
;
1583 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1588 const wxString
&name
)
1589 : wxWindow( parent
, id
, pos
, size
, style
, name
)
1593 m_highlightBrush
= new wxBrush
1595 wxSystemSettings::GetColour
1597 wxSYS_COLOUR_HIGHLIGHT
1602 m_highlightUnfocusedBrush
= new wxBrush
1604 wxSystemSettings::GetColour
1606 wxSYS_COLOUR_BTNSHADOW
1611 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
1612 SetOwnForegroundColour( attr
.colFg
);
1613 SetOwnBackgroundColour( attr
.colBg
);
1615 SetOwnFont( attr
.font
);
1618 wxListMainWindow::~wxListMainWindow()
1621 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
1622 WX_CLEAR_ARRAY(m_aColWidths
);
1624 delete m_highlightBrush
;
1625 delete m_highlightUnfocusedBrush
;
1626 delete m_renameTimer
;
1629 void wxListMainWindow::SetReportView(bool inReportView
)
1631 const size_t count
= m_lines
.size();
1632 for ( size_t n
= 0; n
< count
; n
++ )
1634 m_lines
[n
].SetReportView(inReportView
);
1638 void wxListMainWindow::CacheLineData(size_t line
)
1640 wxGenericListCtrl
*listctrl
= GetListCtrl();
1642 wxListLineData
*ld
= GetDummyLine();
1644 size_t countCol
= GetColumnCount();
1645 for ( size_t col
= 0; col
< countCol
; col
++ )
1647 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1648 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1651 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1654 wxListLineData
*wxListMainWindow::GetDummyLine() const
1656 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1657 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1659 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1661 // we need to recreate the dummy line if the number of columns in the
1662 // control changed as it would have the incorrect number of fields
1664 if ( !m_lines
.IsEmpty() &&
1665 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1667 self
->m_lines
.Clear();
1670 if ( m_lines
.IsEmpty() )
1672 wxListLineData
*line
= new wxListLineData(self
);
1673 self
->m_lines
.Add(line
);
1675 // don't waste extra memory -- there never going to be anything
1676 // else/more in this array
1677 self
->m_lines
.Shrink();
1683 // ----------------------------------------------------------------------------
1684 // line geometry (report mode only)
1685 // ----------------------------------------------------------------------------
1687 wxCoord
wxListMainWindow::GetLineHeight() const
1689 // we cache the line height as calling GetTextExtent() is slow
1690 if ( !m_lineHeight
)
1692 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1694 wxClientDC
dc( self
);
1695 dc
.SetFont( GetFont() );
1698 dc
.GetTextExtent(wxT("H"), NULL
, &y
);
1700 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1703 m_small_image_list
->GetSize(0, iw
, ih
);
1708 self
->m_lineHeight
= y
+ LINE_SPACING
;
1711 return m_lineHeight
;
1714 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1716 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1718 return LINE_SPACING
+ line
* GetLineHeight();
1721 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1723 if ( !InReportView() )
1724 return GetLine(line
)->m_gi
->m_rectAll
;
1727 rect
.x
= HEADER_OFFSET_X
;
1728 rect
.y
= GetLineY(line
);
1729 rect
.width
= GetHeaderWidth();
1730 rect
.height
= GetLineHeight();
1735 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1737 if ( !InReportView() )
1738 return GetLine(line
)->m_gi
->m_rectLabel
;
1741 wxListLineData
*data
= GetLine(line
);
1742 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1745 wxListItemData
*item
= node
->GetData();
1746 if ( item
->HasImage() )
1749 GetImageSize( item
->GetImage(), ix
, iy
);
1750 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1755 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1756 rect
.y
= GetLineY(line
);
1757 rect
.width
= GetColumnWidth(0) - image_x
;
1758 rect
.height
= GetLineHeight();
1763 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1765 if ( !InReportView() )
1766 return GetLine(line
)->m_gi
->m_rectIcon
;
1768 wxListLineData
*ld
= GetLine(line
);
1769 wxASSERT_MSG( ld
->HasImage(), wxT("should have an image") );
1772 rect
.x
= HEADER_OFFSET_X
;
1773 rect
.y
= GetLineY(line
);
1774 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1779 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1781 return InReportView() ? GetLineRect(line
)
1782 : GetLine(line
)->m_gi
->m_rectHighlight
;
1785 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1787 wxASSERT_MSG( line
< GetItemCount(), wxT("invalid line in HitTestLine") );
1789 wxListLineData
*ld
= GetLine(line
);
1791 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1792 return wxLIST_HITTEST_ONITEMICON
;
1794 // VS: Testing for "ld->HasText() || InReportView()" instead of
1795 // "ld->HasText()" is needed to make empty lines in report view
1797 if ( ld
->HasText() || InReportView() )
1799 wxRect rect
= InReportView() ? GetLineRect(line
)
1800 : GetLineLabelRect(line
);
1802 if ( rect
.Contains(x
, y
) )
1803 return wxLIST_HITTEST_ONITEMLABEL
;
1809 // ----------------------------------------------------------------------------
1810 // highlight (selection) handling
1811 // ----------------------------------------------------------------------------
1813 bool wxListMainWindow::IsHighlighted(size_t line
) const
1817 return m_selStore
.IsSelected(line
);
1821 wxListLineData
*ld
= GetLine(line
);
1822 wxCHECK_MSG( ld
, false, wxT("invalid index in IsHighlighted") );
1824 return ld
->IsHighlighted();
1828 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1834 wxArrayInt linesChanged
;
1835 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1838 // meny items changed state, refresh everything
1839 RefreshLines(lineFrom
, lineTo
);
1841 else // only a few items changed state, refresh only them
1843 size_t count
= linesChanged
.GetCount();
1844 for ( size_t n
= 0; n
< count
; n
++ )
1846 RefreshLine(linesChanged
[n
]);
1850 else // iterate over all items in non report view
1852 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1854 if ( HighlightLine(line
, highlight
) )
1860 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1866 changed
= m_selStore
.SelectItem(line
, highlight
);
1870 wxListLineData
*ld
= GetLine(line
);
1871 wxCHECK_MSG( ld
, false, wxT("invalid index in HighlightLine") );
1873 changed
= ld
->Highlight(highlight
);
1878 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1879 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1885 void wxListMainWindow::RefreshLine( size_t line
)
1887 if ( InReportView() )
1889 size_t visibleFrom
, visibleTo
;
1890 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1892 if ( line
< visibleFrom
|| line
> visibleTo
)
1896 wxRect rect
= GetLineRect(line
);
1898 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1899 RefreshRect( rect
);
1902 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1904 // we suppose that they are ordered by caller
1905 wxASSERT_MSG( lineFrom
<= lineTo
, wxT("indices in disorder") );
1907 wxASSERT_MSG( lineTo
< GetItemCount(), wxT("invalid line range") );
1909 if ( InReportView() )
1911 size_t visibleFrom
, visibleTo
;
1912 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1914 if ( lineFrom
< visibleFrom
)
1915 lineFrom
= visibleFrom
;
1916 if ( lineTo
> visibleTo
)
1921 rect
.y
= GetLineY(lineFrom
);
1922 rect
.width
= GetClientSize().x
;
1923 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1925 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1926 RefreshRect( rect
);
1930 // TODO: this should be optimized...
1931 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1938 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1940 if ( InReportView() )
1942 size_t visibleFrom
, visibleTo
;
1943 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1945 if ( lineFrom
< visibleFrom
)
1946 lineFrom
= visibleFrom
;
1947 else if ( lineFrom
> visibleTo
)
1952 rect
.y
= GetLineY(lineFrom
);
1953 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1955 wxSize size
= GetClientSize();
1956 rect
.width
= size
.x
;
1958 // refresh till the bottom of the window
1959 rect
.height
= size
.y
- rect
.y
;
1961 RefreshRect( rect
);
1965 // TODO: how to do it more efficiently?
1970 void wxListMainWindow::RefreshSelected()
1976 if ( InReportView() )
1978 GetVisibleLinesRange(&from
, &to
);
1983 to
= GetItemCount() - 1;
1986 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
1987 RefreshLine(m_current
);
1989 for ( size_t line
= from
; line
<= to
; line
++ )
1991 // NB: the test works as expected even if m_current == -1
1992 if ( line
!= m_current
&& IsHighlighted(line
) )
1997 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1999 // Note: a wxPaintDC must be constructed even if no drawing is
2000 // done (a Windows requirement).
2001 wxPaintDC
dc( this );
2005 // nothing to draw or not the moment to draw it
2010 RecalculatePositions( false );
2012 GetListCtrl()->PrepareDC( dc
);
2015 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2017 dc
.SetFont( GetFont() );
2019 if ( InReportView() )
2021 int lineHeight
= GetLineHeight();
2023 size_t visibleFrom
, visibleTo
;
2024 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2027 int xOrig
= dc
.LogicalToDeviceX( 0 );
2028 int yOrig
= dc
.LogicalToDeviceY( 0 );
2030 // tell the caller cache to cache the data
2033 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2034 GetParent()->GetId());
2035 evCache
.SetEventObject( GetParent() );
2036 evCache
.m_oldItemIndex
= visibleFrom
;
2037 evCache
.m_itemIndex
= visibleTo
;
2038 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2041 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2043 rectLine
= GetLineRect(line
);
2046 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2047 rectLine
.width
, rectLine
.height
) )
2049 // don't redraw unaffected lines to avoid flicker
2053 GetLine(line
)->DrawInReportMode( &dc
,
2055 GetLineHighlightRect(line
),
2056 IsHighlighted(line
),
2057 line
== m_current
);
2060 if ( HasFlag(wxLC_HRULES
) )
2062 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2063 wxSize clientSize
= GetClientSize();
2065 size_t i
= visibleFrom
;
2066 if (i
== 0) i
= 1; // Don't draw the first one
2067 for ( ; i
<= visibleTo
; i
++ )
2070 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2071 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2072 clientSize
.x
- dev_x
, i
* lineHeight
);
2075 // Draw last horizontal rule
2076 if ( visibleTo
== GetItemCount() - 1 )
2079 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2080 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2081 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2085 // Draw vertical rules if required
2086 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2088 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2089 wxRect firstItemRect
, lastItemRect
;
2091 GetItemRect(visibleFrom
, firstItemRect
);
2092 GetItemRect(visibleTo
, lastItemRect
);
2093 int x
= firstItemRect
.GetX();
2095 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2097 for (int col
= 0; col
< GetColumnCount(); col
++)
2099 int colWidth
= GetColumnWidth(col
);
2101 int x_pos
= x
- dev_x
;
2102 if (col
< GetColumnCount()-1) x_pos
-= 2;
2103 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2104 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2110 size_t count
= GetItemCount();
2111 for ( size_t i
= 0; i
< count
; i
++ )
2113 GetLine(i
)->Draw( &dc
);
2117 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2118 // rectangle somehow and so leaves traces when the item is not selected any
2119 // more, see #12229.
2124 if ( IsHighlighted(m_current
) )
2125 flags
|= wxCONTROL_SELECTED
;
2127 wxRendererNative::Get().
2128 DrawFocusRect(this, dc
, GetLineHighlightRect(m_current
), flags
);
2130 #endif // !__WXMAC__
2133 void wxListMainWindow::HighlightAll( bool on
)
2135 if ( IsSingleSel() )
2137 wxASSERT_MSG( !on
, wxT("can't do this in a single selection control") );
2139 // we just have one item to turn off
2140 if ( HasCurrent() && IsHighlighted(m_current
) )
2142 HighlightLine(m_current
, false);
2143 RefreshLine(m_current
);
2146 else // multi selection
2149 HighlightLines(0, GetItemCount() - 1, on
);
2153 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2155 // Do nothing here. This prevents the default handler in wxScrolledWindow
2156 // from needlessly scrolling the window when the edit control is
2157 // dismissed. See ticket #9563.
2160 void wxListMainWindow::SendNotify( size_t line
,
2161 wxEventType command
,
2162 const wxPoint
& point
)
2164 wxListEvent
le( command
, GetParent()->GetId() );
2165 le
.SetEventObject( GetParent() );
2167 le
.m_itemIndex
= line
;
2169 // set only for events which have position
2170 if ( point
!= wxDefaultPosition
)
2171 le
.m_pointDrag
= point
;
2173 // don't try to get the line info for virtual list controls: the main
2174 // program has it anyhow and if we did it would result in accessing all
2175 // the lines, even those which are not visible now and this is precisely
2176 // what we're trying to avoid
2179 if ( line
!= (size_t)-1 )
2181 GetLine(line
)->GetItem( 0, le
.m_item
);
2183 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2185 //else: there may be no more such item
2187 GetParent()->GetEventHandler()->ProcessEvent( le
);
2190 void wxListMainWindow::ChangeCurrent(size_t current
)
2192 m_current
= current
;
2194 // as the current item changed, we shouldn't start editing it when the
2195 // "slow click" timer expires as the click happened on another item
2196 if ( m_renameTimer
->IsRunning() )
2197 m_renameTimer
->Stop();
2199 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2202 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2204 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2205 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2207 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2208 wxT("EditLabel() needs a text control") );
2210 size_t itemEdit
= (size_t)item
;
2212 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2213 le
.SetEventObject( GetParent() );
2214 le
.m_itemIndex
= item
;
2215 wxListLineData
*data
= GetLine(itemEdit
);
2216 wxCHECK_MSG( data
, NULL
, wxT("invalid index in EditLabel()") );
2217 data
->GetItem( 0, le
.m_item
);
2219 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2221 // vetoed by user code
2225 // We have to call this here because the label in question might just have
2226 // been added and no screen update taken place.
2229 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2230 // so that no pending events may change the item count (see below)
2231 // IMPORTANT: needs to be tested!
2234 // Pending events dispatched by wxSafeYield might have changed the item
2236 if ( (size_t)item
>= GetItemCount() )
2240 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2241 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2242 return m_textctrlWrapper
->GetText();
2245 void wxListMainWindow::OnRenameTimer()
2247 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2249 EditLabel( m_current
);
2252 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2254 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2255 le
.SetEventObject( GetParent() );
2256 le
.m_itemIndex
= itemEdit
;
2258 wxListLineData
*data
= GetLine(itemEdit
);
2260 wxCHECK_MSG( data
, false, wxT("invalid index in OnRenameAccept()") );
2262 data
->GetItem( 0, le
.m_item
);
2263 le
.m_item
.m_text
= value
;
2264 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2268 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2270 // let owner know that the edit was cancelled
2271 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2273 le
.SetEditCanceled(true);
2275 le
.SetEventObject( GetParent() );
2276 le
.m_itemIndex
= itemEdit
;
2278 wxListLineData
*data
= GetLine(itemEdit
);
2279 wxCHECK_RET( data
, wxT("invalid index in OnRenameCancelled()") );
2281 data
->GetItem( 0, le
.m_item
);
2282 GetEventHandler()->ProcessEvent( le
);
2285 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2288 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2289 // shutdown the edit control when the mouse is clicked elsewhere on the
2290 // listctrl because the order of events is different (or something like
2291 // that), so explicitly end the edit if it is active.
2292 if ( event
.LeftDown() && m_textctrlWrapper
)
2293 m_textctrlWrapper
->EndEdit( false );
2296 if ( event
.LeftDown() )
2299 event
.SetEventObject( GetParent() );
2300 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2303 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2305 // let the base class handle mouse wheel events.
2310 if ( !HasCurrent() || IsEmpty() )
2312 if (event
.RightDown())
2314 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2316 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2317 GetParent()->GetId(),
2318 ClientToScreen(event
.GetPosition()));
2319 evtCtx
.SetEventObject(GetParent());
2320 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2328 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2329 event
.ButtonDClick()) )
2332 int x
= event
.GetX();
2333 int y
= event
.GetY();
2334 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2336 // where did we hit it (if we did)?
2339 size_t count
= GetItemCount(),
2342 if ( InReportView() )
2344 current
= y
/ GetLineHeight();
2345 if ( current
< count
)
2346 hitResult
= HitTestLine(current
, x
, y
);
2350 // TODO: optimize it too! this is less simple than for report view but
2351 // enumerating all items is still not a way to do it!!
2352 for ( current
= 0; current
< count
; current
++ )
2354 hitResult
= HitTestLine(current
, x
, y
);
2360 if (event
.Dragging())
2362 if (m_dragCount
== 0)
2364 // we have to report the raw, physical coords as we want to be
2365 // able to call HitTest(event.m_pointDrag) from the user code to
2366 // get the item being dragged
2367 m_dragStart
= event
.GetPosition();
2372 if (m_dragCount
!= 3)
2375 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2376 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2378 wxListEvent
le( command
, GetParent()->GetId() );
2379 le
.SetEventObject( GetParent() );
2380 le
.m_itemIndex
= m_lineLastClicked
;
2381 le
.m_pointDrag
= m_dragStart
;
2382 GetParent()->GetEventHandler()->ProcessEvent( le
);
2393 // outside of any item
2394 if (event
.RightDown())
2396 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2398 wxContextMenuEvent
evtCtx(
2400 GetParent()->GetId(),
2401 ClientToScreen(event
.GetPosition()));
2402 evtCtx
.SetEventObject(GetParent());
2403 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2407 // reset the selection and bail out
2408 HighlightAll(false);
2414 bool forceClick
= false;
2415 if (event
.ButtonDClick())
2417 if ( m_renameTimer
->IsRunning() )
2418 m_renameTimer
->Stop();
2420 m_lastOnSame
= false;
2422 if ( current
== m_lineLastClicked
)
2424 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2430 // The first click was on another item, so don't interpret this as
2431 // a double click, but as a simple click instead
2438 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2440 // select single line
2441 HighlightAll( false );
2442 ReverseHighlight(m_lineSelectSingleOnUp
);
2447 if ((current
== m_current
) &&
2448 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2449 HasFlag(wxLC_EDIT_LABELS
) )
2451 if ( !InReportView() ||
2452 GetLineLabelRect(current
).Contains(x
, y
) )
2454 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2455 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2460 m_lastOnSame
= false;
2461 m_lineSelectSingleOnUp
= (size_t)-1;
2465 // This is necessary, because after a DnD operation in
2466 // from and to ourself, the up event is swallowed by the
2467 // DnD code. So on next non-up event (which means here and
2468 // now) m_lineSelectSingleOnUp should be reset.
2469 m_lineSelectSingleOnUp
= (size_t)-1;
2471 if (event
.RightDown())
2473 m_lineBeforeLastClicked
= m_lineLastClicked
;
2474 m_lineLastClicked
= current
;
2476 // If the item is already selected, do not update the selection.
2477 // Multi-selections should not be cleared if a selected item is clicked.
2478 if (!IsHighlighted(current
))
2480 HighlightAll(false);
2481 ChangeCurrent(current
);
2482 ReverseHighlight(m_current
);
2485 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2487 // Allow generation of context menu event
2490 else if (event
.MiddleDown())
2492 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2494 else if ( event
.LeftDown() || forceClick
)
2496 m_lineBeforeLastClicked
= m_lineLastClicked
;
2497 m_lineLastClicked
= current
;
2499 size_t oldCurrent
= m_current
;
2500 bool oldWasSelected
= IsHighlighted(m_current
);
2502 bool cmdModifierDown
= event
.CmdDown();
2503 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2505 if ( IsSingleSel() || !IsHighlighted(current
) )
2507 HighlightAll( false );
2509 ChangeCurrent(current
);
2511 ReverseHighlight(m_current
);
2513 else // multi sel & current is highlighted & no mod keys
2515 m_lineSelectSingleOnUp
= current
;
2516 ChangeCurrent(current
); // change focus
2519 else // multi sel & either ctrl or shift is down
2521 if (cmdModifierDown
)
2523 ChangeCurrent(current
);
2525 ReverseHighlight(m_current
);
2527 else if (event
.ShiftDown())
2529 ChangeCurrent(current
);
2531 size_t lineFrom
= oldCurrent
,
2534 if ( lineTo
< lineFrom
)
2537 lineFrom
= m_current
;
2540 HighlightLines(lineFrom
, lineTo
);
2542 else // !ctrl, !shift
2544 // test in the enclosing if should make it impossible
2545 wxFAIL_MSG( wxT("how did we get here?") );
2549 if (m_current
!= oldCurrent
)
2550 RefreshLine( oldCurrent
);
2552 // forceClick is only set if the previous click was on another item
2553 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2557 void wxListMainWindow::MoveToItem(size_t item
)
2559 if ( item
== (size_t)-1 )
2562 wxRect rect
= GetLineRect(item
);
2564 int client_w
, client_h
;
2565 GetClientSize( &client_w
, &client_h
);
2567 const int hLine
= GetLineHeight();
2569 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2570 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2572 if ( InReportView() )
2574 // the next we need the range of lines shown it might be different,
2575 // so recalculate it
2576 ResetVisibleLinesRange();
2578 if (rect
.y
< view_y
)
2579 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2580 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2581 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2584 // At least on Mac the visible lines value will get reset inside of
2585 // Scroll *before* it actually scrolls the window because of the
2586 // Update() that happens there, so it will still have the wrong value.
2587 // So let's reset it again and wait for it to be recalculated in the
2588 // next paint event. I would expect this problem to show up in wxGTK
2589 // too but couldn't duplicate it there. Perhaps the order of events
2590 // is different... --Robin
2591 ResetVisibleLinesRange();
2599 if (rect
.x
-view_x
< 5)
2600 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2601 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2602 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2604 if (rect
.y
-view_y
< 5)
2605 sy
= (rect
.y
- 5) / hLine
;
2606 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2607 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2609 GetListCtrl()->Scroll(sx
, sy
);
2613 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2615 if ( !InReportView() )
2617 // TODO: this should work in all views but is not implemented now
2622 GetVisibleLinesRange(&top
, &bottom
);
2624 if ( bottom
== (size_t)-1 )
2627 ResetVisibleLinesRange();
2629 int hLine
= GetLineHeight();
2631 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2634 // see comment in MoveToItem() for why we do this
2635 ResetVisibleLinesRange();
2641 // ----------------------------------------------------------------------------
2642 // keyboard handling
2643 // ----------------------------------------------------------------------------
2645 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2647 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2648 wxT("invalid item index in OnArrowChar()") );
2650 size_t oldCurrent
= m_current
;
2652 // in single selection we just ignore Shift as we can't select several
2654 if ( event
.ShiftDown() && !IsSingleSel() )
2656 ChangeCurrent(newCurrent
);
2658 // refresh the old focus to remove it
2659 RefreshLine( oldCurrent
);
2661 // select all the items between the old and the new one
2662 if ( oldCurrent
> newCurrent
)
2664 newCurrent
= oldCurrent
;
2665 oldCurrent
= m_current
;
2668 HighlightLines(oldCurrent
, newCurrent
);
2672 // all previously selected items are unselected unless ctrl is held
2673 // in a multiselection control
2674 if ( !event
.ControlDown() || IsSingleSel() )
2675 HighlightAll(false);
2677 ChangeCurrent(newCurrent
);
2679 // refresh the old focus to remove it
2680 RefreshLine( oldCurrent
);
2682 // in single selection mode we must always have a selected item
2683 if ( !event
.ControlDown() || IsSingleSel() )
2684 HighlightLine( m_current
, true );
2687 RefreshLine( m_current
);
2692 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2694 wxWindow
*parent
= GetParent();
2696 // propagate the key event upwards
2697 wxKeyEvent
ke(event
);
2698 ke
.SetEventObject( parent
);
2699 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2705 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2707 wxWindow
*parent
= GetParent();
2709 // propagate the key event upwards
2710 wxKeyEvent
ke(event
);
2711 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2717 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2719 wxWindow
*parent
= GetParent();
2721 // send a list_key event up
2724 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2725 le
.m_itemIndex
= m_current
;
2726 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2727 le
.m_code
= event
.GetKeyCode();
2728 le
.SetEventObject( parent
);
2729 parent
->GetEventHandler()->ProcessEvent( le
);
2732 // propagate the char event upwards
2733 wxKeyEvent
ke(event
);
2734 ke
.SetEventObject( parent
);
2735 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2738 if ( HandleAsNavigationKey(event
) )
2741 // no item -> nothing to do
2748 // don't use m_linesPerPage directly as it might not be computed yet
2749 const int pageSize
= GetCountPerPage();
2750 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
2752 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2754 if (event
.GetKeyCode() == WXK_RIGHT
)
2755 event
.m_keyCode
= WXK_LEFT
;
2756 else if (event
.GetKeyCode() == WXK_LEFT
)
2757 event
.m_keyCode
= WXK_RIGHT
;
2760 switch ( event
.GetKeyCode() )
2763 if ( m_current
> 0 )
2764 OnArrowChar( m_current
- 1, event
);
2768 if ( m_current
< (size_t)GetItemCount() - 1 )
2769 OnArrowChar( m_current
+ 1, event
);
2774 OnArrowChar( GetItemCount() - 1, event
);
2779 OnArrowChar( 0, event
);
2784 int steps
= InReportView() ? pageSize
- 1
2785 : m_current
% pageSize
;
2787 int index
= m_current
- steps
;
2791 OnArrowChar( index
, event
);
2797 int steps
= InReportView()
2799 : pageSize
- (m_current
% pageSize
) - 1;
2801 size_t index
= m_current
+ steps
;
2802 size_t count
= GetItemCount();
2803 if ( index
>= count
)
2806 OnArrowChar( index
, event
);
2811 if ( !InReportView() )
2813 int index
= m_current
- pageSize
;
2817 OnArrowChar( index
, event
);
2822 if ( !InReportView() )
2824 size_t index
= m_current
+ pageSize
;
2826 size_t count
= GetItemCount();
2827 if ( index
>= count
)
2830 OnArrowChar( index
, event
);
2835 if ( IsSingleSel() )
2837 if ( event
.ControlDown() )
2839 ReverseHighlight(m_current
);
2841 else // normal space press
2843 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2846 else // multiple selection
2848 ReverseHighlight(m_current
);
2854 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2862 // ----------------------------------------------------------------------------
2864 // ----------------------------------------------------------------------------
2866 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2870 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2871 event
.SetEventObject( GetParent() );
2872 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2876 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2877 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2878 // which are already drawn correctly resulting in horrible flicker - avoid
2888 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2892 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2893 event
.SetEventObject( GetParent() );
2894 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2902 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2904 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2906 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2908 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2910 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2912 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2914 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2916 else if ( InReportView() && (m_small_image_list
))
2918 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2922 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2924 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2926 m_normal_image_list
->GetSize( index
, width
, height
);
2928 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2930 m_small_image_list
->GetSize( index
, width
, height
);
2932 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2934 m_small_image_list
->GetSize( index
, width
, height
);
2936 else if ( InReportView() && m_small_image_list
)
2938 m_small_image_list
->GetSize( index
, width
, height
);
2947 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2949 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2950 dc
.SetFont( GetFont() );
2953 dc
.GetTextExtent( s
, &lw
, NULL
);
2955 return lw
+ AUTOSIZE_COL_MARGIN
;
2958 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2962 // calc the spacing from the icon size
2963 int width
= 0, height
= 0;
2965 if ((imageList
) && (imageList
->GetImageCount()) )
2966 imageList
->GetSize(0, width
, height
);
2968 if (which
== wxIMAGE_LIST_NORMAL
)
2970 m_normal_image_list
= imageList
;
2971 m_normal_spacing
= width
+ 8;
2974 if (which
== wxIMAGE_LIST_SMALL
)
2976 m_small_image_list
= imageList
;
2977 m_small_spacing
= width
+ 14;
2978 m_lineHeight
= 0; // ensure that the line height will be recalc'd
2982 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
2986 m_small_spacing
= spacing
;
2988 m_normal_spacing
= spacing
;
2991 int wxListMainWindow::GetItemSpacing( bool isSmall
)
2993 return isSmall
? m_small_spacing
: m_normal_spacing
;
2996 // ----------------------------------------------------------------------------
2998 // ----------------------------------------------------------------------------
3000 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3002 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3004 wxCHECK_RET( node
, wxT("invalid column index in SetColumn") );
3006 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3007 item
.m_width
= GetTextLength( item
.m_text
);
3009 wxListHeaderData
*column
= node
->GetData();
3010 column
->SetItem( item
);
3012 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3014 headerWin
->m_dirty
= true;
3018 // invalidate it as it has to be recalculated
3022 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3024 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3025 wxT("invalid column index") );
3027 wxCHECK_RET( InReportView(),
3028 wxT("SetColumnWidth() can only be called in report mode.") );
3032 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3034 headerWin
->m_dirty
= true;
3036 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3037 wxCHECK_RET( node
, wxT("no column?") );
3039 wxListHeaderData
*column
= node
->GetData();
3041 size_t count
= GetItemCount();
3043 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3045 width
= GetTextLength(column
->GetText());
3046 width
+= 2*EXTRA_WIDTH
;
3048 // check for column header's image availability
3049 const int image
= column
->GetImage();
3052 if ( m_small_image_list
)
3055 m_small_image_list
->GetSize(image
, ix
, iy
);
3056 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3060 else if ( width
== wxLIST_AUTOSIZE
)
3064 // TODO: determine the max width somehow...
3065 width
= WIDTH_COL_DEFAULT
;
3069 wxClientDC
dc(this);
3070 dc
.SetFont( GetFont() );
3072 int max
= AUTOSIZE_COL_MARGIN
;
3074 // if the cached column width isn't valid then recalculate it
3075 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3077 for (size_t i
= 0; i
< count
; i
++)
3079 wxListLineData
*line
= GetLine( i
);
3080 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3082 wxCHECK_RET( n
, wxT("no subitem?") );
3084 wxListItemData
*itemData
= n
->GetData();
3087 itemData
->GetItem(item
);
3088 int itemWidth
= GetItemWidthWithImage(&item
);
3089 if (itemWidth
> max
)
3093 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3094 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3097 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3098 width
= max
+ AUTOSIZE_COL_MARGIN
;
3102 column
->SetWidth( width
);
3104 // invalidate it as it has to be recalculated
3108 int wxListMainWindow::GetHeaderWidth() const
3110 if ( !m_headerWidth
)
3112 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3114 size_t count
= GetColumnCount();
3115 for ( size_t col
= 0; col
< count
; col
++ )
3117 self
->m_headerWidth
+= GetColumnWidth(col
);
3121 return m_headerWidth
;
3124 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3126 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3127 wxCHECK_RET( node
, wxT("invalid column index in GetColumn") );
3129 wxListHeaderData
*column
= node
->GetData();
3130 column
->GetItem( item
);
3133 int wxListMainWindow::GetColumnWidth( int col
) const
3135 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3136 wxCHECK_MSG( node
, 0, wxT("invalid column index") );
3138 wxListHeaderData
*column
= node
->GetData();
3139 return column
->GetWidth();
3142 // ----------------------------------------------------------------------------
3144 // ----------------------------------------------------------------------------
3146 void wxListMainWindow::SetItem( wxListItem
&item
)
3148 long id
= item
.m_itemId
;
3149 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3150 wxT("invalid item index in SetItem") );
3154 wxListLineData
*line
= GetLine((size_t)id
);
3155 line
->SetItem( item
.m_col
, item
);
3157 // Set item state if user wants
3158 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3159 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3163 // update the Max Width Cache if needed
3164 int width
= GetItemWidthWithImage(&item
);
3166 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3167 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3171 // update the item on screen
3173 GetItemRect(id
, rectItem
);
3174 RefreshRect(rectItem
);
3177 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3182 // first deal with selection
3183 if ( stateMask
& wxLIST_STATE_SELECTED
)
3185 // set/clear select state
3188 // optimized version for virtual listctrl.
3189 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3192 else if ( state
& wxLIST_STATE_SELECTED
)
3194 const long count
= GetItemCount();
3195 for( long i
= 0; i
< count
; i
++ )
3197 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3203 // clear for non virtual (somewhat optimized by using GetNextItem())
3205 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3207 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3212 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3214 // unfocus all: only one item can be focussed, so clearing focus for
3215 // all items is simply clearing focus of the focussed item.
3216 SetItemState(m_current
, state
, stateMask
);
3218 //(setting focus to all items makes no sense, so it is not handled here.)
3221 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3225 SetItemStateAll(state
, stateMask
);
3229 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3230 wxT("invalid list ctrl item index in SetItem") );
3232 size_t oldCurrent
= m_current
;
3233 size_t item
= (size_t)litem
; // safe because of the check above
3235 // do we need to change the focus?
3236 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3238 if ( state
& wxLIST_STATE_FOCUSED
)
3240 // don't do anything if this item is already focused
3241 if ( item
!= m_current
)
3243 ChangeCurrent(item
);
3245 if ( oldCurrent
!= (size_t)-1 )
3247 if ( IsSingleSel() )
3249 HighlightLine(oldCurrent
, false);
3252 RefreshLine(oldCurrent
);
3255 RefreshLine( m_current
);
3260 // don't do anything if this item is not focused
3261 if ( item
== m_current
)
3265 if ( IsSingleSel() )
3267 // we must unselect the old current item as well or we
3268 // might end up with more than one selected item in a
3269 // single selection control
3270 HighlightLine(oldCurrent
, false);
3273 RefreshLine( oldCurrent
);
3278 // do we need to change the selection state?
3279 if ( stateMask
& wxLIST_STATE_SELECTED
)
3281 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3283 if ( IsSingleSel() )
3287 // selecting the item also makes it the focused one in the
3289 if ( m_current
!= item
)
3291 ChangeCurrent(item
);
3293 if ( oldCurrent
!= (size_t)-1 )
3295 HighlightLine( oldCurrent
, false );
3296 RefreshLine( oldCurrent
);
3302 // only the current item may be selected anyhow
3303 if ( item
!= m_current
)
3308 if ( HighlightLine(item
, on
) )
3315 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3317 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3318 wxT("invalid list ctrl item index in GetItemState()") );
3320 int ret
= wxLIST_STATE_DONTCARE
;
3322 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3324 if ( (size_t)item
== m_current
)
3325 ret
|= wxLIST_STATE_FOCUSED
;
3328 if ( stateMask
& wxLIST_STATE_SELECTED
)
3330 if ( IsHighlighted(item
) )
3331 ret
|= wxLIST_STATE_SELECTED
;
3337 void wxListMainWindow::GetItem( wxListItem
&item
) const
3339 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3340 wxT("invalid item index in GetItem") );
3342 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3343 line
->GetItem( item
.m_col
, item
);
3345 // Get item state if user wants it
3346 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3347 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3348 wxLIST_STATE_FOCUSED
);
3351 // ----------------------------------------------------------------------------
3353 // ----------------------------------------------------------------------------
3355 size_t wxListMainWindow::GetItemCount() const
3357 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3360 void wxListMainWindow::SetItemCount(long count
)
3362 m_selStore
.SetItemCount(count
);
3363 m_countVirt
= count
;
3365 ResetVisibleLinesRange();
3367 // scrollbars must be reset
3371 int wxListMainWindow::GetSelectedItemCount() const
3373 // deal with the quick case first
3374 if ( IsSingleSel() )
3375 return HasCurrent() ? IsHighlighted(m_current
) : false;
3377 // virtual controls remmebers all its selections itself
3379 return m_selStore
.GetSelectedCount();
3381 // TODO: we probably should maintain the number of items selected even for
3382 // non virtual controls as enumerating all lines is really slow...
3383 size_t countSel
= 0;
3384 size_t count
= GetItemCount();
3385 for ( size_t line
= 0; line
< count
; line
++ )
3387 if ( GetLine(line
)->IsHighlighted() )
3394 // ----------------------------------------------------------------------------
3395 // item position/size
3396 // ----------------------------------------------------------------------------
3398 wxRect
wxListMainWindow::GetViewRect() const
3400 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3402 // we need to find the longest/tallest label
3403 wxCoord xMax
= 0, yMax
= 0;
3404 const int count
= GetItemCount();
3407 for ( int i
= 0; i
< count
; i
++ )
3409 // we need logical, not physical, coordinates here, so use
3410 // GetLineRect() instead of GetItemRect()
3411 wxRect r
= GetLineRect(i
);
3413 wxCoord x
= r
.GetRight(),
3423 // some fudge needed to make it look prettier
3424 xMax
+= 2 * EXTRA_BORDER_X
;
3425 yMax
+= 2 * EXTRA_BORDER_Y
;
3427 // account for the scrollbars if necessary
3428 const wxSize sizeAll
= GetClientSize();
3429 if ( xMax
> sizeAll
.x
)
3430 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3431 if ( yMax
> sizeAll
.y
)
3432 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3434 return wxRect(0, 0, xMax
, yMax
);
3438 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3440 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3442 wxT("GetSubItemRect only meaningful in report view") );
3443 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3444 wxT("invalid item in GetSubItemRect") );
3446 // ensure that we're laid out, otherwise we could return nonsense
3449 wxConstCast(this, wxListMainWindow
)->
3450 RecalculatePositions(true /* no refresh */);
3453 rect
= GetLineRect((size_t)item
);
3455 // Adjust rect to specified column
3456 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3458 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3459 wxT("invalid subItem in GetSubItemRect") );
3461 for (int i
= 0; i
< subItem
; i
++)
3463 rect
.x
+= GetColumnWidth(i
);
3465 rect
.width
= GetColumnWidth(subItem
);
3468 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3473 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3476 GetItemRect(item
, rect
);
3484 // ----------------------------------------------------------------------------
3485 // geometry calculation
3486 // ----------------------------------------------------------------------------
3488 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3490 const int lineHeight
= GetLineHeight();
3492 wxClientDC
dc( this );
3493 dc
.SetFont( GetFont() );
3495 const size_t count
= GetItemCount();
3498 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3499 iconSpacing
= m_normal_spacing
;
3500 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3501 iconSpacing
= m_small_spacing
;
3505 // Note that we do not call GetClientSize() here but
3506 // GetSize() and subtract the border size for sunken
3507 // borders manually. This is technically incorrect,
3508 // but we need to know the client area's size WITHOUT
3509 // scrollbars here. Since we don't know if there are
3510 // any scrollbars, we use GetSize() instead. Another
3511 // solution would be to call SetScrollbars() here to
3512 // remove the scrollbars and call GetClientSize() then,
3513 // but this might result in flicker and - worse - will
3514 // reset the scrollbars to 0 which is not good at all
3515 // if you resize a dialog/window, but don't want to
3516 // reset the window scrolling. RR.
3517 // Furthermore, we actually do NOT subtract the border
3518 // width as 2 pixels is just the extra space which we
3519 // need around the actual content in the window. Other-
3520 // wise the text would e.g. touch the upper border. RR.
3523 GetSize( &clientWidth
, &clientHeight
);
3525 if ( InReportView() )
3527 // all lines have the same height and we scroll one line per step
3528 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3530 m_linesPerPage
= clientHeight
/ lineHeight
;
3532 ResetVisibleLinesRange();
3534 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3535 GetHeaderWidth() / SCROLL_UNIT_X
,
3536 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3537 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3538 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3543 // we have 3 different layout strategies: either layout all items
3544 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3545 // to arrange them in top to bottom, left to right (don't ask me why
3546 // not the other way round...) order
3547 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3549 int x
= EXTRA_BORDER_X
;
3550 int y
= EXTRA_BORDER_Y
;
3552 wxCoord widthMax
= 0;
3555 for ( i
= 0; i
< count
; i
++ )
3557 wxListLineData
*line
= GetLine(i
);
3558 line
->CalculateSize( &dc
, iconSpacing
);
3559 line
->SetPosition( x
, y
, iconSpacing
);
3561 wxSize sizeLine
= GetLineSize(i
);
3563 if ( HasFlag(wxLC_ALIGN_TOP
) )
3565 if ( sizeLine
.x
> widthMax
)
3566 widthMax
= sizeLine
.x
;
3570 else // wxLC_ALIGN_LEFT
3572 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3576 if ( HasFlag(wxLC_ALIGN_TOP
) )
3578 // traverse the items again and tweak their sizes so that they are
3579 // all the same in a row
3580 for ( i
= 0; i
< count
; i
++ )
3582 wxListLineData
*line
= GetLine(i
);
3583 line
->m_gi
->ExtendWidth(widthMax
);
3587 GetListCtrl()->SetScrollbars
3591 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3592 (y
+ lineHeight
) / lineHeight
,
3593 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3594 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3598 else // "flowed" arrangement, the most complicated case
3600 // at first we try without any scrollbars, if the items don't fit into
3601 // the window, we recalculate after subtracting the space taken by the
3604 int entireWidth
= 0;
3606 for (int tries
= 0; tries
< 2; tries
++)
3608 entireWidth
= 2 * EXTRA_BORDER_X
;
3612 // Now we have decided that the items do not fit into the
3613 // client area, so we need a scrollbar
3614 entireWidth
+= SCROLL_UNIT_X
;
3617 int x
= EXTRA_BORDER_X
;
3618 int y
= EXTRA_BORDER_Y
;
3619 int maxWidthInThisRow
= 0;
3622 int currentlyVisibleLines
= 0;
3624 for (size_t i
= 0; i
< count
; i
++)
3626 currentlyVisibleLines
++;
3627 wxListLineData
*line
= GetLine( i
);
3628 line
->CalculateSize( &dc
, iconSpacing
);
3629 line
->SetPosition( x
, y
, iconSpacing
);
3631 wxSize sizeLine
= GetLineSize( i
);
3633 if ( maxWidthInThisRow
< sizeLine
.x
)
3634 maxWidthInThisRow
= sizeLine
.x
;
3637 if (currentlyVisibleLines
> m_linesPerPage
)
3638 m_linesPerPage
= currentlyVisibleLines
;
3640 if ( y
+ sizeLine
.y
>= clientHeight
)
3642 currentlyVisibleLines
= 0;
3644 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3645 x
+= maxWidthInThisRow
;
3646 entireWidth
+= maxWidthInThisRow
;
3647 maxWidthInThisRow
= 0;
3650 // We have reached the last item.
3651 if ( i
== count
- 1 )
3652 entireWidth
+= maxWidthInThisRow
;
3654 if ( (tries
== 0) &&
3655 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3657 clientHeight
-= wxSystemSettings::
3658 GetMetric(wxSYS_HSCROLL_Y
);
3663 if ( i
== count
- 1 )
3664 tries
= 1; // Everything fits, no second try required.
3668 GetListCtrl()->SetScrollbars
3672 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3674 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3683 // FIXME: why should we call it from here?
3690 void wxListMainWindow::RefreshAll()
3695 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3696 if ( headerWin
&& headerWin
->m_dirty
)
3698 headerWin
->m_dirty
= false;
3699 headerWin
->Refresh();
3703 void wxListMainWindow::UpdateCurrent()
3705 if ( !HasCurrent() && !IsEmpty() )
3709 long wxListMainWindow::GetNextItem( long item
,
3710 int WXUNUSED(geometry
),
3714 max
= GetItemCount();
3715 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3716 wxT("invalid listctrl index in GetNextItem()") );
3718 // notice that we start with the next item (or the first one if item == -1)
3719 // and this is intentional to allow writing a simple loop to iterate over
3720 // all selected items
3723 // this is not an error because the index was OK initially,
3724 // just no such item
3731 size_t count
= GetItemCount();
3732 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3734 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3737 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3744 // ----------------------------------------------------------------------------
3746 // ----------------------------------------------------------------------------
3748 void wxListMainWindow::DeleteItem( long lindex
)
3750 size_t count
= GetItemCount();
3752 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3753 wxT("invalid item index in DeleteItem") );
3755 size_t index
= (size_t)lindex
;
3757 // we don't need to adjust the index for the previous items
3758 if ( HasCurrent() && m_current
>= index
)
3760 // if the current item is being deleted, we want the next one to
3761 // become selected - unless there is no next one - so don't adjust
3762 // m_current in this case
3763 if ( m_current
!= index
|| m_current
== count
- 1 )
3767 if ( InReportView() )
3769 // mark the Column Max Width cache as dirty if the items in the line
3770 // we're deleting contain the Max Column Width
3771 wxListLineData
* const line
= GetLine(index
);
3772 wxListItemDataList::compatibility_iterator n
;
3773 wxListItemData
*itemData
;
3777 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3779 n
= line
->m_items
.Item( i
);
3780 itemData
= n
->GetData();
3781 itemData
->GetItem(item
);
3783 itemWidth
= GetItemWidthWithImage(&item
);
3785 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3786 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3789 ResetVisibleLinesRange();
3792 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3797 m_selStore
.OnItemDelete(index
);
3801 m_lines
.RemoveAt( index
);
3804 // we need to refresh the (vert) scrollbar as the number of items changed
3807 RefreshAfter(index
);
3810 void wxListMainWindow::DeleteColumn( int col
)
3812 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3814 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3817 delete node
->GetData();
3818 m_columns
.Erase( node
);
3822 // update all the items
3823 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3825 wxListLineData
* const line
= GetLine(i
);
3826 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3827 delete n
->GetData();
3828 line
->m_items
.Erase(n
);
3832 if ( InReportView() ) // we only cache max widths when in Report View
3834 delete m_aColWidths
.Item(col
);
3835 m_aColWidths
.RemoveAt(col
);
3838 // invalidate it as it has to be recalculated
3842 void wxListMainWindow::DoDeleteAllItems()
3845 // nothing to do - in particular, don't send the event
3850 // to make the deletion of all items faster, we don't send the
3851 // notifications for each item deletion in this case but only one event
3852 // for all of them: this is compatible with wxMSW and documented in
3853 // DeleteAllItems() description
3855 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3856 event
.SetEventObject( GetParent() );
3857 GetParent()->GetEventHandler()->ProcessEvent( event
);
3865 if ( InReportView() )
3867 ResetVisibleLinesRange();
3868 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3870 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3877 void wxListMainWindow::DeleteAllItems()
3881 RecalculatePositions();
3884 void wxListMainWindow::DeleteEverything()
3886 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3887 WX_CLEAR_ARRAY(m_aColWidths
);
3892 // ----------------------------------------------------------------------------
3893 // scanning for an item
3894 // ----------------------------------------------------------------------------
3896 void wxListMainWindow::EnsureVisible( long index
)
3898 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3899 wxT("invalid index in EnsureVisible") );
3901 // We have to call this here because the label in question might just have
3902 // been added and its position is not known yet
3904 RecalculatePositions(true /* no refresh */);
3906 MoveToItem((size_t)index
);
3909 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3915 wxString str_upper
= str
.Upper();
3919 size_t count
= GetItemCount();
3920 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3922 wxListLineData
*line
= GetLine(i
);
3923 wxString line_upper
= line
->GetText(0).Upper();
3926 if (line_upper
== str_upper
)
3931 if (line_upper
.find(str_upper
) == 0)
3939 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3945 size_t count
= GetItemCount();
3946 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3948 wxListLineData
*line
= GetLine(i
);
3950 line
->GetItem( 0, item
);
3951 if (item
.m_data
== data
)
3958 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3961 GetVisibleLinesRange( &topItem
, NULL
);
3964 GetItemPosition( GetItemCount() - 1, p
);
3968 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3969 if ( id
>= 0 && id
< (long)GetItemCount() )
3975 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
3977 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3979 size_t count
= GetItemCount();
3981 if ( InReportView() )
3983 size_t current
= y
/ GetLineHeight();
3984 if ( current
< count
)
3986 flags
= HitTestLine(current
, x
, y
);
3993 // TODO: optimize it too! this is less simple than for report view but
3994 // enumerating all items is still not a way to do it!!
3995 for ( size_t current
= 0; current
< count
; current
++ )
3997 flags
= HitTestLine(current
, x
, y
);
4006 // ----------------------------------------------------------------------------
4008 // ----------------------------------------------------------------------------
4010 void wxListMainWindow::InsertItem( wxListItem
&item
)
4012 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4014 int count
= GetItemCount();
4015 wxCHECK_RET( item
.m_itemId
>= 0, wxT("invalid item index") );
4017 if (item
.m_itemId
> count
)
4018 item
.m_itemId
= count
;
4020 size_t id
= item
.m_itemId
;
4024 if ( InReportView() )
4026 ResetVisibleLinesRange();
4028 const unsigned col
= item
.GetColumn();
4029 wxCHECK_RET( col
< m_aColWidths
.size(), "invalid item column" );
4031 // calculate the width of the item and adjust the max column width
4032 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(col
);
4033 int width
= GetItemWidthWithImage(&item
);
4034 item
.SetWidth(width
);
4035 if (width
> pWidthInfo
->nMaxWidth
)
4036 pWidthInfo
->nMaxWidth
= width
;
4039 wxListLineData
*line
= new wxListLineData(this);
4041 line
->SetItem( item
.m_col
, item
);
4043 m_lines
.Insert( line
, id
);
4047 // If an item is selected at or below the point of insertion, we need to
4048 // increment the member variables because the current row's index has gone
4050 if ( HasCurrent() && m_current
>= id
)
4053 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4055 RefreshLines(id
, GetItemCount() - 1);
4058 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4061 if ( InReportView() )
4063 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4064 item
.m_width
= GetTextLength( item
.m_text
);
4066 wxListHeaderData
*column
= new wxListHeaderData( item
);
4067 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4069 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4072 wxListHeaderDataList::compatibility_iterator
4073 node
= m_columns
.Item( col
);
4074 m_columns
.Insert( node
, column
);
4075 m_aColWidths
.Insert( colWidthInfo
, col
);
4079 m_columns
.Append( column
);
4080 m_aColWidths
.Add( colWidthInfo
);
4085 // update all the items
4086 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4088 wxListLineData
* const line
= GetLine(i
);
4089 wxListItemData
* const data
= new wxListItemData(this);
4091 line
->m_items
.Insert(col
, data
);
4093 line
->m_items
.Append(data
);
4097 // invalidate it as it has to be recalculated
4102 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4105 wxClientDC
dc(this);
4107 dc
.SetFont( GetFont() );
4109 if (item
->GetImage() != -1)
4112 GetImageSize( item
->GetImage(), ix
, iy
);
4116 if (!item
->GetText().empty())
4119 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4126 // ----------------------------------------------------------------------------
4128 // ----------------------------------------------------------------------------
4130 static wxListCtrlCompare list_ctrl_compare_func_2
;
4131 static wxIntPtr list_ctrl_compare_data
;
4133 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4135 wxListLineData
*line1
= *arg1
;
4136 wxListLineData
*line2
= *arg2
;
4138 line1
->GetItem( 0, item
);
4139 wxUIntPtr data1
= item
.m_data
;
4140 line2
->GetItem( 0, item
);
4141 wxUIntPtr data2
= item
.m_data
;
4142 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4145 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4147 // selections won't make sense any more after sorting the items so reset
4149 HighlightAll(false);
4152 list_ctrl_compare_func_2
= fn
;
4153 list_ctrl_compare_data
= data
;
4154 m_lines
.Sort( list_ctrl_compare_func_1
);
4158 // ----------------------------------------------------------------------------
4160 // ----------------------------------------------------------------------------
4162 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4164 // update our idea of which lines are shown when we redraw the window the
4166 ResetVisibleLinesRange();
4168 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4170 wxGenericListCtrl
* lc
= GetListCtrl();
4171 wxCHECK_RET( lc
, wxT("no listctrl window?") );
4173 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4175 lc
->m_headerWin
->Refresh();
4176 lc
->m_headerWin
->Update();
4181 int wxListMainWindow::GetCountPerPage() const
4183 if ( !m_linesPerPage
)
4185 wxConstCast(this, wxListMainWindow
)->
4186 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4189 return m_linesPerPage
;
4192 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4194 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4196 if ( m_lineFrom
== (size_t)-1 )
4198 size_t count
= GetItemCount();
4201 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4203 // this may happen if SetScrollbars() hadn't been called yet
4204 if ( m_lineFrom
>= count
)
4205 m_lineFrom
= count
- 1;
4207 // we redraw one extra line but this is needed to make the redrawing
4208 // logic work when there is a fractional number of lines on screen
4209 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4210 if ( m_lineTo
>= count
)
4211 m_lineTo
= count
- 1;
4213 else // empty control
4216 m_lineTo
= (size_t)-1;
4220 wxASSERT_MSG( IsEmpty() ||
4221 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4222 wxT("GetVisibleLinesRange() returns incorrect result") );
4230 // -------------------------------------------------------------------------------------
4231 // wxGenericListCtrl
4232 // -------------------------------------------------------------------------------------
4234 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4236 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4237 EVT_SIZE(wxGenericListCtrl::OnSize
)
4238 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4241 void wxGenericListCtrl::Init()
4243 m_imageListNormal
= NULL
;
4244 m_imageListSmall
= NULL
;
4245 m_imageListState
= NULL
;
4247 m_ownsImageListNormal
=
4248 m_ownsImageListSmall
=
4249 m_ownsImageListState
= false;
4255 wxGenericListCtrl::~wxGenericListCtrl()
4257 if (m_ownsImageListNormal
)
4258 delete m_imageListNormal
;
4259 if (m_ownsImageListSmall
)
4260 delete m_imageListSmall
;
4261 if (m_ownsImageListState
)
4262 delete m_imageListState
;
4265 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4267 bool needs_header
= HasHeader();
4268 bool has_header
= (m_headerWin
!= NULL
);
4270 if (needs_header
== has_header
)
4275 m_headerWin
= new wxListHeaderWindow
4277 this, wxID_ANY
, m_mainWin
,
4282 wxRendererNative::Get().GetHeaderButtonHeight(this)
4287 #if defined( __WXMAC__ )
4288 static wxFont
font( wxOSX_SYSTEM_FONT_SMALL
);
4289 m_headerWin
->SetFont( font
);
4292 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4296 GetSizer()->Detach( m_headerWin
);
4298 wxDELETE(m_headerWin
);
4302 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4307 const wxValidator
&validator
,
4308 const wxString
&name
)
4312 // just like in other ports, an assert will fail if the user doesn't give any type style:
4313 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4314 wxT("wxListCtrl style should have exactly one mode bit set") );
4316 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4320 style
&= ~wxBORDER_MASK
;
4321 style
|= wxBORDER_THEME
;
4324 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4326 SetTargetWindow( m_mainWin
);
4328 // We use the cursor keys for moving the selection, not scrolling, so call
4329 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4330 // keyboard events forwarded to us from wxListMainWindow.
4331 DisableKeyboardScrolling();
4333 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4334 sizer
->Add( m_mainWin
, 1, wxGROW
);
4337 CreateOrDestroyHeaderWindowAsNeeded();
4339 SetInitialSize(size
);
4344 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4346 return wxBORDER_THEME
;
4349 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4350 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4354 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4356 // we need to process arrows ourselves for scrolling
4357 if ( nMsg
== WM_GETDLGCODE
)
4359 rc
|= DLGC_WANTARROWS
;
4366 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4368 wxSize newsize
= size
;
4370 newsize
.y
-= m_headerWin
->GetSize().y
;
4375 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4377 // update our idea of which lines are shown when we redraw
4378 // the window the next time
4379 m_mainWin
->ResetVisibleLinesRange();
4381 HandleOnScroll( event
);
4383 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4385 m_headerWin
->Refresh();
4386 m_headerWin
->Update();
4390 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4392 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4393 wxT("wxLC_VIRTUAL can't be [un]set") );
4395 long flag
= GetWindowStyle();
4399 if (style
& wxLC_MASK_TYPE
)
4400 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4401 if (style
& wxLC_MASK_ALIGN
)
4402 flag
&= ~wxLC_MASK_ALIGN
;
4403 if (style
& wxLC_MASK_SORT
)
4404 flag
&= ~wxLC_MASK_SORT
;
4412 // some styles can be set without recreating everything (as happens in
4413 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4414 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4417 wxWindow::SetWindowStyleFlag(flag
);
4421 SetWindowStyleFlag( flag
);
4425 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4427 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4428 // makes sense to remove them as we'll always add scrollbars anyhow when
4430 flag
|= wxHSCROLL
| wxVSCROLL
;
4432 const bool wasInReportView
= HasFlag(wxLC_REPORT
);
4434 // update the window style first so that the header is created or destroyed
4435 // corresponding to the new style
4436 wxWindow::SetWindowStyleFlag( flag
);
4440 const bool inReportView
= (flag
& wxLC_REPORT
) != 0;
4441 if ( inReportView
!= wasInReportView
)
4443 // we need to notify the main window about this change as it must
4444 // update its data structures
4445 m_mainWin
->SetReportView(inReportView
);
4448 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4450 CreateOrDestroyHeaderWindowAsNeeded();
4452 GetSizer()->Layout();
4456 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4458 m_mainWin
->GetColumn( col
, item
);
4462 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4464 m_mainWin
->SetColumn( col
, item
);
4468 int wxGenericListCtrl::GetColumnWidth( int col
) const
4470 return m_mainWin
->GetColumnWidth( col
);
4473 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4475 m_mainWin
->SetColumnWidth( col
, width
);
4479 int wxGenericListCtrl::GetCountPerPage() const
4481 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4484 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4486 m_mainWin
->GetItem( info
);
4490 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4492 m_mainWin
->SetItem( info
);
4496 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4499 info
.m_text
= label
;
4500 info
.m_mask
= wxLIST_MASK_TEXT
;
4501 info
.m_itemId
= index
;
4505 info
.m_image
= imageId
;
4506 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4509 m_mainWin
->SetItem(info
);
4513 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4515 return m_mainWin
->GetItemState( item
, stateMask
);
4518 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4520 m_mainWin
->SetItemState( item
, state
, stateMask
);
4525 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4527 return SetItemColumnImage(item
, 0, image
);
4531 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4534 info
.m_image
= image
;
4535 info
.m_mask
= wxLIST_MASK_IMAGE
;
4536 info
.m_itemId
= item
;
4537 info
.m_col
= column
;
4538 m_mainWin
->SetItem( info
);
4542 wxString
wxGenericListCtrl::GetItemText( long item
, int col
) const
4544 return m_mainWin
->GetItemText(item
, col
);
4547 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4549 m_mainWin
->SetItemText(item
, str
);
4552 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4555 info
.m_mask
= wxLIST_MASK_DATA
;
4556 info
.m_itemId
= item
;
4557 m_mainWin
->GetItem( info
);
4561 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4564 info
.m_mask
= wxLIST_MASK_DATA
;
4565 info
.m_itemId
= item
;
4567 m_mainWin
->SetItem( info
);
4571 wxRect
wxGenericListCtrl::GetViewRect() const
4573 return m_mainWin
->GetViewRect();
4576 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4578 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4581 bool wxGenericListCtrl::GetSubItemRect(long item
,
4584 int WXUNUSED(code
)) const
4586 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4589 if ( m_mainWin
->HasHeader() )
4590 rect
.y
+= m_headerWin
->GetSize().y
+ 1;
4595 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4597 m_mainWin
->GetItemPosition( item
, pos
);
4601 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4606 int wxGenericListCtrl::GetItemCount() const
4608 return m_mainWin
->GetItemCount();
4611 int wxGenericListCtrl::GetColumnCount() const
4613 return m_mainWin
->GetColumnCount();
4616 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4618 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4621 wxSize
wxGenericListCtrl::GetItemSpacing() const
4623 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4625 return wxSize(spacing
, spacing
);
4628 #if WXWIN_COMPATIBILITY_2_6
4629 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4631 return m_mainWin
->GetItemSpacing( isSmall
);
4633 #endif // WXWIN_COMPATIBILITY_2_6
4635 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4638 info
.m_itemId
= item
;
4639 info
.SetTextColour( col
);
4640 m_mainWin
->SetItem( info
);
4643 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4646 info
.m_itemId
= item
;
4647 m_mainWin
->GetItem( info
);
4648 return info
.GetTextColour();
4651 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4654 info
.m_itemId
= item
;
4655 info
.SetBackgroundColour( col
);
4656 m_mainWin
->SetItem( info
);
4659 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4662 info
.m_itemId
= item
;
4663 m_mainWin
->GetItem( info
);
4664 return info
.GetBackgroundColour();
4667 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4670 info
.m_itemId
= item
;
4672 m_mainWin
->SetItem( info
);
4675 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4678 info
.m_itemId
= item
;
4679 m_mainWin
->GetItem( info
);
4680 return info
.GetFont();
4683 int wxGenericListCtrl::GetSelectedItemCount() const
4685 return m_mainWin
->GetSelectedItemCount();
4688 wxColour
wxGenericListCtrl::GetTextColour() const
4690 return GetForegroundColour();
4693 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4695 SetForegroundColour(col
);
4698 long wxGenericListCtrl::GetTopItem() const
4701 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4705 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4707 return m_mainWin
->GetNextItem( item
, geom
, state
);
4710 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4712 if (which
== wxIMAGE_LIST_NORMAL
)
4713 return m_imageListNormal
;
4714 else if (which
== wxIMAGE_LIST_SMALL
)
4715 return m_imageListSmall
;
4716 else if (which
== wxIMAGE_LIST_STATE
)
4717 return m_imageListState
;
4722 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4724 if ( which
== wxIMAGE_LIST_NORMAL
)
4726 if (m_ownsImageListNormal
)
4727 delete m_imageListNormal
;
4728 m_imageListNormal
= imageList
;
4729 m_ownsImageListNormal
= false;
4731 else if ( which
== wxIMAGE_LIST_SMALL
)
4733 if (m_ownsImageListSmall
)
4734 delete m_imageListSmall
;
4735 m_imageListSmall
= imageList
;
4736 m_ownsImageListSmall
= false;
4738 else if ( which
== wxIMAGE_LIST_STATE
)
4740 if (m_ownsImageListState
)
4741 delete m_imageListState
;
4742 m_imageListState
= imageList
;
4743 m_ownsImageListState
= false;
4746 m_mainWin
->SetImageList( imageList
, which
);
4749 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4751 SetImageList(imageList
, which
);
4752 if ( which
== wxIMAGE_LIST_NORMAL
)
4753 m_ownsImageListNormal
= true;
4754 else if ( which
== wxIMAGE_LIST_SMALL
)
4755 m_ownsImageListSmall
= true;
4756 else if ( which
== wxIMAGE_LIST_STATE
)
4757 m_ownsImageListState
= true;
4760 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4765 bool wxGenericListCtrl::DeleteItem( long item
)
4767 m_mainWin
->DeleteItem( item
);
4771 bool wxGenericListCtrl::DeleteAllItems()
4773 m_mainWin
->DeleteAllItems();
4777 bool wxGenericListCtrl::DeleteAllColumns()
4779 size_t count
= m_mainWin
->m_columns
.GetCount();
4780 for ( size_t n
= 0; n
< count
; n
++ )
4785 void wxGenericListCtrl::ClearAll()
4787 m_mainWin
->DeleteEverything();
4790 bool wxGenericListCtrl::DeleteColumn( int col
)
4792 m_mainWin
->DeleteColumn( col
);
4794 // if we don't have the header any longer, we need to relayout the window
4795 // if ( !GetColumnCount() )
4800 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4801 wxClassInfo
* textControlClass
)
4803 return m_mainWin
->EditLabel( item
, textControlClass
);
4806 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4808 return m_mainWin
->GetEditControl();
4811 bool wxGenericListCtrl::EnsureVisible( long item
)
4813 m_mainWin
->EnsureVisible( item
);
4817 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4819 return m_mainWin
->FindItem( start
, str
, partial
);
4822 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4824 return m_mainWin
->FindItem( start
, data
);
4827 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4828 int WXUNUSED(direction
))
4830 return m_mainWin
->FindItem( pt
);
4833 // TODO: sub item hit testing
4834 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4836 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4839 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4841 m_mainWin
->InsertItem( info
);
4842 return info
.m_itemId
;
4845 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4848 info
.m_text
= label
;
4849 info
.m_mask
= wxLIST_MASK_TEXT
;
4850 info
.m_itemId
= index
;
4851 return InsertItem( info
);
4854 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4857 info
.m_mask
= wxLIST_MASK_IMAGE
;
4858 info
.m_image
= imageIndex
;
4859 info
.m_itemId
= index
;
4860 return InsertItem( info
);
4863 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4866 info
.m_text
= label
;
4867 info
.m_image
= imageIndex
;
4868 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4869 info
.m_itemId
= index
;
4870 return InsertItem( info
);
4873 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4875 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4877 m_mainWin
->InsertColumn( col
, item
);
4879 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4880 // still have m_headerWin==NULL
4882 m_headerWin
->Refresh();
4887 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4888 int format
, int width
)
4891 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4892 item
.m_text
= heading
;
4895 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4896 item
.m_width
= width
;
4899 item
.m_format
= format
;
4901 return InsertColumn( col
, item
);
4904 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4906 return m_mainWin
->ScrollList(dx
, dy
);
4910 // fn is a function which takes 3 long arguments: item1, item2, data.
4911 // item1 is the long data associated with a first item (NOT the index).
4912 // item2 is the long data associated with a second item (NOT the index).
4913 // data is the same value as passed to SortItems.
4914 // The return value is a negative number if the first item should precede the second
4915 // item, a positive number of the second item should precede the first,
4916 // or zero if the two items are equivalent.
4917 // data is arbitrary data to be passed to the sort function.
4919 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4921 m_mainWin
->SortItems( fn
, data
);
4925 // ----------------------------------------------------------------------------
4927 // ----------------------------------------------------------------------------
4929 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4931 if (!m_mainWin
) return;
4933 // We need to override OnSize so that our scrolled
4934 // window a) does call Layout() to use sizers for
4935 // positioning the controls but b) does not query
4936 // the sizer for their size and use that for setting
4937 // the scrollable area as set that ourselves by
4938 // calling SetScrollbar() further down.
4942 m_mainWin
->RecalculatePositions();
4947 void wxGenericListCtrl::OnInternalIdle()
4949 wxWindow::OnInternalIdle();
4951 if (m_mainWin
->m_dirty
)
4952 m_mainWin
->RecalculatePositions();
4955 // ----------------------------------------------------------------------------
4957 // ----------------------------------------------------------------------------
4959 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4963 m_mainWin
->SetBackgroundColour( colour
);
4964 m_mainWin
->m_dirty
= true;
4970 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4972 if ( !wxWindow::SetForegroundColour( colour
) )
4977 m_mainWin
->SetForegroundColour( colour
);
4978 m_mainWin
->m_dirty
= true;
4982 m_headerWin
->SetForegroundColour( colour
);
4987 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4989 if ( !wxWindow::SetFont( font
) )
4994 m_mainWin
->SetFont( font
);
4995 m_mainWin
->m_dirty
= true;
5000 m_headerWin
->SetFont( font
);
5001 // CalculateAndSetHeaderHeight();
5011 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5014 // Use the same color scheme as wxListBox
5015 return wxListBox::GetClassDefaultAttributes(variant
);
5017 wxUnusedVar(variant
);
5018 wxVisualAttributes attr
;
5019 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5020 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5021 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5026 // ----------------------------------------------------------------------------
5027 // methods forwarded to m_mainWin
5028 // ----------------------------------------------------------------------------
5030 #if wxUSE_DRAG_AND_DROP
5032 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5034 m_mainWin
->SetDropTarget( dropTarget
);
5037 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5039 return m_mainWin
->GetDropTarget();
5044 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5046 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5049 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5051 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5054 wxColour
wxGenericListCtrl::GetForegroundColour() const
5056 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5059 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5062 return m_mainWin
->PopupMenu( menu
, x
, y
);
5068 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5070 m_mainWin
->DoClientToScreen(x
, y
);
5073 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5075 m_mainWin
->DoScreenToClient(x
, y
);
5078 void wxGenericListCtrl::SetFocus()
5080 // The test in window.cpp fails as we are a composite
5081 // window, so it checks against "this", but not m_mainWin.
5082 if ( DoFindFocus() != this )
5083 m_mainWin
->SetFocus();
5086 wxSize
wxGenericListCtrl::DoGetBestClientSize() const
5088 // Something is better than nothing even if this is completely arbitrary.
5089 wxSize
sizeBest(100, 80);
5091 if ( !InReportView() )
5093 // Ensure that our minimal width is at least big enough to show all our
5094 // items. This is important for wxListbook to size itself correctly.
5096 // Remember the offset of the first item: this corresponds to the
5097 // margins around the item so we will add it to the minimal size below
5098 // to ensure that we have equal margins on all sides.
5101 // We can iterate over all items as there shouldn't be too many of them
5102 // in non-report view. If it ever becomes a problem, we could examine
5103 // just the first few items probably, the determination of the best
5104 // size is less important if we will need scrollbars anyhow.
5105 for ( int n
= 0; n
< GetItemCount(); n
++ )
5107 const wxRect itemRect
= m_mainWin
->GetLineRect(n
);
5110 // Remember the position of the first item as all the rest are
5111 // offset by at least this number of pixels too.
5112 ofs
= itemRect
.GetPosition();
5115 sizeBest
.IncTo(itemRect
.GetSize());
5118 sizeBest
.IncBy(2*ofs
);
5121 // If we have the scrollbars we need to account for them too. And to
5122 // make sure the scrollbars status is up to date we need to call this
5123 // function to set them.
5124 m_mainWin
->RecalculatePositions(true /* no refresh */);
5126 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5127 // to use m_mainWin client/virtual size for determination of whether we
5128 // use scrollbars and not the size of this window itself. Maybe that
5129 // function should be extended to work correctly in the case when our
5130 // scrollbars manage a different window from this one but currently it
5132 const wxSize sizeClient
= m_mainWin
->GetClientSize();
5133 const wxSize sizeVirt
= m_mainWin
->GetVirtualSize();
5135 if ( sizeVirt
.x
> sizeClient
.x
/* HasScrollbar(wxHORIZONTAL) */ )
5136 sizeBest
.y
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
5138 if ( sizeVirt
.y
> sizeClient
.y
/* HasScrollbar(wxVERTICAL) */ )
5139 sizeBest
.x
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
5145 // ----------------------------------------------------------------------------
5146 // virtual list control support
5147 // ----------------------------------------------------------------------------
5149 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5151 // this is a pure virtual function, in fact - which is not really pure
5152 // because the controls which are not virtual don't need to implement it
5153 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5155 return wxEmptyString
;
5158 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5160 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5162 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5166 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5169 return OnGetItemImage(item
);
5175 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5177 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5178 wxT("invalid item index in OnGetItemAttr()") );
5180 // no attributes by default
5184 void wxGenericListCtrl::SetItemCount(long count
)
5186 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5188 m_mainWin
->SetItemCount(count
);
5191 void wxGenericListCtrl::RefreshItem(long item
)
5193 m_mainWin
->RefreshLine(item
);
5196 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5198 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5201 // Generic wxListCtrl is more or less a container for two other
5202 // windows which drawings are done upon. These are namely
5203 // 'm_headerWin' and 'm_mainWin'.
5204 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5205 // behaviour wxListCtrl has under wxMSW.
5207 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5211 // The easy case, no rectangle specified.
5213 m_headerWin
->Refresh(eraseBackground
);
5216 m_mainWin
->Refresh(eraseBackground
);
5220 // Refresh the header window
5223 wxRect rectHeader
= m_headerWin
->GetRect();
5224 rectHeader
.Intersect(*rect
);
5225 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5228 m_headerWin
->GetPosition(&x
, &y
);
5229 rectHeader
.Offset(-x
, -y
);
5230 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5234 // Refresh the main window
5237 wxRect rectMain
= m_mainWin
->GetRect();
5238 rectMain
.Intersect(*rect
);
5239 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5242 m_mainWin
->GetPosition(&x
, &y
);
5243 rectMain
.Offset(-x
, -y
);
5244 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5250 void wxGenericListCtrl::Update()
5254 if ( m_mainWin
->m_dirty
)
5255 m_mainWin
->RecalculatePositions();
5257 m_mainWin
->Update();
5261 m_headerWin
->Update();
5264 #endif // wxUSE_LISTCTRL