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
);
2120 if ( IsHighlighted(m_current
) )
2121 flags
|= wxCONTROL_SELECTED
;
2123 wxRendererNative::Get().
2124 DrawFocusRect(this, dc
, GetLineHighlightRect(m_current
), flags
);
2128 void wxListMainWindow::HighlightAll( bool on
)
2130 if ( IsSingleSel() )
2132 wxASSERT_MSG( !on
, wxT("can't do this in a single selection control") );
2134 // we just have one item to turn off
2135 if ( HasCurrent() && IsHighlighted(m_current
) )
2137 HighlightLine(m_current
, false);
2138 RefreshLine(m_current
);
2141 else // multi selection
2144 HighlightLines(0, GetItemCount() - 1, on
);
2148 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2150 // Do nothing here. This prevents the default handler in wxScrolledWindow
2151 // from needlessly scrolling the window when the edit control is
2152 // dismissed. See ticket #9563.
2155 void wxListMainWindow::SendNotify( size_t line
,
2156 wxEventType command
,
2157 const wxPoint
& point
)
2159 wxListEvent
le( command
, GetParent()->GetId() );
2160 le
.SetEventObject( GetParent() );
2162 le
.m_itemIndex
= line
;
2164 // set only for events which have position
2165 if ( point
!= wxDefaultPosition
)
2166 le
.m_pointDrag
= point
;
2168 // don't try to get the line info for virtual list controls: the main
2169 // program has it anyhow and if we did it would result in accessing all
2170 // the lines, even those which are not visible now and this is precisely
2171 // what we're trying to avoid
2174 if ( line
!= (size_t)-1 )
2176 GetLine(line
)->GetItem( 0, le
.m_item
);
2178 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2180 //else: there may be no more such item
2182 GetParent()->GetEventHandler()->ProcessEvent( le
);
2185 void wxListMainWindow::ChangeCurrent(size_t current
)
2187 m_current
= current
;
2189 // as the current item changed, we shouldn't start editing it when the
2190 // "slow click" timer expires as the click happened on another item
2191 if ( m_renameTimer
->IsRunning() )
2192 m_renameTimer
->Stop();
2194 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2197 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2199 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2200 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2202 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2203 wxT("EditLabel() needs a text control") );
2205 size_t itemEdit
= (size_t)item
;
2207 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2208 le
.SetEventObject( GetParent() );
2209 le
.m_itemIndex
= item
;
2210 wxListLineData
*data
= GetLine(itemEdit
);
2211 wxCHECK_MSG( data
, NULL
, wxT("invalid index in EditLabel()") );
2212 data
->GetItem( 0, le
.m_item
);
2214 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2216 // vetoed by user code
2220 // We have to call this here because the label in question might just have
2221 // been added and no screen update taken place.
2224 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2225 // so that no pending events may change the item count (see below)
2226 // IMPORTANT: needs to be tested!
2229 // Pending events dispatched by wxSafeYield might have changed the item
2231 if ( (size_t)item
>= GetItemCount() )
2235 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2236 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2237 return m_textctrlWrapper
->GetText();
2240 void wxListMainWindow::OnRenameTimer()
2242 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2244 EditLabel( m_current
);
2247 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2249 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2250 le
.SetEventObject( GetParent() );
2251 le
.m_itemIndex
= itemEdit
;
2253 wxListLineData
*data
= GetLine(itemEdit
);
2255 wxCHECK_MSG( data
, false, wxT("invalid index in OnRenameAccept()") );
2257 data
->GetItem( 0, le
.m_item
);
2258 le
.m_item
.m_text
= value
;
2259 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2263 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2265 // let owner know that the edit was cancelled
2266 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2268 le
.SetEditCanceled(true);
2270 le
.SetEventObject( GetParent() );
2271 le
.m_itemIndex
= itemEdit
;
2273 wxListLineData
*data
= GetLine(itemEdit
);
2274 wxCHECK_RET( data
, wxT("invalid index in OnRenameCancelled()") );
2276 data
->GetItem( 0, le
.m_item
);
2277 GetEventHandler()->ProcessEvent( le
);
2280 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2283 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2284 // shutdown the edit control when the mouse is clicked elsewhere on the
2285 // listctrl because the order of events is different (or something like
2286 // that), so explicitly end the edit if it is active.
2287 if ( event
.LeftDown() && m_textctrlWrapper
)
2288 m_textctrlWrapper
->EndEdit( false );
2291 if ( event
.LeftDown() )
2294 event
.SetEventObject( GetParent() );
2295 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2298 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2300 // let the base class handle mouse wheel events.
2305 if ( !HasCurrent() || IsEmpty() )
2307 if (event
.RightDown())
2309 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2311 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2312 GetParent()->GetId(),
2313 ClientToScreen(event
.GetPosition()));
2314 evtCtx
.SetEventObject(GetParent());
2315 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2323 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2324 event
.ButtonDClick()) )
2327 int x
= event
.GetX();
2328 int y
= event
.GetY();
2329 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2331 // where did we hit it (if we did)?
2334 size_t count
= GetItemCount(),
2337 if ( InReportView() )
2339 current
= y
/ GetLineHeight();
2340 if ( current
< count
)
2341 hitResult
= HitTestLine(current
, x
, y
);
2345 // TODO: optimize it too! this is less simple than for report view but
2346 // enumerating all items is still not a way to do it!!
2347 for ( current
= 0; current
< count
; current
++ )
2349 hitResult
= HitTestLine(current
, x
, y
);
2355 if (event
.Dragging())
2357 if (m_dragCount
== 0)
2359 // we have to report the raw, physical coords as we want to be
2360 // able to call HitTest(event.m_pointDrag) from the user code to
2361 // get the item being dragged
2362 m_dragStart
= event
.GetPosition();
2367 if (m_dragCount
!= 3)
2370 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2371 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2373 wxListEvent
le( command
, GetParent()->GetId() );
2374 le
.SetEventObject( GetParent() );
2375 le
.m_itemIndex
= m_lineLastClicked
;
2376 le
.m_pointDrag
= m_dragStart
;
2377 GetParent()->GetEventHandler()->ProcessEvent( le
);
2388 // outside of any item
2389 if (event
.RightDown())
2391 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2393 wxContextMenuEvent
evtCtx(
2395 GetParent()->GetId(),
2396 ClientToScreen(event
.GetPosition()));
2397 evtCtx
.SetEventObject(GetParent());
2398 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2402 // reset the selection and bail out
2403 HighlightAll(false);
2409 bool forceClick
= false;
2410 if (event
.ButtonDClick())
2412 if ( m_renameTimer
->IsRunning() )
2413 m_renameTimer
->Stop();
2415 m_lastOnSame
= false;
2417 if ( current
== m_lineLastClicked
)
2419 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2425 // The first click was on another item, so don't interpret this as
2426 // a double click, but as a simple click instead
2433 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2435 // select single line
2436 HighlightAll( false );
2437 ReverseHighlight(m_lineSelectSingleOnUp
);
2442 if ((current
== m_current
) &&
2443 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2444 HasFlag(wxLC_EDIT_LABELS
) )
2446 if ( !InReportView() ||
2447 GetLineLabelRect(current
).Contains(x
, y
) )
2449 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2450 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2455 m_lastOnSame
= false;
2456 m_lineSelectSingleOnUp
= (size_t)-1;
2460 // This is necessary, because after a DnD operation in
2461 // from and to ourself, the up event is swallowed by the
2462 // DnD code. So on next non-up event (which means here and
2463 // now) m_lineSelectSingleOnUp should be reset.
2464 m_lineSelectSingleOnUp
= (size_t)-1;
2466 if (event
.RightDown())
2468 m_lineBeforeLastClicked
= m_lineLastClicked
;
2469 m_lineLastClicked
= current
;
2471 // If the item is already selected, do not update the selection.
2472 // Multi-selections should not be cleared if a selected item is clicked.
2473 if (!IsHighlighted(current
))
2475 HighlightAll(false);
2476 ChangeCurrent(current
);
2477 ReverseHighlight(m_current
);
2480 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2482 // Allow generation of context menu event
2485 else if (event
.MiddleDown())
2487 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2489 else if ( event
.LeftDown() || forceClick
)
2491 m_lineBeforeLastClicked
= m_lineLastClicked
;
2492 m_lineLastClicked
= current
;
2494 size_t oldCurrent
= m_current
;
2495 bool oldWasSelected
= IsHighlighted(m_current
);
2497 bool cmdModifierDown
= event
.CmdDown();
2498 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2500 if ( IsSingleSel() || !IsHighlighted(current
) )
2502 HighlightAll( false );
2504 ChangeCurrent(current
);
2506 ReverseHighlight(m_current
);
2508 else // multi sel & current is highlighted & no mod keys
2510 m_lineSelectSingleOnUp
= current
;
2511 ChangeCurrent(current
); // change focus
2514 else // multi sel & either ctrl or shift is down
2516 if (cmdModifierDown
)
2518 ChangeCurrent(current
);
2520 ReverseHighlight(m_current
);
2522 else if (event
.ShiftDown())
2524 ChangeCurrent(current
);
2526 size_t lineFrom
= oldCurrent
,
2529 if ( lineTo
< lineFrom
)
2532 lineFrom
= m_current
;
2535 HighlightLines(lineFrom
, lineTo
);
2537 else // !ctrl, !shift
2539 // test in the enclosing if should make it impossible
2540 wxFAIL_MSG( wxT("how did we get here?") );
2544 if (m_current
!= oldCurrent
)
2545 RefreshLine( oldCurrent
);
2547 // forceClick is only set if the previous click was on another item
2548 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2552 void wxListMainWindow::MoveToItem(size_t item
)
2554 if ( item
== (size_t)-1 )
2557 wxRect rect
= GetLineRect(item
);
2559 int client_w
, client_h
;
2560 GetClientSize( &client_w
, &client_h
);
2562 const int hLine
= GetLineHeight();
2564 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2565 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2567 if ( InReportView() )
2569 // the next we need the range of lines shown it might be different,
2570 // so recalculate it
2571 ResetVisibleLinesRange();
2573 if (rect
.y
< view_y
)
2574 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2575 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2576 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2579 // At least on Mac the visible lines value will get reset inside of
2580 // Scroll *before* it actually scrolls the window because of the
2581 // Update() that happens there, so it will still have the wrong value.
2582 // So let's reset it again and wait for it to be recalculated in the
2583 // next paint event. I would expect this problem to show up in wxGTK
2584 // too but couldn't duplicate it there. Perhaps the order of events
2585 // is different... --Robin
2586 ResetVisibleLinesRange();
2594 if (rect
.x
-view_x
< 5)
2595 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2596 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2597 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2599 if (rect
.y
-view_y
< 5)
2600 sy
= (rect
.y
- 5) / hLine
;
2601 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2602 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2604 GetListCtrl()->Scroll(sx
, sy
);
2608 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2610 if ( !InReportView() )
2612 // TODO: this should work in all views but is not implemented now
2617 GetVisibleLinesRange(&top
, &bottom
);
2619 if ( bottom
== (size_t)-1 )
2622 ResetVisibleLinesRange();
2624 int hLine
= GetLineHeight();
2626 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2629 // see comment in MoveToItem() for why we do this
2630 ResetVisibleLinesRange();
2636 // ----------------------------------------------------------------------------
2637 // keyboard handling
2638 // ----------------------------------------------------------------------------
2640 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2642 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2643 wxT("invalid item index in OnArrowChar()") );
2645 size_t oldCurrent
= m_current
;
2647 // in single selection we just ignore Shift as we can't select several
2649 if ( event
.ShiftDown() && !IsSingleSel() )
2651 ChangeCurrent(newCurrent
);
2653 // refresh the old focus to remove it
2654 RefreshLine( oldCurrent
);
2656 // select all the items between the old and the new one
2657 if ( oldCurrent
> newCurrent
)
2659 newCurrent
= oldCurrent
;
2660 oldCurrent
= m_current
;
2663 HighlightLines(oldCurrent
, newCurrent
);
2667 // all previously selected items are unselected unless ctrl is held
2668 // in a multiselection control
2669 if ( !event
.ControlDown() || IsSingleSel() )
2670 HighlightAll(false);
2672 ChangeCurrent(newCurrent
);
2674 // refresh the old focus to remove it
2675 RefreshLine( oldCurrent
);
2677 // in single selection mode we must always have a selected item
2678 if ( !event
.ControlDown() || IsSingleSel() )
2679 HighlightLine( m_current
, true );
2682 RefreshLine( m_current
);
2687 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2689 wxWindow
*parent
= GetParent();
2691 // propagate the key event upwards
2692 wxKeyEvent
ke(event
);
2693 ke
.SetEventObject( parent
);
2694 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2700 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2702 wxWindow
*parent
= GetParent();
2704 // propagate the key event upwards
2705 wxKeyEvent
ke(event
);
2706 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2712 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2714 wxWindow
*parent
= GetParent();
2716 // send a list_key event up
2719 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2720 le
.m_itemIndex
= m_current
;
2721 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2722 le
.m_code
= event
.GetKeyCode();
2723 le
.SetEventObject( parent
);
2724 parent
->GetEventHandler()->ProcessEvent( le
);
2727 // propagate the char event upwards
2728 wxKeyEvent
ke(event
);
2729 ke
.SetEventObject( parent
);
2730 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2733 if ( HandleAsNavigationKey(event
) )
2736 // no item -> nothing to do
2743 // don't use m_linesPerPage directly as it might not be computed yet
2744 const int pageSize
= GetCountPerPage();
2745 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
2747 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2749 if (event
.GetKeyCode() == WXK_RIGHT
)
2750 event
.m_keyCode
= WXK_LEFT
;
2751 else if (event
.GetKeyCode() == WXK_LEFT
)
2752 event
.m_keyCode
= WXK_RIGHT
;
2755 switch ( event
.GetKeyCode() )
2758 if ( m_current
> 0 )
2759 OnArrowChar( m_current
- 1, event
);
2763 if ( m_current
< (size_t)GetItemCount() - 1 )
2764 OnArrowChar( m_current
+ 1, event
);
2769 OnArrowChar( GetItemCount() - 1, event
);
2774 OnArrowChar( 0, event
);
2779 int steps
= InReportView() ? pageSize
- 1
2780 : m_current
% pageSize
;
2782 int index
= m_current
- steps
;
2786 OnArrowChar( index
, event
);
2792 int steps
= InReportView()
2794 : pageSize
- (m_current
% pageSize
) - 1;
2796 size_t index
= m_current
+ steps
;
2797 size_t count
= GetItemCount();
2798 if ( index
>= count
)
2801 OnArrowChar( index
, event
);
2806 if ( !InReportView() )
2808 int index
= m_current
- pageSize
;
2812 OnArrowChar( index
, event
);
2817 if ( !InReportView() )
2819 size_t index
= m_current
+ pageSize
;
2821 size_t count
= GetItemCount();
2822 if ( index
>= count
)
2825 OnArrowChar( index
, event
);
2830 if ( IsSingleSel() )
2832 if ( event
.ControlDown() )
2834 ReverseHighlight(m_current
);
2836 else // normal space press
2838 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2841 else // multiple selection
2843 ReverseHighlight(m_current
);
2849 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2857 // ----------------------------------------------------------------------------
2859 // ----------------------------------------------------------------------------
2861 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2865 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2866 event
.SetEventObject( GetParent() );
2867 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2871 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2872 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2873 // which are already drawn correctly resulting in horrible flicker - avoid
2883 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2887 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2888 event
.SetEventObject( GetParent() );
2889 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2897 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2899 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2901 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2903 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2905 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2907 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2909 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2911 else if ( InReportView() && (m_small_image_list
))
2913 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2917 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2919 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2921 m_normal_image_list
->GetSize( index
, width
, height
);
2923 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2925 m_small_image_list
->GetSize( index
, width
, height
);
2927 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2929 m_small_image_list
->GetSize( index
, width
, height
);
2931 else if ( InReportView() && m_small_image_list
)
2933 m_small_image_list
->GetSize( index
, width
, height
);
2942 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2944 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2945 dc
.SetFont( GetFont() );
2948 dc
.GetTextExtent( s
, &lw
, NULL
);
2950 return lw
+ AUTOSIZE_COL_MARGIN
;
2953 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2957 // calc the spacing from the icon size
2958 int width
= 0, height
= 0;
2960 if ((imageList
) && (imageList
->GetImageCount()) )
2961 imageList
->GetSize(0, width
, height
);
2963 if (which
== wxIMAGE_LIST_NORMAL
)
2965 m_normal_image_list
= imageList
;
2966 m_normal_spacing
= width
+ 8;
2969 if (which
== wxIMAGE_LIST_SMALL
)
2971 m_small_image_list
= imageList
;
2972 m_small_spacing
= width
+ 14;
2973 m_lineHeight
= 0; // ensure that the line height will be recalc'd
2977 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
2981 m_small_spacing
= spacing
;
2983 m_normal_spacing
= spacing
;
2986 int wxListMainWindow::GetItemSpacing( bool isSmall
)
2988 return isSmall
? m_small_spacing
: m_normal_spacing
;
2991 // ----------------------------------------------------------------------------
2993 // ----------------------------------------------------------------------------
2995 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
2997 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
2999 wxCHECK_RET( node
, wxT("invalid column index in SetColumn") );
3001 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3002 item
.m_width
= GetTextLength( item
.m_text
);
3004 wxListHeaderData
*column
= node
->GetData();
3005 column
->SetItem( item
);
3007 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3009 headerWin
->m_dirty
= true;
3013 // invalidate it as it has to be recalculated
3017 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3019 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3020 wxT("invalid column index") );
3022 wxCHECK_RET( InReportView(),
3023 wxT("SetColumnWidth() can only be called in report mode.") );
3027 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3029 headerWin
->m_dirty
= true;
3031 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3032 wxCHECK_RET( node
, wxT("no column?") );
3034 wxListHeaderData
*column
= node
->GetData();
3036 size_t count
= GetItemCount();
3038 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3040 width
= GetTextLength(column
->GetText());
3041 width
+= 2*EXTRA_WIDTH
;
3043 // check for column header's image availability
3044 const int image
= column
->GetImage();
3047 if ( m_small_image_list
)
3050 m_small_image_list
->GetSize(image
, ix
, iy
);
3051 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3055 else if ( width
== wxLIST_AUTOSIZE
)
3059 // TODO: determine the max width somehow...
3060 width
= WIDTH_COL_DEFAULT
;
3064 wxClientDC
dc(this);
3065 dc
.SetFont( GetFont() );
3067 int max
= AUTOSIZE_COL_MARGIN
;
3069 // if the cached column width isn't valid then recalculate it
3070 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3072 for (size_t i
= 0; i
< count
; i
++)
3074 wxListLineData
*line
= GetLine( i
);
3075 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3077 wxCHECK_RET( n
, wxT("no subitem?") );
3079 wxListItemData
*itemData
= n
->GetData();
3082 itemData
->GetItem(item
);
3083 int itemWidth
= GetItemWidthWithImage(&item
);
3084 if (itemWidth
> max
)
3088 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3089 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3092 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3093 width
= max
+ AUTOSIZE_COL_MARGIN
;
3097 column
->SetWidth( width
);
3099 // invalidate it as it has to be recalculated
3103 int wxListMainWindow::GetHeaderWidth() const
3105 if ( !m_headerWidth
)
3107 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3109 size_t count
= GetColumnCount();
3110 for ( size_t col
= 0; col
< count
; col
++ )
3112 self
->m_headerWidth
+= GetColumnWidth(col
);
3116 return m_headerWidth
;
3119 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3121 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3122 wxCHECK_RET( node
, wxT("invalid column index in GetColumn") );
3124 wxListHeaderData
*column
= node
->GetData();
3125 column
->GetItem( item
);
3128 int wxListMainWindow::GetColumnWidth( int col
) const
3130 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3131 wxCHECK_MSG( node
, 0, wxT("invalid column index") );
3133 wxListHeaderData
*column
= node
->GetData();
3134 return column
->GetWidth();
3137 // ----------------------------------------------------------------------------
3139 // ----------------------------------------------------------------------------
3141 void wxListMainWindow::SetItem( wxListItem
&item
)
3143 long id
= item
.m_itemId
;
3144 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3145 wxT("invalid item index in SetItem") );
3149 wxListLineData
*line
= GetLine((size_t)id
);
3150 line
->SetItem( item
.m_col
, item
);
3152 // Set item state if user wants
3153 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3154 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3158 // update the Max Width Cache if needed
3159 int width
= GetItemWidthWithImage(&item
);
3161 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3162 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3166 // update the item on screen
3168 GetItemRect(id
, rectItem
);
3169 RefreshRect(rectItem
);
3172 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3177 // first deal with selection
3178 if ( stateMask
& wxLIST_STATE_SELECTED
)
3180 // set/clear select state
3183 // optimized version for virtual listctrl.
3184 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3187 else if ( state
& wxLIST_STATE_SELECTED
)
3189 const long count
= GetItemCount();
3190 for( long i
= 0; i
< count
; i
++ )
3192 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3198 // clear for non virtual (somewhat optimized by using GetNextItem())
3200 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3202 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3207 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3209 // unfocus all: only one item can be focussed, so clearing focus for
3210 // all items is simply clearing focus of the focussed item.
3211 SetItemState(m_current
, state
, stateMask
);
3213 //(setting focus to all items makes no sense, so it is not handled here.)
3216 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3220 SetItemStateAll(state
, stateMask
);
3224 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3225 wxT("invalid list ctrl item index in SetItem") );
3227 size_t oldCurrent
= m_current
;
3228 size_t item
= (size_t)litem
; // safe because of the check above
3230 // do we need to change the focus?
3231 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3233 if ( state
& wxLIST_STATE_FOCUSED
)
3235 // don't do anything if this item is already focused
3236 if ( item
!= m_current
)
3238 ChangeCurrent(item
);
3240 if ( oldCurrent
!= (size_t)-1 )
3242 if ( IsSingleSel() )
3244 HighlightLine(oldCurrent
, false);
3247 RefreshLine(oldCurrent
);
3250 RefreshLine( m_current
);
3255 // don't do anything if this item is not focused
3256 if ( item
== m_current
)
3260 if ( IsSingleSel() )
3262 // we must unselect the old current item as well or we
3263 // might end up with more than one selected item in a
3264 // single selection control
3265 HighlightLine(oldCurrent
, false);
3268 RefreshLine( oldCurrent
);
3273 // do we need to change the selection state?
3274 if ( stateMask
& wxLIST_STATE_SELECTED
)
3276 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3278 if ( IsSingleSel() )
3282 // selecting the item also makes it the focused one in the
3284 if ( m_current
!= item
)
3286 ChangeCurrent(item
);
3288 if ( oldCurrent
!= (size_t)-1 )
3290 HighlightLine( oldCurrent
, false );
3291 RefreshLine( oldCurrent
);
3297 // only the current item may be selected anyhow
3298 if ( item
!= m_current
)
3303 if ( HighlightLine(item
, on
) )
3310 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3312 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3313 wxT("invalid list ctrl item index in GetItemState()") );
3315 int ret
= wxLIST_STATE_DONTCARE
;
3317 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3319 if ( (size_t)item
== m_current
)
3320 ret
|= wxLIST_STATE_FOCUSED
;
3323 if ( stateMask
& wxLIST_STATE_SELECTED
)
3325 if ( IsHighlighted(item
) )
3326 ret
|= wxLIST_STATE_SELECTED
;
3332 void wxListMainWindow::GetItem( wxListItem
&item
) const
3334 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3335 wxT("invalid item index in GetItem") );
3337 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3338 line
->GetItem( item
.m_col
, item
);
3340 // Get item state if user wants it
3341 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3342 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3343 wxLIST_STATE_FOCUSED
);
3346 // ----------------------------------------------------------------------------
3348 // ----------------------------------------------------------------------------
3350 size_t wxListMainWindow::GetItemCount() const
3352 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3355 void wxListMainWindow::SetItemCount(long count
)
3357 m_selStore
.SetItemCount(count
);
3358 m_countVirt
= count
;
3360 ResetVisibleLinesRange();
3362 // scrollbars must be reset
3366 int wxListMainWindow::GetSelectedItemCount() const
3368 // deal with the quick case first
3369 if ( IsSingleSel() )
3370 return HasCurrent() ? IsHighlighted(m_current
) : false;
3372 // virtual controls remmebers all its selections itself
3374 return m_selStore
.GetSelectedCount();
3376 // TODO: we probably should maintain the number of items selected even for
3377 // non virtual controls as enumerating all lines is really slow...
3378 size_t countSel
= 0;
3379 size_t count
= GetItemCount();
3380 for ( size_t line
= 0; line
< count
; line
++ )
3382 if ( GetLine(line
)->IsHighlighted() )
3389 // ----------------------------------------------------------------------------
3390 // item position/size
3391 // ----------------------------------------------------------------------------
3393 wxRect
wxListMainWindow::GetViewRect() const
3395 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3397 // we need to find the longest/tallest label
3398 wxCoord xMax
= 0, yMax
= 0;
3399 const int count
= GetItemCount();
3402 for ( int i
= 0; i
< count
; i
++ )
3404 // we need logical, not physical, coordinates here, so use
3405 // GetLineRect() instead of GetItemRect()
3406 wxRect r
= GetLineRect(i
);
3408 wxCoord x
= r
.GetRight(),
3418 // some fudge needed to make it look prettier
3419 xMax
+= 2 * EXTRA_BORDER_X
;
3420 yMax
+= 2 * EXTRA_BORDER_Y
;
3422 // account for the scrollbars if necessary
3423 const wxSize sizeAll
= GetClientSize();
3424 if ( xMax
> sizeAll
.x
)
3425 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3426 if ( yMax
> sizeAll
.y
)
3427 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3429 return wxRect(0, 0, xMax
, yMax
);
3433 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3435 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3437 wxT("GetSubItemRect only meaningful in report view") );
3438 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3439 wxT("invalid item in GetSubItemRect") );
3441 // ensure that we're laid out, otherwise we could return nonsense
3444 wxConstCast(this, wxListMainWindow
)->
3445 RecalculatePositions(true /* no refresh */);
3448 rect
= GetLineRect((size_t)item
);
3450 // Adjust rect to specified column
3451 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3453 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3454 wxT("invalid subItem in GetSubItemRect") );
3456 for (int i
= 0; i
< subItem
; i
++)
3458 rect
.x
+= GetColumnWidth(i
);
3460 rect
.width
= GetColumnWidth(subItem
);
3463 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3468 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3471 GetItemRect(item
, rect
);
3479 // ----------------------------------------------------------------------------
3480 // geometry calculation
3481 // ----------------------------------------------------------------------------
3483 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3485 const int lineHeight
= GetLineHeight();
3487 wxClientDC
dc( this );
3488 dc
.SetFont( GetFont() );
3490 const size_t count
= GetItemCount();
3493 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3494 iconSpacing
= m_normal_spacing
;
3495 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3496 iconSpacing
= m_small_spacing
;
3500 // Note that we do not call GetClientSize() here but
3501 // GetSize() and subtract the border size for sunken
3502 // borders manually. This is technically incorrect,
3503 // but we need to know the client area's size WITHOUT
3504 // scrollbars here. Since we don't know if there are
3505 // any scrollbars, we use GetSize() instead. Another
3506 // solution would be to call SetScrollbars() here to
3507 // remove the scrollbars and call GetClientSize() then,
3508 // but this might result in flicker and - worse - will
3509 // reset the scrollbars to 0 which is not good at all
3510 // if you resize a dialog/window, but don't want to
3511 // reset the window scrolling. RR.
3512 // Furthermore, we actually do NOT subtract the border
3513 // width as 2 pixels is just the extra space which we
3514 // need around the actual content in the window. Other-
3515 // wise the text would e.g. touch the upper border. RR.
3518 GetSize( &clientWidth
, &clientHeight
);
3520 if ( InReportView() )
3522 // all lines have the same height and we scroll one line per step
3523 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3525 m_linesPerPage
= clientHeight
/ lineHeight
;
3527 ResetVisibleLinesRange();
3529 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3530 GetHeaderWidth() / SCROLL_UNIT_X
,
3531 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3532 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3533 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3538 // we have 3 different layout strategies: either layout all items
3539 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3540 // to arrange them in top to bottom, left to right (don't ask me why
3541 // not the other way round...) order
3542 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3544 int x
= EXTRA_BORDER_X
;
3545 int y
= EXTRA_BORDER_Y
;
3547 wxCoord widthMax
= 0;
3550 for ( i
= 0; i
< count
; i
++ )
3552 wxListLineData
*line
= GetLine(i
);
3553 line
->CalculateSize( &dc
, iconSpacing
);
3554 line
->SetPosition( x
, y
, iconSpacing
);
3556 wxSize sizeLine
= GetLineSize(i
);
3558 if ( HasFlag(wxLC_ALIGN_TOP
) )
3560 if ( sizeLine
.x
> widthMax
)
3561 widthMax
= sizeLine
.x
;
3565 else // wxLC_ALIGN_LEFT
3567 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3571 if ( HasFlag(wxLC_ALIGN_TOP
) )
3573 // traverse the items again and tweak their sizes so that they are
3574 // all the same in a row
3575 for ( i
= 0; i
< count
; i
++ )
3577 wxListLineData
*line
= GetLine(i
);
3578 line
->m_gi
->ExtendWidth(widthMax
);
3582 GetListCtrl()->SetScrollbars
3586 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3587 (y
+ lineHeight
) / lineHeight
,
3588 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3589 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3593 else // "flowed" arrangement, the most complicated case
3595 // at first we try without any scrollbars, if the items don't fit into
3596 // the window, we recalculate after subtracting the space taken by the
3599 int entireWidth
= 0;
3601 for (int tries
= 0; tries
< 2; tries
++)
3603 entireWidth
= 2 * EXTRA_BORDER_X
;
3607 // Now we have decided that the items do not fit into the
3608 // client area, so we need a scrollbar
3609 entireWidth
+= SCROLL_UNIT_X
;
3612 int x
= EXTRA_BORDER_X
;
3613 int y
= EXTRA_BORDER_Y
;
3614 int maxWidthInThisRow
= 0;
3617 int currentlyVisibleLines
= 0;
3619 for (size_t i
= 0; i
< count
; i
++)
3621 currentlyVisibleLines
++;
3622 wxListLineData
*line
= GetLine( i
);
3623 line
->CalculateSize( &dc
, iconSpacing
);
3624 line
->SetPosition( x
, y
, iconSpacing
);
3626 wxSize sizeLine
= GetLineSize( i
);
3628 if ( maxWidthInThisRow
< sizeLine
.x
)
3629 maxWidthInThisRow
= sizeLine
.x
;
3632 if (currentlyVisibleLines
> m_linesPerPage
)
3633 m_linesPerPage
= currentlyVisibleLines
;
3635 if ( y
+ sizeLine
.y
>= clientHeight
)
3637 currentlyVisibleLines
= 0;
3639 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3640 x
+= maxWidthInThisRow
;
3641 entireWidth
+= maxWidthInThisRow
;
3642 maxWidthInThisRow
= 0;
3645 // We have reached the last item.
3646 if ( i
== count
- 1 )
3647 entireWidth
+= maxWidthInThisRow
;
3649 if ( (tries
== 0) &&
3650 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3652 clientHeight
-= wxSystemSettings::
3653 GetMetric(wxSYS_HSCROLL_Y
);
3658 if ( i
== count
- 1 )
3659 tries
= 1; // Everything fits, no second try required.
3663 GetListCtrl()->SetScrollbars
3667 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3669 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3678 // FIXME: why should we call it from here?
3685 void wxListMainWindow::RefreshAll()
3690 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3691 if ( headerWin
&& headerWin
->m_dirty
)
3693 headerWin
->m_dirty
= false;
3694 headerWin
->Refresh();
3698 void wxListMainWindow::UpdateCurrent()
3700 if ( !HasCurrent() && !IsEmpty() )
3704 long wxListMainWindow::GetNextItem( long item
,
3705 int WXUNUSED(geometry
),
3709 max
= GetItemCount();
3710 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3711 wxT("invalid listctrl index in GetNextItem()") );
3713 // notice that we start with the next item (or the first one if item == -1)
3714 // and this is intentional to allow writing a simple loop to iterate over
3715 // all selected items
3718 // this is not an error because the index was OK initially,
3719 // just no such item
3726 size_t count
= GetItemCount();
3727 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3729 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3732 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3739 // ----------------------------------------------------------------------------
3741 // ----------------------------------------------------------------------------
3743 void wxListMainWindow::DeleteItem( long lindex
)
3745 size_t count
= GetItemCount();
3747 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3748 wxT("invalid item index in DeleteItem") );
3750 size_t index
= (size_t)lindex
;
3752 // we don't need to adjust the index for the previous items
3753 if ( HasCurrent() && m_current
>= index
)
3755 // if the current item is being deleted, we want the next one to
3756 // become selected - unless there is no next one - so don't adjust
3757 // m_current in this case
3758 if ( m_current
!= index
|| m_current
== count
- 1 )
3762 if ( InReportView() )
3764 // mark the Column Max Width cache as dirty if the items in the line
3765 // we're deleting contain the Max Column Width
3766 wxListLineData
* const line
= GetLine(index
);
3767 wxListItemDataList::compatibility_iterator n
;
3768 wxListItemData
*itemData
;
3772 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3774 n
= line
->m_items
.Item( i
);
3775 itemData
= n
->GetData();
3776 itemData
->GetItem(item
);
3778 itemWidth
= GetItemWidthWithImage(&item
);
3780 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3781 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3784 ResetVisibleLinesRange();
3787 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3792 m_selStore
.OnItemDelete(index
);
3796 m_lines
.RemoveAt( index
);
3799 // we need to refresh the (vert) scrollbar as the number of items changed
3802 RefreshAfter(index
);
3805 void wxListMainWindow::DeleteColumn( int col
)
3807 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3809 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3812 delete node
->GetData();
3813 m_columns
.Erase( node
);
3817 // update all the items
3818 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3820 wxListLineData
* const line
= GetLine(i
);
3821 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3822 delete n
->GetData();
3823 line
->m_items
.Erase(n
);
3827 if ( InReportView() ) // we only cache max widths when in Report View
3829 delete m_aColWidths
.Item(col
);
3830 m_aColWidths
.RemoveAt(col
);
3833 // invalidate it as it has to be recalculated
3837 void wxListMainWindow::DoDeleteAllItems()
3840 // nothing to do - in particular, don't send the event
3845 // to make the deletion of all items faster, we don't send the
3846 // notifications for each item deletion in this case but only one event
3847 // for all of them: this is compatible with wxMSW and documented in
3848 // DeleteAllItems() description
3850 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3851 event
.SetEventObject( GetParent() );
3852 GetParent()->GetEventHandler()->ProcessEvent( event
);
3860 if ( InReportView() )
3862 ResetVisibleLinesRange();
3863 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3865 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3872 void wxListMainWindow::DeleteAllItems()
3876 RecalculatePositions();
3879 void wxListMainWindow::DeleteEverything()
3881 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3882 WX_CLEAR_ARRAY(m_aColWidths
);
3887 // ----------------------------------------------------------------------------
3888 // scanning for an item
3889 // ----------------------------------------------------------------------------
3891 void wxListMainWindow::EnsureVisible( long index
)
3893 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3894 wxT("invalid index in EnsureVisible") );
3896 // We have to call this here because the label in question might just have
3897 // been added and its position is not known yet
3899 RecalculatePositions(true /* no refresh */);
3901 MoveToItem((size_t)index
);
3904 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3910 wxString str_upper
= str
.Upper();
3914 size_t count
= GetItemCount();
3915 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3917 wxListLineData
*line
= GetLine(i
);
3918 wxString line_upper
= line
->GetText(0).Upper();
3921 if (line_upper
== str_upper
)
3926 if (line_upper
.find(str_upper
) == 0)
3934 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3940 size_t count
= GetItemCount();
3941 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3943 wxListLineData
*line
= GetLine(i
);
3945 line
->GetItem( 0, item
);
3946 if (item
.m_data
== data
)
3953 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3956 GetVisibleLinesRange( &topItem
, NULL
);
3959 GetItemPosition( GetItemCount() - 1, p
);
3963 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3964 if ( id
>= 0 && id
< (long)GetItemCount() )
3970 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
3972 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3974 size_t count
= GetItemCount();
3976 if ( InReportView() )
3978 size_t current
= y
/ GetLineHeight();
3979 if ( current
< count
)
3981 flags
= HitTestLine(current
, x
, y
);
3988 // TODO: optimize it too! this is less simple than for report view but
3989 // enumerating all items is still not a way to do it!!
3990 for ( size_t current
= 0; current
< count
; current
++ )
3992 flags
= HitTestLine(current
, x
, y
);
4001 // ----------------------------------------------------------------------------
4003 // ----------------------------------------------------------------------------
4005 void wxListMainWindow::InsertItem( wxListItem
&item
)
4007 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4009 int count
= GetItemCount();
4010 wxCHECK_RET( item
.m_itemId
>= 0, wxT("invalid item index") );
4012 if (item
.m_itemId
> count
)
4013 item
.m_itemId
= count
;
4015 size_t id
= item
.m_itemId
;
4019 if ( InReportView() )
4021 ResetVisibleLinesRange();
4023 const unsigned col
= item
.GetColumn();
4024 wxCHECK_RET( col
< m_aColWidths
.size(), "invalid item column" );
4026 // calculate the width of the item and adjust the max column width
4027 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(col
);
4028 int width
= GetItemWidthWithImage(&item
);
4029 item
.SetWidth(width
);
4030 if (width
> pWidthInfo
->nMaxWidth
)
4031 pWidthInfo
->nMaxWidth
= width
;
4034 wxListLineData
*line
= new wxListLineData(this);
4036 line
->SetItem( item
.m_col
, item
);
4038 m_lines
.Insert( line
, id
);
4042 // If an item is selected at or below the point of insertion, we need to
4043 // increment the member variables because the current row's index has gone
4045 if ( HasCurrent() && m_current
>= id
)
4048 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4050 RefreshLines(id
, GetItemCount() - 1);
4053 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4056 if ( InReportView() )
4058 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4059 item
.m_width
= GetTextLength( item
.m_text
);
4061 wxListHeaderData
*column
= new wxListHeaderData( item
);
4062 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4064 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4067 wxListHeaderDataList::compatibility_iterator
4068 node
= m_columns
.Item( col
);
4069 m_columns
.Insert( node
, column
);
4070 m_aColWidths
.Insert( colWidthInfo
, col
);
4074 m_columns
.Append( column
);
4075 m_aColWidths
.Add( colWidthInfo
);
4080 // update all the items
4081 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4083 wxListLineData
* const line
= GetLine(i
);
4084 wxListItemData
* const data
= new wxListItemData(this);
4086 line
->m_items
.Insert(col
, data
);
4088 line
->m_items
.Append(data
);
4092 // invalidate it as it has to be recalculated
4097 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4100 wxClientDC
dc(this);
4102 dc
.SetFont( GetFont() );
4104 if (item
->GetImage() != -1)
4107 GetImageSize( item
->GetImage(), ix
, iy
);
4111 if (!item
->GetText().empty())
4114 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4121 // ----------------------------------------------------------------------------
4123 // ----------------------------------------------------------------------------
4125 static wxListCtrlCompare list_ctrl_compare_func_2
;
4126 static wxIntPtr list_ctrl_compare_data
;
4128 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4130 wxListLineData
*line1
= *arg1
;
4131 wxListLineData
*line2
= *arg2
;
4133 line1
->GetItem( 0, item
);
4134 wxUIntPtr data1
= item
.m_data
;
4135 line2
->GetItem( 0, item
);
4136 wxUIntPtr data2
= item
.m_data
;
4137 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4140 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4142 // selections won't make sense any more after sorting the items so reset
4144 HighlightAll(false);
4147 list_ctrl_compare_func_2
= fn
;
4148 list_ctrl_compare_data
= data
;
4149 m_lines
.Sort( list_ctrl_compare_func_1
);
4153 // ----------------------------------------------------------------------------
4155 // ----------------------------------------------------------------------------
4157 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4159 // update our idea of which lines are shown when we redraw the window the
4161 ResetVisibleLinesRange();
4163 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4165 wxGenericListCtrl
* lc
= GetListCtrl();
4166 wxCHECK_RET( lc
, wxT("no listctrl window?") );
4168 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4170 lc
->m_headerWin
->Refresh();
4171 lc
->m_headerWin
->Update();
4176 int wxListMainWindow::GetCountPerPage() const
4178 if ( !m_linesPerPage
)
4180 wxConstCast(this, wxListMainWindow
)->
4181 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4184 return m_linesPerPage
;
4187 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4189 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4191 if ( m_lineFrom
== (size_t)-1 )
4193 size_t count
= GetItemCount();
4196 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4198 // this may happen if SetScrollbars() hadn't been called yet
4199 if ( m_lineFrom
>= count
)
4200 m_lineFrom
= count
- 1;
4202 // we redraw one extra line but this is needed to make the redrawing
4203 // logic work when there is a fractional number of lines on screen
4204 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4205 if ( m_lineTo
>= count
)
4206 m_lineTo
= count
- 1;
4208 else // empty control
4211 m_lineTo
= (size_t)-1;
4215 wxASSERT_MSG( IsEmpty() ||
4216 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4217 wxT("GetVisibleLinesRange() returns incorrect result") );
4225 // -------------------------------------------------------------------------------------
4226 // wxGenericListCtrl
4227 // -------------------------------------------------------------------------------------
4229 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4231 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4232 EVT_SIZE(wxGenericListCtrl::OnSize
)
4233 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4236 void wxGenericListCtrl::Init()
4238 m_imageListNormal
= NULL
;
4239 m_imageListSmall
= NULL
;
4240 m_imageListState
= NULL
;
4242 m_ownsImageListNormal
=
4243 m_ownsImageListSmall
=
4244 m_ownsImageListState
= false;
4250 wxGenericListCtrl::~wxGenericListCtrl()
4252 if (m_ownsImageListNormal
)
4253 delete m_imageListNormal
;
4254 if (m_ownsImageListSmall
)
4255 delete m_imageListSmall
;
4256 if (m_ownsImageListState
)
4257 delete m_imageListState
;
4260 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4262 bool needs_header
= HasHeader();
4263 bool has_header
= (m_headerWin
!= NULL
);
4265 if (needs_header
== has_header
)
4270 m_headerWin
= new wxListHeaderWindow
4272 this, wxID_ANY
, m_mainWin
,
4277 wxRendererNative::Get().GetHeaderButtonHeight(this)
4282 #if defined( __WXMAC__ )
4283 static wxFont
font( wxOSX_SYSTEM_FONT_SMALL
);
4284 m_headerWin
->SetFont( font
);
4287 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4291 GetSizer()->Detach( m_headerWin
);
4293 wxDELETE(m_headerWin
);
4297 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4302 const wxValidator
&validator
,
4303 const wxString
&name
)
4307 // just like in other ports, an assert will fail if the user doesn't give any type style:
4308 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4309 wxT("wxListCtrl style should have exactly one mode bit set") );
4311 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4315 style
&= ~wxBORDER_MASK
;
4316 style
|= wxBORDER_THEME
;
4319 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4321 SetTargetWindow( m_mainWin
);
4323 // We use the cursor keys for moving the selection, not scrolling, so call
4324 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4325 // keyboard events forwarded to us from wxListMainWindow.
4326 DisableKeyboardScrolling();
4328 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4329 sizer
->Add( m_mainWin
, 1, wxGROW
);
4332 CreateOrDestroyHeaderWindowAsNeeded();
4334 SetInitialSize(size
);
4339 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4341 return wxBORDER_THEME
;
4344 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4345 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4349 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4351 // we need to process arrows ourselves for scrolling
4352 if ( nMsg
== WM_GETDLGCODE
)
4354 rc
|= DLGC_WANTARROWS
;
4361 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4363 wxSize newsize
= size
;
4365 newsize
.y
-= m_headerWin
->GetSize().y
;
4370 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4372 // update our idea of which lines are shown when we redraw
4373 // the window the next time
4374 m_mainWin
->ResetVisibleLinesRange();
4376 HandleOnScroll( event
);
4378 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4380 m_headerWin
->Refresh();
4381 m_headerWin
->Update();
4385 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4387 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4388 wxT("wxLC_VIRTUAL can't be [un]set") );
4390 long flag
= GetWindowStyle();
4394 if (style
& wxLC_MASK_TYPE
)
4395 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4396 if (style
& wxLC_MASK_ALIGN
)
4397 flag
&= ~wxLC_MASK_ALIGN
;
4398 if (style
& wxLC_MASK_SORT
)
4399 flag
&= ~wxLC_MASK_SORT
;
4407 // some styles can be set without recreating everything (as happens in
4408 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4409 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4412 wxWindow::SetWindowStyleFlag(flag
);
4416 SetWindowStyleFlag( flag
);
4420 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4422 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4423 // makes sense to remove them as we'll always add scrollbars anyhow when
4425 flag
|= wxHSCROLL
| wxVSCROLL
;
4427 const bool wasInReportView
= HasFlag(wxLC_REPORT
);
4429 // update the window style first so that the header is created or destroyed
4430 // corresponding to the new style
4431 wxWindow::SetWindowStyleFlag( flag
);
4435 const bool inReportView
= (flag
& wxLC_REPORT
) != 0;
4436 if ( inReportView
!= wasInReportView
)
4438 // we need to notify the main window about this change as it must
4439 // update its data structures
4440 m_mainWin
->SetReportView(inReportView
);
4443 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4445 CreateOrDestroyHeaderWindowAsNeeded();
4447 GetSizer()->Layout();
4451 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4453 m_mainWin
->GetColumn( col
, item
);
4457 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4459 m_mainWin
->SetColumn( col
, item
);
4463 int wxGenericListCtrl::GetColumnWidth( int col
) const
4465 return m_mainWin
->GetColumnWidth( col
);
4468 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4470 m_mainWin
->SetColumnWidth( col
, width
);
4474 int wxGenericListCtrl::GetCountPerPage() const
4476 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4479 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4481 m_mainWin
->GetItem( info
);
4485 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4487 m_mainWin
->SetItem( info
);
4491 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4494 info
.m_text
= label
;
4495 info
.m_mask
= wxLIST_MASK_TEXT
;
4496 info
.m_itemId
= index
;
4500 info
.m_image
= imageId
;
4501 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4504 m_mainWin
->SetItem(info
);
4508 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4510 return m_mainWin
->GetItemState( item
, stateMask
);
4513 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4515 m_mainWin
->SetItemState( item
, state
, stateMask
);
4520 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4522 return SetItemColumnImage(item
, 0, image
);
4526 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4529 info
.m_image
= image
;
4530 info
.m_mask
= wxLIST_MASK_IMAGE
;
4531 info
.m_itemId
= item
;
4532 info
.m_col
= column
;
4533 m_mainWin
->SetItem( info
);
4537 wxString
wxGenericListCtrl::GetItemText( long item
, int col
) const
4539 return m_mainWin
->GetItemText(item
, col
);
4542 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4544 m_mainWin
->SetItemText(item
, str
);
4547 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4550 info
.m_mask
= wxLIST_MASK_DATA
;
4551 info
.m_itemId
= item
;
4552 m_mainWin
->GetItem( info
);
4556 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4559 info
.m_mask
= wxLIST_MASK_DATA
;
4560 info
.m_itemId
= item
;
4562 m_mainWin
->SetItem( info
);
4566 wxRect
wxGenericListCtrl::GetViewRect() const
4568 return m_mainWin
->GetViewRect();
4571 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4573 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4576 bool wxGenericListCtrl::GetSubItemRect(long item
,
4579 int WXUNUSED(code
)) const
4581 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4584 if ( m_mainWin
->HasHeader() )
4585 rect
.y
+= m_headerWin
->GetSize().y
+ 1;
4590 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4592 m_mainWin
->GetItemPosition( item
, pos
);
4596 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4601 int wxGenericListCtrl::GetItemCount() const
4603 return m_mainWin
->GetItemCount();
4606 int wxGenericListCtrl::GetColumnCount() const
4608 return m_mainWin
->GetColumnCount();
4611 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4613 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4616 wxSize
wxGenericListCtrl::GetItemSpacing() const
4618 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4620 return wxSize(spacing
, spacing
);
4623 #if WXWIN_COMPATIBILITY_2_6
4624 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4626 return m_mainWin
->GetItemSpacing( isSmall
);
4628 #endif // WXWIN_COMPATIBILITY_2_6
4630 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4633 info
.m_itemId
= item
;
4634 info
.SetTextColour( col
);
4635 m_mainWin
->SetItem( info
);
4638 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4641 info
.m_itemId
= item
;
4642 m_mainWin
->GetItem( info
);
4643 return info
.GetTextColour();
4646 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4649 info
.m_itemId
= item
;
4650 info
.SetBackgroundColour( col
);
4651 m_mainWin
->SetItem( info
);
4654 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4657 info
.m_itemId
= item
;
4658 m_mainWin
->GetItem( info
);
4659 return info
.GetBackgroundColour();
4662 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4665 info
.m_itemId
= item
;
4667 m_mainWin
->SetItem( info
);
4670 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4673 info
.m_itemId
= item
;
4674 m_mainWin
->GetItem( info
);
4675 return info
.GetFont();
4678 int wxGenericListCtrl::GetSelectedItemCount() const
4680 return m_mainWin
->GetSelectedItemCount();
4683 wxColour
wxGenericListCtrl::GetTextColour() const
4685 return GetForegroundColour();
4688 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4690 SetForegroundColour(col
);
4693 long wxGenericListCtrl::GetTopItem() const
4696 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4700 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4702 return m_mainWin
->GetNextItem( item
, geom
, state
);
4705 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4707 if (which
== wxIMAGE_LIST_NORMAL
)
4708 return m_imageListNormal
;
4709 else if (which
== wxIMAGE_LIST_SMALL
)
4710 return m_imageListSmall
;
4711 else if (which
== wxIMAGE_LIST_STATE
)
4712 return m_imageListState
;
4717 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4719 if ( which
== wxIMAGE_LIST_NORMAL
)
4721 if (m_ownsImageListNormal
)
4722 delete m_imageListNormal
;
4723 m_imageListNormal
= imageList
;
4724 m_ownsImageListNormal
= false;
4726 else if ( which
== wxIMAGE_LIST_SMALL
)
4728 if (m_ownsImageListSmall
)
4729 delete m_imageListSmall
;
4730 m_imageListSmall
= imageList
;
4731 m_ownsImageListSmall
= false;
4733 else if ( which
== wxIMAGE_LIST_STATE
)
4735 if (m_ownsImageListState
)
4736 delete m_imageListState
;
4737 m_imageListState
= imageList
;
4738 m_ownsImageListState
= false;
4741 m_mainWin
->SetImageList( imageList
, which
);
4744 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4746 SetImageList(imageList
, which
);
4747 if ( which
== wxIMAGE_LIST_NORMAL
)
4748 m_ownsImageListNormal
= true;
4749 else if ( which
== wxIMAGE_LIST_SMALL
)
4750 m_ownsImageListSmall
= true;
4751 else if ( which
== wxIMAGE_LIST_STATE
)
4752 m_ownsImageListState
= true;
4755 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4760 bool wxGenericListCtrl::DeleteItem( long item
)
4762 m_mainWin
->DeleteItem( item
);
4766 bool wxGenericListCtrl::DeleteAllItems()
4768 m_mainWin
->DeleteAllItems();
4772 bool wxGenericListCtrl::DeleteAllColumns()
4774 size_t count
= m_mainWin
->m_columns
.GetCount();
4775 for ( size_t n
= 0; n
< count
; n
++ )
4780 void wxGenericListCtrl::ClearAll()
4782 m_mainWin
->DeleteEverything();
4785 bool wxGenericListCtrl::DeleteColumn( int col
)
4787 m_mainWin
->DeleteColumn( col
);
4789 // if we don't have the header any longer, we need to relayout the window
4790 // if ( !GetColumnCount() )
4795 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4796 wxClassInfo
* textControlClass
)
4798 return m_mainWin
->EditLabel( item
, textControlClass
);
4801 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4803 return m_mainWin
->GetEditControl();
4806 bool wxGenericListCtrl::EnsureVisible( long item
)
4808 m_mainWin
->EnsureVisible( item
);
4812 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4814 return m_mainWin
->FindItem( start
, str
, partial
);
4817 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4819 return m_mainWin
->FindItem( start
, data
);
4822 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4823 int WXUNUSED(direction
))
4825 return m_mainWin
->FindItem( pt
);
4828 // TODO: sub item hit testing
4829 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4831 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4834 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4836 m_mainWin
->InsertItem( info
);
4837 return info
.m_itemId
;
4840 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4843 info
.m_text
= label
;
4844 info
.m_mask
= wxLIST_MASK_TEXT
;
4845 info
.m_itemId
= index
;
4846 return InsertItem( info
);
4849 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4852 info
.m_mask
= wxLIST_MASK_IMAGE
;
4853 info
.m_image
= imageIndex
;
4854 info
.m_itemId
= index
;
4855 return InsertItem( info
);
4858 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4861 info
.m_text
= label
;
4862 info
.m_image
= imageIndex
;
4863 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4864 info
.m_itemId
= index
;
4865 return InsertItem( info
);
4868 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4870 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4872 m_mainWin
->InsertColumn( col
, item
);
4874 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4875 // still have m_headerWin==NULL
4877 m_headerWin
->Refresh();
4882 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4883 int format
, int width
)
4886 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4887 item
.m_text
= heading
;
4890 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4891 item
.m_width
= width
;
4894 item
.m_format
= format
;
4896 return InsertColumn( col
, item
);
4899 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4901 return m_mainWin
->ScrollList(dx
, dy
);
4905 // fn is a function which takes 3 long arguments: item1, item2, data.
4906 // item1 is the long data associated with a first item (NOT the index).
4907 // item2 is the long data associated with a second item (NOT the index).
4908 // data is the same value as passed to SortItems.
4909 // The return value is a negative number if the first item should precede the second
4910 // item, a positive number of the second item should precede the first,
4911 // or zero if the two items are equivalent.
4912 // data is arbitrary data to be passed to the sort function.
4914 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4916 m_mainWin
->SortItems( fn
, data
);
4920 // ----------------------------------------------------------------------------
4922 // ----------------------------------------------------------------------------
4924 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4926 if (!m_mainWin
) return;
4928 // We need to override OnSize so that our scrolled
4929 // window a) does call Layout() to use sizers for
4930 // positioning the controls but b) does not query
4931 // the sizer for their size and use that for setting
4932 // the scrollable area as set that ourselves by
4933 // calling SetScrollbar() further down.
4937 m_mainWin
->RecalculatePositions();
4942 void wxGenericListCtrl::OnInternalIdle()
4944 wxWindow::OnInternalIdle();
4946 if (m_mainWin
->m_dirty
)
4947 m_mainWin
->RecalculatePositions();
4950 // ----------------------------------------------------------------------------
4952 // ----------------------------------------------------------------------------
4954 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4958 m_mainWin
->SetBackgroundColour( colour
);
4959 m_mainWin
->m_dirty
= true;
4965 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4967 if ( !wxWindow::SetForegroundColour( colour
) )
4972 m_mainWin
->SetForegroundColour( colour
);
4973 m_mainWin
->m_dirty
= true;
4977 m_headerWin
->SetForegroundColour( colour
);
4982 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4984 if ( !wxWindow::SetFont( font
) )
4989 m_mainWin
->SetFont( font
);
4990 m_mainWin
->m_dirty
= true;
4995 m_headerWin
->SetFont( font
);
4996 // CalculateAndSetHeaderHeight();
5006 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5009 // Use the same color scheme as wxListBox
5010 return wxListBox::GetClassDefaultAttributes(variant
);
5012 wxUnusedVar(variant
);
5013 wxVisualAttributes attr
;
5014 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5015 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5016 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5021 // ----------------------------------------------------------------------------
5022 // methods forwarded to m_mainWin
5023 // ----------------------------------------------------------------------------
5025 #if wxUSE_DRAG_AND_DROP
5027 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5029 m_mainWin
->SetDropTarget( dropTarget
);
5032 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5034 return m_mainWin
->GetDropTarget();
5039 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5041 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5044 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5046 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5049 wxColour
wxGenericListCtrl::GetForegroundColour() const
5051 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5054 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5057 return m_mainWin
->PopupMenu( menu
, x
, y
);
5063 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5065 m_mainWin
->DoClientToScreen(x
, y
);
5068 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5070 m_mainWin
->DoScreenToClient(x
, y
);
5073 void wxGenericListCtrl::SetFocus()
5075 // The test in window.cpp fails as we are a composite
5076 // window, so it checks against "this", but not m_mainWin.
5077 if ( DoFindFocus() != this )
5078 m_mainWin
->SetFocus();
5081 wxSize
wxGenericListCtrl::DoGetBestClientSize() const
5083 // Something is better than nothing even if this is completely arbitrary.
5084 wxSize
sizeBest(100, 80);
5086 if ( !InReportView() )
5088 // Ensure that our minimal width is at least big enough to show all our
5089 // items. This is important for wxListbook to size itself correctly.
5091 // Remember the offset of the first item: this corresponds to the
5092 // margins around the item so we will add it to the minimal size below
5093 // to ensure that we have equal margins on all sides.
5096 // We can iterate over all items as there shouldn't be too many of them
5097 // in non-report view. If it ever becomes a problem, we could examine
5098 // just the first few items probably, the determination of the best
5099 // size is less important if we will need scrollbars anyhow.
5100 for ( int n
= 0; n
< GetItemCount(); n
++ )
5102 const wxRect itemRect
= m_mainWin
->GetLineRect(n
);
5105 // Remember the position of the first item as all the rest are
5106 // offset by at least this number of pixels too.
5107 ofs
= itemRect
.GetPosition();
5110 sizeBest
.IncTo(itemRect
.GetSize());
5113 sizeBest
.IncBy(2*ofs
);
5116 // If we have the scrollbars we need to account for them too. And to
5117 // make sure the scrollbars status is up to date we need to call this
5118 // function to set them.
5119 m_mainWin
->RecalculatePositions(true /* no refresh */);
5121 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5122 // to use m_mainWin client/virtual size for determination of whether we
5123 // use scrollbars and not the size of this window itself. Maybe that
5124 // function should be extended to work correctly in the case when our
5125 // scrollbars manage a different window from this one but currently it
5127 const wxSize sizeClient
= m_mainWin
->GetClientSize();
5128 const wxSize sizeVirt
= m_mainWin
->GetVirtualSize();
5130 if ( sizeVirt
.x
> sizeClient
.x
/* HasScrollbar(wxHORIZONTAL) */ )
5131 sizeBest
.y
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
5133 if ( sizeVirt
.y
> sizeClient
.y
/* HasScrollbar(wxVERTICAL) */ )
5134 sizeBest
.x
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
5140 // ----------------------------------------------------------------------------
5141 // virtual list control support
5142 // ----------------------------------------------------------------------------
5144 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5146 // this is a pure virtual function, in fact - which is not really pure
5147 // because the controls which are not virtual don't need to implement it
5148 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5150 return wxEmptyString
;
5153 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5155 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5157 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5161 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5164 return OnGetItemImage(item
);
5170 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5172 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5173 wxT("invalid item index in OnGetItemAttr()") );
5175 // no attributes by default
5179 void wxGenericListCtrl::SetItemCount(long count
)
5181 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5183 m_mainWin
->SetItemCount(count
);
5186 void wxGenericListCtrl::RefreshItem(long item
)
5188 m_mainWin
->RefreshLine(item
);
5191 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5193 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5196 // Generic wxListCtrl is more or less a container for two other
5197 // windows which drawings are done upon. These are namely
5198 // 'm_headerWin' and 'm_mainWin'.
5199 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5200 // behaviour wxListCtrl has under wxMSW.
5202 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5206 // The easy case, no rectangle specified.
5208 m_headerWin
->Refresh(eraseBackground
);
5211 m_mainWin
->Refresh(eraseBackground
);
5215 // Refresh the header window
5218 wxRect rectHeader
= m_headerWin
->GetRect();
5219 rectHeader
.Intersect(*rect
);
5220 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5223 m_headerWin
->GetPosition(&x
, &y
);
5224 rectHeader
.Offset(-x
, -y
);
5225 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5229 // Refresh the main window
5232 wxRect rectMain
= m_mainWin
->GetRect();
5233 rectMain
.Intersect(*rect
);
5234 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5237 m_mainWin
->GetPosition(&x
, &y
);
5238 rectMain
.Offset(-x
, -y
);
5239 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5245 void wxGenericListCtrl::Update()
5249 if ( m_mainWin
->m_dirty
)
5250 m_mainWin
->RecalculatePositions();
5252 m_mainWin
->Update();
5256 m_headerWin
->Update();
5259 #endif // wxUSE_LISTCTRL