+ case WXK_RETURN:
+ EndEdit( End_Accept );
+ break;
+
+ case WXK_ESCAPE:
+ EndEdit( End_Discard );
+ break;
+
+ default:
+ return false;
+ }
+
+ return true;
+}
+
+void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
+{
+ if (m_aboutToFinish)
+ {
+ // auto-grow the textctrl:
+ wxSize parentSize = m_owner->GetSize();
+ wxPoint myPos = m_text->GetPosition();
+ wxSize mySize = m_text->GetSize();
+ int sx, sy;
+ m_text->GetTextExtent(m_text->GetValue() + wxT("MM"), &sx, &sy);
+ if (myPos.x + sx > parentSize.x)
+ sx = parentSize.x - myPos.x;
+ if (mySize.x > sx)
+ sx = mySize.x;
+ m_text->SetSize(sx, wxDefaultCoord);
+ }
+
+ event.Skip();
+}
+
+void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
+{
+ if ( !m_aboutToFinish )
+ {
+ if ( !AcceptChanges() )
+ m_owner->OnRenameCancelled( m_itemEdited );
+
+ Finish( false );
+ }
+
+ // We must let the native text control handle focus
+ event.Skip();
+}
+
+//-----------------------------------------------------------------------------
+// wxListMainWindow
+//-----------------------------------------------------------------------------
+
+BEGIN_EVENT_TABLE(wxListMainWindow, wxWindow)
+ EVT_PAINT (wxListMainWindow::OnPaint)
+ EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
+ EVT_CHAR_HOOK (wxListMainWindow::OnCharHook)
+ EVT_CHAR (wxListMainWindow::OnChar)
+ EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
+ EVT_KEY_UP (wxListMainWindow::OnKeyUp)
+ EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
+ EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
+ EVT_SCROLLWIN (wxListMainWindow::OnScroll)
+ EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus)
+END_EVENT_TABLE()
+
+void wxListMainWindow::Init()
+{
+ m_dirty = true;
+ m_countVirt = 0;
+ m_lineFrom =
+ m_lineTo = (size_t)-1;
+ m_linesPerPage = 0;
+
+ m_headerWidth =
+ m_lineHeight = 0;
+
+ m_small_image_list = NULL;
+ m_normal_image_list = NULL;
+
+ m_small_spacing = 30;
+ m_normal_spacing = 40;
+
+ m_hasFocus = false;
+ m_dragCount = 0;
+ m_isCreated = false;
+
+ m_lastOnSame = false;
+ m_renameTimer = new wxListRenameTimer( this );
+ m_textctrlWrapper = NULL;
+
+ m_current =
+ m_lineLastClicked =
+ m_lineSelectSingleOnUp =
+ m_lineBeforeLastClicked = (size_t)-1;
+}
+
+wxListMainWindow::wxListMainWindow()
+{
+ Init();
+
+ m_highlightBrush =
+ m_highlightUnfocusedBrush = NULL;
+}
+
+wxListMainWindow::wxListMainWindow( wxWindow *parent,
+ wxWindowID id,
+ const wxPoint& pos,
+ const wxSize& size )
+ : wxWindow( parent, id, pos, size,
+ wxWANTS_CHARS | wxBORDER_NONE )
+{
+ Init();
+
+ m_highlightBrush = new wxBrush
+ (
+ wxSystemSettings::GetColour
+ (
+ wxSYS_COLOUR_HIGHLIGHT
+ ),
+ wxBRUSHSTYLE_SOLID
+ );
+
+ m_highlightUnfocusedBrush = new wxBrush
+ (
+ wxSystemSettings::GetColour
+ (
+ wxSYS_COLOUR_BTNSHADOW
+ ),
+ wxBRUSHSTYLE_SOLID
+ );
+
+ wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
+ SetOwnForegroundColour( attr.colFg );
+ SetOwnBackgroundColour( attr.colBg );
+ if (!m_hasFont)
+ SetOwnFont( attr.font );
+}
+
+wxListMainWindow::~wxListMainWindow()
+{
+ if ( m_textctrlWrapper )
+ m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Destroy);
+
+ DoDeleteAllItems();
+ WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
+ WX_CLEAR_ARRAY(m_aColWidths);
+
+ delete m_highlightBrush;
+ delete m_highlightUnfocusedBrush;
+ delete m_renameTimer;
+}
+
+void wxListMainWindow::SetReportView(bool inReportView)
+{
+ const size_t count = m_lines.size();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ m_lines[n].SetReportView(inReportView);
+ }
+}
+
+void wxListMainWindow::CacheLineData(size_t line)
+{
+ wxGenericListCtrl *listctrl = GetListCtrl();
+
+ wxListLineData *ld = GetDummyLine();
+
+ size_t countCol = GetColumnCount();
+ for ( size_t col = 0; col < countCol; col++ )
+ {
+ ld->SetText(col, listctrl->OnGetItemText(line, col));
+ ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
+ }
+
+ ld->SetAttr(listctrl->OnGetItemAttr(line));
+}
+
+wxListLineData *wxListMainWindow::GetDummyLine() const
+{
+ wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
+ wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
+
+ wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
+
+ // we need to recreate the dummy line if the number of columns in the
+ // control changed as it would have the incorrect number of fields
+ // otherwise
+ if ( !m_lines.IsEmpty() &&
+ m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
+ {
+ self->m_lines.Clear();
+ }
+
+ if ( m_lines.IsEmpty() )
+ {
+ wxListLineData *line = new wxListLineData(self);
+ self->m_lines.Add(line);
+
+ // don't waste extra memory -- there never going to be anything
+ // else/more in this array
+ self->m_lines.Shrink();
+ }
+
+ return &m_lines[0];
+}
+
+// ----------------------------------------------------------------------------
+// line geometry (report mode only)
+// ----------------------------------------------------------------------------
+
+wxCoord wxListMainWindow::GetLineHeight() const
+{
+ // we cache the line height as calling GetTextExtent() is slow
+ if ( !m_lineHeight )
+ {
+ wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
+
+ wxClientDC dc( self );
+ dc.SetFont( GetFont() );
+
+ wxCoord y;
+ dc.GetTextExtent(wxT("H"), NULL, &y);
+
+ if ( m_small_image_list && m_small_image_list->GetImageCount() )
+ {
+ int iw = 0, ih = 0;
+ m_small_image_list->GetSize(0, iw, ih);
+ y = wxMax(y, ih);
+ }
+
+ y += EXTRA_HEIGHT;
+ self->m_lineHeight = y + LINE_SPACING;
+ }
+
+ return m_lineHeight;
+}
+
+wxCoord wxListMainWindow::GetLineY(size_t line) const
+{
+ wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
+
+ return LINE_SPACING + line * GetLineHeight();
+}
+
+wxRect wxListMainWindow::GetLineRect(size_t line) const
+{
+ if ( !InReportView() )
+ return GetLine(line)->m_gi->m_rectAll;
+
+ wxRect rect;
+ rect.x = HEADER_OFFSET_X;
+ rect.y = GetLineY(line);
+ rect.width = GetHeaderWidth();
+ rect.height = GetLineHeight();
+
+ return rect;
+}
+
+wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
+{
+ if ( !InReportView() )
+ return GetLine(line)->m_gi->m_rectLabel;
+
+ int image_x = 0;
+ wxListLineData *data = GetLine(line);
+ wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
+ if (node)
+ {
+ wxListItemData *item = node->GetData();
+ if ( item->HasImage() )
+ {
+ int ix, iy;
+ GetImageSize( item->GetImage(), ix, iy );
+ image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
+ }
+ }
+
+ wxRect rect;
+ rect.x = image_x + HEADER_OFFSET_X;
+ rect.y = GetLineY(line);
+ rect.width = GetColumnWidth(0) - image_x;
+ rect.height = GetLineHeight();
+
+ return rect;
+}
+
+wxRect wxListMainWindow::GetLineIconRect(size_t line) const
+{
+ if ( !InReportView() )
+ return GetLine(line)->m_gi->m_rectIcon;
+
+ wxListLineData *ld = GetLine(line);
+ wxASSERT_MSG( ld->HasImage(), wxT("should have an image") );
+
+ wxRect rect;
+ rect.x = HEADER_OFFSET_X;
+ rect.y = GetLineY(line);
+ GetImageSize(ld->GetImage(), rect.width, rect.height);
+
+ return rect;
+}
+
+wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
+{
+ return InReportView() ? GetLineRect(line)
+ : GetLine(line)->m_gi->m_rectHighlight;
+}
+
+long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
+{
+ wxASSERT_MSG( line < GetItemCount(), wxT("invalid line in HitTestLine") );
+
+ wxListLineData *ld = GetLine(line);
+
+ if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
+ return wxLIST_HITTEST_ONITEMICON;
+
+ // VS: Testing for "ld->HasText() || InReportView()" instead of
+ // "ld->HasText()" is needed to make empty lines in report view
+ // possible
+ if ( ld->HasText() || InReportView() )
+ {
+ wxRect rect = InReportView() ? GetLineRect(line)
+ : GetLineLabelRect(line);
+
+ if ( rect.Contains(x, y) )
+ return wxLIST_HITTEST_ONITEMLABEL;
+ }
+
+ return 0;
+}
+
+// ----------------------------------------------------------------------------
+// highlight (selection) handling
+// ----------------------------------------------------------------------------
+
+bool wxListMainWindow::IsHighlighted(size_t line) const
+{
+ if ( IsVirtual() )
+ {
+ return m_selStore.IsSelected(line);
+ }
+ else // !virtual
+ {
+ wxListLineData *ld = GetLine(line);
+ wxCHECK_MSG( ld, false, wxT("invalid index in IsHighlighted") );
+
+ return ld->IsHighlighted();
+ }
+}
+
+void wxListMainWindow::HighlightLines( size_t lineFrom,
+ size_t lineTo,
+ bool highlight )
+{
+ if ( IsVirtual() )
+ {
+ wxArrayInt linesChanged;
+ if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
+ &linesChanged) )
+ {
+ // meny items changed state, refresh everything
+ RefreshLines(lineFrom, lineTo);
+ }
+ else // only a few items changed state, refresh only them
+ {
+ size_t count = linesChanged.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ RefreshLine(linesChanged[n]);
+ }
+ }
+ }
+ else // iterate over all items in non report view
+ {
+ for ( size_t line = lineFrom; line <= lineTo; line++ )
+ {
+ if ( HighlightLine(line, highlight) )
+ RefreshLine(line);
+ }
+ }
+}
+
+bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
+{
+ bool changed;
+
+ if ( IsVirtual() )
+ {
+ changed = m_selStore.SelectItem(line, highlight);
+ }
+ else // !virtual
+ {
+ wxListLineData *ld = GetLine(line);
+ wxCHECK_MSG( ld, false, wxT("invalid index in HighlightLine") );
+
+ changed = ld->Highlight(highlight);
+ }
+
+ if ( changed )
+ {
+ SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
+ : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
+ }
+
+ return changed;
+}
+
+void wxListMainWindow::RefreshLine( size_t line )
+{
+ if ( InReportView() )
+ {
+ size_t visibleFrom, visibleTo;
+ GetVisibleLinesRange(&visibleFrom, &visibleTo);
+
+ if ( line < visibleFrom || line > visibleTo )
+ return;
+ }
+
+ wxRect rect = GetLineRect(line);
+
+ GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
+ RefreshRect( rect );
+}
+
+void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
+{
+ // we suppose that they are ordered by caller
+ wxASSERT_MSG( lineFrom <= lineTo, wxT("indices in disorder") );
+
+ wxASSERT_MSG( lineTo < GetItemCount(), wxT("invalid line range") );
+
+ if ( InReportView() )
+ {
+ size_t visibleFrom, visibleTo;
+ GetVisibleLinesRange(&visibleFrom, &visibleTo);
+
+ if ( lineFrom < visibleFrom )
+ lineFrom = visibleFrom;
+ if ( lineTo > visibleTo )
+ lineTo = visibleTo;
+
+ wxRect rect;
+ rect.x = 0;
+ rect.y = GetLineY(lineFrom);
+ rect.width = GetClientSize().x;
+ rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
+
+ GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
+ RefreshRect( rect );
+ }
+ else // !report
+ {
+ // TODO: this should be optimized...
+ for ( size_t line = lineFrom; line <= lineTo; line++ )
+ {
+ RefreshLine(line);
+ }
+ }
+}
+
+void wxListMainWindow::RefreshAfter( size_t lineFrom )
+{
+ if ( InReportView() )
+ {
+ size_t visibleFrom, visibleTo;
+ GetVisibleLinesRange(&visibleFrom, &visibleTo);
+
+ if ( lineFrom < visibleFrom )
+ lineFrom = visibleFrom;
+ else if ( lineFrom > visibleTo )
+ return;
+
+ wxRect rect;
+ rect.x = 0;
+ rect.y = GetLineY(lineFrom);
+ GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
+
+ wxSize size = GetClientSize();
+ rect.width = size.x;
+
+ // refresh till the bottom of the window
+ rect.height = size.y - rect.y;
+
+ RefreshRect( rect );
+ }
+ else // !report
+ {
+ // TODO: how to do it more efficiently?
+ m_dirty = true;
+ }
+}
+
+void wxListMainWindow::RefreshSelected()
+{
+ if ( IsEmpty() )
+ return;
+
+ size_t from, to;
+ if ( InReportView() )
+ {
+ GetVisibleLinesRange(&from, &to);
+ }
+ else // !virtual
+ {
+ from = 0;
+ to = GetItemCount() - 1;
+ }
+
+ if ( HasCurrent() && m_current >= from && m_current <= to )
+ RefreshLine(m_current);
+
+ for ( size_t line = from; line <= to; line++ )
+ {
+ // NB: the test works as expected even if m_current == -1
+ if ( line != m_current && IsHighlighted(line) )
+ RefreshLine(line);
+ }
+}
+
+void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
+{
+ // Note: a wxPaintDC must be constructed even if no drawing is
+ // done (a Windows requirement).
+ wxPaintDC dc( this );
+
+ if ( IsEmpty() )
+ {
+ // nothing to draw or not the moment to draw it
+ return;
+ }
+
+ if ( m_dirty )
+ RecalculatePositions( false );
+
+ GetListCtrl()->PrepareDC( dc );
+
+ int dev_x, dev_y;
+ GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
+
+ dc.SetFont( GetFont() );
+
+ if ( InReportView() )
+ {
+ int lineHeight = GetLineHeight();
+
+ size_t visibleFrom, visibleTo;
+ GetVisibleLinesRange(&visibleFrom, &visibleTo);
+
+ wxRect rectLine;
+ int xOrig = dc.LogicalToDeviceX( 0 );
+ int yOrig = dc.LogicalToDeviceY( 0 );
+
+ // tell the caller cache to cache the data
+ if ( IsVirtual() )
+ {
+ wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
+ GetParent()->GetId());
+ evCache.SetEventObject( GetParent() );
+ evCache.m_oldItemIndex = visibleFrom;
+ evCache.m_item.m_itemId =
+ evCache.m_itemIndex = visibleTo;
+ GetParent()->GetEventHandler()->ProcessEvent( evCache );
+ }
+
+ for ( size_t line = visibleFrom; line <= visibleTo; line++ )
+ {
+ rectLine = GetLineRect(line);
+
+
+ if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
+ rectLine.width, rectLine.height) )
+ {
+ // don't redraw unaffected lines to avoid flicker
+ continue;
+ }
+
+ GetLine(line)->DrawInReportMode( &dc,
+ rectLine,
+ GetLineHighlightRect(line),
+ IsHighlighted(line),
+ line == m_current );
+ }
+
+ if ( HasFlag(wxLC_HRULES) )
+ {
+ wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
+ wxSize clientSize = GetClientSize();
+
+ size_t i = visibleFrom;
+ if (i == 0) i = 1; // Don't draw the first one
+ for ( ; i <= visibleTo; i++ )
+ {
+ dc.SetPen(pen);
+ dc.SetBrush( *wxTRANSPARENT_BRUSH );
+ dc.DrawLine(0 - dev_x, i * lineHeight,
+ clientSize.x - dev_x, i * lineHeight);
+ }
+
+ // Draw last horizontal rule
+ if ( visibleTo == GetItemCount() - 1 )
+ {
+ dc.SetPen( pen );
+ dc.SetBrush( *wxTRANSPARENT_BRUSH );
+ dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
+ clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
+ }
+ }
+
+ // Draw vertical rules if required
+ if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
+ {
+ wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
+ wxRect firstItemRect, lastItemRect;
+
+ GetItemRect(visibleFrom, firstItemRect);
+ GetItemRect(visibleTo, lastItemRect);
+ int x = firstItemRect.GetX();
+ dc.SetPen(pen);
+ dc.SetBrush(* wxTRANSPARENT_BRUSH);
+
+ for (int col = 0; col < GetColumnCount(); col++)
+ {
+ int colWidth = GetColumnWidth(col);
+ x += colWidth;
+ int x_pos = x - dev_x;
+ if (col < GetColumnCount()-1) x_pos -= 2;
+ dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
+ x_pos, lastItemRect.GetBottom() + 1 - dev_y);
+ }
+ }
+ }
+ else // !report
+ {
+ size_t count = GetItemCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ GetLine(i)->Draw( &dc, i == m_current );
+ }
+ }
+
+ // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
+ // rectangle somehow and so leaves traces when the item is not selected any
+ // more, see #12229.
+#ifndef __WXMAC__
+ if ( HasCurrent() )
+ {
+ int flags = 0;
+ if ( IsHighlighted(m_current) )
+ flags |= wxCONTROL_SELECTED;
+
+ wxRendererNative::Get().
+ DrawFocusRect(this, dc, GetLineHighlightRect(m_current), flags);
+ }
+#endif // !__WXMAC__
+}
+
+void wxListMainWindow::HighlightAll( bool on )
+{
+ if ( IsSingleSel() )
+ {
+ wxASSERT_MSG( !on, wxT("can't do this in a single selection control") );
+
+ // we just have one item to turn off
+ if ( HasCurrent() && IsHighlighted(m_current) )
+ {
+ HighlightLine(m_current, false);
+ RefreshLine(m_current);
+ }
+ }
+ else // multi selection
+ {
+ if ( !IsEmpty() )
+ HighlightLines(0, GetItemCount() - 1, on);
+ }
+}
+
+void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
+{
+ // Do nothing here. This prevents the default handler in wxScrolledWindow
+ // from needlessly scrolling the window when the edit control is
+ // dismissed. See ticket #9563.
+}
+
+void wxListMainWindow::SendNotify( size_t line,
+ wxEventType command,
+ const wxPoint& point )
+{
+ wxListEvent le( command, GetParent()->GetId() );
+ le.SetEventObject( GetParent() );
+
+ le.m_item.m_itemId =
+ le.m_itemIndex = line;
+
+ // set only for events which have position
+ if ( point != wxDefaultPosition )
+ le.m_pointDrag = point;
+
+ // don't try to get the line info for virtual list controls: the main
+ // program has it anyhow and if we did it would result in accessing all
+ // the lines, even those which are not visible now and this is precisely
+ // what we're trying to avoid
+ if ( !IsVirtual() )
+ {
+ if ( line != (size_t)-1 )
+ {
+ GetLine(line)->GetItem( 0, le.m_item );
+ }
+ //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
+ }
+ //else: there may be no more such item
+
+ GetParent()->GetEventHandler()->ProcessEvent( le );
+}
+
+void wxListMainWindow::ChangeCurrent(size_t current)
+{
+ m_current = current;
+
+ // as the current item changed, we shouldn't start editing it when the
+ // "slow click" timer expires as the click happened on another item
+ if ( m_renameTimer->IsRunning() )
+ m_renameTimer->Stop();
+
+ SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
+}
+
+wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
+{
+ wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
+ wxT("wrong index in wxGenericListCtrl::EditLabel()") );
+
+ wxASSERT_MSG( textControlClass->IsKindOf(wxCLASSINFO(wxTextCtrl)),
+ wxT("EditLabel() needs a text control") );
+
+ size_t itemEdit = (size_t)item;
+
+ wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
+ le.SetEventObject( GetParent() );
+ le.m_item.m_itemId =
+ le.m_itemIndex = item;
+ wxListLineData *data = GetLine(itemEdit);
+ wxCHECK_MSG( data, NULL, wxT("invalid index in EditLabel()") );
+ data->GetItem( 0, le.m_item );
+
+ if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
+ {
+ // vetoed by user code
+ return NULL;
+ }
+
+ // We have to call this here because the label in question might just have
+ // been added and no screen update taken place.
+ if ( m_dirty )
+ {
+ // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
+ // so that no pending events may change the item count (see below)
+ // IMPORTANT: needs to be tested!
+ wxSafeYield();
+
+ // Pending events dispatched by wxSafeYield might have changed the item
+ // count
+ if ( (size_t)item >= GetItemCount() )
+ return NULL;
+ }
+
+ wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
+ m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
+ return m_textctrlWrapper->GetText();
+}
+
+void wxListMainWindow::OnRenameTimer()
+{
+ wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
+
+ EditLabel( m_current );
+}
+
+bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
+{
+ wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
+ le.SetEventObject( GetParent() );
+ le.m_item.m_itemId =
+ le.m_itemIndex = itemEdit;
+
+ wxListLineData *data = GetLine(itemEdit);
+
+ wxCHECK_MSG( data, false, wxT("invalid index in OnRenameAccept()") );
+
+ data->GetItem( 0, le.m_item );
+ le.m_item.m_text = value;
+ return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
+ le.IsAllowed();
+}
+
+void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
+{
+ // let owner know that the edit was cancelled
+ wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
+
+ le.SetEditCanceled(true);
+
+ le.SetEventObject( GetParent() );
+ le.m_item.m_itemId =
+ le.m_itemIndex = itemEdit;
+
+ wxListLineData *data = GetLine(itemEdit);
+ wxCHECK_RET( data, wxT("invalid index in OnRenameCancelled()") );
+
+ data->GetItem( 0, le.m_item );
+ GetEventHandler()->ProcessEvent( le );
+}
+
+void wxListMainWindow::OnMouse( wxMouseEvent &event )
+{
+#ifdef __WXMAC__
+ // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
+ // shutdown the edit control when the mouse is clicked elsewhere on the
+ // listctrl because the order of events is different (or something like
+ // that), so explicitly end the edit if it is active.
+ if ( event.LeftDown() && m_textctrlWrapper )
+ m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Accept);
+#endif // __WXMAC__
+
+ if ( event.LeftDown() )
+ SetFocus();
+
+ event.SetEventObject( GetParent() );
+ if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
+ return;
+
+ if (event.GetEventType() == wxEVT_MOUSEWHEEL)
+ {
+ // let the base class handle mouse wheel events.
+ event.Skip();
+ return;
+ }
+
+ if ( !HasCurrent() || IsEmpty() )
+ {
+ if (event.RightDown())
+ {
+ SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
+
+ wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
+ GetParent()->GetId(),
+ ClientToScreen(event.GetPosition()));
+ evtCtx.SetEventObject(GetParent());
+ GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
+ }
+ return;
+ }
+
+ if (m_dirty)
+ return;
+
+ if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
+ event.ButtonDClick()) )
+ return;
+
+ int x = event.GetX();
+ int y = event.GetY();
+ GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
+
+ // where did we hit it (if we did)?
+ long hitResult = 0;
+
+ size_t count = GetItemCount(),
+ current;
+
+ if ( InReportView() )
+ {
+ current = y / GetLineHeight();
+ if ( current < count )
+ hitResult = HitTestLine(current, x, y);
+ }
+ else // !report
+ {
+ // TODO: optimize it too! this is less simple than for report view but
+ // enumerating all items is still not a way to do it!!
+ for ( current = 0; current < count; current++ )
+ {
+ hitResult = HitTestLine(current, x, y);
+ if ( hitResult )
+ break;
+ }
+ }
+
+ // Update drag events counter first as we must do it even if the mouse is
+ // not on any item right now as we must keep count in case we started
+ // dragging from the empty control area but continued to do it over a valid
+ // item -- in this situation we must not start dragging this item.
+ if (event.Dragging())
+ m_dragCount++;
+ else
+ m_dragCount = 0;
+
+ // The only mouse event that can be generated without any valid item is
+ // wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK as it can be useful to have a global
+ // popup menu for the list control itself which should be shown even when
+ // the user clicks outside of any item.
+ if ( !hitResult )
+ {
+ // outside of any item
+ if (event.RightDown())
+ {
+ SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
+
+ wxContextMenuEvent evtCtx(
+ wxEVT_CONTEXT_MENU,
+ GetParent()->GetId(),
+ ClientToScreen(event.GetPosition()));
+ evtCtx.SetEventObject(GetParent());
+ GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
+ }
+ else
+ {
+ // reset the selection and bail out
+ HighlightAll(false);
+ }
+
+ return;
+ }
+
+ if ( event.Dragging() )
+ {
+ if (m_dragCount == 1)
+ {
+ // we have to report the raw, physical coords as we want to be
+ // able to call HitTest(event.m_pointDrag) from the user code to
+ // get the item being dragged
+ m_dragStart = event.GetPosition();
+ }
+
+ if (m_dragCount != 3)
+ return;
+
+ int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
+ : wxEVT_COMMAND_LIST_BEGIN_DRAG;
+
+ SendNotify( m_lineLastClicked, command, m_dragStart );
+
+ return;
+ }
+
+ bool forceClick = false;
+ if (event.ButtonDClick())
+ {
+ if ( m_renameTimer->IsRunning() )
+ m_renameTimer->Stop();
+
+ m_lastOnSame = false;
+
+ if ( current == m_lineLastClicked )
+ {
+ SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
+
+ return;
+ }
+ else
+ {
+ // The first click was on another item, so don't interpret this as
+ // a double click, but as a simple click instead
+ forceClick = true;
+ }
+ }
+
+ if (event.LeftUp())
+ {
+ if (m_lineSelectSingleOnUp != (size_t)-1)
+ {
+ // select single line
+ HighlightAll( false );
+ ReverseHighlight(m_lineSelectSingleOnUp);
+ }
+
+ if (m_lastOnSame)
+ {
+ if ((current == m_current) &&
+ (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
+ HasFlag(wxLC_EDIT_LABELS) )
+ {
+ if ( !InReportView() ||
+ GetLineLabelRect(current).Contains(x, y) )
+ {
+ int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC);
+ m_renameTimer->Start(dclick > 0 ? dclick : 250, true);
+ }
+ }
+ }
+
+ m_lastOnSame = false;
+ m_lineSelectSingleOnUp = (size_t)-1;
+ }
+ else
+ {
+ // This is necessary, because after a DnD operation in
+ // from and to ourself, the up event is swallowed by the
+ // DnD code. So on next non-up event (which means here and
+ // now) m_lineSelectSingleOnUp should be reset.
+ m_lineSelectSingleOnUp = (size_t)-1;
+ }
+ if (event.RightDown())
+ {
+ m_lineBeforeLastClicked = m_lineLastClicked;
+ m_lineLastClicked = current;
+
+ // If the item is already selected, do not update the selection.
+ // Multi-selections should not be cleared if a selected item is clicked.
+ if (!IsHighlighted(current))
+ {
+ HighlightAll(false);
+ ChangeCurrent(current);
+ ReverseHighlight(m_current);
+ }
+
+ SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
+
+ // Allow generation of context menu event
+ event.Skip();
+ }
+ else if (event.MiddleDown())
+ {
+ SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
+ }
+ else if ( event.LeftDown() || forceClick )
+ {
+ m_lineBeforeLastClicked = m_lineLastClicked;
+ m_lineLastClicked = current;
+
+ size_t oldCurrent = m_current;
+ bool oldWasSelected = IsHighlighted(m_current);
+
+ bool cmdModifierDown = event.CmdDown();
+ if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
+ {
+ if ( IsSingleSel() || !IsHighlighted(current) )
+ {
+ HighlightAll( false );
+
+ ChangeCurrent(current);
+
+ ReverseHighlight(m_current);
+ }
+ else // multi sel & current is highlighted & no mod keys
+ {
+ m_lineSelectSingleOnUp = current;
+ ChangeCurrent(current); // change focus
+ }
+ }
+ else // multi sel & either ctrl or shift is down
+ {
+ if (cmdModifierDown)
+ {
+ ChangeCurrent(current);
+
+ ReverseHighlight(m_current);
+ }
+ else if (event.ShiftDown())
+ {
+ ChangeCurrent(current);
+
+ size_t lineFrom = oldCurrent,
+ lineTo = current;
+
+ if ( lineTo < lineFrom )
+ {
+ lineTo = lineFrom;
+ lineFrom = m_current;
+ }
+
+ HighlightLines(lineFrom, lineTo);
+ }
+ else // !ctrl, !shift
+ {
+ // test in the enclosing if should make it impossible
+ wxFAIL_MSG( wxT("how did we get here?") );
+ }
+ }
+
+ if (m_current != oldCurrent)
+ RefreshLine( oldCurrent );
+
+ // forceClick is only set if the previous click was on another item
+ m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
+ }
+}
+
+void wxListMainWindow::MoveToItem(size_t item)
+{
+ if ( item == (size_t)-1 )
+ return;
+
+ wxRect rect = GetLineRect(item);
+
+ int client_w, client_h;
+ GetClientSize( &client_w, &client_h );
+
+ const int hLine = GetLineHeight();
+
+ int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL );
+ int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL );
+
+ if ( InReportView() )
+ {
+ // the next we need the range of lines shown it might be different,
+ // so recalculate it
+ ResetVisibleLinesRange();
+
+ if (rect.y < view_y)
+ GetListCtrl()->Scroll( -1, rect.y / hLine );
+ if (rect.y + rect.height + 5 > view_y + client_h)
+ GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
+
+#ifdef __WXMAC__
+ // At least on Mac the visible lines value will get reset inside of
+ // Scroll *before* it actually scrolls the window because of the
+ // Update() that happens there, so it will still have the wrong value.
+ // So let's reset it again and wait for it to be recalculated in the
+ // next paint event. I would expect this problem to show up in wxGTK
+ // too but couldn't duplicate it there. Perhaps the order of events
+ // is different... --Robin
+ ResetVisibleLinesRange();
+#endif
+ }
+ else // !report
+ {
+ int sx = -1,
+ sy = -1;
+
+ if (rect.x-view_x < 5)
+ sx = (rect.x - 5) / SCROLL_UNIT_X;
+ if (rect.x + rect.width - 5 > view_x + client_w)
+ sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
+
+ if (rect.y-view_y < 5)
+ sy = (rect.y - 5) / hLine;
+ if (rect.y + rect.height - 5 > view_y + client_h)
+ sy = (rect.y + rect.height - client_h + hLine) / hLine;
+
+ GetListCtrl()->Scroll(sx, sy);
+ }
+}
+
+bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
+{
+ if ( !InReportView() )
+ {
+ // TODO: this should work in all views but is not implemented now
+ return false;
+ }
+
+ size_t top, bottom;
+ GetVisibleLinesRange(&top, &bottom);
+
+ if ( bottom == (size_t)-1 )
+ return 0;
+
+ ResetVisibleLinesRange();
+
+ int hLine = GetLineHeight();
+
+ GetListCtrl()->Scroll(-1, top + dy / hLine);
+
+#ifdef __WXMAC__
+ // see comment in MoveToItem() for why we do this
+ ResetVisibleLinesRange();
+#endif
+
+ return true;
+}
+
+// ----------------------------------------------------------------------------
+// keyboard handling
+// ----------------------------------------------------------------------------
+
+void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
+{
+ wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
+ wxT("invalid item index in OnArrowChar()") );
+
+ size_t oldCurrent = m_current;
+
+ // in single selection we just ignore Shift as we can't select several
+ // items anyhow
+ if ( event.ShiftDown() && !IsSingleSel() )
+ {
+ ChangeCurrent(newCurrent);
+
+ // refresh the old focus to remove it
+ RefreshLine( oldCurrent );
+
+ // select all the items between the old and the new one
+ if ( oldCurrent > newCurrent )
+ {
+ newCurrent = oldCurrent;
+ oldCurrent = m_current;
+ }
+
+ HighlightLines(oldCurrent, newCurrent);
+ }
+ else // !shift
+ {
+ // all previously selected items are unselected unless ctrl is held
+ // in a multiselection control
+ if ( !event.ControlDown() || IsSingleSel() )
+ HighlightAll(false);
+
+ ChangeCurrent(newCurrent);
+
+ // refresh the old focus to remove it
+ RefreshLine( oldCurrent );
+
+ // in single selection mode we must always have a selected item
+ if ( !event.ControlDown() || IsSingleSel() )
+ HighlightLine( m_current, true );
+ }
+
+ RefreshLine( m_current );
+
+ MoveToFocus();
+}
+
+void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
+{
+ wxWindow *parent = GetParent();
+
+ // propagate the key event upwards
+ wxKeyEvent ke(event);
+ ke.SetEventObject( parent );
+ if (parent->GetEventHandler()->ProcessEvent( ke ))
+ return;
+
+ // send a list event
+ wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, parent->GetId() );
+ le.m_item.m_itemId =
+ le.m_itemIndex = m_current;
+ if (HasCurrent())
+ GetLine(m_current)->GetItem( 0, le.m_item );
+ le.m_code = event.GetKeyCode();
+ le.SetEventObject( parent );
+ if (parent->GetEventHandler()->ProcessEvent( le ))
+ return;
+
+ event.Skip();
+}
+
+void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
+{
+ wxWindow *parent = GetParent();
+
+ // propagate the key event upwards
+ wxKeyEvent ke(event);
+ if (parent->GetEventHandler()->ProcessEvent( ke ))
+ return;
+
+ event.Skip();
+}
+
+void wxListMainWindow::OnCharHook( wxKeyEvent &event )
+{
+ if ( m_textctrlWrapper )
+ {
+ // When an in-place editor is active we should ensure that it always
+ // gets the key events that are special to it.
+ if ( m_textctrlWrapper->CheckForEndEditKey(event) )
+ {
+ // Skip the call to wxEvent::Skip() below.
+ return;
+ }
+ }
+
+ event.Skip();
+}
+
+void wxListMainWindow::OnChar( wxKeyEvent &event )
+{
+ wxWindow *parent = GetParent();
+
+ // propagate the char event upwards
+ wxKeyEvent ke(event);
+ ke.SetEventObject( parent );
+ if (parent->GetEventHandler()->ProcessEvent( ke ))
+ return;
+
+ if ( HandleAsNavigationKey(event) )
+ return;
+
+ // no item -> nothing to do
+ if (!HasCurrent())
+ {
+ event.Skip();
+ return;
+ }
+
+ // don't use m_linesPerPage directly as it might not be computed yet
+ const int pageSize = GetCountPerPage();
+ wxCHECK_RET( pageSize, wxT("should have non zero page size") );
+
+ if (GetLayoutDirection() == wxLayout_RightToLeft)
+ {
+ if (event.GetKeyCode() == WXK_RIGHT)
+ event.m_keyCode = WXK_LEFT;
+ else if (event.GetKeyCode() == WXK_LEFT)
+ event.m_keyCode = WXK_RIGHT;
+ }
+
+ switch ( event.GetKeyCode() )
+ {
+ case WXK_UP:
+ if ( m_current > 0 )
+ OnArrowChar( m_current - 1, event );
+ break;
+
+ case WXK_DOWN:
+ if ( m_current < (size_t)GetItemCount() - 1 )
+ OnArrowChar( m_current + 1, event );
+ break;
+
+ case WXK_END:
+ if (!IsEmpty())
+ OnArrowChar( GetItemCount() - 1, event );
+ break;
+
+ case WXK_HOME:
+ if (!IsEmpty())
+ OnArrowChar( 0, event );
+ break;
+
+ case WXK_PAGEUP:
+ {
+ int steps = InReportView() ? pageSize - 1
+ : m_current % pageSize;
+
+ int index = m_current - steps;
+ if (index < 0)
+ index = 0;
+
+ OnArrowChar( index, event );
+ }
+ break;
+
+ case WXK_PAGEDOWN:
+ {
+ int steps = InReportView()
+ ? pageSize - 1
+ : pageSize - (m_current % pageSize) - 1;
+
+ size_t index = m_current + steps;
+ size_t count = GetItemCount();
+ if ( index >= count )
+ index = count - 1;
+
+ OnArrowChar( index, event );
+ }
+ break;
+
+ case WXK_LEFT:
+ if ( !InReportView() )
+ {
+ int index = m_current - pageSize;
+ if (index < 0)
+ index = 0;
+
+ OnArrowChar( index, event );
+ }
+ break;
+
+ case WXK_RIGHT:
+ if ( !InReportView() )
+ {
+ size_t index = m_current + pageSize;
+
+ size_t count = GetItemCount();
+ if ( index >= count )
+ index = count - 1;
+
+ OnArrowChar( index, event );
+ }
+ break;
+
+ case WXK_SPACE:
+ if ( IsSingleSel() )
+ {
+ if ( event.ControlDown() )
+ {
+ ReverseHighlight(m_current);
+ }
+ else // normal space press
+ {
+ SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
+ }
+ }
+ else // multiple selection
+ {
+ ReverseHighlight(m_current);
+ }
+ break;
+
+ case WXK_RETURN:
+ case WXK_EXECUTE:
+ SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
+ break;
+
+ default:
+ event.Skip();
+ }
+}
+
+// ----------------------------------------------------------------------------
+// focus handling
+// ----------------------------------------------------------------------------
+
+void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
+{
+ if ( GetParent() )
+ {
+ wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
+ event.SetEventObject( GetParent() );
+ if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
+ return;
+ }
+
+ // wxGTK sends us EVT_SET_FOCUS events even if we had never got
+ // EVT_KILL_FOCUS before which means that we finish by redrawing the items
+ // which are already drawn correctly resulting in horrible flicker - avoid
+ // it
+ if ( !m_hasFocus )
+ {
+ m_hasFocus = true;
+
+ RefreshSelected();
+ }
+}
+
+void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
+{
+ if ( GetParent() )
+ {
+ wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
+ event.SetEventObject( GetParent() );
+ if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
+ return;
+ }
+
+ m_hasFocus = false;
+ RefreshSelected();
+}
+
+void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
+{
+ if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
+ {
+ m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
+ }
+ else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
+ {
+ m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
+ }
+ else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
+ {
+ m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
+ }
+ else if ( InReportView() && (m_small_image_list))
+ {
+ m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
+ }
+}
+
+void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
+{
+ if ( HasFlag(wxLC_ICON) && m_normal_image_list )
+ {
+ m_normal_image_list->GetSize( index, width, height );
+ }
+ else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
+ {
+ m_small_image_list->GetSize( index, width, height );
+ }
+ else if ( HasFlag(wxLC_LIST) && m_small_image_list )
+ {
+ m_small_image_list->GetSize( index, width, height );
+ }
+ else if ( InReportView() && m_small_image_list )
+ {
+ m_small_image_list->GetSize( index, width, height );
+ }
+ else
+ {
+ width =
+ height = 0;
+ }
+}
+
+void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
+{
+ m_dirty = true;
+
+ // calc the spacing from the icon size
+ int width = 0, height = 0;
+
+ if ((imageList) && (imageList->GetImageCount()) )
+ imageList->GetSize(0, width, height);
+
+ if (which == wxIMAGE_LIST_NORMAL)
+ {
+ m_normal_image_list = imageList;
+ m_normal_spacing = width + 8;
+ }
+
+ if (which == wxIMAGE_LIST_SMALL)
+ {
+ m_small_image_list = imageList;
+ m_small_spacing = width + 14;
+ m_lineHeight = 0; // ensure that the line height will be recalc'd
+ }
+}
+
+void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
+{
+ m_dirty = true;
+ if (isSmall)
+ m_small_spacing = spacing;
+ else
+ m_normal_spacing = spacing;
+}
+
+int wxListMainWindow::GetItemSpacing( bool isSmall )
+{
+ return isSmall ? m_small_spacing : m_normal_spacing;
+}
+
+// ----------------------------------------------------------------------------
+// columns
+// ----------------------------------------------------------------------------
+
+int
+wxListMainWindow::ComputeMinHeaderWidth(const wxListHeaderData* column) const
+{
+ wxClientDC dc(const_cast<wxListMainWindow*>(this));
+
+ int width = dc.GetTextExtent(column->GetText()).x + AUTOSIZE_COL_MARGIN;
+
+ width += 2*EXTRA_WIDTH;
+
+ // check for column header's image availability
+ const int image = column->GetImage();
+ if ( image != -1 )
+ {
+ if ( m_small_image_list )
+ {
+ int ix = 0, iy = 0;
+ m_small_image_list->GetSize(image, ix, iy);
+ width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
+ }
+ }
+
+ return width;
+}
+
+void wxListMainWindow::SetColumn( int col, const wxListItem &item )
+{
+ wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
+
+ wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
+
+ wxListHeaderData *column = node->GetData();
+ column->SetItem( item );
+
+ if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
+ column->SetWidth(ComputeMinHeaderWidth(column));
+
+ wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
+ if ( headerWin )
+ headerWin->m_dirty = true;
+
+ m_dirty = true;
+
+ // invalidate it as it has to be recalculated
+ m_headerWidth = 0;
+}
+
+void wxListMainWindow::SetColumnWidth( int col, int width )
+{
+ wxCHECK_RET( col >= 0 && col < GetColumnCount(),
+ wxT("invalid column index") );
+
+ wxCHECK_RET( InReportView(),
+ wxT("SetColumnWidth() can only be called in report mode.") );
+
+ m_dirty = true;
+
+ wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
+ if ( headerWin )
+ headerWin->m_dirty = true;
+
+ wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
+ wxCHECK_RET( node, wxT("no column?") );
+
+ wxListHeaderData *column = node->GetData();
+
+ size_t count = GetItemCount();
+
+ if (width == wxLIST_AUTOSIZE_USEHEADER)
+ {
+ width = ComputeMinHeaderWidth(column);
+ }
+ else if ( width == wxLIST_AUTOSIZE )
+ {
+ width = ComputeMinHeaderWidth(column);
+
+ if ( !IsVirtual() )
+ {
+ wxClientDC dc(this);
+ dc.SetFont( GetFont() );
+
+ int max = AUTOSIZE_COL_MARGIN;
+
+ // if the cached column width isn't valid then recalculate it
+ if (m_aColWidths.Item(col)->bNeedsUpdate)
+ {
+ for (size_t i = 0; i < count; i++)
+ {
+ wxListLineData *line = GetLine( i );
+ wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
+
+ wxCHECK_RET( n, wxT("no subitem?") );
+
+ wxListItemData *itemData = n->GetData();
+ wxListItem item;
+
+ itemData->GetItem(item);
+ int itemWidth = GetItemWidthWithImage(&item);
+ if (itemWidth > max)
+ max = itemWidth;
+ }
+
+ m_aColWidths.Item(col)->bNeedsUpdate = false;
+ m_aColWidths.Item(col)->nMaxWidth = max;
+ }
+
+ max = m_aColWidths.Item(col)->nMaxWidth + AUTOSIZE_COL_MARGIN;
+ if ( width < max )
+ width = max;
+ }
+ }
+
+ column->SetWidth( width );
+
+ // invalidate it as it has to be recalculated
+ m_headerWidth = 0;
+}
+
+int wxListMainWindow::GetHeaderWidth() const
+{
+ if ( !m_headerWidth )
+ {
+ wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
+
+ size_t count = GetColumnCount();
+ for ( size_t col = 0; col < count; col++ )
+ {
+ self->m_headerWidth += GetColumnWidth(col);
+ }
+ }
+
+ return m_headerWidth;
+}
+
+void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
+{
+ wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
+ wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
+
+ wxListHeaderData *column = node->GetData();
+ column->GetItem( item );
+}
+
+int wxListMainWindow::GetColumnWidth( int col ) const
+{
+ wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
+ wxCHECK_MSG( node, 0, wxT("invalid column index") );
+
+ wxListHeaderData *column = node->GetData();
+ return column->GetWidth();
+}
+
+// ----------------------------------------------------------------------------
+// item state
+// ----------------------------------------------------------------------------
+
+void wxListMainWindow::SetItem( wxListItem &item )
+{
+ long id = item.m_itemId;
+ wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
+ wxT("invalid item index in SetItem") );
+
+ if ( !IsVirtual() )
+ {
+ wxListLineData *line = GetLine((size_t)id);
+ line->SetItem( item.m_col, item );
+
+ // Set item state if user wants
+ if ( item.m_mask & wxLIST_MASK_STATE )
+ SetItemState( item.m_itemId, item.m_state, item.m_state );
+
+ if (InReportView())
+ {
+ // update the Max Width Cache if needed
+ int width = GetItemWidthWithImage(&item);
+
+ if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
+ m_aColWidths.Item(item.m_col)->nMaxWidth = width;
+ }
+ }
+
+ // update the item on screen unless we're going to update everything soon
+ // anyhow
+ if ( !m_dirty )
+ {
+ wxRect rectItem;
+ GetItemRect(id, rectItem);
+ RefreshRect(rectItem);
+ }
+}
+
+void wxListMainWindow::SetItemStateAll(long state, long stateMask)
+{
+ if ( IsEmpty() )
+ return;
+
+ // first deal with selection
+ if ( stateMask & wxLIST_STATE_SELECTED )
+ {
+ // set/clear select state
+ if ( IsVirtual() )
+ {
+ // optimized version for virtual listctrl.
+ m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
+ Refresh();
+ }
+ else if ( state & wxLIST_STATE_SELECTED )
+ {
+ const long count = GetItemCount();
+ for( long i = 0; i < count; i++ )
+ {
+ SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
+ }
+
+ }
+ else
+ {
+ // clear for non virtual (somewhat optimized by using GetNextItem())
+ long i = -1;
+ while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
+ {
+ SetItemState( i, 0, wxLIST_STATE_SELECTED );
+ }
+ }
+ }
+
+ if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
+ {
+ // unfocus all: only one item can be focussed, so clearing focus for
+ // all items is simply clearing focus of the focussed item.
+ SetItemState(m_current, state, stateMask);
+ }
+ //(setting focus to all items makes no sense, so it is not handled here.)
+}
+
+void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
+{
+ if ( litem == -1 )
+ {
+ SetItemStateAll(state, stateMask);
+ return;
+ }
+
+ wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
+ wxT("invalid list ctrl item index in SetItem") );
+
+ size_t oldCurrent = m_current;
+ size_t item = (size_t)litem; // safe because of the check above
+
+ // do we need to change the focus?
+ if ( stateMask & wxLIST_STATE_FOCUSED )
+ {
+ if ( state & wxLIST_STATE_FOCUSED )
+ {
+ // don't do anything if this item is already focused
+ if ( item != m_current )
+ {
+ ChangeCurrent(item);
+
+ if ( oldCurrent != (size_t)-1 )
+ {
+ if ( IsSingleSel() )
+ {
+ HighlightLine(oldCurrent, false);
+ }
+
+ RefreshLine(oldCurrent);
+ }
+
+ RefreshLine( m_current );
+ }
+ }
+ else // unfocus
+ {
+ // don't do anything if this item is not focused
+ if ( item == m_current )
+ {
+ ResetCurrent();
+
+ if ( IsSingleSel() )
+ {
+ // we must unselect the old current item as well or we
+ // might end up with more than one selected item in a
+ // single selection control
+ HighlightLine(oldCurrent, false);
+ }
+
+ RefreshLine( oldCurrent );
+ }
+ }
+ }
+
+ // do we need to change the selection state?
+ if ( stateMask & wxLIST_STATE_SELECTED )
+ {
+ bool on = (state & wxLIST_STATE_SELECTED) != 0;
+
+ if ( IsSingleSel() )
+ {
+ if ( on )
+ {
+ // selecting the item also makes it the focused one in the
+ // single sel mode
+ if ( m_current != item )
+ {
+ ChangeCurrent(item);
+
+ if ( oldCurrent != (size_t)-1 )
+ {
+ HighlightLine( oldCurrent, false );
+ RefreshLine( oldCurrent );
+ }
+ }
+ }
+ else // off
+ {
+ // only the current item may be selected anyhow
+ if ( item != m_current )
+ return;
+ }
+ }
+
+ if ( HighlightLine(item, on) )
+ {
+ RefreshLine(item);
+ }
+ }
+}
+
+int wxListMainWindow::GetItemState( long item, long stateMask ) const
+{
+ wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
+ wxT("invalid list ctrl item index in GetItemState()") );
+
+ int ret = wxLIST_STATE_DONTCARE;
+
+ if ( stateMask & wxLIST_STATE_FOCUSED )
+ {
+ if ( (size_t)item == m_current )
+ ret |= wxLIST_STATE_FOCUSED;
+ }
+
+ if ( stateMask & wxLIST_STATE_SELECTED )
+ {
+ if ( IsHighlighted(item) )
+ ret |= wxLIST_STATE_SELECTED;
+ }
+
+ return ret;
+}
+
+void wxListMainWindow::GetItem( wxListItem &item ) const
+{
+ wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
+ wxT("invalid item index in GetItem") );
+
+ wxListLineData *line = GetLine((size_t)item.m_itemId);
+ line->GetItem( item.m_col, item );
+
+ // Get item state if user wants it
+ if ( item.m_mask & wxLIST_MASK_STATE )
+ item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
+ wxLIST_STATE_FOCUSED );
+}
+
+// ----------------------------------------------------------------------------
+// item count
+// ----------------------------------------------------------------------------
+
+size_t wxListMainWindow::GetItemCount() const
+{
+ return IsVirtual() ? m_countVirt : m_lines.GetCount();
+}
+
+void wxListMainWindow::SetItemCount(long count)
+{
+ m_selStore.SetItemCount(count);
+ m_countVirt = count;
+
+ ResetVisibleLinesRange();
+
+ // scrollbars must be reset
+ m_dirty = true;
+}
+
+int wxListMainWindow::GetSelectedItemCount() const
+{
+ // deal with the quick case first
+ if ( IsSingleSel() )
+ return HasCurrent() ? IsHighlighted(m_current) : false;
+
+ // virtual controls remmebers all its selections itself
+ if ( IsVirtual() )
+ return m_selStore.GetSelectedCount();
+
+ // TODO: we probably should maintain the number of items selected even for
+ // non virtual controls as enumerating all lines is really slow...
+ size_t countSel = 0;
+ size_t count = GetItemCount();
+ for ( size_t line = 0; line < count; line++ )
+ {
+ if ( GetLine(line)->IsHighlighted() )
+ countSel++;
+ }
+
+ return countSel;
+}
+
+// ----------------------------------------------------------------------------
+// item position/size
+// ----------------------------------------------------------------------------
+
+wxRect wxListMainWindow::GetViewRect() const
+{
+ wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
+
+ // we need to find the longest/tallest label
+ wxCoord xMax = 0, yMax = 0;
+ const int count = GetItemCount();
+ if ( count )
+ {
+ for ( int i = 0; i < count; i++ )
+ {
+ // we need logical, not physical, coordinates here, so use
+ // GetLineRect() instead of GetItemRect()
+ wxRect r = GetLineRect(i);
+
+ wxCoord x = r.GetRight(),
+ y = r.GetBottom();
+
+ if ( x > xMax )
+ xMax = x;
+ if ( y > yMax )
+ yMax = y;
+ }
+ }
+
+ // some fudge needed to make it look prettier
+ xMax += 2 * EXTRA_BORDER_X;
+ yMax += 2 * EXTRA_BORDER_Y;
+
+ // account for the scrollbars if necessary
+ const wxSize sizeAll = GetClientSize();
+ if ( xMax > sizeAll.x )
+ yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
+ if ( yMax > sizeAll.y )
+ xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
+
+ return wxRect(0, 0, xMax, yMax);