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"
55 // for themeing support
56 #include <Carbon/Carbon.h>
60 // NOTE: If using the wxListBox visual attributes works everywhere then this can
61 // be removed, as well as the #else case below.
62 #define _USE_VISATTR 0
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // // the height of the header window (FIXME: should depend on its font!)
70 // static const int HEADER_HEIGHT = 23;
72 static const int SCROLL_UNIT_X
= 15;
74 // the spacing between the lines (in report mode)
75 static const int LINE_SPACING
= 0;
77 // extra margins around the text label
79 static const int EXTRA_WIDTH
= 6;
81 static const int EXTRA_WIDTH
= 4;
85 static const int EXTRA_HEIGHT
= 6;
87 static const int EXTRA_HEIGHT
= 4;
90 // margin between the window and the items
91 static const int EXTRA_BORDER_X
= 2;
92 static const int EXTRA_BORDER_Y
= 2;
94 // offset for the header window
95 static const int HEADER_OFFSET_X
= 0;
96 static const int HEADER_OFFSET_Y
= 0;
98 // margin between rows of icons in [small] icon view
99 static const int MARGIN_BETWEEN_ROWS
= 6;
101 // when autosizing the columns, add some slack
102 static const int AUTOSIZE_COL_MARGIN
= 10;
104 // default width for the header columns
105 static const int WIDTH_COL_DEFAULT
= 80;
107 // the space between the image and the text in the report mode
108 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
110 // the space between the image and the text in the report mode in header
111 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
115 // ----------------------------------------------------------------------------
116 // arrays/list implementations
117 // ----------------------------------------------------------------------------
119 #include "wx/listimpl.cpp"
120 WX_DEFINE_LIST(wxListItemDataList
)
122 #include "wx/arrimpl.cpp"
123 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
125 #include "wx/listimpl.cpp"
126 WX_DEFINE_LIST(wxListHeaderDataList
)
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 wxListItemData::~wxListItemData()
135 // in the virtual list control the attributes are managed by the main
136 // program, so don't delete them
137 if ( !m_owner
->IsVirtual() )
143 void wxListItemData::Init()
151 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
157 if ( owner
->InReportView() )
163 void wxListItemData::SetItem( const wxListItem
&info
)
165 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
166 SetText(info
.m_text
);
167 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
168 m_image
= info
.m_image
;
169 if ( info
.m_mask
& wxLIST_MASK_DATA
)
170 m_data
= info
.m_data
;
172 if ( info
.HasAttributes() )
175 m_attr
->AssignFrom(*info
.GetAttributes());
177 m_attr
= new wxListItemAttr(*info
.GetAttributes());
185 m_rect
->width
= info
.m_width
;
189 void wxListItemData::SetPosition( int x
, int y
)
191 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
197 void wxListItemData::SetSize( int width
, int height
)
199 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
202 m_rect
->width
= width
;
204 m_rect
->height
= height
;
207 bool wxListItemData::IsHit( int x
, int y
) const
209 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
211 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
214 int wxListItemData::GetX() const
216 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
221 int wxListItemData::GetY() const
223 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
228 int wxListItemData::GetWidth() const
230 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
232 return m_rect
->width
;
235 int wxListItemData::GetHeight() const
237 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
239 return m_rect
->height
;
242 void wxListItemData::GetItem( wxListItem
&info
) const
244 long mask
= info
.m_mask
;
246 // by default, get everything for backwards compatibility
249 if ( mask
& wxLIST_MASK_TEXT
)
250 info
.m_text
= m_text
;
251 if ( mask
& wxLIST_MASK_IMAGE
)
252 info
.m_image
= m_image
;
253 if ( mask
& wxLIST_MASK_DATA
)
254 info
.m_data
= m_data
;
258 if ( m_attr
->HasTextColour() )
259 info
.SetTextColour(m_attr
->GetTextColour());
260 if ( m_attr
->HasBackgroundColour() )
261 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
262 if ( m_attr
->HasFont() )
263 info
.SetFont(m_attr
->GetFont());
267 //-----------------------------------------------------------------------------
269 //-----------------------------------------------------------------------------
271 void wxListHeaderData::Init()
283 wxListHeaderData::wxListHeaderData()
288 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
295 void wxListHeaderData::SetItem( const wxListItem
&item
)
297 m_mask
= item
.m_mask
;
299 if ( m_mask
& wxLIST_MASK_TEXT
)
300 m_text
= item
.m_text
;
302 if ( m_mask
& wxLIST_MASK_IMAGE
)
303 m_image
= item
.m_image
;
305 if ( m_mask
& wxLIST_MASK_FORMAT
)
306 m_format
= item
.m_format
;
308 if ( m_mask
& wxLIST_MASK_WIDTH
)
309 SetWidth(item
.m_width
);
311 if ( m_mask
& wxLIST_MASK_STATE
)
312 SetState(item
.m_state
);
315 void wxListHeaderData::SetPosition( int x
, int y
)
321 void wxListHeaderData::SetHeight( int h
)
326 void wxListHeaderData::SetWidth( int w
)
328 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
331 void wxListHeaderData::SetState( int flag
)
336 void wxListHeaderData::SetFormat( int format
)
341 bool wxListHeaderData::HasImage() const
343 return m_image
!= -1;
346 bool wxListHeaderData::IsHit( int x
, int y
) const
348 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
351 void wxListHeaderData::GetItem( wxListItem
& item
)
353 item
.m_mask
= m_mask
;
354 item
.m_text
= m_text
;
355 item
.m_image
= m_image
;
356 item
.m_format
= m_format
;
357 item
.m_width
= m_width
;
358 item
.m_state
= m_state
;
361 int wxListHeaderData::GetImage() const
366 int wxListHeaderData::GetWidth() const
371 int wxListHeaderData::GetFormat() const
376 int wxListHeaderData::GetState() const
381 //-----------------------------------------------------------------------------
383 //-----------------------------------------------------------------------------
385 inline int wxListLineData::GetMode() const
387 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
390 inline bool wxListLineData::InReportView() const
392 return m_owner
->HasFlag(wxLC_REPORT
);
395 inline bool wxListLineData::IsVirtual() const
397 return m_owner
->IsVirtual();
400 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
404 if ( InReportView() )
407 m_gi
= new GeometryInfo
;
409 m_highlighted
= false;
411 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
414 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
416 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
417 wxCHECK_RET( node
, _T("no subitems at all??") );
419 wxListItemData
*item
= node
->GetData();
427 case wxLC_SMALL_ICON
:
428 m_gi
->m_rectAll
.width
= spacing
;
435 m_gi
->m_rectLabel
.width
=
436 m_gi
->m_rectLabel
.height
= 0;
440 dc
->GetTextExtent( s
, &lw
, &lh
);
444 m_gi
->m_rectAll
.height
= spacing
+ lh
;
446 m_gi
->m_rectAll
.width
= lw
;
448 m_gi
->m_rectLabel
.width
= lw
;
449 m_gi
->m_rectLabel
.height
= lh
;
452 if (item
->HasImage())
455 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
456 m_gi
->m_rectIcon
.width
= w
+ 8;
457 m_gi
->m_rectIcon
.height
= h
+ 8;
459 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
460 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
461 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
462 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
465 if ( item
->HasText() )
467 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
468 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
470 else // no text, highlight the icon
472 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
473 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
478 s
= item
->GetTextForMeasuring();
480 dc
->GetTextExtent( s
, &lw
, &lh
);
484 m_gi
->m_rectLabel
.width
= lw
;
485 m_gi
->m_rectLabel
.height
= lh
;
487 m_gi
->m_rectAll
.width
= lw
;
488 m_gi
->m_rectAll
.height
= lh
;
490 if (item
->HasImage())
493 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
494 m_gi
->m_rectIcon
.width
= w
;
495 m_gi
->m_rectIcon
.height
= h
;
497 m_gi
->m_rectAll
.width
+= 4 + w
;
498 if (h
> m_gi
->m_rectAll
.height
)
499 m_gi
->m_rectAll
.height
= h
;
502 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
503 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
507 wxFAIL_MSG( _T("unexpected call to SetSize") );
511 wxFAIL_MSG( _T("unknown mode") );
516 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
518 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
519 wxCHECK_RET( node
, _T("no subitems at all??") );
521 wxListItemData
*item
= node
->GetData();
526 case wxLC_SMALL_ICON
:
527 m_gi
->m_rectAll
.x
= x
;
528 m_gi
->m_rectAll
.y
= y
;
530 if ( item
->HasImage() )
532 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
533 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
534 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
537 if ( item
->HasText() )
539 if (m_gi
->m_rectAll
.width
> spacing
)
540 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
542 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
543 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
544 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
545 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
547 else // no text, highlight the icon
549 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
550 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
555 m_gi
->m_rectAll
.x
= x
;
556 m_gi
->m_rectAll
.y
= y
;
558 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
559 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
560 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
562 if (item
->HasImage())
564 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
565 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
566 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
570 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
575 wxFAIL_MSG( _T("unexpected call to SetPosition") );
579 wxFAIL_MSG( _T("unknown mode") );
584 void wxListLineData::InitItems( int num
)
586 for (int i
= 0; i
< num
; i
++)
587 m_items
.Append( new wxListItemData(m_owner
) );
590 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
592 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
593 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
595 wxListItemData
*item
= node
->GetData();
596 item
->SetItem( info
);
599 void wxListLineData::GetItem( int index
, wxListItem
&info
)
601 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
604 wxListItemData
*item
= node
->GetData();
605 item
->GetItem( info
);
609 wxString
wxListLineData::GetText(int index
) const
613 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
616 wxListItemData
*item
= node
->GetData();
623 void wxListLineData::SetText( int index
, const wxString
& s
)
625 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
628 wxListItemData
*item
= node
->GetData();
633 void wxListLineData::SetImage( int index
, int image
)
635 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
636 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
638 wxListItemData
*item
= node
->GetData();
639 item
->SetImage(image
);
642 int wxListLineData::GetImage( int index
) const
644 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
645 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
647 wxListItemData
*item
= node
->GetData();
648 return item
->GetImage();
651 wxListItemAttr
*wxListLineData::GetAttr() const
653 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
654 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
656 wxListItemData
*item
= node
->GetData();
657 return item
->GetAttr();
660 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
662 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
663 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
665 wxListItemData
*item
= node
->GetData();
669 bool wxListLineData::SetAttributes(wxDC
*dc
,
670 const wxListItemAttr
*attr
,
673 wxWindow
*listctrl
= m_owner
->GetParent();
677 // don't use foreground colour for drawing highlighted items - this might
678 // make them completely invisible (and there is no way to do bit
679 // arithmetics on wxColour, unfortunately)
684 if (m_owner
->HasFocus()
685 #if !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
686 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
694 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
696 else if ( attr
&& attr
->HasTextColour() )
697 colText
= attr
->GetTextColour();
699 colText
= listctrl
->GetForegroundColour();
701 dc
->SetTextForeground(colText
);
705 if ( attr
&& attr
->HasFont() )
706 font
= attr
->GetFont();
708 font
= listctrl
->GetFont();
713 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
714 if ( highlighted
|| hasBgCol
)
717 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
719 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
721 dc
->SetPen( *wxTRANSPARENT_PEN
);
729 void wxListLineData::Draw( wxDC
*dc
)
731 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
732 wxCHECK_RET( node
, _T("no subitems at all??") );
734 bool highlighted
= IsHighlighted();
736 wxListItemAttr
*attr
= GetAttr();
738 if ( SetAttributes(dc
, attr
, highlighted
) )
739 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
741 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
747 int flags
= wxCONTROL_SELECTED
;
748 if (m_owner
->HasFocus()
749 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
750 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
753 flags
|= wxCONTROL_FOCUSED
;
754 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
759 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
764 // just for debugging to better see where the items are
766 dc
->SetPen(*wxRED_PEN
);
767 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
768 dc
->DrawRectangle( m_gi
->m_rectAll
);
769 dc
->SetPen(*wxGREEN_PEN
);
770 dc
->DrawRectangle( m_gi
->m_rectIcon
);
773 wxListItemData
*item
= node
->GetData();
774 if (item
->HasImage())
776 // centre the image inside our rectangle, this looks nicer when items
777 // ae aligned in a row
778 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
780 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
785 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
787 wxDCClipper
clipper(*dc
, rectLabel
);
788 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
792 void wxListLineData::DrawInReportMode( wxDC
*dc
,
794 const wxRect
& rectHL
,
798 // TODO: later we should support setting different attributes for
799 // different columns - to do it, just add "col" argument to
800 // GetAttr() and move these lines into the loop below
801 wxListItemAttr
*attr
= GetAttr();
802 if ( SetAttributes(dc
, attr
, highlighted
) )
803 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
805 dc
->DrawRectangle( rectHL
);
811 int flags
= wxCONTROL_SELECTED
;
812 if (m_owner
->HasFocus())
813 flags
|= wxCONTROL_FOCUSED
;
815 flags
|= wxCONTROL_CURRENT
;
816 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
820 dc
->DrawRectangle( rectHL
);
825 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
826 yMid
= rect
.y
+ rect
.height
/2;
828 // This probably needs to be done
829 // on all platforms as the icons
830 // otherwise nearly touch the border
835 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
837 node
= node
->GetNext(), col
++ )
839 wxListItemData
*item
= node
->GetData();
841 int width
= m_owner
->GetColumnWidth(col
);
845 const int wText
= width
- 8;
846 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
848 if ( item
->HasImage() )
851 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
852 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
854 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
860 if ( item
->HasText() )
861 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
865 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
866 const wxString
& textOrig
,
872 // we don't support displaying multiple lines currently (and neither does
873 // wxMSW FWIW) so just merge all the lines
874 wxString
text(textOrig
);
875 text
.Replace(_T("\n"), _T(" "));
878 dc
->GetTextExtent(text
, &w
, &h
);
880 const wxCoord y
= yMid
- (h
+ 1)/2;
882 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
884 // determine if the string can fit inside the current width
887 // it can, draw it using the items alignment
889 m_owner
->GetColumn(col
, item
);
890 switch ( item
.GetAlign() )
892 case wxLIST_FORMAT_LEFT
:
896 case wxLIST_FORMAT_RIGHT
:
900 case wxLIST_FORMAT_CENTER
:
901 x
+= (width
- w
) / 2;
905 wxFAIL_MSG( _T("unknown list item format") );
909 dc
->DrawText(text
, x
, y
);
911 else // otherwise, truncate and add an ellipsis if possible
913 // determine the base width
914 wxString
ellipsis(wxT("..."));
916 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
918 // continue until we have enough space or only one character left
920 size_t len
= text
.length();
921 wxString drawntext
= text
.Left(len
);
924 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
925 drawntext
.RemoveLast();
928 if (w
+ base_w
<= width
)
932 // if still not enough space, remove ellipsis characters
933 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
935 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
936 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
940 dc
->DrawText(drawntext
, x
, y
);
941 dc
->DrawText(ellipsis
, x
+ w
, y
);
945 bool wxListLineData::Highlight( bool on
)
947 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
949 if ( on
== m_highlighted
)
957 void wxListLineData::ReverseHighlight( void )
959 Highlight(!IsHighlighted());
962 //-----------------------------------------------------------------------------
963 // wxListHeaderWindow
964 //-----------------------------------------------------------------------------
966 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
967 EVT_PAINT (wxListHeaderWindow::OnPaint
)
968 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
969 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
972 void wxListHeaderWindow::Init()
974 m_currentCursor
= NULL
;
975 m_isDragging
= false;
977 m_sendSetColumnWidth
= false;
980 wxListHeaderWindow::wxListHeaderWindow()
985 m_resizeCursor
= NULL
;
988 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
990 wxListMainWindow
*owner
,
994 const wxString
&name
)
995 : wxWindow( win
, id
, pos
, size
, style
, name
)
1000 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1003 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1004 SetOwnForegroundColour( attr
.colFg
);
1005 SetOwnBackgroundColour( attr
.colBg
);
1007 SetOwnFont( attr
.font
);
1009 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1010 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1012 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1016 wxListHeaderWindow::~wxListHeaderWindow()
1018 delete m_resizeCursor
;
1021 #ifdef __WXUNIVERSAL__
1022 #include "wx/univ/renderer.h"
1023 #include "wx/univ/theme.h"
1026 // shift the DC origin to match the position of the main window horz
1027 // scrollbar: this allows us to always use logical coords
1028 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1030 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1033 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1036 parent
->GetViewStart( &view_start
, NULL
);
1041 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1043 // account for the horz scrollbar offset
1045 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1047 // Maybe we just have to check for m_signX
1048 // in the DC, but I leave the #ifdef __WXGTK__
1050 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1054 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1057 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1059 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1061 wxPaintDC
dc( this );
1065 dc
.SetFont( GetFont() );
1067 // width and height of the entire header window
1069 GetClientSize( &w
, &h
);
1070 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1072 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1073 dc
.SetTextForeground(GetForegroundColour());
1075 int x
= HEADER_OFFSET_X
;
1076 int numColumns
= m_owner
->GetColumnCount();
1078 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1080 m_owner
->GetColumn( i
, item
);
1081 int wCol
= item
.m_width
;
1087 if (!m_parent
->IsEnabled())
1088 flags
|= wxCONTROL_DISABLED
;
1090 // NB: The code below is not really Mac-specific, but since we are close
1091 // to 2.8 release and I don't have time to test on other platforms, I
1092 // defined this only for wxMac. If this behavior is desired on
1093 // other platforms, please go ahead and revise or remove the #ifdef.
1095 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1096 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1097 flags
|= wxCONTROL_SELECTED
;
1101 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1103 wxRendererNative::Get().DrawHeaderButton
1107 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1111 // see if we have enough space for the column label
1113 // for this we need the width of the text
1116 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1117 wLabel
+= 2 * EXTRA_WIDTH
;
1119 // and the width of the icon, if any
1120 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1121 const int image
= item
.m_image
;
1122 wxImageList
*imageList
;
1125 imageList
= m_owner
->GetSmallImageList();
1128 imageList
->GetSize(image
, ix
, iy
);
1129 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1137 // ignore alignment if there is not enough space anyhow
1139 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1142 wxFAIL_MSG( _T("unknown list item format") );
1145 case wxLIST_FORMAT_LEFT
:
1149 case wxLIST_FORMAT_RIGHT
:
1150 xAligned
= x
+ cw
- wLabel
;
1153 case wxLIST_FORMAT_CENTER
:
1154 xAligned
= x
+ (cw
- wLabel
) / 2;
1158 // draw the text and image clipping them so that they
1159 // don't overwrite the column boundary
1160 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1162 // if we have an image, draw it on the right of the label
1169 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1170 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1171 wxIMAGELIST_DRAW_TRANSPARENT
1175 dc
.DrawText( item
.GetText(),
1176 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1181 // Fill in what's missing to the right of the columns, otherwise we will
1182 // leave an unpainted area when columns are removed (and it looks better)
1185 wxRendererNative::Get().DrawHeaderButton
1189 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1190 wxCONTROL_DIRTY
// mark as last column
1195 void wxListHeaderWindow::OnInternalIdle()
1197 wxWindow::OnInternalIdle();
1199 if (m_sendSetColumnWidth
)
1201 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1202 m_sendSetColumnWidth
= false;
1206 void wxListHeaderWindow::DrawCurrent()
1209 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1210 m_sendSetColumnWidth
= true;
1211 m_colToSend
= m_column
;
1212 m_widthToSend
= m_currentX
- m_minX
;
1214 int x1
= m_currentX
;
1216 m_owner
->ClientToScreen( &x1
, &y1
);
1218 int x2
= m_currentX
;
1220 m_owner
->GetClientSize( NULL
, &y2
);
1221 m_owner
->ClientToScreen( &x2
, &y2
);
1224 dc
.SetLogicalFunction( wxINVERT
);
1225 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1226 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1230 dc
.DrawLine( x1
, y1
, x2
, y2
);
1232 dc
.SetLogicalFunction( wxCOPY
);
1234 dc
.SetPen( wxNullPen
);
1235 dc
.SetBrush( wxNullBrush
);
1239 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1241 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1243 // we want to work with logical coords
1245 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1246 int y
= event
.GetY();
1250 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1252 // we don't draw the line beyond our window, but we allow dragging it
1255 GetClientSize( &w
, NULL
);
1256 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1259 // erase the line if it was drawn
1260 if ( m_currentX
< w
)
1263 if (event
.ButtonUp())
1266 m_isDragging
= false;
1268 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1269 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1276 m_currentX
= m_minX
+ 7;
1278 // draw in the new location
1279 if ( m_currentX
< w
)
1283 else // not dragging
1286 bool hit_border
= false;
1288 // end of the current column
1291 // find the column where this event occurred
1293 countCol
= m_owner
->GetColumnCount();
1294 for (col
= 0; col
< countCol
; col
++)
1296 xpos
+= m_owner
->GetColumnWidth( col
);
1299 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1301 // near the column border
1308 // inside the column
1315 if ( col
== countCol
)
1318 if (event
.LeftDown() || event
.RightUp())
1320 if (hit_border
&& event
.LeftDown())
1322 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1323 event
.GetPosition()) )
1325 m_isDragging
= true;
1330 //else: column resizing was vetoed by the user code
1332 else // click on a column
1334 // record the selected state of the columns
1335 if (event
.LeftDown())
1337 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
1340 m_owner
->GetColumn(i
, colItem
);
1341 long state
= colItem
.GetState();
1343 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
1345 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
1346 m_owner
->SetColumn(i
, colItem
);
1350 SendListEvent( event
.LeftDown()
1351 ? wxEVT_COMMAND_LIST_COL_CLICK
1352 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1353 event
.GetPosition());
1356 else if (event
.Moving())
1361 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1362 m_currentCursor
= m_resizeCursor
;
1366 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1367 m_currentCursor
= wxSTANDARD_CURSOR
;
1371 SetCursor(*m_currentCursor
);
1376 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1378 m_owner
->SetFocus();
1382 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1384 wxWindow
*parent
= GetParent();
1385 wxListEvent
le( type
, parent
->GetId() );
1386 le
.SetEventObject( parent
);
1387 le
.m_pointDrag
= pos
;
1389 // the position should be relative to the parent window, not
1390 // this one for compatibility with MSW and common sense: the
1391 // user code doesn't know anything at all about this header
1392 // window, so why should it get positions relative to it?
1393 le
.m_pointDrag
.y
-= GetSize().y
;
1395 le
.m_col
= m_column
;
1396 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1399 //-----------------------------------------------------------------------------
1400 // wxListRenameTimer (internal)
1401 //-----------------------------------------------------------------------------
1403 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1408 void wxListRenameTimer::Notify()
1410 m_owner
->OnRenameTimer();
1413 //-----------------------------------------------------------------------------
1414 // wxListTextCtrlWrapper (internal)
1415 //-----------------------------------------------------------------------------
1417 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
1418 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
1419 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
1420 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
1423 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
1426 : m_startValue(owner
->GetItemText(itemEdit
)),
1427 m_itemEdited(itemEdit
)
1431 m_aboutToFinish
= false;
1433 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1435 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1437 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1438 &rectLabel
.x
, &rectLabel
.y
);
1440 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
1441 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1442 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
1445 m_text
->PushEventHandler(this);
1448 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
1450 m_aboutToFinish
= true;
1452 if ( discardChanges
)
1454 m_owner
->OnRenameCancelled(m_itemEdited
);
1460 // Notify the owner about the changes
1463 // Even if vetoed, close the control (consistent with MSW)
1468 void wxListTextCtrlWrapper::Finish( bool setfocus
)
1470 m_text
->RemoveEventHandler(this);
1471 m_owner
->ResetTextControl( m_text
);
1473 wxPendingDelete
.Append( this );
1476 m_owner
->SetFocus();
1479 bool wxListTextCtrlWrapper::AcceptChanges()
1481 const wxString value
= m_text
->GetValue();
1483 // notice that we should always call OnRenameAccept() to generate the "end
1484 // label editing" event, even if the user hasn't really changed anything
1485 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
1487 // vetoed by the user
1491 // accepted, do rename the item (unless nothing changed)
1492 if ( value
!= m_startValue
)
1493 m_owner
->SetItemText(m_itemEdited
, value
);
1498 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
1500 switch ( event
.m_keyCode
)
1515 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
1517 if (m_aboutToFinish
)
1519 // auto-grow the textctrl:
1520 wxSize parentSize
= m_owner
->GetSize();
1521 wxPoint myPos
= m_text
->GetPosition();
1522 wxSize mySize
= m_text
->GetSize();
1524 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
1525 if (myPos
.x
+ sx
> parentSize
.x
)
1526 sx
= parentSize
.x
- myPos
.x
;
1529 m_text
->SetSize(sx
, wxDefaultCoord
);
1535 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
1537 if ( !m_aboutToFinish
)
1539 if ( !AcceptChanges() )
1540 m_owner
->OnRenameCancelled( m_itemEdited
);
1545 // We must let the native text control handle focus
1549 //-----------------------------------------------------------------------------
1551 //-----------------------------------------------------------------------------
1553 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledCanvas
)
1554 EVT_PAINT (wxListMainWindow::OnPaint
)
1555 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1556 EVT_CHAR (wxListMainWindow::OnChar
)
1557 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1558 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
1559 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1560 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1561 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1562 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
1565 void wxListMainWindow::Init()
1570 m_lineTo
= (size_t)-1;
1576 m_small_image_list
= NULL
;
1577 m_normal_image_list
= NULL
;
1579 m_small_spacing
= 30;
1580 m_normal_spacing
= 40;
1584 m_isCreated
= false;
1586 m_lastOnSame
= false;
1587 m_renameTimer
= new wxListRenameTimer( this );
1588 m_textctrlWrapper
= NULL
;
1592 m_lineSelectSingleOnUp
=
1593 m_lineBeforeLastClicked
= (size_t)-1;
1596 wxListMainWindow::wxListMainWindow()
1601 m_highlightUnfocusedBrush
= NULL
;
1604 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1609 const wxString
&name
)
1610 : wxWindow( parent
, id
, pos
, size
, style
, name
)
1614 m_highlightBrush
= new wxBrush
1616 wxSystemSettings::GetColour
1618 wxSYS_COLOUR_HIGHLIGHT
1623 m_highlightUnfocusedBrush
= new wxBrush
1625 wxSystemSettings::GetColour
1627 wxSYS_COLOUR_BTNSHADOW
1632 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
1633 SetOwnForegroundColour( attr
.colFg
);
1634 SetOwnBackgroundColour( attr
.colBg
);
1636 SetOwnFont( attr
.font
);
1639 wxListMainWindow::~wxListMainWindow()
1642 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
1643 WX_CLEAR_ARRAY(m_aColWidths
);
1645 delete m_highlightBrush
;
1646 delete m_highlightUnfocusedBrush
;
1647 delete m_renameTimer
;
1650 void wxListMainWindow::CacheLineData(size_t line
)
1652 wxGenericListCtrl
*listctrl
= GetListCtrl();
1654 wxListLineData
*ld
= GetDummyLine();
1656 size_t countCol
= GetColumnCount();
1657 for ( size_t col
= 0; col
< countCol
; col
++ )
1659 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1660 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1663 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1666 wxListLineData
*wxListMainWindow::GetDummyLine() const
1668 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
1669 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
1671 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1673 // we need to recreate the dummy line if the number of columns in the
1674 // control changed as it would have the incorrect number of fields
1676 if ( !m_lines
.IsEmpty() &&
1677 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1679 self
->m_lines
.Clear();
1682 if ( m_lines
.IsEmpty() )
1684 wxListLineData
*line
= new wxListLineData(self
);
1685 self
->m_lines
.Add(line
);
1687 // don't waste extra memory -- there never going to be anything
1688 // else/more in this array
1689 self
->m_lines
.Shrink();
1695 // ----------------------------------------------------------------------------
1696 // line geometry (report mode only)
1697 // ----------------------------------------------------------------------------
1699 wxCoord
wxListMainWindow::GetLineHeight() const
1701 // we cache the line height as calling GetTextExtent() is slow
1702 if ( !m_lineHeight
)
1704 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1706 wxClientDC
dc( self
);
1707 dc
.SetFont( GetFont() );
1710 dc
.GetTextExtent(_T("H"), NULL
, &y
);
1712 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1715 m_small_image_list
->GetSize(0, iw
, ih
);
1720 self
->m_lineHeight
= y
+ LINE_SPACING
;
1723 return m_lineHeight
;
1726 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1728 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
1730 return LINE_SPACING
+ line
* GetLineHeight();
1733 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1735 if ( !InReportView() )
1736 return GetLine(line
)->m_gi
->m_rectAll
;
1739 rect
.x
= HEADER_OFFSET_X
;
1740 rect
.y
= GetLineY(line
);
1741 rect
.width
= GetHeaderWidth();
1742 rect
.height
= GetLineHeight();
1747 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1749 if ( !InReportView() )
1750 return GetLine(line
)->m_gi
->m_rectLabel
;
1753 wxListLineData
*data
= GetLine(line
);
1754 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1757 wxListItemData
*item
= node
->GetData();
1758 if ( item
->HasImage() )
1761 GetImageSize( item
->GetImage(), ix
, iy
);
1762 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1767 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1768 rect
.y
= GetLineY(line
);
1769 rect
.width
= GetColumnWidth(0) - image_x
;
1770 rect
.height
= GetLineHeight();
1775 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1777 if ( !InReportView() )
1778 return GetLine(line
)->m_gi
->m_rectIcon
;
1780 wxListLineData
*ld
= GetLine(line
);
1781 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
1784 rect
.x
= HEADER_OFFSET_X
;
1785 rect
.y
= GetLineY(line
);
1786 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1791 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1793 return InReportView() ? GetLineRect(line
)
1794 : GetLine(line
)->m_gi
->m_rectHighlight
;
1797 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1799 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
1801 wxListLineData
*ld
= GetLine(line
);
1803 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1804 return wxLIST_HITTEST_ONITEMICON
;
1806 // VS: Testing for "ld->HasText() || InReportView()" instead of
1807 // "ld->HasText()" is needed to make empty lines in report view
1809 if ( ld
->HasText() || InReportView() )
1811 wxRect rect
= InReportView() ? GetLineRect(line
)
1812 : GetLineLabelRect(line
);
1814 if ( rect
.Contains(x
, y
) )
1815 return wxLIST_HITTEST_ONITEMLABEL
;
1821 // ----------------------------------------------------------------------------
1822 // highlight (selection) handling
1823 // ----------------------------------------------------------------------------
1825 bool wxListMainWindow::IsHighlighted(size_t line
) const
1829 return m_selStore
.IsSelected(line
);
1833 wxListLineData
*ld
= GetLine(line
);
1834 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
1836 return ld
->IsHighlighted();
1840 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1846 wxArrayInt linesChanged
;
1847 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1850 // meny items changed state, refresh everything
1851 RefreshLines(lineFrom
, lineTo
);
1853 else // only a few items changed state, refresh only them
1855 size_t count
= linesChanged
.GetCount();
1856 for ( size_t n
= 0; n
< count
; n
++ )
1858 RefreshLine(linesChanged
[n
]);
1862 else // iterate over all items in non report view
1864 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1866 if ( HighlightLine(line
, highlight
) )
1872 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1878 changed
= m_selStore
.SelectItem(line
, highlight
);
1882 wxListLineData
*ld
= GetLine(line
);
1883 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
1885 changed
= ld
->Highlight(highlight
);
1890 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1891 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1897 void wxListMainWindow::RefreshLine( size_t line
)
1899 if ( InReportView() )
1901 size_t visibleFrom
, visibleTo
;
1902 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1904 if ( line
< visibleFrom
|| line
> visibleTo
)
1908 wxRect rect
= GetLineRect(line
);
1910 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1911 RefreshRect( rect
);
1914 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1916 // we suppose that they are ordered by caller
1917 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
1919 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
1921 if ( InReportView() )
1923 size_t visibleFrom
, visibleTo
;
1924 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1926 if ( lineFrom
< visibleFrom
)
1927 lineFrom
= visibleFrom
;
1928 if ( lineTo
> visibleTo
)
1933 rect
.y
= GetLineY(lineFrom
);
1934 rect
.width
= GetClientSize().x
;
1935 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1937 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1938 RefreshRect( rect
);
1942 // TODO: this should be optimized...
1943 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1950 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1952 if ( InReportView() )
1954 size_t visibleFrom
, visibleTo
;
1955 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1957 if ( lineFrom
< visibleFrom
)
1958 lineFrom
= visibleFrom
;
1959 else if ( lineFrom
> visibleTo
)
1964 rect
.y
= GetLineY(lineFrom
);
1965 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1967 wxSize size
= GetClientSize();
1968 rect
.width
= size
.x
;
1970 // refresh till the bottom of the window
1971 rect
.height
= size
.y
- rect
.y
;
1973 RefreshRect( rect
);
1977 // TODO: how to do it more efficiently?
1982 void wxListMainWindow::RefreshSelected()
1988 if ( InReportView() )
1990 GetVisibleLinesRange(&from
, &to
);
1995 to
= GetItemCount() - 1;
1998 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
1999 RefreshLine(m_current
);
2001 for ( size_t line
= from
; line
<= to
; line
++ )
2003 // NB: the test works as expected even if m_current == -1
2004 if ( line
!= m_current
&& IsHighlighted(line
) )
2009 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2011 // Note: a wxPaintDC must be constructed even if no drawing is
2012 // done (a Windows requirement).
2013 wxPaintDC
dc( this );
2017 // nothing to draw or not the moment to draw it
2022 RecalculatePositions( false );
2024 GetListCtrl()->PrepareDC( dc
);
2027 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2029 dc
.SetFont( GetFont() );
2031 if ( InReportView() )
2033 int lineHeight
= GetLineHeight();
2035 size_t visibleFrom
, visibleTo
;
2036 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2039 int xOrig
= dc
.LogicalToDeviceX( 0 );
2040 int yOrig
= dc
.LogicalToDeviceY( 0 );
2042 // tell the caller cache to cache the data
2045 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2046 GetParent()->GetId());
2047 evCache
.SetEventObject( GetParent() );
2048 evCache
.m_oldItemIndex
= visibleFrom
;
2049 evCache
.m_itemIndex
= visibleTo
;
2050 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2053 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2055 rectLine
= GetLineRect(line
);
2058 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2059 rectLine
.width
, rectLine
.height
) )
2061 // don't redraw unaffected lines to avoid flicker
2065 GetLine(line
)->DrawInReportMode( &dc
,
2067 GetLineHighlightRect(line
),
2068 IsHighlighted(line
),
2069 line
== m_current
);
2072 if ( HasFlag(wxLC_HRULES
) )
2074 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2075 wxSize clientSize
= GetClientSize();
2077 size_t i
= visibleFrom
;
2078 if (i
== 0) i
= 1; // Don't draw the first one
2079 for ( ; i
<= visibleTo
; i
++ )
2082 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2083 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2084 clientSize
.x
- dev_x
, i
* lineHeight
);
2087 // Draw last horizontal rule
2088 if ( visibleTo
== GetItemCount() - 1 )
2091 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2092 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2093 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2097 // Draw vertical rules if required
2098 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2100 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2101 wxRect firstItemRect
, lastItemRect
;
2103 GetItemRect(visibleFrom
, firstItemRect
);
2104 GetItemRect(visibleTo
, lastItemRect
);
2105 int x
= firstItemRect
.GetX();
2107 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2109 for (int col
= 0; col
< GetColumnCount(); col
++)
2111 int colWidth
= GetColumnWidth(col
);
2113 int x_pos
= x
- dev_x
;
2114 if (col
< GetColumnCount()-1) x_pos
-= 2;
2115 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2116 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2122 size_t count
= GetItemCount();
2123 for ( size_t i
= 0; i
< count
; i
++ )
2125 GetLine(i
)->Draw( &dc
);
2129 #if !defined( __WXMAC__) && !defined(__WXGTK20__)
2130 // Don't draw rect outline under Mac at all.
2131 // Draw it elsewhere under GTK.
2136 wxRect
rect( GetLineHighlightRect( m_current
) );
2137 dc
.SetPen( *wxBLACK_PEN
);
2138 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2139 dc
.DrawRectangle( rect
);
2145 void wxListMainWindow::HighlightAll( bool on
)
2147 if ( IsSingleSel() )
2149 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2151 // we just have one item to turn off
2152 if ( HasCurrent() && IsHighlighted(m_current
) )
2154 HighlightLine(m_current
, false);
2155 RefreshLine(m_current
);
2158 else // multi selection
2161 HighlightLines(0, GetItemCount() - 1, on
);
2165 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2167 // Do nothing here. This prevents the default handler in wxScrolledWindow
2168 // from needlessly scrolling the window when the edit control is
2169 // dismissed. See ticket #9563.
2172 void wxListMainWindow::SendNotify( size_t line
,
2173 wxEventType command
,
2174 const wxPoint
& point
)
2176 wxListEvent
le( command
, GetParent()->GetId() );
2177 le
.SetEventObject( GetParent() );
2179 le
.m_itemIndex
= line
;
2181 // set only for events which have position
2182 if ( point
!= wxDefaultPosition
)
2183 le
.m_pointDrag
= point
;
2185 // don't try to get the line info for virtual list controls: the main
2186 // program has it anyhow and if we did it would result in accessing all
2187 // the lines, even those which are not visible now and this is precisely
2188 // what we're trying to avoid
2191 if ( line
!= (size_t)-1 )
2193 GetLine(line
)->GetItem( 0, le
.m_item
);
2195 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2197 //else: there may be no more such item
2199 GetParent()->GetEventHandler()->ProcessEvent( le
);
2202 void wxListMainWindow::ChangeCurrent(size_t current
)
2204 m_current
= current
;
2206 // as the current item changed, we shouldn't start editing it when the
2207 // "slow click" timer expires as the click happened on another item
2208 if ( m_renameTimer
->IsRunning() )
2209 m_renameTimer
->Stop();
2211 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2214 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2216 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2217 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2219 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2220 wxT("EditLabel() needs a text control") );
2222 size_t itemEdit
= (size_t)item
;
2224 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2225 le
.SetEventObject( GetParent() );
2226 le
.m_itemIndex
= item
;
2227 wxListLineData
*data
= GetLine(itemEdit
);
2228 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2229 data
->GetItem( 0, le
.m_item
);
2231 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2233 // vetoed by user code
2237 // We have to call this here because the label in question might just have
2238 // been added and no screen update taken place.
2243 // Pending events dispatched by wxSafeYield might have changed the item
2245 if ( (size_t)item
>= GetItemCount() )
2249 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2250 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2251 return m_textctrlWrapper
->GetText();
2254 void wxListMainWindow::OnRenameTimer()
2256 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2258 EditLabel( m_current
);
2261 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2263 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2264 le
.SetEventObject( GetParent() );
2265 le
.m_itemIndex
= itemEdit
;
2267 wxListLineData
*data
= GetLine(itemEdit
);
2269 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2271 data
->GetItem( 0, le
.m_item
);
2272 le
.m_item
.m_text
= value
;
2273 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2277 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2279 // let owner know that the edit was cancelled
2280 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2282 le
.SetEditCanceled(true);
2284 le
.SetEventObject( GetParent() );
2285 le
.m_itemIndex
= itemEdit
;
2287 wxListLineData
*data
= GetLine(itemEdit
);
2288 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2290 data
->GetItem( 0, le
.m_item
);
2291 GetEventHandler()->ProcessEvent( le
);
2294 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2298 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2299 // shutdown the edit control when the mouse is clicked elsewhere on the
2300 // listctrl because the order of events is different (or something like
2301 // that), so explicitly end the edit if it is active.
2302 if ( event
.LeftDown() && m_textctrlWrapper
)
2303 m_textctrlWrapper
->EndEdit( false );
2306 if ( event
.LeftDown() )
2309 event
.SetEventObject( GetParent() );
2310 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2313 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2315 // let the base handle mouse wheel events.
2320 if ( !HasCurrent() || IsEmpty() )
2322 if (event
.RightDown())
2324 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2326 wxContextMenuEvent
evtCtx(
2328 GetParent()->GetId(),
2329 ClientToScreen(event
.GetPosition()));
2330 evtCtx
.SetEventObject(GetParent());
2331 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2339 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2340 event
.ButtonDClick()) )
2343 int x
= event
.GetX();
2344 int y
= event
.GetY();
2345 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2347 // where did we hit it (if we did)?
2350 size_t count
= GetItemCount(),
2353 if ( InReportView() )
2355 current
= y
/ GetLineHeight();
2356 if ( current
< count
)
2357 hitResult
= HitTestLine(current
, x
, y
);
2361 // TODO: optimize it too! this is less simple than for report view but
2362 // enumerating all items is still not a way to do it!!
2363 for ( current
= 0; current
< count
; current
++ )
2365 hitResult
= HitTestLine(current
, x
, y
);
2371 if (event
.Dragging())
2373 if (m_dragCount
== 0)
2375 // we have to report the raw, physical coords as we want to be
2376 // able to call HitTest(event.m_pointDrag) from the user code to
2377 // get the item being dragged
2378 m_dragStart
= event
.GetPosition();
2383 if (m_dragCount
!= 3)
2386 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2387 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2389 wxListEvent
le( command
, GetParent()->GetId() );
2390 le
.SetEventObject( GetParent() );
2391 le
.m_itemIndex
= m_lineLastClicked
;
2392 le
.m_pointDrag
= m_dragStart
;
2393 GetParent()->GetEventHandler()->ProcessEvent( le
);
2404 // outside of any item
2405 if (event
.RightDown())
2407 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2409 wxContextMenuEvent
evtCtx(
2411 GetParent()->GetId(),
2412 ClientToScreen(event
.GetPosition()));
2413 evtCtx
.SetEventObject(GetParent());
2414 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2418 // reset the selection and bail out
2419 HighlightAll(false);
2425 bool forceClick
= false;
2426 if (event
.ButtonDClick())
2428 if ( m_renameTimer
->IsRunning() )
2429 m_renameTimer
->Stop();
2431 m_lastOnSame
= false;
2433 if ( current
== m_lineLastClicked
)
2435 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2441 // The first click was on another item, so don't interpret this as
2442 // a double click, but as a simple click instead
2449 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2451 // select single line
2452 HighlightAll( false );
2453 ReverseHighlight(m_lineSelectSingleOnUp
);
2458 if ((current
== m_current
) &&
2459 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2460 HasFlag(wxLC_EDIT_LABELS
) )
2462 if ( !InReportView() ||
2463 GetLineLabelRect(current
).Contains(x
, y
) )
2465 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2466 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2471 m_lastOnSame
= false;
2472 m_lineSelectSingleOnUp
= (size_t)-1;
2476 // This is necessary, because after a DnD operation in
2477 // from and to ourself, the up event is swallowed by the
2478 // DnD code. So on next non-up event (which means here and
2479 // now) m_lineSelectSingleOnUp should be reset.
2480 m_lineSelectSingleOnUp
= (size_t)-1;
2482 if (event
.RightDown())
2484 m_lineBeforeLastClicked
= m_lineLastClicked
;
2485 m_lineLastClicked
= current
;
2487 // If the item is already selected, do not update the selection.
2488 // Multi-selections should not be cleared if a selected item is clicked.
2489 if (!IsHighlighted(current
))
2491 HighlightAll(false);
2492 ChangeCurrent(current
);
2493 ReverseHighlight(m_current
);
2496 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2498 // Allow generation of context menu event
2501 else if (event
.MiddleDown())
2503 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2505 else if ( event
.LeftDown() || forceClick
)
2507 m_lineBeforeLastClicked
= m_lineLastClicked
;
2508 m_lineLastClicked
= current
;
2510 size_t oldCurrent
= m_current
;
2511 bool oldWasSelected
= IsHighlighted(m_current
);
2513 bool cmdModifierDown
= event
.CmdDown();
2514 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2516 if ( IsSingleSel() || !IsHighlighted(current
) )
2518 HighlightAll( false );
2520 ChangeCurrent(current
);
2522 ReverseHighlight(m_current
);
2524 else // multi sel & current is highlighted & no mod keys
2526 m_lineSelectSingleOnUp
= current
;
2527 ChangeCurrent(current
); // change focus
2530 else // multi sel & either ctrl or shift is down
2532 if (cmdModifierDown
)
2534 ChangeCurrent(current
);
2536 ReverseHighlight(m_current
);
2538 else if (event
.ShiftDown())
2540 ChangeCurrent(current
);
2542 size_t lineFrom
= oldCurrent
,
2545 if ( lineTo
< lineFrom
)
2548 lineFrom
= m_current
;
2551 HighlightLines(lineFrom
, lineTo
);
2553 else // !ctrl, !shift
2555 // test in the enclosing if should make it impossible
2556 wxFAIL_MSG( _T("how did we get here?") );
2560 if (m_current
!= oldCurrent
)
2561 RefreshLine( oldCurrent
);
2563 // forceClick is only set if the previous click was on another item
2564 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2568 void wxListMainWindow::MoveToItem(size_t item
)
2570 if ( item
== (size_t)-1 )
2573 wxRect rect
= GetLineRect(item
);
2575 int client_w
, client_h
;
2576 GetClientSize( &client_w
, &client_h
);
2578 const int hLine
= GetLineHeight();
2580 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2581 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2583 if ( InReportView() )
2585 // the next we need the range of lines shown it might be different,
2586 // so recalculate it
2587 ResetVisibleLinesRange();
2589 if (rect
.y
< view_y
)
2590 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2591 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2592 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2595 // At least on Mac the visible lines value will get reset inside of
2596 // Scroll *before* it actually scrolls the window because of the
2597 // Update() that happens there, so it will still have the wrong value.
2598 // So let's reset it again and wait for it to be recalculated in the
2599 // next paint event. I would expect this problem to show up in wxGTK
2600 // too but couldn't duplicate it there. Perhaps the order of events
2601 // is different... --Robin
2602 ResetVisibleLinesRange();
2610 if (rect
.x
-view_x
< 5)
2611 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2612 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2613 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2615 if (rect
.y
-view_y
< 5)
2616 sy
= (rect
.y
- 5) / hLine
;
2617 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2618 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2620 GetListCtrl()->Scroll(sx
, sy
);
2624 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2626 if ( !InReportView() )
2628 // TODO: this should work in all views but is not implemented now
2633 GetVisibleLinesRange(&top
, &bottom
);
2635 if ( bottom
== (size_t)-1 )
2638 ResetVisibleLinesRange();
2640 int hLine
= GetLineHeight();
2642 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2645 // see comment in MoveToItem() for why we do this
2646 ResetVisibleLinesRange();
2652 // ----------------------------------------------------------------------------
2653 // keyboard handling
2654 // ----------------------------------------------------------------------------
2656 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2658 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2659 _T("invalid item index in OnArrowChar()") );
2661 size_t oldCurrent
= m_current
;
2663 // in single selection we just ignore Shift as we can't select several
2665 if ( event
.ShiftDown() && !IsSingleSel() )
2667 ChangeCurrent(newCurrent
);
2669 // refresh the old focus to remove it
2670 RefreshLine( oldCurrent
);
2672 // select all the items between the old and the new one
2673 if ( oldCurrent
> newCurrent
)
2675 newCurrent
= oldCurrent
;
2676 oldCurrent
= m_current
;
2679 HighlightLines(oldCurrent
, newCurrent
);
2683 // all previously selected items are unselected unless ctrl is held
2684 // in a multiselection control
2685 if ( !event
.ControlDown() || IsSingleSel() )
2686 HighlightAll(false);
2688 ChangeCurrent(newCurrent
);
2690 // refresh the old focus to remove it
2691 RefreshLine( oldCurrent
);
2693 // in single selection mode we must always have a selected item
2694 if ( !event
.ControlDown() || IsSingleSel() )
2695 HighlightLine( m_current
, true );
2698 RefreshLine( m_current
);
2703 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2705 wxWindow
*parent
= GetParent();
2707 // propagate the key event upwards
2708 wxKeyEvent
ke(event
);
2709 ke
.SetEventObject( parent
);
2710 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2716 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2718 wxWindow
*parent
= GetParent();
2720 // propagate the key event upwards
2721 wxKeyEvent
ke(event
);
2722 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2728 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2730 wxWindow
*parent
= GetParent();
2732 // send a list_key event up
2735 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2736 le
.m_itemIndex
= m_current
;
2737 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2738 le
.m_code
= event
.GetKeyCode();
2739 le
.SetEventObject( parent
);
2740 parent
->GetEventHandler()->ProcessEvent( le
);
2743 if ( (event
.GetKeyCode() != WXK_UP
) &&
2744 (event
.GetKeyCode() != WXK_DOWN
) &&
2745 (event
.GetKeyCode() != WXK_RIGHT
) &&
2746 (event
.GetKeyCode() != WXK_LEFT
) &&
2747 (event
.GetKeyCode() != WXK_PAGEUP
) &&
2748 (event
.GetKeyCode() != WXK_PAGEDOWN
) &&
2749 (event
.GetKeyCode() != WXK_END
) &&
2750 (event
.GetKeyCode() != WXK_HOME
) )
2752 // propagate the char event upwards
2753 wxKeyEvent
ke(event
);
2754 ke
.SetEventObject( parent
);
2755 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2759 if ( HandleAsNavigationKey(event
) )
2762 // no item -> nothing to do
2769 // don't use m_linesPerPage directly as it might not be computed yet
2770 const int pageSize
= GetCountPerPage();
2771 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2773 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2775 if (event
.GetKeyCode() == WXK_RIGHT
)
2776 event
.m_keyCode
= WXK_LEFT
;
2777 else if (event
.GetKeyCode() == WXK_LEFT
)
2778 event
.m_keyCode
= WXK_RIGHT
;
2781 switch ( event
.GetKeyCode() )
2784 if ( m_current
> 0 )
2785 OnArrowChar( m_current
- 1, event
);
2789 if ( m_current
< (size_t)GetItemCount() - 1 )
2790 OnArrowChar( m_current
+ 1, event
);
2795 OnArrowChar( GetItemCount() - 1, event
);
2800 OnArrowChar( 0, event
);
2805 int steps
= InReportView() ? pageSize
- 1
2806 : m_current
% pageSize
;
2808 int index
= m_current
- steps
;
2812 OnArrowChar( index
, event
);
2818 int steps
= InReportView()
2820 : pageSize
- (m_current
% pageSize
) - 1;
2822 size_t index
= m_current
+ steps
;
2823 size_t count
= GetItemCount();
2824 if ( index
>= count
)
2827 OnArrowChar( index
, event
);
2832 if ( !InReportView() )
2834 int index
= m_current
- pageSize
;
2838 OnArrowChar( index
, event
);
2843 if ( !InReportView() )
2845 size_t index
= m_current
+ pageSize
;
2847 size_t count
= GetItemCount();
2848 if ( index
>= count
)
2851 OnArrowChar( index
, event
);
2856 if ( IsSingleSel() )
2858 if ( event
.ControlDown() )
2860 ReverseHighlight(m_current
);
2862 else // normal space press
2864 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2867 else // multiple selection
2869 ReverseHighlight(m_current
);
2875 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2883 // ----------------------------------------------------------------------------
2885 // ----------------------------------------------------------------------------
2887 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2891 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2892 event
.SetEventObject( GetParent() );
2893 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2897 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2898 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2899 // which are already drawn correctly resulting in horrible flicker - avoid
2909 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2913 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2914 event
.SetEventObject( GetParent() );
2915 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2923 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2925 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2927 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2929 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2931 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2933 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2935 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2937 else if ( InReportView() && (m_small_image_list
))
2939 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2943 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2945 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2947 m_normal_image_list
->GetSize( index
, width
, height
);
2949 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2951 m_small_image_list
->GetSize( index
, width
, height
);
2953 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2955 m_small_image_list
->GetSize( index
, width
, height
);
2957 else if ( InReportView() && m_small_image_list
)
2959 m_small_image_list
->GetSize( index
, width
, height
);
2968 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2970 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2971 dc
.SetFont( GetFont() );
2974 dc
.GetTextExtent( s
, &lw
, NULL
);
2976 return lw
+ AUTOSIZE_COL_MARGIN
;
2979 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2983 // calc the spacing from the icon size
2984 int width
= 0, height
= 0;
2986 if ((imageList
) && (imageList
->GetImageCount()) )
2987 imageList
->GetSize(0, width
, height
);
2989 if (which
== wxIMAGE_LIST_NORMAL
)
2991 m_normal_image_list
= imageList
;
2992 m_normal_spacing
= width
+ 8;
2995 if (which
== wxIMAGE_LIST_SMALL
)
2997 m_small_image_list
= imageList
;
2998 m_small_spacing
= width
+ 14;
2999 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3003 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3007 m_small_spacing
= spacing
;
3009 m_normal_spacing
= spacing
;
3012 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3014 return isSmall
? m_small_spacing
: m_normal_spacing
;
3017 // ----------------------------------------------------------------------------
3019 // ----------------------------------------------------------------------------
3021 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3023 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3025 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3027 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3028 item
.m_width
= GetTextLength( item
.m_text
);
3030 wxListHeaderData
*column
= node
->GetData();
3031 column
->SetItem( item
);
3033 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3035 headerWin
->m_dirty
= true;
3039 // invalidate it as it has to be recalculated
3043 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3045 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3046 _T("invalid column index") );
3048 wxCHECK_RET( InReportView(),
3049 _T("SetColumnWidth() can only be called in report mode.") );
3053 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3055 headerWin
->m_dirty
= true;
3057 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3058 wxCHECK_RET( node
, _T("no column?") );
3060 wxListHeaderData
*column
= node
->GetData();
3062 size_t count
= GetItemCount();
3064 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3066 width
= GetTextLength(column
->GetText());
3067 width
+= 2*EXTRA_WIDTH
;
3069 // check for column header's image availability
3070 const int image
= column
->GetImage();
3073 if ( m_small_image_list
)
3076 m_small_image_list
->GetSize(image
, ix
, iy
);
3077 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3081 else if ( width
== wxLIST_AUTOSIZE
)
3085 // TODO: determine the max width somehow...
3086 width
= WIDTH_COL_DEFAULT
;
3090 wxClientDC
dc(this);
3091 dc
.SetFont( GetFont() );
3093 int max
= AUTOSIZE_COL_MARGIN
;
3095 // if the cached column width isn't valid then recalculate it
3096 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3098 for (size_t i
= 0; i
< count
; i
++)
3100 wxListLineData
*line
= GetLine( i
);
3101 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3103 wxCHECK_RET( n
, _T("no subitem?") );
3105 wxListItemData
*itemData
= n
->GetData();
3108 itemData
->GetItem(item
);
3109 int itemWidth
= GetItemWidthWithImage(&item
);
3110 if (itemWidth
> max
)
3114 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3115 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3118 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3119 width
= max
+ AUTOSIZE_COL_MARGIN
;
3123 column
->SetWidth( width
);
3125 // invalidate it as it has to be recalculated
3129 int wxListMainWindow::GetHeaderWidth() const
3131 if ( !m_headerWidth
)
3133 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3135 size_t count
= GetColumnCount();
3136 for ( size_t col
= 0; col
< count
; col
++ )
3138 self
->m_headerWidth
+= GetColumnWidth(col
);
3142 return m_headerWidth
;
3145 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3147 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3148 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3150 wxListHeaderData
*column
= node
->GetData();
3151 column
->GetItem( item
);
3154 int wxListMainWindow::GetColumnWidth( int col
) const
3156 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3157 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3159 wxListHeaderData
*column
= node
->GetData();
3160 return column
->GetWidth();
3163 // ----------------------------------------------------------------------------
3165 // ----------------------------------------------------------------------------
3167 void wxListMainWindow::SetItem( wxListItem
&item
)
3169 long id
= item
.m_itemId
;
3170 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3171 _T("invalid item index in SetItem") );
3175 wxListLineData
*line
= GetLine((size_t)id
);
3176 line
->SetItem( item
.m_col
, item
);
3178 // Set item state if user wants
3179 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3180 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3184 // update the Max Width Cache if needed
3185 int width
= GetItemWidthWithImage(&item
);
3187 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3188 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3192 // update the item on screen
3194 GetItemRect(id
, rectItem
);
3195 RefreshRect(rectItem
);
3198 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3203 // first deal with selection
3204 if ( stateMask
& wxLIST_STATE_SELECTED
)
3206 // set/clear select state
3209 // optimized version for virtual listctrl.
3210 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3213 else if ( state
& wxLIST_STATE_SELECTED
)
3215 const long count
= GetItemCount();
3216 for( long i
= 0; i
< count
; i
++ )
3218 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3224 // clear for non virtual (somewhat optimized by using GetNextItem())
3226 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3228 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3233 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3235 // unfocus all: only one item can be focussed, so clearing focus for
3236 // all items is simply clearing focus of the focussed item.
3237 SetItemState(m_current
, state
, stateMask
);
3239 //(setting focus to all items makes no sense, so it is not handled here.)
3242 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3246 SetItemStateAll(state
, stateMask
);
3250 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3251 _T("invalid list ctrl item index in SetItem") );
3253 size_t oldCurrent
= m_current
;
3254 size_t item
= (size_t)litem
; // safe because of the check above
3256 // do we need to change the focus?
3257 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3259 if ( state
& wxLIST_STATE_FOCUSED
)
3261 // don't do anything if this item is already focused
3262 if ( item
!= m_current
)
3264 ChangeCurrent(item
);
3266 if ( oldCurrent
!= (size_t)-1 )
3268 if ( IsSingleSel() )
3270 HighlightLine(oldCurrent
, false);
3273 RefreshLine(oldCurrent
);
3276 RefreshLine( m_current
);
3281 // don't do anything if this item is not focused
3282 if ( item
== m_current
)
3286 if ( IsSingleSel() )
3288 // we must unselect the old current item as well or we
3289 // might end up with more than one selected item in a
3290 // single selection control
3291 HighlightLine(oldCurrent
, false);
3294 RefreshLine( oldCurrent
);
3299 // do we need to change the selection state?
3300 if ( stateMask
& wxLIST_STATE_SELECTED
)
3302 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3304 if ( IsSingleSel() )
3308 // selecting the item also makes it the focused one in the
3310 if ( m_current
!= item
)
3312 ChangeCurrent(item
);
3314 if ( oldCurrent
!= (size_t)-1 )
3316 HighlightLine( oldCurrent
, false );
3317 RefreshLine( oldCurrent
);
3323 // only the current item may be selected anyhow
3324 if ( item
!= m_current
)
3329 if ( HighlightLine(item
, on
) )
3336 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3338 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3339 _T("invalid list ctrl item index in GetItemState()") );
3341 int ret
= wxLIST_STATE_DONTCARE
;
3343 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3345 if ( (size_t)item
== m_current
)
3346 ret
|= wxLIST_STATE_FOCUSED
;
3349 if ( stateMask
& wxLIST_STATE_SELECTED
)
3351 if ( IsHighlighted(item
) )
3352 ret
|= wxLIST_STATE_SELECTED
;
3358 void wxListMainWindow::GetItem( wxListItem
&item
) const
3360 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3361 _T("invalid item index in GetItem") );
3363 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3364 line
->GetItem( item
.m_col
, item
);
3366 // Get item state if user wants it
3367 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3368 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3369 wxLIST_STATE_FOCUSED
);
3372 // ----------------------------------------------------------------------------
3374 // ----------------------------------------------------------------------------
3376 size_t wxListMainWindow::GetItemCount() const
3378 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3381 void wxListMainWindow::SetItemCount(long count
)
3383 m_selStore
.SetItemCount(count
);
3384 m_countVirt
= count
;
3386 ResetVisibleLinesRange();
3388 // scrollbars must be reset
3392 int wxListMainWindow::GetSelectedItemCount() const
3394 // deal with the quick case first
3395 if ( IsSingleSel() )
3396 return HasCurrent() ? IsHighlighted(m_current
) : false;
3398 // virtual controls remmebers all its selections itself
3400 return m_selStore
.GetSelectedCount();
3402 // TODO: we probably should maintain the number of items selected even for
3403 // non virtual controls as enumerating all lines is really slow...
3404 size_t countSel
= 0;
3405 size_t count
= GetItemCount();
3406 for ( size_t line
= 0; line
< count
; line
++ )
3408 if ( GetLine(line
)->IsHighlighted() )
3415 // ----------------------------------------------------------------------------
3416 // item position/size
3417 // ----------------------------------------------------------------------------
3419 wxRect
wxListMainWindow::GetViewRect() const
3421 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3423 // we need to find the longest/tallest label
3424 wxCoord xMax
= 0, yMax
= 0;
3425 const int count
= GetItemCount();
3428 for ( int i
= 0; i
< count
; i
++ )
3430 // we need logical, not physical, coordinates here, so use
3431 // GetLineRect() instead of GetItemRect()
3432 wxRect r
= GetLineRect(i
);
3434 wxCoord x
= r
.GetRight(),
3444 // some fudge needed to make it look prettier
3445 xMax
+= 2 * EXTRA_BORDER_X
;
3446 yMax
+= 2 * EXTRA_BORDER_Y
;
3448 // account for the scrollbars if necessary
3449 const wxSize sizeAll
= GetClientSize();
3450 if ( xMax
> sizeAll
.x
)
3451 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3452 if ( yMax
> sizeAll
.y
)
3453 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3455 return wxRect(0, 0, xMax
, yMax
);
3459 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3461 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3463 _T("GetSubItemRect only meaningful in report view") );
3464 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3465 _T("invalid item in GetSubItemRect") );
3467 // ensure that we're laid out, otherwise we could return nonsense
3470 wxConstCast(this, wxListMainWindow
)->
3471 RecalculatePositions(true /* no refresh */);
3474 rect
= GetLineRect((size_t)item
);
3476 // Adjust rect to specified column
3477 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3479 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3480 _T("invalid subItem in GetSubItemRect") );
3482 for (int i
= 0; i
< subItem
; i
++)
3484 rect
.x
+= GetColumnWidth(i
);
3486 rect
.width
= GetColumnWidth(subItem
);
3489 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3494 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3497 GetItemRect(item
, rect
);
3505 // ----------------------------------------------------------------------------
3506 // geometry calculation
3507 // ----------------------------------------------------------------------------
3509 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3511 const int lineHeight
= GetLineHeight();
3513 wxClientDC
dc( this );
3514 dc
.SetFont( GetFont() );
3516 const size_t count
= GetItemCount();
3519 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3520 iconSpacing
= m_normal_spacing
;
3521 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3522 iconSpacing
= m_small_spacing
;
3526 // Note that we do not call GetClientSize() here but
3527 // GetSize() and subtract the border size for sunken
3528 // borders manually. This is technically incorrect,
3529 // but we need to know the client area's size WITHOUT
3530 // scrollbars here. Since we don't know if there are
3531 // any scrollbars, we use GetSize() instead. Another
3532 // solution would be to call SetScrollbars() here to
3533 // remove the scrollbars and call GetClientSize() then,
3534 // but this might result in flicker and - worse - will
3535 // reset the scrollbars to 0 which is not good at all
3536 // if you resize a dialog/window, but don't want to
3537 // reset the window scrolling. RR.
3538 // Furthermore, we actually do NOT subtract the border
3539 // width as 2 pixels is just the extra space which we
3540 // need around the actual content in the window. Other-
3541 // wise the text would e.g. touch the upper border. RR.
3544 GetSize( &clientWidth
, &clientHeight
);
3546 if ( InReportView() )
3548 // all lines have the same height and we scroll one line per step
3549 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3551 m_linesPerPage
= clientHeight
/ lineHeight
;
3553 ResetVisibleLinesRange();
3555 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3556 GetHeaderWidth() / SCROLL_UNIT_X
,
3557 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3558 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3559 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3564 // we have 3 different layout strategies: either layout all items
3565 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3566 // to arrange them in top to bottom, left to right (don't ask me why
3567 // not the other way round...) order
3568 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3570 int x
= EXTRA_BORDER_X
;
3571 int y
= EXTRA_BORDER_Y
;
3573 wxCoord widthMax
= 0;
3576 for ( i
= 0; i
< count
; i
++ )
3578 wxListLineData
*line
= GetLine(i
);
3579 line
->CalculateSize( &dc
, iconSpacing
);
3580 line
->SetPosition( x
, y
, iconSpacing
);
3582 wxSize sizeLine
= GetLineSize(i
);
3584 if ( HasFlag(wxLC_ALIGN_TOP
) )
3586 if ( sizeLine
.x
> widthMax
)
3587 widthMax
= sizeLine
.x
;
3591 else // wxLC_ALIGN_LEFT
3593 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3597 if ( HasFlag(wxLC_ALIGN_TOP
) )
3599 // traverse the items again and tweak their sizes so that they are
3600 // all the same in a row
3601 for ( i
= 0; i
< count
; i
++ )
3603 wxListLineData
*line
= GetLine(i
);
3604 line
->m_gi
->ExtendWidth(widthMax
);
3608 GetListCtrl()->SetScrollbars
3612 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3613 (y
+ lineHeight
) / lineHeight
,
3614 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3615 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3619 else // "flowed" arrangement, the most complicated case
3621 // at first we try without any scrollbars, if the items don't fit into
3622 // the window, we recalculate after subtracting the space taken by the
3625 int entireWidth
= 0;
3627 for (int tries
= 0; tries
< 2; tries
++)
3629 entireWidth
= 2 * EXTRA_BORDER_X
;
3633 // Now we have decided that the items do not fit into the
3634 // client area, so we need a scrollbar
3635 entireWidth
+= SCROLL_UNIT_X
;
3638 int x
= EXTRA_BORDER_X
;
3639 int y
= EXTRA_BORDER_Y
;
3640 int maxWidthInThisRow
= 0;
3643 int currentlyVisibleLines
= 0;
3645 for (size_t i
= 0; i
< count
; i
++)
3647 currentlyVisibleLines
++;
3648 wxListLineData
*line
= GetLine( i
);
3649 line
->CalculateSize( &dc
, iconSpacing
);
3650 line
->SetPosition( x
, y
, iconSpacing
);
3652 wxSize sizeLine
= GetLineSize( i
);
3654 if ( maxWidthInThisRow
< sizeLine
.x
)
3655 maxWidthInThisRow
= sizeLine
.x
;
3658 if (currentlyVisibleLines
> m_linesPerPage
)
3659 m_linesPerPage
= currentlyVisibleLines
;
3661 if ( y
+ sizeLine
.y
>= clientHeight
)
3663 currentlyVisibleLines
= 0;
3665 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3666 x
+= maxWidthInThisRow
;
3667 entireWidth
+= maxWidthInThisRow
;
3668 maxWidthInThisRow
= 0;
3671 // We have reached the last item.
3672 if ( i
== count
- 1 )
3673 entireWidth
+= maxWidthInThisRow
;
3675 if ( (tries
== 0) &&
3676 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3678 clientHeight
-= wxSystemSettings::
3679 GetMetric(wxSYS_HSCROLL_Y
);
3684 if ( i
== count
- 1 )
3685 tries
= 1; // Everything fits, no second try required.
3689 GetListCtrl()->SetScrollbars
3693 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3695 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3704 // FIXME: why should we call it from here?
3711 void wxListMainWindow::RefreshAll()
3716 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3717 if ( headerWin
&& headerWin
->m_dirty
)
3719 headerWin
->m_dirty
= false;
3720 headerWin
->Refresh();
3724 void wxListMainWindow::UpdateCurrent()
3726 if ( !HasCurrent() && !IsEmpty() )
3730 long wxListMainWindow::GetNextItem( long item
,
3731 int WXUNUSED(geometry
),
3735 max
= GetItemCount();
3736 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3737 _T("invalid listctrl index in GetNextItem()") );
3739 // notice that we start with the next item (or the first one if item == -1)
3740 // and this is intentional to allow writing a simple loop to iterate over
3741 // all selected items
3744 // this is not an error because the index was OK initially,
3745 // just no such item
3752 size_t count
= GetItemCount();
3753 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3755 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3758 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3765 // ----------------------------------------------------------------------------
3767 // ----------------------------------------------------------------------------
3769 void wxListMainWindow::DeleteItem( long lindex
)
3771 size_t count
= GetItemCount();
3773 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3774 _T("invalid item index in DeleteItem") );
3776 size_t index
= (size_t)lindex
;
3778 // we don't need to adjust the index for the previous items
3779 if ( HasCurrent() && m_current
>= index
)
3781 // if the current item is being deleted, we want the next one to
3782 // become selected - unless there is no next one - so don't adjust
3783 // m_current in this case
3784 if ( m_current
!= index
|| m_current
== count
- 1 )
3788 if ( InReportView() )
3790 // mark the Column Max Width cache as dirty if the items in the line
3791 // we're deleting contain the Max Column Width
3792 wxListLineData
* const line
= GetLine(index
);
3793 wxListItemDataList::compatibility_iterator n
;
3794 wxListItemData
*itemData
;
3798 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3800 n
= line
->m_items
.Item( i
);
3801 itemData
= n
->GetData();
3802 itemData
->GetItem(item
);
3804 itemWidth
= GetItemWidthWithImage(&item
);
3806 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3807 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3810 ResetVisibleLinesRange();
3813 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3818 m_selStore
.OnItemDelete(index
);
3822 m_lines
.RemoveAt( index
);
3825 // we need to refresh the (vert) scrollbar as the number of items changed
3828 RefreshAfter(index
);
3831 void wxListMainWindow::DeleteColumn( int col
)
3833 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3835 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3838 delete node
->GetData();
3839 m_columns
.Erase( node
);
3843 // update all the items
3844 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3846 wxListLineData
* const line
= GetLine(i
);
3847 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3848 delete n
->GetData();
3849 line
->m_items
.Erase(n
);
3853 if ( InReportView() ) // we only cache max widths when in Report View
3855 delete m_aColWidths
.Item(col
);
3856 m_aColWidths
.RemoveAt(col
);
3859 // invalidate it as it has to be recalculated
3863 void wxListMainWindow::DoDeleteAllItems()
3866 // nothing to do - in particular, don't send the event
3871 // to make the deletion of all items faster, we don't send the
3872 // notifications for each item deletion in this case but only one event
3873 // for all of them: this is compatible with wxMSW and documented in
3874 // DeleteAllItems() description
3876 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3877 event
.SetEventObject( GetParent() );
3878 GetParent()->GetEventHandler()->ProcessEvent( event
);
3886 if ( InReportView() )
3888 ResetVisibleLinesRange();
3889 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3891 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3898 void wxListMainWindow::DeleteAllItems()
3902 RecalculatePositions();
3905 void wxListMainWindow::DeleteEverything()
3907 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3908 WX_CLEAR_ARRAY(m_aColWidths
);
3913 // ----------------------------------------------------------------------------
3914 // scanning for an item
3915 // ----------------------------------------------------------------------------
3917 void wxListMainWindow::EnsureVisible( long index
)
3919 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3920 _T("invalid index in EnsureVisible") );
3922 // We have to call this here because the label in question might just have
3923 // been added and its position is not known yet
3925 RecalculatePositions(true /* no refresh */);
3927 MoveToItem((size_t)index
);
3930 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3936 wxString str_upper
= str
.Upper();
3940 size_t count
= GetItemCount();
3941 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3943 wxListLineData
*line
= GetLine(i
);
3944 wxString line_upper
= line
->GetText(0).Upper();
3947 if (line_upper
== str_upper
)
3952 if (line_upper
.find(str_upper
) == 0)
3960 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3966 size_t count
= GetItemCount();
3967 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3969 wxListLineData
*line
= GetLine(i
);
3971 line
->GetItem( 0, item
);
3972 if (item
.m_data
== data
)
3979 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3982 GetVisibleLinesRange( &topItem
, NULL
);
3985 GetItemPosition( GetItemCount() - 1, p
);
3989 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3990 if ( id
>= 0 && id
< (long)GetItemCount() )
3996 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
3998 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
4000 size_t count
= GetItemCount();
4002 if ( InReportView() )
4004 size_t current
= y
/ GetLineHeight();
4005 if ( current
< count
)
4007 flags
= HitTestLine(current
, x
, y
);
4014 // TODO: optimize it too! this is less simple than for report view but
4015 // enumerating all items is still not a way to do it!!
4016 for ( size_t current
= 0; current
< count
; current
++ )
4018 flags
= HitTestLine(current
, x
, y
);
4027 // ----------------------------------------------------------------------------
4029 // ----------------------------------------------------------------------------
4031 void wxListMainWindow::InsertItem( wxListItem
&item
)
4033 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4035 int count
= GetItemCount();
4036 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4038 if (item
.m_itemId
> count
)
4039 item
.m_itemId
= count
;
4041 size_t id
= item
.m_itemId
;
4045 if ( InReportView() )
4047 ResetVisibleLinesRange();
4049 // calculate the width of the item and adjust the max column width
4050 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4051 int width
= GetItemWidthWithImage(&item
);
4052 item
.SetWidth(width
);
4053 if (width
> pWidthInfo
->nMaxWidth
)
4054 pWidthInfo
->nMaxWidth
= width
;
4057 wxListLineData
*line
= new wxListLineData(this);
4059 line
->SetItem( item
.m_col
, item
);
4061 m_lines
.Insert( line
, id
);
4065 // If an item is selected at or below the point of insertion, we need to
4066 // increment the member variables because the current row's index has gone
4068 if ( HasCurrent() && m_current
>= id
)
4071 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4073 RefreshLines(id
, GetItemCount() - 1);
4076 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4079 if ( InReportView() )
4081 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4082 item
.m_width
= GetTextLength( item
.m_text
);
4084 wxListHeaderData
*column
= new wxListHeaderData( item
);
4085 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4087 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4090 wxListHeaderDataList::compatibility_iterator
4091 node
= m_columns
.Item( col
);
4092 m_columns
.Insert( node
, column
);
4093 m_aColWidths
.Insert( colWidthInfo
, col
);
4097 m_columns
.Append( column
);
4098 m_aColWidths
.Add( colWidthInfo
);
4103 // update all the items
4104 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4106 wxListLineData
* const line
= GetLine(i
);
4107 wxListItemData
* const data
= new wxListItemData(this);
4109 line
->m_items
.Insert(col
, data
);
4111 line
->m_items
.Append(data
);
4115 // invalidate it as it has to be recalculated
4120 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4123 wxClientDC
dc(this);
4125 dc
.SetFont( GetFont() );
4127 if (item
->GetImage() != -1)
4130 GetImageSize( item
->GetImage(), ix
, iy
);
4134 if (!item
->GetText().empty())
4137 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4144 // ----------------------------------------------------------------------------
4146 // ----------------------------------------------------------------------------
4148 static wxListCtrlCompare list_ctrl_compare_func_2
;
4149 static long list_ctrl_compare_data
;
4151 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4153 wxListLineData
*line1
= *arg1
;
4154 wxListLineData
*line2
= *arg2
;
4156 line1
->GetItem( 0, item
);
4157 wxUIntPtr data1
= item
.m_data
;
4158 line2
->GetItem( 0, item
);
4159 wxUIntPtr data2
= item
.m_data
;
4160 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4163 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4165 // selections won't make sense any more after sorting the items so reset
4167 HighlightAll(false);
4170 list_ctrl_compare_func_2
= fn
;
4171 list_ctrl_compare_data
= data
;
4172 m_lines
.Sort( list_ctrl_compare_func_1
);
4176 // ----------------------------------------------------------------------------
4178 // ----------------------------------------------------------------------------
4180 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4182 // update our idea of which lines are shown when we redraw the window the
4184 ResetVisibleLinesRange();
4186 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4188 wxGenericListCtrl
* lc
= GetListCtrl();
4189 wxCHECK_RET( lc
, _T("no listctrl window?") );
4191 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4193 lc
->m_headerWin
->Refresh();
4194 lc
->m_headerWin
->Update();
4199 int wxListMainWindow::GetCountPerPage() const
4201 if ( !m_linesPerPage
)
4203 wxConstCast(this, wxListMainWindow
)->
4204 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4207 return m_linesPerPage
;
4210 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4212 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4214 if ( m_lineFrom
== (size_t)-1 )
4216 size_t count
= GetItemCount();
4219 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4221 // this may happen if SetScrollbars() hadn't been called yet
4222 if ( m_lineFrom
>= count
)
4223 m_lineFrom
= count
- 1;
4225 // we redraw one extra line but this is needed to make the redrawing
4226 // logic work when there is a fractional number of lines on screen
4227 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4228 if ( m_lineTo
>= count
)
4229 m_lineTo
= count
- 1;
4231 else // empty control
4234 m_lineTo
= (size_t)-1;
4238 wxASSERT_MSG( IsEmpty() ||
4239 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4240 _T("GetVisibleLinesRange() returns incorrect result") );
4248 // -------------------------------------------------------------------------------------
4249 // wxGenericListCtrl
4250 // -------------------------------------------------------------------------------------
4252 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4254 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4255 EVT_SIZE(wxGenericListCtrl::OnSize
)
4256 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4259 void wxGenericListCtrl::Init()
4261 m_imageListNormal
= NULL
;
4262 m_imageListSmall
= NULL
;
4263 m_imageListState
= NULL
;
4265 m_ownsImageListNormal
=
4266 m_ownsImageListSmall
=
4267 m_ownsImageListState
= false;
4271 m_headerHeight
= wxRendererNative::Get().GetHeaderButtonHeight(this);
4274 wxGenericListCtrl::~wxGenericListCtrl()
4276 if (m_ownsImageListNormal
)
4277 delete m_imageListNormal
;
4278 if (m_ownsImageListSmall
)
4279 delete m_imageListSmall
;
4280 if (m_ownsImageListState
)
4281 delete m_imageListState
;
4284 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4286 bool needs_header
= HasHeader();
4287 bool has_header
= (m_headerWin
!= NULL
);
4289 if (needs_header
== has_header
)
4294 m_headerWin
= new wxListHeaderWindow
4296 this, wxID_ANY
, m_mainWin
,
4298 wxSize(GetClientSize().x
, m_headerHeight
),
4302 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
4304 #if wxOSX_USE_ATSU_TEXT
4305 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
4307 font
.MacCreateFromUIFont( kCTFontSystemFontType
);
4309 m_headerWin
->SetFont( font
);
4312 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4316 GetSizer()->Detach( m_headerWin
);
4324 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4329 const wxValidator
&validator
,
4330 const wxString
&name
)
4334 // just like in other ports, an assert will fail if the user doesn't give any type style:
4335 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4336 _T("wxListCtrl style should have exactly one mode bit set") );
4338 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4342 style
&= ~wxBORDER_MASK
;
4343 style
|= wxBORDER_THEME
;
4346 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4348 SetTargetWindow( m_mainWin
);
4350 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4351 sizer
->Add( m_mainWin
, 1, wxGROW
);
4354 CreateOrDestroyHeaderWindowAsNeeded();
4356 SetInitialSize(size
);
4361 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4363 return wxBORDER_THEME
;
4367 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4371 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4374 // we need to process arrows ourselves for scrolling
4375 if ( nMsg
== WM_GETDLGCODE
)
4377 rc
|= DLGC_WANTARROWS
;
4385 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4387 wxSize newsize
= size
;
4389 newsize
.y
-= m_headerWin
->GetSize().y
;
4394 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4396 // update our idea of which lines are shown when we redraw
4397 // the window the next time
4398 m_mainWin
->ResetVisibleLinesRange();
4400 HandleOnScroll( event
);
4402 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4404 m_headerWin
->Refresh();
4405 m_headerWin
->Update();
4409 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4411 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4412 _T("wxLC_VIRTUAL can't be [un]set") );
4414 long flag
= GetWindowStyle();
4418 if (style
& wxLC_MASK_TYPE
)
4419 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4420 if (style
& wxLC_MASK_ALIGN
)
4421 flag
&= ~wxLC_MASK_ALIGN
;
4422 if (style
& wxLC_MASK_SORT
)
4423 flag
&= ~wxLC_MASK_SORT
;
4431 // some styles can be set without recreating everything (as happens in
4432 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4433 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4436 wxWindow::SetWindowStyleFlag(flag
);
4440 SetWindowStyleFlag( flag
);
4444 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4448 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4450 CreateOrDestroyHeaderWindowAsNeeded();
4452 GetSizer()->Layout();
4455 wxWindow::SetWindowStyleFlag( flag
);
4458 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4460 m_mainWin
->GetColumn( col
, item
);
4464 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4466 m_mainWin
->SetColumn( col
, item
);
4470 int wxGenericListCtrl::GetColumnWidth( int col
) const
4472 return m_mainWin
->GetColumnWidth( col
);
4475 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4477 m_mainWin
->SetColumnWidth( col
, width
);
4481 int wxGenericListCtrl::GetCountPerPage() const
4483 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4486 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4488 m_mainWin
->GetItem( info
);
4492 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4494 m_mainWin
->SetItem( info
);
4498 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4501 info
.m_text
= label
;
4502 info
.m_mask
= wxLIST_MASK_TEXT
;
4503 info
.m_itemId
= index
;
4507 info
.m_image
= imageId
;
4508 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4511 m_mainWin
->SetItem(info
);
4515 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4517 return m_mainWin
->GetItemState( item
, stateMask
);
4520 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4522 m_mainWin
->SetItemState( item
, state
, stateMask
);
4527 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4529 return SetItemColumnImage(item
, 0, image
);
4533 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4536 info
.m_image
= image
;
4537 info
.m_mask
= wxLIST_MASK_IMAGE
;
4538 info
.m_itemId
= item
;
4539 info
.m_col
= column
;
4540 m_mainWin
->SetItem( info
);
4544 wxString
wxGenericListCtrl::GetItemText( long item
) const
4546 return m_mainWin
->GetItemText(item
);
4549 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4551 m_mainWin
->SetItemText(item
, str
);
4554 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4557 info
.m_mask
= wxLIST_MASK_DATA
;
4558 info
.m_itemId
= item
;
4559 m_mainWin
->GetItem( info
);
4563 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4566 info
.m_mask
= wxLIST_MASK_DATA
;
4567 info
.m_itemId
= item
;
4569 m_mainWin
->SetItem( info
);
4573 wxRect
wxGenericListCtrl::GetViewRect() const
4575 return m_mainWin
->GetViewRect();
4578 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4580 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4583 bool wxGenericListCtrl::GetSubItemRect(long item
,
4586 int WXUNUSED(code
)) const
4588 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4591 if ( m_mainWin
->HasHeader() )
4592 rect
.y
+= m_headerHeight
+ 1;
4597 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4599 m_mainWin
->GetItemPosition( item
, pos
);
4603 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4608 int wxGenericListCtrl::GetItemCount() const
4610 return m_mainWin
->GetItemCount();
4613 int wxGenericListCtrl::GetColumnCount() const
4615 return m_mainWin
->GetColumnCount();
4618 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4620 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4623 wxSize
wxGenericListCtrl::GetItemSpacing() const
4625 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4627 return wxSize(spacing
, spacing
);
4630 #if WXWIN_COMPATIBILITY_2_6
4631 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4633 return m_mainWin
->GetItemSpacing( isSmall
);
4635 #endif // WXWIN_COMPATIBILITY_2_6
4637 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4640 info
.m_itemId
= item
;
4641 info
.SetTextColour( col
);
4642 m_mainWin
->SetItem( info
);
4645 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4648 info
.m_itemId
= item
;
4649 m_mainWin
->GetItem( info
);
4650 return info
.GetTextColour();
4653 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4656 info
.m_itemId
= item
;
4657 info
.SetBackgroundColour( col
);
4658 m_mainWin
->SetItem( info
);
4661 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4664 info
.m_itemId
= item
;
4665 m_mainWin
->GetItem( info
);
4666 return info
.GetBackgroundColour();
4669 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4672 info
.m_itemId
= item
;
4674 m_mainWin
->SetItem( info
);
4677 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4680 info
.m_itemId
= item
;
4681 m_mainWin
->GetItem( info
);
4682 return info
.GetFont();
4685 int wxGenericListCtrl::GetSelectedItemCount() const
4687 return m_mainWin
->GetSelectedItemCount();
4690 wxColour
wxGenericListCtrl::GetTextColour() const
4692 return GetForegroundColour();
4695 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4697 SetForegroundColour(col
);
4700 long wxGenericListCtrl::GetTopItem() const
4703 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4707 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4709 return m_mainWin
->GetNextItem( item
, geom
, state
);
4712 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4714 if (which
== wxIMAGE_LIST_NORMAL
)
4715 return m_imageListNormal
;
4716 else if (which
== wxIMAGE_LIST_SMALL
)
4717 return m_imageListSmall
;
4718 else if (which
== wxIMAGE_LIST_STATE
)
4719 return m_imageListState
;
4724 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4726 if ( which
== wxIMAGE_LIST_NORMAL
)
4728 if (m_ownsImageListNormal
)
4729 delete m_imageListNormal
;
4730 m_imageListNormal
= imageList
;
4731 m_ownsImageListNormal
= false;
4733 else if ( which
== wxIMAGE_LIST_SMALL
)
4735 if (m_ownsImageListSmall
)
4736 delete m_imageListSmall
;
4737 m_imageListSmall
= imageList
;
4738 m_ownsImageListSmall
= false;
4740 else if ( which
== wxIMAGE_LIST_STATE
)
4742 if (m_ownsImageListState
)
4743 delete m_imageListState
;
4744 m_imageListState
= imageList
;
4745 m_ownsImageListState
= false;
4748 m_mainWin
->SetImageList( imageList
, which
);
4751 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4753 SetImageList(imageList
, which
);
4754 if ( which
== wxIMAGE_LIST_NORMAL
)
4755 m_ownsImageListNormal
= true;
4756 else if ( which
== wxIMAGE_LIST_SMALL
)
4757 m_ownsImageListSmall
= true;
4758 else if ( which
== wxIMAGE_LIST_STATE
)
4759 m_ownsImageListState
= true;
4762 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4767 bool wxGenericListCtrl::DeleteItem( long item
)
4769 m_mainWin
->DeleteItem( item
);
4773 bool wxGenericListCtrl::DeleteAllItems()
4775 m_mainWin
->DeleteAllItems();
4779 bool wxGenericListCtrl::DeleteAllColumns()
4781 size_t count
= m_mainWin
->m_columns
.GetCount();
4782 for ( size_t n
= 0; n
< count
; n
++ )
4787 void wxGenericListCtrl::ClearAll()
4789 m_mainWin
->DeleteEverything();
4792 bool wxGenericListCtrl::DeleteColumn( int col
)
4794 m_mainWin
->DeleteColumn( col
);
4796 // if we don't have the header any longer, we need to relayout the window
4797 // if ( !GetColumnCount() )
4802 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4803 wxClassInfo
* textControlClass
)
4805 return m_mainWin
->EditLabel( item
, textControlClass
);
4808 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4810 return m_mainWin
->GetEditControl();
4813 bool wxGenericListCtrl::EnsureVisible( long item
)
4815 m_mainWin
->EnsureVisible( item
);
4819 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4821 return m_mainWin
->FindItem( start
, str
, partial
);
4824 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4826 return m_mainWin
->FindItem( start
, data
);
4829 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4830 int WXUNUSED(direction
))
4832 return m_mainWin
->FindItem( pt
);
4835 // TODO: sub item hit testing
4836 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4838 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4841 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4843 m_mainWin
->InsertItem( info
);
4844 return info
.m_itemId
;
4847 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4850 info
.m_text
= label
;
4851 info
.m_mask
= wxLIST_MASK_TEXT
;
4852 info
.m_itemId
= index
;
4853 return InsertItem( info
);
4856 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4859 info
.m_mask
= wxLIST_MASK_IMAGE
;
4860 info
.m_image
= imageIndex
;
4861 info
.m_itemId
= index
;
4862 return InsertItem( info
);
4865 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4868 info
.m_text
= label
;
4869 info
.m_image
= imageIndex
;
4870 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4871 info
.m_itemId
= index
;
4872 return InsertItem( info
);
4875 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4877 wxCHECK_MSG( InReportView(), -1, _T("can't add column in non report mode") );
4879 m_mainWin
->InsertColumn( col
, item
);
4881 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4882 // still have m_headerWin==NULL
4884 m_headerWin
->Refresh();
4889 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4890 int format
, int width
)
4893 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4894 item
.m_text
= heading
;
4897 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4898 item
.m_width
= width
;
4901 item
.m_format
= format
;
4903 return InsertColumn( col
, item
);
4906 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4908 return m_mainWin
->ScrollList(dx
, dy
);
4912 // fn is a function which takes 3 long arguments: item1, item2, data.
4913 // item1 is the long data associated with a first item (NOT the index).
4914 // item2 is the long data associated with a second item (NOT the index).
4915 // data is the same value as passed to SortItems.
4916 // The return value is a negative number if the first item should precede the second
4917 // item, a positive number of the second item should precede the first,
4918 // or zero if the two items are equivalent.
4919 // data is arbitrary data to be passed to the sort function.
4921 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
4923 m_mainWin
->SortItems( fn
, data
);
4927 // ----------------------------------------------------------------------------
4929 // ----------------------------------------------------------------------------
4931 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4933 if (!m_mainWin
) return;
4935 // We need to override OnSize so that our scrolled
4936 // window a) does call Layout() to use sizers for
4937 // positioning the controls but b) does not query
4938 // the sizer for their size and use that for setting
4939 // the scrollable area as set that ourselves by
4940 // calling SetScrollbar() further down.
4944 m_mainWin
->RecalculatePositions();
4949 void wxGenericListCtrl::OnInternalIdle()
4951 wxWindow::OnInternalIdle();
4953 if (m_mainWin
->m_dirty
)
4954 m_mainWin
->RecalculatePositions();
4957 // ----------------------------------------------------------------------------
4959 // ----------------------------------------------------------------------------
4961 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4965 m_mainWin
->SetBackgroundColour( colour
);
4966 m_mainWin
->m_dirty
= true;
4972 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4974 if ( !wxWindow::SetForegroundColour( colour
) )
4979 m_mainWin
->SetForegroundColour( colour
);
4980 m_mainWin
->m_dirty
= true;
4984 m_headerWin
->SetForegroundColour( colour
);
4989 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4991 if ( !wxWindow::SetFont( font
) )
4996 m_mainWin
->SetFont( font
);
4997 m_mainWin
->m_dirty
= true;
5002 m_headerWin
->SetFont( font
);
5003 // CalculateAndSetHeaderHeight();
5013 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5016 // Use the same color scheme as wxListBox
5017 return wxListBox::GetClassDefaultAttributes(variant
);
5019 wxUnusedVar(variant
);
5020 wxVisualAttributes attr
;
5021 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5022 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5023 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5028 // ----------------------------------------------------------------------------
5029 // methods forwarded to m_mainWin
5030 // ----------------------------------------------------------------------------
5032 #if wxUSE_DRAG_AND_DROP
5034 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5036 m_mainWin
->SetDropTarget( dropTarget
);
5039 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5041 return m_mainWin
->GetDropTarget();
5046 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5048 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5051 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5053 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5056 wxColour
wxGenericListCtrl::GetForegroundColour() const
5058 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5061 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5064 return m_mainWin
->PopupMenu( menu
, x
, y
);
5070 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5072 m_mainWin
->DoClientToScreen(x
, y
);
5075 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5077 m_mainWin
->DoScreenToClient(x
, y
);
5080 void wxGenericListCtrl::SetFocus()
5082 // The test in window.cpp fails as we are a composite
5083 // window, so it checks against "this", but not m_mainWin.
5084 if ( DoFindFocus() != this )
5085 m_mainWin
->SetFocus();
5088 wxSize
wxGenericListCtrl::DoGetBestSize() const
5090 // Something is better than nothing...
5091 // 100x80 is what the MSW version will get from the default
5092 // wxControl::DoGetBestSize
5093 return wxSize(100, 80);
5096 // ----------------------------------------------------------------------------
5097 // virtual list control support
5098 // ----------------------------------------------------------------------------
5100 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5102 // this is a pure virtual function, in fact - which is not really pure
5103 // because the controls which are not virtual don't need to implement it
5104 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5106 return wxEmptyString
;
5109 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5111 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5113 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5117 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5120 return OnGetItemImage(item
);
5126 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5128 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5129 _T("invalid item index in OnGetItemAttr()") );
5131 // no attributes by default
5135 void wxGenericListCtrl::SetItemCount(long count
)
5137 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5139 m_mainWin
->SetItemCount(count
);
5142 void wxGenericListCtrl::RefreshItem(long item
)
5144 m_mainWin
->RefreshLine(item
);
5147 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5149 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5152 // Generic wxListCtrl is more or less a container for two other
5153 // windows which drawings are done upon. These are namely
5154 // 'm_headerWin' and 'm_mainWin'.
5155 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5156 // behaviour wxListCtrl has under wxMSW.
5158 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5162 // The easy case, no rectangle specified.
5164 m_headerWin
->Refresh(eraseBackground
);
5167 m_mainWin
->Refresh(eraseBackground
);
5171 // Refresh the header window
5174 wxRect rectHeader
= m_headerWin
->GetRect();
5175 rectHeader
.Intersect(*rect
);
5176 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5179 m_headerWin
->GetPosition(&x
, &y
);
5180 rectHeader
.Offset(-x
, -y
);
5181 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5185 // Refresh the main window
5188 wxRect rectMain
= m_mainWin
->GetRect();
5189 rectMain
.Intersect(*rect
);
5190 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5193 m_mainWin
->GetPosition(&x
, &y
);
5194 rectMain
.Offset(-x
, -y
);
5195 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5201 #endif // wxUSE_LISTCTRL