1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 // 1. we need to implement searching/sorting for virtual controls somehow
13 // 2. when changing selection the lines are refreshed twice
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/listctrl.h"
26 #include "wx/generic/listctrl.h"
28 // Define an event class to implement delayed syncing of the header window
29 wxDECLARE_EVENT(ListCtrlEvent, wxCommandEvent);
30 wxDEFINE_EVENT(ListCtrlEvent, wxCommandEvent);
34 #include "wx/scrolwin.h"
36 #include "wx/settings.h"
37 #include "wx/dynarray.h"
38 #include "wx/dcclient.h"
39 #include "wx/dcscreen.h"
41 #include "wx/settings.h"
45 #include "wx/imaglist.h"
46 #include "wx/renderer.h"
47 // 2013.12.10. Toshi Nagata; include my own version of private/listctrl.h
48 //#include "wx/generic/private/listctrl.h"
49 #include "listctrl_private.h"
53 #include "wx/osx/private.h"
56 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
57 #include "wx/msw/wrapwin.h" // 2013.11.27. define -> include Toshi Nagata
60 // NOTE: If using the wxListBox visual attributes works everywhere then this can
61 // be removed, as well as the #else case below.
62 #define _USE_VISATTR 0
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // // the height of the header window (FIXME: should depend on its font!)
70 // static const int HEADER_HEIGHT = 23;
72 static const int SCROLL_UNIT_X = 15;
74 // the spacing between the lines (in report mode)
75 static const int LINE_SPACING = 0;
77 // extra margins around the text label
79 static const int EXTRA_WIDTH = 6;
81 static const int EXTRA_WIDTH = 4;
85 static const int EXTRA_HEIGHT = 6;
86 // 2013.12.30. Toshi Nagata
87 #elif defined(__WXMSW__)
88 static const int EXTRA_HEIGHT = 1;
91 static const int EXTRA_HEIGHT = 4;
94 // margin between the window and the items
95 static const int EXTRA_BORDER_X = 2;
96 static const int EXTRA_BORDER_Y = 2;
98 // offset for the header window
99 static const int HEADER_OFFSET_X = 0;
100 static const int HEADER_OFFSET_Y = 0;
102 // margin between rows of icons in [small] icon view
103 static const int MARGIN_BETWEEN_ROWS = 6;
105 // when autosizing the columns, add some slack
106 static const int AUTOSIZE_COL_MARGIN = 10;
108 // default width for the header columns
109 static const int WIDTH_COL_DEFAULT = 80;
111 // the space between the image and the text in the report mode
112 static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
114 // the space between the image and the text in the report mode in header
115 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
119 // ----------------------------------------------------------------------------
120 // arrays/list implementations
121 // ----------------------------------------------------------------------------
123 #include "wx/listimpl.cpp"
124 WX_DEFINE_LIST(wxListItemDataList)
126 #include "wx/arrimpl.cpp"
127 WX_DEFINE_OBJARRAY(wxListLineDataArray)
129 #include "wx/listimpl.cpp"
130 WX_DEFINE_LIST(wxListHeaderDataList)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 wxListItemData::~wxListItemData()
139 // in the virtual list control the attributes are managed by the main
140 // program, so don't delete them
141 if ( !m_owner->IsVirtual() )
147 void wxListItemData::Init()
155 wxListItemData::wxListItemData(wxListMainWindow *owner)
161 if ( owner->InReportView() )
167 void wxListItemData::SetItem( const wxListItem &info )
169 if ( info.m_mask & wxLIST_MASK_TEXT )
170 SetText(info.m_text);
171 if ( info.m_mask & wxLIST_MASK_IMAGE )
172 m_image = info.m_image;
173 if ( info.m_mask & wxLIST_MASK_DATA )
174 m_data = info.m_data;
176 if ( info.HasAttributes() )
179 m_attr->AssignFrom(*info.GetAttributes());
181 m_attr = new wxListItemAttr(*info.GetAttributes());
189 m_rect->width = info.m_width;
193 void wxListItemData::SetPosition( int x, int y )
195 wxCHECK_RET( m_rect, wxT("unexpected SetPosition() call") );
201 void wxListItemData::SetSize( int width, int height )
203 wxCHECK_RET( m_rect, wxT("unexpected SetSize() call") );
206 m_rect->width = width;
208 m_rect->height = height;
211 bool wxListItemData::IsHit( int x, int y ) const
213 wxCHECK_MSG( m_rect, false, wxT("can't be called in this mode") );
215 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
218 int wxListItemData::GetX() const
220 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
225 int wxListItemData::GetY() const
227 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
232 int wxListItemData::GetWidth() const
234 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
236 return m_rect->width;
239 int wxListItemData::GetHeight() const
241 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
243 return m_rect->height;
246 void wxListItemData::GetItem( wxListItem &info ) const
248 long mask = info.m_mask;
250 // by default, get everything for backwards compatibility
253 if ( mask & wxLIST_MASK_TEXT )
254 info.m_text = m_text;
255 if ( mask & wxLIST_MASK_IMAGE )
256 info.m_image = m_image;
257 if ( mask & wxLIST_MASK_DATA )
258 info.m_data = m_data;
262 if ( m_attr->HasTextColour() )
263 info.SetTextColour(m_attr->GetTextColour());
264 if ( m_attr->HasBackgroundColour() )
265 info.SetBackgroundColour(m_attr->GetBackgroundColour());
266 if ( m_attr->HasFont() )
267 info.SetFont(m_attr->GetFont());
271 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
275 void wxListHeaderData::Init()
287 wxListHeaderData::wxListHeaderData()
292 wxListHeaderData::wxListHeaderData( const wxListItem &item )
299 void wxListHeaderData::SetItem( const wxListItem &item )
301 m_mask = item.m_mask;
303 if ( m_mask & wxLIST_MASK_TEXT )
304 m_text = item.m_text;
306 if ( m_mask & wxLIST_MASK_IMAGE )
307 m_image = item.m_image;
309 if ( m_mask & wxLIST_MASK_FORMAT )
310 m_format = item.m_format;
312 if ( m_mask & wxLIST_MASK_WIDTH )
313 SetWidth(item.m_width);
315 if ( m_mask & wxLIST_MASK_STATE )
316 SetState(item.m_state);
319 void wxListHeaderData::SetPosition( int x, int y )
325 void wxListHeaderData::SetHeight( int h )
330 void wxListHeaderData::SetWidth( int w )
332 m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
335 void wxListHeaderData::SetState( int flag )
340 void wxListHeaderData::SetFormat( int format )
345 bool wxListHeaderData::HasImage() const
347 return m_image != -1;
350 bool wxListHeaderData::IsHit( int x, int y ) const
352 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
355 void wxListHeaderData::GetItem( wxListItem& item )
357 long mask = item.m_mask;
360 // by default, get everything for backwards compatibility
364 if ( mask & wxLIST_MASK_STATE )
365 item.m_state = m_state;
366 if ( mask & wxLIST_MASK_TEXT )
367 item.m_text = m_text;
368 if ( mask & wxLIST_MASK_IMAGE )
369 item.m_image = m_image;
370 if ( mask & wxLIST_MASK_WIDTH )
371 item.m_width = m_width;
372 if ( mask & wxLIST_MASK_FORMAT )
373 item.m_format = m_format;
376 int wxListHeaderData::GetImage() const
381 int wxListHeaderData::GetWidth() const
386 int wxListHeaderData::GetFormat() const
391 int wxListHeaderData::GetState() const
396 //-----------------------------------------------------------------------------
398 //-----------------------------------------------------------------------------
400 inline int wxListLineData::GetMode() const
402 return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
405 inline bool wxListLineData::InReportView() const
407 return m_owner->HasFlag(wxLC_REPORT);
410 inline bool wxListLineData::IsVirtual() const
412 return m_owner->IsVirtual();
415 wxListLineData::wxListLineData( wxListMainWindow *owner )
419 if ( InReportView() )
422 m_gi = new GeometryInfo;
424 m_highlighted = false;
426 InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
429 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
431 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
432 wxCHECK_RET( node, wxT("no subitems at all??") );
434 wxListItemData *item = node->GetData();
442 case wxLC_SMALL_ICON:
443 m_gi->m_rectAll.width = spacing;
450 m_gi->m_rectLabel.width =
451 m_gi->m_rectLabel.height = 0;
455 dc->GetTextExtent( s, &lw, &lh );
459 m_gi->m_rectAll.height = spacing + lh;
461 m_gi->m_rectAll.width = lw;
463 m_gi->m_rectLabel.width = lw;
464 m_gi->m_rectLabel.height = lh;
467 if (item->HasImage())
470 m_owner->GetImageSize( item->GetImage(), w, h );
471 m_gi->m_rectIcon.width = w + 8;
472 m_gi->m_rectIcon.height = h + 8;
474 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
475 m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
476 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
477 m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
480 if ( item->HasText() )
482 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
483 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
485 else // no text, highlight the icon
487 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
488 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
493 s = item->GetTextForMeasuring();
495 dc->GetTextExtent( s, &lw, &lh );
499 m_gi->m_rectLabel.width = lw;
500 m_gi->m_rectLabel.height = lh;
502 m_gi->m_rectAll.width = lw;
503 m_gi->m_rectAll.height = lh;
505 if (item->HasImage())
508 m_owner->GetImageSize( item->GetImage(), w, h );
509 m_gi->m_rectIcon.width = w;
510 m_gi->m_rectIcon.height = h;
512 m_gi->m_rectAll.width += 4 + w;
513 if (h > m_gi->m_rectAll.height)
514 m_gi->m_rectAll.height = h;
517 m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
518 m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
522 wxFAIL_MSG( wxT("unexpected call to SetSize") );
526 wxFAIL_MSG( wxT("unknown mode") );
531 void wxListLineData::SetPosition( int x, int y, int spacing )
533 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
534 wxCHECK_RET( node, wxT("no subitems at all??") );
536 wxListItemData *item = node->GetData();
541 case wxLC_SMALL_ICON:
542 m_gi->m_rectAll.x = x;
543 m_gi->m_rectAll.y = y;
545 if ( item->HasImage() )
547 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
548 (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
549 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
552 if ( item->HasText() )
554 if (m_gi->m_rectAll.width > spacing)
555 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
557 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
558 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
559 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
560 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
562 else // no text, highlight the icon
564 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
565 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
570 m_gi->m_rectAll.x = x;
571 m_gi->m_rectAll.y = y;
573 m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
574 m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
575 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
577 if (item->HasImage())
579 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
580 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
581 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
585 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
590 wxFAIL_MSG( wxT("unexpected call to SetPosition") );
594 wxFAIL_MSG( wxT("unknown mode") );
599 void wxListLineData::InitItems( int num )
601 for (int i = 0; i < num; i++)
602 m_items.Append( new wxListItemData(m_owner) );
605 void wxListLineData::SetItem( int index, const wxListItem &info )
607 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
608 wxCHECK_RET( node, wxT("invalid column index in SetItem") );
610 wxListItemData *item = node->GetData();
611 item->SetItem( info );
614 void wxListLineData::GetItem( int index, wxListItem &info )
616 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
619 wxListItemData *item = node->GetData();
620 item->GetItem( info );
624 wxString wxListLineData::GetText(int index) const
628 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
631 wxListItemData *item = node->GetData();
638 void wxListLineData::SetText( int index, const wxString& s )
640 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
643 wxListItemData *item = node->GetData();
648 void wxListLineData::SetImage( int index, int image )
650 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
651 wxCHECK_RET( node, wxT("invalid column index in SetImage()") );
653 wxListItemData *item = node->GetData();
654 item->SetImage(image);
657 int wxListLineData::GetImage( int index ) const
659 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
660 wxCHECK_MSG( node, -1, wxT("invalid column index in GetImage()") );
662 wxListItemData *item = node->GetData();
663 return item->GetImage();
666 wxListItemAttr *wxListLineData::GetAttr() const
668 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
669 wxCHECK_MSG( node, NULL, wxT("invalid column index in GetAttr()") );
671 wxListItemData *item = node->GetData();
672 return item->GetAttr();
675 void wxListLineData::SetAttr(wxListItemAttr *attr)
677 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
678 wxCHECK_RET( node, wxT("invalid column index in SetAttr()") );
680 wxListItemData *item = node->GetData();
684 void wxListLineData::ApplyAttributes(wxDC *dc,
685 const wxRect& rectHL,
687 bool current, wxListItemAttr *override) // 2013.12.10. Toshi Nagata; add "override" argument
689 // 2013.12.10. Toshi Nagata
690 // const wxListItemAttr * const attr = GetAttr();
691 const wxListItemAttr * const attr = (override ? override : GetAttr());
694 wxWindow * const listctrl = m_owner->GetParent();
696 const bool hasFocus = listctrl->HasFocus()
697 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
698 && IsControlActive( (ControlRef)listctrl->GetHandle() )
704 // don't use foreground colour for drawing highlighted items - this might
705 // make them completely invisible (and there is no way to do bit
706 // arithmetics on wxColour, unfortunately)
717 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
719 // Modified Toshi Nagata 2013.12.30.
720 // colText = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT);
726 else if ( attr && attr->HasTextColour() )
727 colText = attr->GetTextColour();
729 colText = listctrl->GetForegroundColour();
731 dc->SetTextForeground(colText);
735 if ( attr && attr->HasFont() )
736 font = attr->GetFont();
738 font = listctrl->GetFont();
745 // Use the renderer method to ensure that the selected items use the
747 int flags = wxCONTROL_SELECTED;
749 flags |= wxCONTROL_FOCUSED;
751 flags |= wxCONTROL_CURRENT;
752 wxRendererNative::Get().
753 DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
755 else if ( attr && attr->HasBackgroundColour() )
757 // Draw the background using the items custom background colour.
758 dc->SetBrush(attr->GetBackgroundColour());
759 dc->SetPen(*wxTRANSPARENT_PEN);
760 dc->DrawRectangle(rectHL);
763 // just for debugging to better see where the items are
765 dc->SetPen(*wxRED_PEN);
766 dc->SetBrush(*wxTRANSPARENT_BRUSH);
767 dc->DrawRectangle( m_gi->m_rectAll );
768 dc->SetPen(*wxGREEN_PEN);
769 dc->DrawRectangle( m_gi->m_rectIcon );
773 void wxListLineData::Draw(wxDC *dc, bool current)
775 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
776 wxCHECK_RET( node, wxT("no subitems at all??") );
778 ApplyAttributes(dc, m_gi->m_rectHighlight, IsHighlighted(), current);
780 wxListItemData *item = node->GetData();
781 if (item->HasImage())
783 // centre the image inside our rectangle, this looks nicer when items
784 // ae aligned in a row
785 const wxRect& rectIcon = m_gi->m_rectIcon;
787 m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
792 const wxRect& rectLabel = m_gi->m_rectLabel;
794 wxDCClipper clipper(*dc, rectLabel);
795 dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
799 void wxListLineData::DrawInReportMode( wxDC *dc,
801 const wxRect& rectHL,
803 bool current, long line ) // 2013.12.10. Toshi Nagata; add "line" argument
805 // TODO: later we should support setting different attributes for
806 // different columns - to do it, just add "col" argument to
807 // GetAttr() and move these lines into the loop below
809 // ApplyAttributes(dc, rectHL, highlighted, current); // 2013.12.10. Toshi Nagata; move to the inside the loop
811 wxCoord x = rect.x + HEADER_OFFSET_X,
812 yMid = rect.y + rect.height/2;
814 // This probably needs to be done
815 // on all platforms as the icons
816 // otherwise nearly touch the border
821 for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
823 node = node->GetNext(), col++ )
825 wxListItemData *item = node->GetData();
827 int width = m_owner->GetColumnWidth(col);
831 // 2013.12.10. Toshi Nagata
832 // Implement attribute for each column
834 wxListItemAttr *cattr = m_owner->GetListCtrl()->OnGetItemAttr(line + 1000000 * (col + 1));
835 wxRect itemRect(xOld, rect.y, width, rect.height);
836 ApplyAttributes(dc, itemRect, highlighted, current, cattr);
841 const int wText = width;
842 wxDCClipper clipper(*dc, xOld, rect.y, wText, rect.height);
844 if ( item->HasImage() )
847 m_owner->GetImageSize( item->GetImage(), ix, iy );
848 m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 );
850 ix += IMAGE_MARGIN_IN_REPORT_MODE;
856 if ( item->HasText() )
857 DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, width);
861 void wxListLineData::DrawTextFormatted(wxDC *dc,
862 const wxString& textOrig,
868 // we don't support displaying multiple lines currently (and neither does
869 // wxMSW FWIW) so just merge all the lines
870 wxString text(textOrig);
871 text.Replace(wxT("\n"), wxT(" "));
874 dc->GetTextExtent(text, &w, &h);
876 const wxCoord y = yMid - (h + 1)/2;
878 wxDCClipper clipper(*dc, x, y, width, h);
880 // determine if the string can fit inside the current width
883 // it can, draw it using the items alignment
885 m_owner->GetColumn(col, item);
886 switch ( item.GetAlign() )
888 case wxLIST_FORMAT_LEFT:
892 case wxLIST_FORMAT_RIGHT:
896 case wxLIST_FORMAT_CENTER:
897 x += (width - w) / 2;
901 wxFAIL_MSG( wxT("unknown list item format") );
905 dc->DrawText(text, x, y);
907 else // otherwise, truncate and add an ellipsis if possible
909 // determine the base width
910 wxString ellipsis(wxT("..."));
912 dc->GetTextExtent(ellipsis, &base_w, &h);
914 // continue until we have enough space or only one character left
916 size_t len = text.length();
917 wxString drawntext = text.Left(len);
920 dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
921 drawntext.RemoveLast();
924 if (w + base_w <= width)
928 // if still not enough space, remove ellipsis characters
929 while (ellipsis.length() > 0 && w + base_w > width)
931 ellipsis = ellipsis.Left(ellipsis.length() - 1);
932 dc->GetTextExtent(ellipsis, &base_w, &h);
936 dc->DrawText(drawntext, x, y);
937 dc->DrawText(ellipsis, x + w, y);
941 bool wxListLineData::Highlight( bool on )
943 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
945 if ( on == m_highlighted )
953 void wxListLineData::ReverseHighlight( void )
955 Highlight(!IsHighlighted());
958 //-----------------------------------------------------------------------------
959 // wxListHeaderWindow
960 //-----------------------------------------------------------------------------
962 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
963 EVT_PAINT (wxListHeaderWindow::OnPaint)
964 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
965 EVT_COMMAND(-1, ListCtrlEvent, wxListHeaderWindow::OnListCtrlEvent) // Toshi Nagata 2014.01.13.
968 void wxListHeaderWindow::Init()
970 m_currentCursor = NULL;
971 m_isDragging = false;
973 m_sendSetColumnWidth = false;
976 wxListHeaderWindow::wxListHeaderWindow()
981 m_resizeCursor = NULL;
984 bool wxListHeaderWindow::Create( wxWindow *win,
986 wxListMainWindow *owner,
990 const wxString &name )
992 if ( !wxWindow::Create(win, id, pos, size, style, name) )
998 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
1001 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
1002 SetOwnForegroundColour( attr.colFg );
1003 SetOwnBackgroundColour( attr.colBg );
1005 SetOwnFont( attr.font );
1007 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
1008 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1010 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
1016 wxListHeaderWindow::~wxListHeaderWindow()
1018 delete m_resizeCursor;
1021 #ifdef __WXUNIVERSAL__
1022 #include "wx/univ/renderer.h"
1023 #include "wx/univ/theme.h"
1026 // shift the DC origin to match the position of the main window horz
1027 // scrollbar: this allows us to always use logical coords
1028 void wxListHeaderWindow::AdjustDC(wxDC& dc)
1030 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1033 parent->GetScrollPixelsPerUnit( &xpix, NULL );
1036 parent->GetViewStart( &view_start, NULL );
1040 dc.GetDeviceOrigin( &org_x, &org_y );
1042 // account for the horz scrollbar offset
1044 if (GetLayoutDirection() == wxLayout_RightToLeft)
1046 // Maybe we just have to check for m_signX
1047 // in the DC, but I leave the #ifdef __WXGTK__
1049 dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y );
1053 dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y );
1056 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1058 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1060 wxPaintDC dc( this );
1064 dc.SetFont( GetFont() );
1066 // width and height of the entire header window
1068 GetClientSize( &w, &h );
1069 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
1071 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
1072 dc.SetTextForeground(GetForegroundColour());
1074 int x = HEADER_OFFSET_X;
1075 int numColumns = m_owner->GetColumnCount();
1077 for ( int i = 0; i < numColumns && x < w; i++ )
1079 m_owner->GetColumn( i, item );
1080 int wCol = item.m_width;
1086 if (!m_parent->IsEnabled())
1087 flags |= wxCONTROL_DISABLED;
1089 // NB: The code below is not really Mac-specific, but since we are close
1090 // to 2.8 release and I don't have time to test on other platforms, I
1091 // defined this only for wxMac. If this behaviour is desired on
1092 // other platforms, please go ahead and revise or remove the #ifdef.
1094 if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) &&
1095 (item.m_state & wxLIST_STATE_SELECTED) )
1096 flags |= wxCONTROL_SELECTED;
1100 flags |= wxCONTROL_SPECIAL; // mark as first column
1102 wxRendererNative::Get().DrawHeaderButton
1106 wxRect(x, HEADER_OFFSET_Y, cw, ch),
1110 // see if we have enough space for the column label
1112 // for this we need the width of the text
1115 dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
1116 wLabel += 2 * EXTRA_WIDTH;
1118 // and the width of the icon, if any
1119 int ix = 0, iy = 0; // init them just to suppress the compiler warnings
1120 const int image = item.m_image;
1121 wxImageList *imageList;
1124 imageList = m_owner->GetSmallImageList();
1127 imageList->GetSize(image, ix, iy);
1128 wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
1136 // ignore alignment if there is not enough space anyhow
1138 switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1141 wxFAIL_MSG( wxT("unknown list item format") );
1144 case wxLIST_FORMAT_LEFT:
1148 case wxLIST_FORMAT_RIGHT:
1149 xAligned = x + cw - wLabel;
1152 case wxLIST_FORMAT_CENTER:
1153 xAligned = x + (cw - wLabel) / 2;
1157 // draw the text and image clipping them so that they
1158 // don't overwrite the column boundary
1159 wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h);
1161 // if we have an image, draw it on the right of the label
1168 xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE,
1169 HEADER_OFFSET_Y + (h - iy)/2,
1170 wxIMAGELIST_DRAW_TRANSPARENT
1174 dc.DrawText( item.GetText(),
1175 xAligned + EXTRA_WIDTH, (h - hLabel) / 2 );
1180 // Fill in what's missing to the right of the columns, otherwise we will
1181 // leave an unpainted area when columns are removed (and it looks better)
1184 wxRendererNative::Get().DrawHeaderButton
1188 wxRect(x, HEADER_OFFSET_Y, w - x, h),
1189 wxCONTROL_DIRTY // mark as last column
1194 void wxListHeaderWindow::OnInternalIdle()
1196 wxWindow::OnInternalIdle();
1198 if (m_sendSetColumnWidth)
1200 m_owner->SetColumnWidth( m_colToSend, m_widthToSend );
1201 m_sendSetColumnWidth = false;
1205 void wxListHeaderWindow::DrawCurrent()
1207 m_sendSetColumnWidth = true;
1208 m_colToSend = m_column;
1209 m_widthToSend = m_currentX - m_minX;
1212 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1214 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1216 // we want to work with logical coords
1218 parent->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1219 int y = event.GetY();
1223 SendListEvent(wxEVT_LIST_COL_DRAGGING, event.GetPosition());
1225 // we don't draw the line beyond our window, but we allow dragging it
1228 GetClientSize( &w, NULL );
1229 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
1232 // erase the line if it was drawn
1233 if ( m_currentX < w )
1236 if (event.ButtonUp())
1239 m_isDragging = false;
1241 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1242 SendListEvent(wxEVT_LIST_COL_END_DRAG, event.GetPosition());
1249 m_currentX = m_minX + 7;
1251 // draw in the new location
1252 if ( m_currentX < w )
1256 else // not dragging
1259 bool hit_border = false;
1261 // end of the current column
1264 // find the column where this event occurred
1266 countCol = m_owner->GetColumnCount();
1267 for (col = 0; col < countCol; col++)
1269 xpos += m_owner->GetColumnWidth( col );
1272 if ( (abs(x-xpos) < 3) && (y < 22) )
1274 // near the column border
1281 // inside the column
1288 if ( col == countCol )
1291 if (event.LeftDown() || event.RightUp())
1293 if (hit_border && event.LeftDown())
1295 if ( SendListEvent(wxEVT_LIST_COL_BEGIN_DRAG,
1296 event.GetPosition()) )
1298 m_isDragging = true;
1303 //else: column resizing was vetoed by the user code
1305 else // click on a column
1307 // record the selected state of the columns
1308 if (event.LeftDown())
1310 for (int i=0; i < m_owner->GetColumnCount(); i++)
1313 m_owner->GetColumn(i, colItem);
1314 long state = colItem.GetState();
1316 colItem.SetState(state | wxLIST_STATE_SELECTED);
1318 colItem.SetState(state & ~wxLIST_STATE_SELECTED);
1319 m_owner->SetColumn(i, colItem);
1323 SendListEvent( event.LeftDown()
1324 ? wxEVT_LIST_COL_CLICK
1325 : wxEVT_LIST_COL_RIGHT_CLICK,
1326 event.GetPosition());
1329 else if (event.Moving())
1334 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
1335 m_currentCursor = m_resizeCursor;
1339 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
1340 m_currentCursor = wxSTANDARD_CURSOR;
1344 SetCursor(*m_currentCursor);
1349 bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
1351 wxWindow *parent = GetParent();
1352 wxListEvent le( type, parent->GetId() );
1353 le.SetEventObject( parent );
1354 le.m_pointDrag = pos;
1356 // the position should be relative to the parent window, not
1357 // this one for compatibility with MSW and common sense: the
1358 // user code doesn't know anything at all about this header
1359 // window, so why should it get positions relative to it?
1360 le.m_pointDrag.y -= GetSize().y;
1362 le.m_col = m_column;
1363 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
1366 // Toshi Nagata 2014.01.13.
1367 void wxListHeaderWindow::QueueListCtrlEvent(wxCommandEvent &event)
1369 AddPendingEvent(event);
1372 void wxListHeaderWindow::OnListCtrlEvent(wxCommandEvent &event)
1379 //-----------------------------------------------------------------------------
1380 // wxListRenameTimer (internal)
1381 //-----------------------------------------------------------------------------
1383 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
1388 void wxListRenameTimer::Notify()
1390 m_owner->OnRenameTimer();
1393 //-----------------------------------------------------------------------------
1394 // wxListFindTimer (internal)
1395 //-----------------------------------------------------------------------------
1397 void wxListFindTimer::Notify()
1399 m_owner->OnFindTimer();
1402 //-----------------------------------------------------------------------------
1403 // wxListTextCtrlWrapper (internal)
1404 //-----------------------------------------------------------------------------
1406 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
1407 EVT_CHAR (wxListTextCtrlWrapper::OnChar)
1408 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
1409 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
1412 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
1415 : m_startValue(owner->GetItemText(itemEdit)),
1416 m_itemEdited(itemEdit)
1420 m_aboutToFinish = false;
1422 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1424 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
1426 parent->CalcScrolledPosition(rectLabel.x, rectLabel.y,
1427 &rectLabel.x, &rectLabel.y);
1429 m_text->Create(owner, wxID_ANY, m_startValue,
1430 wxPoint(rectLabel.x-4,rectLabel.y-4),
1431 wxSize(rectLabel.width+11,rectLabel.height+8));
1434 m_text->PushEventHandler(this);
1437 void wxListTextCtrlWrapper::EndEdit(EndReason reason)
1439 m_aboutToFinish = true;
1444 // Notify the owner about the changes
1447 // Even if vetoed, close the control (consistent with MSW)
1452 m_owner->OnRenameCancelled(m_itemEdited);
1458 // Don't generate any notifications for the control being destroyed
1459 // and don't set focus to it neither.
1465 void wxListTextCtrlWrapper::Finish( bool setfocus )
1467 m_text->RemoveEventHandler(this);
1468 m_owner->ResetTextControl( m_text );
1470 wxPendingDelete.Append( this );
1473 m_owner->SetFocus();
1476 bool wxListTextCtrlWrapper::AcceptChanges()
1478 const wxString value = m_text->GetValue();
1480 // notice that we should always call OnRenameAccept() to generate the "end
1481 // label editing" event, even if the user hasn't really changed anything
1482 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
1484 // vetoed by the user
1488 // accepted, do rename the item (unless nothing changed)
1489 if ( value != m_startValue )
1490 m_owner->SetItemText(m_itemEdited, value);
1495 void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
1497 if ( !CheckForEndEditKey(event) )
1501 bool wxListTextCtrlWrapper::CheckForEndEditKey(const wxKeyEvent& event)
1503 switch ( event.m_keyCode )
1506 EndEdit( End_Accept );
1510 EndEdit( End_Discard );
1520 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
1522 if (m_aboutToFinish)
1524 // auto-grow the textctrl:
1525 wxSize parentSize = m_owner->GetSize();
1526 wxPoint myPos = m_text->GetPosition();
1527 wxSize mySize = m_text->GetSize();
1529 m_text->GetTextExtent(m_text->GetValue() + wxT("MM"), &sx, &sy);
1530 if (myPos.x + sx > parentSize.x)
1531 sx = parentSize.x - myPos.x;
1534 m_text->SetSize(sx, wxDefaultCoord);
1540 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
1542 if ( !m_aboutToFinish )
1544 if ( !AcceptChanges() )
1545 m_owner->OnRenameCancelled( m_itemEdited );
1550 // We must let the native text control handle focus
1554 //-----------------------------------------------------------------------------
1556 //-----------------------------------------------------------------------------
1558 BEGIN_EVENT_TABLE(wxListMainWindow, wxWindow)
1559 EVT_PAINT (wxListMainWindow::OnPaint)
1560 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
1561 EVT_CHAR_HOOK (wxListMainWindow::OnCharHook)
1562 EVT_CHAR (wxListMainWindow::OnChar)
1563 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
1564 EVT_KEY_UP (wxListMainWindow::OnKeyUp)
1565 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
1566 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
1567 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
1568 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus)
1571 void wxListMainWindow::Init()
1576 m_lineTo = (size_t)-1;
1582 m_small_image_list = NULL;
1583 m_normal_image_list = NULL;
1585 m_small_spacing = 30;
1586 m_normal_spacing = 40;
1590 m_isCreated = false;
1592 m_lastOnSame = false;
1593 m_renameTimer = new wxListRenameTimer( this );
1595 m_findBell = 0; // default is to not ring bell at all
1596 m_textctrlWrapper = NULL;
1600 m_lineSelectSingleOnUp =
1601 m_lineBeforeLastClicked = (size_t)-1;
1604 wxListMainWindow::wxListMainWindow()
1609 m_highlightUnfocusedBrush = NULL;
1612 wxListMainWindow::wxListMainWindow( wxWindow *parent,
1615 const wxSize& size )
1616 : wxWindow( parent, id, pos, size,
1617 wxWANTS_CHARS | wxBORDER_NONE )
1621 m_highlightBrush = new wxBrush
1623 wxSystemSettings::GetColour
1625 wxSYS_COLOUR_HIGHLIGHT
1630 m_highlightUnfocusedBrush = new wxBrush
1632 wxSystemSettings::GetColour
1634 wxSYS_COLOUR_BTNSHADOW
1639 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
1640 SetOwnForegroundColour( attr.colFg );
1641 SetOwnBackgroundColour( attr.colBg );
1643 SetOwnFont( attr.font );
1646 wxListMainWindow::~wxListMainWindow()
1648 if ( m_textctrlWrapper )
1649 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Destroy);
1652 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
1653 WX_CLEAR_ARRAY(m_aColWidths);
1655 delete m_highlightBrush;
1656 delete m_highlightUnfocusedBrush;
1657 delete m_renameTimer;
1661 void wxListMainWindow::SetReportView(bool inReportView)
1663 const size_t count = m_lines.size();
1664 for ( size_t n = 0; n < count; n++ )
1666 m_lines[n].SetReportView(inReportView);
1670 void wxListMainWindow::CacheLineData(size_t line)
1672 wxGenericListCtrl *listctrl = GetListCtrl();
1674 wxListLineData *ld = GetDummyLine();
1676 size_t countCol = GetColumnCount();
1677 for ( size_t col = 0; col < countCol; col++ )
1679 ld->SetText(col, listctrl->OnGetItemText(line, col));
1680 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
1683 ld->SetAttr(listctrl->OnGetItemAttr(line));
1686 wxListLineData *wxListMainWindow::GetDummyLine() const
1688 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1689 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1691 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1693 // we need to recreate the dummy line if the number of columns in the
1694 // control changed as it would have the incorrect number of fields
1696 if ( !m_lines.IsEmpty() &&
1697 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
1699 self->m_lines.Clear();
1702 if ( m_lines.IsEmpty() )
1704 wxListLineData *line = new wxListLineData(self);
1705 self->m_lines.Add(line);
1707 // don't waste extra memory -- there never going to be anything
1708 // else/more in this array
1709 self->m_lines.Shrink();
1715 // ----------------------------------------------------------------------------
1716 // line geometry (report mode only)
1717 // ----------------------------------------------------------------------------
1719 wxCoord wxListMainWindow::GetLineHeight() const
1721 // we cache the line height as calling GetTextExtent() is slow
1722 if ( !m_lineHeight )
1724 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1726 wxClientDC dc( self );
1727 dc.SetFont( GetFont() );
1730 dc.GetTextExtent(wxT("H"), NULL, &y);
1732 if ( m_small_image_list && m_small_image_list->GetImageCount() )
1735 m_small_image_list->GetSize(0, iw, ih);
1740 self->m_lineHeight = y + LINE_SPACING;
1743 return m_lineHeight;
1746 wxCoord wxListMainWindow::GetLineY(size_t line) const
1748 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1750 return LINE_SPACING + line * GetLineHeight();
1753 wxRect wxListMainWindow::GetLineRect(size_t line) const
1755 if ( !InReportView() )
1756 return GetLine(line)->m_gi->m_rectAll;
1759 rect.x = HEADER_OFFSET_X;
1760 rect.y = GetLineY(line);
1761 rect.width = GetHeaderWidth();
1762 rect.height = GetLineHeight();
1767 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
1769 if ( !InReportView() )
1770 return GetLine(line)->m_gi->m_rectLabel;
1773 wxListLineData *data = GetLine(line);
1774 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
1777 wxListItemData *item = node->GetData();
1778 if ( item->HasImage() )
1781 GetImageSize( item->GetImage(), ix, iy );
1782 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
1787 rect.x = image_x + HEADER_OFFSET_X;
1788 rect.y = GetLineY(line);
1789 rect.width = GetColumnWidth(0) - image_x;
1790 rect.height = GetLineHeight();
1795 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
1797 if ( !InReportView() )
1798 return GetLine(line)->m_gi->m_rectIcon;
1800 wxListLineData *ld = GetLine(line);
1801 wxASSERT_MSG( ld->HasImage(), wxT("should have an image") );
1804 rect.x = HEADER_OFFSET_X;
1805 rect.y = GetLineY(line);
1806 GetImageSize(ld->GetImage(), rect.width, rect.height);
1811 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
1813 return InReportView() ? GetLineRect(line)
1814 : GetLine(line)->m_gi->m_rectHighlight;
1817 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
1819 wxASSERT_MSG( line < GetItemCount(), wxT("invalid line in HitTestLine") );
1821 wxListLineData *ld = GetLine(line);
1823 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
1824 return wxLIST_HITTEST_ONITEMICON;
1826 // VS: Testing for "ld->HasText() || InReportView()" instead of
1827 // "ld->HasText()" is needed to make empty lines in report view
1829 if ( ld->HasText() || InReportView() )
1831 wxRect rect = InReportView() ? GetLineRect(line)
1832 : GetLineLabelRect(line);
1834 if ( rect.Contains(x, y) )
1835 return wxLIST_HITTEST_ONITEMLABEL;
1841 // ----------------------------------------------------------------------------
1842 // highlight (selection) handling
1843 // ----------------------------------------------------------------------------
1845 bool wxListMainWindow::IsHighlighted(size_t line) const
1849 return m_selStore.IsSelected(line);
1853 wxListLineData *ld = GetLine(line);
1854 wxCHECK_MSG( ld, false, wxT("invalid index in IsHighlighted") );
1856 return ld->IsHighlighted();
1860 void wxListMainWindow::HighlightLines( size_t lineFrom,
1866 wxArrayInt linesChanged;
1867 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
1870 // meny items changed state, refresh everything
1871 RefreshLines(lineFrom, lineTo);
1873 else // only a few items changed state, refresh only them
1875 size_t count = linesChanged.GetCount();
1876 for ( size_t n = 0; n < count; n++ )
1878 RefreshLine(linesChanged[n]);
1882 else // iterate over all items in non report view
1884 for ( size_t line = lineFrom; line <= lineTo; line++ )
1886 if ( HighlightLine(line, highlight) )
1892 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
1898 changed = m_selStore.SelectItem(line, highlight);
1902 wxListLineData *ld = GetLine(line);
1903 wxCHECK_MSG( ld, false, wxT("invalid index in HighlightLine") );
1905 changed = ld->Highlight(highlight);
1910 SendNotify( line, highlight ? wxEVT_LIST_ITEM_SELECTED
1911 : wxEVT_LIST_ITEM_DESELECTED );
1917 void wxListMainWindow::RefreshLine( size_t line )
1919 if ( InReportView() )
1921 size_t visibleFrom, visibleTo;
1922 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1924 if ( line < visibleFrom || line > visibleTo )
1928 wxRect rect = GetLineRect(line);
1930 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1931 RefreshRect( rect );
1934 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
1936 // we suppose that they are ordered by caller
1937 wxASSERT_MSG( lineFrom <= lineTo, wxT("indices in disorder") );
1939 wxASSERT_MSG( lineTo < GetItemCount(), wxT("invalid line range") );
1941 if ( InReportView() )
1943 size_t visibleFrom, visibleTo;
1944 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1946 if ( lineFrom < visibleFrom )
1947 lineFrom = visibleFrom;
1948 if ( lineTo > visibleTo )
1953 rect.y = GetLineY(lineFrom);
1954 rect.width = GetClientSize().x;
1955 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
1957 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1958 RefreshRect( rect );
1962 // TODO: this should be optimized...
1963 for ( size_t line = lineFrom; line <= lineTo; line++ )
1970 void wxListMainWindow::RefreshAfter( size_t lineFrom )
1972 if ( InReportView() )
1974 size_t visibleFrom, visibleTo;
1975 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1977 if ( lineFrom < visibleFrom )
1978 lineFrom = visibleFrom;
1979 else if ( lineFrom > visibleTo )
1984 rect.y = GetLineY(lineFrom);
1985 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1987 wxSize size = GetClientSize();
1988 rect.width = size.x;
1990 // refresh till the bottom of the window
1991 rect.height = size.y - rect.y;
1993 RefreshRect( rect );
1997 // TODO: how to do it more efficiently?
2002 void wxListMainWindow::RefreshSelected()
2008 if ( InReportView() )
2010 GetVisibleLinesRange(&from, &to);
2015 to = GetItemCount() - 1;
2018 if ( HasCurrent() && m_current >= from && m_current <= to )
2019 RefreshLine(m_current);
2021 for ( size_t line = from; line <= to; line++ )
2023 // NB: the test works as expected even if m_current == -1
2024 if ( line != m_current && IsHighlighted(line) )
2029 extern void wxListCtrl_onPaintCallback(wxGenericListCtrl *listctrl, wxDC *dc); // 2013.12.10. Toshi Nagata
2031 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2033 // Note: a wxPaintDC must be constructed even if no drawing is
2034 // done (a Windows requirement).
2035 wxPaintDC dc( this );
2039 // nothing to draw or not the moment to draw it
2044 RecalculatePositions( false );
2046 GetListCtrl()->PrepareDC( dc );
2049 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2051 dc.SetFont( GetFont() );
2053 if ( InReportView() )
2055 int lineHeight = GetLineHeight();
2057 size_t visibleFrom, visibleTo;
2058 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2061 int xOrig = dc.LogicalToDeviceX( 0 );
2062 int yOrig = dc.LogicalToDeviceY( 0 );
2064 // tell the caller cache to cache the data
2067 wxListEvent evCache(wxEVT_LIST_CACHE_HINT,
2068 GetParent()->GetId());
2069 evCache.SetEventObject( GetParent() );
2070 evCache.m_oldItemIndex = visibleFrom;
2071 evCache.m_item.m_itemId =
2072 evCache.m_itemIndex = visibleTo;
2073 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2076 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2078 rectLine = GetLineRect(line);
2081 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2082 rectLine.width, rectLine.height) )
2084 // don't redraw unaffected lines to avoid flicker
2088 GetLine(line)->DrawInReportMode( &dc,
2090 GetLineHighlightRect(line),
2091 IsHighlighted(line),
2092 line == m_current, line ); // 2013.12.10. Toshi Nagata; add "line" argument
2095 if ( HasFlag(wxLC_HRULES) )
2097 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2098 wxSize clientSize = GetClientSize();
2100 size_t i = visibleFrom;
2101 if (i == 0) i = 1; // Don't draw the first one
2102 for ( ; i <= visibleTo; i++ )
2105 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2106 dc.DrawLine(0 - dev_x, i * lineHeight,
2107 clientSize.x - dev_x, i * lineHeight);
2110 // Draw last horizontal rule
2111 if ( visibleTo == GetItemCount() - 1 )
2114 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2115 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2116 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2120 // Draw vertical rules if required
2121 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2123 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2124 wxRect firstItemRect, lastItemRect;
2126 GetItemRect(visibleFrom, firstItemRect);
2127 GetItemRect(visibleTo, lastItemRect);
2128 int x = firstItemRect.GetX();
2130 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2132 for (int col = 0; col < GetColumnCount(); col++)
2134 int colWidth = GetColumnWidth(col);
2136 int x_pos = x - dev_x;
2137 if (col < GetColumnCount()-1) x_pos -= 2;
2138 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2139 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2145 size_t count = GetItemCount();
2146 for ( size_t i = 0; i < count; i++ )
2148 GetLine(i)->Draw( &dc, i == m_current );
2152 ::wxListCtrl_onPaintCallback(GetListCtrl(), &dc); // 2013.12.10. Toshi Nagata
2154 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2155 // rectangle somehow and so leaves traces when the item is not selected any
2156 // more, see #12229.
2161 if ( IsHighlighted(m_current) )
2162 flags |= wxCONTROL_SELECTED;
2164 wxRendererNative::Get().
2165 DrawFocusRect(this, dc, GetLineHighlightRect(m_current), flags);
2167 #endif // !__WXMAC__
2170 void wxListMainWindow::HighlightAll( bool on )
2172 if ( IsSingleSel() )
2174 wxASSERT_MSG( !on, wxT("can't do this in a single selection control") );
2176 // we just have one item to turn off
2177 if ( HasCurrent() && IsHighlighted(m_current) )
2179 HighlightLine(m_current, false);
2180 RefreshLine(m_current);
2183 else // multi selection
2186 HighlightLines(0, GetItemCount() - 1, on);
2190 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2192 // Do nothing here. This prevents the default handler in wxScrolledWindow
2193 // from needlessly scrolling the window when the edit control is
2194 // dismissed. See ticket #9563.
2197 void wxListMainWindow::SendNotify( size_t line,
2198 wxEventType command,
2199 const wxPoint& point )
2201 wxListEvent le( command, GetParent()->GetId() );
2202 le.SetEventObject( GetParent() );
2204 le.m_item.m_itemId =
2205 le.m_itemIndex = line;
2207 // set only for events which have position
2208 if ( point != wxDefaultPosition )
2209 le.m_pointDrag = point;
2211 // don't try to get the line info for virtual list controls: the main
2212 // program has it anyhow and if we did it would result in accessing all
2213 // the lines, even those which are not visible now and this is precisely
2214 // what we're trying to avoid
2217 if ( line != (size_t)-1 )
2219 GetLine(line)->GetItem( 0, le.m_item );
2221 //else: this happens for wxEVT_LIST_ITEM_FOCUSED event
2223 //else: there may be no more such item
2225 GetParent()->GetEventHandler()->ProcessEvent( le );
2228 void wxListMainWindow::ChangeCurrent(size_t current)
2230 m_current = current;
2232 // as the current item changed, we shouldn't start editing it when the
2233 // "slow click" timer expires as the click happened on another item
2234 if ( m_renameTimer->IsRunning() )
2235 m_renameTimer->Stop();
2237 SendNotify(current, wxEVT_LIST_ITEM_FOCUSED);
2240 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2242 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2243 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2245 wxASSERT_MSG( textControlClass->IsKindOf(wxCLASSINFO(wxTextCtrl)),
2246 wxT("EditLabel() needs a text control") );
2248 size_t itemEdit = (size_t)item;
2250 wxListEvent le( wxEVT_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2251 le.SetEventObject( GetParent() );
2252 le.m_item.m_itemId =
2253 le.m_itemIndex = item;
2254 wxListLineData *data = GetLine(itemEdit);
2255 wxCHECK_MSG( data, NULL, wxT("invalid index in EditLabel()") );
2256 data->GetItem( 0, le.m_item );
2258 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2260 // vetoed by user code
2266 // Ensure the display is updated before we start editing.
2270 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2271 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2272 return m_textctrlWrapper->GetText();
2275 void wxListMainWindow::OnRenameTimer()
2277 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2279 EditLabel( m_current );
2282 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2284 wxListEvent le( wxEVT_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2285 le.SetEventObject( GetParent() );
2286 le.m_item.m_itemId =
2287 le.m_itemIndex = itemEdit;
2289 wxListLineData *data = GetLine(itemEdit);
2291 wxCHECK_MSG( data, false, wxT("invalid index in OnRenameAccept()") );
2293 data->GetItem( 0, le.m_item );
2294 le.m_item.m_text = value;
2295 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2299 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
2301 // let owner know that the edit was cancelled
2302 wxListEvent le( wxEVT_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2304 le.SetEditCanceled(true);
2306 le.SetEventObject( GetParent() );
2307 le.m_item.m_itemId =
2308 le.m_itemIndex = itemEdit;
2310 wxListLineData *data = GetLine(itemEdit);
2311 wxCHECK_RET( data, wxT("invalid index in OnRenameCancelled()") );
2313 data->GetItem( 0, le.m_item );
2314 GetEventHandler()->ProcessEvent( le );
2317 void wxListMainWindow::OnFindTimer()
2319 m_findPrefix.clear();
2324 void wxListMainWindow::EnableBellOnNoMatch( bool on )
2329 void wxListMainWindow::OnMouse( wxMouseEvent &event )
2332 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2333 // shutdown the edit control when the mouse is clicked elsewhere on the
2334 // listctrl because the order of events is different (or something like
2335 // that), so explicitly end the edit if it is active.
2336 if ( event.LeftDown() && m_textctrlWrapper )
2337 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Accept);
2340 if ( event.LeftDown() )
2342 // Ensure we skip the event to let the system set focus to this window.
2346 // Pretend that the event happened in wxListCtrl itself.
2347 wxMouseEvent me(event);
2348 me.SetEventObject( GetParent() );
2349 me.SetId(GetParent()->GetId());
2350 if ( GetParent()->GetEventHandler()->ProcessEvent( me ))
2353 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2355 // let the base class handle mouse wheel events.
2360 if ( !HasCurrent() || IsEmpty() )
2362 if (event.RightDown())
2364 SendNotify( (size_t)-1, wxEVT_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2366 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2367 GetParent()->GetId(),
2368 ClientToScreen(event.GetPosition()));
2369 evtCtx.SetEventObject(GetParent());
2370 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2378 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2379 event.ButtonDClick()) )
2382 int x = event.GetX();
2383 int y = event.GetY();
2384 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
2386 // where did we hit it (if we did)?
2389 size_t count = GetItemCount(),
2392 if ( InReportView() )
2394 current = y / GetLineHeight();
2395 if ( current < count )
2396 hitResult = HitTestLine(current, x, y);
2400 // TODO: optimize it too! this is less simple than for report view but
2401 // enumerating all items is still not a way to do it!!
2402 for ( current = 0; current < count; current++ )
2404 hitResult = HitTestLine(current, x, y);
2410 // Update drag events counter first as we must do it even if the mouse is
2411 // not on any item right now as we must keep count in case we started
2412 // dragging from the empty control area but continued to do it over a valid
2413 // item -- in this situation we must not start dragging this item.
2414 if (event.Dragging())
2419 // The only mouse event that can be generated without any valid item is
2420 // wxEVT_LIST_ITEM_RIGHT_CLICK as it can be useful to have a global
2421 // popup menu for the list control itself which should be shown even when
2422 // the user clicks outside of any item.
2425 // outside of any item
2426 if (event.RightDown())
2428 SendNotify( (size_t) -1, wxEVT_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2430 wxContextMenuEvent evtCtx(
2432 GetParent()->GetId(),
2433 ClientToScreen(event.GetPosition()));
2434 evtCtx.SetEventObject(GetParent());
2435 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2439 // reset the selection and bail out
2440 HighlightAll(false);
2446 if ( event.Dragging() )
2448 if (m_dragCount == 1)
2450 // we have to report the raw, physical coords as we want to be
2451 // able to call HitTest(event.m_pointDrag) from the user code to
2452 // get the item being dragged
2453 m_dragStart = event.GetPosition();
2456 if (m_dragCount != 3)
2459 int command = event.RightIsDown() ? wxEVT_LIST_BEGIN_RDRAG
2460 : wxEVT_LIST_BEGIN_DRAG;
2462 SendNotify( m_lineLastClicked, command, m_dragStart );
2467 bool forceClick = false;
2468 if (event.ButtonDClick())
2470 if ( m_renameTimer->IsRunning() )
2471 m_renameTimer->Stop();
2473 m_lastOnSame = false;
2475 if ( current == m_lineLastClicked )
2477 SendNotify( current, wxEVT_LIST_ITEM_ACTIVATED );
2483 // The first click was on another item, so don't interpret this as
2484 // a double click, but as a simple click instead
2491 if (m_lineSelectSingleOnUp != (size_t)-1)
2493 // select single line
2494 HighlightAll( false );
2495 ReverseHighlight(m_lineSelectSingleOnUp);
2500 if ((current == m_current) &&
2501 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
2502 HasFlag(wxLC_EDIT_LABELS) )
2504 if ( !InReportView() ||
2505 GetLineLabelRect(current).Contains(x, y) )
2507 int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC);
2508 m_renameTimer->Start(dclick > 0 ? dclick : 250, true);
2512 m_lastOnSame = false;
2515 m_lineSelectSingleOnUp = (size_t)-1;
2519 // This is necessary, because after a DnD operation in
2520 // from and to ourself, the up event is swallowed by the
2521 // DnD code. So on next non-up event (which means here and
2522 // now) m_lineSelectSingleOnUp should be reset.
2523 m_lineSelectSingleOnUp = (size_t)-1;
2525 if (event.RightDown())
2527 m_lineBeforeLastClicked = m_lineLastClicked;
2528 m_lineLastClicked = current;
2530 // If the item is already selected, do not update the selection.
2531 // Multi-selections should not be cleared if a selected item is clicked.
2532 if (!IsHighlighted(current))
2534 HighlightAll(false);
2535 ChangeCurrent(current);
2536 ReverseHighlight(m_current);
2539 SendNotify( current, wxEVT_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2541 // Allow generation of context menu event
2544 else if (event.MiddleDown())
2546 SendNotify( current, wxEVT_LIST_ITEM_MIDDLE_CLICK );
2548 else if ( event.LeftDown() || forceClick )
2550 m_lineBeforeLastClicked = m_lineLastClicked;
2551 m_lineLastClicked = current;
2553 size_t oldCurrent = m_current;
2554 bool oldWasSelected = IsHighlighted(m_current);
2556 bool cmdModifierDown = event.CmdDown();
2557 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
2559 if ( IsSingleSel() || !IsHighlighted(current) )
2561 HighlightAll( false );
2563 ChangeCurrent(current);
2565 ReverseHighlight(m_current);
2567 else // multi sel & current is highlighted & no mod keys
2569 m_lineSelectSingleOnUp = current;
2570 ChangeCurrent(current); // change focus
2573 else // multi sel & either ctrl or shift is down
2575 if (cmdModifierDown)
2577 ChangeCurrent(current);
2579 ReverseHighlight(m_current);
2581 else if (event.ShiftDown())
2583 ChangeCurrent(current);
2585 size_t lineFrom = oldCurrent,
2588 if ( lineTo < lineFrom )
2591 lineFrom = m_current;
2594 HighlightLines(lineFrom, lineTo);
2596 else // !ctrl, !shift
2598 // test in the enclosing if should make it impossible
2599 wxFAIL_MSG( wxT("how did we get here?") );
2603 if (m_current != oldCurrent)
2604 RefreshLine( oldCurrent );
2606 // Set the flag telling us whether the next click on this item should
2607 // start editing its label. This should happen if we clicked on the
2608 // current item and it was already selected, i.e. if this click was not
2609 // done to select it.
2611 // It should not happen if this was a double click (forceClick is true)
2612 // nor if we hadn't had the focus before as then this click was used to
2613 // give focus to the control.
2614 m_lastOnSame = (m_current == oldCurrent) && oldWasSelected &&
2615 !forceClick && HasFocus();
2619 void wxListMainWindow::MoveToItem(size_t item)
2621 if ( item == (size_t)-1 )
2624 wxRect rect = GetLineRect(item);
2626 int client_w, client_h;
2627 GetClientSize( &client_w, &client_h );
2629 const int hLine = GetLineHeight();
2631 int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL );
2632 int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL );
2634 if ( InReportView() )
2636 // the next we need the range of lines shown it might be different,
2637 // so recalculate it
2638 ResetVisibleLinesRange();
2640 if (rect.y < view_y)
2641 GetListCtrl()->Scroll( -1, rect.y / hLine );
2642 if (rect.y + rect.height + 5 > view_y + client_h)
2643 GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
2646 // At least on Mac the visible lines value will get reset inside of
2647 // Scroll *before* it actually scrolls the window because of the
2648 // Update() that happens there, so it will still have the wrong value.
2649 // So let's reset it again and wait for it to be recalculated in the
2650 // next paint event. I would expect this problem to show up in wxGTK
2651 // too but couldn't duplicate it there. Perhaps the order of events
2652 // is different... --Robin
2653 ResetVisibleLinesRange();
2661 if (rect.x-view_x < 5)
2662 sx = (rect.x - 5) / SCROLL_UNIT_X;
2663 if (rect.x + rect.width - 5 > view_x + client_w)
2664 sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
2666 if (rect.y-view_y < 5)
2667 sy = (rect.y - 5) / hLine;
2668 if (rect.y + rect.height - 5 > view_y + client_h)
2669 sy = (rect.y + rect.height - client_h + hLine) / hLine;
2671 GetListCtrl()->Scroll(sx, sy);
2675 bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
2677 if ( !InReportView() )
2679 // TODO: this should work in all views but is not implemented now
2684 GetVisibleLinesRange(&top, &bottom);
2686 if ( bottom == (size_t)-1 )
2689 ResetVisibleLinesRange();
2691 int hLine = GetLineHeight();
2693 GetListCtrl()->Scroll(-1, top + dy / hLine);
2696 // see comment in MoveToItem() for why we do this
2697 ResetVisibleLinesRange();
2703 // ----------------------------------------------------------------------------
2704 // keyboard handling
2705 // ----------------------------------------------------------------------------
2707 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
2709 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
2710 wxT("invalid item index in OnArrowChar()") );
2712 size_t oldCurrent = m_current;
2714 // in single selection we just ignore Shift as we can't select several
2716 if ( event.ShiftDown() && !IsSingleSel() )
2718 ChangeCurrent(newCurrent);
2720 // refresh the old focus to remove it
2721 RefreshLine( oldCurrent );
2723 // select all the items between the old and the new one
2724 if ( oldCurrent > newCurrent )
2726 newCurrent = oldCurrent;
2727 oldCurrent = m_current;
2730 HighlightLines(oldCurrent, newCurrent);
2734 // all previously selected items are unselected unless ctrl is held
2735 // in a multiselection control
2736 if ( !event.ControlDown() || IsSingleSel() )
2737 HighlightAll(false);
2739 ChangeCurrent(newCurrent);
2741 // refresh the old focus to remove it
2742 RefreshLine( oldCurrent );
2744 // in single selection mode we must always have a selected item
2745 if ( !event.ControlDown() || IsSingleSel() )
2746 HighlightLine( m_current, true );
2749 RefreshLine( m_current );
2754 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
2756 wxWindow *parent = GetParent();
2758 // propagate the key event upwards
2759 wxKeyEvent ke(event);
2760 ke.SetEventObject( parent );
2761 ke.SetId(GetParent()->GetId());
2762 if (parent->GetEventHandler()->ProcessEvent( ke ))
2765 // send a list event
2766 wxListEvent le( wxEVT_LIST_KEY_DOWN, parent->GetId() );
2767 le.m_item.m_itemId =
2768 le.m_itemIndex = m_current;
2770 GetLine(m_current)->GetItem( 0, le.m_item );
2771 le.m_code = event.GetKeyCode();
2772 le.SetEventObject( parent );
2773 if (parent->GetEventHandler()->ProcessEvent( le ))
2779 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
2781 wxWindow *parent = GetParent();
2783 // propagate the key event upwards
2784 wxKeyEvent ke(event);
2785 ke.SetEventObject( parent );
2786 ke.SetId(GetParent()->GetId());
2787 if (parent->GetEventHandler()->ProcessEvent( ke ))
2793 void wxListMainWindow::OnCharHook( wxKeyEvent &event )
2795 if ( m_textctrlWrapper )
2797 // When an in-place editor is active we should ensure that it always
2798 // gets the key events that are special to it.
2799 if ( m_textctrlWrapper->CheckForEndEditKey(event) )
2801 // Skip the call to wxEvent::Skip() below.
2809 void wxListMainWindow::OnChar( wxKeyEvent &event )
2811 wxWindow *parent = GetParent();
2813 // propagate the char event upwards
2814 wxKeyEvent ke(event);
2815 ke.SetEventObject( parent );
2816 ke.SetId(GetParent()->GetId());
2817 if (parent->GetEventHandler()->ProcessEvent( ke ))
2820 if ( HandleAsNavigationKey(event) )
2823 // no item -> nothing to do
2830 // don't use m_linesPerPage directly as it might not be computed yet
2831 const int pageSize = GetCountPerPage();
2832 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
2834 if (GetLayoutDirection() == wxLayout_RightToLeft)
2836 if (event.GetKeyCode() == WXK_RIGHT)
2837 event.m_keyCode = WXK_LEFT;
2838 else if (event.GetKeyCode() == WXK_LEFT)
2839 event.m_keyCode = WXK_RIGHT;
2842 int keyCode = event.GetKeyCode();
2846 if ( m_current > 0 )
2847 OnArrowChar( m_current - 1, event );
2851 if ( m_current < (size_t)GetItemCount() - 1 )
2852 OnArrowChar( m_current + 1, event );
2857 OnArrowChar( GetItemCount() - 1, event );
2862 OnArrowChar( 0, event );
2867 int steps = InReportView() ? pageSize - 1
2868 : m_current % pageSize;
2870 int index = m_current - steps;
2874 OnArrowChar( index, event );
2880 int steps = InReportView()
2882 : pageSize - (m_current % pageSize) - 1;
2884 size_t index = m_current + steps;
2885 size_t count = GetItemCount();
2886 if ( index >= count )
2889 OnArrowChar( index, event );
2894 if ( !InReportView() )
2896 int index = m_current - pageSize;
2900 OnArrowChar( index, event );
2905 if ( !InReportView() )
2907 size_t index = m_current + pageSize;
2909 size_t count = GetItemCount();
2910 if ( index >= count )
2913 OnArrowChar( index, event );
2918 if ( IsSingleSel() )
2920 if ( event.ControlDown() )
2922 ReverseHighlight(m_current);
2924 else // normal space press
2926 SendNotify( m_current, wxEVT_LIST_ITEM_ACTIVATED );
2929 else // multiple selection
2931 ReverseHighlight(m_current);
2937 SendNotify( m_current, wxEVT_LIST_ITEM_ACTIVATED );
2941 if ( !event.HasModifiers() &&
2942 ((keyCode >= '0' && keyCode <= '9') ||
2943 (keyCode >= 'a' && keyCode <= 'z') ||
2944 (keyCode >= 'A' && keyCode <= 'Z') ||
2950 // find the next item starting with the given prefix
2951 wxChar ch = (wxChar)keyCode;
2954 // if the same character is typed multiple times then go to the
2955 // next entry starting with that character instead of searching
2956 // for an item starting with multiple copies of this character,
2957 // this is more useful and is how it works under Windows.
2958 if ( m_findPrefix.length() == 1 && m_findPrefix[0] == ch )
2960 item = PrefixFindItem(m_current, ch);
2964 const wxString newPrefix(m_findPrefix + ch);
2965 item = PrefixFindItem(m_current, newPrefix);
2966 if ( item != (size_t)-1 )
2967 m_findPrefix = newPrefix;
2970 // also start the timer to reset the current prefix if the user
2971 // doesn't press any more alnum keys soon -- we wouldn't want
2972 // to use this prefix for a new item search
2975 m_findTimer = new wxListFindTimer( this );
2978 // Notice that we should start the timer even if we didn't find
2979 // anything to make sure we reset the search state later.
2980 m_findTimer->Start(wxListFindTimer::DELAY, wxTIMER_ONE_SHOT);
2982 // restart timer even when there's no match so bell get's reset
2983 if ( item != (size_t)-1 )
2985 // Select the found item and go to it.
2986 HighlightAll(false);
2988 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
2989 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
2991 // Reset the bell flag if it had been temporarily disabled
2996 else // No such item
2998 // Signal it with a bell if enabled.
2999 if ( m_findBell == 1 )
3003 // Disable it for the next unsuccessful match, we only
3004 // beep once, this is usually enough and continuing to
3005 // do it would be annoying.
3017 // ----------------------------------------------------------------------------
3019 // ----------------------------------------------------------------------------
3021 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3025 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3026 event.SetEventObject( GetParent() );
3027 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3031 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3032 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3033 // which are already drawn correctly resulting in horrible flicker - avoid
3043 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3047 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3048 event.SetEventObject( GetParent() );
3049 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3057 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
3059 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
3061 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3063 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
3065 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3067 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
3069 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3071 else if ( InReportView() && (m_small_image_list))
3073 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3077 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
3079 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3081 m_normal_image_list->GetSize( index, width, height );
3083 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3085 m_small_image_list->GetSize( index, width, height );
3087 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
3089 m_small_image_list->GetSize( index, width, height );
3091 else if ( InReportView() && m_small_image_list )
3093 m_small_image_list->GetSize( index, width, height );
3102 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
3106 // calc the spacing from the icon size
3107 int width = 0, height = 0;
3109 if ((imageList) && (imageList->GetImageCount()) )
3110 imageList->GetSize(0, width, height);
3112 if (which == wxIMAGE_LIST_NORMAL)
3114 m_normal_image_list = imageList;
3115 m_normal_spacing = width + 8;
3118 if (which == wxIMAGE_LIST_SMALL)
3120 m_small_image_list = imageList;
3121 m_small_spacing = width + 14;
3122 m_lineHeight = 0; // ensure that the line height will be recalc'd
3126 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3130 m_small_spacing = spacing;
3132 m_normal_spacing = spacing;
3135 int wxListMainWindow::GetItemSpacing( bool isSmall )
3137 return isSmall ? m_small_spacing : m_normal_spacing;
3140 // ----------------------------------------------------------------------------
3142 // ----------------------------------------------------------------------------
3145 wxListMainWindow::ComputeMinHeaderWidth(const wxListHeaderData* column) const
3147 wxClientDC dc(const_cast<wxListMainWindow*>(this));
3149 int width = dc.GetTextExtent(column->GetText()).x + AUTOSIZE_COL_MARGIN;
3151 width += 2*EXTRA_WIDTH;
3153 // check for column header's image availability
3154 const int image = column->GetImage();
3157 if ( m_small_image_list )
3160 m_small_image_list->GetSize(image, ix, iy);
3161 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3168 void wxListMainWindow::SetColumn( int col, const wxListItem &item )
3170 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3172 wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
3174 wxListHeaderData *column = node->GetData();
3175 column->SetItem( item );
3177 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3178 column->SetWidth(ComputeMinHeaderWidth(column));
3180 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3182 headerWin->m_dirty = true;
3186 // invalidate it as it has to be recalculated
3190 void wxListMainWindow::SetColumnWidth( int col, int width )
3192 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3193 wxT("invalid column index") );
3195 wxCHECK_RET( InReportView(),
3196 wxT("SetColumnWidth() can only be called in report mode.") );
3200 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3202 headerWin->m_dirty = true;
3204 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3205 wxCHECK_RET( node, wxT("no column?") );
3207 wxListHeaderData *column = node->GetData();
3209 size_t count = GetItemCount();
3211 if (width == wxLIST_AUTOSIZE_USEHEADER)
3213 width = ComputeMinHeaderWidth(column);
3215 else if ( width == wxLIST_AUTOSIZE )
3217 width = ComputeMinHeaderWidth(column);
3221 wxClientDC dc(this);
3222 dc.SetFont( GetFont() );
3224 int max = AUTOSIZE_COL_MARGIN;
3226 // if the cached column width isn't valid then recalculate it
3227 if (m_aColWidths.Item(col)->bNeedsUpdate)
3229 for (size_t i = 0; i < count; i++)
3231 wxListLineData *line = GetLine( i );
3232 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3234 wxCHECK_RET( n, wxT("no subitem?") );
3236 wxListItemData *itemData = n->GetData();
3239 itemData->GetItem(item);
3240 int itemWidth = GetItemWidthWithImage(&item);
3241 if (itemWidth > max)
3245 m_aColWidths.Item(col)->bNeedsUpdate = false;
3246 m_aColWidths.Item(col)->nMaxWidth = max;
3249 max = m_aColWidths.Item(col)->nMaxWidth + AUTOSIZE_COL_MARGIN;
3255 column->SetWidth( width );
3257 // invalidate it as it has to be recalculated
3261 int wxListMainWindow::GetHeaderWidth() const
3263 if ( !m_headerWidth )
3265 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3267 size_t count = GetColumnCount();
3268 for ( size_t col = 0; col < count; col++ )
3270 self->m_headerWidth += GetColumnWidth(col);
3274 return m_headerWidth;
3277 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3279 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3280 wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
3282 wxListHeaderData *column = node->GetData();
3283 column->GetItem( item );
3286 int wxListMainWindow::GetColumnWidth( int col ) const
3288 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3289 wxCHECK_MSG( node, 0, wxT("invalid column index") );
3291 wxListHeaderData *column = node->GetData();
3292 return column->GetWidth();
3295 // ----------------------------------------------------------------------------
3297 // ----------------------------------------------------------------------------
3299 void wxListMainWindow::SetItem( wxListItem &item )
3301 long id = item.m_itemId;
3302 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3303 wxT("invalid item index in SetItem") );
3307 wxListLineData *line = GetLine((size_t)id);
3308 line->SetItem( item.m_col, item );
3310 // Set item state if user wants
3311 if ( item.m_mask & wxLIST_MASK_STATE )
3312 SetItemState( item.m_itemId, item.m_state, item.m_state );
3316 // update the Max Width Cache if needed
3317 int width = GetItemWidthWithImage(&item);
3319 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3320 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3324 // update the item on screen unless we're going to update everything soon
3329 GetItemRect(id, rectItem);
3330 RefreshRect(rectItem);
3334 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3339 // first deal with selection
3340 if ( stateMask & wxLIST_STATE_SELECTED )
3342 // set/clear select state
3345 // optimized version for virtual listctrl.
3346 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3349 else if ( state & wxLIST_STATE_SELECTED )
3351 const long count = GetItemCount();
3352 for( long i = 0; i < count; i++ )
3354 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3360 // clear for non virtual (somewhat optimized by using GetNextItem())
3362 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3364 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3369 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3371 // unfocus all: only one item can be focussed, so clearing focus for
3372 // all items is simply clearing focus of the focussed item.
3373 SetItemState(m_current, state, stateMask);
3375 //(setting focus to all items makes no sense, so it is not handled here.)
3378 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3382 SetItemStateAll(state, stateMask);
3386 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3387 wxT("invalid list ctrl item index in SetItem") );
3389 size_t oldCurrent = m_current;
3390 size_t item = (size_t)litem; // safe because of the check above
3392 // do we need to change the focus?
3393 if ( stateMask & wxLIST_STATE_FOCUSED )
3395 if ( state & wxLIST_STATE_FOCUSED )
3397 // don't do anything if this item is already focused
3398 if ( item != m_current )
3400 ChangeCurrent(item);
3402 if ( oldCurrent != (size_t)-1 )
3404 if ( IsSingleSel() )
3406 HighlightLine(oldCurrent, false);
3409 RefreshLine(oldCurrent);
3412 RefreshLine( m_current );
3417 // don't do anything if this item is not focused
3418 if ( item == m_current )
3422 if ( IsSingleSel() )
3424 // we must unselect the old current item as well or we
3425 // might end up with more than one selected item in a
3426 // single selection control
3427 HighlightLine(oldCurrent, false);
3430 RefreshLine( oldCurrent );
3435 // do we need to change the selection state?
3436 if ( stateMask & wxLIST_STATE_SELECTED )
3438 bool on = (state & wxLIST_STATE_SELECTED) != 0;
3440 if ( IsSingleSel() )
3444 // selecting the item also makes it the focused one in the
3446 if ( m_current != item )
3448 ChangeCurrent(item);
3450 if ( oldCurrent != (size_t)-1 )
3452 HighlightLine( oldCurrent, false );
3453 RefreshLine( oldCurrent );
3459 // only the current item may be selected anyhow
3460 if ( item != m_current )
3465 if ( HighlightLine(item, on) )
3472 int wxListMainWindow::GetItemState( long item, long stateMask ) const
3474 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
3475 wxT("invalid list ctrl item index in GetItemState()") );
3477 int ret = wxLIST_STATE_DONTCARE;
3479 if ( stateMask & wxLIST_STATE_FOCUSED )
3481 if ( (size_t)item == m_current )
3482 ret |= wxLIST_STATE_FOCUSED;
3485 if ( stateMask & wxLIST_STATE_SELECTED )
3487 if ( IsHighlighted(item) )
3488 ret |= wxLIST_STATE_SELECTED;
3494 void wxListMainWindow::GetItem( wxListItem &item ) const
3496 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
3497 wxT("invalid item index in GetItem") );
3499 wxListLineData *line = GetLine((size_t)item.m_itemId);
3500 line->GetItem( item.m_col, item );
3502 // Get item state if user wants it
3503 if ( item.m_mask & wxLIST_MASK_STATE )
3504 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3505 wxLIST_STATE_FOCUSED );
3508 // ----------------------------------------------------------------------------
3510 // ----------------------------------------------------------------------------
3512 size_t wxListMainWindow::GetItemCount() const
3514 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3517 void wxListMainWindow::SetItemCount(long count)
3519 // Update the current item if it's not valid any longer (notice that this
3520 // invalidates it completely if the control is becoming empty, which is the
3521 // right thing to do).
3522 if ( HasCurrent() && m_current >= (size_t)count )
3523 ChangeCurrent(count - 1);
3525 m_selStore.SetItemCount(count);
3526 m_countVirt = count;
3528 ResetVisibleLinesRange();
3530 // scrollbars must be reset
3534 int wxListMainWindow::GetSelectedItemCount() const
3536 // deal with the quick case first
3537 if ( IsSingleSel() )
3538 return HasCurrent() ? IsHighlighted(m_current) : false;
3540 // virtual controls remmebers all its selections itself
3542 return m_selStore.GetSelectedCount();
3544 // TODO: we probably should maintain the number of items selected even for
3545 // non virtual controls as enumerating all lines is really slow...
3546 size_t countSel = 0;
3547 size_t count = GetItemCount();
3548 for ( size_t line = 0; line < count; line++ )
3550 if ( GetLine(line)->IsHighlighted() )
3557 // ----------------------------------------------------------------------------
3558 // item position/size
3559 // ----------------------------------------------------------------------------
3561 wxRect wxListMainWindow::GetViewRect() const
3563 wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
3565 // we need to find the longest/tallest label
3566 wxCoord xMax = 0, yMax = 0;
3567 const int count = GetItemCount();
3570 for ( int i = 0; i < count; i++ )
3572 // we need logical, not physical, coordinates here, so use
3573 // GetLineRect() instead of GetItemRect()
3574 wxRect r = GetLineRect(i);
3576 wxCoord x = r.GetRight(),
3586 // some fudge needed to make it look prettier
3587 xMax += 2 * EXTRA_BORDER_X;
3588 yMax += 2 * EXTRA_BORDER_Y;
3590 // account for the scrollbars if necessary
3591 const wxSize sizeAll = GetClientSize();
3592 if ( xMax > sizeAll.x )
3593 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3594 if ( yMax > sizeAll.y )
3595 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
3597 return wxRect(0, 0, xMax, yMax);
3601 wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const
3603 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(),
3605 wxT("GetSubItemRect only meaningful in report view") );
3606 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false,
3607 wxT("invalid item in GetSubItemRect") );
3609 // ensure that we're laid out, otherwise we could return nonsense
3612 wxConstCast(this, wxListMainWindow)->
3613 RecalculatePositions(true /* no refresh */);
3616 rect = GetLineRect((size_t)item);
3618 // Adjust rect to specified column
3619 if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM )
3621 wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false,
3622 wxT("invalid subItem in GetSubItemRect") );
3624 for (int i = 0; i < subItem; i++)
3626 rect.x += GetColumnWidth(i);
3628 rect.width = GetColumnWidth(subItem);
3631 GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
3636 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
3639 GetItemRect(item, rect);
3647 // ----------------------------------------------------------------------------
3648 // geometry calculation
3649 // ----------------------------------------------------------------------------
3651 void wxListMainWindow::RecalculatePositions(bool noRefresh)
3653 const int lineHeight = GetLineHeight();
3655 wxClientDC dc( this );
3656 dc.SetFont( GetFont() );
3658 const size_t count = GetItemCount();
3661 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3662 iconSpacing = m_normal_spacing;
3663 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3664 iconSpacing = m_small_spacing;
3668 // Note that we do not call GetClientSize() here but
3669 // GetSize() and subtract the border size for sunken
3670 // borders manually. This is technically incorrect,
3671 // but we need to know the client area's size WITHOUT
3672 // scrollbars here. Since we don't know if there are
3673 // any scrollbars, we use GetSize() instead. Another
3674 // solution would be to call SetScrollbars() here to
3675 // remove the scrollbars and call GetClientSize() then,
3676 // but this might result in flicker and - worse - will
3677 // reset the scrollbars to 0 which is not good at all
3678 // if you resize a dialog/window, but don't want to
3679 // reset the window scrolling. RR.
3680 // Furthermore, we actually do NOT subtract the border
3681 // width as 2 pixels is just the extra space which we
3682 // need around the actual content in the window. Other-
3683 // wise the text would e.g. touch the upper border. RR.
3686 GetSize( &clientWidth, &clientHeight );
3688 if ( InReportView() )
3690 // all lines have the same height and we scroll one line per step
3691 int entireHeight = count * lineHeight + LINE_SPACING;
3693 m_linesPerPage = clientHeight / lineHeight;
3695 ResetVisibleLinesRange();
3697 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight,
3698 GetHeaderWidth() / SCROLL_UNIT_X,
3699 (entireHeight + lineHeight - 1) / lineHeight,
3700 GetListCtrl()->GetScrollPos(wxHORIZONTAL),
3701 GetListCtrl()->GetScrollPos(wxVERTICAL),
3706 // we have 3 different layout strategies: either layout all items
3707 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3708 // to arrange them in top to bottom, left to right (don't ask me why
3709 // not the other way round...) order
3710 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
3712 int x = EXTRA_BORDER_X;
3713 int y = EXTRA_BORDER_Y;
3715 wxCoord widthMax = 0;
3718 for ( i = 0; i < count; i++ )
3720 wxListLineData *line = GetLine(i);
3721 line->CalculateSize( &dc, iconSpacing );
3722 line->SetPosition( x, y, iconSpacing );
3724 wxSize sizeLine = GetLineSize(i);
3726 if ( HasFlag(wxLC_ALIGN_TOP) )
3728 if ( sizeLine.x > widthMax )
3729 widthMax = sizeLine.x;
3733 else // wxLC_ALIGN_LEFT
3735 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3739 if ( HasFlag(wxLC_ALIGN_TOP) )
3741 // traverse the items again and tweak their sizes so that they are
3742 // all the same in a row
3743 for ( i = 0; i < count; i++ )
3745 wxListLineData *line = GetLine(i);
3746 line->m_gi->ExtendWidth(widthMax);
3750 GetListCtrl()->SetScrollbars
3754 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3755 (y + lineHeight) / lineHeight,
3756 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3757 GetListCtrl()->GetScrollPos( wxVERTICAL ),
3761 else // "flowed" arrangement, the most complicated case
3763 // at first we try without any scrollbars, if the items don't fit into
3764 // the window, we recalculate after subtracting the space taken by the
3767 int entireWidth = 0;
3769 for (int tries = 0; tries < 2; tries++)
3771 entireWidth = 2 * EXTRA_BORDER_X;
3775 // Now we have decided that the items do not fit into the
3776 // client area, so we need a scrollbar
3777 entireWidth += SCROLL_UNIT_X;
3780 int x = EXTRA_BORDER_X;
3781 int y = EXTRA_BORDER_Y;
3783 // Note that "row" here is vertical, i.e. what is called
3784 // "column" in many other places in wxWidgets.
3785 int maxWidthInThisRow = 0;
3788 int currentlyVisibleLines = 0;
3790 for (size_t i = 0; i < count; i++)
3792 currentlyVisibleLines++;
3793 wxListLineData *line = GetLine( i );
3794 line->CalculateSize( &dc, iconSpacing );
3795 line->SetPosition( x, y, iconSpacing );
3797 wxSize sizeLine = GetLineSize( i );
3799 if ( maxWidthInThisRow < sizeLine.x )
3800 maxWidthInThisRow = sizeLine.x;
3803 if (currentlyVisibleLines > m_linesPerPage)
3804 m_linesPerPage = currentlyVisibleLines;
3806 // Have we reached the end of the row either because no
3807 // more items would fit or because there are simply no more
3809 if ( y + sizeLine.y >= clientHeight
3812 // Adjust all items in this row to have the same
3813 // width to ensure that they all align horizontally in
3815 if ( HasFlag(wxLC_ICON) || HasFlag(wxLC_SMALL_ICON) )
3817 size_t firstRowLine = i - currentlyVisibleLines + 1;
3818 for (size_t j = firstRowLine; j <= i; j++)
3820 GetLine(j)->m_gi->ExtendWidth(maxWidthInThisRow);
3824 currentlyVisibleLines = 0;
3826 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
3827 x += maxWidthInThisRow;
3828 entireWidth += maxWidthInThisRow;
3829 maxWidthInThisRow = 0;
3832 if ( (tries == 0) &&
3833 (entireWidth + SCROLL_UNIT_X > clientWidth) )
3835 clientHeight -= wxSystemSettings::
3836 GetMetric(wxSYS_HSCROLL_Y);
3841 if ( i == count - 1 )
3842 tries = 1; // Everything fits, no second try required.
3846 GetListCtrl()->SetScrollbars
3850 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3852 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3861 // FIXME: why should we call it from here?
3868 void wxListMainWindow::RefreshAll()
3873 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3874 if ( headerWin && headerWin->m_dirty )
3876 headerWin->m_dirty = false;
3877 headerWin->Refresh();
3881 void wxListMainWindow::UpdateCurrent()
3883 if ( !HasCurrent() && !IsEmpty() )
3887 long wxListMainWindow::GetNextItem( long item,
3888 int WXUNUSED(geometry),
3892 max = GetItemCount();
3893 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
3894 wxT("invalid listctrl index in GetNextItem()") );
3896 // notice that we start with the next item (or the first one if item == -1)
3897 // and this is intentional to allow writing a simple loop to iterate over
3898 // all selected items
3901 // this is not an error because the index was OK initially,
3902 // just no such item
3909 size_t count = GetItemCount();
3910 for ( size_t line = (size_t)ret; line < count; line++ )
3912 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
3915 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
3922 // ----------------------------------------------------------------------------
3924 // ----------------------------------------------------------------------------
3926 void wxListMainWindow::DeleteItem( long lindex )
3928 size_t count = GetItemCount();
3930 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
3931 wxT("invalid item index in DeleteItem") );
3933 size_t index = (size_t)lindex;
3935 // we don't need to adjust the index for the previous items
3936 if ( HasCurrent() && m_current >= index )
3938 // if the current item is being deleted, we want the next one to
3939 // become selected - unless there is no next one - so don't adjust
3940 // m_current in this case
3941 if ( m_current != index || m_current == count - 1 )
3945 if ( InReportView() )
3947 // mark the Column Max Width cache as dirty if the items in the line
3948 // we're deleting contain the Max Column Width
3949 wxListLineData * const line = GetLine(index);
3950 wxListItemDataList::compatibility_iterator n;
3951 wxListItemData *itemData;
3955 for (size_t i = 0; i < m_columns.GetCount(); i++)
3957 n = line->m_items.Item( i );
3958 itemData = n->GetData();
3959 itemData->GetItem(item);
3961 itemWidth = GetItemWidthWithImage(&item);
3963 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
3964 m_aColWidths.Item(i)->bNeedsUpdate = true;
3967 ResetVisibleLinesRange();
3970 SendNotify( index, wxEVT_LIST_DELETE_ITEM, wxDefaultPosition );
3975 m_selStore.OnItemDelete(index);
3979 m_lines.RemoveAt( index );
3982 // we need to refresh the (vert) scrollbar as the number of items changed
3985 RefreshAfter(index);
3988 void wxListMainWindow::DeleteColumn( int col )
3990 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3992 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
3995 delete node->GetData();
3996 m_columns.Erase( node );
4000 // update all the items
4001 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4003 wxListLineData * const line = GetLine(i);
4005 // In the following atypical but possible scenario it can be
4006 // legal to call DeleteColumn() but the items may not have any
4008 // 1. In report view, insert a second column.
4009 // 2. Still in report view, add an item with 2 values.
4010 // 3. Switch to an icon (or list) view.
4011 // 4. Add an item -- necessarily with 1 value only.
4012 // 5. Switch back to report view.
4013 // 6. Call DeleteColumn().
4014 // So we need to check for this as otherwise we would simply crash
4016 if ( line->m_items.GetCount() <= static_cast<unsigned>(col) )
4019 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4020 delete n->GetData();
4021 line->m_items.Erase(n);
4025 if ( InReportView() ) // we only cache max widths when in Report View
4027 delete m_aColWidths.Item(col);
4028 m_aColWidths.RemoveAt(col);
4031 // invalidate it as it has to be recalculated
4035 void wxListMainWindow::DoDeleteAllItems()
4038 // nothing to do - in particular, don't send the event
4043 // to make the deletion of all items faster, we don't send the
4044 // notifications for each item deletion in this case but only one event
4045 // for all of them: this is compatible with wxMSW and documented in
4046 // DeleteAllItems() description
4048 wxListEvent event( wxEVT_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4049 event.SetEventObject( GetParent() );
4050 GetParent()->GetEventHandler()->ProcessEvent( event );
4058 if ( InReportView() )
4060 ResetVisibleLinesRange();
4061 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4063 m_aColWidths.Item(i)->bNeedsUpdate = true;
4070 void wxListMainWindow::DeleteAllItems()
4074 RecalculatePositions();
4077 void wxListMainWindow::DeleteEverything()
4079 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
4080 WX_CLEAR_ARRAY(m_aColWidths);
4085 // ----------------------------------------------------------------------------
4086 // scanning for an item
4087 // ----------------------------------------------------------------------------
4089 void wxListMainWindow::EnsureVisible( long index )
4091 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4092 wxT("invalid index in EnsureVisible") );
4094 // We have to call this here because the label in question might just have
4095 // been added and its position is not known yet
4097 RecalculatePositions(true /* no refresh */);
4099 MoveToItem((size_t)index);
4102 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
4108 wxString str_upper = str.Upper();
4112 size_t count = GetItemCount();
4113 for ( size_t i = (size_t)pos; i < count; i++ )
4115 wxListLineData *line = GetLine(i);
4116 wxString line_upper = line->GetText(0).Upper();
4119 if (line_upper == str_upper )
4124 if (line_upper.find(str_upper) == 0)
4132 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
4138 size_t count = GetItemCount();
4139 for (size_t i = (size_t)pos; i < count; i++)
4141 wxListLineData *line = GetLine(i);
4143 line->GetItem( 0, item );
4144 if (item.m_data == data)
4151 long wxListMainWindow::FindItem( const wxPoint& pt )
4154 GetVisibleLinesRange( &topItem, NULL );
4157 GetItemPosition( GetItemCount() - 1, p );
4161 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4162 if ( id >= 0 && id < (long)GetItemCount() )
4168 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4170 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
4172 size_t count = GetItemCount();
4174 if ( InReportView() )
4176 size_t current = y / GetLineHeight();
4177 if ( current < count )
4179 flags = HitTestLine(current, x, y);
4186 // TODO: optimize it too! this is less simple than for report view but
4187 // enumerating all items is still not a way to do it!!
4188 for ( size_t current = 0; current < count; current++ )
4190 flags = HitTestLine(current, x, y);
4199 // ----------------------------------------------------------------------------
4201 // ----------------------------------------------------------------------------
4203 void wxListMainWindow::InsertItem( wxListItem &item )
4205 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4207 int count = GetItemCount();
4208 wxCHECK_RET( item.m_itemId >= 0, wxT("invalid item index") );
4210 if (item.m_itemId > count)
4211 item.m_itemId = count;
4213 size_t id = item.m_itemId;
4217 if ( InReportView() )
4219 ResetVisibleLinesRange();
4221 const unsigned col = item.GetColumn();
4222 wxCHECK_RET( col < m_aColWidths.size(), "invalid item column" );
4224 // calculate the width of the item and adjust the max column width
4225 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(col);
4226 int width = GetItemWidthWithImage(&item);
4227 item.SetWidth(width);
4228 if (width > pWidthInfo->nMaxWidth)
4229 pWidthInfo->nMaxWidth = width;
4232 wxListLineData *line = new wxListLineData(this);
4234 line->SetItem( item.m_col, item );
4235 if ( item.m_mask & wxLIST_MASK_IMAGE )
4237 // Reset the buffered height if it's not big enough for the new image.
4238 int image = item.GetImage();
4239 if ( m_small_image_list && image != -1 && InReportView() )
4241 int imageWidth, imageHeight;
4242 m_small_image_list->GetSize(image, imageWidth, imageHeight);
4244 if ( imageHeight > m_lineHeight )
4249 m_lines.Insert( line, id );
4253 // If an item is selected at or below the point of insertion, we need to
4254 // increment the member variables because the current row's index has gone
4256 if ( HasCurrent() && m_current >= id )
4259 SendNotify(id, wxEVT_LIST_INSERT_ITEM);
4261 RefreshLines(id, GetItemCount() - 1);
4264 long wxListMainWindow::InsertColumn( long col, const wxListItem &item )
4269 if ( InReportView() )
4271 wxListHeaderData *column = new wxListHeaderData( item );
4272 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4273 column->SetWidth(ComputeMinHeaderWidth(column));
4275 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4277 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4280 wxListHeaderDataList::compatibility_iterator
4281 node = m_columns.Item( col );
4282 m_columns.Insert( node, column );
4283 m_aColWidths.Insert( colWidthInfo, col );
4288 idx = m_aColWidths.GetCount();
4289 m_columns.Append( column );
4290 m_aColWidths.Add( colWidthInfo );
4295 // update all the items
4296 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4298 wxListLineData * const line = GetLine(i);
4299 wxListItemData * const data = new wxListItemData(this);
4301 line->m_items.Insert(col, data);
4303 line->m_items.Append(data);
4307 // invalidate it as it has to be recalculated
4313 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4316 wxClientDC dc(this);
4318 dc.SetFont( GetFont() );
4320 if (item->GetImage() != -1)
4323 GetImageSize( item->GetImage(), ix, iy );
4327 if (!item->GetText().empty())
4330 dc.GetTextExtent( item->GetText(), &w, NULL );
4337 // ----------------------------------------------------------------------------
4339 // ----------------------------------------------------------------------------
4341 static wxListCtrlCompare list_ctrl_compare_func_2;
4342 static wxIntPtr list_ctrl_compare_data;
4344 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4346 wxListLineData *line1 = *arg1;
4347 wxListLineData *line2 = *arg2;
4349 line1->GetItem( 0, item );
4350 wxUIntPtr data1 = item.m_data;
4351 line2->GetItem( 0, item );
4352 wxUIntPtr data2 = item.m_data;
4353 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4356 void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
4358 // selections won't make sense any more after sorting the items so reset
4360 HighlightAll(false);
4363 list_ctrl_compare_func_2 = fn;
4364 list_ctrl_compare_data = data;
4365 m_lines.Sort( list_ctrl_compare_func_1 );
4369 // ----------------------------------------------------------------------------
4371 // ----------------------------------------------------------------------------
4373 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4375 // update our idea of which lines are shown when we redraw the window the
4377 ResetVisibleLinesRange();
4379 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4381 wxGenericListCtrl* lc = GetListCtrl();
4382 wxCHECK_RET( lc, wxT("no listctrl window?") );
4384 if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4386 lc->m_headerWin->Refresh();
4387 lc->m_headerWin->Update();
4392 int wxListMainWindow::GetCountPerPage() const
4394 if ( !m_linesPerPage )
4396 wxConstCast(this, wxListMainWindow)->
4397 m_linesPerPage = GetClientSize().y / GetLineHeight();
4400 return m_linesPerPage;
4403 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4405 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4407 if ( m_lineFrom == (size_t)-1 )
4409 size_t count = GetItemCount();
4412 m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL);
4414 // this may happen if SetScrollbars() hadn't been called yet
4415 if ( m_lineFrom >= count )
4416 m_lineFrom = count - 1;
4418 // we redraw one extra line but this is needed to make the redrawing
4419 // logic work when there is a fractional number of lines on screen
4420 m_lineTo = m_lineFrom + m_linesPerPage;
4421 if ( m_lineTo >= count )
4422 m_lineTo = count - 1;
4424 else // empty control
4427 m_lineTo = (size_t)-1;
4431 wxASSERT_MSG( IsEmpty() ||
4432 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4433 wxT("GetVisibleLinesRange() returns incorrect result") );
4442 wxListMainWindow::PrefixFindItem(size_t idParent,
4443 const wxString& prefixOrig) const
4445 // if no items then just return
4446 if ( idParent == (size_t)-1 )
4449 // match is case insensitive as this is more convenient to the user: having
4450 // to press Shift-letter to go to the item starting with a capital letter
4451 // would be too bothersome
4452 wxString prefix = prefixOrig.Lower();
4454 // determine the starting point: we shouldn't take the current item (this
4455 // allows to switch between two items starting with the same letter just by
4456 // pressing it) but we shouldn't jump to the next one if the user is
4457 // continuing to type as otherwise he might easily skip the item he wanted
4458 size_t itemid = idParent;
4459 if ( prefix.length() == 1 )
4464 // look for the item starting with the given prefix after it
4465 while ( ( itemid < (size_t)GetItemCount() ) &&
4466 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4471 // if we haven't found anything...
4472 if ( !( itemid < (size_t)GetItemCount() ) )
4474 // ... wrap to the beginning
4477 // and try all the items (stop when we get to the one we started from)
4478 while ( ( itemid < (size_t)GetItemCount() ) && itemid != idParent &&
4479 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4483 // If we haven't found the item, id will be (size_t)-1, as per
4485 if ( !( itemid < (size_t)GetItemCount() ) ||
4486 ( ( itemid == idParent ) &&
4487 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) ) )
4489 itemid = (size_t)-1;
4496 // -------------------------------------------------------------------------------------
4497 // wxGenericListCtrl
4498 // -------------------------------------------------------------------------------------
4500 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4502 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxListCtrlBase)
4503 EVT_SIZE(wxGenericListCtrl::OnSize)
4504 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll)
4507 void wxGenericListCtrl::Init()
4509 m_imageListNormal = NULL;
4510 m_imageListSmall = NULL;
4511 m_imageListState = NULL;
4513 m_ownsImageListNormal =
4514 m_ownsImageListSmall =
4515 m_ownsImageListState = false;
4521 wxGenericListCtrl::~wxGenericListCtrl()
4523 if (m_ownsImageListNormal)
4524 delete m_imageListNormal;
4525 if (m_ownsImageListSmall)
4526 delete m_imageListSmall;
4527 if (m_ownsImageListState)
4528 delete m_imageListState;
4531 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4533 bool needs_header = HasHeader();
4534 bool has_header = (m_headerWin != NULL);
4536 if (needs_header == has_header)
4541 // Notice that we must initialize m_headerWin first, and create the
4542 // real window only later, so that the test in the beginning of the
4543 // function blocks repeated creation of the header as it could happen
4544 // before via wxNavigationEnabled::AddChild() -> ToggleWindowStyle() ->
4545 // SetWindowStyleFlag().
4546 m_headerWin = new wxListHeaderWindow();
4549 this, wxID_ANY, m_mainWin,
4554 wxRendererNative::Get().GetHeaderButtonHeight(this)
4559 #if defined( __WXMAC__ )
4560 static wxFont font( wxOSX_SYSTEM_FONT_SMALL );
4561 m_headerWin->SetFont( font );
4564 GetSizer()->Prepend( m_headerWin, 0, wxGROW );
4568 GetSizer()->Detach( m_headerWin );
4570 wxDELETE(m_headerWin);
4574 bool wxGenericListCtrl::Create(wxWindow *parent,
4579 const wxValidator &validator,
4580 const wxString &name)
4584 // just like in other ports, an assert will fail if the user doesn't give any type style:
4585 wxASSERT_MSG( (style & wxLC_MASK_TYPE),
4586 wxT("wxListCtrl style should have exactly one mode bit set") );
4588 if ( !wxListCtrlBase::Create( parent, id, pos, size,
4589 style | wxVSCROLL | wxHSCROLL,
4593 m_mainWin = new wxListMainWindow(this, wxID_ANY, wxPoint(0, 0), size);
4595 SetTargetWindow( m_mainWin );
4597 // We use the cursor keys for moving the selection, not scrolling, so call
4598 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4599 // keyboard events forwarded to us from wxListMainWindow.
4600 DisableKeyboardScrolling();
4602 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4603 sizer->Add( m_mainWin, 1, wxGROW );
4606 CreateOrDestroyHeaderWindowAsNeeded();
4608 SetInitialSize(size);
4613 wxBorder wxGenericListCtrl::GetDefaultBorder() const
4615 return wxBORDER_THEME;
4618 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4619 WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg,
4623 WXLRESULT rc = wxListCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4625 // we need to process arrows ourselves for scrolling
4626 if ( nMsg == WM_GETDLGCODE )
4628 rc |= DLGC_WANTARROWS;
4635 wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4637 wxSize newsize = size;
4639 newsize.y -= m_headerWin->GetSize().y;
4644 void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event)
4646 // update our idea of which lines are shown when we redraw
4647 // the window the next time
4648 m_mainWin->ResetVisibleLinesRange();
4650 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4652 // Toshi Nagata 2014.01.13.
4653 // Defer the scrolling of the header window after all scrolling of the
4654 // List window is complete
4655 // m_headerWin->Refresh();
4656 // m_headerWin->Update();
4657 wxCommandEvent event(ListCtrlEvent);
4658 m_headerWin->QueueListCtrlEvent(event);
4662 // Let the window be scrolled as usual by the default handler.
4666 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
4668 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4669 wxT("wxLC_VIRTUAL can't be [un]set") );
4671 long flag = GetWindowStyle();
4675 if (style & wxLC_MASK_TYPE)
4676 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
4677 if (style & wxLC_MASK_ALIGN)
4678 flag &= ~wxLC_MASK_ALIGN;
4679 if (style & wxLC_MASK_SORT)
4680 flag &= ~wxLC_MASK_SORT;
4688 // some styles can be set without recreating everything (as happens in
4689 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4690 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
4693 wxWindow::SetWindowStyleFlag(flag);
4697 SetWindowStyleFlag( flag );
4701 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
4703 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4704 // makes sense to remove them as we'll always add scrollbars anyhow when
4706 flag |= wxHSCROLL | wxVSCROLL;
4708 const bool wasInReportView = HasFlag(wxLC_REPORT);
4710 // update the window style first so that the header is created or destroyed
4711 // corresponding to the new style
4712 wxWindow::SetWindowStyleFlag( flag );
4716 const bool inReportView = (flag & wxLC_REPORT) != 0;
4717 if ( inReportView != wasInReportView )
4719 // we need to notify the main window about this change as it must
4720 // update its data structures
4721 m_mainWin->SetReportView(inReportView);
4724 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4726 CreateOrDestroyHeaderWindowAsNeeded();
4728 GetSizer()->Layout();
4732 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
4734 m_mainWin->GetColumn( col, item );
4738 bool wxGenericListCtrl::SetColumn( int col, const wxListItem& item )
4740 m_mainWin->SetColumn( col, item );
4744 int wxGenericListCtrl::GetColumnWidth( int col ) const
4746 return m_mainWin->GetColumnWidth( col );
4749 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
4751 m_mainWin->SetColumnWidth( col, width );
4755 int wxGenericListCtrl::GetCountPerPage() const
4757 return m_mainWin->GetCountPerPage(); // different from Windows ?
4760 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
4762 m_mainWin->GetItem( info );
4766 bool wxGenericListCtrl::SetItem( wxListItem &info )
4768 m_mainWin->SetItem( info );
4772 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
4775 info.m_text = label;
4776 info.m_mask = wxLIST_MASK_TEXT;
4777 info.m_itemId = index;
4781 info.m_image = imageId;
4782 info.m_mask |= wxLIST_MASK_IMAGE;
4785 m_mainWin->SetItem(info);
4789 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
4791 return m_mainWin->GetItemState( item, stateMask );
4794 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
4796 m_mainWin->SetItemState( item, state, stateMask );
4801 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
4803 return SetItemColumnImage(item, 0, image);
4807 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
4810 info.m_image = image;
4811 info.m_mask = wxLIST_MASK_IMAGE;
4812 info.m_itemId = item;
4813 info.m_col = column;
4814 m_mainWin->SetItem( info );
4818 wxString wxGenericListCtrl::GetItemText( long item, int col ) const
4820 return m_mainWin->GetItemText(item, col);
4823 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
4825 m_mainWin->SetItemText(item, str);
4828 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
4831 info.m_mask = wxLIST_MASK_DATA;
4832 info.m_itemId = item;
4833 m_mainWin->GetItem( info );
4837 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
4840 info.m_mask = wxLIST_MASK_DATA;
4841 info.m_itemId = item;
4843 m_mainWin->SetItem( info );
4847 wxRect wxGenericListCtrl::GetViewRect() const
4849 return m_mainWin->GetViewRect();
4852 bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const
4854 return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code);
4857 bool wxGenericListCtrl::GetSubItemRect(long item,
4860 int WXUNUSED(code)) const
4862 if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) )
4865 if ( m_mainWin->HasHeader() )
4866 rect.y += m_headerWin->GetSize().y + 1;
4871 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
4873 m_mainWin->GetItemPosition( item, pos );
4877 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
4882 int wxGenericListCtrl::GetItemCount() const
4884 return m_mainWin->GetItemCount();
4887 int wxGenericListCtrl::GetColumnCount() const
4889 return m_mainWin->GetColumnCount();
4892 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
4894 m_mainWin->SetItemSpacing( spacing, isSmall );
4897 wxSize wxGenericListCtrl::GetItemSpacing() const
4899 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
4901 return wxSize(spacing, spacing);
4904 #if WXWIN_COMPATIBILITY_2_6
4905 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
4907 return m_mainWin->GetItemSpacing( isSmall );
4909 #endif // WXWIN_COMPATIBILITY_2_6
4911 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
4914 info.m_itemId = item;
4915 info.SetTextColour( col );
4916 m_mainWin->SetItem( info );
4919 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
4922 info.m_itemId = item;
4923 m_mainWin->GetItem( info );
4924 return info.GetTextColour();
4927 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
4930 info.m_itemId = item;
4931 info.SetBackgroundColour( col );
4932 m_mainWin->SetItem( info );
4935 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
4938 info.m_itemId = item;
4939 m_mainWin->GetItem( info );
4940 return info.GetBackgroundColour();
4943 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
4946 info.m_itemId = item;
4948 m_mainWin->SetItem( info );
4951 wxFont wxGenericListCtrl::GetItemFont( long item ) const
4954 info.m_itemId = item;
4955 m_mainWin->GetItem( info );
4956 return info.GetFont();
4959 int wxGenericListCtrl::GetSelectedItemCount() const
4961 return m_mainWin->GetSelectedItemCount();
4964 wxColour wxGenericListCtrl::GetTextColour() const
4966 return GetForegroundColour();
4969 void wxGenericListCtrl::SetTextColour(const wxColour& col)
4971 SetForegroundColour(col);
4974 long wxGenericListCtrl::GetTopItem() const
4977 m_mainWin->GetVisibleLinesRange(&top, NULL);
4981 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
4983 return m_mainWin->GetNextItem( item, geom, state );
4986 wxImageList *wxGenericListCtrl::GetImageList(int which) const
4988 if (which == wxIMAGE_LIST_NORMAL)
4989 return m_imageListNormal;
4990 else if (which == wxIMAGE_LIST_SMALL)
4991 return m_imageListSmall;
4992 else if (which == wxIMAGE_LIST_STATE)
4993 return m_imageListState;
4998 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
5000 if ( which == wxIMAGE_LIST_NORMAL )
5002 if (m_ownsImageListNormal)
5003 delete m_imageListNormal;
5004 m_imageListNormal = imageList;
5005 m_ownsImageListNormal = false;
5007 else if ( which == wxIMAGE_LIST_SMALL )
5009 if (m_ownsImageListSmall)
5010 delete m_imageListSmall;
5011 m_imageListSmall = imageList;
5012 m_ownsImageListSmall = false;
5014 else if ( which == wxIMAGE_LIST_STATE )
5016 if (m_ownsImageListState)
5017 delete m_imageListState;
5018 m_imageListState = imageList;
5019 m_ownsImageListState = false;
5022 m_mainWin->SetImageList( imageList, which );
5025 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
5027 SetImageList(imageList, which);
5028 if ( which == wxIMAGE_LIST_NORMAL )
5029 m_ownsImageListNormal = true;
5030 else if ( which == wxIMAGE_LIST_SMALL )
5031 m_ownsImageListSmall = true;
5032 else if ( which == wxIMAGE_LIST_STATE )
5033 m_ownsImageListState = true;
5036 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
5041 bool wxGenericListCtrl::DeleteItem( long item )
5043 m_mainWin->DeleteItem( item );
5047 bool wxGenericListCtrl::DeleteAllItems()
5049 m_mainWin->DeleteAllItems();
5053 bool wxGenericListCtrl::DeleteAllColumns()
5055 size_t count = m_mainWin->m_columns.GetCount();
5056 for ( size_t n = 0; n < count; n++ )
5061 void wxGenericListCtrl::ClearAll()
5063 m_mainWin->DeleteEverything();
5066 bool wxGenericListCtrl::DeleteColumn( int col )
5068 m_mainWin->DeleteColumn( col );
5070 // if we don't have the header any longer, we need to relayout the window
5071 // if ( !GetColumnCount() )
5074 // Ensure that the non-existent columns are really removed from display.
5080 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5081 wxClassInfo* textControlClass)
5083 return m_mainWin->EditLabel( item, textControlClass );
5086 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
5088 return m_mainWin->GetEditControl();
5091 bool wxGenericListCtrl::EnsureVisible( long item )
5093 m_mainWin->EnsureVisible( item );
5097 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
5099 return m_mainWin->FindItem( start, str, partial );
5102 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
5104 return m_mainWin->FindItem( start, data );
5107 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
5108 int WXUNUSED(direction))
5110 return m_mainWin->FindItem( pt );
5113 // TODO: sub item hit testing
5114 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
5116 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
5119 long wxGenericListCtrl::InsertItem( wxListItem& info )
5121 m_mainWin->InsertItem( info );
5122 return info.m_itemId;
5125 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
5128 info.m_text = label;
5129 info.m_mask = wxLIST_MASK_TEXT;
5130 info.m_itemId = index;
5131 return InsertItem( info );
5134 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
5137 info.m_mask = wxLIST_MASK_IMAGE;
5138 info.m_image = imageIndex;
5139 info.m_itemId = index;
5140 return InsertItem( info );
5143 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
5146 info.m_text = label;
5147 info.m_image = imageIndex;
5148 info.m_mask = wxLIST_MASK_TEXT;
5149 if (imageIndex > -1)
5150 info.m_mask |= wxLIST_MASK_IMAGE;
5151 info.m_itemId = index;
5152 return InsertItem( info );
5155 long wxGenericListCtrl::DoInsertColumn( long col, const wxListItem &item )
5157 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
5159 long idx = m_mainWin->InsertColumn( col, item );
5161 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
5162 // still have m_headerWin==NULL
5164 m_headerWin->Refresh();
5169 bool wxGenericListCtrl::ScrollList( int dx, int dy )
5171 return m_mainWin->ScrollList(dx, dy);
5175 // fn is a function which takes 3 long arguments: item1, item2, data.
5176 // item1 is the long data associated with a first item (NOT the index).
5177 // item2 is the long data associated with a second item (NOT the index).
5178 // data is the same value as passed to SortItems.
5179 // The return value is a negative number if the first item should precede the second
5180 // item, a positive number of the second item should precede the first,
5181 // or zero if the two items are equivalent.
5182 // data is arbitrary data to be passed to the sort function.
5184 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, wxIntPtr data )
5186 m_mainWin->SortItems( fn, data );
5190 // ----------------------------------------------------------------------------
5192 // ----------------------------------------------------------------------------
5194 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
5196 if (!m_mainWin) return;
5198 // We need to override OnSize so that our scrolled
5199 // window a) does call Layout() to use sizers for
5200 // positioning the controls but b) does not query
5201 // the sizer for their size and use that for setting
5202 // the scrollable area as set that ourselves by
5203 // calling SetScrollbar() further down.
5207 m_mainWin->RecalculatePositions();
5212 void wxGenericListCtrl::OnInternalIdle()
5214 wxWindow::OnInternalIdle();
5216 if (m_mainWin->m_dirty)
5217 m_mainWin->RecalculatePositions();
5220 // ----------------------------------------------------------------------------
5222 // ----------------------------------------------------------------------------
5224 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5228 m_mainWin->SetBackgroundColour( colour );
5229 m_mainWin->m_dirty = true;
5235 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5237 if ( !wxWindow::SetForegroundColour( colour ) )
5242 m_mainWin->SetForegroundColour( colour );
5243 m_mainWin->m_dirty = true;
5247 m_headerWin->SetForegroundColour( colour );
5252 bool wxGenericListCtrl::SetFont( const wxFont &font )
5254 if ( !wxWindow::SetFont( font ) )
5259 m_mainWin->SetFont( font );
5260 m_mainWin->m_dirty = true;
5265 m_headerWin->SetFont( font );
5266 // CalculateAndSetHeaderHeight();
5276 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5279 // Use the same color scheme as wxListBox
5280 return wxListBox::GetClassDefaultAttributes(variant);
5282 wxUnusedVar(variant);
5283 wxVisualAttributes attr;
5284 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
5285 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5286 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5291 // ----------------------------------------------------------------------------
5292 // methods forwarded to m_mainWin
5293 // ----------------------------------------------------------------------------
5295 #if wxUSE_DRAG_AND_DROP
5297 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5299 m_mainWin->SetDropTarget( dropTarget );
5302 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5304 return m_mainWin->GetDropTarget();
5309 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5311 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5314 wxColour wxGenericListCtrl::GetBackgroundColour() const
5316 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5319 wxColour wxGenericListCtrl::GetForegroundColour() const
5321 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5324 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5327 return m_mainWin->PopupMenu( menu, x, y );
5333 wxSize wxGenericListCtrl::DoGetBestClientSize() const
5335 // The base class version can compute the best size in report view only.
5336 wxSize sizeBest = wxListCtrlBase::DoGetBestClientSize();
5338 if ( !InReportView() )
5340 // Ensure that our minimal width is at least big enough to show all our
5341 // items. This is important for wxListbook to size itself correctly.
5343 // Remember the offset of the first item: this corresponds to the
5344 // margins around the item so we will add it to the minimal size below
5345 // to ensure that we have equal margins on all sides.
5348 // We can iterate over all items as there shouldn't be too many of them
5349 // in non-report view. If it ever becomes a problem, we could examine
5350 // just the first few items probably, the determination of the best
5351 // size is less important if we will need scrollbars anyhow.
5352 for ( int n = 0; n < GetItemCount(); n++ )
5354 const wxRect itemRect = m_mainWin->GetLineRect(n);
5357 // Remember the position of the first item as all the rest are
5358 // offset by at least this number of pixels too.
5359 ofs = itemRect.GetPosition();
5362 sizeBest.IncTo(itemRect.GetSize());
5365 sizeBest.IncBy(2*ofs);
5368 // If we have the scrollbars we need to account for them too. And to
5369 // make sure the scrollbars status is up to date we need to call this
5370 // function to set them.
5371 m_mainWin->RecalculatePositions(true /* no refresh */);
5373 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5374 // to use m_mainWin client/virtual size for determination of whether we
5375 // use scrollbars and not the size of this window itself. Maybe that
5376 // function should be extended to work correctly in the case when our
5377 // scrollbars manage a different window from this one but currently it
5379 const wxSize sizeClient = m_mainWin->GetClientSize();
5380 const wxSize sizeVirt = m_mainWin->GetVirtualSize();
5382 if ( sizeVirt.x > sizeClient.x /* HasScrollbar(wxHORIZONTAL) */ )
5383 sizeBest.y += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
5385 if ( sizeVirt.y > sizeClient.y /* HasScrollbar(wxVERTICAL) */ )
5386 sizeBest.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
5392 // ----------------------------------------------------------------------------
5393 // virtual list control support
5394 // ----------------------------------------------------------------------------
5396 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5398 // this is a pure virtual function, in fact - which is not really pure
5399 // because the controls which are not virtual don't need to implement it
5400 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5402 return wxEmptyString;
5405 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5407 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5409 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5413 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5416 return OnGetItemImage(item);
5421 void wxGenericListCtrl::SetItemCount(long count)
5423 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5425 m_mainWin->SetItemCount(count);
5428 void wxGenericListCtrl::RefreshItem(long item)
5430 m_mainWin->RefreshLine(item);
5433 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5435 m_mainWin->RefreshLines(itemFrom, itemTo);
5438 void wxGenericListCtrl::EnableBellOnNoMatch( bool on )
5440 m_mainWin->EnableBellOnNoMatch(on);
5443 // Generic wxListCtrl is more or less a container for two other
5444 // windows which drawings are done upon. These are namely
5445 // 'm_headerWin' and 'm_mainWin'.
5446 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5447 // behaviour wxListCtrl has under wxMSW.
5449 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5453 // The easy case, no rectangle specified.
5455 m_headerWin->Refresh(eraseBackground);
5458 m_mainWin->Refresh(eraseBackground);
5462 // Refresh the header window
5465 wxRect rectHeader = m_headerWin->GetRect();
5466 rectHeader.Intersect(*rect);
5467 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5470 m_headerWin->GetPosition(&x, &y);
5471 rectHeader.Offset(-x, -y);
5472 m_headerWin->Refresh(eraseBackground, &rectHeader);
5476 // Refresh the main window
5479 wxRect rectMain = m_mainWin->GetRect();
5480 rectMain.Intersect(*rect);
5481 if (rectMain.GetWidth() && rectMain.GetHeight())
5484 m_mainWin->GetPosition(&x, &y);
5485 rectMain.Offset(-x, -y);
5486 m_mainWin->Refresh(eraseBackground, &rectMain);
5492 void wxGenericListCtrl::Update()
5496 if ( m_mainWin->m_dirty )
5497 m_mainWin->RecalculatePositions();
5499 m_mainWin->Update();
5503 m_headerWin->Update();
5506 #endif // wxUSE_LISTCTRL