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"
29 #include "wx/scrolwin.h"
31 #include "wx/settings.h"
32 #include "wx/dynarray.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
36 #include "wx/settings.h"
40 #include "wx/imaglist.h"
41 #include "wx/renderer.h"
42 #include "wx/generic/private/listctrl.h"
45 #include "wx/osx/private.h"
48 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
49 #define "wx/msw/wrapwin.h"
52 // NOTE: If using the wxListBox visual attributes works everywhere then this can
53 // be removed, as well as the #else case below.
54 #define _USE_VISATTR 0
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // // the height of the header window (FIXME: should depend on its font!)
62 // static const int HEADER_HEIGHT = 23;
64 static const int SCROLL_UNIT_X
= 15;
66 // the spacing between the lines (in report mode)
67 static const int LINE_SPACING
= 0;
69 // extra margins around the text label
71 static const int EXTRA_WIDTH
= 6;
73 static const int EXTRA_WIDTH
= 4;
77 static const int EXTRA_HEIGHT
= 6;
79 static const int EXTRA_HEIGHT
= 4;
82 // margin between the window and the items
83 static const int EXTRA_BORDER_X
= 2;
84 static const int EXTRA_BORDER_Y
= 2;
86 // offset for the header window
87 static const int HEADER_OFFSET_X
= 0;
88 static const int HEADER_OFFSET_Y
= 0;
90 // margin between rows of icons in [small] icon view
91 static const int MARGIN_BETWEEN_ROWS
= 6;
93 // when autosizing the columns, add some slack
94 static const int AUTOSIZE_COL_MARGIN
= 10;
96 // default width for the header columns
97 static const int WIDTH_COL_DEFAULT
= 80;
99 // the space between the image and the text in the report mode
100 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
102 // the space between the image and the text in the report mode in header
103 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
107 // ----------------------------------------------------------------------------
108 // arrays/list implementations
109 // ----------------------------------------------------------------------------
111 #include "wx/listimpl.cpp"
112 WX_DEFINE_LIST(wxListItemDataList
)
114 #include "wx/arrimpl.cpp"
115 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
117 #include "wx/listimpl.cpp"
118 WX_DEFINE_LIST(wxListHeaderDataList
)
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 wxListItemData::~wxListItemData()
127 // in the virtual list control the attributes are managed by the main
128 // program, so don't delete them
129 if ( !m_owner
->IsVirtual() )
135 void wxListItemData::Init()
143 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
149 if ( owner
->InReportView() )
155 void wxListItemData::SetItem( const wxListItem
&info
)
157 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
158 SetText(info
.m_text
);
159 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
160 m_image
= info
.m_image
;
161 if ( info
.m_mask
& wxLIST_MASK_DATA
)
162 m_data
= info
.m_data
;
164 if ( info
.HasAttributes() )
167 m_attr
->AssignFrom(*info
.GetAttributes());
169 m_attr
= new wxListItemAttr(*info
.GetAttributes());
177 m_rect
->width
= info
.m_width
;
181 void wxListItemData::SetPosition( int x
, int y
)
183 wxCHECK_RET( m_rect
, wxT("unexpected SetPosition() call") );
189 void wxListItemData::SetSize( int width
, int height
)
191 wxCHECK_RET( m_rect
, wxT("unexpected SetSize() call") );
194 m_rect
->width
= width
;
196 m_rect
->height
= height
;
199 bool wxListItemData::IsHit( int x
, int y
) const
201 wxCHECK_MSG( m_rect
, false, wxT("can't be called in this mode") );
203 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
206 int wxListItemData::GetX() const
208 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
213 int wxListItemData::GetY() const
215 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
220 int wxListItemData::GetWidth() const
222 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
224 return m_rect
->width
;
227 int wxListItemData::GetHeight() const
229 wxCHECK_MSG( m_rect
, 0, wxT("can't be called in this mode") );
231 return m_rect
->height
;
234 void wxListItemData::GetItem( wxListItem
&info
) const
236 long mask
= info
.m_mask
;
238 // by default, get everything for backwards compatibility
241 if ( mask
& wxLIST_MASK_TEXT
)
242 info
.m_text
= m_text
;
243 if ( mask
& wxLIST_MASK_IMAGE
)
244 info
.m_image
= m_image
;
245 if ( mask
& wxLIST_MASK_DATA
)
246 info
.m_data
= m_data
;
250 if ( m_attr
->HasTextColour() )
251 info
.SetTextColour(m_attr
->GetTextColour());
252 if ( m_attr
->HasBackgroundColour() )
253 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
254 if ( m_attr
->HasFont() )
255 info
.SetFont(m_attr
->GetFont());
259 //-----------------------------------------------------------------------------
261 //-----------------------------------------------------------------------------
263 void wxListHeaderData::Init()
275 wxListHeaderData::wxListHeaderData()
280 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
287 void wxListHeaderData::SetItem( const wxListItem
&item
)
289 m_mask
= item
.m_mask
;
291 if ( m_mask
& wxLIST_MASK_TEXT
)
292 m_text
= item
.m_text
;
294 if ( m_mask
& wxLIST_MASK_IMAGE
)
295 m_image
= item
.m_image
;
297 if ( m_mask
& wxLIST_MASK_FORMAT
)
298 m_format
= item
.m_format
;
300 if ( m_mask
& wxLIST_MASK_WIDTH
)
301 SetWidth(item
.m_width
);
303 if ( m_mask
& wxLIST_MASK_STATE
)
304 SetState(item
.m_state
);
307 void wxListHeaderData::SetPosition( int x
, int y
)
313 void wxListHeaderData::SetHeight( int h
)
318 void wxListHeaderData::SetWidth( int w
)
320 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
323 void wxListHeaderData::SetState( int flag
)
328 void wxListHeaderData::SetFormat( int format
)
333 bool wxListHeaderData::HasImage() const
335 return m_image
!= -1;
338 bool wxListHeaderData::IsHit( int x
, int y
) const
340 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
343 void wxListHeaderData::GetItem( wxListItem
& item
)
345 item
.m_mask
= m_mask
;
346 item
.m_text
= m_text
;
347 item
.m_image
= m_image
;
348 item
.m_format
= m_format
;
349 item
.m_width
= m_width
;
350 item
.m_state
= m_state
;
353 int wxListHeaderData::GetImage() const
358 int wxListHeaderData::GetWidth() const
363 int wxListHeaderData::GetFormat() const
368 int wxListHeaderData::GetState() const
373 //-----------------------------------------------------------------------------
375 //-----------------------------------------------------------------------------
377 inline int wxListLineData::GetMode() const
379 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
382 inline bool wxListLineData::InReportView() const
384 return m_owner
->HasFlag(wxLC_REPORT
);
387 inline bool wxListLineData::IsVirtual() const
389 return m_owner
->IsVirtual();
392 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
396 if ( InReportView() )
399 m_gi
= new GeometryInfo
;
401 m_highlighted
= false;
403 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
406 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
408 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
409 wxCHECK_RET( node
, wxT("no subitems at all??") );
411 wxListItemData
*item
= node
->GetData();
419 case wxLC_SMALL_ICON
:
420 m_gi
->m_rectAll
.width
= spacing
;
427 m_gi
->m_rectLabel
.width
=
428 m_gi
->m_rectLabel
.height
= 0;
432 dc
->GetTextExtent( s
, &lw
, &lh
);
436 m_gi
->m_rectAll
.height
= spacing
+ lh
;
438 m_gi
->m_rectAll
.width
= lw
;
440 m_gi
->m_rectLabel
.width
= lw
;
441 m_gi
->m_rectLabel
.height
= lh
;
444 if (item
->HasImage())
447 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
448 m_gi
->m_rectIcon
.width
= w
+ 8;
449 m_gi
->m_rectIcon
.height
= h
+ 8;
451 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
452 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
453 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
454 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
457 if ( item
->HasText() )
459 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
460 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
462 else // no text, highlight the icon
464 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
465 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
470 s
= item
->GetTextForMeasuring();
472 dc
->GetTextExtent( s
, &lw
, &lh
);
476 m_gi
->m_rectLabel
.width
= lw
;
477 m_gi
->m_rectLabel
.height
= lh
;
479 m_gi
->m_rectAll
.width
= lw
;
480 m_gi
->m_rectAll
.height
= lh
;
482 if (item
->HasImage())
485 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
486 m_gi
->m_rectIcon
.width
= w
;
487 m_gi
->m_rectIcon
.height
= h
;
489 m_gi
->m_rectAll
.width
+= 4 + w
;
490 if (h
> m_gi
->m_rectAll
.height
)
491 m_gi
->m_rectAll
.height
= h
;
494 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
495 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
499 wxFAIL_MSG( wxT("unexpected call to SetSize") );
503 wxFAIL_MSG( wxT("unknown mode") );
508 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
510 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
511 wxCHECK_RET( node
, wxT("no subitems at all??") );
513 wxListItemData
*item
= node
->GetData();
518 case wxLC_SMALL_ICON
:
519 m_gi
->m_rectAll
.x
= x
;
520 m_gi
->m_rectAll
.y
= y
;
522 if ( item
->HasImage() )
524 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
525 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
526 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
529 if ( item
->HasText() )
531 if (m_gi
->m_rectAll
.width
> spacing
)
532 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
534 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
535 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
536 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
537 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
539 else // no text, highlight the icon
541 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
542 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
547 m_gi
->m_rectAll
.x
= x
;
548 m_gi
->m_rectAll
.y
= y
;
550 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
551 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
552 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
554 if (item
->HasImage())
556 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
557 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
558 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
562 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
567 wxFAIL_MSG( wxT("unexpected call to SetPosition") );
571 wxFAIL_MSG( wxT("unknown mode") );
576 void wxListLineData::InitItems( int num
)
578 for (int i
= 0; i
< num
; i
++)
579 m_items
.Append( new wxListItemData(m_owner
) );
582 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
584 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
585 wxCHECK_RET( node
, wxT("invalid column index in SetItem") );
587 wxListItemData
*item
= node
->GetData();
588 item
->SetItem( info
);
591 void wxListLineData::GetItem( int index
, wxListItem
&info
)
593 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
596 wxListItemData
*item
= node
->GetData();
597 item
->GetItem( info
);
601 wxString
wxListLineData::GetText(int index
) const
605 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
608 wxListItemData
*item
= node
->GetData();
615 void wxListLineData::SetText( int index
, const wxString
& s
)
617 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
620 wxListItemData
*item
= node
->GetData();
625 void wxListLineData::SetImage( int index
, int image
)
627 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
628 wxCHECK_RET( node
, wxT("invalid column index in SetImage()") );
630 wxListItemData
*item
= node
->GetData();
631 item
->SetImage(image
);
634 int wxListLineData::GetImage( int index
) const
636 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
637 wxCHECK_MSG( node
, -1, wxT("invalid column index in GetImage()") );
639 wxListItemData
*item
= node
->GetData();
640 return item
->GetImage();
643 wxListItemAttr
*wxListLineData::GetAttr() const
645 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
646 wxCHECK_MSG( node
, NULL
, wxT("invalid column index in GetAttr()") );
648 wxListItemData
*item
= node
->GetData();
649 return item
->GetAttr();
652 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
654 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
655 wxCHECK_RET( node
, wxT("invalid column index in SetAttr()") );
657 wxListItemData
*item
= node
->GetData();
661 void wxListLineData::ApplyAttributes(wxDC
*dc
,
662 const wxRect
& rectHL
,
666 const wxListItemAttr
* const attr
= GetAttr();
668 wxWindow
* const listctrl
= m_owner
->GetParent();
670 const bool hasFocus
= listctrl
->HasFocus()
671 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
672 && IsControlActive( (ControlRef
)listctrl
->GetHandle() )
678 // don't use foreground colour for drawing highlighted items - this might
679 // make them completely invisible (and there is no way to do bit
680 // arithmetics on wxColour, unfortunately)
690 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
693 else if ( attr
&& attr
->HasTextColour() )
694 colText
= attr
->GetTextColour();
696 colText
= listctrl
->GetForegroundColour();
698 dc
->SetTextForeground(colText
);
702 if ( attr
&& attr
->HasFont() )
703 font
= attr
->GetFont();
705 font
= listctrl
->GetFont();
712 // Use the renderer method to ensure that the selected items use the
714 int flags
= wxCONTROL_SELECTED
;
716 flags
|= wxCONTROL_FOCUSED
;
718 flags
|= wxCONTROL_CURRENT
;
719 wxRendererNative::Get().
720 DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
722 else if ( attr
&& attr
->HasBackgroundColour() )
724 // Draw the background using the items custom background colour.
725 dc
->SetBrush(attr
->GetBackgroundColour());
726 dc
->SetPen(*wxTRANSPARENT_PEN
);
727 dc
->DrawRectangle(rectHL
);
730 // just for debugging to better see where the items are
732 dc
->SetPen(*wxRED_PEN
);
733 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
734 dc
->DrawRectangle( m_gi
->m_rectAll
);
735 dc
->SetPen(*wxGREEN_PEN
);
736 dc
->DrawRectangle( m_gi
->m_rectIcon
);
740 void wxListLineData::Draw(wxDC
*dc
, bool current
)
742 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
743 wxCHECK_RET( node
, wxT("no subitems at all??") );
745 ApplyAttributes(dc
, m_gi
->m_rectHighlight
, IsHighlighted(), current
);
747 wxListItemData
*item
= node
->GetData();
748 if (item
->HasImage())
750 // centre the image inside our rectangle, this looks nicer when items
751 // ae aligned in a row
752 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
754 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
759 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
761 wxDCClipper
clipper(*dc
, rectLabel
);
762 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
766 void wxListLineData::DrawInReportMode( wxDC
*dc
,
768 const wxRect
& rectHL
,
772 // TODO: later we should support setting different attributes for
773 // different columns - to do it, just add "col" argument to
774 // GetAttr() and move these lines into the loop below
776 ApplyAttributes(dc
, rectHL
, highlighted
, current
);
778 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
779 yMid
= rect
.y
+ rect
.height
/2;
781 // This probably needs to be done
782 // on all platforms as the icons
783 // otherwise nearly touch the border
788 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
790 node
= node
->GetNext(), col
++ )
792 wxListItemData
*item
= node
->GetData();
794 int width
= m_owner
->GetColumnWidth(col
);
799 const int wText
= width
;
800 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
802 if ( item
->HasImage() )
805 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
806 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
808 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
814 if ( item
->HasText() )
815 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
);
819 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
820 const wxString
& textOrig
,
826 // we don't support displaying multiple lines currently (and neither does
827 // wxMSW FWIW) so just merge all the lines
828 wxString
text(textOrig
);
829 text
.Replace(wxT("\n"), wxT(" "));
832 dc
->GetTextExtent(text
, &w
, &h
);
834 const wxCoord y
= yMid
- (h
+ 1)/2;
836 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
838 // determine if the string can fit inside the current width
841 // it can, draw it using the items alignment
843 m_owner
->GetColumn(col
, item
);
844 switch ( item
.GetAlign() )
846 case wxLIST_FORMAT_LEFT
:
850 case wxLIST_FORMAT_RIGHT
:
854 case wxLIST_FORMAT_CENTER
:
855 x
+= (width
- w
) / 2;
859 wxFAIL_MSG( wxT("unknown list item format") );
863 dc
->DrawText(text
, x
, y
);
865 else // otherwise, truncate and add an ellipsis if possible
867 // determine the base width
868 wxString
ellipsis(wxT("..."));
870 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
872 // continue until we have enough space or only one character left
874 size_t len
= text
.length();
875 wxString drawntext
= text
.Left(len
);
878 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
879 drawntext
.RemoveLast();
882 if (w
+ base_w
<= width
)
886 // if still not enough space, remove ellipsis characters
887 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
889 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
890 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
894 dc
->DrawText(drawntext
, x
, y
);
895 dc
->DrawText(ellipsis
, x
+ w
, y
);
899 bool wxListLineData::Highlight( bool on
)
901 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
903 if ( on
== m_highlighted
)
911 void wxListLineData::ReverseHighlight( void )
913 Highlight(!IsHighlighted());
916 //-----------------------------------------------------------------------------
917 // wxListHeaderWindow
918 //-----------------------------------------------------------------------------
920 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
921 EVT_PAINT (wxListHeaderWindow::OnPaint
)
922 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
923 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
926 void wxListHeaderWindow::Init()
928 m_currentCursor
= NULL
;
929 m_isDragging
= false;
931 m_sendSetColumnWidth
= false;
934 wxListHeaderWindow::wxListHeaderWindow()
939 m_resizeCursor
= NULL
;
942 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
944 wxListMainWindow
*owner
,
948 const wxString
&name
)
949 : wxWindow( win
, id
, pos
, size
, style
, name
)
954 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
957 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
958 SetOwnForegroundColour( attr
.colFg
);
959 SetOwnBackgroundColour( attr
.colBg
);
961 SetOwnFont( attr
.font
);
963 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
964 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
966 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
970 wxListHeaderWindow::~wxListHeaderWindow()
972 delete m_resizeCursor
;
975 #ifdef __WXUNIVERSAL__
976 #include "wx/univ/renderer.h"
977 #include "wx/univ/theme.h"
980 // shift the DC origin to match the position of the main window horz
981 // scrollbar: this allows us to always use logical coords
982 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
984 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
987 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
990 parent
->GetViewStart( &view_start
, NULL
);
995 dc
.GetDeviceOrigin( &org_x
, &org_y
);
997 // account for the horz scrollbar offset
999 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1001 // Maybe we just have to check for m_signX
1002 // in the DC, but I leave the #ifdef __WXGTK__
1004 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1008 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1011 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1013 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1015 wxPaintDC
dc( this );
1019 dc
.SetFont( GetFont() );
1021 // width and height of the entire header window
1023 GetClientSize( &w
, &h
);
1024 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1026 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1027 dc
.SetTextForeground(GetForegroundColour());
1029 int x
= HEADER_OFFSET_X
;
1030 int numColumns
= m_owner
->GetColumnCount();
1032 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1034 m_owner
->GetColumn( i
, item
);
1035 int wCol
= item
.m_width
;
1041 if (!m_parent
->IsEnabled())
1042 flags
|= wxCONTROL_DISABLED
;
1044 // NB: The code below is not really Mac-specific, but since we are close
1045 // to 2.8 release and I don't have time to test on other platforms, I
1046 // defined this only for wxMac. If this behavior is desired on
1047 // other platforms, please go ahead and revise or remove the #ifdef.
1049 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1050 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1051 flags
|= wxCONTROL_SELECTED
;
1055 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1057 wxRendererNative::Get().DrawHeaderButton
1061 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1065 // see if we have enough space for the column label
1067 // for this we need the width of the text
1070 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1071 wLabel
+= 2 * EXTRA_WIDTH
;
1073 // and the width of the icon, if any
1074 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1075 const int image
= item
.m_image
;
1076 wxImageList
*imageList
;
1079 imageList
= m_owner
->GetSmallImageList();
1082 imageList
->GetSize(image
, ix
, iy
);
1083 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1091 // ignore alignment if there is not enough space anyhow
1093 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1096 wxFAIL_MSG( wxT("unknown list item format") );
1099 case wxLIST_FORMAT_LEFT
:
1103 case wxLIST_FORMAT_RIGHT
:
1104 xAligned
= x
+ cw
- wLabel
;
1107 case wxLIST_FORMAT_CENTER
:
1108 xAligned
= x
+ (cw
- wLabel
) / 2;
1112 // draw the text and image clipping them so that they
1113 // don't overwrite the column boundary
1114 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
);
1116 // if we have an image, draw it on the right of the label
1123 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1124 HEADER_OFFSET_Y
+ (h
- iy
)/2,
1125 wxIMAGELIST_DRAW_TRANSPARENT
1129 dc
.DrawText( item
.GetText(),
1130 xAligned
+ EXTRA_WIDTH
, (h
- hLabel
) / 2 );
1135 // Fill in what's missing to the right of the columns, otherwise we will
1136 // leave an unpainted area when columns are removed (and it looks better)
1139 wxRendererNative::Get().DrawHeaderButton
1143 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1144 wxCONTROL_DIRTY
// mark as last column
1149 void wxListHeaderWindow::OnInternalIdle()
1151 wxWindow::OnInternalIdle();
1153 if (m_sendSetColumnWidth
)
1155 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1156 m_sendSetColumnWidth
= false;
1160 void wxListHeaderWindow::DrawCurrent()
1163 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1164 m_sendSetColumnWidth
= true;
1165 m_colToSend
= m_column
;
1166 m_widthToSend
= m_currentX
- m_minX
;
1168 int x1
= m_currentX
;
1170 m_owner
->ClientToScreen( &x1
, &y1
);
1172 int x2
= m_currentX
;
1174 m_owner
->GetClientSize( NULL
, &y2
);
1175 m_owner
->ClientToScreen( &x2
, &y2
);
1178 dc
.SetLogicalFunction( wxINVERT
);
1179 dc
.SetPen( wxPen(*wxBLACK
, 2) );
1180 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1184 dc
.DrawLine( x1
, y1
, x2
, y2
);
1186 dc
.SetLogicalFunction( wxCOPY
);
1188 dc
.SetPen( wxNullPen
);
1189 dc
.SetBrush( wxNullBrush
);
1193 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1195 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1197 // we want to work with logical coords
1199 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1200 int y
= event
.GetY();
1204 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1206 // we don't draw the line beyond our window, but we allow dragging it
1209 GetClientSize( &w
, NULL
);
1210 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1213 // erase the line if it was drawn
1214 if ( m_currentX
< w
)
1217 if (event
.ButtonUp())
1220 m_isDragging
= false;
1222 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1223 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1230 m_currentX
= m_minX
+ 7;
1232 // draw in the new location
1233 if ( m_currentX
< w
)
1237 else // not dragging
1240 bool hit_border
= false;
1242 // end of the current column
1245 // find the column where this event occurred
1247 countCol
= m_owner
->GetColumnCount();
1248 for (col
= 0; col
< countCol
; col
++)
1250 xpos
+= m_owner
->GetColumnWidth( col
);
1253 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1255 // near the column border
1262 // inside the column
1269 if ( col
== countCol
)
1272 if (event
.LeftDown() || event
.RightUp())
1274 if (hit_border
&& event
.LeftDown())
1276 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1277 event
.GetPosition()) )
1279 m_isDragging
= true;
1284 //else: column resizing was vetoed by the user code
1286 else // click on a column
1288 // record the selected state of the columns
1289 if (event
.LeftDown())
1291 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
1294 m_owner
->GetColumn(i
, colItem
);
1295 long state
= colItem
.GetState();
1297 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
1299 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
1300 m_owner
->SetColumn(i
, colItem
);
1304 SendListEvent( event
.LeftDown()
1305 ? wxEVT_COMMAND_LIST_COL_CLICK
1306 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1307 event
.GetPosition());
1310 else if (event
.Moving())
1315 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1316 m_currentCursor
= m_resizeCursor
;
1320 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1321 m_currentCursor
= wxSTANDARD_CURSOR
;
1325 SetCursor(*m_currentCursor
);
1330 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1332 m_owner
->SetFocus();
1336 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
1338 wxWindow
*parent
= GetParent();
1339 wxListEvent
le( type
, parent
->GetId() );
1340 le
.SetEventObject( parent
);
1341 le
.m_pointDrag
= pos
;
1343 // the position should be relative to the parent window, not
1344 // this one for compatibility with MSW and common sense: the
1345 // user code doesn't know anything at all about this header
1346 // window, so why should it get positions relative to it?
1347 le
.m_pointDrag
.y
-= GetSize().y
;
1349 le
.m_col
= m_column
;
1350 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1353 //-----------------------------------------------------------------------------
1354 // wxListRenameTimer (internal)
1355 //-----------------------------------------------------------------------------
1357 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1362 void wxListRenameTimer::Notify()
1364 m_owner
->OnRenameTimer();
1367 //-----------------------------------------------------------------------------
1368 // wxListTextCtrlWrapper (internal)
1369 //-----------------------------------------------------------------------------
1371 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
1372 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
1373 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
1374 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
1377 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
1380 : m_startValue(owner
->GetItemText(itemEdit
)),
1381 m_itemEdited(itemEdit
)
1385 m_aboutToFinish
= false;
1387 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1389 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1391 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1392 &rectLabel
.x
, &rectLabel
.y
);
1394 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
1395 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1396 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
1399 m_text
->PushEventHandler(this);
1402 void wxListTextCtrlWrapper::EndEdit(EndReason reason
)
1404 m_aboutToFinish
= true;
1409 // Notify the owner about the changes
1412 // Even if vetoed, close the control (consistent with MSW)
1417 m_owner
->OnRenameCancelled(m_itemEdited
);
1423 // Don't generate any notifications for the control being destroyed
1424 // and don't set focus to it neither.
1430 void wxListTextCtrlWrapper::Finish( bool setfocus
)
1432 m_text
->RemoveEventHandler(this);
1433 m_owner
->ResetTextControl( m_text
);
1435 wxPendingDelete
.Append( this );
1438 m_owner
->SetFocus();
1441 bool wxListTextCtrlWrapper::AcceptChanges()
1443 const wxString value
= m_text
->GetValue();
1445 // notice that we should always call OnRenameAccept() to generate the "end
1446 // label editing" event, even if the user hasn't really changed anything
1447 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
1449 // vetoed by the user
1453 // accepted, do rename the item (unless nothing changed)
1454 if ( value
!= m_startValue
)
1455 m_owner
->SetItemText(m_itemEdited
, value
);
1460 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
1462 switch ( event
.m_keyCode
)
1465 EndEdit( End_Accept
);
1469 EndEdit( End_Discard
);
1477 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
1479 if (m_aboutToFinish
)
1481 // auto-grow the textctrl:
1482 wxSize parentSize
= m_owner
->GetSize();
1483 wxPoint myPos
= m_text
->GetPosition();
1484 wxSize mySize
= m_text
->GetSize();
1486 m_text
->GetTextExtent(m_text
->GetValue() + wxT("MM"), &sx
, &sy
);
1487 if (myPos
.x
+ sx
> parentSize
.x
)
1488 sx
= parentSize
.x
- myPos
.x
;
1491 m_text
->SetSize(sx
, wxDefaultCoord
);
1497 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
1499 if ( !m_aboutToFinish
)
1501 if ( !AcceptChanges() )
1502 m_owner
->OnRenameCancelled( m_itemEdited
);
1507 // We must let the native text control handle focus
1511 //-----------------------------------------------------------------------------
1513 //-----------------------------------------------------------------------------
1515 BEGIN_EVENT_TABLE(wxListMainWindow
, wxWindow
)
1516 EVT_PAINT (wxListMainWindow::OnPaint
)
1517 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1518 EVT_CHAR (wxListMainWindow::OnChar
)
1519 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1520 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
1521 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1522 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1523 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1524 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
1527 void wxListMainWindow::Init()
1532 m_lineTo
= (size_t)-1;
1538 m_small_image_list
= NULL
;
1539 m_normal_image_list
= NULL
;
1541 m_small_spacing
= 30;
1542 m_normal_spacing
= 40;
1546 m_isCreated
= false;
1548 m_lastOnSame
= false;
1549 m_renameTimer
= new wxListRenameTimer( this );
1550 m_textctrlWrapper
= NULL
;
1554 m_lineSelectSingleOnUp
=
1555 m_lineBeforeLastClicked
= (size_t)-1;
1558 wxListMainWindow::wxListMainWindow()
1563 m_highlightUnfocusedBrush
= NULL
;
1566 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1571 const wxString
&name
)
1572 : wxWindow( parent
, id
, pos
, size
, style
, name
)
1576 m_highlightBrush
= new wxBrush
1578 wxSystemSettings::GetColour
1580 wxSYS_COLOUR_HIGHLIGHT
1585 m_highlightUnfocusedBrush
= new wxBrush
1587 wxSystemSettings::GetColour
1589 wxSYS_COLOUR_BTNSHADOW
1594 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
1595 SetOwnForegroundColour( attr
.colFg
);
1596 SetOwnBackgroundColour( attr
.colBg
);
1598 SetOwnFont( attr
.font
);
1601 wxListMainWindow::~wxListMainWindow()
1603 if ( m_textctrlWrapper
)
1604 m_textctrlWrapper
->EndEdit(wxListTextCtrlWrapper::End_Destroy
);
1607 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
1608 WX_CLEAR_ARRAY(m_aColWidths
);
1610 delete m_highlightBrush
;
1611 delete m_highlightUnfocusedBrush
;
1612 delete m_renameTimer
;
1615 void wxListMainWindow::SetReportView(bool inReportView
)
1617 const size_t count
= m_lines
.size();
1618 for ( size_t n
= 0; n
< count
; n
++ )
1620 m_lines
[n
].SetReportView(inReportView
);
1624 void wxListMainWindow::CacheLineData(size_t line
)
1626 wxGenericListCtrl
*listctrl
= GetListCtrl();
1628 wxListLineData
*ld
= GetDummyLine();
1630 size_t countCol
= GetColumnCount();
1631 for ( size_t col
= 0; col
< countCol
; col
++ )
1633 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1634 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
1637 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
1640 wxListLineData
*wxListMainWindow::GetDummyLine() const
1642 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1643 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1645 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1647 // we need to recreate the dummy line if the number of columns in the
1648 // control changed as it would have the incorrect number of fields
1650 if ( !m_lines
.IsEmpty() &&
1651 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
1653 self
->m_lines
.Clear();
1656 if ( m_lines
.IsEmpty() )
1658 wxListLineData
*line
= new wxListLineData(self
);
1659 self
->m_lines
.Add(line
);
1661 // don't waste extra memory -- there never going to be anything
1662 // else/more in this array
1663 self
->m_lines
.Shrink();
1669 // ----------------------------------------------------------------------------
1670 // line geometry (report mode only)
1671 // ----------------------------------------------------------------------------
1673 wxCoord
wxListMainWindow::GetLineHeight() const
1675 // we cache the line height as calling GetTextExtent() is slow
1676 if ( !m_lineHeight
)
1678 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1680 wxClientDC
dc( self
);
1681 dc
.SetFont( GetFont() );
1684 dc
.GetTextExtent(wxT("H"), NULL
, &y
);
1686 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
1689 m_small_image_list
->GetSize(0, iw
, ih
);
1694 self
->m_lineHeight
= y
+ LINE_SPACING
;
1697 return m_lineHeight
;
1700 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1702 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1704 return LINE_SPACING
+ line
* GetLineHeight();
1707 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
1709 if ( !InReportView() )
1710 return GetLine(line
)->m_gi
->m_rectAll
;
1713 rect
.x
= HEADER_OFFSET_X
;
1714 rect
.y
= GetLineY(line
);
1715 rect
.width
= GetHeaderWidth();
1716 rect
.height
= GetLineHeight();
1721 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
1723 if ( !InReportView() )
1724 return GetLine(line
)->m_gi
->m_rectLabel
;
1727 wxListLineData
*data
= GetLine(line
);
1728 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
1731 wxListItemData
*item
= node
->GetData();
1732 if ( item
->HasImage() )
1735 GetImageSize( item
->GetImage(), ix
, iy
);
1736 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
1741 rect
.x
= image_x
+ HEADER_OFFSET_X
;
1742 rect
.y
= GetLineY(line
);
1743 rect
.width
= GetColumnWidth(0) - image_x
;
1744 rect
.height
= GetLineHeight();
1749 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
1751 if ( !InReportView() )
1752 return GetLine(line
)->m_gi
->m_rectIcon
;
1754 wxListLineData
*ld
= GetLine(line
);
1755 wxASSERT_MSG( ld
->HasImage(), wxT("should have an image") );
1758 rect
.x
= HEADER_OFFSET_X
;
1759 rect
.y
= GetLineY(line
);
1760 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
1765 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
1767 return InReportView() ? GetLineRect(line
)
1768 : GetLine(line
)->m_gi
->m_rectHighlight
;
1771 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
1773 wxASSERT_MSG( line
< GetItemCount(), wxT("invalid line in HitTestLine") );
1775 wxListLineData
*ld
= GetLine(line
);
1777 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
1778 return wxLIST_HITTEST_ONITEMICON
;
1780 // VS: Testing for "ld->HasText() || InReportView()" instead of
1781 // "ld->HasText()" is needed to make empty lines in report view
1783 if ( ld
->HasText() || InReportView() )
1785 wxRect rect
= InReportView() ? GetLineRect(line
)
1786 : GetLineLabelRect(line
);
1788 if ( rect
.Contains(x
, y
) )
1789 return wxLIST_HITTEST_ONITEMLABEL
;
1795 // ----------------------------------------------------------------------------
1796 // highlight (selection) handling
1797 // ----------------------------------------------------------------------------
1799 bool wxListMainWindow::IsHighlighted(size_t line
) const
1803 return m_selStore
.IsSelected(line
);
1807 wxListLineData
*ld
= GetLine(line
);
1808 wxCHECK_MSG( ld
, false, wxT("invalid index in IsHighlighted") );
1810 return ld
->IsHighlighted();
1814 void wxListMainWindow::HighlightLines( size_t lineFrom
,
1820 wxArrayInt linesChanged
;
1821 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
1824 // meny items changed state, refresh everything
1825 RefreshLines(lineFrom
, lineTo
);
1827 else // only a few items changed state, refresh only them
1829 size_t count
= linesChanged
.GetCount();
1830 for ( size_t n
= 0; n
< count
; n
++ )
1832 RefreshLine(linesChanged
[n
]);
1836 else // iterate over all items in non report view
1838 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1840 if ( HighlightLine(line
, highlight
) )
1846 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
1852 changed
= m_selStore
.SelectItem(line
, highlight
);
1856 wxListLineData
*ld
= GetLine(line
);
1857 wxCHECK_MSG( ld
, false, wxT("invalid index in HighlightLine") );
1859 changed
= ld
->Highlight(highlight
);
1864 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
1865 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
1871 void wxListMainWindow::RefreshLine( size_t line
)
1873 if ( InReportView() )
1875 size_t visibleFrom
, visibleTo
;
1876 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1878 if ( line
< visibleFrom
|| line
> visibleTo
)
1882 wxRect rect
= GetLineRect(line
);
1884 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1885 RefreshRect( rect
);
1888 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
1890 // we suppose that they are ordered by caller
1891 wxASSERT_MSG( lineFrom
<= lineTo
, wxT("indices in disorder") );
1893 wxASSERT_MSG( lineTo
< GetItemCount(), wxT("invalid line range") );
1895 if ( InReportView() )
1897 size_t visibleFrom
, visibleTo
;
1898 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1900 if ( lineFrom
< visibleFrom
)
1901 lineFrom
= visibleFrom
;
1902 if ( lineTo
> visibleTo
)
1907 rect
.y
= GetLineY(lineFrom
);
1908 rect
.width
= GetClientSize().x
;
1909 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
1911 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1912 RefreshRect( rect
);
1916 // TODO: this should be optimized...
1917 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1924 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
1926 if ( InReportView() )
1928 size_t visibleFrom
, visibleTo
;
1929 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
1931 if ( lineFrom
< visibleFrom
)
1932 lineFrom
= visibleFrom
;
1933 else if ( lineFrom
> visibleTo
)
1938 rect
.y
= GetLineY(lineFrom
);
1939 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1941 wxSize size
= GetClientSize();
1942 rect
.width
= size
.x
;
1944 // refresh till the bottom of the window
1945 rect
.height
= size
.y
- rect
.y
;
1947 RefreshRect( rect
);
1951 // TODO: how to do it more efficiently?
1956 void wxListMainWindow::RefreshSelected()
1962 if ( InReportView() )
1964 GetVisibleLinesRange(&from
, &to
);
1969 to
= GetItemCount() - 1;
1972 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
1973 RefreshLine(m_current
);
1975 for ( size_t line
= from
; line
<= to
; line
++ )
1977 // NB: the test works as expected even if m_current == -1
1978 if ( line
!= m_current
&& IsHighlighted(line
) )
1983 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1985 // Note: a wxPaintDC must be constructed even if no drawing is
1986 // done (a Windows requirement).
1987 wxPaintDC
dc( this );
1991 // nothing to draw or not the moment to draw it
1996 RecalculatePositions( false );
1998 GetListCtrl()->PrepareDC( dc
);
2001 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2003 dc
.SetFont( GetFont() );
2005 if ( InReportView() )
2007 int lineHeight
= GetLineHeight();
2009 size_t visibleFrom
, visibleTo
;
2010 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2013 int xOrig
= dc
.LogicalToDeviceX( 0 );
2014 int yOrig
= dc
.LogicalToDeviceY( 0 );
2016 // tell the caller cache to cache the data
2019 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2020 GetParent()->GetId());
2021 evCache
.SetEventObject( GetParent() );
2022 evCache
.m_oldItemIndex
= visibleFrom
;
2023 evCache
.m_itemIndex
= visibleTo
;
2024 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2027 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2029 rectLine
= GetLineRect(line
);
2032 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2033 rectLine
.width
, rectLine
.height
) )
2035 // don't redraw unaffected lines to avoid flicker
2039 GetLine(line
)->DrawInReportMode( &dc
,
2041 GetLineHighlightRect(line
),
2042 IsHighlighted(line
),
2043 line
== m_current
);
2046 if ( HasFlag(wxLC_HRULES
) )
2048 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2049 wxSize clientSize
= GetClientSize();
2051 size_t i
= visibleFrom
;
2052 if (i
== 0) i
= 1; // Don't draw the first one
2053 for ( ; i
<= visibleTo
; i
++ )
2056 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2057 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2058 clientSize
.x
- dev_x
, i
* lineHeight
);
2061 // Draw last horizontal rule
2062 if ( visibleTo
== GetItemCount() - 1 )
2065 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2066 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2067 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2071 // Draw vertical rules if required
2072 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2074 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2075 wxRect firstItemRect
, lastItemRect
;
2077 GetItemRect(visibleFrom
, firstItemRect
);
2078 GetItemRect(visibleTo
, lastItemRect
);
2079 int x
= firstItemRect
.GetX();
2081 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2083 for (int col
= 0; col
< GetColumnCount(); col
++)
2085 int colWidth
= GetColumnWidth(col
);
2087 int x_pos
= x
- dev_x
;
2088 if (col
< GetColumnCount()-1) x_pos
-= 2;
2089 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2090 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2096 size_t count
= GetItemCount();
2097 for ( size_t i
= 0; i
< count
; i
++ )
2099 GetLine(i
)->Draw( &dc
, i
== m_current
);
2103 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2104 // rectangle somehow and so leaves traces when the item is not selected any
2105 // more, see #12229.
2110 if ( IsHighlighted(m_current
) )
2111 flags
|= wxCONTROL_SELECTED
;
2113 wxRendererNative::Get().
2114 DrawFocusRect(this, dc
, GetLineHighlightRect(m_current
), flags
);
2116 #endif // !__WXMAC__
2119 void wxListMainWindow::HighlightAll( bool on
)
2121 if ( IsSingleSel() )
2123 wxASSERT_MSG( !on
, wxT("can't do this in a single selection control") );
2125 // we just have one item to turn off
2126 if ( HasCurrent() && IsHighlighted(m_current
) )
2128 HighlightLine(m_current
, false);
2129 RefreshLine(m_current
);
2132 else // multi selection
2135 HighlightLines(0, GetItemCount() - 1, on
);
2139 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2141 // Do nothing here. This prevents the default handler in wxScrolledWindow
2142 // from needlessly scrolling the window when the edit control is
2143 // dismissed. See ticket #9563.
2146 void wxListMainWindow::SendNotify( size_t line
,
2147 wxEventType command
,
2148 const wxPoint
& point
)
2150 wxListEvent
le( command
, GetParent()->GetId() );
2151 le
.SetEventObject( GetParent() );
2153 le
.m_itemIndex
= line
;
2155 // set only for events which have position
2156 if ( point
!= wxDefaultPosition
)
2157 le
.m_pointDrag
= point
;
2159 // don't try to get the line info for virtual list controls: the main
2160 // program has it anyhow and if we did it would result in accessing all
2161 // the lines, even those which are not visible now and this is precisely
2162 // what we're trying to avoid
2165 if ( line
!= (size_t)-1 )
2167 GetLine(line
)->GetItem( 0, le
.m_item
);
2169 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2171 //else: there may be no more such item
2173 GetParent()->GetEventHandler()->ProcessEvent( le
);
2176 void wxListMainWindow::ChangeCurrent(size_t current
)
2178 m_current
= current
;
2180 // as the current item changed, we shouldn't start editing it when the
2181 // "slow click" timer expires as the click happened on another item
2182 if ( m_renameTimer
->IsRunning() )
2183 m_renameTimer
->Stop();
2185 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2188 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2190 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2191 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2193 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2194 wxT("EditLabel() needs a text control") );
2196 size_t itemEdit
= (size_t)item
;
2198 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2199 le
.SetEventObject( GetParent() );
2200 le
.m_itemIndex
= item
;
2201 wxListLineData
*data
= GetLine(itemEdit
);
2202 wxCHECK_MSG( data
, NULL
, wxT("invalid index in EditLabel()") );
2203 data
->GetItem( 0, le
.m_item
);
2205 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2207 // vetoed by user code
2211 // We have to call this here because the label in question might just have
2212 // been added and no screen update taken place.
2215 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2216 // so that no pending events may change the item count (see below)
2217 // IMPORTANT: needs to be tested!
2220 // Pending events dispatched by wxSafeYield might have changed the item
2222 if ( (size_t)item
>= GetItemCount() )
2226 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2227 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2228 return m_textctrlWrapper
->GetText();
2231 void wxListMainWindow::OnRenameTimer()
2233 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2235 EditLabel( m_current
);
2238 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2240 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2241 le
.SetEventObject( GetParent() );
2242 le
.m_itemIndex
= itemEdit
;
2244 wxListLineData
*data
= GetLine(itemEdit
);
2246 wxCHECK_MSG( data
, false, wxT("invalid index in OnRenameAccept()") );
2248 data
->GetItem( 0, le
.m_item
);
2249 le
.m_item
.m_text
= value
;
2250 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2254 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2256 // let owner know that the edit was cancelled
2257 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2259 le
.SetEditCanceled(true);
2261 le
.SetEventObject( GetParent() );
2262 le
.m_itemIndex
= itemEdit
;
2264 wxListLineData
*data
= GetLine(itemEdit
);
2265 wxCHECK_RET( data
, wxT("invalid index in OnRenameCancelled()") );
2267 data
->GetItem( 0, le
.m_item
);
2268 GetEventHandler()->ProcessEvent( le
);
2271 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2274 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2275 // shutdown the edit control when the mouse is clicked elsewhere on the
2276 // listctrl because the order of events is different (or something like
2277 // that), so explicitly end the edit if it is active.
2278 if ( event
.LeftDown() && m_textctrlWrapper
)
2279 m_textctrlWrapper
->EndEdit(wxListTextCtrlWrapper::End_Accept
);
2282 if ( event
.LeftDown() )
2285 event
.SetEventObject( GetParent() );
2286 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2289 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
2291 // let the base class handle mouse wheel events.
2296 if ( !HasCurrent() || IsEmpty() )
2298 if (event
.RightDown())
2300 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2302 wxContextMenuEvent
evtCtx(wxEVT_CONTEXT_MENU
,
2303 GetParent()->GetId(),
2304 ClientToScreen(event
.GetPosition()));
2305 evtCtx
.SetEventObject(GetParent());
2306 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2314 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2315 event
.ButtonDClick()) )
2318 int x
= event
.GetX();
2319 int y
= event
.GetY();
2320 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2322 // where did we hit it (if we did)?
2325 size_t count
= GetItemCount(),
2328 if ( InReportView() )
2330 current
= y
/ GetLineHeight();
2331 if ( current
< count
)
2332 hitResult
= HitTestLine(current
, x
, y
);
2336 // TODO: optimize it too! this is less simple than for report view but
2337 // enumerating all items is still not a way to do it!!
2338 for ( current
= 0; current
< count
; current
++ )
2340 hitResult
= HitTestLine(current
, x
, y
);
2346 if (event
.Dragging())
2348 if (m_dragCount
== 0)
2350 // we have to report the raw, physical coords as we want to be
2351 // able to call HitTest(event.m_pointDrag) from the user code to
2352 // get the item being dragged
2353 m_dragStart
= event
.GetPosition();
2358 if (m_dragCount
!= 3)
2361 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2362 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2364 wxListEvent
le( command
, GetParent()->GetId() );
2365 le
.SetEventObject( GetParent() );
2366 le
.m_itemIndex
= m_lineLastClicked
;
2367 le
.m_pointDrag
= m_dragStart
;
2368 GetParent()->GetEventHandler()->ProcessEvent( le
);
2379 // outside of any item
2380 if (event
.RightDown())
2382 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2384 wxContextMenuEvent
evtCtx(
2386 GetParent()->GetId(),
2387 ClientToScreen(event
.GetPosition()));
2388 evtCtx
.SetEventObject(GetParent());
2389 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
2393 // reset the selection and bail out
2394 HighlightAll(false);
2400 bool forceClick
= false;
2401 if (event
.ButtonDClick())
2403 if ( m_renameTimer
->IsRunning() )
2404 m_renameTimer
->Stop();
2406 m_lastOnSame
= false;
2408 if ( current
== m_lineLastClicked
)
2410 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2416 // The first click was on another item, so don't interpret this as
2417 // a double click, but as a simple click instead
2424 if (m_lineSelectSingleOnUp
!= (size_t)-1)
2426 // select single line
2427 HighlightAll( false );
2428 ReverseHighlight(m_lineSelectSingleOnUp
);
2433 if ((current
== m_current
) &&
2434 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2435 HasFlag(wxLC_EDIT_LABELS
) )
2437 if ( !InReportView() ||
2438 GetLineLabelRect(current
).Contains(x
, y
) )
2440 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
2441 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
2446 m_lastOnSame
= false;
2447 m_lineSelectSingleOnUp
= (size_t)-1;
2451 // This is necessary, because after a DnD operation in
2452 // from and to ourself, the up event is swallowed by the
2453 // DnD code. So on next non-up event (which means here and
2454 // now) m_lineSelectSingleOnUp should be reset.
2455 m_lineSelectSingleOnUp
= (size_t)-1;
2457 if (event
.RightDown())
2459 m_lineBeforeLastClicked
= m_lineLastClicked
;
2460 m_lineLastClicked
= current
;
2462 // If the item is already selected, do not update the selection.
2463 // Multi-selections should not be cleared if a selected item is clicked.
2464 if (!IsHighlighted(current
))
2466 HighlightAll(false);
2467 ChangeCurrent(current
);
2468 ReverseHighlight(m_current
);
2471 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
2473 // Allow generation of context menu event
2476 else if (event
.MiddleDown())
2478 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2480 else if ( event
.LeftDown() || forceClick
)
2482 m_lineBeforeLastClicked
= m_lineLastClicked
;
2483 m_lineLastClicked
= current
;
2485 size_t oldCurrent
= m_current
;
2486 bool oldWasSelected
= IsHighlighted(m_current
);
2488 bool cmdModifierDown
= event
.CmdDown();
2489 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
2491 if ( IsSingleSel() || !IsHighlighted(current
) )
2493 HighlightAll( false );
2495 ChangeCurrent(current
);
2497 ReverseHighlight(m_current
);
2499 else // multi sel & current is highlighted & no mod keys
2501 m_lineSelectSingleOnUp
= current
;
2502 ChangeCurrent(current
); // change focus
2505 else // multi sel & either ctrl or shift is down
2507 if (cmdModifierDown
)
2509 ChangeCurrent(current
);
2511 ReverseHighlight(m_current
);
2513 else if (event
.ShiftDown())
2515 ChangeCurrent(current
);
2517 size_t lineFrom
= oldCurrent
,
2520 if ( lineTo
< lineFrom
)
2523 lineFrom
= m_current
;
2526 HighlightLines(lineFrom
, lineTo
);
2528 else // !ctrl, !shift
2530 // test in the enclosing if should make it impossible
2531 wxFAIL_MSG( wxT("how did we get here?") );
2535 if (m_current
!= oldCurrent
)
2536 RefreshLine( oldCurrent
);
2538 // forceClick is only set if the previous click was on another item
2539 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
2543 void wxListMainWindow::MoveToItem(size_t item
)
2545 if ( item
== (size_t)-1 )
2548 wxRect rect
= GetLineRect(item
);
2550 int client_w
, client_h
;
2551 GetClientSize( &client_w
, &client_h
);
2553 const int hLine
= GetLineHeight();
2555 int view_x
= SCROLL_UNIT_X
* GetListCtrl()->GetScrollPos( wxHORIZONTAL
);
2556 int view_y
= hLine
* GetListCtrl()->GetScrollPos( wxVERTICAL
);
2558 if ( InReportView() )
2560 // the next we need the range of lines shown it might be different,
2561 // so recalculate it
2562 ResetVisibleLinesRange();
2564 if (rect
.y
< view_y
)
2565 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
2566 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
2567 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
2570 // At least on Mac the visible lines value will get reset inside of
2571 // Scroll *before* it actually scrolls the window because of the
2572 // Update() that happens there, so it will still have the wrong value.
2573 // So let's reset it again and wait for it to be recalculated in the
2574 // next paint event. I would expect this problem to show up in wxGTK
2575 // too but couldn't duplicate it there. Perhaps the order of events
2576 // is different... --Robin
2577 ResetVisibleLinesRange();
2585 if (rect
.x
-view_x
< 5)
2586 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
2587 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
2588 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
2590 if (rect
.y
-view_y
< 5)
2591 sy
= (rect
.y
- 5) / hLine
;
2592 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
2593 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
2595 GetListCtrl()->Scroll(sx
, sy
);
2599 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
2601 if ( !InReportView() )
2603 // TODO: this should work in all views but is not implemented now
2608 GetVisibleLinesRange(&top
, &bottom
);
2610 if ( bottom
== (size_t)-1 )
2613 ResetVisibleLinesRange();
2615 int hLine
= GetLineHeight();
2617 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
2620 // see comment in MoveToItem() for why we do this
2621 ResetVisibleLinesRange();
2627 // ----------------------------------------------------------------------------
2628 // keyboard handling
2629 // ----------------------------------------------------------------------------
2631 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2633 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2634 wxT("invalid item index in OnArrowChar()") );
2636 size_t oldCurrent
= m_current
;
2638 // in single selection we just ignore Shift as we can't select several
2640 if ( event
.ShiftDown() && !IsSingleSel() )
2642 ChangeCurrent(newCurrent
);
2644 // refresh the old focus to remove it
2645 RefreshLine( oldCurrent
);
2647 // select all the items between the old and the new one
2648 if ( oldCurrent
> newCurrent
)
2650 newCurrent
= oldCurrent
;
2651 oldCurrent
= m_current
;
2654 HighlightLines(oldCurrent
, newCurrent
);
2658 // all previously selected items are unselected unless ctrl is held
2659 // in a multiselection control
2660 if ( !event
.ControlDown() || IsSingleSel() )
2661 HighlightAll(false);
2663 ChangeCurrent(newCurrent
);
2665 // refresh the old focus to remove it
2666 RefreshLine( oldCurrent
);
2668 // in single selection mode we must always have a selected item
2669 if ( !event
.ControlDown() || IsSingleSel() )
2670 HighlightLine( m_current
, true );
2673 RefreshLine( m_current
);
2678 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2680 wxWindow
*parent
= GetParent();
2682 // propagate the key event upwards
2683 wxKeyEvent
ke(event
);
2684 ke
.SetEventObject( parent
);
2685 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2688 // send a list event
2689 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, parent
->GetId() );
2690 le
.m_itemIndex
= m_current
;
2692 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2693 le
.m_code
= event
.GetKeyCode();
2694 le
.SetEventObject( parent
);
2695 if (parent
->GetEventHandler()->ProcessEvent( le
))
2701 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
2703 wxWindow
*parent
= GetParent();
2705 // propagate the key event upwards
2706 wxKeyEvent
ke(event
);
2707 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2713 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2715 wxWindow
*parent
= GetParent();
2717 // propagate the char event upwards
2718 wxKeyEvent
ke(event
);
2719 ke
.SetEventObject( parent
);
2720 if (parent
->GetEventHandler()->ProcessEvent( ke
))
2723 if ( HandleAsNavigationKey(event
) )
2726 // no item -> nothing to do
2733 // don't use m_linesPerPage directly as it might not be computed yet
2734 const int pageSize
= GetCountPerPage();
2735 wxCHECK_RET( pageSize
, wxT("should have non zero page size") );
2737 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2739 if (event
.GetKeyCode() == WXK_RIGHT
)
2740 event
.m_keyCode
= WXK_LEFT
;
2741 else if (event
.GetKeyCode() == WXK_LEFT
)
2742 event
.m_keyCode
= WXK_RIGHT
;
2745 switch ( event
.GetKeyCode() )
2748 if ( m_current
> 0 )
2749 OnArrowChar( m_current
- 1, event
);
2753 if ( m_current
< (size_t)GetItemCount() - 1 )
2754 OnArrowChar( m_current
+ 1, event
);
2759 OnArrowChar( GetItemCount() - 1, event
);
2764 OnArrowChar( 0, event
);
2769 int steps
= InReportView() ? pageSize
- 1
2770 : m_current
% pageSize
;
2772 int index
= m_current
- steps
;
2776 OnArrowChar( index
, event
);
2782 int steps
= InReportView()
2784 : pageSize
- (m_current
% pageSize
) - 1;
2786 size_t index
= m_current
+ steps
;
2787 size_t count
= GetItemCount();
2788 if ( index
>= count
)
2791 OnArrowChar( index
, event
);
2796 if ( !InReportView() )
2798 int index
= m_current
- pageSize
;
2802 OnArrowChar( index
, event
);
2807 if ( !InReportView() )
2809 size_t index
= m_current
+ pageSize
;
2811 size_t count
= GetItemCount();
2812 if ( index
>= count
)
2815 OnArrowChar( index
, event
);
2820 if ( IsSingleSel() )
2822 if ( event
.ControlDown() )
2824 ReverseHighlight(m_current
);
2826 else // normal space press
2828 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2831 else // multiple selection
2833 ReverseHighlight(m_current
);
2839 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2847 // ----------------------------------------------------------------------------
2849 // ----------------------------------------------------------------------------
2851 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2855 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2856 event
.SetEventObject( GetParent() );
2857 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2861 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2862 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2863 // which are already drawn correctly resulting in horrible flicker - avoid
2873 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2877 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
2878 event
.SetEventObject( GetParent() );
2879 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2887 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2889 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2891 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2893 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2895 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2897 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2899 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2901 else if ( InReportView() && (m_small_image_list
))
2903 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2907 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
2909 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2911 m_normal_image_list
->GetSize( index
, width
, height
);
2913 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2915 m_small_image_list
->GetSize( index
, width
, height
);
2917 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2919 m_small_image_list
->GetSize( index
, width
, height
);
2921 else if ( InReportView() && m_small_image_list
)
2923 m_small_image_list
->GetSize( index
, width
, height
);
2932 int wxListMainWindow::GetTextLength( const wxString
&s
) const
2934 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
2935 dc
.SetFont( GetFont() );
2938 dc
.GetTextExtent( s
, &lw
, NULL
);
2940 return lw
+ AUTOSIZE_COL_MARGIN
;
2943 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2947 // calc the spacing from the icon size
2948 int width
= 0, height
= 0;
2950 if ((imageList
) && (imageList
->GetImageCount()) )
2951 imageList
->GetSize(0, width
, height
);
2953 if (which
== wxIMAGE_LIST_NORMAL
)
2955 m_normal_image_list
= imageList
;
2956 m_normal_spacing
= width
+ 8;
2959 if (which
== wxIMAGE_LIST_SMALL
)
2961 m_small_image_list
= imageList
;
2962 m_small_spacing
= width
+ 14;
2963 m_lineHeight
= 0; // ensure that the line height will be recalc'd
2967 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
2971 m_small_spacing
= spacing
;
2973 m_normal_spacing
= spacing
;
2976 int wxListMainWindow::GetItemSpacing( bool isSmall
)
2978 return isSmall
? m_small_spacing
: m_normal_spacing
;
2981 // ----------------------------------------------------------------------------
2983 // ----------------------------------------------------------------------------
2985 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
2987 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
2989 wxCHECK_RET( node
, wxT("invalid column index in SetColumn") );
2991 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2992 item
.m_width
= GetTextLength( item
.m_text
);
2994 wxListHeaderData
*column
= node
->GetData();
2995 column
->SetItem( item
);
2997 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
2999 headerWin
->m_dirty
= true;
3003 // invalidate it as it has to be recalculated
3007 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3009 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3010 wxT("invalid column index") );
3012 wxCHECK_RET( InReportView(),
3013 wxT("SetColumnWidth() can only be called in report mode.") );
3017 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3019 headerWin
->m_dirty
= true;
3021 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3022 wxCHECK_RET( node
, wxT("no column?") );
3024 wxListHeaderData
*column
= node
->GetData();
3026 size_t count
= GetItemCount();
3028 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3030 width
= GetTextLength(column
->GetText());
3031 width
+= 2*EXTRA_WIDTH
;
3033 // check for column header's image availability
3034 const int image
= column
->GetImage();
3037 if ( m_small_image_list
)
3040 m_small_image_list
->GetSize(image
, ix
, iy
);
3041 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3045 else if ( width
== wxLIST_AUTOSIZE
)
3049 // TODO: determine the max width somehow...
3050 width
= WIDTH_COL_DEFAULT
;
3054 wxClientDC
dc(this);
3055 dc
.SetFont( GetFont() );
3057 int max
= AUTOSIZE_COL_MARGIN
;
3059 // if the cached column width isn't valid then recalculate it
3060 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3062 for (size_t i
= 0; i
< count
; i
++)
3064 wxListLineData
*line
= GetLine( i
);
3065 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3067 wxCHECK_RET( n
, wxT("no subitem?") );
3069 wxListItemData
*itemData
= n
->GetData();
3072 itemData
->GetItem(item
);
3073 int itemWidth
= GetItemWidthWithImage(&item
);
3074 if (itemWidth
> max
)
3078 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3079 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3082 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3083 width
= max
+ AUTOSIZE_COL_MARGIN
;
3087 column
->SetWidth( width
);
3089 // invalidate it as it has to be recalculated
3093 int wxListMainWindow::GetHeaderWidth() const
3095 if ( !m_headerWidth
)
3097 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3099 size_t count
= GetColumnCount();
3100 for ( size_t col
= 0; col
< count
; col
++ )
3102 self
->m_headerWidth
+= GetColumnWidth(col
);
3106 return m_headerWidth
;
3109 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3111 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3112 wxCHECK_RET( node
, wxT("invalid column index in GetColumn") );
3114 wxListHeaderData
*column
= node
->GetData();
3115 column
->GetItem( item
);
3118 int wxListMainWindow::GetColumnWidth( int col
) const
3120 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3121 wxCHECK_MSG( node
, 0, wxT("invalid column index") );
3123 wxListHeaderData
*column
= node
->GetData();
3124 return column
->GetWidth();
3127 // ----------------------------------------------------------------------------
3129 // ----------------------------------------------------------------------------
3131 void wxListMainWindow::SetItem( wxListItem
&item
)
3133 long id
= item
.m_itemId
;
3134 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3135 wxT("invalid item index in SetItem") );
3139 wxListLineData
*line
= GetLine((size_t)id
);
3140 line
->SetItem( item
.m_col
, item
);
3142 // Set item state if user wants
3143 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3144 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3148 // update the Max Width Cache if needed
3149 int width
= GetItemWidthWithImage(&item
);
3151 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3152 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3156 // update the item on screen
3158 GetItemRect(id
, rectItem
);
3159 RefreshRect(rectItem
);
3162 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3167 // first deal with selection
3168 if ( stateMask
& wxLIST_STATE_SELECTED
)
3170 // set/clear select state
3173 // optimized version for virtual listctrl.
3174 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3177 else if ( state
& wxLIST_STATE_SELECTED
)
3179 const long count
= GetItemCount();
3180 for( long i
= 0; i
< count
; i
++ )
3182 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3188 // clear for non virtual (somewhat optimized by using GetNextItem())
3190 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3192 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3197 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3199 // unfocus all: only one item can be focussed, so clearing focus for
3200 // all items is simply clearing focus of the focussed item.
3201 SetItemState(m_current
, state
, stateMask
);
3203 //(setting focus to all items makes no sense, so it is not handled here.)
3206 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3210 SetItemStateAll(state
, stateMask
);
3214 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3215 wxT("invalid list ctrl item index in SetItem") );
3217 size_t oldCurrent
= m_current
;
3218 size_t item
= (size_t)litem
; // safe because of the check above
3220 // do we need to change the focus?
3221 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3223 if ( state
& wxLIST_STATE_FOCUSED
)
3225 // don't do anything if this item is already focused
3226 if ( item
!= m_current
)
3228 ChangeCurrent(item
);
3230 if ( oldCurrent
!= (size_t)-1 )
3232 if ( IsSingleSel() )
3234 HighlightLine(oldCurrent
, false);
3237 RefreshLine(oldCurrent
);
3240 RefreshLine( m_current
);
3245 // don't do anything if this item is not focused
3246 if ( item
== m_current
)
3250 if ( IsSingleSel() )
3252 // we must unselect the old current item as well or we
3253 // might end up with more than one selected item in a
3254 // single selection control
3255 HighlightLine(oldCurrent
, false);
3258 RefreshLine( oldCurrent
);
3263 // do we need to change the selection state?
3264 if ( stateMask
& wxLIST_STATE_SELECTED
)
3266 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3268 if ( IsSingleSel() )
3272 // selecting the item also makes it the focused one in the
3274 if ( m_current
!= item
)
3276 ChangeCurrent(item
);
3278 if ( oldCurrent
!= (size_t)-1 )
3280 HighlightLine( oldCurrent
, false );
3281 RefreshLine( oldCurrent
);
3287 // only the current item may be selected anyhow
3288 if ( item
!= m_current
)
3293 if ( HighlightLine(item
, on
) )
3300 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3302 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3303 wxT("invalid list ctrl item index in GetItemState()") );
3305 int ret
= wxLIST_STATE_DONTCARE
;
3307 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3309 if ( (size_t)item
== m_current
)
3310 ret
|= wxLIST_STATE_FOCUSED
;
3313 if ( stateMask
& wxLIST_STATE_SELECTED
)
3315 if ( IsHighlighted(item
) )
3316 ret
|= wxLIST_STATE_SELECTED
;
3322 void wxListMainWindow::GetItem( wxListItem
&item
) const
3324 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3325 wxT("invalid item index in GetItem") );
3327 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3328 line
->GetItem( item
.m_col
, item
);
3330 // Get item state if user wants it
3331 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3332 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3333 wxLIST_STATE_FOCUSED
);
3336 // ----------------------------------------------------------------------------
3338 // ----------------------------------------------------------------------------
3340 size_t wxListMainWindow::GetItemCount() const
3342 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3345 void wxListMainWindow::SetItemCount(long count
)
3347 m_selStore
.SetItemCount(count
);
3348 m_countVirt
= count
;
3350 ResetVisibleLinesRange();
3352 // scrollbars must be reset
3356 int wxListMainWindow::GetSelectedItemCount() const
3358 // deal with the quick case first
3359 if ( IsSingleSel() )
3360 return HasCurrent() ? IsHighlighted(m_current
) : false;
3362 // virtual controls remmebers all its selections itself
3364 return m_selStore
.GetSelectedCount();
3366 // TODO: we probably should maintain the number of items selected even for
3367 // non virtual controls as enumerating all lines is really slow...
3368 size_t countSel
= 0;
3369 size_t count
= GetItemCount();
3370 for ( size_t line
= 0; line
< count
; line
++ )
3372 if ( GetLine(line
)->IsHighlighted() )
3379 // ----------------------------------------------------------------------------
3380 // item position/size
3381 // ----------------------------------------------------------------------------
3383 wxRect
wxListMainWindow::GetViewRect() const
3385 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
3387 // we need to find the longest/tallest label
3388 wxCoord xMax
= 0, yMax
= 0;
3389 const int count
= GetItemCount();
3392 for ( int i
= 0; i
< count
; i
++ )
3394 // we need logical, not physical, coordinates here, so use
3395 // GetLineRect() instead of GetItemRect()
3396 wxRect r
= GetLineRect(i
);
3398 wxCoord x
= r
.GetRight(),
3408 // some fudge needed to make it look prettier
3409 xMax
+= 2 * EXTRA_BORDER_X
;
3410 yMax
+= 2 * EXTRA_BORDER_Y
;
3412 // account for the scrollbars if necessary
3413 const wxSize sizeAll
= GetClientSize();
3414 if ( xMax
> sizeAll
.x
)
3415 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3416 if ( yMax
> sizeAll
.y
)
3417 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3419 return wxRect(0, 0, xMax
, yMax
);
3423 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
3425 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
3427 wxT("GetSubItemRect only meaningful in report view") );
3428 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
3429 wxT("invalid item in GetSubItemRect") );
3431 // ensure that we're laid out, otherwise we could return nonsense
3434 wxConstCast(this, wxListMainWindow
)->
3435 RecalculatePositions(true /* no refresh */);
3438 rect
= GetLineRect((size_t)item
);
3440 // Adjust rect to specified column
3441 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
3443 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
3444 wxT("invalid subItem in GetSubItemRect") );
3446 for (int i
= 0; i
< subItem
; i
++)
3448 rect
.x
+= GetColumnWidth(i
);
3450 rect
.width
= GetColumnWidth(subItem
);
3453 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3458 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3461 GetItemRect(item
, rect
);
3469 // ----------------------------------------------------------------------------
3470 // geometry calculation
3471 // ----------------------------------------------------------------------------
3473 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3475 const int lineHeight
= GetLineHeight();
3477 wxClientDC
dc( this );
3478 dc
.SetFont( GetFont() );
3480 const size_t count
= GetItemCount();
3483 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3484 iconSpacing
= m_normal_spacing
;
3485 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3486 iconSpacing
= m_small_spacing
;
3490 // Note that we do not call GetClientSize() here but
3491 // GetSize() and subtract the border size for sunken
3492 // borders manually. This is technically incorrect,
3493 // but we need to know the client area's size WITHOUT
3494 // scrollbars here. Since we don't know if there are
3495 // any scrollbars, we use GetSize() instead. Another
3496 // solution would be to call SetScrollbars() here to
3497 // remove the scrollbars and call GetClientSize() then,
3498 // but this might result in flicker and - worse - will
3499 // reset the scrollbars to 0 which is not good at all
3500 // if you resize a dialog/window, but don't want to
3501 // reset the window scrolling. RR.
3502 // Furthermore, we actually do NOT subtract the border
3503 // width as 2 pixels is just the extra space which we
3504 // need around the actual content in the window. Other-
3505 // wise the text would e.g. touch the upper border. RR.
3508 GetSize( &clientWidth
, &clientHeight
);
3510 if ( InReportView() )
3512 // all lines have the same height and we scroll one line per step
3513 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
3515 m_linesPerPage
= clientHeight
/ lineHeight
;
3517 ResetVisibleLinesRange();
3519 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3520 GetHeaderWidth() / SCROLL_UNIT_X
,
3521 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3522 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
3523 GetListCtrl()->GetScrollPos(wxVERTICAL
),
3528 // we have 3 different layout strategies: either layout all items
3529 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3530 // to arrange them in top to bottom, left to right (don't ask me why
3531 // not the other way round...) order
3532 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3534 int x
= EXTRA_BORDER_X
;
3535 int y
= EXTRA_BORDER_Y
;
3537 wxCoord widthMax
= 0;
3540 for ( i
= 0; i
< count
; i
++ )
3542 wxListLineData
*line
= GetLine(i
);
3543 line
->CalculateSize( &dc
, iconSpacing
);
3544 line
->SetPosition( x
, y
, iconSpacing
);
3546 wxSize sizeLine
= GetLineSize(i
);
3548 if ( HasFlag(wxLC_ALIGN_TOP
) )
3550 if ( sizeLine
.x
> widthMax
)
3551 widthMax
= sizeLine
.x
;
3555 else // wxLC_ALIGN_LEFT
3557 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3561 if ( HasFlag(wxLC_ALIGN_TOP
) )
3563 // traverse the items again and tweak their sizes so that they are
3564 // all the same in a row
3565 for ( i
= 0; i
< count
; i
++ )
3567 wxListLineData
*line
= GetLine(i
);
3568 line
->m_gi
->ExtendWidth(widthMax
);
3572 GetListCtrl()->SetScrollbars
3576 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3577 (y
+ lineHeight
) / lineHeight
,
3578 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3579 GetListCtrl()->GetScrollPos( wxVERTICAL
),
3583 else // "flowed" arrangement, the most complicated case
3585 // at first we try without any scrollbars, if the items don't fit into
3586 // the window, we recalculate after subtracting the space taken by the
3589 int entireWidth
= 0;
3591 for (int tries
= 0; tries
< 2; tries
++)
3593 entireWidth
= 2 * EXTRA_BORDER_X
;
3597 // Now we have decided that the items do not fit into the
3598 // client area, so we need a scrollbar
3599 entireWidth
+= SCROLL_UNIT_X
;
3602 int x
= EXTRA_BORDER_X
;
3603 int y
= EXTRA_BORDER_Y
;
3604 int maxWidthInThisRow
= 0;
3607 int currentlyVisibleLines
= 0;
3609 for (size_t i
= 0; i
< count
; i
++)
3611 currentlyVisibleLines
++;
3612 wxListLineData
*line
= GetLine( i
);
3613 line
->CalculateSize( &dc
, iconSpacing
);
3614 line
->SetPosition( x
, y
, iconSpacing
);
3616 wxSize sizeLine
= GetLineSize( i
);
3618 if ( maxWidthInThisRow
< sizeLine
.x
)
3619 maxWidthInThisRow
= sizeLine
.x
;
3622 if (currentlyVisibleLines
> m_linesPerPage
)
3623 m_linesPerPage
= currentlyVisibleLines
;
3625 if ( y
+ sizeLine
.y
>= clientHeight
)
3627 currentlyVisibleLines
= 0;
3629 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3630 x
+= maxWidthInThisRow
;
3631 entireWidth
+= maxWidthInThisRow
;
3632 maxWidthInThisRow
= 0;
3635 // We have reached the last item.
3636 if ( i
== count
- 1 )
3637 entireWidth
+= maxWidthInThisRow
;
3639 if ( (tries
== 0) &&
3640 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
3642 clientHeight
-= wxSystemSettings::
3643 GetMetric(wxSYS_HSCROLL_Y
);
3648 if ( i
== count
- 1 )
3649 tries
= 1; // Everything fits, no second try required.
3653 GetListCtrl()->SetScrollbars
3657 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3659 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
3668 // FIXME: why should we call it from here?
3675 void wxListMainWindow::RefreshAll()
3680 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3681 if ( headerWin
&& headerWin
->m_dirty
)
3683 headerWin
->m_dirty
= false;
3684 headerWin
->Refresh();
3688 void wxListMainWindow::UpdateCurrent()
3690 if ( !HasCurrent() && !IsEmpty() )
3694 long wxListMainWindow::GetNextItem( long item
,
3695 int WXUNUSED(geometry
),
3699 max
= GetItemCount();
3700 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3701 wxT("invalid listctrl index in GetNextItem()") );
3703 // notice that we start with the next item (or the first one if item == -1)
3704 // and this is intentional to allow writing a simple loop to iterate over
3705 // all selected items
3708 // this is not an error because the index was OK initially,
3709 // just no such item
3716 size_t count
= GetItemCount();
3717 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3719 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3722 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
3729 // ----------------------------------------------------------------------------
3731 // ----------------------------------------------------------------------------
3733 void wxListMainWindow::DeleteItem( long lindex
)
3735 size_t count
= GetItemCount();
3737 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
3738 wxT("invalid item index in DeleteItem") );
3740 size_t index
= (size_t)lindex
;
3742 // we don't need to adjust the index for the previous items
3743 if ( HasCurrent() && m_current
>= index
)
3745 // if the current item is being deleted, we want the next one to
3746 // become selected - unless there is no next one - so don't adjust
3747 // m_current in this case
3748 if ( m_current
!= index
|| m_current
== count
- 1 )
3752 if ( InReportView() )
3754 // mark the Column Max Width cache as dirty if the items in the line
3755 // we're deleting contain the Max Column Width
3756 wxListLineData
* const line
= GetLine(index
);
3757 wxListItemDataList::compatibility_iterator n
;
3758 wxListItemData
*itemData
;
3762 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
3764 n
= line
->m_items
.Item( i
);
3765 itemData
= n
->GetData();
3766 itemData
->GetItem(item
);
3768 itemWidth
= GetItemWidthWithImage(&item
);
3770 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
3771 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3774 ResetVisibleLinesRange();
3777 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
3782 m_selStore
.OnItemDelete(index
);
3786 m_lines
.RemoveAt( index
);
3789 // we need to refresh the (vert) scrollbar as the number of items changed
3792 RefreshAfter(index
);
3795 void wxListMainWindow::DeleteColumn( int col
)
3797 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3799 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3802 delete node
->GetData();
3803 m_columns
.Erase( node
);
3807 // update all the items
3808 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
3810 wxListLineData
* const line
= GetLine(i
);
3811 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3812 delete n
->GetData();
3813 line
->m_items
.Erase(n
);
3817 if ( InReportView() ) // we only cache max widths when in Report View
3819 delete m_aColWidths
.Item(col
);
3820 m_aColWidths
.RemoveAt(col
);
3823 // invalidate it as it has to be recalculated
3827 void wxListMainWindow::DoDeleteAllItems()
3830 // nothing to do - in particular, don't send the event
3835 // to make the deletion of all items faster, we don't send the
3836 // notifications for each item deletion in this case but only one event
3837 // for all of them: this is compatible with wxMSW and documented in
3838 // DeleteAllItems() description
3840 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3841 event
.SetEventObject( GetParent() );
3842 GetParent()->GetEventHandler()->ProcessEvent( event
);
3850 if ( InReportView() )
3852 ResetVisibleLinesRange();
3853 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
3855 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
3862 void wxListMainWindow::DeleteAllItems()
3866 RecalculatePositions();
3869 void wxListMainWindow::DeleteEverything()
3871 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
3872 WX_CLEAR_ARRAY(m_aColWidths
);
3877 // ----------------------------------------------------------------------------
3878 // scanning for an item
3879 // ----------------------------------------------------------------------------
3881 void wxListMainWindow::EnsureVisible( long index
)
3883 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3884 wxT("invalid index in EnsureVisible") );
3886 // We have to call this here because the label in question might just have
3887 // been added and its position is not known yet
3889 RecalculatePositions(true /* no refresh */);
3891 MoveToItem((size_t)index
);
3894 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
3900 wxString str_upper
= str
.Upper();
3904 size_t count
= GetItemCount();
3905 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3907 wxListLineData
*line
= GetLine(i
);
3908 wxString line_upper
= line
->GetText(0).Upper();
3911 if (line_upper
== str_upper
)
3916 if (line_upper
.find(str_upper
) == 0)
3924 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
3930 size_t count
= GetItemCount();
3931 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3933 wxListLineData
*line
= GetLine(i
);
3935 line
->GetItem( 0, item
);
3936 if (item
.m_data
== data
)
3943 long wxListMainWindow::FindItem( const wxPoint
& pt
)
3946 GetVisibleLinesRange( &topItem
, NULL
);
3949 GetItemPosition( GetItemCount() - 1, p
);
3953 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
3954 if ( id
>= 0 && id
< (long)GetItemCount() )
3960 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
3962 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3964 size_t count
= GetItemCount();
3966 if ( InReportView() )
3968 size_t current
= y
/ GetLineHeight();
3969 if ( current
< count
)
3971 flags
= HitTestLine(current
, x
, y
);
3978 // TODO: optimize it too! this is less simple than for report view but
3979 // enumerating all items is still not a way to do it!!
3980 for ( size_t current
= 0; current
< count
; current
++ )
3982 flags
= HitTestLine(current
, x
, y
);
3991 // ----------------------------------------------------------------------------
3993 // ----------------------------------------------------------------------------
3995 void wxListMainWindow::InsertItem( wxListItem
&item
)
3997 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
3999 int count
= GetItemCount();
4000 wxCHECK_RET( item
.m_itemId
>= 0, wxT("invalid item index") );
4002 if (item
.m_itemId
> count
)
4003 item
.m_itemId
= count
;
4005 size_t id
= item
.m_itemId
;
4009 if ( InReportView() )
4011 ResetVisibleLinesRange();
4013 const unsigned col
= item
.GetColumn();
4014 wxCHECK_RET( col
< m_aColWidths
.size(), "invalid item column" );
4016 // calculate the width of the item and adjust the max column width
4017 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(col
);
4018 int width
= GetItemWidthWithImage(&item
);
4019 item
.SetWidth(width
);
4020 if (width
> pWidthInfo
->nMaxWidth
)
4021 pWidthInfo
->nMaxWidth
= width
;
4024 wxListLineData
*line
= new wxListLineData(this);
4026 line
->SetItem( item
.m_col
, item
);
4028 m_lines
.Insert( line
, id
);
4032 // If an item is selected at or below the point of insertion, we need to
4033 // increment the member variables because the current row's index has gone
4035 if ( HasCurrent() && m_current
>= id
)
4038 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4040 RefreshLines(id
, GetItemCount() - 1);
4043 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4046 if ( InReportView() )
4048 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4049 item
.m_width
= GetTextLength( item
.m_text
);
4051 wxListHeaderData
*column
= new wxListHeaderData( item
);
4052 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4054 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4057 wxListHeaderDataList::compatibility_iterator
4058 node
= m_columns
.Item( col
);
4059 m_columns
.Insert( node
, column
);
4060 m_aColWidths
.Insert( colWidthInfo
, col
);
4064 m_columns
.Append( column
);
4065 m_aColWidths
.Add( colWidthInfo
);
4070 // update all the items
4071 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4073 wxListLineData
* const line
= GetLine(i
);
4074 wxListItemData
* const data
= new wxListItemData(this);
4076 line
->m_items
.Insert(col
, data
);
4078 line
->m_items
.Append(data
);
4082 // invalidate it as it has to be recalculated
4087 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4090 wxClientDC
dc(this);
4092 dc
.SetFont( GetFont() );
4094 if (item
->GetImage() != -1)
4097 GetImageSize( item
->GetImage(), ix
, iy
);
4101 if (!item
->GetText().empty())
4104 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4111 // ----------------------------------------------------------------------------
4113 // ----------------------------------------------------------------------------
4115 static wxListCtrlCompare list_ctrl_compare_func_2
;
4116 static wxIntPtr list_ctrl_compare_data
;
4118 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4120 wxListLineData
*line1
= *arg1
;
4121 wxListLineData
*line2
= *arg2
;
4123 line1
->GetItem( 0, item
);
4124 wxUIntPtr data1
= item
.m_data
;
4125 line2
->GetItem( 0, item
);
4126 wxUIntPtr data2
= item
.m_data
;
4127 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4130 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4132 // selections won't make sense any more after sorting the items so reset
4134 HighlightAll(false);
4137 list_ctrl_compare_func_2
= fn
;
4138 list_ctrl_compare_data
= data
;
4139 m_lines
.Sort( list_ctrl_compare_func_1
);
4143 // ----------------------------------------------------------------------------
4145 // ----------------------------------------------------------------------------
4147 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4149 // update our idea of which lines are shown when we redraw the window the
4151 ResetVisibleLinesRange();
4153 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4155 wxGenericListCtrl
* lc
= GetListCtrl();
4156 wxCHECK_RET( lc
, wxT("no listctrl window?") );
4158 if (lc
->m_headerWin
) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4160 lc
->m_headerWin
->Refresh();
4161 lc
->m_headerWin
->Update();
4166 int wxListMainWindow::GetCountPerPage() const
4168 if ( !m_linesPerPage
)
4170 wxConstCast(this, wxListMainWindow
)->
4171 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4174 return m_linesPerPage
;
4177 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4179 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4181 if ( m_lineFrom
== (size_t)-1 )
4183 size_t count
= GetItemCount();
4186 m_lineFrom
= GetListCtrl()->GetScrollPos(wxVERTICAL
);
4188 // this may happen if SetScrollbars() hadn't been called yet
4189 if ( m_lineFrom
>= count
)
4190 m_lineFrom
= count
- 1;
4192 // we redraw one extra line but this is needed to make the redrawing
4193 // logic work when there is a fractional number of lines on screen
4194 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4195 if ( m_lineTo
>= count
)
4196 m_lineTo
= count
- 1;
4198 else // empty control
4201 m_lineTo
= (size_t)-1;
4205 wxASSERT_MSG( IsEmpty() ||
4206 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4207 wxT("GetVisibleLinesRange() returns incorrect result") );
4215 // -------------------------------------------------------------------------------------
4216 // wxGenericListCtrl
4217 // -------------------------------------------------------------------------------------
4219 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4221 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4222 EVT_SIZE(wxGenericListCtrl::OnSize
)
4223 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4226 void wxGenericListCtrl::Init()
4228 m_imageListNormal
= NULL
;
4229 m_imageListSmall
= NULL
;
4230 m_imageListState
= NULL
;
4232 m_ownsImageListNormal
=
4233 m_ownsImageListSmall
=
4234 m_ownsImageListState
= false;
4240 wxGenericListCtrl::~wxGenericListCtrl()
4242 if (m_ownsImageListNormal
)
4243 delete m_imageListNormal
;
4244 if (m_ownsImageListSmall
)
4245 delete m_imageListSmall
;
4246 if (m_ownsImageListState
)
4247 delete m_imageListState
;
4250 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4252 bool needs_header
= HasHeader();
4253 bool has_header
= (m_headerWin
!= NULL
);
4255 if (needs_header
== has_header
)
4260 m_headerWin
= new wxListHeaderWindow
4262 this, wxID_ANY
, m_mainWin
,
4267 wxRendererNative::Get().GetHeaderButtonHeight(this)
4272 #if defined( __WXMAC__ )
4273 static wxFont
font( wxOSX_SYSTEM_FONT_SMALL
);
4274 m_headerWin
->SetFont( font
);
4277 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
4281 GetSizer()->Detach( m_headerWin
);
4283 wxDELETE(m_headerWin
);
4287 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4292 const wxValidator
&validator
,
4293 const wxString
&name
)
4297 // just like in other ports, an assert will fail if the user doesn't give any type style:
4298 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
4299 wxT("wxListCtrl style should have exactly one mode bit set") );
4301 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
4305 style
&= ~wxBORDER_MASK
;
4306 style
|= wxBORDER_THEME
;
4309 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4311 SetTargetWindow( m_mainWin
);
4313 // We use the cursor keys for moving the selection, not scrolling, so call
4314 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4315 // keyboard events forwarded to us from wxListMainWindow.
4316 DisableKeyboardScrolling();
4318 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
4319 sizer
->Add( m_mainWin
, 1, wxGROW
);
4322 CreateOrDestroyHeaderWindowAsNeeded();
4324 SetInitialSize(size
);
4329 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
4331 return wxBORDER_THEME
;
4334 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4335 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
4339 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
4341 // we need to process arrows ourselves for scrolling
4342 if ( nMsg
== WM_GETDLGCODE
)
4344 rc
|= DLGC_WANTARROWS
;
4351 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4353 wxSize newsize
= size
;
4355 newsize
.y
-= m_headerWin
->GetSize().y
;
4360 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
4362 // update our idea of which lines are shown when we redraw
4363 // the window the next time
4364 m_mainWin
->ResetVisibleLinesRange();
4366 HandleOnScroll( event
);
4368 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4370 m_headerWin
->Refresh();
4371 m_headerWin
->Update();
4375 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4377 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4378 wxT("wxLC_VIRTUAL can't be [un]set") );
4380 long flag
= GetWindowStyle();
4384 if (style
& wxLC_MASK_TYPE
)
4385 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4386 if (style
& wxLC_MASK_ALIGN
)
4387 flag
&= ~wxLC_MASK_ALIGN
;
4388 if (style
& wxLC_MASK_SORT
)
4389 flag
&= ~wxLC_MASK_SORT
;
4397 // some styles can be set without recreating everything (as happens in
4398 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4399 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
4402 wxWindow::SetWindowStyleFlag(flag
);
4406 SetWindowStyleFlag( flag
);
4410 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4412 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4413 // makes sense to remove them as we'll always add scrollbars anyhow when
4415 flag
|= wxHSCROLL
| wxVSCROLL
;
4417 const bool wasInReportView
= HasFlag(wxLC_REPORT
);
4419 // update the window style first so that the header is created or destroyed
4420 // corresponding to the new style
4421 wxWindow::SetWindowStyleFlag( flag
);
4425 const bool inReportView
= (flag
& wxLC_REPORT
) != 0;
4426 if ( inReportView
!= wasInReportView
)
4428 // we need to notify the main window about this change as it must
4429 // update its data structures
4430 m_mainWin
->SetReportView(inReportView
);
4433 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4435 CreateOrDestroyHeaderWindowAsNeeded();
4437 GetSizer()->Layout();
4441 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4443 m_mainWin
->GetColumn( col
, item
);
4447 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4449 m_mainWin
->SetColumn( col
, item
);
4453 int wxGenericListCtrl::GetColumnWidth( int col
) const
4455 return m_mainWin
->GetColumnWidth( col
);
4458 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4460 m_mainWin
->SetColumnWidth( col
, width
);
4464 int wxGenericListCtrl::GetCountPerPage() const
4466 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4469 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4471 m_mainWin
->GetItem( info
);
4475 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4477 m_mainWin
->SetItem( info
);
4481 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4484 info
.m_text
= label
;
4485 info
.m_mask
= wxLIST_MASK_TEXT
;
4486 info
.m_itemId
= index
;
4490 info
.m_image
= imageId
;
4491 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4494 m_mainWin
->SetItem(info
);
4498 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4500 return m_mainWin
->GetItemState( item
, stateMask
);
4503 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4505 m_mainWin
->SetItemState( item
, state
, stateMask
);
4510 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4512 return SetItemColumnImage(item
, 0, image
);
4516 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
4519 info
.m_image
= image
;
4520 info
.m_mask
= wxLIST_MASK_IMAGE
;
4521 info
.m_itemId
= item
;
4522 info
.m_col
= column
;
4523 m_mainWin
->SetItem( info
);
4527 wxString
wxGenericListCtrl::GetItemText( long item
, int col
) const
4529 return m_mainWin
->GetItemText(item
, col
);
4532 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4534 m_mainWin
->SetItemText(item
, str
);
4537 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
4540 info
.m_mask
= wxLIST_MASK_DATA
;
4541 info
.m_itemId
= item
;
4542 m_mainWin
->GetItem( info
);
4546 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
4549 info
.m_mask
= wxLIST_MASK_DATA
;
4550 info
.m_itemId
= item
;
4552 m_mainWin
->SetItem( info
);
4556 wxRect
wxGenericListCtrl::GetViewRect() const
4558 return m_mainWin
->GetViewRect();
4561 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
4563 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
4566 bool wxGenericListCtrl::GetSubItemRect(long item
,
4569 int WXUNUSED(code
)) const
4571 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
4574 if ( m_mainWin
->HasHeader() )
4575 rect
.y
+= m_headerWin
->GetSize().y
+ 1;
4580 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4582 m_mainWin
->GetItemPosition( item
, pos
);
4586 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4591 int wxGenericListCtrl::GetItemCount() const
4593 return m_mainWin
->GetItemCount();
4596 int wxGenericListCtrl::GetColumnCount() const
4598 return m_mainWin
->GetColumnCount();
4601 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4603 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4606 wxSize
wxGenericListCtrl::GetItemSpacing() const
4608 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4610 return wxSize(spacing
, spacing
);
4613 #if WXWIN_COMPATIBILITY_2_6
4614 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4616 return m_mainWin
->GetItemSpacing( isSmall
);
4618 #endif // WXWIN_COMPATIBILITY_2_6
4620 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4623 info
.m_itemId
= item
;
4624 info
.SetTextColour( col
);
4625 m_mainWin
->SetItem( info
);
4628 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4631 info
.m_itemId
= item
;
4632 m_mainWin
->GetItem( info
);
4633 return info
.GetTextColour();
4636 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4639 info
.m_itemId
= item
;
4640 info
.SetBackgroundColour( col
);
4641 m_mainWin
->SetItem( info
);
4644 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4647 info
.m_itemId
= item
;
4648 m_mainWin
->GetItem( info
);
4649 return info
.GetBackgroundColour();
4652 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
4655 info
.m_itemId
= item
;
4657 m_mainWin
->SetItem( info
);
4660 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
4663 info
.m_itemId
= item
;
4664 m_mainWin
->GetItem( info
);
4665 return info
.GetFont();
4668 int wxGenericListCtrl::GetSelectedItemCount() const
4670 return m_mainWin
->GetSelectedItemCount();
4673 wxColour
wxGenericListCtrl::GetTextColour() const
4675 return GetForegroundColour();
4678 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4680 SetForegroundColour(col
);
4683 long wxGenericListCtrl::GetTopItem() const
4686 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4690 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4692 return m_mainWin
->GetNextItem( item
, geom
, state
);
4695 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
4697 if (which
== wxIMAGE_LIST_NORMAL
)
4698 return m_imageListNormal
;
4699 else if (which
== wxIMAGE_LIST_SMALL
)
4700 return m_imageListSmall
;
4701 else if (which
== wxIMAGE_LIST_STATE
)
4702 return m_imageListState
;
4707 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4709 if ( which
== wxIMAGE_LIST_NORMAL
)
4711 if (m_ownsImageListNormal
)
4712 delete m_imageListNormal
;
4713 m_imageListNormal
= imageList
;
4714 m_ownsImageListNormal
= false;
4716 else if ( which
== wxIMAGE_LIST_SMALL
)
4718 if (m_ownsImageListSmall
)
4719 delete m_imageListSmall
;
4720 m_imageListSmall
= imageList
;
4721 m_ownsImageListSmall
= false;
4723 else if ( which
== wxIMAGE_LIST_STATE
)
4725 if (m_ownsImageListState
)
4726 delete m_imageListState
;
4727 m_imageListState
= imageList
;
4728 m_ownsImageListState
= false;
4731 m_mainWin
->SetImageList( imageList
, which
);
4734 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4736 SetImageList(imageList
, which
);
4737 if ( which
== wxIMAGE_LIST_NORMAL
)
4738 m_ownsImageListNormal
= true;
4739 else if ( which
== wxIMAGE_LIST_SMALL
)
4740 m_ownsImageListSmall
= true;
4741 else if ( which
== wxIMAGE_LIST_STATE
)
4742 m_ownsImageListState
= true;
4745 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4750 bool wxGenericListCtrl::DeleteItem( long item
)
4752 m_mainWin
->DeleteItem( item
);
4756 bool wxGenericListCtrl::DeleteAllItems()
4758 m_mainWin
->DeleteAllItems();
4762 bool wxGenericListCtrl::DeleteAllColumns()
4764 size_t count
= m_mainWin
->m_columns
.GetCount();
4765 for ( size_t n
= 0; n
< count
; n
++ )
4770 void wxGenericListCtrl::ClearAll()
4772 m_mainWin
->DeleteEverything();
4775 bool wxGenericListCtrl::DeleteColumn( int col
)
4777 m_mainWin
->DeleteColumn( col
);
4779 // if we don't have the header any longer, we need to relayout the window
4780 // if ( !GetColumnCount() )
4785 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
4786 wxClassInfo
* textControlClass
)
4788 return m_mainWin
->EditLabel( item
, textControlClass
);
4791 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
4793 return m_mainWin
->GetEditControl();
4796 bool wxGenericListCtrl::EnsureVisible( long item
)
4798 m_mainWin
->EnsureVisible( item
);
4802 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4804 return m_mainWin
->FindItem( start
, str
, partial
);
4807 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
4809 return m_mainWin
->FindItem( start
, data
);
4812 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
4813 int WXUNUSED(direction
))
4815 return m_mainWin
->FindItem( pt
);
4818 // TODO: sub item hit testing
4819 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
4821 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4824 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4826 m_mainWin
->InsertItem( info
);
4827 return info
.m_itemId
;
4830 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4833 info
.m_text
= label
;
4834 info
.m_mask
= wxLIST_MASK_TEXT
;
4835 info
.m_itemId
= index
;
4836 return InsertItem( info
);
4839 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4842 info
.m_mask
= wxLIST_MASK_IMAGE
;
4843 info
.m_image
= imageIndex
;
4844 info
.m_itemId
= index
;
4845 return InsertItem( info
);
4848 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4851 info
.m_text
= label
;
4852 info
.m_image
= imageIndex
;
4853 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4854 info
.m_itemId
= index
;
4855 return InsertItem( info
);
4858 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4860 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4862 m_mainWin
->InsertColumn( col
, item
);
4864 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4865 // still have m_headerWin==NULL
4867 m_headerWin
->Refresh();
4872 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4873 int format
, int width
)
4876 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4877 item
.m_text
= heading
;
4880 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4881 item
.m_width
= width
;
4884 item
.m_format
= format
;
4886 return InsertColumn( col
, item
);
4889 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
4891 return m_mainWin
->ScrollList(dx
, dy
);
4895 // fn is a function which takes 3 long arguments: item1, item2, data.
4896 // item1 is the long data associated with a first item (NOT the index).
4897 // item2 is the long data associated with a second item (NOT the index).
4898 // data is the same value as passed to SortItems.
4899 // The return value is a negative number if the first item should precede the second
4900 // item, a positive number of the second item should precede the first,
4901 // or zero if the two items are equivalent.
4902 // data is arbitrary data to be passed to the sort function.
4904 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, wxIntPtr data
)
4906 m_mainWin
->SortItems( fn
, data
);
4910 // ----------------------------------------------------------------------------
4912 // ----------------------------------------------------------------------------
4914 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
4916 if (!m_mainWin
) return;
4918 // We need to override OnSize so that our scrolled
4919 // window a) does call Layout() to use sizers for
4920 // positioning the controls but b) does not query
4921 // the sizer for their size and use that for setting
4922 // the scrollable area as set that ourselves by
4923 // calling SetScrollbar() further down.
4927 m_mainWin
->RecalculatePositions();
4932 void wxGenericListCtrl::OnInternalIdle()
4934 wxWindow::OnInternalIdle();
4936 if (m_mainWin
->m_dirty
)
4937 m_mainWin
->RecalculatePositions();
4940 // ----------------------------------------------------------------------------
4942 // ----------------------------------------------------------------------------
4944 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
4948 m_mainWin
->SetBackgroundColour( colour
);
4949 m_mainWin
->m_dirty
= true;
4955 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
4957 if ( !wxWindow::SetForegroundColour( colour
) )
4962 m_mainWin
->SetForegroundColour( colour
);
4963 m_mainWin
->m_dirty
= true;
4967 m_headerWin
->SetForegroundColour( colour
);
4972 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
4974 if ( !wxWindow::SetFont( font
) )
4979 m_mainWin
->SetFont( font
);
4980 m_mainWin
->m_dirty
= true;
4985 m_headerWin
->SetFont( font
);
4986 // CalculateAndSetHeaderHeight();
4996 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
4999 // Use the same color scheme as wxListBox
5000 return wxListBox::GetClassDefaultAttributes(variant
);
5002 wxUnusedVar(variant
);
5003 wxVisualAttributes attr
;
5004 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5005 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5006 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5011 // ----------------------------------------------------------------------------
5012 // methods forwarded to m_mainWin
5013 // ----------------------------------------------------------------------------
5015 #if wxUSE_DRAG_AND_DROP
5017 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5019 m_mainWin
->SetDropTarget( dropTarget
);
5022 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5024 return m_mainWin
->GetDropTarget();
5029 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5031 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5034 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5036 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5039 wxColour
wxGenericListCtrl::GetForegroundColour() const
5041 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5044 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5047 return m_mainWin
->PopupMenu( menu
, x
, y
);
5053 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5055 // It's not clear whether this can be called before m_mainWin is created
5056 // but it seems better to be on the safe side and check.
5058 m_mainWin
->DoClientToScreen(x
, y
);
5060 wxControl::DoClientToScreen(x
, y
);
5063 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5065 // At least in wxGTK/Univ build this method can be called before m_mainWin
5066 // is created so avoid crashes in this case.
5068 m_mainWin
->DoScreenToClient(x
, y
);
5070 wxControl::DoScreenToClient(x
, y
);
5073 void wxGenericListCtrl::SetFocus()
5075 // The test in window.cpp fails as we are a composite
5076 // window, so it checks against "this", but not m_mainWin.
5077 if ( DoFindFocus() != this )
5078 m_mainWin
->SetFocus();
5081 wxSize
wxGenericListCtrl::DoGetBestClientSize() const
5083 // Something is better than nothing even if this is completely arbitrary.
5084 wxSize
sizeBest(100, 80);
5086 if ( !InReportView() )
5088 // Ensure that our minimal width is at least big enough to show all our
5089 // items. This is important for wxListbook to size itself correctly.
5091 // Remember the offset of the first item: this corresponds to the
5092 // margins around the item so we will add it to the minimal size below
5093 // to ensure that we have equal margins on all sides.
5096 // We can iterate over all items as there shouldn't be too many of them
5097 // in non-report view. If it ever becomes a problem, we could examine
5098 // just the first few items probably, the determination of the best
5099 // size is less important if we will need scrollbars anyhow.
5100 for ( int n
= 0; n
< GetItemCount(); n
++ )
5102 const wxRect itemRect
= m_mainWin
->GetLineRect(n
);
5105 // Remember the position of the first item as all the rest are
5106 // offset by at least this number of pixels too.
5107 ofs
= itemRect
.GetPosition();
5110 sizeBest
.IncTo(itemRect
.GetSize());
5113 sizeBest
.IncBy(2*ofs
);
5116 // If we have the scrollbars we need to account for them too. And to
5117 // make sure the scrollbars status is up to date we need to call this
5118 // function to set them.
5119 m_mainWin
->RecalculatePositions(true /* no refresh */);
5121 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5122 // to use m_mainWin client/virtual size for determination of whether we
5123 // use scrollbars and not the size of this window itself. Maybe that
5124 // function should be extended to work correctly in the case when our
5125 // scrollbars manage a different window from this one but currently it
5127 const wxSize sizeClient
= m_mainWin
->GetClientSize();
5128 const wxSize sizeVirt
= m_mainWin
->GetVirtualSize();
5130 if ( sizeVirt
.x
> sizeClient
.x
/* HasScrollbar(wxHORIZONTAL) */ )
5131 sizeBest
.y
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
5133 if ( sizeVirt
.y
> sizeClient
.y
/* HasScrollbar(wxVERTICAL) */ )
5134 sizeBest
.x
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
5140 // ----------------------------------------------------------------------------
5141 // virtual list control support
5142 // ----------------------------------------------------------------------------
5144 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5146 // this is a pure virtual function, in fact - which is not really pure
5147 // because the controls which are not virtual don't need to implement it
5148 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5150 return wxEmptyString
;
5153 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5155 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5157 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5161 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5164 return OnGetItemImage(item
);
5170 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5172 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5173 wxT("invalid item index in OnGetItemAttr()") );
5175 // no attributes by default
5179 void wxGenericListCtrl::SetItemCount(long count
)
5181 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5183 m_mainWin
->SetItemCount(count
);
5186 void wxGenericListCtrl::RefreshItem(long item
)
5188 m_mainWin
->RefreshLine(item
);
5191 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5193 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5196 // Generic wxListCtrl is more or less a container for two other
5197 // windows which drawings are done upon. These are namely
5198 // 'm_headerWin' and 'm_mainWin'.
5199 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5200 // behaviour wxListCtrl has under wxMSW.
5202 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5206 // The easy case, no rectangle specified.
5208 m_headerWin
->Refresh(eraseBackground
);
5211 m_mainWin
->Refresh(eraseBackground
);
5215 // Refresh the header window
5218 wxRect rectHeader
= m_headerWin
->GetRect();
5219 rectHeader
.Intersect(*rect
);
5220 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5223 m_headerWin
->GetPosition(&x
, &y
);
5224 rectHeader
.Offset(-x
, -y
);
5225 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5229 // Refresh the main window
5232 wxRect rectMain
= m_mainWin
->GetRect();
5233 rectMain
.Intersect(*rect
);
5234 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5237 m_mainWin
->GetPosition(&x
, &y
);
5238 rectMain
.Offset(-x
, -y
);
5239 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5245 void wxGenericListCtrl::Update()
5249 if ( m_mainWin
->m_dirty
)
5250 m_mainWin
->RecalculatePositions();
5252 m_mainWin
->Update();
5256 m_headerWin
->Update();
5259 #endif // wxUSE_LISTCTRL