X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/33fe475aeb7ad2bd10bcfc3e79dcfff220b9a3a9..14d776932c74f52c5fcc12f9dc2814f177a9f329:/src/generic/listctrl.cpp?ds=sidebyside diff --git a/src/generic/listctrl.cpp b/src/generic/listctrl.cpp index 1126d66902..c6a51c9524 100644 --- a/src/generic/listctrl.cpp +++ b/src/generic/listctrl.cpp @@ -23,11 +23,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "listctrl.h" - #pragma implementation "listctrlbase.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -142,9 +137,8 @@ static const int MARGIN_BETWEEN_ROWS = 6; // when autosizing the columns, add some slack static const int AUTOSIZE_COL_MARGIN = 10; -// default and minimal widths for the header columns +// default width for the header columns static const int WIDTH_COL_DEFAULT = 80; -static const int WIDTH_COL_MIN = 10; // the space between the image and the text in the report mode static const int IMAGE_MARGIN_IN_REPORT_MODE = 5; @@ -153,6 +147,25 @@ static const int IMAGE_MARGIN_IN_REPORT_MODE = 5; // private classes // ============================================================================ +//----------------------------------------------------------------------------- +// wxColWidthInfo (internal) +//----------------------------------------------------------------------------- + +struct wxColWidthInfo +{ + int nMaxWidth; + bool bNeedsUpdate; // only set to true when an item whose + // width == nMaxWidth is removed + + wxColWidthInfo(int w = 0, bool needsUpdate = false) + { + nMaxWidth = w; + bNeedsUpdate = needsUpdate; + } +}; + +WX_DEFINE_ARRAY_PTR(wxColWidthInfo *, ColWidthArray); + //----------------------------------------------------------------------------- // wxListItemData (internal) //----------------------------------------------------------------------------- @@ -271,7 +284,7 @@ private: WX_DECLARE_LIST(wxListItemData, wxListItemDataList); #include "wx/listimpl.cpp" -WX_DEFINE_LIST(wxListItemDataList); +WX_DEFINE_LIST(wxListItemDataList) class wxListLineData { @@ -294,7 +307,7 @@ public: // the part to be highlighted wxRect m_rectHighlight; - // extend all our rects to be centered inside theo ne of given width + // extend all our rects to be centered inside the one of given width void ExtendWidth(wxCoord w) { wxASSERT_MSG( m_rectAll.width <= w, @@ -348,7 +361,7 @@ public: void GetItem( int index, wxListItem &info ); wxString GetText(int index) const; - void SetText( int index, const wxString s ); + void SetText( int index, const wxString& s ); wxListItemAttr *GetAttr() const; void SetAttr(wxListItemAttr *attr); @@ -399,7 +412,7 @@ private: WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData, wxListLineDataArray); #include "wx/arrimpl.cpp" -WX_DEFINE_OBJARRAY(wxListLineDataArray); +WX_DEFINE_OBJARRAY(wxListLineDataArray) //----------------------------------------------------------------------------- // wxListHeaderWindow (internal) @@ -452,7 +465,7 @@ private: // generate and process the list event of the given type, return true if // it wasn't vetoed, i.e. if we should proceed - bool SendListEvent(wxEventType type, wxPoint pos); + bool SendListEvent(wxEventType type, const wxPoint& pos); DECLARE_DYNAMIC_CLASS(wxListHeaderWindow) DECLARE_EVENT_TABLE() @@ -481,6 +494,8 @@ class WXDLLEXPORT wxListTextCtrl: public wxTextCtrl public: wxListTextCtrl(wxListMainWindow *owner, size_t itemEdit); + void AcceptChangesAndFinish(); + protected: void OnChar( wxKeyEvent &event ); void OnKeyUp( wxKeyEvent &event ); @@ -505,7 +520,7 @@ private: WX_DECLARE_LIST(wxListHeaderData, wxListHeaderDataList); #include "wx/listimpl.cpp" -WX_DEFINE_LIST(wxListHeaderDataList); +WX_DEFINE_LIST(wxListHeaderDataList) class wxListMainWindow : public wxScrolledWindow { @@ -655,6 +670,7 @@ public: wxString GetItemText(long item) const { wxListItem info; + info.m_mask = wxLIST_MASK_TEXT; info.m_itemId = item; GetItem( info ); return info.m_text; @@ -687,6 +703,7 @@ public: long HitTest( int x, int y, int &flags ); void InsertItem( wxListItem &item ); void InsertColumn( long col, wxListItem &item ); + int GetItemWidthWithImage(wxListItem * item); void SortItems( wxListCtrlCompare fn, long data ); size_t GetItemCount() const; @@ -701,7 +718,7 @@ public: // send out a wxListEvent void SendNotify( size_t line, wxEventType command, - wxPoint point = wxDefaultPosition ); + const wxPoint& point = wxDefaultPosition ); // override base class virtual to reset m_lineHeight when the font changes virtual bool SetFont(const wxFont& font) @@ -768,12 +785,15 @@ public: bool m_isCreated; int m_dragCount; wxPoint m_dragStart; + ColWidthArray m_aColWidths; // for double click logic size_t m_lineLastClicked, m_lineBeforeLastClicked, m_lineSelectSingleOnUp; + wxListTextCtrl* m_textctrl; + protected: // the total count of items in a virtual list control size_t m_countVirt; @@ -816,11 +836,7 @@ protected: // get the colour to be used for drawing the rules wxColour GetRuleColour() const { -#ifdef __WXMAC__ - return *wxWHITE; -#else return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); -#endif } private: @@ -980,9 +996,19 @@ int wxListItemData::GetHeight() const void wxListItemData::GetItem( wxListItem &info ) const { - info.m_text = m_text; - info.m_image = m_image; - info.m_data = m_data; + long mask = info.m_mask; + if ( !mask ) + { + // by default, get everything for backwards compatibility + mask = -1; + } + + if ( mask & wxLIST_MASK_TEXT ) + info.m_text = m_text; + if ( mask & wxLIST_MASK_IMAGE ) + info.m_image = m_image; + if ( mask & wxLIST_MASK_DATA ) + info.m_data = m_data; if ( m_attr ) { @@ -1052,11 +1078,7 @@ void wxListHeaderData::SetHeight( int h ) void wxListHeaderData::SetWidth( int w ) { - m_width = w; - if (m_width < 0) - m_width = WIDTH_COL_DEFAULT; - else if (m_width < WIDTH_COL_MIN) - m_width = WIDTH_COL_MIN; + m_width = w < 0 ? WIDTH_COL_DEFAULT : w; } void wxListHeaderData::SetFormat( int format ) @@ -1342,7 +1364,7 @@ wxString wxListLineData::GetText(int index) const return s; } -void wxListLineData::SetText( int index, const wxString s ) +void wxListLineData::SetText( int index, const wxString& s ) { wxListItemDataList::compatibility_iterator node = m_items.Item( index ); if (node) @@ -1911,7 +1933,7 @@ void wxListHeaderWindow::OnMouse( wxMouseEvent &event ) // end of the current column int xpos = 0; - // find the column where this event occured + // find the column where this event occurred int col, countCol = m_owner->GetColumnCount(); for (col = 0; col < countCol; col++) @@ -1986,7 +2008,7 @@ void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) ) m_owner->Update(); } -bool wxListHeaderWindow::SendListEvent(wxEventType type, wxPoint pos) +bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos) { wxWindow *parent = GetParent(); wxListEvent le( type, parent->GetId() ); @@ -2050,7 +2072,8 @@ void wxListTextCtrl::Finish() if ( !m_finished ) { wxPendingDelete.Append(this); - + m_owner->m_textctrl = NULL; + m_finished = true; m_owner->SetFocusIgnoringChildren(); @@ -2079,16 +2102,21 @@ bool wxListTextCtrl::AcceptChanges() return true; } +void wxListTextCtrl::AcceptChangesAndFinish() +{ + m_aboutToFinish = true; + // Notify the owner about the changes + AcceptChanges(); + // Even if vetoed, close the control (consistent with MSW) + Finish(); +} + void wxListTextCtrl::OnChar( wxKeyEvent &event ) { switch ( event.m_keyCode ) { case WXK_RETURN: - m_aboutToFinish = true; - // Notify the owner about the changes - AcceptChanges(); - // Even if vetoed, close the control (consistent with MSW) - Finish(); + AcceptChangesAndFinish(); break; case WXK_ESCAPE: @@ -2126,7 +2154,7 @@ void wxListTextCtrl::OnKeyUp( wxKeyEvent &event ) void wxListTextCtrl::OnKillFocus( wxFocusEvent &event ) { - if ( !m_finished ) + if ( !m_finished && !m_aboutToFinish ) { // We must finish regardless of success, otherwise we'll get // focus problems: @@ -2180,7 +2208,8 @@ void wxListMainWindow::Init() m_lastOnSame = false; m_renameTimer = new wxListRenameTimer( this ); - + m_textctrl = NULL; + m_current = m_lineLastClicked = m_lineSelectSingleOnUp = @@ -2226,9 +2255,6 @@ wxListMainWindow::wxListMainWindow( wxWindow *parent, wxSOLID ); - wxSize sz = size; - sz.y = 25; - SetScrollbars( 0, 0, 0, 0, 0, 0 ); wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes(); @@ -2242,6 +2268,7 @@ wxListMainWindow::~wxListMainWindow() { DoDeleteAllItems(); WX_CLEAR_LIST(wxListHeaderDataList, m_columns); + WX_CLEAR_ARRAY(m_aColWidths); delete m_highlightBrush; delete m_highlightUnfocusedBrush; @@ -2773,7 +2800,7 @@ void wxListMainWindow::HighlightAll( bool on ) void wxListMainWindow::SendNotify( size_t line, wxEventType command, - wxPoint point ) + const wxPoint& point ) { wxListEvent le( command, GetParent()->GetId() ); le.SetEventObject( GetParent() ); @@ -2831,9 +2858,8 @@ void wxListMainWindow::EditLabel( long item ) if ( m_dirty ) wxSafeYield(); - wxListTextCtrl *text = new wxListTextCtrl(this, itemEdit); - - text->SetFocus(); + m_textctrl = new wxListTextCtrl(this, itemEdit); + m_textctrl->SetFocus(); } void wxListMainWindow::OnRenameTimer() @@ -2878,6 +2904,17 @@ void wxListMainWindow::OnRenameCancelled(size_t itemEdit) 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_textctrl) + { + m_textctrl->AcceptChangesAndFinish(); + } +#endif + event.SetEventObject( GetParent() ); if ( GetParent()->GetEventHandler()->ProcessEvent( event) ) return; @@ -2992,11 +3029,11 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event ) HighlightAll( false ); ReverseHighlight(m_lineSelectSingleOnUp); } - else if (m_lastOnSame) + if (m_lastOnSame) { if ((current == m_current) && (hitResult == wxLIST_HITTEST_ONITEMLABEL) && - HasFlag(wxLC_EDIT_LABELS) ) + HasFlag(wxLC_EDIT_LABELS) ) { m_renameTimer->Start( 100, true ); } @@ -3006,7 +3043,7 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event ) } else { - // This is neccessary , because after a DnD operation in + // 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. @@ -3039,8 +3076,10 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event ) m_lineLastClicked = current; size_t oldCurrent = m_current; + bool oldWasSelected = IsHighlighted(m_current); + bool cmdModifierDown = event.CmdDown(); - if ( !(cmdModifierDown || event.ShiftDown()) ) + if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) ) { if( IsSingleSel() || !IsHighlighted(current) ) { @@ -3092,7 +3131,7 @@ void wxListMainWindow::OnMouse( wxMouseEvent &event ) } // forceClick is only set if the previous click was on another item - m_lastOnSame = !forceClick && (m_current == oldCurrent); + m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected; } } @@ -3548,34 +3587,30 @@ void wxListMainWindow::SetColumnWidth( int col, int width ) int max = AUTOSIZE_COL_MARGIN; - for ( size_t i = 0; i < count; i++ ) + // if the cached column width isn't valid then recalculate it + if (m_aColWidths.Item(col)->bNeedsUpdate) { - wxListLineData *line = GetLine(i); - wxListItemDataList::compatibility_iterator n = line->m_items.Item( col ); + for (size_t i = 0; i < count; i++) + { + wxListLineData *line = GetLine(i); + wxListItemDataList::compatibility_iterator n = line->m_items.Item( col ); - wxCHECK_RET( n, _T("no subitem?") ); + wxCHECK_RET( n, _T("no subitem?") ); - wxListItemData *item = n->GetData(); - int current = 0; + wxListItemData *itemData = n->GetData(); + wxListItem item; - if (item->HasImage()) - { - int ix, iy; - GetImageSize( item->GetImage(), ix, iy ); - current += ix + 5; + itemData->GetItem(item); + int itemWidth = GetItemWidthWithImage(&item); + if (itemWidth > max) + max = itemWidth; } - if (item->HasText()) - { - wxCoord w; - dc.GetTextExtent( item->GetText(), &w, NULL ); - current += w; - } - - if (current > max) - max = current; + m_aColWidths.Item(col)->bNeedsUpdate = false; + m_aColWidths.Item(col)->nMaxWidth = max; } + max = m_aColWidths.Item(col)->nMaxWidth; width = max + AUTOSIZE_COL_MARGIN; } } @@ -3634,6 +3669,19 @@ void wxListMainWindow::SetItem( wxListItem &item ) { 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 @@ -3694,8 +3742,8 @@ void wxListMainWindow::SetItemState( long litem, long state, long stateMask ) return; } - wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(), - _T("invalid list ctrl item index in SetItem") ); + wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(), + _T("invalid list ctrl item index in SetItem") ); size_t oldCurrent = m_current; size_t item = (size_t)litem; // safe because of the check above @@ -3809,6 +3857,11 @@ void wxListMainWindow::GetItem( wxListItem &item ) const 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 ); } // ---------------------------------------------------------------------------- @@ -4222,6 +4275,26 @@ void wxListMainWindow::DeleteItem( long lindex ) if ( InReportView() ) { + // mark the Column Max Width cache as dirty if the items in the line + // we're deleting contain the Max Column Width + wxListLineData * const line = GetLine(index); + wxListItemDataList::compatibility_iterator n; + wxListItemData *itemData; + wxListItem item; + int itemWidth; + + for (size_t i = 0; i < m_columns.GetCount(); i++) + { + n = line->m_items.Item( i ); + itemData = n->GetData(); + itemData->GetItem(item); + + itemWidth = GetItemWidthWithImage(&item); + + if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth) + m_aColWidths.Item(i)->bNeedsUpdate = true; + } + ResetVisibleLinesRange(); } @@ -4266,6 +4339,12 @@ void wxListMainWindow::DeleteColumn( int col ) } } + if ( InReportView() ) // we only cache max widths when in Report View + { + delete m_aColWidths.Item(col); + m_aColWidths.RemoveAt(col); + } + // invalidate it as it has to be recalculated m_headerWidth = 0; } @@ -4299,6 +4378,10 @@ void wxListMainWindow::DoDeleteAllItems() if ( InReportView() ) { ResetVisibleLinesRange(); + for (size_t i = 0; i < m_aColWidths.GetCount(); i++) + { + m_aColWidths.Item(i)->bNeedsUpdate = true; + } } m_lines.Clear(); @@ -4314,6 +4397,7 @@ void wxListMainWindow::DeleteAllItems() void wxListMainWindow::DeleteEverything() { WX_CLEAR_LIST(wxListHeaderDataList, m_columns); + WX_CLEAR_ARRAY(m_aColWidths); DeleteAllItems(); } @@ -4439,42 +4523,16 @@ void wxListMainWindow::InsertItem( wxListItem &item ) m_dirty = true; - #if 0 - // this is unused variable - int mode = 0; - #endif if ( InReportView() ) { - #if 0 - // this is unused variable - mode = wxLC_REPORT; - #endif ResetVisibleLinesRange(); - } - else if ( HasFlag(wxLC_LIST) ) - #if 0 - // this is unused variable - mode = wxLC_LIST; - #else - {} - #endif - else if ( HasFlag(wxLC_ICON) ) - #if 0 - // this is unused variable - mode = wxLC_ICON; - #else - {} - #endif - else if ( HasFlag(wxLC_SMALL_ICON) ) - #if 0 - // this is unused variable - mode = wxLC_ICON; // no typo - #else - {} - #endif - else - { - wxFAIL_MSG( _T("unknown mode") ); + + // calculate the width of the item and adjust the max column width + wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn()); + int width = GetItemWidthWithImage(&item); + item.SetWidth(width); + if (width > pWidthInfo->nMaxWidth) + pWidthInfo->nMaxWidth = width; } wxListLineData *line = new wxListLineData(this); @@ -4507,15 +4565,20 @@ void wxListMainWindow::InsertColumn( long col, wxListItem &item ) item.m_width = GetTextLength( item.m_text ); wxListHeaderData *column = new wxListHeaderData( item ); + wxColWidthInfo *colWidthInfo = new wxColWidthInfo(); + bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount()); if ( insert ) { - wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col ); + wxListHeaderDataList::compatibility_iterator + node = m_columns.Item( col ); m_columns.Insert( node, column ); + m_aColWidths.Insert( colWidthInfo, col ); } else { m_columns.Append( column ); + m_aColWidths.Add( colWidthInfo ); } if ( !IsVirtual() ) @@ -4537,6 +4600,30 @@ void wxListMainWindow::InsertColumn( long col, wxListItem &item ) } } +int wxListMainWindow::GetItemWidthWithImage(wxListItem * item) +{ + int width = 0; + wxClientDC dc(this); + + dc.SetFont( GetFont() ); + + if (item->GetImage() != -1) + { + int ix, iy; + GetImageSize( item->GetImage(), ix, iy ); + width += ix + 5; + } + + if (!item->GetText().empty()) + { + wxCoord w; + dc.GetTextExtent( item->GetText(), &w, NULL ); + width += w; + } + + return width; +} + // ---------------------------------------------------------------------------- // sorting // ---------------------------------------------------------------------------- @@ -4575,7 +4662,7 @@ void wxListMainWindow::OnScroll(wxScrollWinEvent& event) ResetVisibleLinesRange(); // FIXME -#if defined(__WXGTK__) && !defined(__WXUNIVERSAL__) +#if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__) wxScrolledWindow::OnScroll(event); #else HandleOnScroll( event ); @@ -4940,6 +5027,7 @@ void wxGenericListCtrl::SetItemText( long item, const wxString& str ) wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const { wxListItem info; + info.m_mask = wxLIST_MASK_DATA; info.m_itemId = item; m_mainWin->GetItem( info ); return info.m_data; @@ -5038,6 +5126,22 @@ wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const return info.GetBackgroundColour(); } +void wxGenericListCtrl::SetItemFont( long item, const wxFont &f ) +{ + wxListItem info; + info.m_itemId = item; + info.SetFont( f ); + m_mainWin->SetItem( info ); +} + +wxFont wxGenericListCtrl::GetItemFont( long item ) const +{ + wxListItem info; + info.m_itemId = item; + m_mainWin->GetItem( info ); + return info.GetFont(); +} + int wxGenericListCtrl::GetSelectedItemCount() const { return m_mainWin->GetSelectedItemCount();