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
);
807 const int wText
= width
- 8;
808 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
810 if ( item
->HasImage() )
813 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
814 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
816 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
822 if ( item
->HasText() )
823 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
827 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
828 const wxString
& textOrig
,
834 // we don't support displaying multiple lines currently (and neither does
835 // wxMSW FWIW) so just merge all the lines
836 wxString
text(textOrig
);
837 text
.Replace(wxT("\n"), wxT(" "));
840 dc
->GetTextExtent(text
, &w
, &h
);
842 const wxCoord y
= yMid
- (h
+ 1)/2;
844 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
846 // determine if the string can fit inside the current width
849 // it can, draw it using the items alignment
851 m_owner
->GetColumn(col
, item
);
852 switch ( item
.GetAlign() )
854 case wxLIST_FORMAT_LEFT
:
858 case wxLIST_FORMAT_RIGHT
:
862 case wxLIST_FORMAT_CENTER
:
863 x
+= (width
- w
) / 2;
867 wxFAIL_MSG( wxT("unknown list item format") );
871 dc
->DrawText(text
, x
, y
);
873 else // otherwise, truncate and add an ellipsis if possible
875 // determine the base width
876 wxString
ellipsis(wxT("..."));
878 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
880 // continue until we have enough space or only one character left
882 size_t len
= text
.length();
883 wxString drawntext
= text
.Left(len
);
886 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
887 drawntext
.RemoveLast();
890 if (w
+ base_w
<= width
)
894 // if still not enough space, remove ellipsis characters
895 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
897 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
898 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
902 dc
->DrawText(drawntext
, x
, y
);
903 dc
->DrawText(ellipsis
, x
+ w
, y
);
907 bool wxListLineData::Highlight( bool on
)
909 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
911 if ( on
== m_highlighted
)
919 void wxListLineData::ReverseHighlight( void )
921 Highlight(!IsHighlighted());
924 //-----------------------------------------------------------------------------
925 // wxListHeaderWindow
926 //-----------------------------------------------------------------------------
928 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
929 EVT_PAINT (wxListHeaderWindow::OnPaint
)
930 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
931 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
934 void wxListHeaderWindow::Init()
936 m_currentCursor
= NULL
;
937 m_isDragging
= false;
939 m_sendSetColumnWidth
= false;
942 wxListHeaderWindow::wxListHeaderWindow()
947 m_resizeCursor
= NULL
;
950 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
952 wxListMainWindow
*owner
,
956 const wxString
&name
)
957 : wxWindow( win
, id
, pos
, size
, style
, name
)
962 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
965 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
966 SetOwnForegroundColour( attr
.colFg
);
967 SetOwnBackgroundColour( attr
.colBg
);
969 SetOwnFont( attr
.font
);
971 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
972 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
974 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
978 wxListHeaderWindow::~wxListHeaderWindow()
980 delete m_resizeCursor
;
983 #ifdef __WXUNIVERSAL__
984 #include "wx/univ/renderer.h"
985 #include "wx/univ/theme.h"
988 // shift the DC origin to match the position of the main window horz
989 // scrollbar: this allows us to always use logical coords
990 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
992 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
995 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
998 parent
->GetViewStart( &view_start
, NULL
);
1003 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1005 // account for the horz scrollbar offset
1007 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1009 // Maybe we just have to check for m_signX
1010 // in the DC, but I leave the #ifdef __WXGTK__
1012 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1016 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1019 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1021 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1023 wxPaintDC
dc( this );
1027 dc
.SetFont( GetFont() );
1029 // width and height of the entire header window
1031 GetClientSize( &w
, &h
);
1032 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1034 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1035 dc
.SetTextForeground(GetForegroundColour());
1037 int x
= HEADER_OFFSET_X
;
1038 int numColumns
= m_owner
->GetColumnCount();
1040 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1042 m_owner
->GetColumn( i
, item
);
1043 int wCol
= item
.m_width
;
1049 if (!m_parent
->IsEnabled())
1050 flags
|= wxCONTROL_DISABLED
;
1052 // NB: The code below is not really Mac-specific, but since we are close
1053 // to 2.8 release and I don't have time to test on other platforms, I
1054 // defined this only for wxMac. If this behavior is desired on
1055 // other platforms, please go ahead and revise or remove the #ifdef.
1057 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1058 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1059 flags
|= wxCONTROL_SELECTED
;
1063 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1065 wxRendererNative::Get().DrawHeaderButton
1069 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1073 // see if we have enough space for the column label
1075 // for this we need the width of the text
1078 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1079 wLabel
+= 2 * EXTRA_WIDTH
;
1081 // and the width of the icon, if any
1082 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1083 const int image
= item
.m_image
;
1084 wxImageList
*imageList
;
1087 imageList
= m_owner
->GetSmallImageList();
1090 imageList
->GetSize(image
, ix
, iy
);
1091 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1099 // ignore alignment if there is not enough space anyhow
1101 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1104 wxFAIL_MSG( wxT("unknown list item format") );
1107 case wxLIST_FORMAT_LEFT
:
1111 case wxLIST_FORMAT_RIGHT
:
1112 xAligned
= x
+ cw
- wLabel
;
1115 case wxLIST_FORMAT_CENTER
:
1116 xAligned
= x
+ (cw
- wLabel
) / 2;
1120 // draw the text and image clipping them so that they
1121 // don't overwrite the column boundary
1122 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
);
1124 // if we have an image, draw it on the right of the label
1131 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1132 HEADER_OFFSET_Y
+ (h
- iy
)/2,
1133 wxIMAGELIST_DRAW_TRANSPARENT
1137 dc
.DrawText( item
.GetText(),
1138 xAligned
+ EXTRA_WIDTH
, (h
- hLabel
) / 2 );
1143 // Fill in what's missing to the right of the columns, otherwise we will
1144 // leave an unpainted area when columns are removed (and it looks better)
1147 wxRendererNative::Get().DrawHeaderButton
1151 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1152 wxCONTROL_DIRTY
// mark as last column
1157 void wxListHeaderWindow::OnInternalIdle()
1159 wxWindow::OnInternalIdle();
1161 if (m_sendSetColumnWidth
)
1163 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1164 m_sendSetColumnWidth
= false;
1168 void wxListHeaderWindow::DrawCurrent()
1171 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1172 m_sendSetColumnWidth
= true;
1173 m_colToSend
= m_column
;
1174 m_widthToSend
= m_currentX
- m_minX
;
1176 int x1
= m_currentX
;
1178 m_owner
->ClientToScreen( &x1
, &y1
);
1180 int x2
= m_currentX
;
1182 m_owner
->GetClientSize( NULL
, &y2
);
1183 m_owner
->ClientToScreen( &x2
, &y2
);
1186 dc
.SetLogicalFunction( wxINVERT
);
1187 dc
.SetPen( wxPen(*wxBLACK
, 2) );
1188 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1192 dc
.DrawLine( x1
, y1
, x2
, y2
);
1194 dc
.SetLogicalFunction( wxCOPY
);
1196 dc
.SetPen( wxNullPen
);
1197 dc
.SetBrush( wxNullBrush
);
1201 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1203 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1205 // we want to work with logical coords
1207 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1208 int y
= event
.GetY();
1212 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1214 // we don't draw the line beyond our window, but we allow dragging it
1217 GetClientSize( &w
, NULL
);
1218 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1221 // erase the line if it was drawn
1222 if ( m_currentX
< w
)
1225 if (event
.ButtonUp())
1228 m_isDragging
= false;
1230 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1231 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1238 m_currentX
= m_minX
+ 7;
1240 // draw in the new location
1241 if ( m_currentX
< w
)
1245 else // not dragging
1248 bool hit_border
= false;
1250 // end of the current column
1253 // find the column where this event occurred
1255 countCol
= m_owner
->GetColumnCount();
1256 for (col
= 0; col
< countCol
; col
++)
1258 xpos
+= m_owner
->GetColumnWidth( col
);
1261 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1263 // near the column border
1270 // inside the column
1277 if ( col
== countCol
)
1280 if (event
.LeftDown() || event
.RightUp())
1282 if (hit_border
&& event
.LeftDown())
1284 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1285 event
.GetPosition()) )
1287 m_isDragging
= true;
1292 //else: column resizing was vetoed by the user code
1294 else // click on a column
1296 // record the selected state of the columns
1297 if (event
.LeftDown())
1299 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
1302 m_owner
->GetColumn(i
, colItem
);
1303 long state
= colItem
.GetState();
1305 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
1307 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
1308 m_owner
->SetColumn(i
, colItem
);
1312 SendListEvent( event
.LeftDown()
1313 ? wxEVT_COMMAND_LIST_COL_CLICK
1314 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1315 event
.GetPosition());
1318 else if (event
.Moving())
1323 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1324 m_currentCursor
= m_resizeCursor
;
1328 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1329 m_currentCursor
= wxSTANDARD_CURSOR
;
1333 SetCursor(*m_currentCursor
);
1338 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1340 m_owner
->SetFocus();
1344 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1346 wxWindow
*parent
= GetParent();
1347 wxListEvent
le( type
, parent
->GetId() );
1348 le
.SetEventObject( parent
);
1349 le
.m_pointDrag
= pos
;
1351 // the position should be relative to the parent window, not
1352 // this one for compatibility with MSW and common sense: the
1353 // user code doesn't know anything at all about this header
1354 // window, so why should it get positions relative to it?
1355 le
.m_pointDrag
.y
-= GetSize().y
;
1357 le
.m_col
= m_column
;
1358 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1361 //-----------------------------------------------------------------------------
1362 // wxListRenameTimer (internal)
1363 //-----------------------------------------------------------------------------
1365 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1370 void wxListRenameTimer::Notify()
1372 m_owner
->OnRenameTimer();
1375 //-----------------------------------------------------------------------------
1376 // wxListTextCtrlWrapper (internal)
1377 //-----------------------------------------------------------------------------
1379 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
1380 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
1381 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
1382 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
1385 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
1388 : m_startValue(owner
->GetItemText(itemEdit
)),
1389 m_itemEdited(itemEdit
)
1393 m_aboutToFinish
= false;
1395 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1397 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1399 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1400 &rectLabel
.x
, &rectLabel
.y
);
1402 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
1403 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1404 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
1407 m_text
->PushEventHandler(this);
1410 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
1412 m_aboutToFinish
= true;
1414 if ( discardChanges
)
1416 m_owner
->OnRenameCancelled(m_itemEdited
);
1422 // Notify the owner about the changes
1425 // Even if vetoed, close the control (consistent with MSW)
1430 void wxListTextCtrlWrapper::Finish( bool setfocus
)
1432 m_text
->RemoveEventHandler(this);
1433 m_owner
->ResetTextControl( m_text
);
1435 wxPendingDelete
.Append( this );
1438 m_owner
->SetFocus();
1441 bool wxListTextCtrlWrapper::AcceptChanges()
1443 const wxString value
= m_text
->GetValue();
1445 // notice that we should always call OnRenameAccept() to generate the "end
1446 // label editing" event, even if the user hasn't really changed anything
1447 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
1449 // vetoed by the user
1453 // accepted, do rename the item (unless nothing changed)
1454 if ( value
!= m_startValue
)
1455 m_owner
->SetItemText(m_itemEdited
, value
);
1460 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
1462 switch ( event
.m_keyCode
)
1477 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
1479 if (m_aboutToFinish
)
1481 // auto-grow the textctrl:
1482 wxSize parentSize
= m_owner
->GetSize();
1483 wxPoint myPos
= m_text
->GetPosition();
1484 wxSize mySize
= m_text
->GetSize();
1486 m_text
->GetTextExtent(m_text
->GetValue() + wxT("MM"), &sx
, &sy
);
1487 if (myPos
.x
+ sx
> parentSize
.x
)
1488 sx
= parentSize
.x
- myPos
.x
;
1491 m_text
->SetSize(sx
, wxDefaultCoord
);
1497 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
1499 if ( !m_aboutToFinish
)
1501 if ( !AcceptChanges() )
1502 m_owner
->OnRenameCancelled( m_itemEdited
);
1507 // We must let the native text control handle focus
1511 //-----------------------------------------------------------------------------
1513 //-----------------------------------------------------------------------------
1515 BEGIN_EVENT_TABLE(wxListMainWindow
, wxWindow
)
1516 EVT_PAINT (wxListMainWindow::OnPaint
)
1517 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1518 EVT_CHAR (wxListMainWindow::OnChar
)
1519 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1520 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
1521 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1522 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1523 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1524 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
1527 void wxListMainWindow::Init()
1532 m_lineTo
= (size_t)-1;
1538 m_small_image_list
= NULL
;
1539 m_normal_image_list
= NULL
;
1541 m_small_spacing
= 30;
1542 m_normal_spacing
= 40;
1546 m_isCreated
= false;
1548 m_lastOnSame
= false;
1549 m_renameTimer
= new wxListRenameTimer( this );
1550 m_textctrlWrapper
= NULL
;
1554 m_lineSelectSingleOnUp
=
1555 m_lineBeforeLastClicked
= (size_t)-1;
1558 wxListMainWindow::wxListMainWindow()
1563 m_highlightUnfocusedBrush
= NULL
;
1566 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1571 const wxString
&name
)
1572 : wxWindow( parent
, id
, pos
, size
, style
, name
)
1576 m_highlightBrush
= new wxBrush
1578 wxSystemSettings::GetColour
1580 wxSYS_COLOUR_HIGHLIGHT
1585 m_highlightUnfocusedBrush
= new wxBrush
1587 wxSystemSettings::GetColour
1589 wxSYS_COLOUR_BTNSHADOW
1594 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
1595 SetOwnForegroundColour( attr
.colFg
);
1596 SetOwnBackgroundColour( attr
.colBg
);
1598 SetOwnFont( attr
.font
);
1601 wxListMainWindow::~wxListMainWindow()
1604 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
1605 WX_CLEAR_ARRAY(m_aColWidths
);
1607 delete m_highlightBrush
;
1608 delete m_highlightUnfocusedBrush
;
1609 delete m_renameTimer
;
1612 void wxListMainWindow::SetReportView(bool inReportView
)
1614 const size_t count
= m_lines
.size();
1615 for ( size_t n
= 0; n
< count
; n
++ )
1617 m_lines
[n
].SetReportView(inReportView
);
1621 void wxListMainWindow::CacheLineData(size_t line
)
1623 wxGenericListCtrl
*listctrl
= GetListCtrl();
1625 wxListLineData
*ld
= GetDummyLine();
1627 size_t countCol
= GetColumnCount();
1628 for ( size_t col
= 0; col
< countCol
; col
++ )
1630 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1631 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1634 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1637 wxListLineData
*wxListMainWindow::GetDummyLine() const
1639 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1640 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1642 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1644 // we need to recreate the dummy line if the number of columns in the
1645 // control changed as it would have the incorrect number of fields
1647 if ( !m_lines
.IsEmpty() &&
1648 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1650 self
->m_lines
.Clear();
1653 if ( m_lines
.IsEmpty() )
1655 wxListLineData
*line
= new wxListLineData(self
);
1656 self
->m_lines
.Add(line
);
1658 // don't waste extra memory -- there never going to be anything
1659 // else/more in this array
1660 self
->m_lines
.Shrink();
1666 // ----------------------------------------------------------------------------
1667 // line geometry (report mode only)
1668 // ----------------------------------------------------------------------------
1670 wxCoord
wxListMainWindow::GetLineHeight() const
1672 // we cache the line height as calling GetTextExtent() is slow
1673 if ( !m_lineHeight
)
1675 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1677 wxClientDC
dc( self
);
1678 dc
.SetFont( GetFont() );
1681 dc
.GetTextExtent(wxT("H"), NULL
, &y
);
1683 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1686 m_small_image_list
->GetSize(0, iw
, ih
);
1691 self
->m_lineHeight
= y
+ LINE_SPACING
;
1694 return m_lineHeight
;
1697 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1699 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1701 return LINE_SPACING
+ line
* GetLineHeight();
1704 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1706 if ( !InReportView() )
1707 return GetLine(line
)->m_gi
->m_rectAll
;
1710 rect
.x
= HEADER_OFFSET_X
;
1711 rect
.y
= GetLineY(line
);
1712 rect
.width
= GetHeaderWidth();
1713 rect
.height
= GetLineHeight();
1718 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1720 if ( !InReportView() )
1721 return GetLine(line
)->m_gi
->m_rectLabel
;
1724 wxListLineData
*data
= GetLine(line
);
1725 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1728 wxListItemData
*item
= node
->GetData();
1729 if ( item
->HasImage() )
1732 GetImageSize( item
->GetImage(), ix
, iy
);
1733 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1738 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1739 rect
.y
= GetLineY(line
);
1740 rect
.width
= GetColumnWidth(0) - image_x
;
1741 rect
.height
= GetLineHeight();
1746 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1748 if ( !InReportView() )
1749 return GetLine(line
)->m_gi
->m_rectIcon
;
1751 wxListLineData
*ld
= GetLine(line
);
1752 wxASSERT_MSG( ld
->HasImage(), wxT("should have an image") );
1755 rect
.x
= HEADER_OFFSET_X
;
1756 rect
.y
= GetLineY(line
);
1757 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1762 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1764 return InReportView() ? GetLineRect(line
)
1765 : GetLine(line
)->m_gi
->m_rectHighlight
;
1768 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1770 wxASSERT_MSG( line
< GetItemCount(), wxT("invalid line in HitTestLine") );
1772 wxListLineData
*ld
= GetLine(line
);
1774 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1775 return wxLIST_HITTEST_ONITEMICON
;
1777 // VS: Testing for "ld->HasText() || InReportView()" instead of
1778 // "ld->HasText()" is needed to make empty lines in report view
1780 if ( ld
->HasText() || InReportView() )
1782 wxRect rect
= InReportView() ? GetLineRect(line
)
1783 : GetLineLabelRect(line
);
1785 if ( rect
.Contains(x
, y
) )
1786 return wxLIST_HITTEST_ONITEMLABEL
;
1792 // ----------------------------------------------------------------------------
1793 // highlight (selection) handling
1794 // ----------------------------------------------------------------------------
1796 bool wxListMainWindow::IsHighlighted(size_t line
) const
1800 return m_selStore
.IsSelected(line
);
1804 wxListLineData
*ld
= GetLine(line
);
1805 wxCHECK_MSG( ld
, false, wxT("invalid index in IsHighlighted") );
1807 return ld
->IsHighlighted();
1811 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1817 wxArrayInt linesChanged
;
1818 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1821 // meny items changed state, refresh everything
1822 RefreshLines(lineFrom
, lineTo
);
1824 else // only a few items changed state, refresh only them
1826 size_t count
= linesChanged
.GetCount();
1827 for ( size_t n
= 0; n
< count
; n
++ )
1829 RefreshLine(linesChanged
[n
]);
1833 else // iterate over all items in non report view
1835 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1837 if ( HighlightLine(line
, highlight
) )
1843 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1849 changed
= m_selStore
.SelectItem(line
, highlight
);
1853 wxListLineData
*ld
= GetLine(line
);
1854 wxCHECK_MSG( ld
, false, wxT("invalid index in HighlightLine") );
1856 changed
= ld
->Highlight(highlight
);
1861 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1862 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1868 void wxListMainWindow::RefreshLine( size_t line
)
1870 if ( InReportView() )
1872 size_t visibleFrom
, visibleTo
;
1873 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1875 if ( line
< visibleFrom
|| line
> visibleTo
)
1879 wxRect rect
= GetLineRect(line
);
1881 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1882 RefreshRect( rect
);
1885 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1887 // we suppose that they are ordered by caller
1888 wxASSERT_MSG( lineFrom
<= lineTo
, wxT("indices in disorder") );
1890 wxASSERT_MSG( lineTo
< GetItemCount(), wxT("invalid line range") );
1892 if ( InReportView() )
1894 size_t visibleFrom
, visibleTo
;
1895 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1897 if ( lineFrom
< visibleFrom
)
1898 lineFrom
= visibleFrom
;
1899 if ( lineTo
> visibleTo
)
1904 rect
.y
= GetLineY(lineFrom
);
1905 rect
.width
= GetClientSize().x
;
1906 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1908 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1909 RefreshRect( rect
);
1913 // TODO: this should be optimized...
1914 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1921 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1923 if ( InReportView() )
1925 size_t visibleFrom
, visibleTo
;
1926 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1928 if ( lineFrom
< visibleFrom
)
1929 lineFrom
= visibleFrom
;
1930 else if ( lineFrom
> visibleTo
)
1935 rect
.y
= GetLineY(lineFrom
);
1936 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1938 wxSize size
= GetClientSize();
1939 rect
.width
= size
.x
;
1941 // refresh till the bottom of the window
1942 rect
.height
= size
.y
- rect
.y
;
1944 RefreshRect( rect
);
1948 // TODO: how to do it more efficiently?
1953 void wxListMainWindow::RefreshSelected()
1959 if ( InReportView() )
1961 GetVisibleLinesRange(&from
, &to
);
1966 to
= GetItemCount() - 1;
1969 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
1970 RefreshLine(m_current
);
1972 for ( size_t line
= from
; line
<= to
; line
++ )
1974 // NB: the test works as expected even if m_current == -1
1975 if ( line
!= m_current
&& IsHighlighted(line
) )
1980 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1982 // Note: a wxPaintDC must be constructed even if no drawing is
1983 // done (a Windows requirement).
1984 wxPaintDC
dc( this );
1988 // nothing to draw or not the moment to draw it
1993 RecalculatePositions( false );
1995 GetListCtrl()->PrepareDC( dc
);
1998 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2000 dc
.SetFont( GetFont() );
2002 if ( InReportView() )
2004 int lineHeight
= GetLineHeight();
2006 size_t visibleFrom
, visibleTo
;
2007 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2010 int xOrig
= dc
.LogicalToDeviceX( 0 );
2011 int yOrig
= dc
.LogicalToDeviceY( 0 );
2013 // tell the caller cache to cache the data
2016 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2017 GetParent()->GetId());
2018 evCache
.SetEventObject( GetParent() );
2019 evCache
.m_oldItemIndex
= visibleFrom
;
2020 evCache
.m_itemIndex
= visibleTo
;
2021 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2024 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2026 rectLine
= GetLineRect(line
);
2029 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2030 rectLine
.width
, rectLine
.height
) )
2032 // don't redraw unaffected lines to avoid flicker
2036 GetLine(line
)->DrawInReportMode( &dc
,
2038 GetLineHighlightRect(line
),
2039 IsHighlighted(line
),
2040 line
== m_current
);
2043 if ( HasFlag(wxLC_HRULES
) )
2045 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2046 wxSize clientSize
= GetClientSize();
2048 size_t i
= visibleFrom
;
2049 if (i
== 0) i
= 1; // Don't draw the first one
2050 for ( ; i
<= visibleTo
; i
++ )
2053 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2054 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2055 clientSize
.x
- dev_x
, i
* lineHeight
);
2058 // Draw last horizontal rule
2059 if ( visibleTo
== GetItemCount() - 1 )
2062 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2063 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2064 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2068 // Draw vertical rules if required
2069 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2071 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2072 wxRect firstItemRect
, lastItemRect
;
2074 GetItemRect(visibleFrom
, firstItemRect
);
2075 GetItemRect(visibleTo
, lastItemRect
);
2076 int x
= firstItemRect
.GetX();
2078 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2080 for (int col
= 0; col
< GetColumnCount(); col
++)
2082 int colWidth
= GetColumnWidth(col
);
2084 int x_pos
= x
- dev_x
;
2085 if (col
< GetColumnCount()-1) x_pos
-= 2;
2086 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2087 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2093 size_t count
= GetItemCount();
2094 for ( size_t i
= 0; i
< count
; i
++ )
2096 GetLine(i
)->Draw( &dc
, i
== m_current
);
2100 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2101 // rectangle somehow and so leaves traces when the item is not selected any
2102 // more, see #12229.
2107 if ( IsHighlighted(m_current
) )
2108 flags
|= wxCONTROL_SELECTED
;
2110 wxRendererNative::Get().
2111 DrawFocusRect(this, dc
, GetLineHighlightRect(m_current
), flags
);
2113 #endif // !__WXMAC__
2116 void wxListMainWindow::HighlightAll( bool on
)
2118 if ( IsSingleSel() )
2120 wxASSERT_MSG( !on
, wxT("can't do this in a single selection control") );
2122 // we just have one item to turn off
2123 if ( HasCurrent() && IsHighlighted(m_current
) )
2125 HighlightLine(m_current
, false);
2126 RefreshLine(m_current
);
2129 else // multi selection
2132 HighlightLines(0, GetItemCount() - 1, on
);
2136 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2138 // Do nothing here. This prevents the default handler in wxScrolledWindow
2139 // from needlessly scrolling the window when the edit control is
2140 // dismissed. See ticket #9563.
2143 void wxListMainWindow::SendNotify( size_t line
,
2144 wxEventType command
,
2145 const wxPoint
& point
)
2147 wxListEvent
le( command
, GetParent()->GetId() );
2148 le
.SetEventObject( GetParent() );
2150 le
.m_itemIndex
= line
;
2152 // set only for events which have position
2153 if ( point
!= wxDefaultPosition
)
2154 le
.m_pointDrag
= point
;
2156 // don't try to get the line info for virtual list controls: the main
2157 // program has it anyhow and if we did it would result in accessing all
2158 // the lines, even those which are not visible now and this is precisely
2159 // what we're trying to avoid
2162 if ( line
!= (size_t)-1 )
2164 GetLine(line
)->GetItem( 0, le
.m_item
);
2166 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2168 //else: there may be no more such item
2170 GetParent()->GetEventHandler()->ProcessEvent( le
);
2173 void wxListMainWindow::ChangeCurrent(size_t current
)
2175 m_current
= current
;
2177 // as the current item changed, we shouldn't start editing it when the
2178 // "slow click" timer expires as the click happened on another item
2179 if ( m_renameTimer
->IsRunning() )
2180 m_renameTimer
->Stop();
2182 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2185 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2187 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2188 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2190 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2191 wxT("EditLabel() needs a text control") );
2193 size_t itemEdit
= (size_t)item
;
2195 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2196 le
.SetEventObject( GetParent() );
2197 le
.m_itemIndex
= item
;
2198 wxListLineData
*data
= GetLine(itemEdit
);
2199 wxCHECK_MSG( data
, NULL
, wxT("invalid index in EditLabel()") );
2200 data
->GetItem( 0, le
.m_item
);
2202 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2204 // vetoed by user code
2208 // We have to call this here because the label in question might just have
2209 // been added and no screen update taken place.
2212 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2213 // so that no pending events may change the item count (see below)
2214 // IMPORTANT: needs to be tested!
2217 // Pending events dispatched by wxSafeYield might have changed the item
2219 if ( (size_t)item
>= GetItemCount() )
2223 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2224 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2225 return m_textctrlWrapper
->GetText();
2228 void wxListMainWindow::OnRenameTimer()
2230 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2232 EditLabel( m_current
);
2235 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2237 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2238 le
.SetEventObject( GetParent() );
2239 le
.m_itemIndex
= itemEdit
;
2241 wxListLineData
*data
= GetLine(itemEdit
);
2243 wxCHECK_MSG( data
, false, wxT("invalid index in OnRenameAccept()") );
2245 data
->GetItem( 0, le
.m_item
);
2246 le
.m_item
.m_text
= value
;
2247 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2251 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2253 // let owner know that the edit was cancelled
2254 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2256 le
.SetEditCanceled(true);
2258 le
.SetEventObject( GetParent() );
2259 le
.m_itemIndex
= itemEdit
;
2261 wxListLineData
*data
= GetLine(itemEdit
);
2262 wxCHECK_RET( data
, wxT("invalid index in OnRenameCancelled()") );
2264 data
->GetItem( 0, le
.m_item
);
2265 GetEventHandler()->ProcessEvent( le
);
2268 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2271 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2272 // shutdown the edit control when the mouse is clicked elsewhere on the
2273 // listctrl because the order of events is different (or something like
2274 // that), so explicitly end the edit if it is active.
2275 if ( event
.LeftDown() && m_textctrlWrapper
)
2276 m_textctrlWrapper
->EndEdit( false );
2279 if ( event
.LeftDown() )
2282 event
.SetEventObject( GetParent() );
2283 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2286 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2288 // let the base class handle mouse wheel events.
2293 if ( !HasCurrent() || IsEmpty() )
2295 if (event
.RightDown())
2297 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2299 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2300 GetParent()->GetId(),
2301 ClientToScreen(event
.GetPosition()));
2302 evtCtx
.SetEventObject(GetParent());
2303 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2311 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2312 event
.ButtonDClick()) )
2315 int x
= event
.GetX();
2316 int y
= event
.GetY();
2317 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2319 // where did we hit it (if we did)?
2322 size_t count
= GetItemCount(),
2325 if ( InReportView() )
2327 current
= y
/ GetLineHeight();
2328 if ( current
< count
)
2329 hitResult
= HitTestLine(current
, x
, y
);
2333 // TODO: optimize it too! this is less simple than for report view but
2334 // enumerating all items is still not a way to do it!!
2335 for ( current
= 0; current
< count
; current
++ )
2337 hitResult
= HitTestLine(current
, x
, y
);
2343 if (event
.Dragging())
2345 if (m_dragCount
== 0)
2347 // we have to report the raw, physical coords as we want to be
2348 // able to call HitTest(event.m_pointDrag) from the user code to
2349 // get the item being dragged
2350 m_dragStart
= event
.GetPosition();
2355 if (m_dragCount
!= 3)
2358 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2359 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2361 wxListEvent
le( command
, GetParent()->GetId() );
2362 le
.SetEventObject( GetParent() );
2363 le
.m_itemIndex
= m_lineLastClicked
;
2364 le
.m_pointDrag
= m_dragStart
;
2365 GetParent()->GetEventHandler()->ProcessEvent( le
);
2376 // outside of any item
2377 if (event
.RightDown())
2379 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2381 wxContextMenuEvent
evtCtx(
2383 GetParent()->GetId(),
2384 ClientToScreen(event
.GetPosition()));
2385 evtCtx
.SetEventObject(GetParent());
2386 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2390 // reset the selection and bail out
2391 HighlightAll(false);
2397 bool forceClick
= false;
2398 if (event
.ButtonDClick())
2400 if ( m_renameTimer
->IsRunning() )
2401 m_renameTimer
->Stop();
2403 m_lastOnSame
= false;
2405 if ( current
== m_lineLastClicked
)
2407 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2413 // The first click was on another item, so don't interpret this as
2414 // a double click, but as a simple click instead
2421 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2423 // select single line
2424 HighlightAll( false );
2425 ReverseHighlight(m_lineSelectSingleOnUp
);
2430 if ((current
== m_current
) &&
2431 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2432 HasFlag(wxLC_EDIT_LABELS
) )
2434 if ( !InReportView() ||
2435 GetLineLabelRect(current
).Contains(x
, y
) )
2437 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2438 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2443 m_lastOnSame
= false;
2444 m_lineSelectSingleOnUp
= (size_t)-1;
2448 // This is necessary, because after a DnD operation in
2449 // from and to ourself, the up event is swallowed by the
2450 // DnD code. So on next non-up event (which means here and
2451 // now) m_lineSelectSingleOnUp should be reset.
2452 m_lineSelectSingleOnUp
= (size_t)-1;
2454 if (event
.RightDown())
2456 m_lineBeforeLastClicked
= m_lineLastClicked
;
2457 m_lineLastClicked
= current
;
2459 // If the item is already selected, do not update the selection.
2460 // Multi-selections should not be cleared if a selected item is clicked.
2461 if (!IsHighlighted(current
))
2463 HighlightAll(false);
2464 ChangeCurrent(current
);
2465 ReverseHighlight(m_current
);
2468 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2470 // Allow generation of context menu event
2473 else if (event
.MiddleDown())
2475 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2477 else if ( event
.LeftDown() || forceClick
)
2479 m_lineBeforeLastClicked
= m_lineLastClicked
;
2480 m_lineLastClicked
= current
;
2482 size_t oldCurrent
= m_current
;
2483 bool oldWasSelected
= IsHighlighted(m_current
);
2485 bool cmdModifierDown
= event
.CmdDown();
2486 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2488 if ( IsSingleSel() || !IsHighlighted(current
) )
2490 HighlightAll( false );
2492 ChangeCurrent(current
);
2494 ReverseHighlight(m_current
);
2496 else // multi sel & current is highlighted & no mod keys
2498 m_lineSelectSingleOnUp
= current
;
2499 ChangeCurrent(current
); // change focus
2502 else // multi sel & either ctrl or shift is down
2504 if (cmdModifierDown
)
2506 ChangeCurrent(current
);
2508 ReverseHighlight(m_current
);
2510 else if (event
.ShiftDown())
2512 ChangeCurrent(current
);
2514 size_t lineFrom
= oldCurrent
,
2517 if ( lineTo
< lineFrom
)
2520 lineFrom
= m_current
;
2523 HighlightLines(lineFrom
, lineTo
);
2525 else // !ctrl, !shift
2527 // test in the enclosing if should make it impossible
2528 wxFAIL_MSG( wxT("how did we get here?") );
2532 if (m_current
!= oldCurrent
)
2533 RefreshLine( oldCurrent
);
2535 // forceClick is only set if the previous click was on another item
2536 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2540 void wxListMainWindow::MoveToItem(size_t item
)
2542 if ( item
== (size_t)-1 )
2545 wxRect rect
= GetLineRect(item
);
2547 int client_w
, client_h
;
2548 GetClientSize( &client_w
, &client_h
);
2550 const int hLine
= GetLineHeight();
2552 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2553 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2555 if ( InReportView() )
2557 // the next we need the range of lines shown it might be different,
2558 // so recalculate it
2559 ResetVisibleLinesRange();
2561 if (rect
.y
< view_y
)
2562 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2563 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2564 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2567 // At least on Mac the visible lines value will get reset inside of
2568 // Scroll *before* it actually scrolls the window because of the
2569 // Update() that happens there, so it will still have the wrong value.
2570 // So let's reset it again and wait for it to be recalculated in the
2571 // next paint event. I would expect this problem to show up in wxGTK
2572 // too but couldn't duplicate it there. Perhaps the order of events
2573 // is different... --Robin
2574 ResetVisibleLinesRange();
2582 if (rect
.x
-view_x
< 5)
2583 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2584 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2585 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2587 if (rect
.y
-view_y
< 5)
2588 sy
= (rect
.y
- 5) / hLine
;
2589 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2590 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2592 GetListCtrl()->Scroll(sx
, sy
);
2596 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2598 if ( !InReportView() )
2600 // TODO: this should work in all views but is not implemented now
2605 GetVisibleLinesRange(&top
, &bottom
);
2607 if ( bottom
== (size_t)-1 )
2610 ResetVisibleLinesRange();
2612 int hLine
= GetLineHeight();
2614 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2617 // see comment in MoveToItem() for why we do this
2618 ResetVisibleLinesRange();
2624 // ----------------------------------------------------------------------------
2625 // keyboard handling
2626 // ----------------------------------------------------------------------------
2628 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2630 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2631 wxT("invalid item index in OnArrowChar()") );
2633 size_t oldCurrent
= m_current
;
2635 // in single selection we just ignore Shift as we can't select several
2637 if ( event
.ShiftDown() && !IsSingleSel() )
2639 ChangeCurrent(newCurrent
);
2641 // refresh the old focus to remove it
2642 RefreshLine( oldCurrent
);
2644 // select all the items between the old and the new one
2645 if ( oldCurrent
> newCurrent
)
2647 newCurrent
= oldCurrent
;
2648 oldCurrent
= m_current
;
2651 HighlightLines(oldCurrent
, newCurrent
);
2655 // all previously selected items are unselected unless ctrl is held
2656 // in a multiselection control
2657 if ( !event
.ControlDown() || IsSingleSel() )
2658 HighlightAll(false);
2660 ChangeCurrent(newCurrent
);
2662 // refresh the old focus to remove it
2663 RefreshLine( oldCurrent
);
2665 // in single selection mode we must always have a selected item
2666 if ( !event
.ControlDown() || IsSingleSel() )
2667 HighlightLine( m_current
, true );
2670 RefreshLine( m_current
);
2675 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2677 wxWindow
*parent
= GetParent();
2679 // propagate the key event upwards
2680 wxKeyEvent
ke(event
);
2681 ke
.SetEventObject( parent
);
2682 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2688 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2690 wxWindow
*parent
= GetParent();
2692 // propagate the key event upwards
2693 wxKeyEvent
ke(event
);
2694 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2700 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2702 wxWindow
*parent
= GetParent();
2704 // send a list_key event up
2707 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2708 le
.m_itemIndex
= m_current
;
2709 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2710 le
.m_code
= event
.GetKeyCode();
2711 le
.SetEventObject( parent
);
2712 parent
->GetEventHandler()->ProcessEvent( le
);
2715 // propagate the char event upwards
2716 wxKeyEvent
ke(event
);
2717 ke
.SetEventObject( parent
);
2718 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2721 if ( HandleAsNavigationKey(event
) )
2724 // no item -> nothing to do
2731 // don't use m_linesPerPage directly as it might not be computed yet
2732 const int pageSize
= GetCountPerPage();
2733 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
2735 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2737 if (event
.GetKeyCode() == WXK_RIGHT
)
2738 event
.m_keyCode
= WXK_LEFT
;
2739 else if (event
.GetKeyCode() == WXK_LEFT
)
2740 event
.m_keyCode
= WXK_RIGHT
;
2743 switch ( event
.GetKeyCode() )
2746 if ( m_current
> 0 )
2747 OnArrowChar( m_current
- 1, event
);
2751 if ( m_current
< (size_t)GetItemCount() - 1 )
2752 OnArrowChar( m_current
+ 1, event
);
2757 OnArrowChar( GetItemCount() - 1, event
);
2762 OnArrowChar( 0, event
);
2767 int steps
= InReportView() ? pageSize
- 1
2768 : m_current
% pageSize
;
2770 int index
= m_current
- steps
;
2774 OnArrowChar( index
, event
);
2780 int steps
= InReportView()
2782 : pageSize
- (m_current
% pageSize
) - 1;
2784 size_t index
= m_current
+ steps
;
2785 size_t count
= GetItemCount();
2786 if ( index
>= count
)
2789 OnArrowChar( index
, event
);
2794 if ( !InReportView() )
2796 int index
= m_current
- pageSize
;
2800 OnArrowChar( index
, event
);
2805 if ( !InReportView() )
2807 size_t index
= m_current
+ pageSize
;
2809 size_t count
= GetItemCount();
2810 if ( index
>= count
)
2813 OnArrowChar( index
, event
);
2818 if ( IsSingleSel() )
2820 if ( event
.ControlDown() )
2822 ReverseHighlight(m_current
);
2824 else // normal space press
2826 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2829 else // multiple selection
2831 ReverseHighlight(m_current
);
2837 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2845 // ----------------------------------------------------------------------------
2847 // ----------------------------------------------------------------------------
2849 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2853 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2854 event
.SetEventObject( GetParent() );
2855 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2859 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2860 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2861 // which are already drawn correctly resulting in horrible flicker - avoid
2871 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2875 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2876 event
.SetEventObject( GetParent() );
2877 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2885 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2887 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2889 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2891 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2893 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2895 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2897 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2899 else if ( InReportView() && (m_small_image_list
))
2901 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2905 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2907 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2909 m_normal_image_list
->GetSize( index
, width
, height
);
2911 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2913 m_small_image_list
->GetSize( index
, width
, height
);
2915 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2917 m_small_image_list
->GetSize( index
, width
, height
);
2919 else if ( InReportView() && m_small_image_list
)
2921 m_small_image_list
->GetSize( index
, width
, height
);
2930 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2932 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2933 dc
.SetFont( GetFont() );
2936 dc
.GetTextExtent( s
, &lw
, NULL
);
2938 return lw
+ AUTOSIZE_COL_MARGIN
;
2941 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2945 // calc the spacing from the icon size
2946 int width
= 0, height
= 0;
2948 if ((imageList
) && (imageList
->GetImageCount()) )
2949 imageList
->GetSize(0, width
, height
);
2951 if (which
== wxIMAGE_LIST_NORMAL
)
2953 m_normal_image_list
= imageList
;
2954 m_normal_spacing
= width
+ 8;
2957 if (which
== wxIMAGE_LIST_SMALL
)
2959 m_small_image_list
= imageList
;
2960 m_small_spacing
= width
+ 14;
2961 m_lineHeight
= 0; // ensure that the line height will be recalc'd
2965 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
2969 m_small_spacing
= spacing
;
2971 m_normal_spacing
= spacing
;
2974 int wxListMainWindow::GetItemSpacing( bool isSmall
)
2976 return isSmall
? m_small_spacing
: m_normal_spacing
;
2979 // ----------------------------------------------------------------------------
2981 // ----------------------------------------------------------------------------
2983 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
2985 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
2987 wxCHECK_RET( node
, wxT("invalid column index in SetColumn") );
2989 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2990 item
.m_width
= GetTextLength( item
.m_text
);
2992 wxListHeaderData
*column
= node
->GetData();
2993 column
->SetItem( item
);
2995 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
2997 headerWin
->m_dirty
= true;
3001 // invalidate it as it has to be recalculated
3005 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3007 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3008 wxT("invalid column index") );
3010 wxCHECK_RET( InReportView(),
3011 wxT("SetColumnWidth() can only be called in report mode.") );
3015 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3017 headerWin
->m_dirty
= true;
3019 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3020 wxCHECK_RET( node
, wxT("no column?") );
3022 wxListHeaderData
*column
= node
->GetData();
3024 size_t count
= GetItemCount();
3026 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3028 width
= GetTextLength(column
->GetText());
3029 width
+= 2*EXTRA_WIDTH
;
3031 // check for column header's image availability
3032 const int image
= column
->GetImage();
3035 if ( m_small_image_list
)
3038 m_small_image_list
->GetSize(image
, ix
, iy
);
3039 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3043 else if ( width
== wxLIST_AUTOSIZE
)
3047 // TODO: determine the max width somehow...
3048 width
= WIDTH_COL_DEFAULT
;
3052 wxClientDC
dc(this);
3053 dc
.SetFont( GetFont() );
3055 int max
= AUTOSIZE_COL_MARGIN
;
3057 // if the cached column width isn't valid then recalculate it
3058 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3060 for (size_t i
= 0; i
< count
; i
++)
3062 wxListLineData
*line
= GetLine( i
);
3063 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3065 wxCHECK_RET( n
, wxT("no subitem?") );
3067 wxListItemData
*itemData
= n
->GetData();
3070 itemData
->GetItem(item
);
3071 int itemWidth
= GetItemWidthWithImage(&item
);
3072 if (itemWidth
> max
)
3076 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3077 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3080 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3081 width
= max
+ AUTOSIZE_COL_MARGIN
;
3085 column
->SetWidth( width
);
3087 // invalidate it as it has to be recalculated
3091 int wxListMainWindow::GetHeaderWidth() const
3093 if ( !m_headerWidth
)
3095 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3097 size_t count
= GetColumnCount();
3098 for ( size_t col
= 0; col
< count
; col
++ )
3100 self
->m_headerWidth
+= GetColumnWidth(col
);
3104 return m_headerWidth
;
3107 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3109 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3110 wxCHECK_RET( node
, wxT("invalid column index in GetColumn") );
3112 wxListHeaderData
*column
= node
->GetData();
3113 column
->GetItem( item
);
3116 int wxListMainWindow::GetColumnWidth( int col
) const
3118 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3119 wxCHECK_MSG( node
, 0, wxT("invalid column index") );
3121 wxListHeaderData
*column
= node
->GetData();
3122 return column
->GetWidth();
3125 // ----------------------------------------------------------------------------
3127 // ----------------------------------------------------------------------------
3129 void wxListMainWindow::SetItem( wxListItem
&item
)
3131 long id
= item
.m_itemId
;
3132 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3133 wxT("invalid item index in SetItem") );
3137 wxListLineData
*line
= GetLine((size_t)id
);
3138 line
->SetItem( item
.m_col
, item
);
3140 // Set item state if user wants
3141 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3142 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3146 // update the Max Width Cache if needed
3147 int width
= GetItemWidthWithImage(&item
);
3149 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3150 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3154 // update the item on screen
3156 GetItemRect(id
, rectItem
);
3157 RefreshRect(rectItem
);
3160 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3165 // first deal with selection
3166 if ( stateMask
& wxLIST_STATE_SELECTED
)
3168 // set/clear select state
3171 // optimized version for virtual listctrl.
3172 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3175 else if ( state
& wxLIST_STATE_SELECTED
)
3177 const long count
= GetItemCount();
3178 for( long i
= 0; i
< count
; i
++ )
3180 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3186 // clear for non virtual (somewhat optimized by using GetNextItem())
3188 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3190 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3195 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3197 // unfocus all: only one item can be focussed, so clearing focus for
3198 // all items is simply clearing focus of the focussed item.
3199 SetItemState(m_current
, state
, stateMask
);
3201 //(setting focus to all items makes no sense, so it is not handled here.)
3204 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3208 SetItemStateAll(state
, stateMask
);
3212 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3213 wxT("invalid list ctrl item index in SetItem") );
3215 size_t oldCurrent
= m_current
;
3216 size_t item
= (size_t)litem
; // safe because of the check above
3218 // do we need to change the focus?
3219 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3221 if ( state
& wxLIST_STATE_FOCUSED
)
3223 // don't do anything if this item is already focused
3224 if ( item
!= m_current
)
3226 ChangeCurrent(item
);
3228 if ( oldCurrent
!= (size_t)-1 )
3230 if ( IsSingleSel() )
3232 HighlightLine(oldCurrent
, false);
3235 RefreshLine(oldCurrent
);
3238 RefreshLine( m_current
);
3243 // don't do anything if this item is not focused
3244 if ( item
== m_current
)
3248 if ( IsSingleSel() )
3250 // we must unselect the old current item as well or we
3251 // might end up with more than one selected item in a
3252 // single selection control
3253 HighlightLine(oldCurrent
, false);
3256 RefreshLine( oldCurrent
);
3261 // do we need to change the selection state?
3262 if ( stateMask
& wxLIST_STATE_SELECTED
)
3264 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3266 if ( IsSingleSel() )
3270 // selecting the item also makes it the focused one in the
3272 if ( m_current
!= item
)
3274 ChangeCurrent(item
);
3276 if ( oldCurrent
!= (size_t)-1 )
3278 HighlightLine( oldCurrent
, false );
3279 RefreshLine( oldCurrent
);
3285 // only the current item may be selected anyhow
3286 if ( item
!= m_current
)
3291 if ( HighlightLine(item
, on
) )
3298 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3300 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3301 wxT("invalid list ctrl item index in GetItemState()") );
3303 int ret
= wxLIST_STATE_DONTCARE
;
3305 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3307 if ( (size_t)item
== m_current
)
3308 ret
|= wxLIST_STATE_FOCUSED
;
3311 if ( stateMask
& wxLIST_STATE_SELECTED
)
3313 if ( IsHighlighted(item
) )
3314 ret
|= wxLIST_STATE_SELECTED
;
3320 void wxListMainWindow::GetItem( wxListItem
&item
) const
3322 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3323 wxT("invalid item index in GetItem") );
3325 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3326 line
->GetItem( item
.m_col
, item
);
3328 // Get item state if user wants it
3329 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3330 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3331 wxLIST_STATE_FOCUSED
);
3334 // ----------------------------------------------------------------------------
3336 // ----------------------------------------------------------------------------
3338 size_t wxListMainWindow::GetItemCount() const
3340 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3343 void wxListMainWindow::SetItemCount(long count
)
3345 m_selStore
.SetItemCount(count
);
3346 m_countVirt
= count
;
3348 ResetVisibleLinesRange();
3350 // scrollbars must be reset
3354 int wxListMainWindow::GetSelectedItemCount() const
3356 // deal with the quick case first
3357 if ( IsSingleSel() )
3358 return HasCurrent() ? IsHighlighted(m_current
) : false;
3360 // virtual controls remmebers all its selections itself
3362 return m_selStore
.GetSelectedCount();
3364 // TODO: we probably should maintain the number of items selected even for
3365 // non virtual controls as enumerating all lines is really slow...
3366 size_t countSel
= 0;
3367 size_t count
= GetItemCount();
3368 for ( size_t line
= 0; line
< count
; line
++ )
3370 if ( GetLine(line
)->IsHighlighted() )
3377 // ----------------------------------------------------------------------------
3378 // item position/size
3379 // ----------------------------------------------------------------------------
3381 wxRect
wxListMainWindow::GetViewRect() const
3383 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3385 // we need to find the longest/tallest label
3386 wxCoord xMax
= 0, yMax
= 0;
3387 const int count
= GetItemCount();
3390 for ( int i
= 0; i
< count
; i
++ )
3392 // we need logical, not physical, coordinates here, so use
3393 // GetLineRect() instead of GetItemRect()
3394 wxRect r
= GetLineRect(i
);
3396 wxCoord x
= r
.GetRight(),
3406 // some fudge needed to make it look prettier
3407 xMax
+= 2 * EXTRA_BORDER_X
;
3408 yMax
+= 2 * EXTRA_BORDER_Y
;
3410 // account for the scrollbars if necessary
3411 const wxSize sizeAll
= GetClientSize();
3412 if ( xMax
> sizeAll
.x
)
3413 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3414 if ( yMax
> sizeAll
.y
)
3415 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3417 return wxRect(0, 0, xMax
, yMax
);
3421 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3423 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3425 wxT("GetSubItemRect only meaningful in report view") );
3426 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3427 wxT("invalid item in GetSubItemRect") );
3429 // ensure that we're laid out, otherwise we could return nonsense
3432 wxConstCast(this, wxListMainWindow
)->
3433 RecalculatePositions(true /* no refresh */);
3436 rect
= GetLineRect((size_t)item
);
3438 // Adjust rect to specified column
3439 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3441 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3442 wxT("invalid subItem in GetSubItemRect") );
3444 for (int i
= 0; i
< subItem
; i
++)
3446 rect
.x
+= GetColumnWidth(i
);
3448 rect
.width
= GetColumnWidth(subItem
);
3451 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3456 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3459 GetItemRect(item
, rect
);
3467 // ----------------------------------------------------------------------------
3468 // geometry calculation
3469 // ----------------------------------------------------------------------------
3471 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3473 const int lineHeight
= GetLineHeight();
3475 wxClientDC
dc( this );
3476 dc
.SetFont( GetFont() );
3478 const size_t count
= GetItemCount();
3481 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3482 iconSpacing
= m_normal_spacing
;
3483 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3484 iconSpacing
= m_small_spacing
;
3488 // Note that we do not call GetClientSize() here but
3489 // GetSize() and subtract the border size for sunken
3490 // borders manually. This is technically incorrect,
3491 // but we need to know the client area's size WITHOUT
3492 // scrollbars here. Since we don't know if there are
3493 // any scrollbars, we use GetSize() instead. Another
3494 // solution would be to call SetScrollbars() here to
3495 // remove the scrollbars and call GetClientSize() then,
3496 // but this might result in flicker and - worse - will
3497 // reset the scrollbars to 0 which is not good at all
3498 // if you resize a dialog/window, but don't want to
3499 // reset the window scrolling. RR.
3500 // Furthermore, we actually do NOT subtract the border
3501 // width as 2 pixels is just the extra space which we
3502 // need around the actual content in the window. Other-
3503 // wise the text would e.g. touch the upper border. RR.
3506 GetSize( &clientWidth
, &clientHeight
);
3508 if ( InReportView() )
3510 // all lines have the same height and we scroll one line per step
3511 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3513 m_linesPerPage
= clientHeight
/ lineHeight
;
3515 ResetVisibleLinesRange();
3517 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3518 GetHeaderWidth() / SCROLL_UNIT_X
,
3519 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3520 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3521 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3526 // we have 3 different layout strategies: either layout all items
3527 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3528 // to arrange them in top to bottom, left to right (don't ask me why
3529 // not the other way round...) order
3530 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3532 int x
= EXTRA_BORDER_X
;
3533 int y
= EXTRA_BORDER_Y
;
3535 wxCoord widthMax
= 0;
3538 for ( i
= 0; i
< count
; i
++ )
3540 wxListLineData
*line
= GetLine(i
);
3541 line
->CalculateSize( &dc
, iconSpacing
);
3542 line
->SetPosition( x
, y
, iconSpacing
);
3544 wxSize sizeLine
= GetLineSize(i
);
3546 if ( HasFlag(wxLC_ALIGN_TOP
) )
3548 if ( sizeLine
.x
> widthMax
)
3549 widthMax
= sizeLine
.x
;
3553 else // wxLC_ALIGN_LEFT
3555 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3559 if ( HasFlag(wxLC_ALIGN_TOP
) )
3561 // traverse the items again and tweak their sizes so that they are
3562 // all the same in a row
3563 for ( i
= 0; i
< count
; i
++ )
3565 wxListLineData
*line
= GetLine(i
);
3566 line
->m_gi
->ExtendWidth(widthMax
);
3570 GetListCtrl()->SetScrollbars
3574 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3575 (y
+ lineHeight
) / lineHeight
,
3576 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3577 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3581 else // "flowed" arrangement, the most complicated case
3583 // at first we try without any scrollbars, if the items don't fit into
3584 // the window, we recalculate after subtracting the space taken by the
3587 int entireWidth
= 0;
3589 for (int tries
= 0; tries
< 2; tries
++)
3591 entireWidth
= 2 * EXTRA_BORDER_X
;
3595 // Now we have decided that the items do not fit into the
3596 // client area, so we need a scrollbar
3597 entireWidth
+= SCROLL_UNIT_X
;
3600 int x
= EXTRA_BORDER_X
;
3601 int y
= EXTRA_BORDER_Y
;
3602 int maxWidthInThisRow
= 0;
3605 int currentlyVisibleLines
= 0;
3607 for (size_t i
= 0; i
< count
; i
++)
3609 currentlyVisibleLines
++;
3610 wxListLineData
*line
= GetLine( i
);
3611 line
->CalculateSize( &dc
, iconSpacing
);
3612 line
->SetPosition( x
, y
, iconSpacing
);
3614 wxSize sizeLine
= GetLineSize( i
);
3616 if ( maxWidthInThisRow
< sizeLine
.x
)
3617 maxWidthInThisRow
= sizeLine
.x
;
3620 if (currentlyVisibleLines
> m_linesPerPage
)
3621 m_linesPerPage
= currentlyVisibleLines
;
3623 if ( y
+ sizeLine
.y
>= clientHeight
)
3625 currentlyVisibleLines
= 0;
3627 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3628 x
+= maxWidthInThisRow
;
3629 entireWidth
+= maxWidthInThisRow
;
3630 maxWidthInThisRow
= 0;
3633 // We have reached the last item.
3634 if ( i
== count
- 1 )
3635 entireWidth
+= maxWidthInThisRow
;
3637 if ( (tries
== 0) &&
3638 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3640 clientHeight
-= wxSystemSettings::
3641 GetMetric(wxSYS_HSCROLL_Y
);
3646 if ( i
== count
- 1 )
3647 tries
= 1; // Everything fits, no second try required.
3651 GetListCtrl()->SetScrollbars
3655 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3657 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3666 // FIXME: why should we call it from here?
3673 void wxListMainWindow::RefreshAll()
3678 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3679 if ( headerWin
&& headerWin
->m_dirty
)
3681 headerWin
->m_dirty
= false;
3682 headerWin
->Refresh();
3686 void wxListMainWindow::UpdateCurrent()
3688 if ( !HasCurrent() && !IsEmpty() )
3692 long wxListMainWindow::GetNextItem( long item
,
3693 int WXUNUSED(geometry
),
3697 max
= GetItemCount();
3698 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3699 wxT("invalid listctrl index in GetNextItem()") );
3701 // notice that we start with the next item (or the first one if item == -1)
3702 // and this is intentional to allow writing a simple loop to iterate over
3703 // all selected items
3706 // this is not an error because the index was OK initially,
3707 // just no such item
3714 size_t count
= GetItemCount();
3715 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3717 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3720 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3727 // ----------------------------------------------------------------------------
3729 // ----------------------------------------------------------------------------
3731 void wxListMainWindow::DeleteItem( long lindex
)
3733 size_t count
= GetItemCount();
3735 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3736 wxT("invalid item index in DeleteItem") );
3738 size_t index
= (size_t)lindex
;
3740 // we don't need to adjust the index for the previous items
3741 if ( HasCurrent() && m_current
>= index
)
3743 // if the current item is being deleted, we want the next one to
3744 // become selected - unless there is no next one - so don't adjust
3745 // m_current in this case
3746 if ( m_current
!= index
|| m_current
== count
- 1 )
3750 if ( InReportView() )
3752 // mark the Column Max Width cache as dirty if the items in the line
3753 // we're deleting contain the Max Column Width
3754 wxListLineData
* const line
= GetLine(index
);
3755 wxListItemDataList::compatibility_iterator n
;
3756 wxListItemData
*itemData
;
3760 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3762 n
= line
->m_items
.Item( i
);
3763 itemData
= n
->GetData();
3764 itemData
->GetItem(item
);
3766 itemWidth
= GetItemWidthWithImage(&item
);
3768 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3769 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3772 ResetVisibleLinesRange();
3775 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3780 m_selStore
.OnItemDelete(index
);
3784 m_lines
.RemoveAt( index
);
3787 // we need to refresh the (vert) scrollbar as the number of items changed
3790 RefreshAfter(index
);
3793 void wxListMainWindow::DeleteColumn( int col
)
3795 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3797 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3800 delete node
->GetData();
3801 m_columns
.Erase( node
);
3805 // update all the items
3806 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3808 wxListLineData
* const line
= GetLine(i
);
3809 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3810 delete n
->GetData();
3811 line
->m_items
.Erase(n
);
3815 if ( InReportView() ) // we only cache max widths when in Report View
3817 delete m_aColWidths
.Item(col
);
3818 m_aColWidths
.RemoveAt(col
);
3821 // invalidate it as it has to be recalculated
3825 void wxListMainWindow::DoDeleteAllItems()
3828 // nothing to do - in particular, don't send the event
3833 // to make the deletion of all items faster, we don't send the
3834 // notifications for each item deletion in this case but only one event
3835 // for all of them: this is compatible with wxMSW and documented in
3836 // DeleteAllItems() description
3838 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3839 event
.SetEventObject( GetParent() );
3840 GetParent()->GetEventHandler()->ProcessEvent( event
);
3848 if ( InReportView() )
3850 ResetVisibleLinesRange();
3851 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3853 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3860 void wxListMainWindow::DeleteAllItems()
3864 RecalculatePositions();
3867 void wxListMainWindow::DeleteEverything()
3869 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3870 WX_CLEAR_ARRAY(m_aColWidths
);
3875 // ----------------------------------------------------------------------------
3876 // scanning for an item
3877 // ----------------------------------------------------------------------------
3879 void wxListMainWindow::EnsureVisible( long index
)
3881 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3882 wxT("invalid index in EnsureVisible") );
3884 // We have to call this here because the label in question might just have
3885 // been added and its position is not known yet
3887 RecalculatePositions(true /* no refresh */);
3889 MoveToItem((size_t)index
);
3892 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3898 wxString str_upper
= str
.Upper();
3902 size_t count
= GetItemCount();
3903 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3905 wxListLineData
*line
= GetLine(i
);
3906 wxString line_upper
= line
->GetText(0).Upper();
3909 if (line_upper
== str_upper
)
3914 if (line_upper
.find(str_upper
) == 0)
3922 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3928 size_t count
= GetItemCount();
3929 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3931 wxListLineData
*line
= GetLine(i
);
3933 line
->GetItem( 0, item
);
3934 if (item
.m_data
== data
)
3941 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3944 GetVisibleLinesRange( &topItem
, NULL
);
3947 GetItemPosition( GetItemCount() - 1, p
);
3951 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3952 if ( id
>= 0 && id
< (long)GetItemCount() )
3958 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
3960 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3962 size_t count
= GetItemCount();
3964 if ( InReportView() )
3966 size_t current
= y
/ GetLineHeight();
3967 if ( current
< count
)
3969 flags
= HitTestLine(current
, x
, y
);
3976 // TODO: optimize it too! this is less simple than for report view but
3977 // enumerating all items is still not a way to do it!!
3978 for ( size_t current
= 0; current
< count
; current
++ )
3980 flags
= HitTestLine(current
, x
, y
);
3989 // ----------------------------------------------------------------------------
3991 // ----------------------------------------------------------------------------
3993 void wxListMainWindow::InsertItem( wxListItem
&item
)
3995 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
3997 int count
= GetItemCount();
3998 wxCHECK_RET( item
.m_itemId
>= 0, wxT("invalid item index") );
4000 if (item
.m_itemId
> count
)
4001 item
.m_itemId
= count
;
4003 size_t id
= item
.m_itemId
;
4007 if ( InReportView() )
4009 ResetVisibleLinesRange();
4011 const unsigned col
= item
.GetColumn();
4012 wxCHECK_RET( col
< m_aColWidths
.size(), "invalid item column" );
4014 // calculate the width of the item and adjust the max column width
4015 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(col
);
4016 int width
= GetItemWidthWithImage(&item
);
4017 item
.SetWidth(width
);
4018 if (width
> pWidthInfo
->nMaxWidth
)
4019 pWidthInfo
->nMaxWidth
= width
;
4022 wxListLineData
*line
= new wxListLineData(this);
4024 line
->SetItem( item
.m_col
, item
);
4026 m_lines
.Insert( line
, id
);
4030 // If an item is selected at or below the point of insertion, we need to
4031 // increment the member variables because the current row's index has gone
4033 if ( HasCurrent() && m_current
>= id
)
4036 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4038 RefreshLines(id
, GetItemCount() - 1);
4041 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4044 if ( InReportView() )
4046 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4047 item
.m_width
= GetTextLength( item
.m_text
);
4049 wxListHeaderData
*column
= new wxListHeaderData( item
);
4050 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4052 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4055 wxListHeaderDataList::compatibility_iterator
4056 node
= m_columns
.Item( col
);
4057 m_columns
.Insert( node
, column
);
4058 m_aColWidths
.Insert( colWidthInfo
, col
);
4062 m_columns
.Append( column
);
4063 m_aColWidths
.Add( colWidthInfo
);
4068 // update all the items
4069 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4071 wxListLineData
* const line
= GetLine(i
);
4072 wxListItemData
* const data
= new wxListItemData(this);
4074 line
->m_items
.Insert(col
, data
);
4076 line
->m_items
.Append(data
);
4080 // invalidate it as it has to be recalculated
4085 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4088 wxClientDC
dc(this);
4090 dc
.SetFont( GetFont() );
4092 if (item
->GetImage() != -1)
4095 GetImageSize( item
->GetImage(), ix
, iy
);
4099 if (!item
->GetText().empty())
4102 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4109 // ----------------------------------------------------------------------------
4111 // ----------------------------------------------------------------------------
4113 static wxListCtrlCompare list_ctrl_compare_func_2
;
4114 static wxIntPtr list_ctrl_compare_data
;
4116 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4118 wxListLineData
*line1
= *arg1
;
4119 wxListLineData
*line2
= *arg2
;
4121 line1
->GetItem( 0, item
);
4122 wxUIntPtr data1
= item
.m_data
;
4123 line2
->GetItem( 0, item
);
4124 wxUIntPtr data2
= item
.m_data
;
4125 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4128 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4130 // selections won't make sense any more after sorting the items so reset
4132 HighlightAll(false);
4135 list_ctrl_compare_func_2
= fn
;
4136 list_ctrl_compare_data
= data
;
4137 m_lines
.Sort( list_ctrl_compare_func_1
);
4141 // ----------------------------------------------------------------------------
4143 // ----------------------------------------------------------------------------
4145 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4147 // update our idea of which lines are shown when we redraw the window the
4149 ResetVisibleLinesRange();
4151 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4153 wxGenericListCtrl
* lc
= GetListCtrl();
4154 wxCHECK_RET( lc
, wxT("no listctrl window?") );
4156 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4158 lc
->m_headerWin
->Refresh();
4159 lc
->m_headerWin
->Update();
4164 int wxListMainWindow::GetCountPerPage() const
4166 if ( !m_linesPerPage
)
4168 wxConstCast(this, wxListMainWindow
)->
4169 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4172 return m_linesPerPage
;
4175 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4177 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4179 if ( m_lineFrom
== (size_t)-1 )
4181 size_t count
= GetItemCount();
4184 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4186 // this may happen if SetScrollbars() hadn't been called yet
4187 if ( m_lineFrom
>= count
)
4188 m_lineFrom
= count
- 1;
4190 // we redraw one extra line but this is needed to make the redrawing
4191 // logic work when there is a fractional number of lines on screen
4192 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4193 if ( m_lineTo
>= count
)
4194 m_lineTo
= count
- 1;
4196 else // empty control
4199 m_lineTo
= (size_t)-1;
4203 wxASSERT_MSG( IsEmpty() ||
4204 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4205 wxT("GetVisibleLinesRange() returns incorrect result") );
4213 // -------------------------------------------------------------------------------------
4214 // wxGenericListCtrl
4215 // -------------------------------------------------------------------------------------
4217 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4219 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4220 EVT_SIZE(wxGenericListCtrl::OnSize
)
4221 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4224 void wxGenericListCtrl::Init()
4226 m_imageListNormal
= NULL
;
4227 m_imageListSmall
= NULL
;
4228 m_imageListState
= NULL
;
4230 m_ownsImageListNormal
=
4231 m_ownsImageListSmall
=
4232 m_ownsImageListState
= false;
4238 wxGenericListCtrl::~wxGenericListCtrl()
4240 if (m_ownsImageListNormal
)
4241 delete m_imageListNormal
;
4242 if (m_ownsImageListSmall
)
4243 delete m_imageListSmall
;
4244 if (m_ownsImageListState
)
4245 delete m_imageListState
;
4248 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4250 bool needs_header
= HasHeader();
4251 bool has_header
= (m_headerWin
!= NULL
);
4253 if (needs_header
== has_header
)
4258 m_headerWin
= new wxListHeaderWindow
4260 this, wxID_ANY
, m_mainWin
,
4265 wxRendererNative::Get().GetHeaderButtonHeight(this)
4270 #if defined( __WXMAC__ )
4271 static wxFont
font( wxOSX_SYSTEM_FONT_SMALL
);
4272 m_headerWin
->SetFont( font
);
4275 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4279 GetSizer()->Detach( m_headerWin
);
4281 wxDELETE(m_headerWin
);
4285 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4290 const wxValidator
&validator
,
4291 const wxString
&name
)
4295 // just like in other ports, an assert will fail if the user doesn't give any type style:
4296 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4297 wxT("wxListCtrl style should have exactly one mode bit set") );
4299 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4303 style
&= ~wxBORDER_MASK
;
4304 style
|= wxBORDER_THEME
;
4307 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4309 SetTargetWindow( m_mainWin
);
4311 // We use the cursor keys for moving the selection, not scrolling, so call
4312 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4313 // keyboard events forwarded to us from wxListMainWindow.
4314 DisableKeyboardScrolling();
4316 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4317 sizer
->Add( m_mainWin
, 1, wxGROW
);
4320 CreateOrDestroyHeaderWindowAsNeeded();
4322 SetInitialSize(size
);
4327 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4329 return wxBORDER_THEME
;
4332 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4333 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4337 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4339 // we need to process arrows ourselves for scrolling
4340 if ( nMsg
== WM_GETDLGCODE
)
4342 rc
|= DLGC_WANTARROWS
;
4349 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4351 wxSize newsize
= size
;
4353 newsize
.y
-= m_headerWin
->GetSize().y
;
4358 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4360 // update our idea of which lines are shown when we redraw
4361 // the window the next time
4362 m_mainWin
->ResetVisibleLinesRange();
4364 HandleOnScroll( event
);
4366 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4368 m_headerWin
->Refresh();
4369 m_headerWin
->Update();
4373 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4375 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4376 wxT("wxLC_VIRTUAL can't be [un]set") );
4378 long flag
= GetWindowStyle();
4382 if (style
& wxLC_MASK_TYPE
)
4383 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4384 if (style
& wxLC_MASK_ALIGN
)
4385 flag
&= ~wxLC_MASK_ALIGN
;
4386 if (style
& wxLC_MASK_SORT
)
4387 flag
&= ~wxLC_MASK_SORT
;
4395 // some styles can be set without recreating everything (as happens in
4396 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4397 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4400 wxWindow::SetWindowStyleFlag(flag
);
4404 SetWindowStyleFlag( flag
);
4408 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4410 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4411 // makes sense to remove them as we'll always add scrollbars anyhow when
4413 flag
|= wxHSCROLL
| wxVSCROLL
;
4415 const bool wasInReportView
= HasFlag(wxLC_REPORT
);
4417 // update the window style first so that the header is created or destroyed
4418 // corresponding to the new style
4419 wxWindow::SetWindowStyleFlag( flag
);
4423 const bool inReportView
= (flag
& wxLC_REPORT
) != 0;
4424 if ( inReportView
!= wasInReportView
)
4426 // we need to notify the main window about this change as it must
4427 // update its data structures
4428 m_mainWin
->SetReportView(inReportView
);
4431 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4433 CreateOrDestroyHeaderWindowAsNeeded();
4435 GetSizer()->Layout();
4439 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4441 m_mainWin
->GetColumn( col
, item
);
4445 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4447 m_mainWin
->SetColumn( col
, item
);
4451 int wxGenericListCtrl::GetColumnWidth( int col
) const
4453 return m_mainWin
->GetColumnWidth( col
);
4456 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4458 m_mainWin
->SetColumnWidth( col
, width
);
4462 int wxGenericListCtrl::GetCountPerPage() const
4464 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4467 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4469 m_mainWin
->GetItem( info
);
4473 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4475 m_mainWin
->SetItem( info
);
4479 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4482 info
.m_text
= label
;
4483 info
.m_mask
= wxLIST_MASK_TEXT
;
4484 info
.m_itemId
= index
;
4488 info
.m_image
= imageId
;
4489 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4492 m_mainWin
->SetItem(info
);
4496 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4498 return m_mainWin
->GetItemState( item
, stateMask
);
4501 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4503 m_mainWin
->SetItemState( item
, state
, stateMask
);
4508 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4510 return SetItemColumnImage(item
, 0, image
);
4514 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4517 info
.m_image
= image
;
4518 info
.m_mask
= wxLIST_MASK_IMAGE
;
4519 info
.m_itemId
= item
;
4520 info
.m_col
= column
;
4521 m_mainWin
->SetItem( info
);
4525 wxString
wxGenericListCtrl::GetItemText( long item
, int col
) const
4527 return m_mainWin
->GetItemText(item
, col
);
4530 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4532 m_mainWin
->SetItemText(item
, str
);
4535 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4538 info
.m_mask
= wxLIST_MASK_DATA
;
4539 info
.m_itemId
= item
;
4540 m_mainWin
->GetItem( info
);
4544 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4547 info
.m_mask
= wxLIST_MASK_DATA
;
4548 info
.m_itemId
= item
;
4550 m_mainWin
->SetItem( info
);
4554 wxRect
wxGenericListCtrl::GetViewRect() const
4556 return m_mainWin
->GetViewRect();
4559 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4561 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4564 bool wxGenericListCtrl::GetSubItemRect(long item
,
4567 int WXUNUSED(code
)) const
4569 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4572 if ( m_mainWin
->HasHeader() )
4573 rect
.y
+= m_headerWin
->GetSize().y
+ 1;
4578 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4580 m_mainWin
->GetItemPosition( item
, pos
);
4584 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4589 int wxGenericListCtrl::GetItemCount() const
4591 return m_mainWin
->GetItemCount();
4594 int wxGenericListCtrl::GetColumnCount() const
4596 return m_mainWin
->GetColumnCount();
4599 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4601 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4604 wxSize
wxGenericListCtrl::GetItemSpacing() const
4606 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4608 return wxSize(spacing
, spacing
);
4611 #if WXWIN_COMPATIBILITY_2_6
4612 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4614 return m_mainWin
->GetItemSpacing( isSmall
);
4616 #endif // WXWIN_COMPATIBILITY_2_6
4618 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4621 info
.m_itemId
= item
;
4622 info
.SetTextColour( col
);
4623 m_mainWin
->SetItem( info
);
4626 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4629 info
.m_itemId
= item
;
4630 m_mainWin
->GetItem( info
);
4631 return info
.GetTextColour();
4634 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4637 info
.m_itemId
= item
;
4638 info
.SetBackgroundColour( col
);
4639 m_mainWin
->SetItem( info
);
4642 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4645 info
.m_itemId
= item
;
4646 m_mainWin
->GetItem( info
);
4647 return info
.GetBackgroundColour();
4650 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4653 info
.m_itemId
= item
;
4655 m_mainWin
->SetItem( info
);
4658 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4661 info
.m_itemId
= item
;
4662 m_mainWin
->GetItem( info
);
4663 return info
.GetFont();
4666 int wxGenericListCtrl::GetSelectedItemCount() const
4668 return m_mainWin
->GetSelectedItemCount();
4671 wxColour
wxGenericListCtrl::GetTextColour() const
4673 return GetForegroundColour();
4676 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4678 SetForegroundColour(col
);
4681 long wxGenericListCtrl::GetTopItem() const
4684 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4688 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4690 return m_mainWin
->GetNextItem( item
, geom
, state
);
4693 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4695 if (which
== wxIMAGE_LIST_NORMAL
)
4696 return m_imageListNormal
;
4697 else if (which
== wxIMAGE_LIST_SMALL
)
4698 return m_imageListSmall
;
4699 else if (which
== wxIMAGE_LIST_STATE
)
4700 return m_imageListState
;
4705 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4707 if ( which
== wxIMAGE_LIST_NORMAL
)
4709 if (m_ownsImageListNormal
)
4710 delete m_imageListNormal
;
4711 m_imageListNormal
= imageList
;
4712 m_ownsImageListNormal
= false;
4714 else if ( which
== wxIMAGE_LIST_SMALL
)
4716 if (m_ownsImageListSmall
)
4717 delete m_imageListSmall
;
4718 m_imageListSmall
= imageList
;
4719 m_ownsImageListSmall
= false;
4721 else if ( which
== wxIMAGE_LIST_STATE
)
4723 if (m_ownsImageListState
)
4724 delete m_imageListState
;
4725 m_imageListState
= imageList
;
4726 m_ownsImageListState
= false;
4729 m_mainWin
->SetImageList( imageList
, which
);
4732 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4734 SetImageList(imageList
, which
);
4735 if ( which
== wxIMAGE_LIST_NORMAL
)
4736 m_ownsImageListNormal
= true;
4737 else if ( which
== wxIMAGE_LIST_SMALL
)
4738 m_ownsImageListSmall
= true;
4739 else if ( which
== wxIMAGE_LIST_STATE
)
4740 m_ownsImageListState
= true;
4743 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4748 bool wxGenericListCtrl::DeleteItem( long item
)
4750 m_mainWin
->DeleteItem( item
);
4754 bool wxGenericListCtrl::DeleteAllItems()
4756 m_mainWin
->DeleteAllItems();
4760 bool wxGenericListCtrl::DeleteAllColumns()
4762 size_t count
= m_mainWin
->m_columns
.GetCount();
4763 for ( size_t n
= 0; n
< count
; n
++ )
4768 void wxGenericListCtrl::ClearAll()
4770 m_mainWin
->DeleteEverything();
4773 bool wxGenericListCtrl::DeleteColumn( int col
)
4775 m_mainWin
->DeleteColumn( col
);
4777 // if we don't have the header any longer, we need to relayout the window
4778 // if ( !GetColumnCount() )
4783 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4784 wxClassInfo
* textControlClass
)
4786 return m_mainWin
->EditLabel( item
, textControlClass
);
4789 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4791 return m_mainWin
->GetEditControl();
4794 bool wxGenericListCtrl::EnsureVisible( long item
)
4796 m_mainWin
->EnsureVisible( item
);
4800 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4802 return m_mainWin
->FindItem( start
, str
, partial
);
4805 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4807 return m_mainWin
->FindItem( start
, data
);
4810 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4811 int WXUNUSED(direction
))
4813 return m_mainWin
->FindItem( pt
);
4816 // TODO: sub item hit testing
4817 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4819 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4822 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4824 m_mainWin
->InsertItem( info
);
4825 return info
.m_itemId
;
4828 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4831 info
.m_text
= label
;
4832 info
.m_mask
= wxLIST_MASK_TEXT
;
4833 info
.m_itemId
= index
;
4834 return InsertItem( info
);
4837 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4840 info
.m_mask
= wxLIST_MASK_IMAGE
;
4841 info
.m_image
= imageIndex
;
4842 info
.m_itemId
= index
;
4843 return InsertItem( info
);
4846 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4849 info
.m_text
= label
;
4850 info
.m_image
= imageIndex
;
4851 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4852 info
.m_itemId
= index
;
4853 return InsertItem( info
);
4856 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4858 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4860 m_mainWin
->InsertColumn( col
, item
);
4862 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4863 // still have m_headerWin==NULL
4865 m_headerWin
->Refresh();
4870 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4871 int format
, int width
)
4874 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4875 item
.m_text
= heading
;
4878 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4879 item
.m_width
= width
;
4882 item
.m_format
= format
;
4884 return InsertColumn( col
, item
);
4887 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4889 return m_mainWin
->ScrollList(dx
, dy
);
4893 // fn is a function which takes 3 long arguments: item1, item2, data.
4894 // item1 is the long data associated with a first item (NOT the index).
4895 // item2 is the long data associated with a second item (NOT the index).
4896 // data is the same value as passed to SortItems.
4897 // The return value is a negative number if the first item should precede the second
4898 // item, a positive number of the second item should precede the first,
4899 // or zero if the two items are equivalent.
4900 // data is arbitrary data to be passed to the sort function.
4902 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4904 m_mainWin
->SortItems( fn
, data
);
4908 // ----------------------------------------------------------------------------
4910 // ----------------------------------------------------------------------------
4912 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4914 if (!m_mainWin
) return;
4916 // We need to override OnSize so that our scrolled
4917 // window a) does call Layout() to use sizers for
4918 // positioning the controls but b) does not query
4919 // the sizer for their size and use that for setting
4920 // the scrollable area as set that ourselves by
4921 // calling SetScrollbar() further down.
4925 m_mainWin
->RecalculatePositions();
4930 void wxGenericListCtrl::OnInternalIdle()
4932 wxWindow::OnInternalIdle();
4934 if (m_mainWin
->m_dirty
)
4935 m_mainWin
->RecalculatePositions();
4938 // ----------------------------------------------------------------------------
4940 // ----------------------------------------------------------------------------
4942 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4946 m_mainWin
->SetBackgroundColour( colour
);
4947 m_mainWin
->m_dirty
= true;
4953 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4955 if ( !wxWindow::SetForegroundColour( colour
) )
4960 m_mainWin
->SetForegroundColour( colour
);
4961 m_mainWin
->m_dirty
= true;
4965 m_headerWin
->SetForegroundColour( colour
);
4970 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4972 if ( !wxWindow::SetFont( font
) )
4977 m_mainWin
->SetFont( font
);
4978 m_mainWin
->m_dirty
= true;
4983 m_headerWin
->SetFont( font
);
4984 // CalculateAndSetHeaderHeight();
4994 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
4997 // Use the same color scheme as wxListBox
4998 return wxListBox::GetClassDefaultAttributes(variant
);
5000 wxUnusedVar(variant
);
5001 wxVisualAttributes attr
;
5002 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5003 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5004 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5009 // ----------------------------------------------------------------------------
5010 // methods forwarded to m_mainWin
5011 // ----------------------------------------------------------------------------
5013 #if wxUSE_DRAG_AND_DROP
5015 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5017 m_mainWin
->SetDropTarget( dropTarget
);
5020 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5022 return m_mainWin
->GetDropTarget();
5027 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5029 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5032 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5034 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5037 wxColour
wxGenericListCtrl::GetForegroundColour() const
5039 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5042 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5045 return m_mainWin
->PopupMenu( menu
, x
, y
);
5051 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5053 m_mainWin
->DoClientToScreen(x
, y
);
5056 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5058 m_mainWin
->DoScreenToClient(x
, y
);
5061 void wxGenericListCtrl::SetFocus()
5063 // The test in window.cpp fails as we are a composite
5064 // window, so it checks against "this", but not m_mainWin.
5065 if ( DoFindFocus() != this )
5066 m_mainWin
->SetFocus();
5069 wxSize
wxGenericListCtrl::DoGetBestClientSize() const
5071 // Something is better than nothing even if this is completely arbitrary.
5072 wxSize
sizeBest(100, 80);
5074 if ( !InReportView() )
5076 // Ensure that our minimal width is at least big enough to show all our
5077 // items. This is important for wxListbook to size itself correctly.
5079 // Remember the offset of the first item: this corresponds to the
5080 // margins around the item so we will add it to the minimal size below
5081 // to ensure that we have equal margins on all sides.
5084 // We can iterate over all items as there shouldn't be too many of them
5085 // in non-report view. If it ever becomes a problem, we could examine
5086 // just the first few items probably, the determination of the best
5087 // size is less important if we will need scrollbars anyhow.
5088 for ( int n
= 0; n
< GetItemCount(); n
++ )
5090 const wxRect itemRect
= m_mainWin
->GetLineRect(n
);
5093 // Remember the position of the first item as all the rest are
5094 // offset by at least this number of pixels too.
5095 ofs
= itemRect
.GetPosition();
5098 sizeBest
.IncTo(itemRect
.GetSize());
5101 sizeBest
.IncBy(2*ofs
);
5104 // If we have the scrollbars we need to account for them too. And to
5105 // make sure the scrollbars status is up to date we need to call this
5106 // function to set them.
5107 m_mainWin
->RecalculatePositions(true /* no refresh */);
5109 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5110 // to use m_mainWin client/virtual size for determination of whether we
5111 // use scrollbars and not the size of this window itself. Maybe that
5112 // function should be extended to work correctly in the case when our
5113 // scrollbars manage a different window from this one but currently it
5115 const wxSize sizeClient
= m_mainWin
->GetClientSize();
5116 const wxSize sizeVirt
= m_mainWin
->GetVirtualSize();
5118 if ( sizeVirt
.x
> sizeClient
.x
/* HasScrollbar(wxHORIZONTAL) */ )
5119 sizeBest
.y
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
5121 if ( sizeVirt
.y
> sizeClient
.y
/* HasScrollbar(wxVERTICAL) */ )
5122 sizeBest
.x
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
5128 // ----------------------------------------------------------------------------
5129 // virtual list control support
5130 // ----------------------------------------------------------------------------
5132 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5134 // this is a pure virtual function, in fact - which is not really pure
5135 // because the controls which are not virtual don't need to implement it
5136 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5138 return wxEmptyString
;
5141 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5143 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5145 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5149 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5152 return OnGetItemImage(item
);
5158 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5160 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5161 wxT("invalid item index in OnGetItemAttr()") );
5163 // no attributes by default
5167 void wxGenericListCtrl::SetItemCount(long count
)
5169 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5171 m_mainWin
->SetItemCount(count
);
5174 void wxGenericListCtrl::RefreshItem(long item
)
5176 m_mainWin
->RefreshLine(item
);
5179 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5181 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5184 // Generic wxListCtrl is more or less a container for two other
5185 // windows which drawings are done upon. These are namely
5186 // 'm_headerWin' and 'm_mainWin'.
5187 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5188 // behaviour wxListCtrl has under wxMSW.
5190 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5194 // The easy case, no rectangle specified.
5196 m_headerWin
->Refresh(eraseBackground
);
5199 m_mainWin
->Refresh(eraseBackground
);
5203 // Refresh the header window
5206 wxRect rectHeader
= m_headerWin
->GetRect();
5207 rectHeader
.Intersect(*rect
);
5208 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5211 m_headerWin
->GetPosition(&x
, &y
);
5212 rectHeader
.Offset(-x
, -y
);
5213 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5217 // Refresh the main window
5220 wxRect rectMain
= m_mainWin
->GetRect();
5221 rectMain
.Intersect(*rect
);
5222 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5225 m_mainWin
->GetPosition(&x
, &y
);
5226 rectMain
.Offset(-x
, -y
);
5227 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5233 void wxGenericListCtrl::Update()
5237 if ( m_mainWin
->m_dirty
)
5238 m_mainWin
->RecalculatePositions();
5240 m_mainWin
->Update();
5244 m_headerWin
->Update();
5247 #endif // wxUSE_LISTCTRL