X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/31cc5049d386913adeba034e499e3c181ad54a50..ba0cf6af6d19a7a91ed5087f20bf9b93b7a4f911:/src/msw/listctrl.cpp diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index d3ec4279ed..9684bf0e9a 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -34,6 +34,7 @@ #include "wx/intl.h" #include "wx/log.h" #include "wx/settings.h" + #include "wx/stopwatch.h" #include "wx/dcclient.h" #include "wx/textctrl.h" #endif @@ -42,6 +43,7 @@ #include "wx/vector.h" #include "wx/msw/private.h" +#include "wx/msw/private/keyboard.h" #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) #include @@ -142,7 +144,7 @@ public: // init without conversion void Init(LV_ITEM_NATIVE& item) { - wxASSERT_MSG( !m_pItem, _T("Init() called twice?") ); + wxASSERT_MSG( !m_pItem, wxT("Init() called twice?") ); m_pItem = &item; } @@ -224,86 +226,9 @@ public: wxDECLARE_NO_COPY_CLASS(wxMSWListItemData); }; -// Get the internal data structure -static wxMSWListItemData *wxGetInternalData(HWND hwnd, long itemId); -static wxMSWListItemData *wxGetInternalData(const wxListCtrl *ctl, long itemId); - - -#if wxUSE_EXTENDED_RTTI -WX_DEFINE_FLAGS( wxListCtrlStyle ) - -wxBEGIN_FLAGS( wxListCtrlStyle ) - // new style border flags, we put them first to - // use them for streaming out - wxFLAGS_MEMBER(wxBORDER_SIMPLE) - wxFLAGS_MEMBER(wxBORDER_SUNKEN) - wxFLAGS_MEMBER(wxBORDER_DOUBLE) - wxFLAGS_MEMBER(wxBORDER_RAISED) - wxFLAGS_MEMBER(wxBORDER_STATIC) - wxFLAGS_MEMBER(wxBORDER_NONE) - - // old style border flags - wxFLAGS_MEMBER(wxSIMPLE_BORDER) - wxFLAGS_MEMBER(wxSUNKEN_BORDER) - wxFLAGS_MEMBER(wxDOUBLE_BORDER) - wxFLAGS_MEMBER(wxRAISED_BORDER) - wxFLAGS_MEMBER(wxSTATIC_BORDER) - wxFLAGS_MEMBER(wxBORDER) - - // standard window styles - wxFLAGS_MEMBER(wxTAB_TRAVERSAL) - wxFLAGS_MEMBER(wxCLIP_CHILDREN) - wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) - wxFLAGS_MEMBER(wxWANTS_CHARS) - wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) - wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) - wxFLAGS_MEMBER(wxVSCROLL) - wxFLAGS_MEMBER(wxHSCROLL) - - wxFLAGS_MEMBER(wxLC_LIST) - wxFLAGS_MEMBER(wxLC_REPORT) - wxFLAGS_MEMBER(wxLC_ICON) - wxFLAGS_MEMBER(wxLC_SMALL_ICON) - wxFLAGS_MEMBER(wxLC_ALIGN_TOP) - wxFLAGS_MEMBER(wxLC_ALIGN_LEFT) - wxFLAGS_MEMBER(wxLC_AUTOARRANGE) - wxFLAGS_MEMBER(wxLC_USER_TEXT) - wxFLAGS_MEMBER(wxLC_EDIT_LABELS) - wxFLAGS_MEMBER(wxLC_NO_HEADER) - wxFLAGS_MEMBER(wxLC_SINGLE_SEL) - wxFLAGS_MEMBER(wxLC_SORT_ASCENDING) - wxFLAGS_MEMBER(wxLC_SORT_DESCENDING) - wxFLAGS_MEMBER(wxLC_VIRTUAL) - -wxEND_FLAGS( wxListCtrlStyle ) - -IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl, wxControl,"wx/listctrl.h") - -wxBEGIN_PROPERTIES_TABLE(wxListCtrl) - wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent ) - - wxPROPERTY_FLAGS( WindowStyle , wxListCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style -wxEND_PROPERTIES_TABLE() - -wxBEGIN_HANDLERS_TABLE(wxListCtrl) -wxEND_HANDLERS_TABLE() - -wxCONSTRUCTOR_5( wxListCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) - -/* - TODO : Expose more information of a list's layout etc. via appropriate objects (a la NotebookPageInfo) -*/ -#else -IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl) -#endif - -IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl) -IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject) - -IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent) - -BEGIN_EVENT_TABLE(wxListCtrl, wxControl) +BEGIN_EVENT_TABLE(wxListCtrl, wxListCtrlBase) EVT_PAINT(wxListCtrl::OnPaint) + EVT_CHAR_HOOK(wxListCtrl::OnCharHook) END_EVENT_TABLE() // ============================================================================ @@ -384,7 +309,7 @@ void wxListCtrl::MSWSetExListStyles() WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const { - WXDWORD wstyle = wxControl::MSWGetStyle(style, exstyle); + WXDWORD wstyle = wxListCtrlBase::MSWGetStyle(style, exstyle); wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS; @@ -404,7 +329,7 @@ WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const MAP_MODE_STYLE(wxLC_REPORT, LVS_REPORT) wxASSERT_MSG( nModes == 1, - _T("wxListCtrl style should have exactly one mode bit set") ); + wxT("wxListCtrl style should have exactly one mode bit set") ); #undef MAP_MODE_STYLE @@ -434,7 +359,7 @@ WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const wstyle |= LVS_SORTASCENDING; wxASSERT_MSG( !(style & wxLC_SORT_DESCENDING), - _T("can't sort in ascending and descending orders at once") ); + wxT("can't sort in ascending and descending orders at once") ); } else if ( style & wxLC_SORT_DESCENDING ) wstyle |= LVS_SORTDESCENDING; @@ -502,8 +427,7 @@ void wxListCtrl::DeleteEditControl() { m_textCtrl->UnsubclassWin(); m_textCtrl->SetHWND(0); - delete m_textCtrl; - m_textCtrl = NULL; + wxDELETE(m_textCtrl); } } @@ -554,7 +478,7 @@ void wxListCtrl::SetWindowStyleFlag(long flag) { if ( flag != m_windowStyle ) { - wxControl::SetWindowStyleFlag(flag); + wxListCtrlBase::SetWindowStyleFlag(flag); UpdateStyle(); @@ -708,7 +632,7 @@ int wxListCtrl::GetColumnIndexFromOrder(int order) const { const int numCols = GetColumnCount(); wxCHECK_MSG( order >= 0 && order < numCols, -1, - _T("Column position out of bounds") ); + wxT("Column position out of bounds") ); wxArrayInt indexArray(numCols); if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) ) @@ -720,7 +644,7 @@ int wxListCtrl::GetColumnIndexFromOrder(int order) const int wxListCtrl::GetColumnOrder(int col) const { const int numCols = GetColumnCount(); - wxASSERT_MSG( col >= 0 && col < numCols, _T("Column index out of bounds") ); + wxASSERT_MSG( col >= 0 && col < numCols, wxT("Column index out of bounds") ); wxArrayInt indexArray(numCols); if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) ) @@ -732,7 +656,7 @@ int wxListCtrl::GetColumnOrder(int col) const return pos; } - wxFAIL_MSG( _T("no column with with given order?") ); + wxFAIL_MSG( wxT("no column with with given order?") ); return -1; } @@ -755,7 +679,7 @@ bool wxListCtrl::SetColumnsOrder(const wxArrayInt& orders) const int numCols = GetColumnCount(); wxCHECK_MSG( orders.size() == (size_t)numCols, false, - _T("wrong number of elements in column orders array") ); + wxT("wrong number of elements in column orders array") ); return ListView_SetColumnOrderArray(GetHwnd(), numCols, &orders[0]) != 0; } @@ -776,7 +700,7 @@ wxTextCtrl* wxListCtrl::GetEditControl() const // first check corresponds to the case when the label editing was started // by user and hence m_textCtrl wasn't created by EditLabel() at all, while // the second case corresponds to us being called from inside EditLabel() - // (e.g. from a user wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT handler): in this + // (e.g. from a user wxEVT_LIST_BEGIN_LABEL_EDIT handler): in this // case EditLabel() did create the control but it didn't have an HWND to // initialize it with yet if ( !m_textCtrl || !m_textCtrl->GetHWND() ) @@ -850,7 +774,7 @@ bool wxListCtrl::SetItem(wxListItem& info) { const long id = info.GetId(); wxCHECK_MSG( id >= 0 && id < GetItemCount(), false, - _T("invalid item index in SetItem") ); + wxT("invalid item index in SetItem") ); LV_ITEM item; wxConvertToMSWListItem(this, info, item); @@ -899,7 +823,7 @@ bool wxListCtrl::SetItem(wxListItem& info) { if ( !ListView_SetItem(GetHwnd(), &item) ) { - wxLogDebug(_T("ListView_SetItem() failed")); + wxLogDebug(wxT("ListView_SetItem() failed")); return false; } @@ -987,7 +911,7 @@ bool wxListCtrl::SetItemState(long item, long state, long stateMask) if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE, (WPARAM)item, (LPARAM)&lvItem) ) { - wxLogLastError(_T("ListView_SetItemState")); + wxLogLastError(wxT("ListView_SetItemState")); return false; } @@ -1036,12 +960,13 @@ bool wxListCtrl::SetItemColumnImage(long item, long column, int image) } // Gets the item text -wxString wxListCtrl::GetItemText(long item) const +wxString wxListCtrl::GetItemText(long item, int col) const { wxListItem info; info.m_mask = wxLIST_MASK_TEXT; info.m_itemId = item; + info.m_col = col; if (!GetItem(info)) return wxEmptyString; @@ -1110,7 +1035,7 @@ wxRect wxListCtrl::GetViewRect() const RECT rc; if ( !ListView_GetViewRect(GetHwnd(), &rc) ) { - wxLogDebug(_T("ListView_GetViewRect() failed.")); + wxLogDebug(wxT("ListView_GetViewRect() failed.")); wxZeroMemory(rc); } @@ -1132,7 +1057,7 @@ wxRect wxListCtrl::GetViewRect() const } else { - wxFAIL_MSG( _T("not implemented in this mode") ); + wxFAIL_MSG( wxT("not implemented in this mode") ); } return rect; @@ -1151,11 +1076,11 @@ bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) // completely bogus in this case), so we check item validity ourselves wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || (subItem >= 0 && subItem < GetColumnCount()), - false, _T("invalid sub item index") ); + false, wxT("invalid sub item index") ); // use wxCHECK_MSG against "item" too, for coherency with the generic implementation: wxCHECK_MSG( item >= 0 && item < GetItemCount(), false, - _T("invalid item in GetSubItemRect") ); + wxT("invalid item in GetSubItemRect") ); int codeWin; if ( code == wxLIST_RECT_BOUNDS ) @@ -1166,7 +1091,7 @@ bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) codeWin = LVIR_LABEL; else { - wxFAIL_MSG( _T("incorrect code in GetItemRect() / GetSubItemRect()") ); + wxFAIL_MSG( wxT("incorrect code in GetItemRect() / GetSubItemRect()") ); codeWin = LVIR_BOUNDS; } @@ -1188,7 +1113,7 @@ bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) // there is no way to retrieve the first sub item bounding rectangle using // wxGetListCtrlSubItemRect() as 0 means the whole item, so we need to // truncate it at first column ourselves - if ( subItem == 0 ) + if ( subItem == 0 && code == wxLIST_RECT_BOUNDS ) rect.width = GetColumnWidth(0); return true; @@ -1408,6 +1333,29 @@ void wxListCtrl::AssignImageList(wxImageList *imageList, int which) m_ownsImageListState = true; } +// ---------------------------------------------------------------------------- +// Geometry +// ---------------------------------------------------------------------------- + +wxSize wxListCtrl::MSWGetBestViewRect(int x, int y) const +{ + const DWORD rc = ListView_ApproximateViewRect(GetHwnd(), x, y, -1); + + wxSize size(LOWORD(rc), HIWORD(rc)); + + // We have to add space for the scrollbars ourselves, they're not taken + // into account by ListView_ApproximateViewRect(), at least not with + // commctrl32.dll v6. + const DWORD mswStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE); + + if ( mswStyle & WS_HSCROLL ) + size.y += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y); + if ( mswStyle & WS_VSCROLL ) + size.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); + + return size; +} + // ---------------------------------------------------------------------------- // Operations // ---------------------------------------------------------------------------- @@ -1433,7 +1381,7 @@ bool wxListCtrl::DeleteItem(long item) { if ( !ListView_DeleteItem(GetHwnd(), (int) item) ) { - wxLogLastError(_T("ListView_DeleteItem")); + wxLogLastError(wxT("ListView_DeleteItem")); return false; } @@ -1469,7 +1417,9 @@ bool wxListCtrl::DeleteItem(long item) // Deletes all items bool wxListCtrl::DeleteAllItems() { - return ListView_DeleteAllItems(GetHwnd()) != 0; + // Calling ListView_DeleteAllItems() will always generate an event but we + // shouldn't do it if the control is empty + return !GetItemCount() || ListView_DeleteAllItems(GetHwnd()) != 0; } // Deletes all items @@ -1516,7 +1466,7 @@ void wxListCtrl::InitEditControl(WXHWND hWnd) m_textCtrl->SubclassWin(hWnd); m_textCtrl->SetParent(this); - // we must disallow TABbing away from the control while the edit contol is + // we must disallow TABbing away from the control while the edit control is // shown because this leaves it in some strange state (just try removing // this line and then pressing TAB while editing an item in listctrl // inside a panel) @@ -1525,14 +1475,14 @@ void wxListCtrl::InitEditControl(WXHWND hWnd) wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass) { - wxCHECK_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)), NULL, + wxCHECK_MSG( textControlClass->IsKindOf(wxCLASSINFO(wxTextCtrl)), NULL, "control used for label editing must be a wxTextCtrl" ); // ListView_EditLabel requires that the list has focus. SetFocus(); // create m_textCtrl here before calling ListView_EditLabel() because it - // generates wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT event from inside it and + // generates wxEVT_LIST_BEGIN_LABEL_EDIT event from inside it and // the user handler for it can call GetEditControl() resulting in an on // demand creation of a stock wxTextCtrl instead of the control of a // (possibly) custom wxClassInfo @@ -1543,8 +1493,7 @@ wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass) if ( !hWnd ) { // failed to start editing - delete m_textCtrl; - m_textCtrl = NULL; + wxDELETE(m_textCtrl); return NULL; } @@ -1565,14 +1514,15 @@ bool wxListCtrl::EndEditLabel(bool cancel) if ( !hwnd ) return false; - if ( cancel ) - ::SetWindowText(hwnd, wxEmptyString); // dubious but better than nothing + // Newer versions of Windows have a special ListView_CancelEditLabel() + // message for cancelling editing but it, rather counter-intuitively, keeps + // the last text entered in the dialog while cancelling as we do it below + // restores the original text which is the more expected behaviour. - // we shouldn't destroy the control ourselves according to MSDN, which - // proposes WM_CANCELMODE to do this, but it doesn't seem to work - // - // posting WM_CLOSE to it does seem to work without any side effects - ::PostMessage(hwnd, WM_CLOSE, 0, 0); + // We shouldn't destroy the control ourselves according to MSDN, which + // proposes WM_CANCELMODE to do this, but it doesn't seem to work so + // emulate the corresponding user action instead. + ::SendMessage(hwnd, WM_KEYDOWN, cancel ? VK_ESCAPE : VK_RETURN, 0); return true; } @@ -1592,7 +1542,7 @@ long wxListCtrl::FindItem(long start, const wxString& str, bool partial) findInfo.flags = LVFI_STRING; if ( partial ) findInfo.flags |= LVFI_PARTIAL; - findInfo.psz = str.wx_str(); + findInfo.psz = str.t_str(); // ListView_FindItem() excludes the first item from search and to look // through all the items you need to start from -1 which is unnatural and @@ -1727,7 +1677,13 @@ wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const // -1 otherwise. long wxListCtrl::InsertItem(const wxListItem& info) { - wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") ); + wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual controls") ); + + // In 2.8 it was possible to succeed inserting an item without initializing + // its ID as it defaulted to 0. This was however never supported and in 2.9 + // the ID is -1 by default and inserting it simply fails, but it might be + // not obvious why does it happen, so check it proactively. + wxASSERT_MSG( info.m_itemId != -1, wxS("Item ID must be set.") ); LV_ITEM item; wxConvertToMSWListItem(this, info, item); @@ -1758,7 +1714,10 @@ long wxListCtrl::InsertItem(const wxListItem& info) } } - long rv = ListView_InsertItem(GetHwnd(), & item); + const long rv = ListView_InsertItem(GetHwnd(), & item); + + // failing to insert the item is really unexpected + wxCHECK_MSG( rv != -1, rv, "failed to insert an item in wxListCtrl" ); m_count++; wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), @@ -1792,18 +1751,27 @@ long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex) wxListItem info; info.m_image = imageIndex; info.m_text = label; - info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT; + info.m_mask = wxLIST_MASK_TEXT; + if (imageIndex > -1) + info.m_mask |= wxLIST_MASK_IMAGE; info.m_itemId = index; return InsertItem(info); } // For list view mode (only), inserts a column. -long wxListCtrl::InsertColumn(long col, const wxListItem& item) +long wxListCtrl::DoInsertColumn(long col, const wxListItem& item) { LV_COLUMN lvCol; wxConvertToMSWListCol(GetHwnd(), col, item, lvCol); - if ( !(lvCol.mask & LVCF_WIDTH) ) + // LVSCW_AUTOSIZE_USEHEADER is not supported when inserting new column, + // we'll deal with it below instead. Plain LVSCW_AUTOSIZE is not supported + // neither but it doesn't need any special handling as we use fixed value + // for it here, both because we can't do anything else (there are no items + // with values in this column to compute the size from yet) and for + // compatibility as wxLIST_AUTOSIZE == -1 and -1 as InsertColumn() width + // parameter used to mean "arbitrary fixed width". + if ( !(lvCol.mask & LVCF_WIDTH) || lvCol.cx < 0 ) { // always give some width to the new column: this one is compatible // with the generic version @@ -1812,35 +1780,23 @@ long wxListCtrl::InsertColumn(long col, const wxListItem& item) } long n = ListView_InsertColumn(GetHwnd(), col, &lvCol); - if ( n != -1 ) - { - m_colCount++; - } - else // failed to insert? + if ( n == -1 ) { wxLogDebug(wxT("Failed to insert the column '%s' into listview!"), lvCol.pszText); + return -1; } - return n; -} + m_colCount++; -long wxListCtrl::InsertColumn(long col, - const wxString& heading, - int format, - int width) -{ - wxListItem item; - item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT; - item.m_text = heading; - if ( width > -1 ) + // Now adjust the new column size. + if ( (item.GetMask() & wxLIST_MASK_WIDTH) && + (item.GetWidth() == wxLIST_AUTOSIZE_USEHEADER) ) { - item.m_mask |= wxLIST_MASK_WIDTH; - item.m_width = width; + SetColumnWidth(n, wxLIST_AUTOSIZE_USEHEADER); } - item.m_format = format; - return InsertColumn(col, item); + return n; } // scroll the control by the given number of pixels (exception: in list view, @@ -1849,7 +1805,7 @@ bool wxListCtrl::ScrollList(int dx, int dy) { if ( !ListView_Scroll(GetHwnd(), dx, dy) ) { - wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy); + wxLogDebug(wxT("ListView_Scroll(%d, %d) failed"), dx, dy); return false; } @@ -1886,8 +1842,8 @@ int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM l wxMSWListItemData *data1 = (wxMSWListItemData *) lParam1; wxMSWListItemData *data2 = (wxMSWListItemData *) lParam2; - long d1 = (data1 == NULL ? 0 : data1->lParam); - long d2 = (data2 == NULL ? 0 : data2->lParam); + wxIntPtr d1 = (data1 == NULL ? 0 : data1->lParam); + wxIntPtr d2 = (data2 == NULL ? 0 : data2->lParam); return internalData->user_fn(d1, d2, internalData->data); @@ -1904,7 +1860,7 @@ bool wxListCtrl::SortItems(wxListCtrlCompare fn, wxIntPtr data) wxInternalDataCompareFunc, (WPARAM) &internalData) ) { - wxLogDebug(_T("ListView_SortItems() failed")); + wxLogDebug(wxT("ListView_SortItems() failed")); return false; } @@ -1926,11 +1882,11 @@ bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG* msg) // conjunction with modifiers if ( msg->wParam == VK_RETURN && !wxIsAnyModifierDown() ) { - // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED + // we need VK_RETURN to generate wxEVT_LIST_ITEM_ACTIVATED return false; } } - return wxControl::MSWShouldPreProcessMessage(msg); + return wxListCtrlBase::MSWShouldPreProcessMessage(msg); } bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id_) @@ -1938,7 +1894,7 @@ bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id_) const int id = (signed short)id_; if (cmd == EN_UPDATE) { - wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); + wxCommandEvent event(wxEVT_TEXT, id); event.SetEventObject( this ); ProcessCommand(event); return true; @@ -1968,9 +1924,8 @@ int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick) } else #endif //__WXWINCE__ - if ( !::GetCursorPos(ptClick) ) { - wxLogLastError(_T("GetCursorPos")); + wxGetCursorPosMSW(ptClick); } // we need to use listctrl coordinates for the event point so this is what @@ -1979,12 +1934,12 @@ int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick) POINT ptClickHeader = *ptClick; if ( !::ScreenToClient(nmhdr->hwndFrom, &ptClickHeader) ) { - wxLogLastError(_T("ScreenToClient(listctrl header)")); + wxLogLastError(wxT("ScreenToClient(listctrl header)")); } if ( !::ScreenToClient(::GetParent(nmhdr->hwndFrom), ptClick) ) { - wxLogLastError(_T("ScreenToClient(listctrl)")); + wxLogLastError(wxT("ScreenToClient(listctrl)")); } const int colCount = Header_GetItemCount(nmhdr->hwndFrom); @@ -2031,35 +1986,56 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) event.m_itemIndex = -1; + bool ignore = false; switch ( nmhdr->code ) { // yet another comctl32.dll bug: under NT/W2K it sends Unicode // TRACK messages even to ANSI programs: on my system I get - // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all! + // HDN_BEGINTRACKW and HDN_ENDTRACKA! // // work around is to simply catch both versions and hope that it // works (why should this message exist in ANSI and Unicode is // beyond me as it doesn't deal with strings at all...) // - // note that fr HDN_TRACK another possibility could be to use - // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when - // something other than the item width changes so we'd have to - // filter out the unwanted events then + // another problem is that HDN_TRACK is not sent at all by header + // with HDS_FULLDRAG style which is used by default by wxListCtrl + // under recent Windows versions (starting from at least XP) so we + // need to use HDN_ITEMCHANGING instead of it case HDN_BEGINTRACKA: case HDN_BEGINTRACKW: - eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG; + eventType = wxEVT_LIST_COL_BEGIN_DRAG; // fall through - case HDN_TRACKA: - case HDN_TRACKW: + case HDN_ITEMCHANGING: if ( eventType == wxEVT_NULL ) - eventType = wxEVT_COMMAND_LIST_COL_DRAGGING; + { + if ( !nmHDR->pitem || !(nmHDR->pitem->mask & HDI_WIDTH) ) + { + // something other than the width is being changed, + // ignore it + ignore = true; + break; + } + + // also ignore the events sent when the width didn't really + // change: this is not just an optimization but also gets + // rid of a useless and unexpected DRAGGING event which + // would otherwise be sent after the END_DRAG one as we get + // an HDN_ITEMCHANGING after HDN_ENDTRACK for some reason + if ( nmHDR->pitem->cxy == GetColumnWidth(nmHDR->iItem) ) + { + ignore = true; + break; + } + + eventType = wxEVT_LIST_COL_DRAGGING; + } // fall through case HDN_ENDTRACKA: case HDN_ENDTRACKW: if ( eventType == wxEVT_NULL ) - eventType = wxEVT_COMMAND_LIST_COL_END_DRAG; + eventType = wxEVT_LIST_COL_END_DRAG; event.m_item.m_width = nmHDR->pitem->cxy; event.m_col = nmHDR->iItem; @@ -2072,7 +2048,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) { POINT ptClick; - eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK; + eventType = wxEVT_LIST_COL_RIGHT_CLICK; event.m_col = wxMSWGetColumnClicked(nmhdr, &ptClick); event.m_pointDrag.x = ptClick.x; event.m_pointDrag.y = ptClick.y; @@ -2091,8 +2067,11 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) return true; default: - return wxControl::MSWOnNotify(idCtrl, lParam, result); + ignore = true; } + + if ( ignore ) + return wxListCtrlBase::MSWOnNotify(idCtrl, lParam, result); } else #endif // defined(HDN_BEGINTRACKA) @@ -2119,13 +2098,13 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) switch ( nmhdr->code ) { case LVN_BEGINRDRAG: - eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG; + eventType = wxEVT_LIST_BEGIN_RDRAG; // fall through case LVN_BEGINDRAG: if ( eventType == wxEVT_NULL ) { - eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG; + eventType = wxEVT_LIST_BEGIN_DRAG; } event.m_itemIndex = iItem; @@ -2149,7 +2128,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) item.Init(((LV_DISPINFOW *)lParam)->item); } - eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT; + eventType = wxEVT_LIST_BEGIN_LABEL_EDIT; wxConvertFromMSWListItem(GetHwnd(), event.m_item, item); event.m_itemIndex = event.m_item.m_itemId; } @@ -2179,20 +2158,20 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) event.SetEditCanceled(true); } - eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT; + eventType = wxEVT_LIST_END_LABEL_EDIT; wxConvertFromMSWListItem(NULL, event.m_item, item); event.m_itemIndex = event.m_item.m_itemId; } break; case LVN_COLUMNCLICK: - eventType = wxEVT_COMMAND_LIST_COL_CLICK; + eventType = wxEVT_LIST_COL_CLICK; event.m_itemIndex = -1; event.m_col = nmLV->iSubItem; break; case LVN_DELETEALLITEMS: - eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS; + eventType = wxEVT_LIST_DELETE_ALL_ITEMS; event.m_itemIndex = -1; break; @@ -2204,30 +2183,13 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) return false; } - eventType = wxEVT_COMMAND_LIST_DELETE_ITEM; + eventType = wxEVT_LIST_DELETE_ITEM; event.m_itemIndex = iItem; - // delete the associated internal data - if ( wxMSWListItemData *data = MSWGetItemData(iItem) ) - { - const unsigned count = m_internalData.size(); - for ( unsigned n = 0; n < count; n++ ) - { - if ( m_internalData[n] == data ) - { - m_internalData.erase(m_internalData.begin() + n); - delete data; - data = NULL; - break; - } - } - - wxASSERT_MSG( !data, "invalid internal data pointer?" ); - } break; case LVN_INSERTITEM: - eventType = wxEVT_COMMAND_LIST_INSERT_ITEM; + eventType = wxEVT_LIST_INSERT_ITEM; event.m_itemIndex = iItem; break; @@ -2253,7 +2215,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // has the focus changed? if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) ) { - eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED; + eventType = wxEVT_LIST_ITEM_FOCUSED; event.m_itemIndex = iItem; } @@ -2275,8 +2237,8 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) } eventType = stNew & LVIS_SELECTED - ? wxEVT_COMMAND_LIST_ITEM_SELECTED - : wxEVT_COMMAND_LIST_ITEM_DESELECTED; + ? wxEVT_LIST_ITEM_SELECTED + : wxEVT_LIST_ITEM_DESELECTED; } } @@ -2305,16 +2267,21 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) (wVKey == VK_RETURN || wVKey == VK_SPACE) && !wxIsAnyModifierDown() ) { - eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED; + eventType = wxEVT_LIST_ITEM_ACTIVATED; } else { - eventType = wxEVT_COMMAND_LIST_KEY_DOWN; + eventType = wxEVT_LIST_KEY_DOWN; + + event.m_code = wxMSWKeyboard::VKToWX(wVKey); - // wxCharCodeMSWToWX() returns 0 if the key is an ASCII - // value which should be used as is - int code = wxCharCodeMSWToWX(wVKey); - event.m_code = code ? code : wVKey; + if ( event.m_code == WXK_NONE ) + { + // We can't translate this to a standard key code, + // until support for Unicode key codes is added to + // wxListEvent we just ignore them. + return false; + } } event.m_itemIndex = @@ -2332,12 +2299,12 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) case NM_DBLCLK: // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do // anything else - if ( wxControl::MSWOnNotify(idCtrl, lParam, result) ) + if ( wxListCtrlBase::MSWOnNotify(idCtrl, lParam, result) ) { return true; } - // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event + // else translate it into wxEVT_LIST_ITEM_ACTIVATED event // if it happened on an item (and not on empty place) if ( iItem == -1 ) { @@ -2345,7 +2312,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) return false; } - eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED; + eventType = wxEVT_LIST_ITEM_ACTIVATED; event.m_itemIndex = iItem; event.m_item.m_text = GetItemText(iItem); event.m_item.m_data = GetItemData(iItem); @@ -2357,27 +2324,32 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) case NM_RCLICK: // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(), // don't do anything else - if ( wxControl::MSWOnNotify(idCtrl, lParam, result) ) + if ( wxListCtrlBase::MSWOnNotify(idCtrl, lParam, result) ) { return true; } - // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event + // else translate it into wxEVT_LIST_ITEM_RIGHT_CLICK event LV_HITTESTINFO lvhti; wxZeroMemory(lvhti); #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 - if(nmhdr->code == GN_CONTEXTMENU) { - lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction; - } else + if ( nmhdr->code == GN_CONTEXTMENU ) + { + lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction; + } + else #endif //__WXWINCE__ - ::GetCursorPos(&(lvhti.pt)); - ::ScreenToClient(GetHwnd(),&(lvhti.pt)); - if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 ) + { + wxGetCursorPosMSW(&(lvhti.pt)); + } + + ::ScreenToClient(GetHwnd(), &lvhti.pt); + if ( ListView_HitTest(GetHwnd(), &lvhti) != -1 ) { if ( lvhti.flags & LVHT_ONITEM ) { - eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK; + eventType = wxEVT_LIST_ITEM_RIGHT_CLICK; event.m_itemIndex = lvhti.iItem; event.m_pointDrag.x = lvhti.pt.x; event.m_pointDrag.y = lvhti.pt.y; @@ -2396,7 +2368,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) { const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam; - eventType = wxEVT_COMMAND_LIST_CACHE_HINT; + eventType = wxEVT_LIST_CACHE_HINT; // we get some really stupid cache hints like ones for // items in range 0..0 for an empty control or, after @@ -2415,12 +2387,8 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) #ifdef HAVE_NMLVFINDITEM case LVN_ODFINDITEM: - // this message is only used with the virtual list control but - // even there we don't want to always use it: in a control with - // sufficiently big number of items (defined as > 1000 here), - // accidentally pressing a key could result in hanging an - // application waiting while it performs linear search - if ( IsVirtual() && GetItemCount() <= 1000 ) + // Find an item in a (necessarily virtual) list control. + if ( IsVirtual() ) { NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam; @@ -2440,22 +2408,34 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // this is the first item we should examine, search from it // wrapping if necessary - const int startPos = pFindInfo->iStart; + int startPos = pFindInfo->iStart; const int maxPos = GetItemCount(); - wxCHECK_MSG( startPos <= maxPos, false, - _T("bad starting position in LVN_ODFINDITEM") ); - int currentPos = startPos; - do + // Check that the index is valid to ensure that our loop + // below always terminates. + if ( startPos < 0 || startPos >= maxPos ) { - // wrap to the beginning if necessary - if ( currentPos == maxPos ) + // When the last item in the control is selected, + // iStart is really set to (invalid) maxPos index so + // accept this silently. + if ( startPos != maxPos ) { - // somewhat surprizingly, LVFI_WRAP isn't set in - // flags but we still should wrap - currentPos = 0; + wxLogDebug(wxT("Ignoring invalid search start ") + wxT("position %d in list control with ") + wxT("%d items."), startPos, maxPos); } + startPos = 0; + } + + // Linear search in a control with a lot of items can take + // a long time so we limit the total time of the search to + // ensure that the program doesn't appear to hang. +#if wxUSE_STOPWATCH + wxStopWatch sw; +#endif // wxUSE_STOPWATCH + for ( int currentPos = startPos; ; ) + { // does this item begin with searchstr? if ( wxStrnicmp(searchstr, GetItemText(currentPos), len) == 0 ) @@ -2463,13 +2443,46 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) *result = currentPos; break; } - } - while ( ++currentPos != startPos ); - if ( *result == -1 ) - { - // not found - return false; + // Go to next item with wrapping if necessary. + if ( ++currentPos == maxPos ) + { + // Surprisingly, LVFI_WRAP seems to be never set in + // the flags so wrap regardless of it. + currentPos = 0; + } + + if ( currentPos == startPos ) + { + // We examined all items without finding anything. + // + // Notice that we still return true as we did + // perform the search, if we didn't do this the + // message would have been considered unhandled and + // the control seems to always select the first + // item by default in this case. + return true; + } + +#if wxUSE_STOPWATCH + // Check the time elapsed only every thousand + // iterations for performance reasons: if we did it + // more often calling wxStopWatch::Time() could take + // noticeable time on its own. + if ( !((currentPos - startPos)%1000) ) + { + // We use half a second to limit the search time + // which is about as long as we can take without + // annoying the user. + if ( sw.Time() > 500 ) + { + // As above, return true to prevent the control + // from selecting the first item by default. + return true; + } + } +#endif // wxUSE_STOPWATCH + } SetItemState(*result, @@ -2524,7 +2537,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) } if ( !processed ) - return wxControl::MSWOnNotify(idCtrl, lParam, result); + return wxListCtrlBase::MSWOnNotify(idCtrl, lParam, result); } else { @@ -2537,6 +2550,21 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) event.SetEventType(eventType); + // fill in the item before passing it to the event handler if we do have a + // valid item index and haven't filled it yet (e.g. for LVN_ITEMCHANGED) + // and we're not using a virtual control as in this case the program + // already has the data anyhow and we don't want to call GetItem() for + // potentially many items + if ( event.m_itemIndex != -1 && !event.m_item.GetMask() + && !IsVirtual() ) + { + wxListItem& item = event.m_item; + + item.SetId(event.m_itemIndex); + item.SetMask(wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_DATA); + GetItem(item); + } + bool processed = HandleWindowEvent(event); // post processing @@ -2558,6 +2586,27 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) m_count = 0; return true; + case LVN_DELETEITEM: + // Delete the associated internal data. Notice that this can be + // done only after the event has been handled as the data could be + // accessed during the handling of the event. + if ( wxMSWListItemData *data = MSWGetItemData(event.m_itemIndex) ) + { + const unsigned count = m_internalData.size(); + for ( unsigned n = 0; n < count; n++ ) + { + if ( m_internalData[n] == data ) + { + m_internalData.erase(m_internalData.begin() + n); + wxDELETE(data); + break; + } + } + + wxASSERT_MSG( !data, "invalid internal data pointer?" ); + } + break; + case LVN_ENDLABELEDITA: case LVN_ENDLABELEDITW: // logic here is inverted compared to all the other messages @@ -2612,10 +2661,21 @@ bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont, int colCount) SelectInHDC selFont(hdc, hfont); // get the rectangle to paint - int subitem = colCount ? col + 1 : col; RECT rc; - wxGetListCtrlSubItemRect(hwndList, item, subitem, LVIR_BOUNDS, rc); - rc.left += 6; + wxGetListCtrlSubItemRect(hwndList, item, col, LVIR_BOUNDS, rc); + if ( !col && colCount > 1 ) + { + // ListView_GetSubItemRect() returns the entire item rect for 0th + // subitem while we really need just the part for this column + RECT rc2; + wxGetListCtrlSubItemRect(hwndList, item, 1, LVIR_BOUNDS, rc2); + rc.right = rc2.left; + rc.left += 4; + } + else // not first subitem + { + rc.left += 6; + } // get the image and text to draw wxChar text[512]; @@ -2878,7 +2938,7 @@ void wxListCtrl::OnPaint(wxPaintEvent& event) wxPaintDC dc(this); - wxControl::OnPaint(event); + wxListCtrlBase::OnPaint(event); // Reset the device origin since it may have been set dc.SetDeviceOrigin(0, 0); @@ -2937,7 +2997,7 @@ void wxListCtrl::OnPaint(wxPaintEvent& event) numCols, &indexArray[0]) ) { - wxFAIL_MSG( _T("invalid column index array in OnPaint()") ); + wxFAIL_MSG( wxT("invalid column index array in OnPaint()") ); return; } @@ -2953,6 +3013,27 @@ void wxListCtrl::OnPaint(wxPaintEvent& event) } } +void wxListCtrl::OnCharHook(wxKeyEvent& event) +{ + if ( GetEditControl() ) + { + // We need to ensure that Escape is not stolen from the in-place editor + // by the containing dialog. + // + // Notice that we don't have to care about Enter key here as we return + // false from MSWShouldPreProcessMessage() for it. + if ( event.GetKeyCode() == WXK_ESCAPE ) + { + EndEditLabel(true /* cancel */); + + // Don't call Skip() below. + return; + } + } + + event.Skip(); +} + WXLRESULT wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) { @@ -2969,14 +3050,14 @@ wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) // because this message is propagated upwards the child-parent // chain, we get it for the right clicks on the header window but // this is confusing in wx as right clicking there already - // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event + // generates a separate wxEVT_LIST_COL_RIGHT_CLICK event // so just ignore them if ( (HWND)wParam == ListView_GetHeader(GetHwnd()) ) return 0; //else: break } - return wxControl::MSWWindowProc(nMsg, wParam, lParam); + return wxListCtrlBase::MSWWindowProc(nMsg, wParam, lParam); } // ---------------------------------------------------------------------------- @@ -2987,7 +3068,7 @@ wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) cons { // this is a pure virtual function, in fact - which is not really pure // because the controls which are not virtual don't need to implement it - wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") ); + wxFAIL_MSG( wxT("wxListCtrl::OnGetItemText not supposed to be called") ); return wxEmptyString; } @@ -3008,15 +3089,6 @@ int wxListCtrl::OnGetItemColumnImage(long item, long column) const return -1; } -wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const -{ - wxASSERT_MSG( item >= 0 && item < GetItemCount(), - _T("invalid item index in OnGetItemAttr()") ); - - // no attributes by default - return NULL; -} - wxListItemAttr *wxListCtrl::DoGetItemColumnAttr(long item, long column) const { if ( IsVirtual() ) @@ -3028,12 +3100,12 @@ wxListItemAttr *wxListCtrl::DoGetItemColumnAttr(long item, long column) const void wxListCtrl::SetItemCount(long count) { - wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); + wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") ); if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count, LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) ) { - wxLogLastError(_T("ListView_SetItemCount")); + wxLogLastError(wxT("ListView_SetItemCount")); } m_count = count; wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), @@ -3209,7 +3281,7 @@ static void wxConvertToMSWListItem(const wxListCtrl *ctrl, else { // pszText is not const, hence the cast - lvItem.pszText = (wxChar *)info.m_text.wx_str(); + lvItem.pszText = wxMSW_CONV_LPTSTR(info.m_text); if ( lvItem.pszText ) lvItem.cchTextMax = info.m_text.length(); else @@ -3230,7 +3302,7 @@ static void wxConvertToMSWListCol(HWND hwndList, if ( item.m_mask & wxLIST_MASK_TEXT ) { lvCol.mask |= LVCF_TEXT; - lvCol.pszText = (wxChar *)item.m_text.wx_str(); // cast is safe + lvCol.pszText = wxMSW_CONV_LPTSTR(item.m_text); } if ( item.m_mask & wxLIST_MASK_FORMAT )