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
)
2297 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2298 // shutdown the edit control when the mouse is clicked elsewhere on the
2299 // listctrl because the order of events is different (or something like
2300 // that), so explicitly end the edit if it is active.
2301 if ( event
.LeftDown() && m_textctrlWrapper
)
2302 m_textctrlWrapper
->EndEdit( false );
2305 if ( event
.LeftDown() )
2308 event
.SetEventObject( GetParent() );
2309 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2312 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2314 // let the base class handle mouse wheel events.
2319 if ( !HasCurrent() || IsEmpty() )
2321 if (event
.RightDown())
2323 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2325 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2326 GetParent()->GetId(),
2327 ClientToScreen(event
.GetPosition()));
2328 evtCtx
.SetEventObject(GetParent());
2329 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2337 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2338 event
.ButtonDClick()) )
2341 int x
= event
.GetX();
2342 int y
= event
.GetY();
2343 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2345 // where did we hit it (if we did)?
2348 size_t count
= GetItemCount(),
2351 if ( InReportView() )
2353 current
= y
/ GetLineHeight();
2354 if ( current
< count
)
2355 hitResult
= HitTestLine(current
, x
, y
);
2359 // TODO: optimize it too! this is less simple than for report view but
2360 // enumerating all items is still not a way to do it!!
2361 for ( current
= 0; current
< count
; current
++ )
2363 hitResult
= HitTestLine(current
, x
, y
);
2369 if (event
.Dragging())
2371 if (m_dragCount
== 0)
2373 // we have to report the raw, physical coords as we want to be
2374 // able to call HitTest(event.m_pointDrag) from the user code to
2375 // get the item being dragged
2376 m_dragStart
= event
.GetPosition();
2381 if (m_dragCount
!= 3)
2384 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2385 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2387 wxListEvent
le( command
, GetParent()->GetId() );
2388 le
.SetEventObject( GetParent() );
2389 le
.m_itemIndex
= m_lineLastClicked
;
2390 le
.m_pointDrag
= m_dragStart
;
2391 GetParent()->GetEventHandler()->ProcessEvent( le
);
2402 // outside of any item
2403 if (event
.RightDown())
2405 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2407 wxContextMenuEvent
evtCtx(
2409 GetParent()->GetId(),
2410 ClientToScreen(event
.GetPosition()));
2411 evtCtx
.SetEventObject(GetParent());
2412 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2416 // reset the selection and bail out
2417 HighlightAll(false);
2423 bool forceClick
= false;
2424 if (event
.ButtonDClick())
2426 if ( m_renameTimer
->IsRunning() )
2427 m_renameTimer
->Stop();
2429 m_lastOnSame
= false;
2431 if ( current
== m_lineLastClicked
)
2433 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2439 // The first click was on another item, so don't interpret this as
2440 // a double click, but as a simple click instead
2447 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2449 // select single line
2450 HighlightAll( false );
2451 ReverseHighlight(m_lineSelectSingleOnUp
);
2456 if ((current
== m_current
) &&
2457 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2458 HasFlag(wxLC_EDIT_LABELS
) )
2460 if ( !InReportView() ||
2461 GetLineLabelRect(current
).Contains(x
, y
) )
2463 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2464 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2469 m_lastOnSame
= false;
2470 m_lineSelectSingleOnUp
= (size_t)-1;
2474 // This is necessary, because after a DnD operation in
2475 // from and to ourself, the up event is swallowed by the
2476 // DnD code. So on next non-up event (which means here and
2477 // now) m_lineSelectSingleOnUp should be reset.
2478 m_lineSelectSingleOnUp
= (size_t)-1;
2480 if (event
.RightDown())
2482 m_lineBeforeLastClicked
= m_lineLastClicked
;
2483 m_lineLastClicked
= current
;
2485 // If the item is already selected, do not update the selection.
2486 // Multi-selections should not be cleared if a selected item is clicked.
2487 if (!IsHighlighted(current
))
2489 HighlightAll(false);
2490 ChangeCurrent(current
);
2491 ReverseHighlight(m_current
);
2494 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2496 // Allow generation of context menu event
2499 else if (event
.MiddleDown())
2501 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2503 else if ( event
.LeftDown() || forceClick
)
2505 m_lineBeforeLastClicked
= m_lineLastClicked
;
2506 m_lineLastClicked
= current
;
2508 size_t oldCurrent
= m_current
;
2509 bool oldWasSelected
= IsHighlighted(m_current
);
2511 bool cmdModifierDown
= event
.CmdDown();
2512 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2514 if ( IsSingleSel() || !IsHighlighted(current
) )
2516 HighlightAll( false );
2518 ChangeCurrent(current
);
2520 ReverseHighlight(m_current
);
2522 else // multi sel & current is highlighted & no mod keys
2524 m_lineSelectSingleOnUp
= current
;
2525 ChangeCurrent(current
); // change focus
2528 else // multi sel & either ctrl or shift is down
2530 if (cmdModifierDown
)
2532 ChangeCurrent(current
);
2534 ReverseHighlight(m_current
);
2536 else if (event
.ShiftDown())
2538 ChangeCurrent(current
);
2540 size_t lineFrom
= oldCurrent
,
2543 if ( lineTo
< lineFrom
)
2546 lineFrom
= m_current
;
2549 HighlightLines(lineFrom
, lineTo
);
2551 else // !ctrl, !shift
2553 // test in the enclosing if should make it impossible
2554 wxFAIL_MSG( _T("how did we get here?") );
2558 if (m_current
!= oldCurrent
)
2559 RefreshLine( oldCurrent
);
2561 // forceClick is only set if the previous click was on another item
2562 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2566 void wxListMainWindow::MoveToItem(size_t item
)
2568 if ( item
== (size_t)-1 )
2571 wxRect rect
= GetLineRect(item
);
2573 int client_w
, client_h
;
2574 GetClientSize( &client_w
, &client_h
);
2576 const int hLine
= GetLineHeight();
2578 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2579 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2581 if ( InReportView() )
2583 // the next we need the range of lines shown it might be different,
2584 // so recalculate it
2585 ResetVisibleLinesRange();
2587 if (rect
.y
< view_y
)
2588 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2589 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2590 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2593 // At least on Mac the visible lines value will get reset inside of
2594 // Scroll *before* it actually scrolls the window because of the
2595 // Update() that happens there, so it will still have the wrong value.
2596 // So let's reset it again and wait for it to be recalculated in the
2597 // next paint event. I would expect this problem to show up in wxGTK
2598 // too but couldn't duplicate it there. Perhaps the order of events
2599 // is different... --Robin
2600 ResetVisibleLinesRange();
2608 if (rect
.x
-view_x
< 5)
2609 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2610 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2611 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2613 if (rect
.y
-view_y
< 5)
2614 sy
= (rect
.y
- 5) / hLine
;
2615 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2616 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2618 GetListCtrl()->Scroll(sx
, sy
);
2622 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2624 if ( !InReportView() )
2626 // TODO: this should work in all views but is not implemented now
2631 GetVisibleLinesRange(&top
, &bottom
);
2633 if ( bottom
== (size_t)-1 )
2636 ResetVisibleLinesRange();
2638 int hLine
= GetLineHeight();
2640 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2643 // see comment in MoveToItem() for why we do this
2644 ResetVisibleLinesRange();
2650 // ----------------------------------------------------------------------------
2651 // keyboard handling
2652 // ----------------------------------------------------------------------------
2654 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2656 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2657 _T("invalid item index in OnArrowChar()") );
2659 size_t oldCurrent
= m_current
;
2661 // in single selection we just ignore Shift as we can't select several
2663 if ( event
.ShiftDown() && !IsSingleSel() )
2665 ChangeCurrent(newCurrent
);
2667 // refresh the old focus to remove it
2668 RefreshLine( oldCurrent
);
2670 // select all the items between the old and the new one
2671 if ( oldCurrent
> newCurrent
)
2673 newCurrent
= oldCurrent
;
2674 oldCurrent
= m_current
;
2677 HighlightLines(oldCurrent
, newCurrent
);
2681 // all previously selected items are unselected unless ctrl is held
2682 // in a multiselection control
2683 if ( !event
.ControlDown() || IsSingleSel() )
2684 HighlightAll(false);
2686 ChangeCurrent(newCurrent
);
2688 // refresh the old focus to remove it
2689 RefreshLine( oldCurrent
);
2691 // in single selection mode we must always have a selected item
2692 if ( !event
.ControlDown() || IsSingleSel() )
2693 HighlightLine( m_current
, true );
2696 RefreshLine( m_current
);
2701 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2703 wxWindow
*parent
= GetParent();
2705 // propagate the key event upwards
2706 wxKeyEvent
ke(event
);
2707 ke
.SetEventObject( parent
);
2708 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2714 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2716 wxWindow
*parent
= GetParent();
2718 // propagate the key event upwards
2719 wxKeyEvent
ke(event
);
2720 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2726 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2728 wxWindow
*parent
= GetParent();
2730 // send a list_key event up
2733 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2734 le
.m_itemIndex
= m_current
;
2735 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2736 le
.m_code
= event
.GetKeyCode();
2737 le
.SetEventObject( parent
);
2738 parent
->GetEventHandler()->ProcessEvent( le
);
2741 if ( (event
.GetKeyCode() != WXK_UP
) &&
2742 (event
.GetKeyCode() != WXK_DOWN
) &&
2743 (event
.GetKeyCode() != WXK_RIGHT
) &&
2744 (event
.GetKeyCode() != WXK_LEFT
) &&
2745 (event
.GetKeyCode() != WXK_PAGEUP
) &&
2746 (event
.GetKeyCode() != WXK_PAGEDOWN
) &&
2747 (event
.GetKeyCode() != WXK_END
) &&
2748 (event
.GetKeyCode() != WXK_HOME
) )
2750 // propagate the char event upwards
2751 wxKeyEvent
ke(event
);
2752 ke
.SetEventObject( parent
);
2753 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2757 if ( HandleAsNavigationKey(event
) )
2760 // no item -> nothing to do
2767 // don't use m_linesPerPage directly as it might not be computed yet
2768 const int pageSize
= GetCountPerPage();
2769 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
2771 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2773 if (event
.GetKeyCode() == WXK_RIGHT
)
2774 event
.m_keyCode
= WXK_LEFT
;
2775 else if (event
.GetKeyCode() == WXK_LEFT
)
2776 event
.m_keyCode
= WXK_RIGHT
;
2779 switch ( event
.GetKeyCode() )
2782 if ( m_current
> 0 )
2783 OnArrowChar( m_current
- 1, event
);
2787 if ( m_current
< (size_t)GetItemCount() - 1 )
2788 OnArrowChar( m_current
+ 1, event
);
2793 OnArrowChar( GetItemCount() - 1, event
);
2798 OnArrowChar( 0, event
);
2803 int steps
= InReportView() ? pageSize
- 1
2804 : m_current
% pageSize
;
2806 int index
= m_current
- steps
;
2810 OnArrowChar( index
, event
);
2816 int steps
= InReportView()
2818 : pageSize
- (m_current
% pageSize
) - 1;
2820 size_t index
= m_current
+ steps
;
2821 size_t count
= GetItemCount();
2822 if ( index
>= count
)
2825 OnArrowChar( index
, event
);
2830 if ( !InReportView() )
2832 int index
= m_current
- pageSize
;
2836 OnArrowChar( index
, event
);
2841 if ( !InReportView() )
2843 size_t index
= m_current
+ pageSize
;
2845 size_t count
= GetItemCount();
2846 if ( index
>= count
)
2849 OnArrowChar( index
, event
);
2854 if ( IsSingleSel() )
2856 if ( event
.ControlDown() )
2858 ReverseHighlight(m_current
);
2860 else // normal space press
2862 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2865 else // multiple selection
2867 ReverseHighlight(m_current
);
2873 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2881 // ----------------------------------------------------------------------------
2883 // ----------------------------------------------------------------------------
2885 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2889 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2890 event
.SetEventObject( GetParent() );
2891 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2895 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2896 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2897 // which are already drawn correctly resulting in horrible flicker - avoid
2907 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2911 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2912 event
.SetEventObject( GetParent() );
2913 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2921 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2923 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2925 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2927 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2929 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2931 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2933 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2935 else if ( InReportView() && (m_small_image_list
))
2937 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2941 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2943 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2945 m_normal_image_list
->GetSize( index
, width
, height
);
2947 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2949 m_small_image_list
->GetSize( index
, width
, height
);
2951 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2953 m_small_image_list
->GetSize( index
, width
, height
);
2955 else if ( InReportView() && m_small_image_list
)
2957 m_small_image_list
->GetSize( index
, width
, height
);
2966 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2968 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2969 dc
.SetFont( GetFont() );
2972 dc
.GetTextExtent( s
, &lw
, NULL
);
2974 return lw
+ AUTOSIZE_COL_MARGIN
;
2977 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2981 // calc the spacing from the icon size
2982 int width
= 0, height
= 0;
2984 if ((imageList
) && (imageList
->GetImageCount()) )
2985 imageList
->GetSize(0, width
, height
);
2987 if (which
== wxIMAGE_LIST_NORMAL
)
2989 m_normal_image_list
= imageList
;
2990 m_normal_spacing
= width
+ 8;
2993 if (which
== wxIMAGE_LIST_SMALL
)
2995 m_small_image_list
= imageList
;
2996 m_small_spacing
= width
+ 14;
2997 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3001 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3005 m_small_spacing
= spacing
;
3007 m_normal_spacing
= spacing
;
3010 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3012 return isSmall
? m_small_spacing
: m_normal_spacing
;
3015 // ----------------------------------------------------------------------------
3017 // ----------------------------------------------------------------------------
3019 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3021 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3023 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3025 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3026 item
.m_width
= GetTextLength( item
.m_text
);
3028 wxListHeaderData
*column
= node
->GetData();
3029 column
->SetItem( item
);
3031 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3033 headerWin
->m_dirty
= true;
3037 // invalidate it as it has to be recalculated
3041 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3043 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3044 _T("invalid column index") );
3046 wxCHECK_RET( InReportView(),
3047 _T("SetColumnWidth() can only be called in report mode.") );
3051 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3053 headerWin
->m_dirty
= true;
3055 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3056 wxCHECK_RET( node
, _T("no column?") );
3058 wxListHeaderData
*column
= node
->GetData();
3060 size_t count
= GetItemCount();
3062 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3064 width
= GetTextLength(column
->GetText());
3065 width
+= 2*EXTRA_WIDTH
;
3067 // check for column header's image availability
3068 const int image
= column
->GetImage();
3071 if ( m_small_image_list
)
3074 m_small_image_list
->GetSize(image
, ix
, iy
);
3075 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3079 else if ( width
== wxLIST_AUTOSIZE
)
3083 // TODO: determine the max width somehow...
3084 width
= WIDTH_COL_DEFAULT
;
3088 wxClientDC
dc(this);
3089 dc
.SetFont( GetFont() );
3091 int max
= AUTOSIZE_COL_MARGIN
;
3093 // if the cached column width isn't valid then recalculate it
3094 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3096 for (size_t i
= 0; i
< count
; i
++)
3098 wxListLineData
*line
= GetLine( i
);
3099 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3101 wxCHECK_RET( n
, _T("no subitem?") );
3103 wxListItemData
*itemData
= n
->GetData();
3106 itemData
->GetItem(item
);
3107 int itemWidth
= GetItemWidthWithImage(&item
);
3108 if (itemWidth
> max
)
3112 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3113 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3116 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3117 width
= max
+ AUTOSIZE_COL_MARGIN
;
3121 column
->SetWidth( width
);
3123 // invalidate it as it has to be recalculated
3127 int wxListMainWindow::GetHeaderWidth() const
3129 if ( !m_headerWidth
)
3131 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3133 size_t count
= GetColumnCount();
3134 for ( size_t col
= 0; col
< count
; col
++ )
3136 self
->m_headerWidth
+= GetColumnWidth(col
);
3140 return m_headerWidth
;
3143 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3145 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3146 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3148 wxListHeaderData
*column
= node
->GetData();
3149 column
->GetItem( item
);
3152 int wxListMainWindow::GetColumnWidth( int col
) const
3154 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3155 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3157 wxListHeaderData
*column
= node
->GetData();
3158 return column
->GetWidth();
3161 // ----------------------------------------------------------------------------
3163 // ----------------------------------------------------------------------------
3165 void wxListMainWindow::SetItem( wxListItem
&item
)
3167 long id
= item
.m_itemId
;
3168 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3169 _T("invalid item index in SetItem") );
3173 wxListLineData
*line
= GetLine((size_t)id
);
3174 line
->SetItem( item
.m_col
, item
);
3176 // Set item state if user wants
3177 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3178 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3182 // update the Max Width Cache if needed
3183 int width
= GetItemWidthWithImage(&item
);
3185 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3186 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3190 // update the item on screen
3192 GetItemRect(id
, rectItem
);
3193 RefreshRect(rectItem
);
3196 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3201 // first deal with selection
3202 if ( stateMask
& wxLIST_STATE_SELECTED
)
3204 // set/clear select state
3207 // optimized version for virtual listctrl.
3208 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3211 else if ( state
& wxLIST_STATE_SELECTED
)
3213 const long count
= GetItemCount();
3214 for( long i
= 0; i
< count
; i
++ )
3216 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3222 // clear for non virtual (somewhat optimized by using GetNextItem())
3224 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3226 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3231 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3233 // unfocus all: only one item can be focussed, so clearing focus for
3234 // all items is simply clearing focus of the focussed item.
3235 SetItemState(m_current
, state
, stateMask
);
3237 //(setting focus to all items makes no sense, so it is not handled here.)
3240 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3244 SetItemStateAll(state
, stateMask
);
3248 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3249 _T("invalid list ctrl item index in SetItem") );
3251 size_t oldCurrent
= m_current
;
3252 size_t item
= (size_t)litem
; // safe because of the check above
3254 // do we need to change the focus?
3255 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3257 if ( state
& wxLIST_STATE_FOCUSED
)
3259 // don't do anything if this item is already focused
3260 if ( item
!= m_current
)
3262 ChangeCurrent(item
);
3264 if ( oldCurrent
!= (size_t)-1 )
3266 if ( IsSingleSel() )
3268 HighlightLine(oldCurrent
, false);
3271 RefreshLine(oldCurrent
);
3274 RefreshLine( m_current
);
3279 // don't do anything if this item is not focused
3280 if ( item
== m_current
)
3284 if ( IsSingleSel() )
3286 // we must unselect the old current item as well or we
3287 // might end up with more than one selected item in a
3288 // single selection control
3289 HighlightLine(oldCurrent
, false);
3292 RefreshLine( oldCurrent
);
3297 // do we need to change the selection state?
3298 if ( stateMask
& wxLIST_STATE_SELECTED
)
3300 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3302 if ( IsSingleSel() )
3306 // selecting the item also makes it the focused one in the
3308 if ( m_current
!= item
)
3310 ChangeCurrent(item
);
3312 if ( oldCurrent
!= (size_t)-1 )
3314 HighlightLine( oldCurrent
, false );
3315 RefreshLine( oldCurrent
);
3321 // only the current item may be selected anyhow
3322 if ( item
!= m_current
)
3327 if ( HighlightLine(item
, on
) )
3334 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3336 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3337 _T("invalid list ctrl item index in GetItemState()") );
3339 int ret
= wxLIST_STATE_DONTCARE
;
3341 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3343 if ( (size_t)item
== m_current
)
3344 ret
|= wxLIST_STATE_FOCUSED
;
3347 if ( stateMask
& wxLIST_STATE_SELECTED
)
3349 if ( IsHighlighted(item
) )
3350 ret
|= wxLIST_STATE_SELECTED
;
3356 void wxListMainWindow::GetItem( wxListItem
&item
) const
3358 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3359 _T("invalid item index in GetItem") );
3361 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3362 line
->GetItem( item
.m_col
, item
);
3364 // Get item state if user wants it
3365 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3366 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3367 wxLIST_STATE_FOCUSED
);
3370 // ----------------------------------------------------------------------------
3372 // ----------------------------------------------------------------------------
3374 size_t wxListMainWindow::GetItemCount() const
3376 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3379 void wxListMainWindow::SetItemCount(long count
)
3381 m_selStore
.SetItemCount(count
);
3382 m_countVirt
= count
;
3384 ResetVisibleLinesRange();
3386 // scrollbars must be reset
3390 int wxListMainWindow::GetSelectedItemCount() const
3392 // deal with the quick case first
3393 if ( IsSingleSel() )
3394 return HasCurrent() ? IsHighlighted(m_current
) : false;
3396 // virtual controls remmebers all its selections itself
3398 return m_selStore
.GetSelectedCount();
3400 // TODO: we probably should maintain the number of items selected even for
3401 // non virtual controls as enumerating all lines is really slow...
3402 size_t countSel
= 0;
3403 size_t count
= GetItemCount();
3404 for ( size_t line
= 0; line
< count
; line
++ )
3406 if ( GetLine(line
)->IsHighlighted() )
3413 // ----------------------------------------------------------------------------
3414 // item position/size
3415 // ----------------------------------------------------------------------------
3417 wxRect
wxListMainWindow::GetViewRect() const
3419 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3421 // we need to find the longest/tallest label
3422 wxCoord xMax
= 0, yMax
= 0;
3423 const int count
= GetItemCount();
3426 for ( int i
= 0; i
< count
; i
++ )
3428 // we need logical, not physical, coordinates here, so use
3429 // GetLineRect() instead of GetItemRect()
3430 wxRect r
= GetLineRect(i
);
3432 wxCoord x
= r
.GetRight(),
3442 // some fudge needed to make it look prettier
3443 xMax
+= 2 * EXTRA_BORDER_X
;
3444 yMax
+= 2 * EXTRA_BORDER_Y
;
3446 // account for the scrollbars if necessary
3447 const wxSize sizeAll
= GetClientSize();
3448 if ( xMax
> sizeAll
.x
)
3449 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3450 if ( yMax
> sizeAll
.y
)
3451 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3453 return wxRect(0, 0, xMax
, yMax
);
3457 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3459 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3461 _T("GetSubItemRect only meaningful in report view") );
3462 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3463 _T("invalid item in GetSubItemRect") );
3465 // ensure that we're laid out, otherwise we could return nonsense
3468 wxConstCast(this, wxListMainWindow
)->
3469 RecalculatePositions(true /* no refresh */);
3472 rect
= GetLineRect((size_t)item
);
3474 // Adjust rect to specified column
3475 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3477 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3478 _T("invalid subItem in GetSubItemRect") );
3480 for (int i
= 0; i
< subItem
; i
++)
3482 rect
.x
+= GetColumnWidth(i
);
3484 rect
.width
= GetColumnWidth(subItem
);
3487 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3492 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3495 GetItemRect(item
, rect
);
3503 // ----------------------------------------------------------------------------
3504 // geometry calculation
3505 // ----------------------------------------------------------------------------
3507 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3509 const int lineHeight
= GetLineHeight();
3511 wxClientDC
dc( this );
3512 dc
.SetFont( GetFont() );
3514 const size_t count
= GetItemCount();
3517 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3518 iconSpacing
= m_normal_spacing
;
3519 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3520 iconSpacing
= m_small_spacing
;
3524 // Note that we do not call GetClientSize() here but
3525 // GetSize() and subtract the border size for sunken
3526 // borders manually. This is technically incorrect,
3527 // but we need to know the client area's size WITHOUT
3528 // scrollbars here. Since we don't know if there are
3529 // any scrollbars, we use GetSize() instead. Another
3530 // solution would be to call SetScrollbars() here to
3531 // remove the scrollbars and call GetClientSize() then,
3532 // but this might result in flicker and - worse - will
3533 // reset the scrollbars to 0 which is not good at all
3534 // if you resize a dialog/window, but don't want to
3535 // reset the window scrolling. RR.
3536 // Furthermore, we actually do NOT subtract the border
3537 // width as 2 pixels is just the extra space which we
3538 // need around the actual content in the window. Other-
3539 // wise the text would e.g. touch the upper border. RR.
3542 GetSize( &clientWidth
, &clientHeight
);
3544 if ( InReportView() )
3546 // all lines have the same height and we scroll one line per step
3547 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3549 m_linesPerPage
= clientHeight
/ lineHeight
;
3551 ResetVisibleLinesRange();
3553 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3554 GetHeaderWidth() / SCROLL_UNIT_X
,
3555 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3556 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3557 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3562 // we have 3 different layout strategies: either layout all items
3563 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3564 // to arrange them in top to bottom, left to right (don't ask me why
3565 // not the other way round...) order
3566 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3568 int x
= EXTRA_BORDER_X
;
3569 int y
= EXTRA_BORDER_Y
;
3571 wxCoord widthMax
= 0;
3574 for ( i
= 0; i
< count
; i
++ )
3576 wxListLineData
*line
= GetLine(i
);
3577 line
->CalculateSize( &dc
, iconSpacing
);
3578 line
->SetPosition( x
, y
, iconSpacing
);
3580 wxSize sizeLine
= GetLineSize(i
);
3582 if ( HasFlag(wxLC_ALIGN_TOP
) )
3584 if ( sizeLine
.x
> widthMax
)
3585 widthMax
= sizeLine
.x
;
3589 else // wxLC_ALIGN_LEFT
3591 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3595 if ( HasFlag(wxLC_ALIGN_TOP
) )
3597 // traverse the items again and tweak their sizes so that they are
3598 // all the same in a row
3599 for ( i
= 0; i
< count
; i
++ )
3601 wxListLineData
*line
= GetLine(i
);
3602 line
->m_gi
->ExtendWidth(widthMax
);
3606 GetListCtrl()->SetScrollbars
3610 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3611 (y
+ lineHeight
) / lineHeight
,
3612 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3613 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3617 else // "flowed" arrangement, the most complicated case
3619 // at first we try without any scrollbars, if the items don't fit into
3620 // the window, we recalculate after subtracting the space taken by the
3623 int entireWidth
= 0;
3625 for (int tries
= 0; tries
< 2; tries
++)
3627 entireWidth
= 2 * EXTRA_BORDER_X
;
3631 // Now we have decided that the items do not fit into the
3632 // client area, so we need a scrollbar
3633 entireWidth
+= SCROLL_UNIT_X
;
3636 int x
= EXTRA_BORDER_X
;
3637 int y
= EXTRA_BORDER_Y
;
3638 int maxWidthInThisRow
= 0;
3641 int currentlyVisibleLines
= 0;
3643 for (size_t i
= 0; i
< count
; i
++)
3645 currentlyVisibleLines
++;
3646 wxListLineData
*line
= GetLine( i
);
3647 line
->CalculateSize( &dc
, iconSpacing
);
3648 line
->SetPosition( x
, y
, iconSpacing
);
3650 wxSize sizeLine
= GetLineSize( i
);
3652 if ( maxWidthInThisRow
< sizeLine
.x
)
3653 maxWidthInThisRow
= sizeLine
.x
;
3656 if (currentlyVisibleLines
> m_linesPerPage
)
3657 m_linesPerPage
= currentlyVisibleLines
;
3659 if ( y
+ sizeLine
.y
>= clientHeight
)
3661 currentlyVisibleLines
= 0;
3663 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3664 x
+= maxWidthInThisRow
;
3665 entireWidth
+= maxWidthInThisRow
;
3666 maxWidthInThisRow
= 0;
3669 // We have reached the last item.
3670 if ( i
== count
- 1 )
3671 entireWidth
+= maxWidthInThisRow
;
3673 if ( (tries
== 0) &&
3674 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3676 clientHeight
-= wxSystemSettings::
3677 GetMetric(wxSYS_HSCROLL_Y
);
3682 if ( i
== count
- 1 )
3683 tries
= 1; // Everything fits, no second try required.
3687 GetListCtrl()->SetScrollbars
3691 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3693 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3702 // FIXME: why should we call it from here?
3709 void wxListMainWindow::RefreshAll()
3714 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3715 if ( headerWin
&& headerWin
->m_dirty
)
3717 headerWin
->m_dirty
= false;
3718 headerWin
->Refresh();
3722 void wxListMainWindow::UpdateCurrent()
3724 if ( !HasCurrent() && !IsEmpty() )
3728 long wxListMainWindow::GetNextItem( long item
,
3729 int WXUNUSED(geometry
),
3733 max
= GetItemCount();
3734 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3735 _T("invalid listctrl index in GetNextItem()") );
3737 // notice that we start with the next item (or the first one if item == -1)
3738 // and this is intentional to allow writing a simple loop to iterate over
3739 // all selected items
3742 // this is not an error because the index was OK initially,
3743 // just no such item
3750 size_t count
= GetItemCount();
3751 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3753 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3756 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3763 // ----------------------------------------------------------------------------
3765 // ----------------------------------------------------------------------------
3767 void wxListMainWindow::DeleteItem( long lindex
)
3769 size_t count
= GetItemCount();
3771 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3772 _T("invalid item index in DeleteItem") );
3774 size_t index
= (size_t)lindex
;
3776 // we don't need to adjust the index for the previous items
3777 if ( HasCurrent() && m_current
>= index
)
3779 // if the current item is being deleted, we want the next one to
3780 // become selected - unless there is no next one - so don't adjust
3781 // m_current in this case
3782 if ( m_current
!= index
|| m_current
== count
- 1 )
3786 if ( InReportView() )
3788 // mark the Column Max Width cache as dirty if the items in the line
3789 // we're deleting contain the Max Column Width
3790 wxListLineData
* const line
= GetLine(index
);
3791 wxListItemDataList::compatibility_iterator n
;
3792 wxListItemData
*itemData
;
3796 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3798 n
= line
->m_items
.Item( i
);
3799 itemData
= n
->GetData();
3800 itemData
->GetItem(item
);
3802 itemWidth
= GetItemWidthWithImage(&item
);
3804 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3805 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3808 ResetVisibleLinesRange();
3811 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3816 m_selStore
.OnItemDelete(index
);
3820 m_lines
.RemoveAt( index
);
3823 // we need to refresh the (vert) scrollbar as the number of items changed
3826 RefreshAfter(index
);
3829 void wxListMainWindow::DeleteColumn( int col
)
3831 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3833 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3836 delete node
->GetData();
3837 m_columns
.Erase( node
);
3841 // update all the items
3842 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3844 wxListLineData
* const line
= GetLine(i
);
3845 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3846 delete n
->GetData();
3847 line
->m_items
.Erase(n
);
3851 if ( InReportView() ) // we only cache max widths when in Report View
3853 delete m_aColWidths
.Item(col
);
3854 m_aColWidths
.RemoveAt(col
);
3857 // invalidate it as it has to be recalculated
3861 void wxListMainWindow::DoDeleteAllItems()
3864 // nothing to do - in particular, don't send the event
3869 // to make the deletion of all items faster, we don't send the
3870 // notifications for each item deletion in this case but only one event
3871 // for all of them: this is compatible with wxMSW and documented in
3872 // DeleteAllItems() description
3874 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3875 event
.SetEventObject( GetParent() );
3876 GetParent()->GetEventHandler()->ProcessEvent( event
);
3884 if ( InReportView() )
3886 ResetVisibleLinesRange();
3887 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3889 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3896 void wxListMainWindow::DeleteAllItems()
3900 RecalculatePositions();
3903 void wxListMainWindow::DeleteEverything()
3905 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3906 WX_CLEAR_ARRAY(m_aColWidths
);
3911 // ----------------------------------------------------------------------------
3912 // scanning for an item
3913 // ----------------------------------------------------------------------------
3915 void wxListMainWindow::EnsureVisible( long index
)
3917 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3918 _T("invalid index in EnsureVisible") );
3920 // We have to call this here because the label in question might just have
3921 // been added and its position is not known yet
3923 RecalculatePositions(true /* no refresh */);
3925 MoveToItem((size_t)index
);
3928 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3934 wxString str_upper
= str
.Upper();
3938 size_t count
= GetItemCount();
3939 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3941 wxListLineData
*line
= GetLine(i
);
3942 wxString line_upper
= line
->GetText(0).Upper();
3945 if (line_upper
== str_upper
)
3950 if (line_upper
.find(str_upper
) == 0)
3958 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3964 size_t count
= GetItemCount();
3965 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3967 wxListLineData
*line
= GetLine(i
);
3969 line
->GetItem( 0, item
);
3970 if (item
.m_data
== data
)
3977 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3980 GetVisibleLinesRange( &topItem
, NULL
);
3983 GetItemPosition( GetItemCount() - 1, p
);
3987 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3988 if ( id
>= 0 && id
< (long)GetItemCount() )
3994 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
3996 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3998 size_t count
= GetItemCount();
4000 if ( InReportView() )
4002 size_t current
= y
/ GetLineHeight();
4003 if ( current
< count
)
4005 flags
= HitTestLine(current
, x
, y
);
4012 // TODO: optimize it too! this is less simple than for report view but
4013 // enumerating all items is still not a way to do it!!
4014 for ( size_t current
= 0; current
< count
; current
++ )
4016 flags
= HitTestLine(current
, x
, y
);
4025 // ----------------------------------------------------------------------------
4027 // ----------------------------------------------------------------------------
4029 void wxListMainWindow::InsertItem( wxListItem
&item
)
4031 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4033 int count
= GetItemCount();
4034 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4036 if (item
.m_itemId
> count
)
4037 item
.m_itemId
= count
;
4039 size_t id
= item
.m_itemId
;
4043 if ( InReportView() )
4045 ResetVisibleLinesRange();
4047 // calculate the width of the item and adjust the max column width
4048 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4049 int width
= GetItemWidthWithImage(&item
);
4050 item
.SetWidth(width
);
4051 if (width
> pWidthInfo
->nMaxWidth
)
4052 pWidthInfo
->nMaxWidth
= width
;
4055 wxListLineData
*line
= new wxListLineData(this);
4057 line
->SetItem( item
.m_col
, item
);
4059 m_lines
.Insert( line
, id
);
4063 // If an item is selected at or below the point of insertion, we need to
4064 // increment the member variables because the current row's index has gone
4066 if ( HasCurrent() && m_current
>= id
)
4069 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4071 RefreshLines(id
, GetItemCount() - 1);
4074 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4077 if ( InReportView() )
4079 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4080 item
.m_width
= GetTextLength( item
.m_text
);
4082 wxListHeaderData
*column
= new wxListHeaderData( item
);
4083 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4085 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4088 wxListHeaderDataList::compatibility_iterator
4089 node
= m_columns
.Item( col
);
4090 m_columns
.Insert( node
, column
);
4091 m_aColWidths
.Insert( colWidthInfo
, col
);
4095 m_columns
.Append( column
);
4096 m_aColWidths
.Add( colWidthInfo
);
4101 // update all the items
4102 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4104 wxListLineData
* const line
= GetLine(i
);
4105 wxListItemData
* const data
= new wxListItemData(this);
4107 line
->m_items
.Insert(col
, data
);
4109 line
->m_items
.Append(data
);
4113 // invalidate it as it has to be recalculated
4118 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4121 wxClientDC
dc(this);
4123 dc
.SetFont( GetFont() );
4125 if (item
->GetImage() != -1)
4128 GetImageSize( item
->GetImage(), ix
, iy
);
4132 if (!item
->GetText().empty())
4135 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4142 // ----------------------------------------------------------------------------
4144 // ----------------------------------------------------------------------------
4146 static wxListCtrlCompare list_ctrl_compare_func_2
;
4147 static long list_ctrl_compare_data
;
4149 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4151 wxListLineData
*line1
= *arg1
;
4152 wxListLineData
*line2
= *arg2
;
4154 line1
->GetItem( 0, item
);
4155 wxUIntPtr data1
= item
.m_data
;
4156 line2
->GetItem( 0, item
);
4157 wxUIntPtr data2
= item
.m_data
;
4158 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4161 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4163 // selections won't make sense any more after sorting the items so reset
4165 HighlightAll(false);
4168 list_ctrl_compare_func_2
= fn
;
4169 list_ctrl_compare_data
= data
;
4170 m_lines
.Sort( list_ctrl_compare_func_1
);
4174 // ----------------------------------------------------------------------------
4176 // ----------------------------------------------------------------------------
4178 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4180 // update our idea of which lines are shown when we redraw the window the
4182 ResetVisibleLinesRange();
4184 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4186 wxGenericListCtrl
* lc
= GetListCtrl();
4187 wxCHECK_RET( lc
, _T("no listctrl window?") );
4189 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4191 lc
->m_headerWin
->Refresh();
4192 lc
->m_headerWin
->Update();
4197 int wxListMainWindow::GetCountPerPage() const
4199 if ( !m_linesPerPage
)
4201 wxConstCast(this, wxListMainWindow
)->
4202 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4205 return m_linesPerPage
;
4208 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4210 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4212 if ( m_lineFrom
== (size_t)-1 )
4214 size_t count
= GetItemCount();
4217 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4219 // this may happen if SetScrollbars() hadn't been called yet
4220 if ( m_lineFrom
>= count
)
4221 m_lineFrom
= count
- 1;
4223 // we redraw one extra line but this is needed to make the redrawing
4224 // logic work when there is a fractional number of lines on screen
4225 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4226 if ( m_lineTo
>= count
)
4227 m_lineTo
= count
- 1;
4229 else // empty control
4232 m_lineTo
= (size_t)-1;
4236 wxASSERT_MSG( IsEmpty() ||
4237 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4238 _T("GetVisibleLinesRange() returns incorrect result") );
4246 // -------------------------------------------------------------------------------------
4247 // wxGenericListCtrl
4248 // -------------------------------------------------------------------------------------
4250 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4252 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4253 EVT_SIZE(wxGenericListCtrl::OnSize
)
4254 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4257 void wxGenericListCtrl::Init()
4259 m_imageListNormal
= NULL
;
4260 m_imageListSmall
= NULL
;
4261 m_imageListState
= NULL
;
4263 m_ownsImageListNormal
=
4264 m_ownsImageListSmall
=
4265 m_ownsImageListState
= false;
4269 m_headerHeight
= wxRendererNative::Get().GetHeaderButtonHeight(this);
4272 wxGenericListCtrl::~wxGenericListCtrl()
4274 if (m_ownsImageListNormal
)
4275 delete m_imageListNormal
;
4276 if (m_ownsImageListSmall
)
4277 delete m_imageListSmall
;
4278 if (m_ownsImageListState
)
4279 delete m_imageListState
;
4282 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4284 bool needs_header
= HasHeader();
4285 bool has_header
= (m_headerWin
!= NULL
);
4287 if (needs_header
== has_header
)
4292 m_headerWin
= new wxListHeaderWindow
4294 this, wxID_ANY
, m_mainWin
,
4296 wxSize(GetClientSize().x
, m_headerHeight
),
4300 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
4302 #if wxOSX_USE_ATSU_TEXT
4303 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
4305 font
.MacCreateFromUIFont( kCTFontSystemFontType
);
4307 m_headerWin
->SetFont( font
);
4310 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4314 GetSizer()->Detach( m_headerWin
);
4322 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4327 const wxValidator
&validator
,
4328 const wxString
&name
)
4332 // just like in other ports, an assert will fail if the user doesn't give any type style:
4333 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4334 _T("wxListCtrl style should have exactly one mode bit set") );
4336 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4340 style
&= ~wxBORDER_MASK
;
4341 style
|= wxBORDER_THEME
;
4344 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4346 SetTargetWindow( m_mainWin
);
4348 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4349 sizer
->Add( m_mainWin
, 1, wxGROW
);
4352 CreateOrDestroyHeaderWindowAsNeeded();
4354 SetInitialSize(size
);
4359 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4361 return wxBORDER_THEME
;
4365 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4369 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4372 // we need to process arrows ourselves for scrolling
4373 if ( nMsg
== WM_GETDLGCODE
)
4375 rc
|= DLGC_WANTARROWS
;
4383 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4385 wxSize newsize
= size
;
4387 newsize
.y
-= m_headerWin
->GetSize().y
;
4392 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4394 // update our idea of which lines are shown when we redraw
4395 // the window the next time
4396 m_mainWin
->ResetVisibleLinesRange();
4398 HandleOnScroll( event
);
4400 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4402 m_headerWin
->Refresh();
4403 m_headerWin
->Update();
4407 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4409 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4410 _T("wxLC_VIRTUAL can't be [un]set") );
4412 long flag
= GetWindowStyle();
4416 if (style
& wxLC_MASK_TYPE
)
4417 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4418 if (style
& wxLC_MASK_ALIGN
)
4419 flag
&= ~wxLC_MASK_ALIGN
;
4420 if (style
& wxLC_MASK_SORT
)
4421 flag
&= ~wxLC_MASK_SORT
;
4429 // some styles can be set without recreating everything (as happens in
4430 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4431 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4434 wxWindow::SetWindowStyleFlag(flag
);
4438 SetWindowStyleFlag( flag
);
4442 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4446 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4448 CreateOrDestroyHeaderWindowAsNeeded();
4450 GetSizer()->Layout();
4453 wxWindow::SetWindowStyleFlag( flag
);
4456 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4458 m_mainWin
->GetColumn( col
, item
);
4462 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4464 m_mainWin
->SetColumn( col
, item
);
4468 int wxGenericListCtrl::GetColumnWidth( int col
) const
4470 return m_mainWin
->GetColumnWidth( col
);
4473 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4475 m_mainWin
->SetColumnWidth( col
, width
);
4479 int wxGenericListCtrl::GetCountPerPage() const
4481 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4484 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4486 m_mainWin
->GetItem( info
);
4490 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4492 m_mainWin
->SetItem( info
);
4496 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4499 info
.m_text
= label
;
4500 info
.m_mask
= wxLIST_MASK_TEXT
;
4501 info
.m_itemId
= index
;
4505 info
.m_image
= imageId
;
4506 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4509 m_mainWin
->SetItem(info
);
4513 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4515 return m_mainWin
->GetItemState( item
, stateMask
);
4518 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4520 m_mainWin
->SetItemState( item
, state
, stateMask
);
4525 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4527 return SetItemColumnImage(item
, 0, image
);
4531 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4534 info
.m_image
= image
;
4535 info
.m_mask
= wxLIST_MASK_IMAGE
;
4536 info
.m_itemId
= item
;
4537 info
.m_col
= column
;
4538 m_mainWin
->SetItem( info
);
4542 wxString
wxGenericListCtrl::GetItemText( long item
) const
4544 return m_mainWin
->GetItemText(item
);
4547 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4549 m_mainWin
->SetItemText(item
, str
);
4552 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4555 info
.m_mask
= wxLIST_MASK_DATA
;
4556 info
.m_itemId
= item
;
4557 m_mainWin
->GetItem( info
);
4561 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4564 info
.m_mask
= wxLIST_MASK_DATA
;
4565 info
.m_itemId
= item
;
4567 m_mainWin
->SetItem( info
);
4571 wxRect
wxGenericListCtrl::GetViewRect() const
4573 return m_mainWin
->GetViewRect();
4576 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4578 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4581 bool wxGenericListCtrl::GetSubItemRect(long item
,
4584 int WXUNUSED(code
)) const
4586 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4589 if ( m_mainWin
->HasHeader() )
4590 rect
.y
+= m_headerHeight
+ 1;
4595 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4597 m_mainWin
->GetItemPosition( item
, pos
);
4601 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4606 int wxGenericListCtrl::GetItemCount() const
4608 return m_mainWin
->GetItemCount();
4611 int wxGenericListCtrl::GetColumnCount() const
4613 return m_mainWin
->GetColumnCount();
4616 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4618 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4621 wxSize
wxGenericListCtrl::GetItemSpacing() const
4623 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4625 return wxSize(spacing
, spacing
);
4628 #if WXWIN_COMPATIBILITY_2_6
4629 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4631 return m_mainWin
->GetItemSpacing( isSmall
);
4633 #endif // WXWIN_COMPATIBILITY_2_6
4635 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4638 info
.m_itemId
= item
;
4639 info
.SetTextColour( col
);
4640 m_mainWin
->SetItem( info
);
4643 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4646 info
.m_itemId
= item
;
4647 m_mainWin
->GetItem( info
);
4648 return info
.GetTextColour();
4651 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4654 info
.m_itemId
= item
;
4655 info
.SetBackgroundColour( col
);
4656 m_mainWin
->SetItem( info
);
4659 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4662 info
.m_itemId
= item
;
4663 m_mainWin
->GetItem( info
);
4664 return info
.GetBackgroundColour();
4667 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4670 info
.m_itemId
= item
;
4672 m_mainWin
->SetItem( info
);
4675 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4678 info
.m_itemId
= item
;
4679 m_mainWin
->GetItem( info
);
4680 return info
.GetFont();
4683 int wxGenericListCtrl::GetSelectedItemCount() const
4685 return m_mainWin
->GetSelectedItemCount();
4688 wxColour
wxGenericListCtrl::GetTextColour() const
4690 return GetForegroundColour();
4693 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4695 SetForegroundColour(col
);
4698 long wxGenericListCtrl::GetTopItem() const
4701 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4705 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4707 return m_mainWin
->GetNextItem( item
, geom
, state
);
4710 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4712 if (which
== wxIMAGE_LIST_NORMAL
)
4713 return m_imageListNormal
;
4714 else if (which
== wxIMAGE_LIST_SMALL
)
4715 return m_imageListSmall
;
4716 else if (which
== wxIMAGE_LIST_STATE
)
4717 return m_imageListState
;
4722 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4724 if ( which
== wxIMAGE_LIST_NORMAL
)
4726 if (m_ownsImageListNormal
)
4727 delete m_imageListNormal
;
4728 m_imageListNormal
= imageList
;
4729 m_ownsImageListNormal
= false;
4731 else if ( which
== wxIMAGE_LIST_SMALL
)
4733 if (m_ownsImageListSmall
)
4734 delete m_imageListSmall
;
4735 m_imageListSmall
= imageList
;
4736 m_ownsImageListSmall
= false;
4738 else if ( which
== wxIMAGE_LIST_STATE
)
4740 if (m_ownsImageListState
)
4741 delete m_imageListState
;
4742 m_imageListState
= imageList
;
4743 m_ownsImageListState
= false;
4746 m_mainWin
->SetImageList( imageList
, which
);
4749 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4751 SetImageList(imageList
, which
);
4752 if ( which
== wxIMAGE_LIST_NORMAL
)
4753 m_ownsImageListNormal
= true;
4754 else if ( which
== wxIMAGE_LIST_SMALL
)
4755 m_ownsImageListSmall
= true;
4756 else if ( which
== wxIMAGE_LIST_STATE
)
4757 m_ownsImageListState
= true;
4760 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4765 bool wxGenericListCtrl::DeleteItem( long item
)
4767 m_mainWin
->DeleteItem( item
);
4771 bool wxGenericListCtrl::DeleteAllItems()
4773 m_mainWin
->DeleteAllItems();
4777 bool wxGenericListCtrl::DeleteAllColumns()
4779 size_t count
= m_mainWin
->m_columns
.GetCount();
4780 for ( size_t n
= 0; n
< count
; n
++ )
4785 void wxGenericListCtrl::ClearAll()
4787 m_mainWin
->DeleteEverything();
4790 bool wxGenericListCtrl::DeleteColumn( int col
)
4792 m_mainWin
->DeleteColumn( col
);
4794 // if we don't have the header any longer, we need to relayout the window
4795 // if ( !GetColumnCount() )
4800 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4801 wxClassInfo
* textControlClass
)
4803 return m_mainWin
->EditLabel( item
, textControlClass
);
4806 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4808 return m_mainWin
->GetEditControl();
4811 bool wxGenericListCtrl::EnsureVisible( long item
)
4813 m_mainWin
->EnsureVisible( item
);
4817 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4819 return m_mainWin
->FindItem( start
, str
, partial
);
4822 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4824 return m_mainWin
->FindItem( start
, data
);
4827 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4828 int WXUNUSED(direction
))
4830 return m_mainWin
->FindItem( pt
);
4833 // TODO: sub item hit testing
4834 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4836 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4839 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4841 m_mainWin
->InsertItem( info
);
4842 return info
.m_itemId
;
4845 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4848 info
.m_text
= label
;
4849 info
.m_mask
= wxLIST_MASK_TEXT
;
4850 info
.m_itemId
= index
;
4851 return InsertItem( info
);
4854 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4857 info
.m_mask
= wxLIST_MASK_IMAGE
;
4858 info
.m_image
= imageIndex
;
4859 info
.m_itemId
= index
;
4860 return InsertItem( info
);
4863 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4866 info
.m_text
= label
;
4867 info
.m_image
= imageIndex
;
4868 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4869 info
.m_itemId
= index
;
4870 return InsertItem( info
);
4873 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4875 wxCHECK_MSG( InReportView(), -1, _T("can't add column in non report mode") );
4877 m_mainWin
->InsertColumn( col
, item
);
4879 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4880 // still have m_headerWin==NULL
4882 m_headerWin
->Refresh();
4887 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4888 int format
, int width
)
4891 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4892 item
.m_text
= heading
;
4895 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4896 item
.m_width
= width
;
4899 item
.m_format
= format
;
4901 return InsertColumn( col
, item
);
4904 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4906 return m_mainWin
->ScrollList(dx
, dy
);
4910 // fn is a function which takes 3 long arguments: item1, item2, data.
4911 // item1 is the long data associated with a first item (NOT the index).
4912 // item2 is the long data associated with a second item (NOT the index).
4913 // data is the same value as passed to SortItems.
4914 // The return value is a negative number if the first item should precede the second
4915 // item, a positive number of the second item should precede the first,
4916 // or zero if the two items are equivalent.
4917 // data is arbitrary data to be passed to the sort function.
4919 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
4921 m_mainWin
->SortItems( fn
, data
);
4925 // ----------------------------------------------------------------------------
4927 // ----------------------------------------------------------------------------
4929 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4931 if (!m_mainWin
) return;
4933 // We need to override OnSize so that our scrolled
4934 // window a) does call Layout() to use sizers for
4935 // positioning the controls but b) does not query
4936 // the sizer for their size and use that for setting
4937 // the scrollable area as set that ourselves by
4938 // calling SetScrollbar() further down.
4942 m_mainWin
->RecalculatePositions();
4947 void wxGenericListCtrl::OnInternalIdle()
4949 wxWindow::OnInternalIdle();
4951 if (m_mainWin
->m_dirty
)
4952 m_mainWin
->RecalculatePositions();
4955 // ----------------------------------------------------------------------------
4957 // ----------------------------------------------------------------------------
4959 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4963 m_mainWin
->SetBackgroundColour( colour
);
4964 m_mainWin
->m_dirty
= true;
4970 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4972 if ( !wxWindow::SetForegroundColour( colour
) )
4977 m_mainWin
->SetForegroundColour( colour
);
4978 m_mainWin
->m_dirty
= true;
4982 m_headerWin
->SetForegroundColour( colour
);
4987 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4989 if ( !wxWindow::SetFont( font
) )
4994 m_mainWin
->SetFont( font
);
4995 m_mainWin
->m_dirty
= true;
5000 m_headerWin
->SetFont( font
);
5001 // CalculateAndSetHeaderHeight();
5011 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5014 // Use the same color scheme as wxListBox
5015 return wxListBox::GetClassDefaultAttributes(variant
);
5017 wxUnusedVar(variant
);
5018 wxVisualAttributes attr
;
5019 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5020 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5021 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5026 // ----------------------------------------------------------------------------
5027 // methods forwarded to m_mainWin
5028 // ----------------------------------------------------------------------------
5030 #if wxUSE_DRAG_AND_DROP
5032 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5034 m_mainWin
->SetDropTarget( dropTarget
);
5037 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5039 return m_mainWin
->GetDropTarget();
5044 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5046 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5049 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5051 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5054 wxColour
wxGenericListCtrl::GetForegroundColour() const
5056 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5059 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5062 return m_mainWin
->PopupMenu( menu
, x
, y
);
5068 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5070 m_mainWin
->DoClientToScreen(x
, y
);
5073 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5075 m_mainWin
->DoScreenToClient(x
, y
);
5078 void wxGenericListCtrl::SetFocus()
5080 // The test in window.cpp fails as we are a composite
5081 // window, so it checks against "this", but not m_mainWin.
5082 if ( DoFindFocus() != this )
5083 m_mainWin
->SetFocus();
5086 wxSize
wxGenericListCtrl::DoGetBestSize() const
5088 // Something is better than nothing...
5089 // 100x80 is what the MSW version will get from the default
5090 // wxControl::DoGetBestSize
5091 return wxSize(100, 80);
5094 // ----------------------------------------------------------------------------
5095 // virtual list control support
5096 // ----------------------------------------------------------------------------
5098 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5100 // this is a pure virtual function, in fact - which is not really pure
5101 // because the controls which are not virtual don't need to implement it
5102 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5104 return wxEmptyString
;
5107 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5109 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5111 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5115 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5118 return OnGetItemImage(item
);
5124 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5126 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5127 _T("invalid item index in OnGetItemAttr()") );
5129 // no attributes by default
5133 void wxGenericListCtrl::SetItemCount(long count
)
5135 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5137 m_mainWin
->SetItemCount(count
);
5140 void wxGenericListCtrl::RefreshItem(long item
)
5142 m_mainWin
->RefreshLine(item
);
5145 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5147 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5150 // Generic wxListCtrl is more or less a container for two other
5151 // windows which drawings are done upon. These are namely
5152 // 'm_headerWin' and 'm_mainWin'.
5153 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5154 // behaviour wxListCtrl has under wxMSW.
5156 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5160 // The easy case, no rectangle specified.
5162 m_headerWin
->Refresh(eraseBackground
);
5165 m_mainWin
->Refresh(eraseBackground
);
5169 // Refresh the header window
5172 wxRect rectHeader
= m_headerWin
->GetRect();
5173 rectHeader
.Intersect(*rect
);
5174 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5177 m_headerWin
->GetPosition(&x
, &y
);
5178 rectHeader
.Offset(-x
, -y
);
5179 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5183 // Refresh the main window
5186 wxRect rectMain
= m_mainWin
->GetRect();
5187 rectMain
.Intersect(*rect
);
5188 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5191 m_mainWin
->GetPosition(&x
, &y
);
5192 rectMain
.Offset(-x
, -y
);
5193 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5199 #endif // wxUSE_LISTCTRL