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 void wxListLineData::ApplyAttributes(wxDC
*dc
,
671 const wxRect
& rectHL
,
675 const wxListItemAttr
* const attr
= GetAttr();
677 wxWindow
* const listctrl
= m_owner
->GetParent();
679 const bool hasFocus
= listctrl
->HasFocus()
680 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
681 && IsControlActive( (ControlRef
)listctrl
->GetHandle() )
687 // don't use foreground colour for drawing highlighted items - this might
688 // make them completely invisible (and there is no way to do bit
689 // arithmetics on wxColour, unfortunately)
699 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
702 else if ( attr
&& attr
->HasTextColour() )
703 colText
= attr
->GetTextColour();
705 colText
= listctrl
->GetForegroundColour();
707 dc
->SetTextForeground(colText
);
711 if ( attr
&& attr
->HasFont() )
712 font
= attr
->GetFont();
714 font
= listctrl
->GetFont();
721 // Use the renderer method to ensure that the selected items use the
723 int flags
= wxCONTROL_SELECTED
;
725 flags
|= wxCONTROL_FOCUSED
;
727 flags
|= wxCONTROL_CURRENT
;
728 wxRendererNative::Get().
729 DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
731 else if ( attr
&& attr
->HasBackgroundColour() )
733 // Draw the background using the items custom background colour.
734 dc
->SetBrush(attr
->GetBackgroundColour());
735 dc
->SetPen(*wxTRANSPARENT_PEN
);
736 dc
->DrawRectangle(rectHL
);
739 // just for debugging to better see where the items are
741 dc
->SetPen(*wxRED_PEN
);
742 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
743 dc
->DrawRectangle( m_gi
->m_rectAll
);
744 dc
->SetPen(*wxGREEN_PEN
);
745 dc
->DrawRectangle( m_gi
->m_rectIcon
);
749 void wxListLineData::Draw(wxDC
*dc
, bool current
)
751 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
752 wxCHECK_RET( node
, wxT("no subitems at all??") );
754 ApplyAttributes(dc
, m_gi
->m_rectHighlight
, IsHighlighted(), current
);
756 wxListItemData
*item
= node
->GetData();
757 if (item
->HasImage())
759 // centre the image inside our rectangle, this looks nicer when items
760 // ae aligned in a row
761 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
763 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
768 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
770 wxDCClipper
clipper(*dc
, rectLabel
);
771 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
775 void wxListLineData::DrawInReportMode( wxDC
*dc
,
777 const wxRect
& rectHL
,
781 // TODO: later we should support setting different attributes for
782 // different columns - to do it, just add "col" argument to
783 // GetAttr() and move these lines into the loop below
785 ApplyAttributes(dc
, rectHL
, highlighted
, current
);
787 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
788 yMid
= rect
.y
+ rect
.height
/2;
790 // This probably needs to be done
791 // on all platforms as the icons
792 // otherwise nearly touch the border
797 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
799 node
= node
->GetNext(), col
++ )
801 wxListItemData
*item
= node
->GetData();
803 int width
= m_owner
->GetColumnWidth(col
);
808 const int wText
= width
;
809 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
811 if ( item
->HasImage() )
814 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
815 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
817 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
823 if ( item
->HasText() )
824 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
);
828 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
829 const wxString
& textOrig
,
835 // we don't support displaying multiple lines currently (and neither does
836 // wxMSW FWIW) so just merge all the lines
837 wxString
text(textOrig
);
838 text
.Replace(wxT("\n"), wxT(" "));
841 dc
->GetTextExtent(text
, &w
, &h
);
843 const wxCoord y
= yMid
- (h
+ 1)/2;
845 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
847 // determine if the string can fit inside the current width
850 // it can, draw it using the items alignment
852 m_owner
->GetColumn(col
, item
);
853 switch ( item
.GetAlign() )
855 case wxLIST_FORMAT_LEFT
:
859 case wxLIST_FORMAT_RIGHT
:
863 case wxLIST_FORMAT_CENTER
:
864 x
+= (width
- w
) / 2;
868 wxFAIL_MSG( wxT("unknown list item format") );
872 dc
->DrawText(text
, x
, y
);
874 else // otherwise, truncate and add an ellipsis if possible
876 // determine the base width
877 wxString
ellipsis(wxT("..."));
879 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
881 // continue until we have enough space or only one character left
883 size_t len
= text
.length();
884 wxString drawntext
= text
.Left(len
);
887 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
888 drawntext
.RemoveLast();
891 if (w
+ base_w
<= width
)
895 // if still not enough space, remove ellipsis characters
896 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
898 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
899 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
903 dc
->DrawText(drawntext
, x
, y
);
904 dc
->DrawText(ellipsis
, x
+ w
, y
);
908 bool wxListLineData::Highlight( bool on
)
910 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
912 if ( on
== m_highlighted
)
920 void wxListLineData::ReverseHighlight( void )
922 Highlight(!IsHighlighted());
925 //-----------------------------------------------------------------------------
926 // wxListHeaderWindow
927 //-----------------------------------------------------------------------------
929 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
930 EVT_PAINT (wxListHeaderWindow::OnPaint
)
931 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
932 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
935 void wxListHeaderWindow::Init()
937 m_currentCursor
= NULL
;
938 m_isDragging
= false;
940 m_sendSetColumnWidth
= false;
943 wxListHeaderWindow::wxListHeaderWindow()
948 m_resizeCursor
= NULL
;
951 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
953 wxListMainWindow
*owner
,
957 const wxString
&name
)
958 : wxWindow( win
, id
, pos
, size
, style
, name
)
963 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
966 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
967 SetOwnForegroundColour( attr
.colFg
);
968 SetOwnBackgroundColour( attr
.colBg
);
970 SetOwnFont( attr
.font
);
972 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
973 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
975 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
979 wxListHeaderWindow::~wxListHeaderWindow()
981 delete m_resizeCursor
;
984 #ifdef __WXUNIVERSAL__
985 #include "wx/univ/renderer.h"
986 #include "wx/univ/theme.h"
989 // shift the DC origin to match the position of the main window horz
990 // scrollbar: this allows us to always use logical coords
991 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
993 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
996 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
999 parent
->GetViewStart( &view_start
, NULL
);
1004 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1006 // account for the horz scrollbar offset
1008 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1010 // Maybe we just have to check for m_signX
1011 // in the DC, but I leave the #ifdef __WXGTK__
1013 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1017 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1020 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1022 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1024 wxPaintDC
dc( this );
1028 dc
.SetFont( GetFont() );
1030 // width and height of the entire header window
1032 GetClientSize( &w
, &h
);
1033 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1035 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1036 dc
.SetTextForeground(GetForegroundColour());
1038 int x
= HEADER_OFFSET_X
;
1039 int numColumns
= m_owner
->GetColumnCount();
1041 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1043 m_owner
->GetColumn( i
, item
);
1044 int wCol
= item
.m_width
;
1050 if (!m_parent
->IsEnabled())
1051 flags
|= wxCONTROL_DISABLED
;
1053 // NB: The code below is not really Mac-specific, but since we are close
1054 // to 2.8 release and I don't have time to test on other platforms, I
1055 // defined this only for wxMac. If this behavior is desired on
1056 // other platforms, please go ahead and revise or remove the #ifdef.
1058 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1059 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1060 flags
|= wxCONTROL_SELECTED
;
1064 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1066 wxRendererNative::Get().DrawHeaderButton
1070 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1074 // see if we have enough space for the column label
1076 // for this we need the width of the text
1079 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1080 wLabel
+= 2 * EXTRA_WIDTH
;
1082 // and the width of the icon, if any
1083 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1084 const int image
= item
.m_image
;
1085 wxImageList
*imageList
;
1088 imageList
= m_owner
->GetSmallImageList();
1091 imageList
->GetSize(image
, ix
, iy
);
1092 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1100 // ignore alignment if there is not enough space anyhow
1102 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1105 wxFAIL_MSG( wxT("unknown list item format") );
1108 case wxLIST_FORMAT_LEFT
:
1112 case wxLIST_FORMAT_RIGHT
:
1113 xAligned
= x
+ cw
- wLabel
;
1116 case wxLIST_FORMAT_CENTER
:
1117 xAligned
= x
+ (cw
- wLabel
) / 2;
1121 // draw the text and image clipping them so that they
1122 // don't overwrite the column boundary
1123 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
);
1125 // if we have an image, draw it on the right of the label
1132 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1133 HEADER_OFFSET_Y
+ (h
- iy
)/2,
1134 wxIMAGELIST_DRAW_TRANSPARENT
1138 dc
.DrawText( item
.GetText(),
1139 xAligned
+ EXTRA_WIDTH
, (h
- hLabel
) / 2 );
1144 // Fill in what's missing to the right of the columns, otherwise we will
1145 // leave an unpainted area when columns are removed (and it looks better)
1148 wxRendererNative::Get().DrawHeaderButton
1152 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1153 wxCONTROL_DIRTY
// mark as last column
1158 void wxListHeaderWindow::OnInternalIdle()
1160 wxWindow::OnInternalIdle();
1162 if (m_sendSetColumnWidth
)
1164 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1165 m_sendSetColumnWidth
= false;
1169 void wxListHeaderWindow::DrawCurrent()
1172 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1173 m_sendSetColumnWidth
= true;
1174 m_colToSend
= m_column
;
1175 m_widthToSend
= m_currentX
- m_minX
;
1177 int x1
= m_currentX
;
1179 m_owner
->ClientToScreen( &x1
, &y1
);
1181 int x2
= m_currentX
;
1183 m_owner
->GetClientSize( NULL
, &y2
);
1184 m_owner
->ClientToScreen( &x2
, &y2
);
1187 dc
.SetLogicalFunction( wxINVERT
);
1188 dc
.SetPen( wxPen(*wxBLACK
, 2) );
1189 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1193 dc
.DrawLine( x1
, y1
, x2
, y2
);
1195 dc
.SetLogicalFunction( wxCOPY
);
1197 dc
.SetPen( wxNullPen
);
1198 dc
.SetBrush( wxNullBrush
);
1202 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1204 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1206 // we want to work with logical coords
1208 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1209 int y
= event
.GetY();
1213 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1215 // we don't draw the line beyond our window, but we allow dragging it
1218 GetClientSize( &w
, NULL
);
1219 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1222 // erase the line if it was drawn
1223 if ( m_currentX
< w
)
1226 if (event
.ButtonUp())
1229 m_isDragging
= false;
1231 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1232 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1239 m_currentX
= m_minX
+ 7;
1241 // draw in the new location
1242 if ( m_currentX
< w
)
1246 else // not dragging
1249 bool hit_border
= false;
1251 // end of the current column
1254 // find the column where this event occurred
1256 countCol
= m_owner
->GetColumnCount();
1257 for (col
= 0; col
< countCol
; col
++)
1259 xpos
+= m_owner
->GetColumnWidth( col
);
1262 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1264 // near the column border
1271 // inside the column
1278 if ( col
== countCol
)
1281 if (event
.LeftDown() || event
.RightUp())
1283 if (hit_border
&& event
.LeftDown())
1285 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1286 event
.GetPosition()) )
1288 m_isDragging
= true;
1293 //else: column resizing was vetoed by the user code
1295 else // click on a column
1297 // record the selected state of the columns
1298 if (event
.LeftDown())
1300 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
1303 m_owner
->GetColumn(i
, colItem
);
1304 long state
= colItem
.GetState();
1306 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
1308 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
1309 m_owner
->SetColumn(i
, colItem
);
1313 SendListEvent( event
.LeftDown()
1314 ? wxEVT_COMMAND_LIST_COL_CLICK
1315 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1316 event
.GetPosition());
1319 else if (event
.Moving())
1324 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1325 m_currentCursor
= m_resizeCursor
;
1329 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1330 m_currentCursor
= wxSTANDARD_CURSOR
;
1334 SetCursor(*m_currentCursor
);
1339 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1341 m_owner
->SetFocus();
1345 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1347 wxWindow
*parent
= GetParent();
1348 wxListEvent
le( type
, parent
->GetId() );
1349 le
.SetEventObject( parent
);
1350 le
.m_pointDrag
= pos
;
1352 // the position should be relative to the parent window, not
1353 // this one for compatibility with MSW and common sense: the
1354 // user code doesn't know anything at all about this header
1355 // window, so why should it get positions relative to it?
1356 le
.m_pointDrag
.y
-= GetSize().y
;
1358 le
.m_col
= m_column
;
1359 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1362 //-----------------------------------------------------------------------------
1363 // wxListRenameTimer (internal)
1364 //-----------------------------------------------------------------------------
1366 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1371 void wxListRenameTimer::Notify()
1373 m_owner
->OnRenameTimer();
1376 //-----------------------------------------------------------------------------
1377 // wxListTextCtrlWrapper (internal)
1378 //-----------------------------------------------------------------------------
1380 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
1381 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
1382 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
1383 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
1386 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
1389 : m_startValue(owner
->GetItemText(itemEdit
)),
1390 m_itemEdited(itemEdit
)
1394 m_aboutToFinish
= false;
1396 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1398 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1400 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1401 &rectLabel
.x
, &rectLabel
.y
);
1403 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
1404 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1405 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
1408 m_text
->PushEventHandler(this);
1411 void wxListTextCtrlWrapper::EndEdit(EndReason reason
)
1413 m_aboutToFinish
= true;
1418 // Notify the owner about the changes
1421 // Even if vetoed, close the control (consistent with MSW)
1426 m_owner
->OnRenameCancelled(m_itemEdited
);
1432 // Don't generate any notifications for the control being destroyed
1433 // and don't set focus to it neither.
1439 void wxListTextCtrlWrapper::Finish( bool setfocus
)
1441 m_text
->RemoveEventHandler(this);
1442 m_owner
->ResetTextControl( m_text
);
1444 wxPendingDelete
.Append( this );
1447 m_owner
->SetFocus();
1450 bool wxListTextCtrlWrapper::AcceptChanges()
1452 const wxString value
= m_text
->GetValue();
1454 // notice that we should always call OnRenameAccept() to generate the "end
1455 // label editing" event, even if the user hasn't really changed anything
1456 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
1458 // vetoed by the user
1462 // accepted, do rename the item (unless nothing changed)
1463 if ( value
!= m_startValue
)
1464 m_owner
->SetItemText(m_itemEdited
, value
);
1469 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
1471 switch ( event
.m_keyCode
)
1474 EndEdit( End_Accept
);
1478 EndEdit( End_Discard
);
1486 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
1488 if (m_aboutToFinish
)
1490 // auto-grow the textctrl:
1491 wxSize parentSize
= m_owner
->GetSize();
1492 wxPoint myPos
= m_text
->GetPosition();
1493 wxSize mySize
= m_text
->GetSize();
1495 m_text
->GetTextExtent(m_text
->GetValue() + wxT("MM"), &sx
, &sy
);
1496 if (myPos
.x
+ sx
> parentSize
.x
)
1497 sx
= parentSize
.x
- myPos
.x
;
1500 m_text
->SetSize(sx
, wxDefaultCoord
);
1506 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
1508 if ( !m_aboutToFinish
)
1510 if ( !AcceptChanges() )
1511 m_owner
->OnRenameCancelled( m_itemEdited
);
1516 // We must let the native text control handle focus
1520 //-----------------------------------------------------------------------------
1522 //-----------------------------------------------------------------------------
1524 BEGIN_EVENT_TABLE(wxListMainWindow
, wxWindow
)
1525 EVT_PAINT (wxListMainWindow::OnPaint
)
1526 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1527 EVT_CHAR (wxListMainWindow::OnChar
)
1528 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1529 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
1530 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1531 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1532 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1533 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
1536 void wxListMainWindow::Init()
1541 m_lineTo
= (size_t)-1;
1547 m_small_image_list
= NULL
;
1548 m_normal_image_list
= NULL
;
1550 m_small_spacing
= 30;
1551 m_normal_spacing
= 40;
1555 m_isCreated
= false;
1557 m_lastOnSame
= false;
1558 m_renameTimer
= new wxListRenameTimer( this );
1559 m_textctrlWrapper
= NULL
;
1563 m_lineSelectSingleOnUp
=
1564 m_lineBeforeLastClicked
= (size_t)-1;
1567 wxListMainWindow::wxListMainWindow()
1572 m_highlightUnfocusedBrush
= NULL
;
1575 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1580 const wxString
&name
)
1581 : wxWindow( parent
, id
, pos
, size
, style
, name
)
1585 m_highlightBrush
= new wxBrush
1587 wxSystemSettings::GetColour
1589 wxSYS_COLOUR_HIGHLIGHT
1594 m_highlightUnfocusedBrush
= new wxBrush
1596 wxSystemSettings::GetColour
1598 wxSYS_COLOUR_BTNSHADOW
1603 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
1604 SetOwnForegroundColour( attr
.colFg
);
1605 SetOwnBackgroundColour( attr
.colBg
);
1607 SetOwnFont( attr
.font
);
1610 wxListMainWindow::~wxListMainWindow()
1612 if ( m_textctrlWrapper
)
1613 m_textctrlWrapper
->EndEdit(wxListTextCtrlWrapper::End_Destroy
);
1616 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
1617 WX_CLEAR_ARRAY(m_aColWidths
);
1619 delete m_highlightBrush
;
1620 delete m_highlightUnfocusedBrush
;
1621 delete m_renameTimer
;
1624 void wxListMainWindow::SetReportView(bool inReportView
)
1626 const size_t count
= m_lines
.size();
1627 for ( size_t n
= 0; n
< count
; n
++ )
1629 m_lines
[n
].SetReportView(inReportView
);
1633 void wxListMainWindow::CacheLineData(size_t line
)
1635 wxGenericListCtrl
*listctrl
= GetListCtrl();
1637 wxListLineData
*ld
= GetDummyLine();
1639 size_t countCol
= GetColumnCount();
1640 for ( size_t col
= 0; col
< countCol
; col
++ )
1642 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1643 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1646 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1649 wxListLineData
*wxListMainWindow::GetDummyLine() const
1651 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1652 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1654 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1656 // we need to recreate the dummy line if the number of columns in the
1657 // control changed as it would have the incorrect number of fields
1659 if ( !m_lines
.IsEmpty() &&
1660 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1662 self
->m_lines
.Clear();
1665 if ( m_lines
.IsEmpty() )
1667 wxListLineData
*line
= new wxListLineData(self
);
1668 self
->m_lines
.Add(line
);
1670 // don't waste extra memory -- there never going to be anything
1671 // else/more in this array
1672 self
->m_lines
.Shrink();
1678 // ----------------------------------------------------------------------------
1679 // line geometry (report mode only)
1680 // ----------------------------------------------------------------------------
1682 wxCoord
wxListMainWindow::GetLineHeight() const
1684 // we cache the line height as calling GetTextExtent() is slow
1685 if ( !m_lineHeight
)
1687 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1689 wxClientDC
dc( self
);
1690 dc
.SetFont( GetFont() );
1693 dc
.GetTextExtent(wxT("H"), NULL
, &y
);
1695 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1698 m_small_image_list
->GetSize(0, iw
, ih
);
1703 self
->m_lineHeight
= y
+ LINE_SPACING
;
1706 return m_lineHeight
;
1709 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1711 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1713 return LINE_SPACING
+ line
* GetLineHeight();
1716 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1718 if ( !InReportView() )
1719 return GetLine(line
)->m_gi
->m_rectAll
;
1722 rect
.x
= HEADER_OFFSET_X
;
1723 rect
.y
= GetLineY(line
);
1724 rect
.width
= GetHeaderWidth();
1725 rect
.height
= GetLineHeight();
1730 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1732 if ( !InReportView() )
1733 return GetLine(line
)->m_gi
->m_rectLabel
;
1736 wxListLineData
*data
= GetLine(line
);
1737 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1740 wxListItemData
*item
= node
->GetData();
1741 if ( item
->HasImage() )
1744 GetImageSize( item
->GetImage(), ix
, iy
);
1745 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1750 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1751 rect
.y
= GetLineY(line
);
1752 rect
.width
= GetColumnWidth(0) - image_x
;
1753 rect
.height
= GetLineHeight();
1758 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1760 if ( !InReportView() )
1761 return GetLine(line
)->m_gi
->m_rectIcon
;
1763 wxListLineData
*ld
= GetLine(line
);
1764 wxASSERT_MSG( ld
->HasImage(), wxT("should have an image") );
1767 rect
.x
= HEADER_OFFSET_X
;
1768 rect
.y
= GetLineY(line
);
1769 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1774 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1776 return InReportView() ? GetLineRect(line
)
1777 : GetLine(line
)->m_gi
->m_rectHighlight
;
1780 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1782 wxASSERT_MSG( line
< GetItemCount(), wxT("invalid line in HitTestLine") );
1784 wxListLineData
*ld
= GetLine(line
);
1786 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1787 return wxLIST_HITTEST_ONITEMICON
;
1789 // VS: Testing for "ld->HasText() || InReportView()" instead of
1790 // "ld->HasText()" is needed to make empty lines in report view
1792 if ( ld
->HasText() || InReportView() )
1794 wxRect rect
= InReportView() ? GetLineRect(line
)
1795 : GetLineLabelRect(line
);
1797 if ( rect
.Contains(x
, y
) )
1798 return wxLIST_HITTEST_ONITEMLABEL
;
1804 // ----------------------------------------------------------------------------
1805 // highlight (selection) handling
1806 // ----------------------------------------------------------------------------
1808 bool wxListMainWindow::IsHighlighted(size_t line
) const
1812 return m_selStore
.IsSelected(line
);
1816 wxListLineData
*ld
= GetLine(line
);
1817 wxCHECK_MSG( ld
, false, wxT("invalid index in IsHighlighted") );
1819 return ld
->IsHighlighted();
1823 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1829 wxArrayInt linesChanged
;
1830 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1833 // meny items changed state, refresh everything
1834 RefreshLines(lineFrom
, lineTo
);
1836 else // only a few items changed state, refresh only them
1838 size_t count
= linesChanged
.GetCount();
1839 for ( size_t n
= 0; n
< count
; n
++ )
1841 RefreshLine(linesChanged
[n
]);
1845 else // iterate over all items in non report view
1847 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1849 if ( HighlightLine(line
, highlight
) )
1855 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1861 changed
= m_selStore
.SelectItem(line
, highlight
);
1865 wxListLineData
*ld
= GetLine(line
);
1866 wxCHECK_MSG( ld
, false, wxT("invalid index in HighlightLine") );
1868 changed
= ld
->Highlight(highlight
);
1873 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1874 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1880 void wxListMainWindow::RefreshLine( size_t line
)
1882 if ( InReportView() )
1884 size_t visibleFrom
, visibleTo
;
1885 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1887 if ( line
< visibleFrom
|| line
> visibleTo
)
1891 wxRect rect
= GetLineRect(line
);
1893 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1894 RefreshRect( rect
);
1897 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1899 // we suppose that they are ordered by caller
1900 wxASSERT_MSG( lineFrom
<= lineTo
, wxT("indices in disorder") );
1902 wxASSERT_MSG( lineTo
< GetItemCount(), wxT("invalid line range") );
1904 if ( InReportView() )
1906 size_t visibleFrom
, visibleTo
;
1907 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1909 if ( lineFrom
< visibleFrom
)
1910 lineFrom
= visibleFrom
;
1911 if ( lineTo
> visibleTo
)
1916 rect
.y
= GetLineY(lineFrom
);
1917 rect
.width
= GetClientSize().x
;
1918 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1920 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1921 RefreshRect( rect
);
1925 // TODO: this should be optimized...
1926 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1933 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1935 if ( InReportView() )
1937 size_t visibleFrom
, visibleTo
;
1938 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1940 if ( lineFrom
< visibleFrom
)
1941 lineFrom
= visibleFrom
;
1942 else if ( lineFrom
> visibleTo
)
1947 rect
.y
= GetLineY(lineFrom
);
1948 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1950 wxSize size
= GetClientSize();
1951 rect
.width
= size
.x
;
1953 // refresh till the bottom of the window
1954 rect
.height
= size
.y
- rect
.y
;
1956 RefreshRect( rect
);
1960 // TODO: how to do it more efficiently?
1965 void wxListMainWindow::RefreshSelected()
1971 if ( InReportView() )
1973 GetVisibleLinesRange(&from
, &to
);
1978 to
= GetItemCount() - 1;
1981 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
1982 RefreshLine(m_current
);
1984 for ( size_t line
= from
; line
<= to
; line
++ )
1986 // NB: the test works as expected even if m_current == -1
1987 if ( line
!= m_current
&& IsHighlighted(line
) )
1992 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1994 // Note: a wxPaintDC must be constructed even if no drawing is
1995 // done (a Windows requirement).
1996 wxPaintDC
dc( this );
2000 // nothing to draw or not the moment to draw it
2005 RecalculatePositions( false );
2007 GetListCtrl()->PrepareDC( dc
);
2010 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2012 dc
.SetFont( GetFont() );
2014 if ( InReportView() )
2016 int lineHeight
= GetLineHeight();
2018 size_t visibleFrom
, visibleTo
;
2019 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2022 int xOrig
= dc
.LogicalToDeviceX( 0 );
2023 int yOrig
= dc
.LogicalToDeviceY( 0 );
2025 // tell the caller cache to cache the data
2028 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2029 GetParent()->GetId());
2030 evCache
.SetEventObject( GetParent() );
2031 evCache
.m_oldItemIndex
= visibleFrom
;
2032 evCache
.m_itemIndex
= visibleTo
;
2033 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2036 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2038 rectLine
= GetLineRect(line
);
2041 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2042 rectLine
.width
, rectLine
.height
) )
2044 // don't redraw unaffected lines to avoid flicker
2048 GetLine(line
)->DrawInReportMode( &dc
,
2050 GetLineHighlightRect(line
),
2051 IsHighlighted(line
),
2052 line
== m_current
);
2055 if ( HasFlag(wxLC_HRULES
) )
2057 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2058 wxSize clientSize
= GetClientSize();
2060 size_t i
= visibleFrom
;
2061 if (i
== 0) i
= 1; // Don't draw the first one
2062 for ( ; i
<= visibleTo
; i
++ )
2065 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2066 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2067 clientSize
.x
- dev_x
, i
* lineHeight
);
2070 // Draw last horizontal rule
2071 if ( visibleTo
== GetItemCount() - 1 )
2074 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2075 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2076 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2080 // Draw vertical rules if required
2081 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2083 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2084 wxRect firstItemRect
, lastItemRect
;
2086 GetItemRect(visibleFrom
, firstItemRect
);
2087 GetItemRect(visibleTo
, lastItemRect
);
2088 int x
= firstItemRect
.GetX();
2090 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2092 for (int col
= 0; col
< GetColumnCount(); col
++)
2094 int colWidth
= GetColumnWidth(col
);
2096 int x_pos
= x
- dev_x
;
2097 if (col
< GetColumnCount()-1) x_pos
-= 2;
2098 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2099 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2105 size_t count
= GetItemCount();
2106 for ( size_t i
= 0; i
< count
; i
++ )
2108 GetLine(i
)->Draw( &dc
, i
== m_current
);
2112 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2113 // rectangle somehow and so leaves traces when the item is not selected any
2114 // more, see #12229.
2119 if ( IsHighlighted(m_current
) )
2120 flags
|= wxCONTROL_SELECTED
;
2122 wxRendererNative::Get().
2123 DrawFocusRect(this, dc
, GetLineHighlightRect(m_current
), flags
);
2125 #endif // !__WXMAC__
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(wxListTextCtrlWrapper::End_Accept
);
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
))
2697 // send a list event
2698 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, parent
->GetId() );
2699 le
.m_itemIndex
= m_current
;
2701 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2702 le
.m_code
= event
.GetKeyCode();
2703 le
.SetEventObject( parent
);
2704 if (parent
->GetEventHandler()->ProcessEvent( le
))
2710 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2712 wxWindow
*parent
= GetParent();
2714 // propagate the key event upwards
2715 wxKeyEvent
ke(event
);
2716 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2722 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2724 wxWindow
*parent
= GetParent();
2726 // propagate the char event upwards
2727 wxKeyEvent
ke(event
);
2728 ke
.SetEventObject( parent
);
2729 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2732 if ( HandleAsNavigationKey(event
) )
2735 // no item -> nothing to do
2742 // don't use m_linesPerPage directly as it might not be computed yet
2743 const int pageSize
= GetCountPerPage();
2744 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
2746 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2748 if (event
.GetKeyCode() == WXK_RIGHT
)
2749 event
.m_keyCode
= WXK_LEFT
;
2750 else if (event
.GetKeyCode() == WXK_LEFT
)
2751 event
.m_keyCode
= WXK_RIGHT
;
2754 switch ( event
.GetKeyCode() )
2757 if ( m_current
> 0 )
2758 OnArrowChar( m_current
- 1, event
);
2762 if ( m_current
< (size_t)GetItemCount() - 1 )
2763 OnArrowChar( m_current
+ 1, event
);
2768 OnArrowChar( GetItemCount() - 1, event
);
2773 OnArrowChar( 0, event
);
2778 int steps
= InReportView() ? pageSize
- 1
2779 : m_current
% pageSize
;
2781 int index
= m_current
- steps
;
2785 OnArrowChar( index
, event
);
2791 int steps
= InReportView()
2793 : pageSize
- (m_current
% pageSize
) - 1;
2795 size_t index
= m_current
+ steps
;
2796 size_t count
= GetItemCount();
2797 if ( index
>= count
)
2800 OnArrowChar( index
, event
);
2805 if ( !InReportView() )
2807 int index
= m_current
- pageSize
;
2811 OnArrowChar( index
, event
);
2816 if ( !InReportView() )
2818 size_t index
= m_current
+ pageSize
;
2820 size_t count
= GetItemCount();
2821 if ( index
>= count
)
2824 OnArrowChar( index
, event
);
2829 if ( IsSingleSel() )
2831 if ( event
.ControlDown() )
2833 ReverseHighlight(m_current
);
2835 else // normal space press
2837 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2840 else // multiple selection
2842 ReverseHighlight(m_current
);
2848 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2856 // ----------------------------------------------------------------------------
2858 // ----------------------------------------------------------------------------
2860 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2864 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2865 event
.SetEventObject( GetParent() );
2866 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2870 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2871 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2872 // which are already drawn correctly resulting in horrible flicker - avoid
2882 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2886 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2887 event
.SetEventObject( GetParent() );
2888 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2896 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2898 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2900 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2902 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2904 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2906 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2908 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2910 else if ( InReportView() && (m_small_image_list
))
2912 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2916 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2918 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2920 m_normal_image_list
->GetSize( index
, width
, height
);
2922 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2924 m_small_image_list
->GetSize( index
, width
, height
);
2926 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2928 m_small_image_list
->GetSize( index
, width
, height
);
2930 else if ( InReportView() && m_small_image_list
)
2932 m_small_image_list
->GetSize( index
, width
, height
);
2941 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2943 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2944 dc
.SetFont( GetFont() );
2947 dc
.GetTextExtent( s
, &lw
, NULL
);
2949 return lw
+ AUTOSIZE_COL_MARGIN
;
2952 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2956 // calc the spacing from the icon size
2957 int width
= 0, height
= 0;
2959 if ((imageList
) && (imageList
->GetImageCount()) )
2960 imageList
->GetSize(0, width
, height
);
2962 if (which
== wxIMAGE_LIST_NORMAL
)
2964 m_normal_image_list
= imageList
;
2965 m_normal_spacing
= width
+ 8;
2968 if (which
== wxIMAGE_LIST_SMALL
)
2970 m_small_image_list
= imageList
;
2971 m_small_spacing
= width
+ 14;
2972 m_lineHeight
= 0; // ensure that the line height will be recalc'd
2976 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
2980 m_small_spacing
= spacing
;
2982 m_normal_spacing
= spacing
;
2985 int wxListMainWindow::GetItemSpacing( bool isSmall
)
2987 return isSmall
? m_small_spacing
: m_normal_spacing
;
2990 // ----------------------------------------------------------------------------
2992 // ----------------------------------------------------------------------------
2994 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
2996 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
2998 wxCHECK_RET( node
, wxT("invalid column index in SetColumn") );
3000 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3001 item
.m_width
= GetTextLength( item
.m_text
);
3003 wxListHeaderData
*column
= node
->GetData();
3004 column
->SetItem( item
);
3006 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3008 headerWin
->m_dirty
= true;
3012 // invalidate it as it has to be recalculated
3016 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3018 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3019 wxT("invalid column index") );
3021 wxCHECK_RET( InReportView(),
3022 wxT("SetColumnWidth() can only be called in report mode.") );
3026 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3028 headerWin
->m_dirty
= true;
3030 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3031 wxCHECK_RET( node
, wxT("no column?") );
3033 wxListHeaderData
*column
= node
->GetData();
3035 size_t count
= GetItemCount();
3037 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3039 width
= GetTextLength(column
->GetText());
3040 width
+= 2*EXTRA_WIDTH
;
3042 // check for column header's image availability
3043 const int image
= column
->GetImage();
3046 if ( m_small_image_list
)
3049 m_small_image_list
->GetSize(image
, ix
, iy
);
3050 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3054 else if ( width
== wxLIST_AUTOSIZE
)
3058 // TODO: determine the max width somehow...
3059 width
= WIDTH_COL_DEFAULT
;
3063 wxClientDC
dc(this);
3064 dc
.SetFont( GetFont() );
3066 int max
= AUTOSIZE_COL_MARGIN
;
3068 // if the cached column width isn't valid then recalculate it
3069 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3071 for (size_t i
= 0; i
< count
; i
++)
3073 wxListLineData
*line
= GetLine( i
);
3074 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3076 wxCHECK_RET( n
, wxT("no subitem?") );
3078 wxListItemData
*itemData
= n
->GetData();
3081 itemData
->GetItem(item
);
3082 int itemWidth
= GetItemWidthWithImage(&item
);
3083 if (itemWidth
> max
)
3087 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3088 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3091 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3092 width
= max
+ AUTOSIZE_COL_MARGIN
;
3096 column
->SetWidth( width
);
3098 // invalidate it as it has to be recalculated
3102 int wxListMainWindow::GetHeaderWidth() const
3104 if ( !m_headerWidth
)
3106 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3108 size_t count
= GetColumnCount();
3109 for ( size_t col
= 0; col
< count
; col
++ )
3111 self
->m_headerWidth
+= GetColumnWidth(col
);
3115 return m_headerWidth
;
3118 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3120 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3121 wxCHECK_RET( node
, wxT("invalid column index in GetColumn") );
3123 wxListHeaderData
*column
= node
->GetData();
3124 column
->GetItem( item
);
3127 int wxListMainWindow::GetColumnWidth( int col
) const
3129 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3130 wxCHECK_MSG( node
, 0, wxT("invalid column index") );
3132 wxListHeaderData
*column
= node
->GetData();
3133 return column
->GetWidth();
3136 // ----------------------------------------------------------------------------
3138 // ----------------------------------------------------------------------------
3140 void wxListMainWindow::SetItem( wxListItem
&item
)
3142 long id
= item
.m_itemId
;
3143 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3144 wxT("invalid item index in SetItem") );
3148 wxListLineData
*line
= GetLine((size_t)id
);
3149 line
->SetItem( item
.m_col
, item
);
3151 // Set item state if user wants
3152 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3153 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3157 // update the Max Width Cache if needed
3158 int width
= GetItemWidthWithImage(&item
);
3160 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3161 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3165 // update the item on screen
3167 GetItemRect(id
, rectItem
);
3168 RefreshRect(rectItem
);
3171 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3176 // first deal with selection
3177 if ( stateMask
& wxLIST_STATE_SELECTED
)
3179 // set/clear select state
3182 // optimized version for virtual listctrl.
3183 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3186 else if ( state
& wxLIST_STATE_SELECTED
)
3188 const long count
= GetItemCount();
3189 for( long i
= 0; i
< count
; i
++ )
3191 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3197 // clear for non virtual (somewhat optimized by using GetNextItem())
3199 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3201 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3206 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3208 // unfocus all: only one item can be focussed, so clearing focus for
3209 // all items is simply clearing focus of the focussed item.
3210 SetItemState(m_current
, state
, stateMask
);
3212 //(setting focus to all items makes no sense, so it is not handled here.)
3215 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3219 SetItemStateAll(state
, stateMask
);
3223 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3224 wxT("invalid list ctrl item index in SetItem") );
3226 size_t oldCurrent
= m_current
;
3227 size_t item
= (size_t)litem
; // safe because of the check above
3229 // do we need to change the focus?
3230 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3232 if ( state
& wxLIST_STATE_FOCUSED
)
3234 // don't do anything if this item is already focused
3235 if ( item
!= m_current
)
3237 ChangeCurrent(item
);
3239 if ( oldCurrent
!= (size_t)-1 )
3241 if ( IsSingleSel() )
3243 HighlightLine(oldCurrent
, false);
3246 RefreshLine(oldCurrent
);
3249 RefreshLine( m_current
);
3254 // don't do anything if this item is not focused
3255 if ( item
== m_current
)
3259 if ( IsSingleSel() )
3261 // we must unselect the old current item as well or we
3262 // might end up with more than one selected item in a
3263 // single selection control
3264 HighlightLine(oldCurrent
, false);
3267 RefreshLine( oldCurrent
);
3272 // do we need to change the selection state?
3273 if ( stateMask
& wxLIST_STATE_SELECTED
)
3275 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3277 if ( IsSingleSel() )
3281 // selecting the item also makes it the focused one in the
3283 if ( m_current
!= item
)
3285 ChangeCurrent(item
);
3287 if ( oldCurrent
!= (size_t)-1 )
3289 HighlightLine( oldCurrent
, false );
3290 RefreshLine( oldCurrent
);
3296 // only the current item may be selected anyhow
3297 if ( item
!= m_current
)
3302 if ( HighlightLine(item
, on
) )
3309 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3311 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3312 wxT("invalid list ctrl item index in GetItemState()") );
3314 int ret
= wxLIST_STATE_DONTCARE
;
3316 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3318 if ( (size_t)item
== m_current
)
3319 ret
|= wxLIST_STATE_FOCUSED
;
3322 if ( stateMask
& wxLIST_STATE_SELECTED
)
3324 if ( IsHighlighted(item
) )
3325 ret
|= wxLIST_STATE_SELECTED
;
3331 void wxListMainWindow::GetItem( wxListItem
&item
) const
3333 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3334 wxT("invalid item index in GetItem") );
3336 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3337 line
->GetItem( item
.m_col
, item
);
3339 // Get item state if user wants it
3340 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3341 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3342 wxLIST_STATE_FOCUSED
);
3345 // ----------------------------------------------------------------------------
3347 // ----------------------------------------------------------------------------
3349 size_t wxListMainWindow::GetItemCount() const
3351 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3354 void wxListMainWindow::SetItemCount(long count
)
3356 m_selStore
.SetItemCount(count
);
3357 m_countVirt
= count
;
3359 ResetVisibleLinesRange();
3361 // scrollbars must be reset
3365 int wxListMainWindow::GetSelectedItemCount() const
3367 // deal with the quick case first
3368 if ( IsSingleSel() )
3369 return HasCurrent() ? IsHighlighted(m_current
) : false;
3371 // virtual controls remmebers all its selections itself
3373 return m_selStore
.GetSelectedCount();
3375 // TODO: we probably should maintain the number of items selected even for
3376 // non virtual controls as enumerating all lines is really slow...
3377 size_t countSel
= 0;
3378 size_t count
= GetItemCount();
3379 for ( size_t line
= 0; line
< count
; line
++ )
3381 if ( GetLine(line
)->IsHighlighted() )
3388 // ----------------------------------------------------------------------------
3389 // item position/size
3390 // ----------------------------------------------------------------------------
3392 wxRect
wxListMainWindow::GetViewRect() const
3394 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3396 // we need to find the longest/tallest label
3397 wxCoord xMax
= 0, yMax
= 0;
3398 const int count
= GetItemCount();
3401 for ( int i
= 0; i
< count
; i
++ )
3403 // we need logical, not physical, coordinates here, so use
3404 // GetLineRect() instead of GetItemRect()
3405 wxRect r
= GetLineRect(i
);
3407 wxCoord x
= r
.GetRight(),
3417 // some fudge needed to make it look prettier
3418 xMax
+= 2 * EXTRA_BORDER_X
;
3419 yMax
+= 2 * EXTRA_BORDER_Y
;
3421 // account for the scrollbars if necessary
3422 const wxSize sizeAll
= GetClientSize();
3423 if ( xMax
> sizeAll
.x
)
3424 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3425 if ( yMax
> sizeAll
.y
)
3426 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3428 return wxRect(0, 0, xMax
, yMax
);
3432 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3434 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3436 wxT("GetSubItemRect only meaningful in report view") );
3437 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3438 wxT("invalid item in GetSubItemRect") );
3440 // ensure that we're laid out, otherwise we could return nonsense
3443 wxConstCast(this, wxListMainWindow
)->
3444 RecalculatePositions(true /* no refresh */);
3447 rect
= GetLineRect((size_t)item
);
3449 // Adjust rect to specified column
3450 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3452 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3453 wxT("invalid subItem in GetSubItemRect") );
3455 for (int i
= 0; i
< subItem
; i
++)
3457 rect
.x
+= GetColumnWidth(i
);
3459 rect
.width
= GetColumnWidth(subItem
);
3462 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3467 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3470 GetItemRect(item
, rect
);
3478 // ----------------------------------------------------------------------------
3479 // geometry calculation
3480 // ----------------------------------------------------------------------------
3482 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3484 const int lineHeight
= GetLineHeight();
3486 wxClientDC
dc( this );
3487 dc
.SetFont( GetFont() );
3489 const size_t count
= GetItemCount();
3492 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3493 iconSpacing
= m_normal_spacing
;
3494 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3495 iconSpacing
= m_small_spacing
;
3499 // Note that we do not call GetClientSize() here but
3500 // GetSize() and subtract the border size for sunken
3501 // borders manually. This is technically incorrect,
3502 // but we need to know the client area's size WITHOUT
3503 // scrollbars here. Since we don't know if there are
3504 // any scrollbars, we use GetSize() instead. Another
3505 // solution would be to call SetScrollbars() here to
3506 // remove the scrollbars and call GetClientSize() then,
3507 // but this might result in flicker and - worse - will
3508 // reset the scrollbars to 0 which is not good at all
3509 // if you resize a dialog/window, but don't want to
3510 // reset the window scrolling. RR.
3511 // Furthermore, we actually do NOT subtract the border
3512 // width as 2 pixels is just the extra space which we
3513 // need around the actual content in the window. Other-
3514 // wise the text would e.g. touch the upper border. RR.
3517 GetSize( &clientWidth
, &clientHeight
);
3519 if ( InReportView() )
3521 // all lines have the same height and we scroll one line per step
3522 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3524 m_linesPerPage
= clientHeight
/ lineHeight
;
3526 ResetVisibleLinesRange();
3528 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3529 GetHeaderWidth() / SCROLL_UNIT_X
,
3530 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3531 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3532 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3537 // we have 3 different layout strategies: either layout all items
3538 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3539 // to arrange them in top to bottom, left to right (don't ask me why
3540 // not the other way round...) order
3541 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3543 int x
= EXTRA_BORDER_X
;
3544 int y
= EXTRA_BORDER_Y
;
3546 wxCoord widthMax
= 0;
3549 for ( i
= 0; i
< count
; i
++ )
3551 wxListLineData
*line
= GetLine(i
);
3552 line
->CalculateSize( &dc
, iconSpacing
);
3553 line
->SetPosition( x
, y
, iconSpacing
);
3555 wxSize sizeLine
= GetLineSize(i
);
3557 if ( HasFlag(wxLC_ALIGN_TOP
) )
3559 if ( sizeLine
.x
> widthMax
)
3560 widthMax
= sizeLine
.x
;
3564 else // wxLC_ALIGN_LEFT
3566 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3570 if ( HasFlag(wxLC_ALIGN_TOP
) )
3572 // traverse the items again and tweak their sizes so that they are
3573 // all the same in a row
3574 for ( i
= 0; i
< count
; i
++ )
3576 wxListLineData
*line
= GetLine(i
);
3577 line
->m_gi
->ExtendWidth(widthMax
);
3581 GetListCtrl()->SetScrollbars
3585 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3586 (y
+ lineHeight
) / lineHeight
,
3587 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3588 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3592 else // "flowed" arrangement, the most complicated case
3594 // at first we try without any scrollbars, if the items don't fit into
3595 // the window, we recalculate after subtracting the space taken by the
3598 int entireWidth
= 0;
3600 for (int tries
= 0; tries
< 2; tries
++)
3602 entireWidth
= 2 * EXTRA_BORDER_X
;
3606 // Now we have decided that the items do not fit into the
3607 // client area, so we need a scrollbar
3608 entireWidth
+= SCROLL_UNIT_X
;
3611 int x
= EXTRA_BORDER_X
;
3612 int y
= EXTRA_BORDER_Y
;
3613 int maxWidthInThisRow
= 0;
3616 int currentlyVisibleLines
= 0;
3618 for (size_t i
= 0; i
< count
; i
++)
3620 currentlyVisibleLines
++;
3621 wxListLineData
*line
= GetLine( i
);
3622 line
->CalculateSize( &dc
, iconSpacing
);
3623 line
->SetPosition( x
, y
, iconSpacing
);
3625 wxSize sizeLine
= GetLineSize( i
);
3627 if ( maxWidthInThisRow
< sizeLine
.x
)
3628 maxWidthInThisRow
= sizeLine
.x
;
3631 if (currentlyVisibleLines
> m_linesPerPage
)
3632 m_linesPerPage
= currentlyVisibleLines
;
3634 if ( y
+ sizeLine
.y
>= clientHeight
)
3636 currentlyVisibleLines
= 0;
3638 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3639 x
+= maxWidthInThisRow
;
3640 entireWidth
+= maxWidthInThisRow
;
3641 maxWidthInThisRow
= 0;
3644 // We have reached the last item.
3645 if ( i
== count
- 1 )
3646 entireWidth
+= maxWidthInThisRow
;
3648 if ( (tries
== 0) &&
3649 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3651 clientHeight
-= wxSystemSettings::
3652 GetMetric(wxSYS_HSCROLL_Y
);
3657 if ( i
== count
- 1 )
3658 tries
= 1; // Everything fits, no second try required.
3662 GetListCtrl()->SetScrollbars
3666 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3668 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3677 // FIXME: why should we call it from here?
3684 void wxListMainWindow::RefreshAll()
3689 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3690 if ( headerWin
&& headerWin
->m_dirty
)
3692 headerWin
->m_dirty
= false;
3693 headerWin
->Refresh();
3697 void wxListMainWindow::UpdateCurrent()
3699 if ( !HasCurrent() && !IsEmpty() )
3703 long wxListMainWindow::GetNextItem( long item
,
3704 int WXUNUSED(geometry
),
3708 max
= GetItemCount();
3709 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3710 wxT("invalid listctrl index in GetNextItem()") );
3712 // notice that we start with the next item (or the first one if item == -1)
3713 // and this is intentional to allow writing a simple loop to iterate over
3714 // all selected items
3717 // this is not an error because the index was OK initially,
3718 // just no such item
3725 size_t count
= GetItemCount();
3726 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3728 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3731 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3738 // ----------------------------------------------------------------------------
3740 // ----------------------------------------------------------------------------
3742 void wxListMainWindow::DeleteItem( long lindex
)
3744 size_t count
= GetItemCount();
3746 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3747 wxT("invalid item index in DeleteItem") );
3749 size_t index
= (size_t)lindex
;
3751 // we don't need to adjust the index for the previous items
3752 if ( HasCurrent() && m_current
>= index
)
3754 // if the current item is being deleted, we want the next one to
3755 // become selected - unless there is no next one - so don't adjust
3756 // m_current in this case
3757 if ( m_current
!= index
|| m_current
== count
- 1 )
3761 if ( InReportView() )
3763 // mark the Column Max Width cache as dirty if the items in the line
3764 // we're deleting contain the Max Column Width
3765 wxListLineData
* const line
= GetLine(index
);
3766 wxListItemDataList::compatibility_iterator n
;
3767 wxListItemData
*itemData
;
3771 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3773 n
= line
->m_items
.Item( i
);
3774 itemData
= n
->GetData();
3775 itemData
->GetItem(item
);
3777 itemWidth
= GetItemWidthWithImage(&item
);
3779 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3780 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3783 ResetVisibleLinesRange();
3786 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3791 m_selStore
.OnItemDelete(index
);
3795 m_lines
.RemoveAt( index
);
3798 // we need to refresh the (vert) scrollbar as the number of items changed
3801 RefreshAfter(index
);
3804 void wxListMainWindow::DeleteColumn( int col
)
3806 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3808 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3811 delete node
->GetData();
3812 m_columns
.Erase( node
);
3816 // update all the items
3817 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3819 wxListLineData
* const line
= GetLine(i
);
3820 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3821 delete n
->GetData();
3822 line
->m_items
.Erase(n
);
3826 if ( InReportView() ) // we only cache max widths when in Report View
3828 delete m_aColWidths
.Item(col
);
3829 m_aColWidths
.RemoveAt(col
);
3832 // invalidate it as it has to be recalculated
3836 void wxListMainWindow::DoDeleteAllItems()
3839 // nothing to do - in particular, don't send the event
3844 // to make the deletion of all items faster, we don't send the
3845 // notifications for each item deletion in this case but only one event
3846 // for all of them: this is compatible with wxMSW and documented in
3847 // DeleteAllItems() description
3849 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3850 event
.SetEventObject( GetParent() );
3851 GetParent()->GetEventHandler()->ProcessEvent( event
);
3859 if ( InReportView() )
3861 ResetVisibleLinesRange();
3862 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3864 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3871 void wxListMainWindow::DeleteAllItems()
3875 RecalculatePositions();
3878 void wxListMainWindow::DeleteEverything()
3880 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3881 WX_CLEAR_ARRAY(m_aColWidths
);
3886 // ----------------------------------------------------------------------------
3887 // scanning for an item
3888 // ----------------------------------------------------------------------------
3890 void wxListMainWindow::EnsureVisible( long index
)
3892 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3893 wxT("invalid index in EnsureVisible") );
3895 // We have to call this here because the label in question might just have
3896 // been added and its position is not known yet
3898 RecalculatePositions(true /* no refresh */);
3900 MoveToItem((size_t)index
);
3903 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3909 wxString str_upper
= str
.Upper();
3913 size_t count
= GetItemCount();
3914 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3916 wxListLineData
*line
= GetLine(i
);
3917 wxString line_upper
= line
->GetText(0).Upper();
3920 if (line_upper
== str_upper
)
3925 if (line_upper
.find(str_upper
) == 0)
3933 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3939 size_t count
= GetItemCount();
3940 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3942 wxListLineData
*line
= GetLine(i
);
3944 line
->GetItem( 0, item
);
3945 if (item
.m_data
== data
)
3952 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3955 GetVisibleLinesRange( &topItem
, NULL
);
3958 GetItemPosition( GetItemCount() - 1, p
);
3962 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3963 if ( id
>= 0 && id
< (long)GetItemCount() )
3969 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
3971 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3973 size_t count
= GetItemCount();
3975 if ( InReportView() )
3977 size_t current
= y
/ GetLineHeight();
3978 if ( current
< count
)
3980 flags
= HitTestLine(current
, x
, y
);
3987 // TODO: optimize it too! this is less simple than for report view but
3988 // enumerating all items is still not a way to do it!!
3989 for ( size_t current
= 0; current
< count
; current
++ )
3991 flags
= HitTestLine(current
, x
, y
);
4000 // ----------------------------------------------------------------------------
4002 // ----------------------------------------------------------------------------
4004 void wxListMainWindow::InsertItem( wxListItem
&item
)
4006 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4008 int count
= GetItemCount();
4009 wxCHECK_RET( item
.m_itemId
>= 0, wxT("invalid item index") );
4011 if (item
.m_itemId
> count
)
4012 item
.m_itemId
= count
;
4014 size_t id
= item
.m_itemId
;
4018 if ( InReportView() )
4020 ResetVisibleLinesRange();
4022 const unsigned col
= item
.GetColumn();
4023 wxCHECK_RET( col
< m_aColWidths
.size(), "invalid item column" );
4025 // calculate the width of the item and adjust the max column width
4026 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(col
);
4027 int width
= GetItemWidthWithImage(&item
);
4028 item
.SetWidth(width
);
4029 if (width
> pWidthInfo
->nMaxWidth
)
4030 pWidthInfo
->nMaxWidth
= width
;
4033 wxListLineData
*line
= new wxListLineData(this);
4035 line
->SetItem( item
.m_col
, item
);
4037 m_lines
.Insert( line
, id
);
4041 // If an item is selected at or below the point of insertion, we need to
4042 // increment the member variables because the current row's index has gone
4044 if ( HasCurrent() && m_current
>= id
)
4047 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4049 RefreshLines(id
, GetItemCount() - 1);
4052 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4055 if ( InReportView() )
4057 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4058 item
.m_width
= GetTextLength( item
.m_text
);
4060 wxListHeaderData
*column
= new wxListHeaderData( item
);
4061 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4063 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4066 wxListHeaderDataList::compatibility_iterator
4067 node
= m_columns
.Item( col
);
4068 m_columns
.Insert( node
, column
);
4069 m_aColWidths
.Insert( colWidthInfo
, col
);
4073 m_columns
.Append( column
);
4074 m_aColWidths
.Add( colWidthInfo
);
4079 // update all the items
4080 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4082 wxListLineData
* const line
= GetLine(i
);
4083 wxListItemData
* const data
= new wxListItemData(this);
4085 line
->m_items
.Insert(col
, data
);
4087 line
->m_items
.Append(data
);
4091 // invalidate it as it has to be recalculated
4096 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4099 wxClientDC
dc(this);
4101 dc
.SetFont( GetFont() );
4103 if (item
->GetImage() != -1)
4106 GetImageSize( item
->GetImage(), ix
, iy
);
4110 if (!item
->GetText().empty())
4113 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4120 // ----------------------------------------------------------------------------
4122 // ----------------------------------------------------------------------------
4124 static wxListCtrlCompare list_ctrl_compare_func_2
;
4125 static wxIntPtr list_ctrl_compare_data
;
4127 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4129 wxListLineData
*line1
= *arg1
;
4130 wxListLineData
*line2
= *arg2
;
4132 line1
->GetItem( 0, item
);
4133 wxUIntPtr data1
= item
.m_data
;
4134 line2
->GetItem( 0, item
);
4135 wxUIntPtr data2
= item
.m_data
;
4136 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4139 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4141 // selections won't make sense any more after sorting the items so reset
4143 HighlightAll(false);
4146 list_ctrl_compare_func_2
= fn
;
4147 list_ctrl_compare_data
= data
;
4148 m_lines
.Sort( list_ctrl_compare_func_1
);
4152 // ----------------------------------------------------------------------------
4154 // ----------------------------------------------------------------------------
4156 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4158 // update our idea of which lines are shown when we redraw the window the
4160 ResetVisibleLinesRange();
4162 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4164 wxGenericListCtrl
* lc
= GetListCtrl();
4165 wxCHECK_RET( lc
, wxT("no listctrl window?") );
4167 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4169 lc
->m_headerWin
->Refresh();
4170 lc
->m_headerWin
->Update();
4175 int wxListMainWindow::GetCountPerPage() const
4177 if ( !m_linesPerPage
)
4179 wxConstCast(this, wxListMainWindow
)->
4180 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4183 return m_linesPerPage
;
4186 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4188 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4190 if ( m_lineFrom
== (size_t)-1 )
4192 size_t count
= GetItemCount();
4195 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4197 // this may happen if SetScrollbars() hadn't been called yet
4198 if ( m_lineFrom
>= count
)
4199 m_lineFrom
= count
- 1;
4201 // we redraw one extra line but this is needed to make the redrawing
4202 // logic work when there is a fractional number of lines on screen
4203 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4204 if ( m_lineTo
>= count
)
4205 m_lineTo
= count
- 1;
4207 else // empty control
4210 m_lineTo
= (size_t)-1;
4214 wxASSERT_MSG( IsEmpty() ||
4215 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4216 wxT("GetVisibleLinesRange() returns incorrect result") );
4224 // -------------------------------------------------------------------------------------
4225 // wxGenericListCtrl
4226 // -------------------------------------------------------------------------------------
4228 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4230 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4231 EVT_SIZE(wxGenericListCtrl::OnSize
)
4232 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4235 void wxGenericListCtrl::Init()
4237 m_imageListNormal
= NULL
;
4238 m_imageListSmall
= NULL
;
4239 m_imageListState
= NULL
;
4241 m_ownsImageListNormal
=
4242 m_ownsImageListSmall
=
4243 m_ownsImageListState
= false;
4249 wxGenericListCtrl::~wxGenericListCtrl()
4251 if (m_ownsImageListNormal
)
4252 delete m_imageListNormal
;
4253 if (m_ownsImageListSmall
)
4254 delete m_imageListSmall
;
4255 if (m_ownsImageListState
)
4256 delete m_imageListState
;
4259 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4261 bool needs_header
= HasHeader();
4262 bool has_header
= (m_headerWin
!= NULL
);
4264 if (needs_header
== has_header
)
4269 m_headerWin
= new wxListHeaderWindow
4271 this, wxID_ANY
, m_mainWin
,
4276 wxRendererNative::Get().GetHeaderButtonHeight(this)
4281 #if defined( __WXMAC__ )
4282 static wxFont
font( wxOSX_SYSTEM_FONT_SMALL
);
4283 m_headerWin
->SetFont( font
);
4286 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4290 GetSizer()->Detach( m_headerWin
);
4292 wxDELETE(m_headerWin
);
4296 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4301 const wxValidator
&validator
,
4302 const wxString
&name
)
4306 // just like in other ports, an assert will fail if the user doesn't give any type style:
4307 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4308 wxT("wxListCtrl style should have exactly one mode bit set") );
4310 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4314 style
&= ~wxBORDER_MASK
;
4315 style
|= wxBORDER_THEME
;
4318 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4320 SetTargetWindow( m_mainWin
);
4322 // We use the cursor keys for moving the selection, not scrolling, so call
4323 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4324 // keyboard events forwarded to us from wxListMainWindow.
4325 DisableKeyboardScrolling();
4327 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4328 sizer
->Add( m_mainWin
, 1, wxGROW
);
4331 CreateOrDestroyHeaderWindowAsNeeded();
4333 SetInitialSize(size
);
4338 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4340 return wxBORDER_THEME
;
4343 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4344 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4348 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4350 // we need to process arrows ourselves for scrolling
4351 if ( nMsg
== WM_GETDLGCODE
)
4353 rc
|= DLGC_WANTARROWS
;
4360 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4362 wxSize newsize
= size
;
4364 newsize
.y
-= m_headerWin
->GetSize().y
;
4369 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4371 // update our idea of which lines are shown when we redraw
4372 // the window the next time
4373 m_mainWin
->ResetVisibleLinesRange();
4375 HandleOnScroll( event
);
4377 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4379 m_headerWin
->Refresh();
4380 m_headerWin
->Update();
4384 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4386 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4387 wxT("wxLC_VIRTUAL can't be [un]set") );
4389 long flag
= GetWindowStyle();
4393 if (style
& wxLC_MASK_TYPE
)
4394 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4395 if (style
& wxLC_MASK_ALIGN
)
4396 flag
&= ~wxLC_MASK_ALIGN
;
4397 if (style
& wxLC_MASK_SORT
)
4398 flag
&= ~wxLC_MASK_SORT
;
4406 // some styles can be set without recreating everything (as happens in
4407 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4408 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4411 wxWindow::SetWindowStyleFlag(flag
);
4415 SetWindowStyleFlag( flag
);
4419 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4421 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4422 // makes sense to remove them as we'll always add scrollbars anyhow when
4424 flag
|= wxHSCROLL
| wxVSCROLL
;
4426 const bool wasInReportView
= HasFlag(wxLC_REPORT
);
4428 // update the window style first so that the header is created or destroyed
4429 // corresponding to the new style
4430 wxWindow::SetWindowStyleFlag( flag
);
4434 const bool inReportView
= (flag
& wxLC_REPORT
) != 0;
4435 if ( inReportView
!= wasInReportView
)
4437 // we need to notify the main window about this change as it must
4438 // update its data structures
4439 m_mainWin
->SetReportView(inReportView
);
4442 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4444 CreateOrDestroyHeaderWindowAsNeeded();
4446 GetSizer()->Layout();
4450 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4452 m_mainWin
->GetColumn( col
, item
);
4456 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4458 m_mainWin
->SetColumn( col
, item
);
4462 int wxGenericListCtrl::GetColumnWidth( int col
) const
4464 return m_mainWin
->GetColumnWidth( col
);
4467 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4469 m_mainWin
->SetColumnWidth( col
, width
);
4473 int wxGenericListCtrl::GetCountPerPage() const
4475 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4478 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4480 m_mainWin
->GetItem( info
);
4484 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4486 m_mainWin
->SetItem( info
);
4490 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4493 info
.m_text
= label
;
4494 info
.m_mask
= wxLIST_MASK_TEXT
;
4495 info
.m_itemId
= index
;
4499 info
.m_image
= imageId
;
4500 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4503 m_mainWin
->SetItem(info
);
4507 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4509 return m_mainWin
->GetItemState( item
, stateMask
);
4512 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4514 m_mainWin
->SetItemState( item
, state
, stateMask
);
4519 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4521 return SetItemColumnImage(item
, 0, image
);
4525 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4528 info
.m_image
= image
;
4529 info
.m_mask
= wxLIST_MASK_IMAGE
;
4530 info
.m_itemId
= item
;
4531 info
.m_col
= column
;
4532 m_mainWin
->SetItem( info
);
4536 wxString
wxGenericListCtrl::GetItemText( long item
, int col
) const
4538 return m_mainWin
->GetItemText(item
, col
);
4541 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4543 m_mainWin
->SetItemText(item
, str
);
4546 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4549 info
.m_mask
= wxLIST_MASK_DATA
;
4550 info
.m_itemId
= item
;
4551 m_mainWin
->GetItem( info
);
4555 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4558 info
.m_mask
= wxLIST_MASK_DATA
;
4559 info
.m_itemId
= item
;
4561 m_mainWin
->SetItem( info
);
4565 wxRect
wxGenericListCtrl::GetViewRect() const
4567 return m_mainWin
->GetViewRect();
4570 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4572 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4575 bool wxGenericListCtrl::GetSubItemRect(long item
,
4578 int WXUNUSED(code
)) const
4580 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4583 if ( m_mainWin
->HasHeader() )
4584 rect
.y
+= m_headerWin
->GetSize().y
+ 1;
4589 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4591 m_mainWin
->GetItemPosition( item
, pos
);
4595 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4600 int wxGenericListCtrl::GetItemCount() const
4602 return m_mainWin
->GetItemCount();
4605 int wxGenericListCtrl::GetColumnCount() const
4607 return m_mainWin
->GetColumnCount();
4610 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4612 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4615 wxSize
wxGenericListCtrl::GetItemSpacing() const
4617 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4619 return wxSize(spacing
, spacing
);
4622 #if WXWIN_COMPATIBILITY_2_6
4623 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4625 return m_mainWin
->GetItemSpacing( isSmall
);
4627 #endif // WXWIN_COMPATIBILITY_2_6
4629 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4632 info
.m_itemId
= item
;
4633 info
.SetTextColour( col
);
4634 m_mainWin
->SetItem( info
);
4637 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4640 info
.m_itemId
= item
;
4641 m_mainWin
->GetItem( info
);
4642 return info
.GetTextColour();
4645 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4648 info
.m_itemId
= item
;
4649 info
.SetBackgroundColour( col
);
4650 m_mainWin
->SetItem( info
);
4653 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4656 info
.m_itemId
= item
;
4657 m_mainWin
->GetItem( info
);
4658 return info
.GetBackgroundColour();
4661 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4664 info
.m_itemId
= item
;
4666 m_mainWin
->SetItem( info
);
4669 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4672 info
.m_itemId
= item
;
4673 m_mainWin
->GetItem( info
);
4674 return info
.GetFont();
4677 int wxGenericListCtrl::GetSelectedItemCount() const
4679 return m_mainWin
->GetSelectedItemCount();
4682 wxColour
wxGenericListCtrl::GetTextColour() const
4684 return GetForegroundColour();
4687 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4689 SetForegroundColour(col
);
4692 long wxGenericListCtrl::GetTopItem() const
4695 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4699 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4701 return m_mainWin
->GetNextItem( item
, geom
, state
);
4704 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4706 if (which
== wxIMAGE_LIST_NORMAL
)
4707 return m_imageListNormal
;
4708 else if (which
== wxIMAGE_LIST_SMALL
)
4709 return m_imageListSmall
;
4710 else if (which
== wxIMAGE_LIST_STATE
)
4711 return m_imageListState
;
4716 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4718 if ( which
== wxIMAGE_LIST_NORMAL
)
4720 if (m_ownsImageListNormal
)
4721 delete m_imageListNormal
;
4722 m_imageListNormal
= imageList
;
4723 m_ownsImageListNormal
= false;
4725 else if ( which
== wxIMAGE_LIST_SMALL
)
4727 if (m_ownsImageListSmall
)
4728 delete m_imageListSmall
;
4729 m_imageListSmall
= imageList
;
4730 m_ownsImageListSmall
= false;
4732 else if ( which
== wxIMAGE_LIST_STATE
)
4734 if (m_ownsImageListState
)
4735 delete m_imageListState
;
4736 m_imageListState
= imageList
;
4737 m_ownsImageListState
= false;
4740 m_mainWin
->SetImageList( imageList
, which
);
4743 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4745 SetImageList(imageList
, which
);
4746 if ( which
== wxIMAGE_LIST_NORMAL
)
4747 m_ownsImageListNormal
= true;
4748 else if ( which
== wxIMAGE_LIST_SMALL
)
4749 m_ownsImageListSmall
= true;
4750 else if ( which
== wxIMAGE_LIST_STATE
)
4751 m_ownsImageListState
= true;
4754 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4759 bool wxGenericListCtrl::DeleteItem( long item
)
4761 m_mainWin
->DeleteItem( item
);
4765 bool wxGenericListCtrl::DeleteAllItems()
4767 m_mainWin
->DeleteAllItems();
4771 bool wxGenericListCtrl::DeleteAllColumns()
4773 size_t count
= m_mainWin
->m_columns
.GetCount();
4774 for ( size_t n
= 0; n
< count
; n
++ )
4779 void wxGenericListCtrl::ClearAll()
4781 m_mainWin
->DeleteEverything();
4784 bool wxGenericListCtrl::DeleteColumn( int col
)
4786 m_mainWin
->DeleteColumn( col
);
4788 // if we don't have the header any longer, we need to relayout the window
4789 // if ( !GetColumnCount() )
4794 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4795 wxClassInfo
* textControlClass
)
4797 return m_mainWin
->EditLabel( item
, textControlClass
);
4800 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4802 return m_mainWin
->GetEditControl();
4805 bool wxGenericListCtrl::EnsureVisible( long item
)
4807 m_mainWin
->EnsureVisible( item
);
4811 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4813 return m_mainWin
->FindItem( start
, str
, partial
);
4816 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4818 return m_mainWin
->FindItem( start
, data
);
4821 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4822 int WXUNUSED(direction
))
4824 return m_mainWin
->FindItem( pt
);
4827 // TODO: sub item hit testing
4828 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4830 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4833 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4835 m_mainWin
->InsertItem( info
);
4836 return info
.m_itemId
;
4839 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4842 info
.m_text
= label
;
4843 info
.m_mask
= wxLIST_MASK_TEXT
;
4844 info
.m_itemId
= index
;
4845 return InsertItem( info
);
4848 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4851 info
.m_mask
= wxLIST_MASK_IMAGE
;
4852 info
.m_image
= imageIndex
;
4853 info
.m_itemId
= index
;
4854 return InsertItem( info
);
4857 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4860 info
.m_text
= label
;
4861 info
.m_image
= imageIndex
;
4862 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4863 info
.m_itemId
= index
;
4864 return InsertItem( info
);
4867 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4869 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4871 m_mainWin
->InsertColumn( col
, item
);
4873 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4874 // still have m_headerWin==NULL
4876 m_headerWin
->Refresh();
4881 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4882 int format
, int width
)
4885 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4886 item
.m_text
= heading
;
4889 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4890 item
.m_width
= width
;
4893 item
.m_format
= format
;
4895 return InsertColumn( col
, item
);
4898 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4900 return m_mainWin
->ScrollList(dx
, dy
);
4904 // fn is a function which takes 3 long arguments: item1, item2, data.
4905 // item1 is the long data associated with a first item (NOT the index).
4906 // item2 is the long data associated with a second item (NOT the index).
4907 // data is the same value as passed to SortItems.
4908 // The return value is a negative number if the first item should precede the second
4909 // item, a positive number of the second item should precede the first,
4910 // or zero if the two items are equivalent.
4911 // data is arbitrary data to be passed to the sort function.
4913 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4915 m_mainWin
->SortItems( fn
, data
);
4919 // ----------------------------------------------------------------------------
4921 // ----------------------------------------------------------------------------
4923 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4925 if (!m_mainWin
) return;
4927 // We need to override OnSize so that our scrolled
4928 // window a) does call Layout() to use sizers for
4929 // positioning the controls but b) does not query
4930 // the sizer for their size and use that for setting
4931 // the scrollable area as set that ourselves by
4932 // calling SetScrollbar() further down.
4936 m_mainWin
->RecalculatePositions();
4941 void wxGenericListCtrl::OnInternalIdle()
4943 wxWindow::OnInternalIdle();
4945 if (m_mainWin
->m_dirty
)
4946 m_mainWin
->RecalculatePositions();
4949 // ----------------------------------------------------------------------------
4951 // ----------------------------------------------------------------------------
4953 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4957 m_mainWin
->SetBackgroundColour( colour
);
4958 m_mainWin
->m_dirty
= true;
4964 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4966 if ( !wxWindow::SetForegroundColour( colour
) )
4971 m_mainWin
->SetForegroundColour( colour
);
4972 m_mainWin
->m_dirty
= true;
4976 m_headerWin
->SetForegroundColour( colour
);
4981 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4983 if ( !wxWindow::SetFont( font
) )
4988 m_mainWin
->SetFont( font
);
4989 m_mainWin
->m_dirty
= true;
4994 m_headerWin
->SetFont( font
);
4995 // CalculateAndSetHeaderHeight();
5005 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5008 // Use the same color scheme as wxListBox
5009 return wxListBox::GetClassDefaultAttributes(variant
);
5011 wxUnusedVar(variant
);
5012 wxVisualAttributes attr
;
5013 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5014 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5015 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5020 // ----------------------------------------------------------------------------
5021 // methods forwarded to m_mainWin
5022 // ----------------------------------------------------------------------------
5024 #if wxUSE_DRAG_AND_DROP
5026 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5028 m_mainWin
->SetDropTarget( dropTarget
);
5031 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5033 return m_mainWin
->GetDropTarget();
5038 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5040 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5043 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5045 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5048 wxColour
wxGenericListCtrl::GetForegroundColour() const
5050 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5053 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5056 return m_mainWin
->PopupMenu( menu
, x
, y
);
5062 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5064 // It's not clear whether this can be called before m_mainWin is created
5065 // but it seems better to be on the safe side and check.
5067 m_mainWin
->DoClientToScreen(x
, y
);
5069 wxControl::DoClientToScreen(x
, y
);
5072 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5074 // At least in wxGTK/Univ build this method can be called before m_mainWin
5075 // is created so avoid crashes in this case.
5077 m_mainWin
->DoScreenToClient(x
, y
);
5079 wxControl::DoScreenToClient(x
, y
);
5082 void wxGenericListCtrl::SetFocus()
5084 // The test in window.cpp fails as we are a composite
5085 // window, so it checks against "this", but not m_mainWin.
5086 if ( DoFindFocus() != this )
5087 m_mainWin
->SetFocus();
5090 wxSize
wxGenericListCtrl::DoGetBestClientSize() const
5092 // Something is better than nothing even if this is completely arbitrary.
5093 wxSize
sizeBest(100, 80);
5095 if ( !InReportView() )
5097 // Ensure that our minimal width is at least big enough to show all our
5098 // items. This is important for wxListbook to size itself correctly.
5100 // Remember the offset of the first item: this corresponds to the
5101 // margins around the item so we will add it to the minimal size below
5102 // to ensure that we have equal margins on all sides.
5105 // We can iterate over all items as there shouldn't be too many of them
5106 // in non-report view. If it ever becomes a problem, we could examine
5107 // just the first few items probably, the determination of the best
5108 // size is less important if we will need scrollbars anyhow.
5109 for ( int n
= 0; n
< GetItemCount(); n
++ )
5111 const wxRect itemRect
= m_mainWin
->GetLineRect(n
);
5114 // Remember the position of the first item as all the rest are
5115 // offset by at least this number of pixels too.
5116 ofs
= itemRect
.GetPosition();
5119 sizeBest
.IncTo(itemRect
.GetSize());
5122 sizeBest
.IncBy(2*ofs
);
5125 // If we have the scrollbars we need to account for them too. And to
5126 // make sure the scrollbars status is up to date we need to call this
5127 // function to set them.
5128 m_mainWin
->RecalculatePositions(true /* no refresh */);
5130 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5131 // to use m_mainWin client/virtual size for determination of whether we
5132 // use scrollbars and not the size of this window itself. Maybe that
5133 // function should be extended to work correctly in the case when our
5134 // scrollbars manage a different window from this one but currently it
5136 const wxSize sizeClient
= m_mainWin
->GetClientSize();
5137 const wxSize sizeVirt
= m_mainWin
->GetVirtualSize();
5139 if ( sizeVirt
.x
> sizeClient
.x
/* HasScrollbar(wxHORIZONTAL) */ )
5140 sizeBest
.y
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
5142 if ( sizeVirt
.y
> sizeClient
.y
/* HasScrollbar(wxVERTICAL) */ )
5143 sizeBest
.x
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
5149 // ----------------------------------------------------------------------------
5150 // virtual list control support
5151 // ----------------------------------------------------------------------------
5153 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5155 // this is a pure virtual function, in fact - which is not really pure
5156 // because the controls which are not virtual don't need to implement it
5157 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5159 return wxEmptyString
;
5162 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5164 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5166 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5170 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5173 return OnGetItemImage(item
);
5179 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5181 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5182 wxT("invalid item index in OnGetItemAttr()") );
5184 // no attributes by default
5188 void wxGenericListCtrl::SetItemCount(long count
)
5190 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5192 m_mainWin
->SetItemCount(count
);
5195 void wxGenericListCtrl::RefreshItem(long item
)
5197 m_mainWin
->RefreshLine(item
);
5200 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5202 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5205 // Generic wxListCtrl is more or less a container for two other
5206 // windows which drawings are done upon. These are namely
5207 // 'm_headerWin' and 'm_mainWin'.
5208 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5209 // behaviour wxListCtrl has under wxMSW.
5211 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5215 // The easy case, no rectangle specified.
5217 m_headerWin
->Refresh(eraseBackground
);
5220 m_mainWin
->Refresh(eraseBackground
);
5224 // Refresh the header window
5227 wxRect rectHeader
= m_headerWin
->GetRect();
5228 rectHeader
.Intersect(*rect
);
5229 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5232 m_headerWin
->GetPosition(&x
, &y
);
5233 rectHeader
.Offset(-x
, -y
);
5234 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5238 // Refresh the main window
5241 wxRect rectMain
= m_mainWin
->GetRect();
5242 rectMain
.Intersect(*rect
);
5243 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5246 m_mainWin
->GetPosition(&x
, &y
);
5247 rectMain
.Offset(-x
, -y
);
5248 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5254 void wxGenericListCtrl::Update()
5258 if ( m_mainWin
->m_dirty
)
5259 m_mainWin
->RecalculatePositions();
5261 m_mainWin
->Update();
5265 m_headerWin
->Update();
5268 #endif // wxUSE_LISTCTRL